X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=finddup.c;h=e36c22981a3839c3fe9cb2bd78c624667a79622d;hb=bb7ec484aa23fe0ee2dc0b0b915eb7a7f496898a;hp=5dacc63481c752e79d7d28f087bea2e06fe8600b;hpb=78603b4d25cbe3325e9e48f4eba407d21e8a5adf;p=finddup.git diff --git a/finddup.c b/finddup.c index 5dacc63..e36c229 100644 --- a/finddup.c +++ b/finddup.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -105,7 +106,7 @@ struct file_with_size { size_t size; ino_t inode; struct file_with_size *next; - int id; + int group_id; }; void file_list_delete(struct file_with_size *head) { @@ -196,7 +197,7 @@ struct file_with_size *scan_directory(struct file_with_size *tail, struct dirent *dir_e; struct stat sb; struct file_with_size *tmp; - char subname[PATH_MAX]; + char subname[PATH_MAX + 1]; if(lstat(name, &sb) != 0) { fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno)); @@ -224,7 +225,7 @@ struct file_with_size *scan_directory(struct file_with_size *tail, tmp->filename = strdup(name); tmp->size = sb.st_size; tmp->inode = sb.st_ino; - tmp->id = -1; + tmp->group_id = -1; tail = tmp; } } @@ -233,21 +234,82 @@ struct file_with_size *scan_directory(struct file_with_size *tail, } void print_file(struct file_with_size *node) { + char tmp[PATH_MAX + 1]; if(show_realpaths) { if(show_groups) { - printf("%d %s\n", node->id, realpath(node->filename, 0)); + realpath(node->filename, tmp); + printf("%d %s\n", node->group_id, tmp); } else { - printf("%s\n", realpath(node->filename, 0)); + realpath(node->filename, tmp); + printf("%s\n", tmp); } } else { if(show_groups) { - printf("%d %s\n", node->id, node->filename); + printf("%d %s\n", node->group_id, node->filename); } else { printf("%s\n", node->filename); } } } +int compare_nodes(const void *x1, const void *x2) { + const struct file_with_size **f1, **f2; + + f1 = (const struct file_with_size **) x1; + f2 = (const struct file_with_size **) x2; + + if((*f1)->group_id < (*f2)->group_id) { + return -1; + } else if((*f1)->group_id > (*f2)->group_id) { + return 1; + } else { + return 0; + } +} + + +void print_result(struct file_with_size *list1, struct file_with_size *list2) { + struct file_with_size *node1, *node2; + struct file_with_size **nodes; + int nb, n; + + nb = 0; + for(node1 = list1; node1; node1 = node1->next) { + if(node1->group_id >= 0) { nb++; } + } + + if(show_hits) { + for(node2 = list2; node2; node2 = node2->next) { + if(node2->group_id >= 0) { nb++; } + } + } + + nodes = safe_malloc(nb * sizeof(struct file_with_size *)); + + n = 0; + for(node1 = list1; node1; node1 = node1->next) { + if(node1->group_id >= 0) { + nodes[n++] = node1; + } + } + + if(show_hits) { + for(node2 = list2; node2; node2 = node2->next) { + if(node2->group_id >= 0) { + nodes[n++] = node2; + } + } + } + + qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes); + + for(n = 0; n < nb; n++) { + print_file(nodes[n]); + } + + free(nodes); +} + void start(const char *dirname1, const char *dirname2) { struct file_with_size *list1, *list2; struct file_with_size *node1, *node2; @@ -339,26 +401,24 @@ void start(const char *dirname1, const char *dirname2) { for(node2 = list2; node2; node2 = node2->next) { if(node1->inode != node2->inode && same_files(node1, node2)) { - if(node1->id < 0) { - if(node2->id >= 0) { - node1->id = node2->id; + if(node1->group_id < 0) { + if(node2->group_id >= 0) { + node1->group_id = node2->group_id; } else { - node1->id = k; + node1->group_id = k; k++; } - print_file(node1); } - if(node2->id < 0) { - node2->id = node1->id; - if(show_hits) { - print_file(node2); - } + if(node2->group_id < 0) { + node2->group_id = node1->group_id; } } } } } + print_result(list1, list2); + file_list_delete(list1); if(!same_dir) { file_list_delete(list2);