Sfoglia il codice sorgente

Amélioration myls

Loquicom 6 anni fa
parent
commit
b68d26fe0b
1 ha cambiato i file con 19 aggiunte e 9 eliminazioni
  1. 19 9
      myls.c

+ 19 - 9
myls.c

@@ -22,14 +22,21 @@ void printls(char* path, char* filename){
         struct group* grp;
         struct passwd* user;
         struct tm* date;
-        //Création chemin vers le fichier
-        length = strlen(path) + strlen(filename) + 2;
-        completePath = malloc(sizeof(char) * length);
-        memset(completePath, 0, length);
-        if(path[strlen(path)-1] != '/'){
-            snprintf(completePath, length, "%s/%s", path, filename);
+        //Si le nom est en 2 partie
+        if(filename != NULL){
+            //Création chemin vers le fichier
+            length = strlen(path) + strlen(filename) + 2;
+            completePath = malloc(sizeof(char) * length);
+            memset(completePath, 0, length);
+            if(path[strlen(path)-1] != '/'){
+                snprintf(completePath, length, "%s/%s", path, filename);
+            } else {
+                 snprintf(completePath, length, "%s%s", path, filename);
+            }
         } else {
-             snprintf(completePath, length, "%s%s", path, filename);
+            filename = path;
+            completePath = path;
+            path = NULL;
         }
         
         //Recup info fichier
@@ -37,6 +44,9 @@ void printls(char* path, char* filename){
             addperror("Erreur stat");
             return;
         }
+        if(path != NULL){
+            free(completePath);
+        }
 
         //Calcul permission
         memset(permission, 0, 11);
@@ -123,7 +133,7 @@ void printls(char* path, char* filename){
         }
 
         //Affiche
-        printf("%s 1 %s %s %s %d %d:%d %ld  ", permission, user->pw_name, grp->gr_name, mois, date->tm_mday, date->tm_hour, date->tm_min, info.st_size);
+        printf("%s X %s %s %ld %s  %d %d:%d ", permission, user->pw_name, grp->gr_name, info.st_size, mois, date->tm_mday, date->tm_hour, date->tm_min);
 
         //color the name
         if(permission[0] == 'd'){
@@ -239,7 +249,7 @@ int main(int argc, char* argv[]){
                 displayed = true;
             }
             else{
-                //printls(argv[i]);
+                printls(argv[i], NULL);
                 displayed = true;
             }
         }