+void print_file(struct file_node *node) {
+ char tmp[PATH_MAX + 1];
+ if(show_realpaths) {
+ if(realpath(node->name, tmp)) {
+ if(show_groups) {
+ printf("%d %s\n", node->group_id, tmp);
+ } else {
+ printf("%s\n", tmp);
+ }
+ } else {
+ printf("Can not get the realpath of \"%s\": %s\n",
+ node->name,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ if(show_groups) {
+ printf("%d %s\n", node->group_id, node->name);
+ } else {
+ printf("%s\n", node->name);
+ }
+ }
+}
+
+int compare_nodes(const void *x1, const void *x2) {
+ const struct file_node **f1, **f2;
+
+ f1 = (const struct file_node **) x1;
+ f2 = (const struct file_node **) x2;
+
+ if((*f1)->group_id < (*f2)->group_id) {
+ return -1;
+ } else if((*f1)->group_id > (*f2)->group_id) {
+ return 1;
+ } else {
+ if((*f1)->dir_id < (*f2)->dir_id) {
+ return -1;
+ } else if((*f1)->dir_id > (*f2)->dir_id) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+}
+
+
+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;
+ for(node1 = list1; node1; node1 = node1->next) {
+ if(node1->group_id >= 0) { nb++; }
+ }
+
+ if(list2) {
+ for(node2 = list2; node2; node2 = node2->next) {
+ 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, tty_width;
+ struct winsize win;
+
+ if(show_progress &&
+ isatty(STDOUT_FILENO) &&
+ !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
+ tty_width = win.ws_col;
+ 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);
+ }
+ }
+}
+