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)
68 #define g_read_buf bb_common_bufsiz1
82 * The following struct is used to record change information
83 * doing a "context" or "unified" diff. (see routine "change" to
84 * understand the highly mnemonic field names)
87 int a; /* start line in old file */
88 int b; /* end line in old file */
89 int c; /* start line in new file */
90 int d; /* end line in new file */
94 USE_FEATURE_DIFF_DIR(char **dl;)
95 USE_FEATURE_DIFF_DIR(int dl_count;)
96 /* This is the default number of lines of context. */
103 struct line *file[2];
104 int *J; /* will be overlaid on class */
105 int *class; /* will be overlaid on file[0] */
106 int *klist; /* will be overlaid on file[0] after class */
107 int *member; /* will be overlaid on file[1] */
110 int pref, suff; /* length of prefix and suffix */
113 long *ixnew; /* will be overlaid on file[1] */
114 long *ixold; /* will be overlaid on klist */
115 struct cand *clist; /* merely a free storage pot for candidates */
116 int clistlen; /* the length of clist */
117 struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
118 struct context_vec *context_vec_start;
119 struct context_vec *context_vec_end;
120 struct context_vec *context_vec_ptr;
121 struct stat stb1, stb2;
123 #define G (*ptr_to_globals)
125 #define dl_count (G.dl_count )
126 #define context (G.context )
127 #define max_context (G.max_context )
128 #define status (G.status )
129 #define start (G.start )
130 #define label1 (G.label1 )
131 #define label2 (G.label2 )
132 #define file (G.file )
134 #define class (G.class )
135 #define klist (G.klist )
136 #define member (G.member )
137 #define clen (G.clen )
139 #define pref (G.pref )
140 #define suff (G.suff )
141 #define slen (G.slen )
142 #define anychange (G.anychange )
143 #define ixnew (G.ixnew )
144 #define ixold (G.ixold )
145 #define clist (G.clist )
146 #define clistlen (G.clistlen )
147 #define sfile (G.sfile )
148 #define context_vec_start (G.context_vec_start )
149 #define context_vec_end (G.context_vec_end )
150 #define context_vec_ptr (G.context_vec_ptr )
151 #define stb1 (G.stb1 )
152 #define stb2 (G.stb2 )
153 #define INIT_G() do { \
154 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
160 static void print_only(const char *path, size_t dirlen, const char *entry)
164 printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
167 static void print_status(int val, char *path1, char *path2, char *entry)
169 const char *const _entry = entry ? entry : "";
170 char * const _path1 = entry ? concat_path_file(path1, _entry) : path1;
171 char * const _path2 = entry ? concat_path_file(path2, _entry) : path2;
175 print_only(path1, strlen(path1), entry);
178 printf("Common subdirectories: %s and %s\n", _path1, _path2);
181 printf("Binary files %s and %s differ\n", _path1, _path2);
184 if (option_mask32 & FLAG_q)
185 printf("Files %s and %s differ\n", _path1, _path2);
188 if (option_mask32 & FLAG_s)
189 printf("Files %s and %s are identical\n", _path1, _path2);
192 printf("File %s is a %s while file %s is a %s\n",
193 _path1, "directory", _path2, "regular file");
196 printf("File %s is a %s while file %s is a %s\n",
197 _path1, "regular file", _path2, "directory");
200 printf("File %s is not a regular file or directory and was skipped\n",
204 printf("File %s is not a regular file or directory and was skipped\n",
213 static ALWAYS_INLINE int fiddle_sum(int sum, int t)
215 return sum * 127 + t;
218 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
220 static int readhash(FILE *fp)
227 if (!(option_mask32 & (FLAG_b | FLAG_w))) {
228 for (i = 0; (t = getc(fp)) != '\n'; i++) {
234 sum = fiddle_sum(sum, t);
238 switch (t = getc(fp)) {
247 if (space && !(option_mask32 & FLAG_w)) {
251 sum = fiddle_sum(sum, t);
265 * There is a remote possibility that we end up with a zero sum.
266 * Zero is used as an EOF marker, so return 1 instead.
268 return (sum == 0 ? 1 : sum);
273 * Check to see if the given files differ.
274 * Returns 0 if they are the same, 1 if different, and -1 on error.
276 static int files_differ(FILE *f1, FILE *f2, int flags)
280 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size
281 || (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)
286 i = fread(g_read_buf, 1, COMMON_BUFSIZE/2, f1);
287 j = fread(g_read_buf + COMMON_BUFSIZE/2, 1, COMMON_BUFSIZE/2, f2);
291 return (ferror(f1) || ferror(f2));
292 if (memcmp(g_read_buf,
293 g_read_buf + COMMON_BUFSIZE/2, i) != 0)
299 static void prepare(int i, FILE *fp /*, off_t filesize*/)
307 /*sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;*/
311 p = xmalloc((sz + 3) * sizeof(p[0]));
313 while ((h = readhash(fp))) {
316 p = xrealloc(p, (sz + 3) * sizeof(p[0]));
325 static void prune(void)
329 for (pref = 0; pref < len[0] && pref < len[1] &&
330 file[0][pref + 1].value == file[1][pref + 1].value; pref++)
332 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
333 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
336 for (j = 0; j < 2; j++) {
337 sfile[j] = file[j] + pref;
338 slen[j] = len[j] - pref - suff;
339 for (i = 0; i <= slen[j]; i++)
340 sfile[j][i].serial = i;
345 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
350 while (i <= n && j <= m) {
351 if (a[i].value < b[j].value)
353 else if (a[i].value == b[j].value)
364 while (b[j + 1].value == b[j].value) {
373 static int isqrt(int n)
385 } while ((x - y) > 1 || (x - y) < -1);
391 static int newcand(int x, int y, int pred)
395 if (clen == clistlen) {
396 clistlen = clistlen * 11 / 10;
397 clist = xrealloc(clist, clistlen * sizeof(struct cand));
407 static int search(int *c, int k, int y)
411 if (clist[c[k]].y < y) /* quick look for typical case */
431 static int stone(int *a, int n, int *b, int *c)
435 unsigned int numtries;
437 #if ENABLE_FEATURE_DIFF_MINIMAL
438 const unsigned int bound =
439 (option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
441 const unsigned int bound = MAX(256, isqrt(n));
444 c[0] = newcand(0, 0, 0);
445 for (i = 1; i <= n; i++) {
454 if (y <= clist[oldc].y)
460 if (clist[c[l]].y <= y)
463 c[l] = newcand(i, y, oldc);
468 c[l] = newcand(i, y, oldc);
472 } while ((y = b[++j]) > 0 && numtries < bound);
478 static void unravel(int p)
483 for (i = 0; i <= len[0]; i++)
484 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0;
485 for (q = clist + p; q->y != 0; q = clist + q->pred)
486 J[q->x + pref] = q->y + pref;
490 static void unsort(struct line *f, int l, int *b)
494 a = xmalloc((l + 1) * sizeof(int));
495 for (i = 1; i <= l; i++)
496 a[f[i].serial] = f[i].value;
497 for (i = 1; i <= l; i++)
503 static int skipline(FILE * f)
507 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
514 * Check does double duty:
515 * 1. ferret out any fortuitous correspondences due
516 * to confounding by hashing (which result in "jackpot")
517 * 2. collect random access indexes to the two files
519 static void check(FILE * f1, FILE * f2)
521 int i, j, jackpot, c, d;
527 ixold[0] = ixnew[0] = 0;
530 for (i = 1; i <= len[0]; i++) {
532 ixold[i] = ctold += skipline(f1);
536 ixnew[j] = ctnew += skipline(f2);
539 if ((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)
540 || (option_mask32 & FLAG_i)) {
545 * GNU diff ignores a missing newline
546 * in one file if bflag || wflag.
548 if (((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)) &&
549 ((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
554 if ((option_mask32 & FLAG_b) && isspace(c) && isspace(d)) {
559 } while (isspace(c = getc(f1)));
564 } while (isspace(d = getc(f2)));
565 } else if (option_mask32 & FLAG_w) {
566 while (isspace(c) && c != '\n') {
570 while (isspace(d) && d != '\n') {
578 if (c != '\n' && c != EOF)
579 ctold += skipline(f1);
580 if (d != '\n' && c != EOF)
581 ctnew += skipline(f2);
584 if (c == '\n' || c == EOF)
595 if (c != '\n' && c != EOF)
596 ctold += skipline(f1);
597 if (d != '\n' && c != EOF)
598 ctnew += skipline(f2);
601 if (c == '\n' || c == EOF)
609 for (; j <= len[1]; j++)
610 ixnew[j] = ctnew += skipline(f2);
614 /* shellsort CACM #201 */
615 static void sort(struct line *a, int n)
617 struct line *ai, *aim, w;
622 for (j = 1; j <= n; j *= 2)
624 for (m /= 2; m != 0; m /= 2) {
626 for (j = 1; j <= k; j++) {
627 for (ai = &a[j]; ai > a; ai -= m) {
630 break; /* wraparound */
631 if (aim->value > ai[0].value ||
632 (aim->value == ai[0].value && aim->serial > ai[0].serial))
634 w.value = ai[0].value;
635 ai[0].value = aim->value;
636 aim->value = w.value;
637 w.serial = ai[0].serial;
638 ai[0].serial = aim->serial;
639 aim->serial = w.serial;
646 static void uni_range(int a, int b)
649 printf("%d,%d", a, b - a + 1);
657 static void fetch(long *f, int a, int b, FILE * lb, int ch)
659 int i, j, c, lastc, col, nc;
663 for (i = a; i <= b; i++) {
664 fseek(lb, f[i - 1], SEEK_SET);
665 nc = f[i] - f[i - 1];
668 if (option_mask32 & FLAG_T)
672 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
675 printf("\n\\ No newline at end of file\n");
678 if (c == '\t' && (option_mask32 & FLAG_t)) {
691 static int asciifile(FILE * f)
693 #if ENABLE_FEATURE_DIFF_BINARY
697 if ((option_mask32 & FLAG_a) || f == NULL)
700 #if ENABLE_FEATURE_DIFF_BINARY
702 cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f);
703 for (i = 0; i < cnt; i++) {
704 if (!isprint(g_read_buf[i])
705 && !isspace(g_read_buf[i])) {
714 /* dump accumulated "unified" diff changes */
715 static void dump_unified_vec(FILE * f1, FILE * f2)
717 struct context_vec *cvp = context_vec_start;
718 int lowa, upb, lowc, upd;
722 if (context_vec_start > context_vec_ptr)
726 lowa = MAX(1, cvp->a - context);
727 upb = MIN(len[0], context_vec_ptr->b + context);
728 lowc = MAX(1, cvp->c - context);
729 upd = MIN(len[1], context_vec_ptr->d + context);
732 uni_range(lowa, upb);
734 uni_range(lowc, upd);
738 * Output changes in "unified" diff format--the old and new lines
739 * are printed together.
741 for (; cvp <= context_vec_ptr; cvp++) {
748 * c: both new and old changes
749 * d: only changes in the old file
750 * a: only changes in the new file
752 if (a <= b && c <= d)
755 ch = (a <= b) ? 'd' : 'a';
759 fetch(ixold, lowa, a - 1, f1, ' ');
760 fetch(ixold, a, b, f1, '-');
761 fetch(ixnew, c, d, f2, '+');
764 fetch(ixold, lowa, a - 1, f1, ' ');
765 fetch(ixold, a, b, f1, '-');
768 fetch(ixnew, lowc, c - 1, f2, ' ');
769 fetch(ixnew, c, d, f2, '+');
773 if (ch == 'c' || ch == 'd') {
774 fetch(ixold, lowa, a - 1, f1, ' ');
775 fetch(ixold, a, b, f1, '-');
778 fetch(ixnew, lowc, c - 1, f2, ' ');
779 if (ch == 'c' || ch == 'a')
780 fetch(ixnew, c, d, f2, '+');
785 fetch(ixnew, d + 1, upd, f2, ' ');
787 context_vec_ptr = context_vec_start - 1;
791 static void print_header(const char *file1, const char *file2)
794 printf("--- %s\n", label1);
796 printf("--- %s\t%s", file1, ctime(&stb1.st_mtime));
798 printf("+++ %s\n", label2);
800 printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
805 * Indicate that there is a difference between lines a and b of the from file
806 * to get to lines c to d of the to file. If a is greater than b then there
807 * are no lines in the from file involved and this means that there were
808 * lines appended (beginning at b). If c is greater than d then there are
809 * lines missing from the to file.
811 static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
814 if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
820 * Allocate change records as needed.
822 if (context_vec_ptr == context_vec_end - 1) {
823 ptrdiff_t offset = context_vec_ptr - context_vec_start;
826 context_vec_start = xrealloc(context_vec_start,
827 max_context * sizeof(struct context_vec));
828 context_vec_end = context_vec_start + max_context;
829 context_vec_ptr = context_vec_start + offset;
831 if (anychange == 0) {
833 * Print the context/unidiff header first time through.
835 print_header(file1, file2);
836 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
837 c > context_vec_ptr->d + (2 * context) + 1) {
839 * If this change is more than 'context' lines from the
840 * previous change, dump the record and reset it.
842 dump_unified_vec(f1, f2);
845 context_vec_ptr->a = a;
846 context_vec_ptr->b = b;
847 context_vec_ptr->c = c;
848 context_vec_ptr->d = d;
853 static void output(char *file1, FILE * f1, char *file2, FILE * f2)
855 /* Note that j0 and j1 can't be used as they are defined in math.h.
856 * This also allows the rather amusing variable 'j00'... */
857 int m, i0, i1, j00, j01;
863 J[m + 1] = len[1] + 1;
864 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
865 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
869 while (i1 < m && J[i1 + 1] == 0)
873 change(file1, f1, file2, f2, i0, i1, j00, j01);
876 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
878 if (anychange != 0 && !(option_mask32 & FLAG_q)) {
879 dump_unified_vec(f1, f2);
884 * The following code uses an algorithm due to Harold Stone,
885 * which finds a pair of longest identical subsequences in
888 * The major goal is to generate the match vector J.
889 * J[i] is the index of the line in file1 corresponding
890 * to line i file0. J[i] = 0 if there is no
891 * such line in file1.
893 * Lines are hashed so as to work in core. All potential
894 * matches are located by sorting the lines of each file
895 * on the hash (called ``value''). In particular, this
896 * collects the equivalence classes in file1 together.
897 * Subroutine equiv replaces the value of each line in
898 * file0 by the index of the first element of its
899 * matching equivalence in (the reordered) file1.
900 * To save space equiv squeezes file1 into a single
901 * array member in which the equivalence classes
902 * are simply concatenated, except that their first
903 * members are flagged by changing sign.
905 * Next the indices that point into member are unsorted into
906 * array class according to the original order of file0.
908 * The cleverness lies in routine stone. This marches
909 * through the lines of file0, developing a vector klist
910 * of "k-candidates". At step i a k-candidate is a matched
911 * pair of lines x,y (x in file0 y in file1) such that
912 * there is a common subsequence of length k
913 * between the first i lines of file0 and the first y
914 * lines of file1, but there is no such subsequence for
915 * any smaller y. x is the earliest possible mate to y
916 * that occurs in such a subsequence.
918 * Whenever any of the members of the equivalence class of
919 * lines in file1 matable to a line in file0 has serial number
920 * less than the y of some k-candidate, that k-candidate
921 * with the smallest such y is replaced. The new
922 * k-candidate is chained (via pred) to the current
923 * k-1 candidate so that the actual subsequence can
924 * be recovered. When a member has serial number greater
925 * that the y of all k-candidates, the klist is extended.
926 * At the end, the longest subsequence is pulled out
927 * and placed in the array J by unravel
929 * With J in hand, the matches there recorded are
930 * checked against reality to assure that no spurious
931 * matches have crept in due to hashing. If they have,
932 * they are broken, and "jackpot" is recorded--a harmless
933 * matter except that a true match for a spuriously
934 * mated line may now be unnecessarily reported as a change.
936 * Much of the complexity of the program comes simply
937 * from trying to minimize core utilization and
938 * maximize the range of doable problems by dynamically
939 * allocating what is needed and reusing what is not.
940 * The core requirements for problems larger than somewhat
941 * are (in words) 2*length(file0) + length(file1) +
942 * 3*(number of k-candidates installed), typically about
943 * 6n words for files of length n.
945 static unsigned diffreg(char *ofile1, char *ofile2, int flags)
947 char *file1 = ofile1;
948 char *file2 = ofile2;
949 FILE *f1 = stdin, *f2 = stdin;
954 context_vec_ptr = context_vec_start - 1;
956 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
957 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
961 if (LONE_DASH(file1) && LONE_DASH(file2))
964 if (flags & D_EMPTY1)
965 f1 = xfopen(bb_dev_null, "r");
966 else if (NOT_LONE_DASH(file1))
967 f1 = xfopen(file1, "r");
968 if (flags & D_EMPTY2)
969 f2 = xfopen(bb_dev_null, "r");
970 else if (NOT_LONE_DASH(file2))
971 f2 = xfopen(file2, "r");
973 /* We can't diff non-seekable stream - we use rewind(), fseek().
974 * This can be fixed (volunteers?).
975 * Meanwhile we should check it here by stat'ing input fds,
976 * but I am lazy and check that in main() instead.
977 * Check in main won't catch "diffing fifos buried in subdirectories"
978 * failure scenario - not very likely in real life... */
980 i = files_differ(f1, f2, flags);
983 else if (i != 1) { /* 1 == ok */
989 if (!asciifile(f1) || !asciifile(f2)) {
995 prepare(0, f1 /*, stb1.st_size*/);
996 prepare(1, f2 /*, stb2.st_size*/);
998 sort(sfile[0], slen[0]);
999 sort(sfile[1], slen[1]);
1001 member = (int *) file[1];
1002 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1003 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
1005 class = (int *) file[0];
1006 unsort(sfile[0], slen[0], class);
1007 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
1009 klist = xmalloc((slen[0] + 2) * sizeof(int));
1012 clist = xmalloc(clistlen * sizeof(struct cand));
1013 i = stone(class, slen[0], member, klist);
1017 J = xrealloc(J, (len[0] + 2) * sizeof(int));
1022 ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
1023 ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
1025 output(file1, f1, file2, f2);
1033 fclose_if_not_stdin(f1);
1034 fclose_if_not_stdin(f2);
1035 if (file1 != ofile1)
1037 if (file2 != ofile2)
1043 #if ENABLE_FEATURE_DIFF_DIR
1044 static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1046 int flags = D_HEADER;
1048 char *fullpath1 = NULL; /* if -N */
1049 char *fullpath2 = NULL;
1052 fullpath1 = concat_path_file(dir1, path1);
1054 fullpath2 = concat_path_file(dir2, path2);
1056 if (!fullpath1 || stat(fullpath1, &stb1) != 0) {
1058 memset(&stb1, 0, sizeof(stb1));
1061 fullpath1 = concat_path_file(dir1, path2);
1064 if (!fullpath2 || stat(fullpath2, &stb2) != 0) {
1066 memset(&stb2, 0, sizeof(stb2));
1067 stb2.st_mode = stb1.st_mode;
1070 fullpath2 = concat_path_file(dir2, path1);
1074 if (stb1.st_mode == 0)
1075 stb1.st_mode = stb2.st_mode;
1077 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1078 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1082 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1084 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1087 val = diffreg(fullpath1, fullpath2, flags);
1089 print_status(val, fullpath1, fullpath2, NULL);
1097 #if ENABLE_FEATURE_DIFF_DIR
1098 /* This function adds a filename to dl, the directory listing. */
1099 static int add_to_dirlist(const char *filename,
1100 struct stat ATTRIBUTE_UNUSED * sb, void *userdata,
1101 int depth ATTRIBUTE_UNUSED)
1103 /* +2: with space for eventual trailing NULL */
1104 dl = xrealloc(dl, (dl_count+2) * sizeof(dl[0]));
1105 dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata);
1111 /* This returns a sorted directory listing. */
1112 static char **get_dir(char *path)
1115 dl = xzalloc(sizeof(dl[0]));
1117 /* If -r has been set, then the recursive_action function will be
1118 * used. Unfortunately, this outputs the root directory along with
1119 * the recursed paths, so use void *userdata to specify the string
1120 * length of the root directory - '(void*)(strlen(path)+)'.
1121 * add_to_dirlist then removes root dir prefix. */
1123 if (option_mask32 & FLAG_r) {
1124 recursive_action(path, ACTION_RECURSE|ACTION_FOLLOWLINKS,
1125 add_to_dirlist, NULL,
1126 (void*)(strlen(path)+1), 0);
1131 dp = warn_opendir(path);
1132 while ((ep = readdir(dp))) {
1133 if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
1135 add_to_dirlist(ep->d_name, NULL, (void*)(int)0, 0);
1140 /* Sort dl alphabetically. */
1141 qsort_string_vector(dl, dl_count);
1143 dl[dl_count] = NULL;
1148 static void diffdir(char *p1, char *p2)
1150 char **dirlist1, **dirlist2;
1154 /* Check for trailing slashes. */
1155 dp1 = last_char_is(p1, '/');
1158 dp2 = last_char_is(p2, '/');
1162 /* Get directory listings for p1 and p2. */
1164 dirlist1 = get_dir(p1);
1165 dirlist2 = get_dir(p2);
1167 /* If -S was set, find the starting point. */
1169 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1171 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1173 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
1174 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
1177 /* Now that both dirlist1 and dirlist2 contain sorted directory
1178 * listings, we can start to go through dirlist1. If both listings
1179 * contain the same file, then do a normal diff. Otherwise, behaviour
1180 * is determined by whether the -N flag is set. */
1181 while (*dirlist1 != NULL || *dirlist2 != NULL) {
1184 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1186 do_diff(p1, dp1, p2, dp2);
1189 } else if (pos < 0) {
1190 if (option_mask32 & FLAG_N)
1191 do_diff(p1, dp1, p2, NULL);
1193 print_only(p1, strlen(p1) + 1, dp1);
1196 if (option_mask32 & FLAG_N)
1197 do_diff(p1, NULL, p2, dp2);
1199 print_only(p2, strlen(p2) + 1, dp2);
1207 int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1208 int diff_main(int argc ATTRIBUTE_UNUSED, char **argv)
1212 llist_t *L_arg = NULL;
1216 /* exactly 2 params; collect multiple -L <label>; -U N */
1217 opt_complementary = "=2:L::U+";
1218 getopt32(argv, "abdiL:NqrsS:tTU:wu"
1219 "p" /* ignored (for compatibility) */,
1220 &L_arg, &start, &context);
1224 if (label1 && label2)
1227 label1 = L_arg->data;
1228 else { /* then label2 is NULL */
1230 label1 = L_arg->data;
1232 /* we leak L_arg here... */
1233 L_arg = L_arg->link;
1237 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1238 * driver routine. Both drivers use the contents of stb1 and stb2.
1243 if (LONE_DASH(f1)) {
1244 fstat(STDIN_FILENO, &stb1);
1248 if (LONE_DASH(f2)) {
1249 fstat(STDIN_FILENO, &stb2);
1253 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1254 bb_error_msg_and_die("can't compare - to a directory");
1255 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1256 #if ENABLE_FEATURE_DIFF_DIR
1259 bb_error_msg_and_die("directory comparison not supported");
1262 if (S_ISDIR(stb1.st_mode)) {
1263 f1 = concat_path_file(f1, f2);
1266 if (S_ISDIR(stb2.st_mode)) {
1267 f2 = concat_path_file(f2, f1);
1271 /* We can't diff e.g. stdin supplied by a pipe - we use rewind(), fseek().
1272 * This can be fixed (volunteers?) */
1273 if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
1274 bb_error_msg_and_die("can't diff non-seekable stream");
1275 print_status(diffreg(f1, f2, 0), f1, f2, NULL);