+ if(node2->group_id >= 0) { nb++; }
+ }
+ }
+
+ nodes = safe_malloc(nb * sizeof(struct file_node *));
+
+ n = 0;
+ for(node1 = list1; node1; node1 = node1->next) {
+ if(node1->group_id >= 0) {
+ nodes[n++] = node1;
+ }
+ }
+
+ if(list2) {
+ for(node2 = list2; node2; node2 = node2->next) {
+ if(node2->group_id >= 0) {
+ nodes[n++] = node2;
+ }
+ }
+ }
+
+ 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) {
+ printf("\n");
+ }
+ print_file(nodes[n]);
+ }
+
+ free(nodes);
+}
+
+void print_progress(int max, int n, int *pp) {
+ int p, k;
+ int width;
+ if(show_progress && tty_width > 0) {
+ width = tty_width - 7;
+ p = (width * n) / (max - 1);
+ if(p > *pp) {
+ for(k = 0; k < p; k++) {
+ fprintf(stderr, "+");
+ }
+ for(; k < width; k++) {
+ fprintf(stderr, "-");
+ }
+ *pp = p;
+ p = (100 * n) / (max - 1);
+ fprintf(stderr, " [%3d%%]\r", p);
+ }
+ }
+}
+
+void start(const char *dirname1, const char *dirname2) {
+ struct file_node *list1, *list2;
+ struct file_node *node1, *node2;
+ 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);
+
+ 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);
+ }
+
+ list1 = scan_directory(0, dirname1);
+
+ if(dirname2) {
+ if(strncmp(dirname2, "not:", 4) == 0) {
+ not_in = 1;
+ /* groups are not computed in the not: mode */
+ show_groups = 0;
+ dirname2 += 4;
+ } else if(strncmp(dirname2, "and:", 4) == 0) {
+ dirname2 += 4;
+ }
+ if(show_progress) {
+ fprintf(stderr, "%s ... ", dirname2);
+ }
+ list2 = scan_directory(0, dirname2);
+ } else {
+ list2 = list1;
+ }
+
+ if(show_progress) {
+ fprintf(stderr, "done.\n");
+ }
+
+ nb_groups = 0;
+ previous_progress = -1;
+ nb_nodes = 0;
+ list1_length = file_list_length(list1);
+
+ if(not_in) {
+ for(node1 = list1; node1; node1 = node1->next) {
+ print_progress(list1_length, nb_nodes, &previous_progress);
+ nb_nodes++;
+
+ found = 0;
+
+ for(node2 = list2; !found && node2; node2 = node2->next) {
+ if(same_files(node1, node2, buffer1, buffer2)) {
+ found = 1;
+ }
+ }
+
+ if(!found) {
+ if(show_realpaths) {
+ printf("%s\n", realpath(node1->name, 0));
+ } else {
+ printf("%s\n", node1->name);
+ }
+ }
+ }
+
+ } else {
+ for(node1 = list1; node1; node1 = node1->next) {
+ print_progress(list1_length, nb_nodes, &previous_progress);
+ nb_nodes++;
+
+ for(node2 = list2; node2; node2 = node2->next) {
+ if(node1->group_id < 0 || node2->group_id < 0) {
+ if(same_files(node1, node2, buffer1, buffer2)) {
+ if(node1->group_id < 0) {
+ if(node2->group_id >= 0) {
+ node1->group_id = node2->group_id;
+ } else {
+ node1->group_id = nb_groups;
+ node1->dir_id = 1;
+ nb_groups++;
+ }
+ }
+ if(node2->group_id < 0) {
+ node2->group_id = node1->group_id;
+ node2->dir_id = 2;
+ }
+ }
+ }