Przeglądaj źródła

Ajout total et nombre lien myls

Loquicom 6 lat temu
rodzic
commit
9f8b7bd5b5
1 zmienionych plików z 31 dodań i 2 usunięć
  1. 31 2
      myls.c

+ 31 - 2
myls.c

@@ -13,6 +13,34 @@
 #include "color.h"
 #include "constante.h"
 
+int get_total(char* path, struct dirent** dir, int nb, boolean hidden){
+    struct stat info;
+    char* completePath;
+    int length, total = 0, i = 0;
+    //Si besoins passe les fichiers cachés
+    while(i < nb && !hidden && *dir[i]->d_name == '.') i++;
+    //Parcours tous les fichiers
+    for(; i < nb; i++){
+        //Création chemin vers le dossier
+        length = strlen(path) + strlen(dir[i]->d_name) + 2;
+        completePath = malloc(sizeof(char) * length);
+        memset(completePath, 0, length);
+        if(path[strlen(path)-1] != '/'){
+            snprintf(completePath, length, "%s/%s", path, dir[i]->d_name);
+        } else {
+            snprintf(completePath, length, "%s%s", path, dir[i]->d_name);
+        }
+        //Recup info fichier
+        if(stat(completePath, &info) == ERR){
+            free(completePath);
+            continue;
+        }
+        //Ajoute au total
+        total += info.st_blocks;
+    }
+    return total / 2;
+}
+
 /**
  * Affiche sous la forme ls -l les infos d'un fichier/dossier
  * @param char* Le chemin vers le fichier (avec ou sans le nom du fichier)
@@ -159,7 +187,7 @@ void printls(char* path, char* filename){
         }
 
         //Affiche
-        printf("%s X %s %s %ld %s  %d %s:%s ", permission, user->pw_name, grp->gr_name, info.st_size, mois, date->tm_mday, heure, minute);
+        printf("%s %ld %s %s %ld %s  %d %s:%s ", permission, info.st_nlink, user->pw_name, grp->gr_name, info.st_size, mois, date->tm_mday, heure, minute);
 
         //color the name
         if(permission[0] == 'd'){
@@ -192,6 +220,7 @@ void printdir(char* path, boolean subdir, boolean hidden){
     if(subdir){
         printf("%s :\n", path);
     }
+    printf("total %d\n", get_total(path, contentsDir, nbFile, hidden));
     //Si besoins passe les fichiers cachés
     while(j < nbFile && !hidden && *contentsDir[j]->d_name == '.') j++;
     //Parcours les fichiers du dossier
@@ -221,7 +250,7 @@ void printdir(char* path, boolean subdir, boolean hidden){
             if(path[strlen(path)-1] != '/'){
                 snprintf(completePath, length, "%s/%s", path, contentsDir[j]->d_name);
             } else {
-                 snprintf(completePath, length, "%s%s", path, contentsDir[j]->d_name);
+                snprintf(completePath, length, "%s%s", path, contentsDir[j]->d_name);
             }
             //Recup info fichier
             if(stat(completePath, &info) == ERR){