X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=finddup.c;h=f1e3cf0f007327d1d0ab56eac803f334348a0c73;hb=f34775663d2b9b3a3cea95b0954e2e62dad1410e;hp=bca9c5c3ebfd63262cb47d1e022faf6b9fbcb5a4;hpb=630c461cbdb6610420863dcfe7af247d62bf004e;p=finddup.git diff --git a/finddup.c b/finddup.c index bca9c5c..f1e3cf0 100644 --- a/finddup.c +++ b/finddup.c @@ -112,7 +112,7 @@ struct file_with_size { size_t size; ino_t inode; struct file_with_size *next; - int group_id; + int group_id, dir_id; }; void file_list_delete(struct file_with_size *head) { @@ -160,7 +160,7 @@ int same_content(struct file_with_size *f1, struct file_with_size *f2) { close(fd2); return 1; } else { - if(strncmp(buffer1, buffer2, s1)) { + if(memcmp(buffer1, buffer2, s1)) { close(fd1); close(fd2); return 0; @@ -237,6 +237,7 @@ struct file_with_size *scan_directory(struct file_with_size *tail, tmp->size = sb.st_size; tmp->inode = sb.st_ino; tmp->group_id = -1; + tmp->dir_id = -1; tail = tmp; } } @@ -276,7 +277,13 @@ int compare_nodes(const void *x1, const void *x2) { } else if((*f1)->group_id > (*f2)->group_id) { return 1; } else { - return 0; + if((*f1)->dir_id < (*f2)->dir_id) { + return -1; + } else if((*f1)->dir_id > (*f2)->dir_id) { + return 1; + } else { + return 0; + } } } @@ -291,7 +298,7 @@ void print_result(struct file_with_size *list1, struct file_with_size *list2) { if(node1->group_id >= 0) { nb++; } } - if(show_hits) { + if(list2) { for(node2 = list2; node2; node2 = node2->next) { if(node2->group_id >= 0) { nb++; } } @@ -306,7 +313,7 @@ void print_result(struct file_with_size *list1, struct file_with_size *list2) { } } - if(show_hits) { + if(list2) { for(node2 = list2; node2; node2 = node2->next) { if(node2->group_id >= 0) { nodes[n++] = node2; @@ -317,6 +324,9 @@ void print_result(struct file_with_size *list1, struct file_with_size *list2) { qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes); for(n = 0; n < nb; n++) { + if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) { + printf("\n"); + } print_file(nodes[n]); } @@ -340,6 +350,9 @@ void start(const char *dirname1, const char *dirname2) { if(dirname2) { if(strncmp(dirname2, "not:", 4) == 0) { not_in = 1; + /* We should show groups even in that mode. However they are not + properly calculated for now, so we force it off. */ + show_groups = 0; dirname2 += 4; } if(show_progress) { @@ -406,11 +419,13 @@ void start(const char *dirname1, const char *dirname2) { node1->group_id = node2->group_id; } else { node1->group_id = k; + node1->dir_id = 1; k++; } } if(node2->group_id < 0) { node2->group_id = node1->group_id; + node2->dir_id = 2; } } } @@ -418,12 +433,13 @@ void start(const char *dirname1, const char *dirname2) { } } - print_result(list1, list2); - - file_list_delete(list1); - if(dirname2) { + print_result(list1, list2); + file_list_delete(list1); file_list_delete(list2); + } else { + print_result(list1, 0); + file_list_delete(list1); } } @@ -455,7 +471,7 @@ int main(int argc, char **argv) { setlocale (LC_ALL, ""); while (1) { - c = getopt(argc, argv, "hrcgd0p"); + c = getopt(argc, argv, "hircgd0p"); if (c == -1) break;