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.
16 #include <sys/types.h>
17 #include <sys/param.h>
34 #define FSIZE_MAX 32768
39 #define D_HEADER 1 /* Print a header/footer between files */
40 #define D_EMPTY1 2 /* Treat first file as empty (/dev/null) */
41 #define D_EMPTY2 4 /* Treat second file as empty (/dev/null) */
44 * Status values for print_status() and diffreg() return values
46 * D_SAME - files are the same
47 * D_DIFFER - files differ
48 * D_BINARY - binary files differ
49 * D_COMMON - subdirectory common to both dirs
50 * D_ONLY - file only exists in one dir
51 * D_MISMATCH1 - path1 a dir, path2 a file
52 * D_MISMATCH2 - path1 a file, path2 a dir
53 * D_ERROR - error occurred
54 * D_SKIPPED1 - skipped path1 as it is a special file
55 * D_SKIPPED2 - skipped path2 as it is a special file
59 #define D_DIFFER (1<<0)
60 #define D_BINARY (1<<1)
61 #define D_COMMON (1<<2)
63 #define D_MISMATCH1 (1<<4)
64 #define D_MISMATCH2 (1<<5)
65 #define D_ERROR (1<<6)
66 #define D_SKIPPED1 (1<<7)
67 #define D_SKIPPED2 (1<<8)
69 /* Command line options */
70 static unsigned long cmd_flags;
82 #define FLAG_t (1<<10)
83 #define FLAG_T (1<<11)
84 #define FLAG_U (1<<12)
85 #define FLAG_w (1<<13)
88 char *start, *label[2];
89 struct stat stb1, stb2;
105 * The following struct is used to record change information
106 * doing a "context" or "unified" diff. (see routine "change" to
107 * understand the highly mnemonic field names)
110 int a; /* start line in old file */
111 int b; /* end line in old file */
112 int c; /* start line in new file */
113 int d; /* end line in new file */
116 static int *J; /* will be overlaid on class */
117 static int *class; /* will be overlaid on file[0] */
118 static int *klist; /* will be overlaid on file[0] after class */
119 static int *member; /* will be overlaid on file[1] */
122 static int pref, suff; /* length of prefix and suffix */
124 static int anychange;
125 static long *ixnew; /* will be overlaid on file[1] */
126 static long *ixold; /* will be overlaid on klist */
127 static struct cand *clist; /* merely a free storage pot for candidates */
128 static int clistlen; /* the length of clist */
129 static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
130 static struct context_vec *context_vec_start;
131 static struct context_vec *context_vec_end;
132 static struct context_vec *context_vec_ptr;
134 static void print_only(const char *path, size_t dirlen, const char *entry)
138 printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
141 static void print_status(int val, char *path1, char *path2, char *entry)
143 const char *const _entry = entry ? entry : "";
144 char *_path1 = entry ? concat_path_file(path1, _entry) : path1;
145 char *_path2 = entry ? concat_path_file(path2, _entry) : path2;
149 print_only(path1, strlen(path1), entry);
152 printf("Common subdirectories: %s and %s\n", _path1, _path2);
155 printf("Binary files %s and %s differ\n", _path1, _path2);
158 if (cmd_flags & FLAG_q)
159 printf("Files %s and %s differ\n", _path1, _path2);
162 if (cmd_flags & FLAG_s)
163 printf("Files %s and %s are identical\n", _path1, _path2);
166 printf("File %s is a directory while file %s is a regular file\n",
170 printf("File %s is a regular file while file %s is a directory\n",
174 printf("File %s is not a regular file or directory and was skipped\n",
178 printf("File %s is not a regular file or directory and was skipped\n",
189 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
191 static int readhash(FILE * f)
198 if (!(cmd_flags & FLAG_b) && !(cmd_flags & FLAG_w)) {
200 for (i = 0; (t = getc(f)) != '\n'; i++) {
208 for (i = 0; (t = getc(f)) != '\n'; i++) {
218 switch (t = getc(f)) {
227 if (space && !(cmd_flags & FLAG_w)) {
245 * There is a remote possibility that we end up with a zero sum.
246 * Zero is used as an EOF marker, so return 1 instead.
248 return (sum == 0 ? 1 : sum);
254 * Check to see if the given files differ.
255 * Returns 0 if they are the same, 1 if different, and -1 on error.
257 static int files_differ(FILE * f1, FILE * f2, int flags)
259 char buf1[BUFSIZ], buf2[BUFSIZ];
262 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size ||
263 (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
266 i = fread(buf1, 1, sizeof(buf1), f1);
267 j = fread(buf2, 1, sizeof(buf2), f2);
270 if (i == 0 && j == 0) {
271 if (ferror(f1) || ferror(f2))
275 if (memcmp(buf1, buf2, i) != 0)
280 static void prepare(int i, FILE * fd, off_t filesize)
288 sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;
292 p = xmalloc((sz + 3) * sizeof(struct line));
293 for (j = 0; (h = readhash(fd));) {
296 p = xrealloc(p, (sz + 3) * sizeof(struct line));
304 static void prune(void)
308 for (pref = 0; pref < len[0] && pref < len[1] &&
309 file[0][pref + 1].value == file[1][pref + 1].value; pref++);
310 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
311 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
313 for (j = 0; j < 2; j++) {
314 sfile[j] = file[j] + pref;
315 slen[j] = len[j] - pref - suff;
316 for (i = 0; i <= slen[j]; i++)
317 sfile[j][i].serial = i;
321 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
326 while (i <= n && j <= m) {
327 if (a[i].value < b[j].value)
329 else if (a[i].value == b[j].value)
340 while (b[j + 1].value == b[j].value) {
348 static int isqrt(int n)
360 } while ((x - y) > 1 || (x - y) < -1);
365 static int newcand(int x, int y, int pred)
369 if (clen == clistlen) {
370 clistlen = clistlen * 11 / 10;
371 clist = xrealloc(clist, clistlen * sizeof(struct cand));
381 static int search(int *c, int k, int y)
385 if (clist[c[k]].y < y) /* quick look for typical case */
405 static int stone(int *a, int n, int *b, int *c)
409 unsigned int numtries;
411 #if ENABLE_FEATURE_DIFF_MINIMAL
412 const unsigned int bound =
413 (cmd_flags & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
415 const unsigned int bound = MAX(256, isqrt(n));
418 c[0] = newcand(0, 0, 0);
419 for (i = 1; i <= n; i++) {
428 if (y <= clist[oldc].y)
434 if (clist[c[l]].y <= y)
437 c[l] = newcand(i, y, oldc);
442 c[l] = newcand(i, y, oldc);
446 } while ((y = b[++j]) > 0 && numtries < bound);
451 static void unravel(int p)
456 for (i = 0; i <= len[0]; i++)
457 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0;
458 for (q = clist + p; q->y != 0; q = clist + q->pred)
459 J[q->x + pref] = q->y + pref;
463 static void unsort(struct line *f, int l, int *b)
467 a = xmalloc((l + 1) * sizeof(int));
468 for (i = 1; i <= l; i++)
469 a[f[i].serial] = f[i].value;
470 for (i = 1; i <= l; i++)
475 static int skipline(FILE * f)
479 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
486 * Check does double duty:
487 * 1. ferret out any fortuitous correspondences due
488 * to confounding by hashing (which result in "jackpot")
489 * 2. collect random access indexes to the two files
491 static void check(FILE * f1, FILE * f2)
493 int i, j, jackpot, c, d;
499 ixold[0] = ixnew[0] = 0;
502 for (i = 1; i <= len[0]; i++) {
504 ixold[i] = ctold += skipline(f1);
508 ixnew[j] = ctnew += skipline(f2);
511 if ((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w)
512 || (cmd_flags & FLAG_i)) {
517 * GNU diff ignores a missing newline
518 * in one file if bflag || wflag.
520 if (((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w)) &&
521 ((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
526 if ((cmd_flags & FLAG_b) && isspace(c) && isspace(d)) {
531 } while (isspace(c = getc(f1)));
536 } while (isspace(d = getc(f2)));
537 } else if (cmd_flags & FLAG_w) {
538 while (isspace(c) && c != '\n') {
542 while (isspace(d) && d != '\n') {
550 if (c != '\n' && c != EOF)
551 ctold += skipline(f1);
552 if (d != '\n' && c != EOF)
553 ctnew += skipline(f2);
556 if (c == '\n' || c == EOF)
563 if ((c = getc(f1)) != (d = getc(f2))) {
565 if (c != '\n' && c != EOF)
566 ctold += skipline(f1);
567 if (d != '\n' && c != EOF)
568 ctnew += skipline(f2);
571 if (c == '\n' || c == EOF)
579 for (; j <= len[1]; j++)
580 ixnew[j] = ctnew += skipline(f2);
583 /* shellsort CACM #201 */
584 static void sort(struct line *a, int n)
586 struct line *ai, *aim, w;
591 for (j = 1; j <= n; j *= 2)
593 for (m /= 2; m != 0; m /= 2) {
595 for (j = 1; j <= k; j++) {
596 for (ai = &a[j]; ai > a; ai -= m) {
599 break; /* wraparound */
600 if (aim->value > ai[0].value ||
601 (aim->value == ai[0].value && aim->serial > ai[0].serial))
603 w.value = ai[0].value;
604 ai[0].value = aim->value;
605 aim->value = w.value;
606 w.serial = ai[0].serial;
607 ai[0].serial = aim->serial;
608 aim->serial = w.serial;
615 static void uni_range(int a, int b)
618 printf("%d,%d", a, b - a + 1);
625 static int fetch(long *f, int a, int b, FILE * lb, int ch)
627 int i, j, c, lastc, col, nc;
631 for (i = a; i <= b; i++) {
632 fseek(lb, f[i - 1], SEEK_SET);
633 nc = f[i] - f[i - 1];
636 if (cmd_flags & FLAG_T)
640 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
641 if ((c = getc(lb)) == EOF) {
642 puts("\n\\ No newline at end of file");
645 if (c == '\t' && (cmd_flags & FLAG_t)) {
658 static int asciifile(FILE * f)
660 #if ENABLE_FEATURE_DIFF_BINARY
661 unsigned char buf[BUFSIZ];
665 if ((cmd_flags & FLAG_a) || f == NULL)
668 #if ENABLE_FEATURE_DIFF_BINARY
670 cnt = fread(buf, 1, sizeof(buf), f);
671 for (i = 0; i < cnt; i++) {
672 if (!isprint(buf[i]) && !isspace(buf[i])) {
680 /* dump accumulated "unified" diff changes */
681 static void dump_unified_vec(FILE * f1, FILE * f2)
683 struct context_vec *cvp = context_vec_start;
684 int lowa, upb, lowc, upd;
688 if (context_vec_start > context_vec_ptr)
692 lowa = MAX(1, cvp->a - context);
693 upb = MIN(len[0], context_vec_ptr->b + context);
694 lowc = MAX(1, cvp->c - context);
695 upd = MIN(len[1], context_vec_ptr->d + context);
697 fputs("@@ -", stdout);
698 uni_range(lowa, upb);
700 uni_range(lowc, upd);
701 fputs(" @@", stdout);
705 * Output changes in "unified" diff format--the old and new lines
706 * are printed together.
708 for (; cvp <= context_vec_ptr; cvp++) {
715 * c: both new and old changes
716 * d: only changes in the old file
717 * a: only changes in the new file
719 if (a <= b && c <= d)
722 ch = (a <= b) ? 'd' : 'a';
726 fetch(ixold, lowa, a - 1, f1, ' ');
727 fetch(ixold, a, b, f1, '-');
728 fetch(ixnew, c, d, f2, '+');
731 fetch(ixold, lowa, a - 1, f1, ' ');
732 fetch(ixold, a, b, f1, '-');
735 fetch(ixnew, lowc, c - 1, f2, ' ');
736 fetch(ixnew, c, d, f2, '+');
740 if (ch == 'c' || ch == 'd') {
741 fetch(ixold, lowa, a - 1, f1, ' ');
742 fetch(ixold, a, b, f1, '-');
745 fetch(ixnew, lowc, c - 1, f2, ' ');
746 if (ch == 'c' || ch == 'a')
747 fetch(ixnew, c, d, f2, '+');
752 fetch(ixnew, d + 1, upd, f2, ' ');
754 context_vec_ptr = context_vec_start - 1;
758 static void print_header(const char *file1, const char *file2)
760 if (label[0] != NULL)
761 printf("%s %s\n", "---", label[0]);
763 printf("%s %s\t%s", "---", file1, ctime(&stb1.st_mtime));
764 if (label[1] != NULL)
765 printf("%s %s\n", "+++", label[1]);
767 printf("%s %s\t%s", "+++", file2, ctime(&stb2.st_mtime));
773 * Indicate that there is a difference between lines a and b of the from file
774 * to get to lines c to d of the to file. If a is greater then b then there
775 * are no lines in the from file involved and this means that there were
776 * lines appended (beginning at b). If c is greater than d then there are
777 * lines missing from the to file.
779 static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
782 static size_t max_context = 64;
786 if (cmd_flags & FLAG_q)
790 * Allocate change records as needed.
792 if (context_vec_ptr == context_vec_end - 1) {
793 ptrdiff_t offset = context_vec_ptr - context_vec_start;
796 context_vec_start = xrealloc(context_vec_start,
798 sizeof(struct context_vec));
799 context_vec_end = context_vec_start + max_context;
800 context_vec_ptr = context_vec_start + offset;
802 if (anychange == 0) {
804 * Print the context/unidiff header first time through.
806 print_header(file1, file2);
808 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
809 c > context_vec_ptr->d + (2 * context) + 1) {
811 * If this change is more than 'context' lines from the
812 * previous change, dump the record and reset it.
814 dump_unified_vec(f1, f2);
817 context_vec_ptr->a = a;
818 context_vec_ptr->b = b;
819 context_vec_ptr->c = c;
820 context_vec_ptr->d = d;
826 static void output(char *file1, FILE * f1, char *file2, FILE * f2)
829 /* Note that j0 and j1 can't be used as they are defined in math.h.
830 * This also allows the rather amusing variable 'j00'... */
831 int m, i0, i1, j00, j01;
837 J[m + 1] = len[1] + 1;
838 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
839 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
843 while (i1 < m && J[i1 + 1] == 0)
847 change(file1, f1, file2, f2, i0, i1, j00, j01);
850 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
852 if (anychange != 0) {
853 dump_unified_vec(f1, f2);
858 * The following code uses an algorithm due to Harold Stone,
859 * which finds a pair of longest identical subsequences in
862 * The major goal is to generate the match vector J.
863 * J[i] is the index of the line in file1 corresponding
864 * to line i file0. J[i] = 0 if there is no
865 * such line in file1.
867 * Lines are hashed so as to work in core. All potential
868 * matches are located by sorting the lines of each file
869 * on the hash (called ``value''). In particular, this
870 * collects the equivalence classes in file1 together.
871 * Subroutine equiv replaces the value of each line in
872 * file0 by the index of the first element of its
873 * matching equivalence in (the reordered) file1.
874 * To save space equiv squeezes file1 into a single
875 * array member in which the equivalence classes
876 * are simply concatenated, except that their first
877 * members are flagged by changing sign.
879 * Next the indices that point into member are unsorted into
880 * array class according to the original order of file0.
882 * The cleverness lies in routine stone. This marches
883 * through the lines of file0, developing a vector klist
884 * of "k-candidates". At step i a k-candidate is a matched
885 * pair of lines x,y (x in file0 y in file1) such that
886 * there is a common subsequence of length k
887 * between the first i lines of file0 and the first y
888 * lines of file1, but there is no such subsequence for
889 * any smaller y. x is the earliest possible mate to y
890 * that occurs in such a subsequence.
892 * Whenever any of the members of the equivalence class of
893 * lines in file1 matable to a line in file0 has serial number
894 * less than the y of some k-candidate, that k-candidate
895 * with the smallest such y is replaced. The new
896 * k-candidate is chained (via pred) to the current
897 * k-1 candidate so that the actual subsequence can
898 * be recovered. When a member has serial number greater
899 * that the y of all k-candidates, the klist is extended.
900 * At the end, the longest subsequence is pulled out
901 * and placed in the array J by unravel
903 * With J in hand, the matches there recorded are
904 * checked against reality to assure that no spurious
905 * matches have crept in due to hashing. If they have,
906 * they are broken, and "jackpot" is recorded--a harmless
907 * matter except that a true match for a spuriously
908 * mated line may now be unnecessarily reported as a change.
910 * Much of the complexity of the program comes simply
911 * from trying to minimize core utilization and
912 * maximize the range of doable problems by dynamically
913 * allocating what is needed and reusing what is not.
914 * The core requirements for problems larger than somewhat
915 * are (in words) 2*length(file0) + length(file1) +
916 * 3*(number of k-candidates installed), typically about
917 * 6n words for files of length n.
920 static int diffreg(char *ofile1, char *ofile2, int flags)
922 char *file1 = ofile1;
923 char *file2 = ofile2;
930 context_vec_ptr = context_vec_start - 1;
932 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
933 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
934 if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
937 if (flags & D_EMPTY1)
938 f1 = bb_xfopen(bb_dev_null, "r");
940 if (strcmp(file1, "-") == 0)
943 f1 = bb_xfopen(file1, "r");
946 if (flags & D_EMPTY2)
947 f2 = bb_xfopen(bb_dev_null, "r");
949 if (strcmp(file2, "-") == 0)
952 f2 = bb_xfopen(file2, "r");
955 if ((i = files_differ(f1, f2, flags)) == 0)
957 else if (i != 1) { /* 1 == ok */
963 if (!asciifile(f1) || !asciifile(f2)) {
969 prepare(0, f1, stb1.st_size);
970 prepare(1, f2, stb2.st_size);
972 sort(sfile[0], slen[0]);
973 sort(sfile[1], slen[1]);
975 member = (int *) file[1];
976 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
977 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
979 class = (int *) file[0];
980 unsort(sfile[0], slen[0], class);
981 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
983 klist = xmalloc((slen[0] + 2) * sizeof(int));
986 clist = xmalloc(clistlen * sizeof(struct cand));
987 i = stone(class, slen[0], member, klist);
991 J = xrealloc(J, (len[0] + 2) * sizeof(int));
996 ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
997 ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
999 output(file1, f1, file2, f2);
1011 if (file1 != ofile1)
1013 if (file2 != ofile2)
1018 #if ENABLE_FEATURE_DIFF_DIR
1019 static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1022 int flags = D_HEADER;
1025 char *fullpath1 = bb_xasprintf("%s/%s", dir1, path1);
1026 char *fullpath2 = bb_xasprintf("%s/%s", dir2, path2);
1028 if (stat(fullpath1, &stb1) != 0) {
1030 memset(&stb1, 0, sizeof(stb1));
1031 fullpath1 = bb_xasprintf("%s/%s", dir1, path2);
1033 if (stat(fullpath2, &stb2) != 0) {
1035 memset(&stb2, 0, sizeof(stb2));
1036 stb2.st_mode = stb1.st_mode;
1037 fullpath2 = bb_xasprintf("%s/%s", dir2, path1);
1040 if (stb1.st_mode == 0)
1041 stb1.st_mode = stb2.st_mode;
1043 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1044 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1048 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1050 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1053 val = diffreg(fullpath1, fullpath2, flags);
1055 print_status(val, fullpath1, fullpath2, NULL);
1059 #if ENABLE_FEATURE_DIFF_DIR
1060 static int dir_strcmp(const void *p1, const void *p2)
1062 return strcmp(*(char *const *) p1, *(char *const *) p2);
1065 /* This function adds a filename to dl, the directory listing. */
1067 static int add_to_dirlist(const char *filename,
1068 struct stat ATTRIBUTE_UNUSED * sb, void *userdata)
1071 dl = xrealloc(dl, dl_count * sizeof(char *));
1072 dl[dl_count - 1] = bb_xstrdup(filename);
1073 if (cmd_flags & FLAG_r) {
1074 int *pp = (int *) userdata;
1075 int path_len = *pp + 1;
1077 dl[dl_count - 1] = &(dl[dl_count - 1])[path_len];
1082 /* This returns a sorted directory listing. */
1083 static char **get_dir(char *path)
1089 /* If -r has been set, then the recursive_action function will be
1090 * used. Unfortunately, this outputs the root directory along with
1091 * the recursed paths, so use void *userdata to specify the string
1092 * length of the root directory. It can then be removed in
1093 * add_to_dirlist. */
1095 int path_len = strlen(path);
1096 void *userdata = &path_len;
1098 /* Reset dl_count - there's no need to free dl as bb_xrealloc does
1099 * the job nicely. */
1102 /* Now fill dl with a listing. */
1103 if (cmd_flags & FLAG_r)
1104 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL,
1110 dp = bb_opendir(path);
1111 while ((ep = readdir(dp))) {
1112 if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, ".")))
1114 add_to_dirlist(ep->d_name, NULL, NULL);
1119 /* Sort dl alphabetically. */
1120 qsort(dl, dl_count, sizeof(char *), dir_strcmp);
1122 /* Copy dl so that we can return it. */
1123 retval = xmalloc(dl_count * sizeof(char *));
1124 for (i = 0; i < dl_count; i++)
1125 retval[i] = bb_xstrdup(dl[i]);
1130 static void diffdir(char *p1, char *p2)
1133 char **dirlist1, **dirlist2;
1135 int dirlist1_count, dirlist2_count;
1138 /* Check for trailing slashes. */
1140 if (p1[strlen(p1) - 1] == '/')
1141 p1[strlen(p1) - 1] = '\0';
1142 if (p2[strlen(p2) - 1] == '/')
1143 p2[strlen(p2) - 1] = '\0';
1145 /* Get directory listings for p1 and p2. */
1147 dirlist1 = get_dir(p1);
1148 dirlist1_count = dl_count;
1149 dirlist1[dirlist1_count] = NULL;
1150 dirlist2 = get_dir(p2);
1151 dirlist2_count = dl_count;
1152 dirlist2[dirlist2_count] = NULL;
1154 /* If -S was set, find the starting point. */
1156 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1158 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1160 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
1161 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
1164 /* Now that both dirlist1 and dirlist2 contain sorted directory
1165 * listings, we can start to go through dirlist1. If both listings
1166 * contain the same file, then do a normal diff. Otherwise, behaviour
1167 * is determined by whether the -N flag is set. */
1168 while (*dirlist1 != NULL || *dirlist2 != NULL) {
1171 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1173 do_diff(p1, dp1, p2, dp2);
1176 } else if (pos < 0) {
1177 if (cmd_flags & FLAG_N)
1178 do_diff(p1, dp1, p2, NULL);
1180 print_only(p1, strlen(p1) + 1, dp1);
1183 if (cmd_flags & FLAG_N)
1184 do_diff(p1, NULL, p2, dp2);
1186 print_only(p2, strlen(p2) + 1, dp2);
1195 int diff_main(int argc, char **argv)
1200 llist_t *L_arg = NULL;
1202 bb_opt_complementally = "L::";
1204 bb_getopt_ulflags(argc, argv, "abdiL:NqrsS:tTU:wu", &L_arg, &start,
1207 if (cmd_flags & FLAG_L) {
1209 if (label[0] == NULL)
1210 label[0] = L_arg->data;
1211 else if (label[1] == NULL)
1212 label[1] = L_arg->data;
1216 L_arg = L_arg->link;
1219 /* If both label[0] and label[1] were set, they need to be swapped. */
1220 if (label[0] && label[1]) {
1224 label[1] = label[0];
1229 context = 3; /* This is the default number of lines of context. */
1230 if (cmd_flags & FLAG_U) {
1231 context = bb_xgetlarg(U_opt, 10, 1, INT_MAX);
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.
1241 bb_error_msg("Missing filename");
1244 if (strcmp(argv[0], "-") == 0) {
1245 fstat(STDIN_FILENO, &stb1);
1248 xstat(argv[0], &stb1);
1249 if (strcmp(argv[1], "-") == 0) {
1250 fstat(STDIN_FILENO, &stb2);
1253 xstat(argv[1], &stb2);
1254 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1255 bb_error_msg_and_die("Can't compare - to a directory");
1256 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1257 #if ENABLE_FEATURE_DIFF_DIR
1258 diffdir(argv[0], argv[1]);
1260 bb_error_msg_and_die("Directory comparison not supported");
1263 if (S_ISDIR(stb1.st_mode)) {
1264 argv[0] = concat_path_file(argv[0], argv[1]);
1265 xstat(argv[0], &stb1);
1267 if (S_ISDIR(stb2.st_mode)) {
1268 argv[1] = concat_path_file(argv[1], argv[0]);
1269 xstat(argv[1], &stb2);
1271 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], NULL);