Cosmetics.
authorFrancois Fleuret <francois@fleuret.org>
Wed, 17 Mar 2010 07:00:19 +0000 (08:00 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Wed, 17 Mar 2010 07:00:19 +0000 (08:00 +0100)
finddup.c

index 9c71321..a888bee 100644 (file)
--- a/finddup.c
+++ b/finddup.c
@@ -23,7 +23,7 @@
  *
  */
 
  *
  */
 
-#define VERSION_NUMBER "0.7"
+#define VERSION_NUMBER "0.8"
 
 #define _BSD_SOURCE
 
 
 #define _BSD_SOURCE
 
@@ -95,25 +95,26 @@ int ignore_entry(const char *name) {
 
 /**********************************************************************/
 
 
 /**********************************************************************/
 
-struct file_with_size {
-  char *filename;
+struct file_node {
+  struct file_node *next;
+  char *name;
   size_t size;
   ino_t inode;
   size_t size;
   ino_t inode;
-  struct file_with_size *next;
-  int group_id, dir_id;
+  int group_id; /* one per identical file content */
+  int dir_id; /* 1 for DIR1, and 2 for DIR2 */
 };
 
 };
 
-void file_list_delete(struct file_with_size *head) {
-  struct file_with_size *next;
+void file_list_delete(struct file_node *head) {
+  struct file_node *next;
   while(head) {
     next = head->next;
   while(head) {
     next = head->next;
-    free(head->filename);
+    free(head->name);
     free(head);
     head = next;
   }
 }
 
     free(head);
     head = next;
   }
 }
 
-int file_list_length(struct file_with_size *head) {
+int file_list_length(struct file_node *head) {
   int l = 0;
   while(head) {
     l++;
   int l = 0;
   while(head) {
     l++;
@@ -124,12 +125,12 @@ int file_list_length(struct file_with_size *head) {
 
 /**********************************************************************/
 
 
 /**********************************************************************/
 
-int same_content(struct file_with_size *f1, struct file_with_size *f2,
+int same_content(struct file_node *f1, struct file_node *f2,
                  char *buffer1, char *buffer2) {
   int fd1, fd2, s1, s2;
 
                  char *buffer1, char *buffer2) {
   int fd1, fd2, s1, s2;
 
-  fd1 = open(f1->filename, O_RDONLY);
-  fd2 = open(f2->filename, O_RDONLY);
+  fd1 = open(f1->name, O_RDONLY);
+  fd2 = open(f2->name, O_RDONLY);
 
   if(fd1 >= 0 && fd2 >= 0) {
     while(1) {
 
   if(fd1 >= 0 && fd2 >= 0) {
     while(1) {
@@ -165,21 +166,21 @@ int same_content(struct file_with_size *f1, struct file_with_size *f2,
     if(fd1 < 0) {
       fprintf(stderr,
               "Can not open \"%s\" error: %s\n",
     if(fd1 < 0) {
       fprintf(stderr,
               "Can not open \"%s\" error: %s\n",
-              f1->filename,
+              f1->name,
               strerror(errno));
     }
 
     if(fd2 < 0) {
       fprintf(stderr,
               "Can not open \"%s\" error: %s\n",
               strerror(errno));
     }
 
     if(fd2 < 0) {
       fprintf(stderr,
               "Can not open \"%s\" error: %s\n",
-              f2->filename,
+              f2->name,
               strerror(errno));
     }
     exit(EXIT_FAILURE);
   }
 }
 
               strerror(errno));
     }
     exit(EXIT_FAILURE);
   }
 }
 
-int same_files(struct file_with_size *f1, struct file_with_size *f2,
+int same_files(struct file_node *f1, struct file_node *f2,
                char *buffer1, char *buffer2) {
   if(ignore_same_inode && f1->inode == f2->inode) {
     return 0;
                char *buffer1, char *buffer2) {
   if(ignore_same_inode && f1->inode == f2->inode) {
     return 0;
@@ -189,12 +190,12 @@ int same_files(struct file_with_size *f1, struct file_with_size *f2,
 
 /**********************************************************************/
 
 
 /**********************************************************************/
 
-struct file_with_size *scan_directory(struct file_with_size *tail,
+struct file_node *scan_directory(struct file_node *tail,
                                       const char *name) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat sb;
                                       const char *name) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat sb;
-  struct file_with_size *tmp;
+  struct file_node *tmp;
   char subname[PATH_MAX + 1];
 
   if(lstat(name, &sb) != 0) {
   char subname[PATH_MAX + 1];
 
   if(lstat(name, &sb) != 0) {
@@ -220,9 +221,9 @@ struct file_with_size *scan_directory(struct file_with_size *tail,
     if(S_ISREG(sb.st_mode)) {
       if(!ignore_entry(name)) {
         if(!ignore_empty_files || sb.st_size > 0) {
     if(S_ISREG(sb.st_mode)) {
       if(!ignore_entry(name)) {
         if(!ignore_empty_files || sb.st_size > 0) {
-          tmp = safe_malloc(sizeof(struct file_with_size));
+          tmp = safe_malloc(sizeof(struct file_node));
           tmp->next = tail;
           tmp->next = tail;
-          tmp->filename = strdup(name);
+          tmp->name = strdup(name);
           tmp->size = sb.st_size;
           tmp->inode = sb.st_ino;
           tmp->group_id = -1;
           tmp->size = sb.st_size;
           tmp->inode = sb.st_ino;
           tmp->group_id = -1;
@@ -236,10 +237,10 @@ struct file_with_size *scan_directory(struct file_with_size *tail,
   return tail;
 }
 
   return tail;
 }
 
-void print_file(struct file_with_size *node) {
+void print_file(struct file_node *node) {
   char tmp[PATH_MAX + 1];
   if(show_realpaths) {
   char tmp[PATH_MAX + 1];
   if(show_realpaths) {
-    if(realpath(node->filename, tmp)) {
+    if(realpath(node->name, tmp)) {
       if(show_groups) {
         printf("%d %s\n", node->group_id, tmp);
       } else {
       if(show_groups) {
         printf("%d %s\n", node->group_id, tmp);
       } else {
@@ -247,24 +248,24 @@ void print_file(struct file_with_size *node) {
       }
     } else {
       printf("Can not get the realpath of \"%s\": %s\n",
       }
     } else {
       printf("Can not get the realpath of \"%s\": %s\n",
-             node->filename,
+             node->name,
              strerror(errno));
       exit(EXIT_FAILURE);
     }
   } else {
     if(show_groups) {
              strerror(errno));
       exit(EXIT_FAILURE);
     }
   } else {
     if(show_groups) {
-      printf("%d %s\n", node->group_id, node->filename);
+      printf("%d %s\n", node->group_id, node->name);
     } else {
     } else {
-      printf("%s\n", node->filename);
+      printf("%s\n", node->name);
     }
   }
 }
 
 int compare_nodes(const void *x1, const void *x2) {
     }
   }
 }
 
 int compare_nodes(const void *x1, const void *x2) {
-  const struct file_with_size **f1, **f2;
+  const struct file_node **f1, **f2;
 
 
-  f1 = (const struct file_with_size **) x1;
-  f2 = (const struct file_with_size **) x2;
+  f1 = (const struct file_node **) x1;
+  f2 = (const struct file_node **) x2;
 
   if((*f1)->group_id < (*f2)->group_id) {
     return -1;
 
   if((*f1)->group_id < (*f2)->group_id) {
     return -1;
@@ -282,9 +283,9 @@ int compare_nodes(const void *x1, const void *x2) {
 }
 
 
 }
 
 
-void print_result(struct file_with_size *list1, struct file_with_size *list2) {
-  struct file_with_size *node1, *node2;
-  struct file_with_size **nodes;
+void print_result(struct file_node *list1, struct file_node *list2) {
+  struct file_node *node1, *node2;
+  struct file_node **nodes;
   int nb, n;
 
   nb = 0;
   int nb, n;
 
   nb = 0;
@@ -298,7 +299,7 @@ void print_result(struct file_with_size *list1, struct file_with_size *list2) {
     }
   }
 
     }
   }
 
-  nodes = safe_malloc(nb * sizeof(struct file_with_size *));
+  nodes = safe_malloc(nb * sizeof(struct file_node *));
 
   n = 0;
   for(node1 = list1; node1; node1 = node1->next) {
 
   n = 0;
   for(node1 = list1; node1; node1 = node1->next) {
@@ -315,7 +316,7 @@ void print_result(struct file_with_size *list1, struct file_with_size *list2) {
     }
   }
 
     }
   }
 
-  qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
+  qsort(nodes, nb, sizeof(struct file_node *), compare_nodes);
 
   for(n = 0; n < nb; n++) {
     if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
 
   for(n = 0; n < nb; n++) {
     if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
@@ -348,11 +349,12 @@ void print_progress(int max, int n, int *pp) {
 }
 
 void start(const char *dirname1, const char *dirname2) {
 }
 
 void start(const char *dirname1, const char *dirname2) {
-  struct file_with_size *list1, *list2;
-  struct file_with_size *node1, *node2;
+  struct file_node *list1, *list2;
+  struct file_node *node1, *node2;
   int not_in, found;
   int nb_groups, nb_nodes;
   int list1_length, previous_progress;
   int not_in, found;
   int nb_groups, nb_nodes;
   int list1_length, previous_progress;
+  struct winsize win;
 
   char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
   char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
 
   char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
   char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
@@ -360,6 +362,10 @@ void start(const char *dirname1, const char *dirname2) {
   not_in = 0;
 
   if(show_progress) {
   not_in = 0;
 
   if(show_progress) {
+    if(isatty(STDOUT_FILENO) &&
+       !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
+      tty_width = win.ws_col;
+    }
     fprintf(stderr, "Scanning %s ... ", dirname1);
   }
 
     fprintf(stderr, "Scanning %s ... ", dirname1);
   }
 
@@ -406,9 +412,9 @@ void start(const char *dirname1, const char *dirname2) {
 
       if(!found) {
         if(show_realpaths) {
 
       if(!found) {
         if(show_realpaths) {
-          printf("%s\n", realpath(node1->filename, 0));
+          printf("%s\n", realpath(node1->name, 0));
         } else {
         } else {
-          printf("%s\n", node1->filename);
+          printf("%s\n", node1->name);
         }
       }
     }
         }
       }
     }
@@ -484,34 +490,25 @@ void print_help(FILE *out) {
 
 /**********************************************************************/
 
 
 /**********************************************************************/
 
+static struct option long_options[] = {
+  { "help", no_argument, 0, 'h' },
+  { "same-inodes-are-different", no_argument, 0, 'i' },
+  { "real-paths", no_argument, 0, 'r' },
+  { "hide-matchings", no_argument, 0, 'c' },
+  { "no-group-ids", no_argument, 0, 'g' },
+  { "ignore-dots", no_argument, 0, 'd' },
+  { "ignore-empty", no_argument, 0, '0' },
+  { "show-progress", no_argument, 0, 'p' },
+  { 0, 0, 0, 0 }
+};
+
 int main(int argc, char **argv) {
   int c;
 int main(int argc, char **argv) {
   int c;
-  struct file_with_size *root;
-  struct winsize win;
-
-  root = 0;
 
   setlocale (LC_ALL, "");
 
 
   setlocale (LC_ALL, "");
 
-  while (1) {
-    int option_index = 0;
-    static struct option long_options[] = {
-      { "help", no_argument, 0, 'h' },
-      { "same-inodes-are-different", no_argument, 0, 'i' },
-      { "real-paths", no_argument, 0, 'r' },
-      { "hide-matchings", no_argument, 0, 'c' },
-      { "no-group-ids", no_argument, 0, 'g' },
-      { "ignore-dots", no_argument, 0, 'd' },
-      { "ignore-empty", no_argument, 0, '0' },
-      { "show-progress", no_argument, 0, 'p' },
-      { 0, 0, 0, 0 }
-    };
-
-    c = getopt_long(argc, argv, "hircgd0p",
-                    long_options, &option_index);
-    if (c == -1)
-      break;
-
+  while ((c = getopt_long(argc, argv, "hircgd0p",
+                          long_options, NULL)) != -1) {
     switch (c) {
 
     case 'h':
     switch (c) {
 
     case 'h':
@@ -553,12 +550,6 @@ int main(int argc, char **argv) {
     }
   }
 
     }
   }
 
-  if(show_progress &&
-     isatty(STDOUT_FILENO) &&
-     !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
-    tty_width = win.ws_col;
-  }
-
   if(optind + 2 == argc) {
     start(argv[optind], argv[optind + 1]);
   } else if(optind + 1 == argc) {
   if(optind + 2 == argc) {
     start(argv[optind], argv[optind + 1]);
   } else if(optind + 1 == argc) {