int file_size_cmp(void *ptr1, void *ptr2) { t_file *f1; t_file *f2; f1 = (t_file *)ptr1; f2 = (t_file *)ptr2; return (f2->stats.st_size - f1->stats.st_size ? f2->stats.st_size - f1->stats.st_size : file_name_cmp(ptr1, ptr2)); }
static int compare_names (char const *name1, char const *name2) { if (locale_specific_sorting) { int diff = compare_collated (name1, name2); if (diff || ignore_file_name_case) return diff; } return file_name_cmp (name1, name2); }
static int compare_names_for_qsort (void const *file1, void const *file2) { char const *const *f1 = file1; char const *const *f2 = file2; char const *name1 = *f1; char const *name2 = *f2; if (locale_specific_sorting) { int diff = compare_collated (name1, name2); if (diff) return diff; } return file_name_cmp (name1, name2); }
char * find_dir_file_pathname (char const *dir, char const *file) { /* The 'IF_LINT (volatile)' works around what appears to be a bug in gcc 4.8.0 20120825; see <http://lists.gnu.org/archive/html/bug-diffutils/2012-08/msg00007.html>. */ char const * IF_LINT (volatile) match = file; char *val; struct dirdata dirdata; dirdata.names = NULL; dirdata.data = NULL; if (ignore_file_name_case) { struct file_data filedata; filedata.name = dir; filedata.desc = 0; if (dir_read (&filedata, &dirdata)) { locale_specific_sorting = true; if (setjmp (failed_locale_specific_sorting)) match = file; /* longjmp may mess up MATCH. */ else { for (char const **p = dirdata.names; *p; p++) if (compare_names (*p, file) == 0) { if (file_name_cmp (*p, file) == 0) { match = *p; break; } if (match == file) match = *p; } } } } val = file_name_concat (dir, match, NULL); free (dirdata.names); free (dirdata.data); return val; }
static int compare_names (char const *name1, char const *name2) { if (locale_specific_sorting) { int r; errno = 0; if (ignore_file_name_case) r = strcasecoll (name1, name2); else r = strcoll (name1, name2); if (errno) { error (0, errno, _("cannot compare file names `%s' and `%s'"), name1, name2); longjmp (failed_locale_specific_sorting, 1); } return r; } return (ignore_file_name_case ? strcasecmp (name1, name2) : file_name_cmp (name1, name2)); }
int diff_dirs (struct comparison const *cmp, int (*handle_file) (struct comparison const *, char const *, char const *)) { struct dirdata dirdata[2]; int volatile val = EXIT_SUCCESS; int i; if ((cmp->file[0].desc == -1 || dir_loop (cmp, 0)) && (cmp->file[1].desc == -1 || dir_loop (cmp, 1))) { error (0, 0, _("%s: recursive directory loop"), cmp->file[cmp->file[0].desc == -1].name); return EXIT_TROUBLE; } /* Get contents of both dirs. */ for (i = 0; i < 2; i++) if (! dir_read (&cmp->file[i], &dirdata[i])) { perror_with_name (cmp->file[i].name); val = EXIT_TROUBLE; } if (val == EXIT_SUCCESS) { char const **volatile names[2]; names[0] = dirdata[0].names; names[1] = dirdata[1].names; /* Use locale-specific sorting if possible, else native byte order. */ locale_specific_sorting = true; if (setjmp (failed_locale_specific_sorting)) locale_specific_sorting = false; /* Sort the directories. */ for (i = 0; i < 2; i++) qsort (names[i], dirdata[i].nnames, sizeof *dirdata[i].names, compare_names_for_qsort); /* If '-S name' was given, and this is the topmost level of comparison, ignore all file names less than the specified starting name. */ if (starting_file && ! cmp->parent) { while (*names[0] && compare_names (*names[0], starting_file) < 0) names[0]++; while (*names[1] && compare_names (*names[1], starting_file) < 0) names[1]++; } /* Loop while files remain in one or both dirs. */ while (*names[0] || *names[1]) { /* Compare next name in dir 0 with next name in dir 1. At the end of a dir, pretend the "next name" in that dir is very large. */ int nameorder = (!*names[0] ? 1 : !*names[1] ? -1 : compare_names (*names[0], *names[1])); /* Prefer a file_name_cmp match if available. This algorithm is O(N**2), where N is the number of names in a directory that compare_names says are all equal, but in practice N is so small it's not worth tuning. */ if (nameorder == 0 && ignore_file_name_case) { int raw_order = file_name_cmp (*names[0], *names[1]); if (raw_order != 0) { int greater_side = raw_order < 0; int lesser_side = 1 - greater_side; char const **lesser = names[lesser_side]; char const *greater_name = *names[greater_side]; char const **p; for (p = lesser + 1; *p && compare_names (*p, greater_name) == 0; p++) { int c = file_name_cmp (*p, greater_name); if (0 <= c) { if (c == 0) { memmove (lesser + 1, lesser, (char *) p - (char *) lesser); *lesser = greater_name; } break; } } } } int v1 = (*handle_file) (cmp, 0 < nameorder ? 0 : *names[0]++, nameorder < 0 ? 0 : *names[1]++); if (val < v1) val = v1; } } for (i = 0; i < 2; i++) { free (dirdata[i].names); free (dirdata[i].data); } return val; }