Added an option to see progress.
[finddup.git] / finddup.c
index 7a20257..5dacc63 100644 (file)
--- a/finddup.c
+++ b/finddup.c
@@ -52,7 +52,7 @@ int ignore_dotfiles = 0; /* 1 means ignore files and directories
 int show_realpaths = 0; /* 1 means ignore files and directories
                             starting with a dot */
 
-int show_progress = 1; /* 1 means show a progress bar when we are in a
+int show_progress = 0; /* 1 means show a progress bar when we are in a
                           tty */
 
 int show_hits = 1; /* 1 means to show the files from dir2
@@ -253,6 +253,7 @@ void start(const char *dirname1, const char *dirname2) {
   struct file_with_size *node1, *node2;
   struct stat sb1, sb2;
   int not_in, found, same_dir;
+  int k, p, pp, l1, n;
 
   if(strncmp(dirname2, "not:", 4) == 0) {
     not_in = 1;
@@ -273,18 +274,41 @@ void start(const char *dirname1, const char *dirname2) {
 
   same_dir = (sb1.st_ino == sb2.st_ino);
 
-  fprintf(stderr, "Scanning %s ... ", dirname1);
+  if(show_progress) {
+    fprintf(stderr, "Scanning %s ... ", dirname1);
+  }
+
   list1 = scan_directory(0, dirname1);
+
   if(same_dir) {
     list2 = list1;
   } else {
-    fprintf(stderr, "%s ... ", dirname2);
+    if(show_progress) {
+      fprintf(stderr, "%s ... ", dirname2);
+    }
     list2 = scan_directory(0, dirname2);
   }
-  fprintf(stderr, "done.\n");
+
+  if(show_progress) {
+    fprintf(stderr, "done.\n");
+  }
+
+  k = 0;
+  pp = -1;
+  n = 0;
+  l1 = file_list_length(list1);
 
   if(not_in) {
     for(node1 = list1; node1; node1 = node1->next) {
+      if(show_progress) {
+        p = (100 * n)/l1;
+        if(p > pp) {
+          fprintf(stderr, "%d%%\n", p);
+          pp = p;
+        }
+        n++;
+      }
+
       found = 0;
 
       for(node2 = list2; !found && node2; node2 = node2->next) {
@@ -303,9 +327,16 @@ void start(const char *dirname1, const char *dirname2) {
     }
 
   } else {
-    int k = 0;
-
     for(node1 = list1; node1; node1 = node1->next) {
+      if(show_progress) {
+        p = (100 * n)/l1;
+        if(p > pp) {
+          fprintf(stderr, "%d%%\n", p);
+          pp = p;
+        }
+        n++;
+      }
+
       for(node2 = list2; node2; node2 = node2->next) {
         if(node1->inode != node2->inode && same_files(node1, node2)) {
           if(node1->id < 0) {
@@ -343,6 +374,7 @@ void print_help(FILE *out) {
   fprintf(out, "   -d   ignore dot files and directories\n");
   fprintf(out, "   -c   do not show which files in DIR2 corresponds to those in DIR1\n");
   fprintf(out, "   -g   do not show the file groups\n");
+  fprintf(out, "   -p   show progress\n");
   fprintf(out, "   -r   show the real file paths\n");
   fprintf(out, "\n");
   fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
@@ -359,7 +391,7 @@ int main(int argc, char **argv) {
   setlocale (LC_ALL, "");
 
   while (1) {
-    c = getopt(argc, argv, "hrcgd");
+    c = getopt(argc, argv, "hrcgdp");
     if (c == -1)
       break;
 
@@ -383,6 +415,10 @@ int main(int argc, char **argv) {
       show_groups = 0;
       break;
 
+    case 'p':
+      show_progress = 1;
+      break;
+
     case 'c':
       show_hits = 0;
       break;