1 /* vi: set sw=4 ts=4: */
3 * Mini diff implementation for busybox, adapted from OpenBSD diff.
5 * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com>
6 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
8 * Sponsored in part by the Defense Advanced Research Projects
9 * Agency (DARPA) and Air Force Research Laboratory, Air Force
10 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 #define FSIZE_MAX 32768
22 #define D_HEADER 1 /* Print a header/footer between files */
23 #define D_EMPTY1 2 /* Treat first file as empty (/dev/null) */
24 #define D_EMPTY2 4 /* Treat second file as empty (/dev/null) */
27 * Status values for print_status() and diffreg() return values
29 * D_SAME - files are the same
30 * D_DIFFER - files differ
31 * D_BINARY - binary files differ
32 * D_COMMON - subdirectory common to both dirs
33 * D_ONLY - file only exists in one dir
34 * D_MISMATCH1 - path1 a dir, path2 a file
35 * D_MISMATCH2 - path1 a file, path2 a dir
36 * D_ERROR - error occurred
37 * D_SKIPPED1 - skipped path1 as it is a special file
38 * D_SKIPPED2 - skipped path2 as it is a special file
42 #define D_DIFFER (1<<0)
43 #define D_BINARY (1<<1)
44 #define D_COMMON (1<<2)
46 #define D_MISMATCH1 (1<<4)
47 #define D_MISMATCH2 (1<<5)
48 #define D_ERROR (1<<6)
49 #define D_SKIPPED1 (1<<7)
50 #define D_SKIPPED2 (1<<8)
52 /* Command line options */
63 #define FLAG_t (1<<10)
64 #define FLAG_T (1<<11)
65 #define FLAG_U (1<<12)
66 #define FLAG_w (1<<13)
80 * The following struct is used to record change information
81 * doing a "context" or "unified" diff. (see routine "change" to
82 * understand the highly mnemonic field names)
85 int a; /* start line in old file */
86 int b; /* end line in old file */
87 int c; /* start line in new file */
88 int d; /* end line in new file */
92 USE_FEATURE_DIFF_DIR(char **dl;)
93 USE_FEATURE_DIFF_DIR(int dl_count;)
94 /* This is the default number of lines of context. */
101 struct line *file[2];
102 int *J; /* will be overlaid on class */
103 int *class; /* will be overlaid on file[0] */
104 int *klist; /* will be overlaid on file[0] after class */
105 int *member; /* will be overlaid on file[1] */
108 int pref, suff; /* length of prefix and suffix */
111 long *ixnew; /* will be overlaid on file[1] */
112 long *ixold; /* will be overlaid on klist */
113 struct cand *clist; /* merely a free storage pot for candidates */
114 int clistlen; /* the length of clist */
115 struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
116 struct context_vec *context_vec_start;
117 struct context_vec *context_vec_end;
118 struct context_vec *context_vec_ptr;
119 struct stat stb1, stb2;
121 #define G (*ptr_to_globals)
123 #define dl_count (G.dl_count )
124 #define context (G.context )
125 #define max_context (G.max_context )
126 #define status (G.status )
127 #define start (G.start )
128 #define label1 (G.label1 )
129 #define label2 (G.label2 )
130 #define file (G.file )
132 #define class (G.class )
133 #define klist (G.klist )
134 #define member (G.member )
135 #define clen (G.clen )
137 #define pref (G.pref )
138 #define suff (G.suff )
139 #define slen (G.slen )
140 #define anychange (G.anychange )
141 #define ixnew (G.ixnew )
142 #define ixold (G.ixold )
143 #define clist (G.clist )
144 #define clistlen (G.clistlen )
145 #define sfile (G.sfile )
146 #define context_vec_start (G.context_vec_start )
147 #define context_vec_end (G.context_vec_end )
148 #define context_vec_ptr (G.context_vec_ptr )
149 #define stb1 (G.stb1 )
150 #define stb2 (G.stb2 )
151 #define INIT_G() do { \
152 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
158 static void print_only(const char *path, size_t dirlen, const char *entry)
162 printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
165 static void print_status(int val, char *path1, char *path2, char *entry)
167 const char * const _entry = entry ? entry : "";
168 char * const _path1 = entry ? concat_path_file(path1, _entry) : path1;
169 char * const _path2 = entry ? concat_path_file(path2, _entry) : path2;
173 print_only(path1, strlen(path1), entry);
176 printf("Common subdirectories: %s and %s\n", _path1, _path2);
179 printf("Binary files %s and %s differ\n", _path1, _path2);
182 if (option_mask32 & FLAG_q)
183 printf("Files %s and %s differ\n", _path1, _path2);
186 if (option_mask32 & FLAG_s)
187 printf("Files %s and %s are identical\n", _path1, _path2);
190 printf("File %s is a %s while file %s is a %s\n",
191 _path1, "directory", _path2, "regular file");
194 printf("File %s is a %s while file %s is a %s\n",
195 _path1, "regular file", _path2, "directory");
198 printf("File %s is not a regular file or directory and was skipped\n",
202 printf("File %s is not a regular file or directory and was skipped\n",
211 static void fiddle_sum(int *sum, int t)
213 *sum = (int)(*sum * 127 + t);
216 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
218 static int readhash(FILE * f)
225 if (!(option_mask32 & (FLAG_b | FLAG_w))) {
226 for (i = 0; (t = getc(f)) != '\n'; i++) {
236 switch (t = getc(f)) {
245 if (space && !(option_mask32 & FLAG_w)) {
263 * There is a remote possibility that we end up with a zero sum.
264 * Zero is used as an EOF marker, so return 1 instead.
266 return (sum == 0 ? 1 : sum);
271 * Check to see if the given files differ.
272 * Returns 0 if they are the same, 1 if different, and -1 on error.
274 static int files_differ(FILE * f1, FILE * f2, int flags)
278 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size
279 || (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)
284 i = fread(bb_common_bufsiz1, 1, BUFSIZ/2, f1);
285 j = fread(bb_common_bufsiz1 + BUFSIZ/2, 1, BUFSIZ/2, f2);
289 return (ferror(f1) || ferror(f2));
290 if (memcmp(bb_common_bufsiz1,
291 bb_common_bufsiz1 + BUFSIZ/2, i) != 0)
297 static void prepare(int i, FILE * fd, off_t filesize)
305 sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;
309 p = xmalloc((sz + 3) * sizeof(struct line));
311 while ((h = readhash(fd))) {
314 p = xrealloc(p, (sz + 3) * sizeof(struct line));
323 static void prune(void)
327 for (pref = 0; pref < len[0] && pref < len[1] &&
328 file[0][pref + 1].value == file[1][pref + 1].value; pref++)
330 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
331 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
334 for (j = 0; j < 2; j++) {
335 sfile[j] = file[j] + pref;
336 slen[j] = len[j] - pref - suff;
337 for (i = 0; i <= slen[j]; i++)
338 sfile[j][i].serial = i;
343 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
348 while (i <= n && j <= m) {
349 if (a[i].value < b[j].value)
351 else if (a[i].value == b[j].value)
362 while (b[j + 1].value == b[j].value) {
371 static int isqrt(int n)
383 } while ((x - y) > 1 || (x - y) < -1);
389 static int newcand(int x, int y, int pred)
393 if (clen == clistlen) {
394 clistlen = clistlen * 11 / 10;
395 clist = xrealloc(clist, clistlen * sizeof(struct cand));
405 static int search(int *c, int k, int y)
409 if (clist[c[k]].y < y) /* quick look for typical case */
429 static int stone(int *a, int n, int *b, int *c)
433 unsigned int numtries;
435 #if ENABLE_FEATURE_DIFF_MINIMAL
436 const unsigned int bound =
437 (option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
439 const unsigned int bound = MAX(256, isqrt(n));
442 c[0] = newcand(0, 0, 0);
443 for (i = 1; i <= n; i++) {
452 if (y <= clist[oldc].y)
458 if (clist[c[l]].y <= y)
461 c[l] = newcand(i, y, oldc);
466 c[l] = newcand(i, y, oldc);
470 } while ((y = b[++j]) > 0 && numtries < bound);
476 static void unravel(int p)
481 for (i = 0; i <= len[0]; i++)
482 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0;
483 for (q = clist + p; q->y != 0; q = clist + q->pred)
484 J[q->x + pref] = q->y + pref;
488 static void unsort(struct line *f, int l, int *b)
492 a = xmalloc((l + 1) * sizeof(int));
493 for (i = 1; i <= l; i++)
494 a[f[i].serial] = f[i].value;
495 for (i = 1; i <= l; i++)
501 static int skipline(FILE * f)
505 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
512 * Check does double duty:
513 * 1. ferret out any fortuitous correspondences due
514 * to confounding by hashing (which result in "jackpot")
515 * 2. collect random access indexes to the two files
517 static void check(FILE * f1, FILE * f2)
519 int i, j, jackpot, c, d;
525 ixold[0] = ixnew[0] = 0;
528 for (i = 1; i <= len[0]; i++) {
530 ixold[i] = ctold += skipline(f1);
534 ixnew[j] = ctnew += skipline(f2);
537 if ((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)
538 || (option_mask32 & FLAG_i)) {
543 * GNU diff ignores a missing newline
544 * in one file if bflag || wflag.
546 if (((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)) &&
547 ((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
552 if ((option_mask32 & FLAG_b) && isspace(c) && isspace(d)) {
557 } while (isspace(c = getc(f1)));
562 } while (isspace(d = getc(f2)));
563 } else if (option_mask32 & FLAG_w) {
564 while (isspace(c) && c != '\n') {
568 while (isspace(d) && d != '\n') {
576 if (c != '\n' && c != EOF)
577 ctold += skipline(f1);
578 if (d != '\n' && c != EOF)
579 ctnew += skipline(f2);
582 if (c == '\n' || c == EOF)
589 if ((c = getc(f1)) != (d = getc(f2))) {
591 if (c != '\n' && c != EOF)
592 ctold += skipline(f1);
593 if (d != '\n' && c != EOF)
594 ctnew += skipline(f2);
597 if (c == '\n' || c == EOF)
605 for (; j <= len[1]; j++)
606 ixnew[j] = ctnew += skipline(f2);
610 /* shellsort CACM #201 */
611 static void sort(struct line *a, int n)
613 struct line *ai, *aim, w;
618 for (j = 1; j <= n; j *= 2)
620 for (m /= 2; m != 0; m /= 2) {
622 for (j = 1; j <= k; j++) {
623 for (ai = &a[j]; ai > a; ai -= m) {
626 break; /* wraparound */
627 if (aim->value > ai[0].value ||
628 (aim->value == ai[0].value && aim->serial > ai[0].serial))
630 w.value = ai[0].value;
631 ai[0].value = aim->value;
632 aim->value = w.value;
633 w.serial = ai[0].serial;
634 ai[0].serial = aim->serial;
635 aim->serial = w.serial;
642 static void uni_range(int a, int b)
645 printf("%d,%d", a, b - a + 1);
653 static void fetch(long *f, int a, int b, FILE * lb, int ch)
655 int i, j, c, lastc, col, nc;
659 for (i = a; i <= b; i++) {
660 fseek(lb, f[i - 1], SEEK_SET);
661 nc = f[i] - f[i - 1];
664 if (option_mask32 & FLAG_T)
668 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
669 if ((c = getc(lb)) == EOF) {
670 printf("\n\\ No newline at end of file\n");
673 if (c == '\t' && (option_mask32 & FLAG_t)) {
686 static int asciifile(FILE * f)
688 #if ENABLE_FEATURE_DIFF_BINARY
692 if ((option_mask32 & FLAG_a) || f == NULL)
695 #if ENABLE_FEATURE_DIFF_BINARY
697 cnt = fread(bb_common_bufsiz1, 1, BUFSIZ, f);
698 for (i = 0; i < cnt; i++) {
699 if (!isprint(bb_common_bufsiz1[i])
700 && !isspace(bb_common_bufsiz1[i])) {
709 /* dump accumulated "unified" diff changes */
710 static void dump_unified_vec(FILE * f1, FILE * f2)
712 struct context_vec *cvp = context_vec_start;
713 int lowa, upb, lowc, upd;
717 if (context_vec_start > context_vec_ptr)
721 lowa = MAX(1, cvp->a - context);
722 upb = MIN(len[0], context_vec_ptr->b + context);
723 lowc = MAX(1, cvp->c - context);
724 upd = MIN(len[1], context_vec_ptr->d + context);
727 uni_range(lowa, upb);
729 uni_range(lowc, upd);
733 * Output changes in "unified" diff format--the old and new lines
734 * are printed together.
736 for (; cvp <= context_vec_ptr; cvp++) {
743 * c: both new and old changes
744 * d: only changes in the old file
745 * a: only changes in the new file
747 if (a <= b && c <= d)
750 ch = (a <= b) ? 'd' : 'a';
751 if (ch == 'c' || ch == 'd') {
752 fetch(ixold, lowa, a - 1, f1, ' ');
753 fetch(ixold, a, b, f1, '-');
756 fetch(ixnew, lowc, c - 1, f2, ' ');
757 if (ch == 'c' || ch == 'a')
758 fetch(ixnew, c, d, f2, '+');
762 fetch(ixnew, d + 1, upd, f2, ' ');
764 context_vec_ptr = context_vec_start - 1;
768 static void print_header(const char *file1, const char *file2)
771 printf("--- %s\n", label1);
773 printf("--- %s\t%s", file1, ctime(&stb1.st_mtime));
775 printf("+++ %s\n", label2);
777 printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
782 * Indicate that there is a difference between lines a and b of the from file
783 * to get to lines c to d of the to file. If a is greater than b then there
784 * are no lines in the from file involved and this means that there were
785 * lines appended (beginning at b). If c is greater than d then there are
786 * lines missing from the to file.
788 static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
791 if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
797 * Allocate change records as needed.
799 if (context_vec_ptr == context_vec_end - 1) {
800 ptrdiff_t offset = context_vec_ptr - context_vec_start;
803 context_vec_start = xrealloc(context_vec_start,
804 max_context * sizeof(struct context_vec));
805 context_vec_end = context_vec_start + max_context;
806 context_vec_ptr = context_vec_start + offset;
808 if (anychange == 0) {
810 * Print the context/unidiff header first time through.
812 print_header(file1, file2);
813 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
814 c > context_vec_ptr->d + (2 * context) + 1) {
816 * If this change is more than 'context' lines from the
817 * previous change, dump the record and reset it.
819 dump_unified_vec(f1, f2);
822 context_vec_ptr->a = a;
823 context_vec_ptr->b = b;
824 context_vec_ptr->c = c;
825 context_vec_ptr->d = d;
830 static void output(char *file1, FILE * f1, char *file2, FILE * f2)
832 /* Note that j0 and j1 can't be used as they are defined in math.h.
833 * This also allows the rather amusing variable 'j00'... */
834 int m, i0, i1, j00, j01;
840 J[m + 1] = len[1] + 1;
841 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
842 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
846 while (i1 < m && J[i1 + 1] == 0)
850 change(file1, f1, file2, f2, i0, i1, j00, j01);
853 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
855 if (anychange != 0 && !(option_mask32 & FLAG_q)) {
856 dump_unified_vec(f1, f2);
861 * The following code uses an algorithm due to Harold Stone,
862 * which finds a pair of longest identical subsequences in
865 * The major goal is to generate the match vector J.
866 * J[i] is the index of the line in file1 corresponding
867 * to line i file0. J[i] = 0 if there is no
868 * such line in file1.
870 * Lines are hashed so as to work in core. All potential
871 * matches are located by sorting the lines of each file
872 * on the hash (called ``value''). In particular, this
873 * collects the equivalence classes in file1 together.
874 * Subroutine equiv replaces the value of each line in
875 * file0 by the index of the first element of its
876 * matching equivalence in (the reordered) file1.
877 * To save space equiv squeezes file1 into a single
878 * array member in which the equivalence classes
879 * are simply concatenated, except that their first
880 * members are flagged by changing sign.
882 * Next the indices that point into member are unsorted into
883 * array class according to the original order of file0.
885 * The cleverness lies in routine stone. This marches
886 * through the lines of file0, developing a vector klist
887 * of "k-candidates". At step i a k-candidate is a matched
888 * pair of lines x,y (x in file0 y in file1) such that
889 * there is a common subsequence of length k
890 * between the first i lines of file0 and the first y
891 * lines of file1, but there is no such subsequence for
892 * any smaller y. x is the earliest possible mate to y
893 * that occurs in such a subsequence.
895 * Whenever any of the members of the equivalence class of
896 * lines in file1 matable to a line in file0 has serial number
897 * less than the y of some k-candidate, that k-candidate
898 * with the smallest such y is replaced. The new
899 * k-candidate is chained (via pred) to the current
900 * k-1 candidate so that the actual subsequence can
901 * be recovered. When a member has serial number greater
902 * that the y of all k-candidates, the klist is extended.
903 * At the end, the longest subsequence is pulled out
904 * and placed in the array J by unravel
906 * With J in hand, the matches there recorded are
907 * checked against reality to assure that no spurious
908 * matches have crept in due to hashing. If they have,
909 * they are broken, and "jackpot" is recorded--a harmless
910 * matter except that a true match for a spuriously
911 * mated line may now be unnecessarily reported as a change.
913 * Much of the complexity of the program comes simply
914 * from trying to minimize core utilization and
915 * maximize the range of doable problems by dynamically
916 * allocating what is needed and reusing what is not.
917 * The core requirements for problems larger than somewhat
918 * are (in words) 2*length(file0) + length(file1) +
919 * 3*(number of k-candidates installed), typically about
920 * 6n words for files of length n.
922 static unsigned diffreg(char * ofile1, char * ofile2, int flags)
924 char *file1 = ofile1;
925 char *file2 = ofile2;
926 FILE *f1 = stdin, *f2 = stdin;
931 context_vec_ptr = context_vec_start - 1;
933 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
934 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
938 if (LONE_DASH(file1) && LONE_DASH(file2))
941 if (flags & D_EMPTY1)
942 f1 = xfopen(bb_dev_null, "r");
943 else if (NOT_LONE_DASH(file1))
944 f1 = xfopen(file1, "r");
945 if (flags & D_EMPTY2)
946 f2 = xfopen(bb_dev_null, "r");
947 else if (NOT_LONE_DASH(file2))
948 f2 = xfopen(file2, "r");
950 /* We can't diff non-seekable stream - we use rewind(), fseek().
951 * This can be fixed (volunteers?).
952 * Meanwhile we should check it here by stat'ing input fds,
953 * but I am lazy and check that in main() instead.
954 * Check in main won't catch "diffing fifos buried in subdirectories"
955 * failure scenario - not very likely in real life... */
957 i = files_differ(f1, f2, flags);
960 else if (i != 1) { /* 1 == ok */
966 if (!asciifile(f1) || !asciifile(f2)) {
972 prepare(0, f1, stb1.st_size);
973 prepare(1, f2, stb2.st_size);
975 sort(sfile[0], slen[0]);
976 sort(sfile[1], slen[1]);
978 member = (int *) file[1];
979 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
980 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
982 class = (int *) file[0];
983 unsort(sfile[0], slen[0], class);
984 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
986 klist = xmalloc((slen[0] + 2) * sizeof(int));
989 clist = xmalloc(clistlen * sizeof(struct cand));
990 i = stone(class, slen[0], member, klist);
994 J = xrealloc(J, (len[0] + 2) * sizeof(int));
999 ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
1000 ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
1002 output(file1, f1, file2, f2);
1010 fclose_if_not_stdin(f1);
1011 fclose_if_not_stdin(f2);
1012 if (file1 != ofile1)
1014 if (file2 != ofile2)
1020 #if ENABLE_FEATURE_DIFF_DIR
1021 static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1023 int flags = D_HEADER;
1025 char *fullpath1 = NULL; /* if -N */
1026 char *fullpath2 = NULL;
1029 fullpath1 = concat_path_file(dir1, path1);
1031 fullpath2 = concat_path_file(dir2, path2);
1033 if (!fullpath1 || stat(fullpath1, &stb1) != 0) {
1035 memset(&stb1, 0, sizeof(stb1));
1038 fullpath1 = concat_path_file(dir1, path2);
1041 if (!fullpath2 || stat(fullpath2, &stb2) != 0) {
1043 memset(&stb2, 0, sizeof(stb2));
1044 stb2.st_mode = stb1.st_mode;
1047 fullpath2 = concat_path_file(dir2, path1);
1051 if (stb1.st_mode == 0)
1052 stb1.st_mode = stb2.st_mode;
1054 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1055 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1059 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1061 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1064 val = diffreg(fullpath1, fullpath2, flags);
1066 print_status(val, fullpath1, fullpath2, NULL);
1074 #if ENABLE_FEATURE_DIFF_DIR
1075 static int dir_strcmp(const void *p1, const void *p2)
1077 return strcmp(*(char *const *) p1, *(char *const *) p2);
1081 /* This function adds a filename to dl, the directory listing. */
1082 static int add_to_dirlist(const char *filename,
1083 struct stat ATTRIBUTE_UNUSED * sb, void *userdata,
1084 int depth ATTRIBUTE_UNUSED)
1086 /* +2: with space for eventual trailing NULL */
1087 dl = xrealloc(dl, (dl_count+2) * sizeof(dl[0]));
1088 dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata);
1094 /* This returns a sorted directory listing. */
1095 static char **get_dir(char *path)
1098 dl = xzalloc(sizeof(dl[0]));
1100 /* If -r has been set, then the recursive_action function will be
1101 * used. Unfortunately, this outputs the root directory along with
1102 * the recursed paths, so use void *userdata to specify the string
1103 * length of the root directory - '(void*)(strlen(path)+)'.
1104 * add_to_dirlist then removes root dir prefix. */
1106 if (option_mask32 & FLAG_r) {
1107 recursive_action(path, ACTION_RECURSE|ACTION_FOLLOWLINKS,
1108 add_to_dirlist, NULL,
1109 (void*)(strlen(path)+1), 0);
1114 dp = warn_opendir(path);
1115 while ((ep = readdir(dp))) {
1116 if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
1118 add_to_dirlist(ep->d_name, NULL, (void*)(int)0, 0);
1123 /* Sort dl alphabetically. */
1124 qsort(dl, dl_count, sizeof(char *), dir_strcmp);
1126 dl[dl_count] = NULL;
1131 static void diffdir(char *p1, char *p2)
1133 char **dirlist1, **dirlist2;
1137 /* Check for trailing slashes. */
1138 dp1 = last_char_is(p1, '/');
1141 dp2 = last_char_is(p2, '/');
1145 /* Get directory listings for p1 and p2. */
1147 dirlist1 = get_dir(p1);
1148 dirlist2 = get_dir(p2);
1150 /* If -S was set, find the starting point. */
1152 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1154 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1156 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
1157 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
1160 /* Now that both dirlist1 and dirlist2 contain sorted directory
1161 * listings, we can start to go through dirlist1. If both listings
1162 * contain the same file, then do a normal diff. Otherwise, behaviour
1163 * is determined by whether the -N flag is set. */
1164 while (*dirlist1 != NULL || *dirlist2 != NULL) {
1167 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1169 do_diff(p1, dp1, p2, dp2);
1172 } else if (pos < 0) {
1173 if (option_mask32 & FLAG_N)
1174 do_diff(p1, dp1, p2, NULL);
1176 print_only(p1, strlen(p1) + 1, dp1);
1179 if (option_mask32 & FLAG_N)
1180 do_diff(p1, NULL, p2, dp2);
1182 print_only(p2, strlen(p2) + 1, dp2);
1190 int diff_main(int argc, char **argv);
1191 int diff_main(int argc, char **argv)
1196 llist_t *L_arg = NULL;
1200 /* exactly 2 params; collect multiple -L <label> */
1201 opt_complementary = "=2:L::";
1202 getopt32(argc, argv, "abdiL:NqrsS:tTU:wu"
1203 "p" /* ignored (for compatibility) */,
1204 &L_arg, &start, &U_opt);
1208 if (label1 && label2)
1211 label1 = L_arg->data;
1212 else { /* then label2 is NULL */
1214 label1 = L_arg->data;
1216 /* we leak L_arg here... */
1217 L_arg = L_arg->link;
1219 if (option_mask32 & FLAG_U)
1220 context = xatoi_u(U_opt);
1223 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1224 * driver routine. Both drivers use the contents of stb1 and stb2.
1229 if (LONE_DASH(f1)) {
1230 fstat(STDIN_FILENO, &stb1);
1234 if (LONE_DASH(f2)) {
1235 fstat(STDIN_FILENO, &stb2);
1239 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1240 bb_error_msg_and_die("can't compare - to a directory");
1241 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1242 #if ENABLE_FEATURE_DIFF_DIR
1245 bb_error_msg_and_die("directory comparison not supported");
1248 if (S_ISDIR(stb1.st_mode)) {
1249 f1 = concat_path_file(f1, f2);
1252 if (S_ISDIR(stb2.st_mode)) {
1253 f2 = concat_path_file(f2, f1);
1257 /* We can't diff e.g. stdin supplied by a pipe - we use rewind(), fseek().
1258 * This can be fixed (volunteers?) */
1259 if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
1260 bb_error_msg_and_die("can't diff non-seekable stream");
1261 print_status(diffreg(f1, f2, 0), f1, f2, NULL);