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 /* XXX: FIXME: the following variables should be static, but gcc currently
69 * creates a much bigger object if we do this. [which version of gcc? --vda] */
70 /* 4.x, IIRC also 3.x --bernhard */
71 /* This is the default number of lines of context. */
77 struct stat stb1, stb2;
79 USE_FEATURE_DIFF_DIR(int dl_count;)
93 * The following struct is used to record change information
94 * doing a "context" or "unified" diff. (see routine "change" to
95 * understand the highly mnemonic field names)
98 int a; /* start line in old file */
99 int b; /* end line in old file */
100 int c; /* start line in new file */
101 int d; /* end line in new file */
104 static int *J; /* will be overlaid on class */
105 static int *class; /* will be overlaid on file[0] */
106 static int *klist; /* will be overlaid on file[0] after class */
107 static int *member; /* will be overlaid on file[1] */
110 static int pref, suff; /* length of prefix and suffix */
112 static smallint anychange;
113 static long *ixnew; /* will be overlaid on file[1] */
114 static long *ixold; /* will be overlaid on klist */
115 static struct cand *clist; /* merely a free storage pot for candidates */
116 static int clistlen; /* the length of clist */
117 static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
118 static struct context_vec *context_vec_start;
119 static struct context_vec *context_vec_end;
120 static struct context_vec *context_vec_ptr;
123 static void print_only(const char *path, size_t dirlen, const char *entry)
127 printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
131 static void print_status(int val, char *path1, char *path2, char *entry)
133 const char * const _entry = entry ? entry : "";
134 char * const _path1 = entry ? concat_path_file(path1, _entry) : path1;
135 char * const _path2 = entry ? concat_path_file(path2, _entry) : path2;
139 print_only(path1, strlen(path1), entry);
142 printf("Common subdirectories: %s and %s\n", _path1, _path2);
145 printf("Binary files %s and %s differ\n", _path1, _path2);
148 if (option_mask32 & FLAG_q)
149 printf("Files %s and %s differ\n", _path1, _path2);
152 if (option_mask32 & FLAG_s)
153 printf("Files %s and %s are identical\n", _path1, _path2);
156 printf("File %s is a %s while file %s is a %s\n",
157 _path1, "directory", _path2, "regular file");
160 printf("File %s is a %s while file %s is a %s\n",
161 _path1, "regular file", _path2, "directory");
164 printf("File %s is not a regular file or directory and was skipped\n",
168 printf("File %s is not a regular file or directory and was skipped\n",
177 static void fiddle_sum(int *sum, int t)
179 *sum = (int)(*sum * 127 + t);
182 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
184 static int readhash(FILE * f)
191 if (!(option_mask32 & FLAG_b) && !(option_mask32 & FLAG_w)) {
192 for (i = 0; (t = getc(f)) != '\n'; i++) {
202 switch (t = getc(f)) {
211 if (space && !(option_mask32 & FLAG_w)) {
229 * There is a remote possibility that we end up with a zero sum.
230 * Zero is used as an EOF marker, so return 1 instead.
232 return (sum == 0 ? 1 : sum);
237 * Check to see if the given files differ.
238 * Returns 0 if they are the same, 1 if different, and -1 on error.
240 static int files_differ(FILE * f1, FILE * f2, int flags)
244 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size ||
245 (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
248 i = fread(bb_common_bufsiz1, 1, BUFSIZ/2, f1);
249 j = fread(bb_common_bufsiz1 + BUFSIZ/2, 1, BUFSIZ/2, f2);
252 if (i == 0 && j == 0) {
253 if (ferror(f1) || ferror(f2))
257 if (memcmp(bb_common_bufsiz1,
258 bb_common_bufsiz1 + BUFSIZ/2, i) != 0)
264 static void prepare(int i, FILE * fd, off_t filesize)
272 sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;
276 p = xmalloc((sz + 3) * sizeof(struct line));
278 while ((h = readhash(fd))) {
281 p = xrealloc(p, (sz + 3) * sizeof(struct line));
290 static void prune(void)
294 for (pref = 0; pref < len[0] && pref < len[1] &&
295 file[0][pref + 1].value == file[1][pref + 1].value; pref++)
297 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
298 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
301 for (j = 0; j < 2; j++) {
302 sfile[j] = file[j] + pref;
303 slen[j] = len[j] - pref - suff;
304 for (i = 0; i <= slen[j]; i++)
305 sfile[j][i].serial = i;
310 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
315 while (i <= n && j <= m) {
316 if (a[i].value < b[j].value)
318 else if (a[i].value == b[j].value)
329 while (b[j + 1].value == b[j].value) {
338 static int isqrt(int n)
350 } while ((x - y) > 1 || (x - y) < -1);
356 static int newcand(int x, int y, int pred)
360 if (clen == clistlen) {
361 clistlen = clistlen * 11 / 10;
362 clist = xrealloc(clist, clistlen * sizeof(struct cand));
372 static int search(int *c, int k, int y)
376 if (clist[c[k]].y < y) /* quick look for typical case */
396 static int stone(int *a, int n, int *b, int *c)
400 unsigned int numtries;
402 #if ENABLE_FEATURE_DIFF_MINIMAL
403 const unsigned int bound =
404 (option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
406 const unsigned int bound = MAX(256, isqrt(n));
409 c[0] = newcand(0, 0, 0);
410 for (i = 1; i <= n; i++) {
419 if (y <= clist[oldc].y)
425 if (clist[c[l]].y <= y)
428 c[l] = newcand(i, y, oldc);
433 c[l] = newcand(i, y, oldc);
437 } while ((y = b[++j]) > 0 && numtries < bound);
443 static void unravel(int p)
448 for (i = 0; i <= len[0]; i++)
449 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0;
450 for (q = clist + p; q->y != 0; q = clist + q->pred)
451 J[q->x + pref] = q->y + pref;
455 static void unsort(struct line *f, int l, int *b)
459 a = xmalloc((l + 1) * sizeof(int));
460 for (i = 1; i <= l; i++)
461 a[f[i].serial] = f[i].value;
462 for (i = 1; i <= l; i++)
468 static int skipline(FILE * f)
472 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
479 * Check does double duty:
480 * 1. ferret out any fortuitous correspondences due
481 * to confounding by hashing (which result in "jackpot")
482 * 2. collect random access indexes to the two files
484 static void check(FILE * f1, FILE * f2)
486 int i, j, jackpot, c, d;
492 ixold[0] = ixnew[0] = 0;
495 for (i = 1; i <= len[0]; i++) {
497 ixold[i] = ctold += skipline(f1);
501 ixnew[j] = ctnew += skipline(f2);
504 if ((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)
505 || (option_mask32 & FLAG_i)) {
510 * GNU diff ignores a missing newline
511 * in one file if bflag || wflag.
513 if (((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)) &&
514 ((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
519 if ((option_mask32 & FLAG_b) && isspace(c) && isspace(d)) {
524 } while (isspace(c = getc(f1)));
529 } while (isspace(d = getc(f2)));
530 } else if (option_mask32 & FLAG_w) {
531 while (isspace(c) && c != '\n') {
535 while (isspace(d) && d != '\n') {
543 if (c != '\n' && c != EOF)
544 ctold += skipline(f1);
545 if (d != '\n' && c != EOF)
546 ctnew += skipline(f2);
549 if (c == '\n' || c == EOF)
556 if ((c = getc(f1)) != (d = getc(f2))) {
558 if (c != '\n' && c != EOF)
559 ctold += skipline(f1);
560 if (d != '\n' && c != EOF)
561 ctnew += skipline(f2);
564 if (c == '\n' || c == EOF)
572 for (; j <= len[1]; j++)
573 ixnew[j] = ctnew += skipline(f2);
577 /* shellsort CACM #201 */
578 static void sort(struct line *a, int n)
580 struct line *ai, *aim, w;
585 for (j = 1; j <= n; j *= 2)
587 for (m /= 2; m != 0; m /= 2) {
589 for (j = 1; j <= k; j++) {
590 for (ai = &a[j]; ai > a; ai -= m) {
593 break; /* wraparound */
594 if (aim->value > ai[0].value ||
595 (aim->value == ai[0].value && aim->serial > ai[0].serial))
597 w.value = ai[0].value;
598 ai[0].value = aim->value;
599 aim->value = w.value;
600 w.serial = ai[0].serial;
601 ai[0].serial = aim->serial;
602 aim->serial = w.serial;
609 static void uni_range(int a, int b)
612 printf("%d,%d", a, b - a + 1);
620 static void fetch(long *f, int a, int b, FILE * lb, int ch)
622 int i, j, c, lastc, col, nc;
626 for (i = a; i <= b; i++) {
627 fseek(lb, f[i - 1], SEEK_SET);
628 nc = f[i] - f[i - 1];
631 if (option_mask32 & FLAG_T)
635 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
636 if ((c = getc(lb)) == EOF) {
637 printf("\n\\ No newline at end of file\n");
640 if (c == '\t' && (option_mask32 & FLAG_t)) {
654 static int asciifile(FILE * f)
656 #if ENABLE_FEATURE_DIFF_BINARY
660 if ((option_mask32 & FLAG_a) || f == NULL)
663 #if ENABLE_FEATURE_DIFF_BINARY
665 cnt = fread(bb_common_bufsiz1, 1, BUFSIZ, f);
666 for (i = 0; i < cnt; i++) {
667 if (!isprint(bb_common_bufsiz1[i])
668 && !isspace(bb_common_bufsiz1[i])) {
677 /* dump accumulated "unified" diff changes */
678 static void dump_unified_vec(FILE * f1, FILE * f2)
680 struct context_vec *cvp = context_vec_start;
681 int lowa, upb, lowc, upd;
685 if (context_vec_start > context_vec_ptr)
689 lowa = MAX(1, cvp->a - context);
690 upb = MIN(len[0], context_vec_ptr->b + context);
691 lowc = MAX(1, cvp->c - context);
692 upd = MIN(len[1], context_vec_ptr->d + context);
695 uni_range(lowa, upb);
697 uni_range(lowc, upd);
701 * Output changes in "unified" diff format--the old and new lines
702 * are printed together.
704 for (; cvp <= context_vec_ptr; cvp++) {
711 * c: both new and old changes
712 * d: only changes in the old file
713 * a: only changes in the new file
715 if (a <= b && c <= d)
718 ch = (a <= b) ? 'd' : 'a';
719 if (ch == 'c' || ch == 'd') {
720 fetch(ixold, lowa, a - 1, f1, ' ');
721 fetch(ixold, a, b, f1, '-');
724 fetch(ixnew, lowc, c - 1, f2, ' ');
725 if (ch == 'c' || ch == 'a')
726 fetch(ixnew, c, d, f2, '+');
730 fetch(ixnew, d + 1, upd, f2, ' ');
732 context_vec_ptr = context_vec_start - 1;
736 static void print_header(const char *file1, const char *file2)
739 printf("--- %s\n", label1);
741 printf("--- %s\t%s", file1, ctime(&stb1.st_mtime));
743 printf("+++ %s\n", label2);
745 printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
750 * Indicate that there is a difference between lines a and b of the from file
751 * to get to lines c to d of the to file. If a is greater than b then there
752 * are no lines in the from file involved and this means that there were
753 * lines appended (beginning at b). If c is greater than d then there are
754 * lines missing from the to file.
756 static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
759 static size_t max_context = 64;
761 if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
767 * Allocate change records as needed.
769 if (context_vec_ptr == context_vec_end - 1) {
770 ptrdiff_t offset = context_vec_ptr - context_vec_start;
773 context_vec_start = xrealloc(context_vec_start,
774 max_context * sizeof(struct context_vec));
775 context_vec_end = context_vec_start + max_context;
776 context_vec_ptr = context_vec_start + offset;
778 if (anychange == 0) {
780 * Print the context/unidiff header first time through.
782 print_header(file1, file2);
783 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
784 c > context_vec_ptr->d + (2 * context) + 1) {
786 * If this change is more than 'context' lines from the
787 * previous change, dump the record and reset it.
789 dump_unified_vec(f1, f2);
792 context_vec_ptr->a = a;
793 context_vec_ptr->b = b;
794 context_vec_ptr->c = c;
795 context_vec_ptr->d = d;
800 static void output(char *file1, FILE * f1, char *file2, FILE * f2)
802 /* Note that j0 and j1 can't be used as they are defined in math.h.
803 * This also allows the rather amusing variable 'j00'... */
804 int m, i0, i1, j00, j01;
810 J[m + 1] = len[1] + 1;
811 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
812 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
816 while (i1 < m && J[i1 + 1] == 0)
820 change(file1, f1, file2, f2, i0, i1, j00, j01);
823 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
825 if (anychange != 0 && !(option_mask32 & FLAG_q)) {
826 dump_unified_vec(f1, f2);
831 * The following code uses an algorithm due to Harold Stone,
832 * which finds a pair of longest identical subsequences in
835 * The major goal is to generate the match vector J.
836 * J[i] is the index of the line in file1 corresponding
837 * to line i file0. J[i] = 0 if there is no
838 * such line in file1.
840 * Lines are hashed so as to work in core. All potential
841 * matches are located by sorting the lines of each file
842 * on the hash (called ``value''). In particular, this
843 * collects the equivalence classes in file1 together.
844 * Subroutine equiv replaces the value of each line in
845 * file0 by the index of the first element of its
846 * matching equivalence in (the reordered) file1.
847 * To save space equiv squeezes file1 into a single
848 * array member in which the equivalence classes
849 * are simply concatenated, except that their first
850 * members are flagged by changing sign.
852 * Next the indices that point into member are unsorted into
853 * array class according to the original order of file0.
855 * The cleverness lies in routine stone. This marches
856 * through the lines of file0, developing a vector klist
857 * of "k-candidates". At step i a k-candidate is a matched
858 * pair of lines x,y (x in file0 y in file1) such that
859 * there is a common subsequence of length k
860 * between the first i lines of file0 and the first y
861 * lines of file1, but there is no such subsequence for
862 * any smaller y. x is the earliest possible mate to y
863 * that occurs in such a subsequence.
865 * Whenever any of the members of the equivalence class of
866 * lines in file1 matable to a line in file0 has serial number
867 * less than the y of some k-candidate, that k-candidate
868 * with the smallest such y is replaced. The new
869 * k-candidate is chained (via pred) to the current
870 * k-1 candidate so that the actual subsequence can
871 * be recovered. When a member has serial number greater
872 * that the y of all k-candidates, the klist is extended.
873 * At the end, the longest subsequence is pulled out
874 * and placed in the array J by unravel
876 * With J in hand, the matches there recorded are
877 * checked against reality to assure that no spurious
878 * matches have crept in due to hashing. If they have,
879 * they are broken, and "jackpot" is recorded--a harmless
880 * matter except that a true match for a spuriously
881 * mated line may now be unnecessarily reported as a change.
883 * Much of the complexity of the program comes simply
884 * from trying to minimize core utilization and
885 * maximize the range of doable problems by dynamically
886 * allocating what is needed and reusing what is not.
887 * The core requirements for problems larger than somewhat
888 * are (in words) 2*length(file0) + length(file1) +
889 * 3*(number of k-candidates installed), typically about
890 * 6n words for files of length n.
892 static unsigned diffreg(char * ofile1, char * ofile2, int flags)
894 char *file1 = ofile1;
895 char *file2 = ofile2;
896 FILE *f1 = stdin, *f2 = stdin;
901 context_vec_ptr = context_vec_start - 1;
903 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
904 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
908 if (LONE_DASH(file1) && LONE_DASH(file2))
911 if (flags & D_EMPTY1)
912 f1 = xfopen(bb_dev_null, "r");
913 else if (NOT_LONE_DASH(file1))
914 f1 = xfopen(file1, "r");
915 if (flags & D_EMPTY2)
916 f2 = xfopen(bb_dev_null, "r");
917 else if (NOT_LONE_DASH(file2))
918 f2 = xfopen(file2, "r");
920 /* We can't diff non-seekable stream - we use rewind(), fseek().
921 * This can be fixed (volunteers?).
922 * Meanwhile we should check it here by stat'ing input fds,
923 * but I am lazy and check that in main() instead.
924 * Check in main won't catch "diffing fifos buried in subdirectories"
925 * failure scenario - not very likely in real life... */
927 i = files_differ(f1, f2, flags);
930 else if (i != 1) { /* 1 == ok */
936 if (!asciifile(f1) || !asciifile(f2)) {
942 prepare(0, f1, stb1.st_size);
943 prepare(1, f2, stb2.st_size);
945 sort(sfile[0], slen[0]);
946 sort(sfile[1], slen[1]);
948 member = (int *) file[1];
949 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
950 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
952 class = (int *) file[0];
953 unsort(sfile[0], slen[0], class);
954 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
956 klist = xmalloc((slen[0] + 2) * sizeof(int));
959 clist = xmalloc(clistlen * sizeof(struct cand));
960 i = stone(class, slen[0], member, klist);
964 J = xrealloc(J, (len[0] + 2) * sizeof(int));
969 ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
970 ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
972 output(file1, f1, file2, f2);
980 fclose_if_not_stdin(f1);
981 fclose_if_not_stdin(f2);
990 #if ENABLE_FEATURE_DIFF_DIR
991 static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
993 int flags = D_HEADER;
996 char *fullpath1 = concat_path_file(dir1, path1);
997 char *fullpath2 = concat_path_file(dir2, path2);
999 if (stat(fullpath1, &stb1) != 0) {
1001 memset(&stb1, 0, sizeof(stb1));
1002 if (ENABLE_FEATURE_CLEAN_UP)
1004 fullpath1 = concat_path_file(dir1, path2);
1006 if (stat(fullpath2, &stb2) != 0) {
1008 memset(&stb2, 0, sizeof(stb2));
1009 stb2.st_mode = stb1.st_mode;
1010 if (ENABLE_FEATURE_CLEAN_UP)
1012 fullpath2 = concat_path_file(dir2, path1);
1015 if (stb1.st_mode == 0)
1016 stb1.st_mode = stb2.st_mode;
1018 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1019 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1023 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1025 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1028 val = diffreg(fullpath1, fullpath2, flags);
1030 print_status(val, fullpath1, fullpath2, NULL);
1035 #if ENABLE_FEATURE_DIFF_DIR
1036 static int dir_strcmp(const void *p1, const void *p2)
1038 return strcmp(*(char *const *) p1, *(char *const *) p2);
1042 /* This function adds a filename to dl, the directory listing. */
1043 static int add_to_dirlist(const char *filename,
1044 struct stat ATTRIBUTE_UNUSED * sb, void *userdata,
1045 int depth ATTRIBUTE_UNUSED)
1048 dl = xrealloc(dl, dl_count * sizeof(char *));
1049 dl[dl_count - 1] = xstrdup(filename);
1050 if (option_mask32 & FLAG_r) {
1051 int *pp = (int *) userdata;
1052 int path_len = *pp + 1;
1054 dl[dl_count - 1] = &(dl[dl_count - 1])[path_len];
1060 /* This returns a sorted directory listing. */
1061 static char **get_dir(char *path)
1066 /* If -r has been set, then the recursive_action function will be
1067 * used. Unfortunately, this outputs the root directory along with
1068 * the recursed paths, so use void *userdata to specify the string
1069 * length of the root directory. It can then be removed in
1070 * add_to_dirlist. */
1072 int path_len = strlen(path);
1073 void *userdata = &path_len;
1075 /* Reset dl_count - there's no need to free dl as xrealloc does
1076 * the job nicely. */
1079 /* Now fill dl with a listing. */
1080 if (option_mask32 & FLAG_r)
1081 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL,
1087 dp = warn_opendir(path);
1088 while ((ep = readdir(dp))) {
1089 if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
1091 add_to_dirlist(ep->d_name, NULL, NULL, 0);
1096 /* Sort dl alphabetically. */
1097 qsort(dl, dl_count, sizeof(char *), dir_strcmp);
1099 /* Copy dl so that we can return it. */
1100 retval = xmalloc(dl_count * sizeof(char *));
1101 for (i = 0; i < dl_count; i++)
1102 retval[i] = xstrdup(dl[i]);
1108 static void diffdir(char *p1, char *p2)
1110 char **dirlist1, **dirlist2;
1112 int dirlist1_count, dirlist2_count;
1115 /* Check for trailing slashes. */
1117 dp1 = last_char_is(p1, '/');
1120 dp2 = last_char_is(p2, '/');
1124 /* Get directory listings for p1 and p2. */
1126 dirlist1 = get_dir(p1);
1127 dirlist1_count = dl_count;
1128 dirlist1[dirlist1_count] = NULL;
1129 dirlist2 = get_dir(p2);
1130 dirlist2_count = dl_count;
1131 dirlist2[dirlist2_count] = NULL;
1133 /* If -S was set, find the starting point. */
1135 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1137 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1139 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
1140 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
1143 /* Now that both dirlist1 and dirlist2 contain sorted directory
1144 * listings, we can start to go through dirlist1. If both listings
1145 * contain the same file, then do a normal diff. Otherwise, behaviour
1146 * is determined by whether the -N flag is set. */
1147 while (*dirlist1 != NULL || *dirlist2 != NULL) {
1150 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1152 do_diff(p1, dp1, p2, dp2);
1155 } else if (pos < 0) {
1156 if (option_mask32 & FLAG_N)
1157 do_diff(p1, dp1, p2, NULL);
1159 print_only(p1, strlen(p1) + 1, dp1);
1162 if (option_mask32 & FLAG_N)
1163 do_diff(p1, NULL, p2, dp2);
1165 print_only(p2, strlen(p2) + 1, dp2);
1173 int diff_main(int argc, char **argv)
1175 smallint gotstdin = 0;
1178 llist_t *L_arg = NULL;
1180 /* exactly 2 params; collect multiple -L <label> */
1181 opt_complementary = "=2:L::";
1182 getopt32(argc, argv, "abdiL:NqrsS:tTU:wu"
1183 "p" /* ignored (for compatibility) */,
1184 &L_arg, &start, &U_opt);
1188 if (label1 && label2)
1191 label1 = L_arg->data;
1192 else { /* then label2 is NULL */
1194 label1 = L_arg->data;
1196 /* we leak L_arg here... */
1197 L_arg = L_arg->link;
1199 if (option_mask32 & FLAG_U)
1200 context = xatou_range(U_opt, 1, INT_MAX);
1203 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1204 * driver routine. Both drivers use the contents of stb1 and stb2.
1209 if (LONE_DASH(f1)) {
1210 fstat(STDIN_FILENO, &stb1);
1214 if (LONE_DASH(f2)) {
1215 fstat(STDIN_FILENO, &stb2);
1219 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1220 bb_error_msg_and_die("can't compare - to a directory");
1221 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1222 #if ENABLE_FEATURE_DIFF_DIR
1225 bb_error_msg_and_die("directory comparison not supported");
1228 if (S_ISDIR(stb1.st_mode)) {
1229 f1 = concat_path_file(f1, f2);
1232 if (S_ISDIR(stb2.st_mode)) {
1233 f2 = concat_path_file(f2, f1);
1237 /* We can't diff e.g. stdin supplied by a pipe - we use rewind(), fseek().
1238 * This can be fixed (volunteers?) */
1239 if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
1240 bb_error_msg_and_die("can't diff non-seekable stream");
1241 print_status(diffreg(f1, f2, 0), f1, f2, NULL);