MaxenceBacquet před 6 roky
rodič
revize
c044029987
1 změnil soubory, kde provedl 15 přidání a 25 odebrání
  1. 15 25
      myls.c

+ 15 - 25
myls.c

@@ -14,7 +14,8 @@ int printDir(struct dirent** contentsDir, char* path, int nbFile,
                     boolean hiddenFile){
 
     struct stat info;
-    char permission[10] = "---------";
+    char permission[11] = "---------";
+    char completePath[1024];
     int j = 0;
 
     //enleve les fichiers caches
@@ -22,31 +23,21 @@ int printDir(struct dirent** contentsDir, char* path, int nbFile,
 
     //Affiche les fichiers
     while(j < nbFile){
-        if(stat(contentsDir[j]->d_name, &info) == ERR){
+        path[strlen(path)-1] != '/' ? sprintf(completePath, "%s/%s", path, contentsDir[j]->d_name) : sprintf(completePath, "%s%s", path, contentsDir[j]->d_name);
+        if(stat(completePath, &info) == ERR){
             addperror("Erreur stat");
             return -1;
         }
-        /*switch(info.st_mode){
-            case S_IRUSR:
-                permission[1] = 'r';
-            case S_IWUSR:
-                permission[2] = 'w';
-            case S_IXUSR:
-                permission[3] = 'x';
-            case S_IRGRP:
-                permission[4] = 'r';
-            case S_IWGRP:
-                permission[5] = 'w';
-            case S_IXGRP:
-                permission[6] = 'x';
-            case S_IROTH:
-                permission[7] = 'r';
-            case S_IWOTH:
-                permission[8] = 'w';
-            case S_IXOTH:
-                permission[9] = 'x';
-        }*/
-        printf("%s  %d  %ld  %s\n", permission, info.st_mode, info.st_size, contentsDir[j]->d_name);
+        info.st_mode & S_IRUSR ? (permission[1] = 'r') : (permission[1] = '-');
+        info.st_mode & S_IWUSR ? (permission[2] = 'w') : (permission[2] = '-');
+        info.st_mode & S_IXUSR ? (permission[3] = 'x') : (permission[3] = '-');
+        info.st_mode & S_IRGRP ? (permission[4] = 'r') : (permission[4] = '-');
+        info.st_mode & S_IWGRP ? (permission[5] = 'w') : (permission[5] = '-');
+        info.st_mode & S_IXGRP ? (permission[6] = 'x') : (permission[6] = '-');
+        info.st_mode & S_IROTH ? (permission[7] = 'r') : (permission[7] = '-');
+        info.st_mode & S_IWOTH ? (permission[8] = 'w') : (permission[8] = '-');
+        info.st_mode & S_IXOTH ? (permission[9] = 'x') : (permission[9] = '-');
+        printf("%s  %ld  %s\n", permission, info.st_size, contentsDir[j]->d_name);
         j++;
     }
     printf("\n");
@@ -82,8 +73,7 @@ void ls(int argc, char* argv[]){
                 return;
             }
             if(S_ISDIR(info.st_mode)){
-                nbFile = scandir(argv[i], &contentsDir, 0, alphasort);
-                if (nbFile < 0) {
+                if((nbFile = scandir(argv[i], &contentsDir, 0, alphasort)) == ERR){
                     addperror("Erreur scandir()");
                     return;
                 }