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 /* 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 /* Works for gcc 3.4.3. Sizes without and with "static":
72 # size busybox.t[34]/coreutils/diff.o
73 text data bss dec hex filename
74 6969 8 305 7282 1c72 busybox.t3/coreutils/diff.o
75 6969 8 305 7282 1c72 busybox.t4/coreutils/diff.o
78 /* This is the default number of lines of context. */
79 static int context
= 3;
82 static const char *label1
;
83 static const char *label2
;
84 static struct stat stb1
, stb2
;
85 USE_FEATURE_DIFF_DIR(static char **dl
;)
86 USE_FEATURE_DIFF_DIR(static int dl_count
;)
100 * The following struct is used to record change information
101 * doing a "context" or "unified" diff. (see routine "change" to
102 * understand the highly mnemonic field names)
105 int a
; /* start line in old file */
106 int b
; /* end line in old file */
107 int c
; /* start line in new file */
108 int d
; /* end line in new file */
111 static int *J
; /* will be overlaid on class */
112 static int *class; /* will be overlaid on file[0] */
113 static int *klist
; /* will be overlaid on file[0] after class */
114 static int *member
; /* will be overlaid on file[1] */
117 static int pref
, suff
; /* length of prefix and suffix */
119 static bool anychange
;
120 static long *ixnew
; /* will be overlaid on file[1] */
121 static long *ixold
; /* will be overlaid on klist */
122 static struct cand
*clist
; /* merely a free storage pot for candidates */
123 static int clistlen
; /* the length of clist */
124 static struct line
*sfile
[2]; /* shortened by pruning common prefix/suffix */
125 static struct context_vec
*context_vec_start
;
126 static struct context_vec
*context_vec_end
;
127 static struct context_vec
*context_vec_ptr
;
130 static void print_only(const char *path
, size_t dirlen
, const char *entry
)
134 printf("Only in %.*s: %s\n", (int) dirlen
, path
, entry
);
138 static void print_status(int val
, char *path1
, char *path2
, char *entry
)
140 const char * const _entry
= entry
? entry
: "";
141 char * const _path1
= entry
? concat_path_file(path1
, _entry
) : path1
;
142 char * const _path2
= entry
? concat_path_file(path2
, _entry
) : path2
;
146 print_only(path1
, strlen(path1
), entry
);
149 printf("Common subdirectories: %s and %s\n", _path1
, _path2
);
152 printf("Binary files %s and %s differ\n", _path1
, _path2
);
155 if (option_mask32
& FLAG_q
)
156 printf("Files %s and %s differ\n", _path1
, _path2
);
159 if (option_mask32
& FLAG_s
)
160 printf("Files %s and %s are identical\n", _path1
, _path2
);
163 printf("File %s is a %s while file %s is a %s\n",
164 _path1
, "directory", _path2
, "regular file");
167 printf("File %s is a %s while file %s is a %s\n",
168 _path1
, "regular file", _path2
, "directory");
171 printf("File %s is not a regular file or directory and was skipped\n",
175 printf("File %s is not a regular file or directory and was skipped\n",
184 static void fiddle_sum(int *sum
, int t
)
186 *sum
= (int)(*sum
* 127 + t
);
189 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
191 static int readhash(FILE * f
)
198 if (!(option_mask32
& (FLAG_b
| FLAG_w
))) {
199 for (i
= 0; (t
= getc(f
)) != '\n'; i
++) {
209 switch (t
= getc(f
)) {
218 if (space
&& !(option_mask32
& FLAG_w
)) {
236 * There is a remote possibility that we end up with a zero sum.
237 * Zero is used as an EOF marker, so return 1 instead.
239 return (sum
== 0 ? 1 : sum
);
244 * Check to see if the given files differ.
245 * Returns 0 if they are the same, 1 if different, and -1 on error.
247 static int files_differ(FILE * f1
, FILE * f2
, int flags
)
251 if ((flags
& (D_EMPTY1
| D_EMPTY2
)) || stb1
.st_size
!= stb2
.st_size
252 || (stb1
.st_mode
& S_IFMT
) != (stb2
.st_mode
& S_IFMT
)
257 i
= fread(bb_common_bufsiz1
, 1, BUFSIZ
/2, f1
);
258 j
= fread(bb_common_bufsiz1
+ BUFSIZ
/2, 1, BUFSIZ
/2, f2
);
262 return (ferror(f1
) || ferror(f2
));
263 if (memcmp(bb_common_bufsiz1
,
264 bb_common_bufsiz1
+ BUFSIZ
/2, i
) != 0)
270 static void prepare(int i
, FILE * fd
, off_t filesize
)
278 sz
= (filesize
<= FSIZE_MAX
? filesize
: FSIZE_MAX
) / 25;
282 p
= xmalloc((sz
+ 3) * sizeof(struct line
));
284 while ((h
= readhash(fd
))) {
287 p
= xrealloc(p
, (sz
+ 3) * sizeof(struct line
));
296 static void prune(void)
300 for (pref
= 0; pref
< len
[0] && pref
< len
[1] &&
301 file
[0][pref
+ 1].value
== file
[1][pref
+ 1].value
; pref
++)
303 for (suff
= 0; suff
< len
[0] - pref
&& suff
< len
[1] - pref
&&
304 file
[0][len
[0] - suff
].value
== file
[1][len
[1] - suff
].value
;
307 for (j
= 0; j
< 2; j
++) {
308 sfile
[j
] = file
[j
] + pref
;
309 slen
[j
] = len
[j
] - pref
- suff
;
310 for (i
= 0; i
<= slen
[j
]; i
++)
311 sfile
[j
][i
].serial
= i
;
316 static void equiv(struct line
*a
, int n
, struct line
*b
, int m
, int *c
)
321 while (i
<= n
&& j
<= m
) {
322 if (a
[i
].value
< b
[j
].value
)
324 else if (a
[i
].value
== b
[j
].value
)
335 while (b
[j
+ 1].value
== b
[j
].value
) {
344 static int isqrt(int n
)
356 } while ((x
- y
) > 1 || (x
- y
) < -1);
362 static int newcand(int x
, int y
, int pred
)
366 if (clen
== clistlen
) {
367 clistlen
= clistlen
* 11 / 10;
368 clist
= xrealloc(clist
, clistlen
* sizeof(struct cand
));
378 static int search(int *c
, int k
, int y
)
382 if (clist
[c
[k
]].y
< y
) /* quick look for typical case */
402 static int stone(int *a
, int n
, int *b
, int *c
)
406 unsigned int numtries
;
408 #if ENABLE_FEATURE_DIFF_MINIMAL
409 const unsigned int bound
=
410 (option_mask32
& FLAG_d
) ? UINT_MAX
: MAX(256, isqrt(n
));
412 const unsigned int bound
= MAX(256, isqrt(n
));
415 c
[0] = newcand(0, 0, 0);
416 for (i
= 1; i
<= n
; i
++) {
425 if (y
<= clist
[oldc
].y
)
431 if (clist
[c
[l
]].y
<= y
)
434 c
[l
] = newcand(i
, y
, oldc
);
439 c
[l
] = newcand(i
, y
, oldc
);
443 } while ((y
= b
[++j
]) > 0 && numtries
< bound
);
449 static void unravel(int p
)
454 for (i
= 0; i
<= len
[0]; i
++)
455 J
[i
] = i
<= pref
? i
: i
> len
[0] - suff
? i
+ len
[1] - len
[0] : 0;
456 for (q
= clist
+ p
; q
->y
!= 0; q
= clist
+ q
->pred
)
457 J
[q
->x
+ pref
] = q
->y
+ pref
;
461 static void unsort(struct line
*f
, int l
, int *b
)
465 a
= xmalloc((l
+ 1) * sizeof(int));
466 for (i
= 1; i
<= l
; i
++)
467 a
[f
[i
].serial
] = f
[i
].value
;
468 for (i
= 1; i
<= l
; i
++)
474 static int skipline(FILE * f
)
478 for (i
= 1; (c
= getc(f
)) != '\n' && c
!= EOF
; i
++)
485 * Check does double duty:
486 * 1. ferret out any fortuitous correspondences due
487 * to confounding by hashing (which result in "jackpot")
488 * 2. collect random access indexes to the two files
490 static void check(FILE * f1
, FILE * f2
)
492 int i
, j
, jackpot
, c
, d
;
498 ixold
[0] = ixnew
[0] = 0;
501 for (i
= 1; i
<= len
[0]; i
++) {
503 ixold
[i
] = ctold
+= skipline(f1
);
507 ixnew
[j
] = ctnew
+= skipline(f2
);
510 if ((option_mask32
& FLAG_b
) || (option_mask32
& FLAG_w
)
511 || (option_mask32
& FLAG_i
)) {
516 * GNU diff ignores a missing newline
517 * in one file if bflag || wflag.
519 if (((option_mask32
& FLAG_b
) || (option_mask32
& FLAG_w
)) &&
520 ((c
== EOF
&& d
== '\n') || (c
== '\n' && d
== EOF
))) {
525 if ((option_mask32
& FLAG_b
) && isspace(c
) && isspace(d
)) {
530 } while (isspace(c
= getc(f1
)));
535 } while (isspace(d
= getc(f2
)));
536 } else if (option_mask32
& FLAG_w
) {
537 while (isspace(c
) && c
!= '\n') {
541 while (isspace(d
) && d
!= '\n') {
549 if (c
!= '\n' && c
!= EOF
)
550 ctold
+= skipline(f1
);
551 if (d
!= '\n' && c
!= EOF
)
552 ctnew
+= skipline(f2
);
555 if (c
== '\n' || c
== EOF
)
562 if ((c
= getc(f1
)) != (d
= getc(f2
))) {
564 if (c
!= '\n' && c
!= EOF
)
565 ctold
+= skipline(f1
);
566 if (d
!= '\n' && c
!= EOF
)
567 ctnew
+= skipline(f2
);
570 if (c
== '\n' || c
== EOF
)
578 for (; j
<= len
[1]; j
++)
579 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);
626 static void fetch(long *f
, int a
, int b
, FILE * lb
, int ch
)
628 int i
, j
, c
, lastc
, col
, nc
;
632 for (i
= a
; i
<= b
; i
++) {
633 fseek(lb
, f
[i
- 1], SEEK_SET
);
634 nc
= f
[i
] - f
[i
- 1];
637 if (option_mask32
& FLAG_T
)
641 for (j
= 0, lastc
= '\0'; j
< nc
; j
++, lastc
= c
) {
642 if ((c
= getc(lb
)) == EOF
) {
643 printf("\n\\ No newline at end of file\n");
646 if (c
== '\t' && (option_mask32
& FLAG_t
)) {
659 static int asciifile(FILE * f
)
661 #if ENABLE_FEATURE_DIFF_BINARY
665 if ((option_mask32
& FLAG_a
) || f
== NULL
)
668 #if ENABLE_FEATURE_DIFF_BINARY
670 cnt
= fread(bb_common_bufsiz1
, 1, BUFSIZ
, f
);
671 for (i
= 0; i
< cnt
; i
++) {
672 if (!isprint(bb_common_bufsiz1
[i
])
673 && !isspace(bb_common_bufsiz1
[i
])) {
682 /* dump accumulated "unified" diff changes */
683 static void dump_unified_vec(FILE * f1
, FILE * f2
)
685 struct context_vec
*cvp
= context_vec_start
;
686 int lowa
, upb
, lowc
, upd
;
690 if (context_vec_start
> context_vec_ptr
)
694 lowa
= MAX(1, cvp
->a
- context
);
695 upb
= MIN(len
[0], context_vec_ptr
->b
+ context
);
696 lowc
= MAX(1, cvp
->c
- context
);
697 upd
= MIN(len
[1], context_vec_ptr
->d
+ context
);
700 uni_range(lowa
, upb
);
702 uni_range(lowc
, upd
);
706 * Output changes in "unified" diff format--the old and new lines
707 * are printed together.
709 for (; cvp
<= context_vec_ptr
; cvp
++) {
716 * c: both new and old changes
717 * d: only changes in the old file
718 * a: only changes in the new file
720 if (a
<= b
&& c
<= d
)
723 ch
= (a
<= b
) ? 'd' : 'a';
724 if (ch
== 'c' || ch
== 'd') {
725 fetch(ixold
, lowa
, a
- 1, f1
, ' ');
726 fetch(ixold
, a
, b
, f1
, '-');
729 fetch(ixnew
, lowc
, c
- 1, f2
, ' ');
730 if (ch
== 'c' || ch
== 'a')
731 fetch(ixnew
, c
, d
, f2
, '+');
735 fetch(ixnew
, d
+ 1, upd
, f2
, ' ');
737 context_vec_ptr
= context_vec_start
- 1;
741 static void print_header(const char *file1
, const char *file2
)
744 printf("--- %s\n", label1
);
746 printf("--- %s\t%s", file1
, ctime(&stb1
.st_mtime
));
748 printf("+++ %s\n", label2
);
750 printf("+++ %s\t%s", file2
, ctime(&stb2
.st_mtime
));
755 * Indicate that there is a difference between lines a and b of the from file
756 * to get to lines c to d of the to file. If a is greater than b then there
757 * are no lines in the from file involved and this means that there were
758 * lines appended (beginning at b). If c is greater than d then there are
759 * lines missing from the to file.
761 static void change(char *file1
, FILE * f1
, char *file2
, FILE * f2
, int a
,
764 static size_t max_context
= 64;
766 if ((a
> b
&& c
> d
) || (option_mask32
& FLAG_q
)) {
772 * Allocate change records as needed.
774 if (context_vec_ptr
== context_vec_end
- 1) {
775 ptrdiff_t offset
= context_vec_ptr
- context_vec_start
;
778 context_vec_start
= xrealloc(context_vec_start
,
779 max_context
* sizeof(struct context_vec
));
780 context_vec_end
= context_vec_start
+ max_context
;
781 context_vec_ptr
= context_vec_start
+ offset
;
783 if (anychange
== 0) {
785 * Print the context/unidiff header first time through.
787 print_header(file1
, file2
);
788 } else if (a
> context_vec_ptr
->b
+ (2 * context
) + 1 &&
789 c
> context_vec_ptr
->d
+ (2 * context
) + 1) {
791 * If this change is more than 'context' lines from the
792 * previous change, dump the record and reset it.
794 dump_unified_vec(f1
, f2
);
797 context_vec_ptr
->a
= a
;
798 context_vec_ptr
->b
= b
;
799 context_vec_ptr
->c
= c
;
800 context_vec_ptr
->d
= d
;
805 static void output(char *file1
, FILE * f1
, char *file2
, FILE * f2
)
807 /* Note that j0 and j1 can't be used as they are defined in math.h.
808 * This also allows the rather amusing variable 'j00'... */
809 int m
, i0
, i1
, j00
, j01
;
815 J
[m
+ 1] = len
[1] + 1;
816 for (i0
= 1; i0
<= m
; i0
= i1
+ 1) {
817 while (i0
<= m
&& J
[i0
] == J
[i0
- 1] + 1)
821 while (i1
< m
&& J
[i1
+ 1] == 0)
825 change(file1
, f1
, file2
, f2
, i0
, i1
, j00
, j01
);
828 change(file1
, f1
, file2
, f2
, 1, 0, 1, len
[1]);
830 if (anychange
!= 0 && !(option_mask32
& FLAG_q
)) {
831 dump_unified_vec(f1
, f2
);
836 * The following code uses an algorithm due to Harold Stone,
837 * which finds a pair of longest identical subsequences in
840 * The major goal is to generate the match vector J.
841 * J[i] is the index of the line in file1 corresponding
842 * to line i file0. J[i] = 0 if there is no
843 * such line in file1.
845 * Lines are hashed so as to work in core. All potential
846 * matches are located by sorting the lines of each file
847 * on the hash (called ``value''). In particular, this
848 * collects the equivalence classes in file1 together.
849 * Subroutine equiv replaces the value of each line in
850 * file0 by the index of the first element of its
851 * matching equivalence in (the reordered) file1.
852 * To save space equiv squeezes file1 into a single
853 * array member in which the equivalence classes
854 * are simply concatenated, except that their first
855 * members are flagged by changing sign.
857 * Next the indices that point into member are unsorted into
858 * array class according to the original order of file0.
860 * The cleverness lies in routine stone. This marches
861 * through the lines of file0, developing a vector klist
862 * of "k-candidates". At step i a k-candidate is a matched
863 * pair of lines x,y (x in file0 y in file1) such that
864 * there is a common subsequence of length k
865 * between the first i lines of file0 and the first y
866 * lines of file1, but there is no such subsequence for
867 * any smaller y. x is the earliest possible mate to y
868 * that occurs in such a subsequence.
870 * Whenever any of the members of the equivalence class of
871 * lines in file1 matable to a line in file0 has serial number
872 * less than the y of some k-candidate, that k-candidate
873 * with the smallest such y is replaced. The new
874 * k-candidate is chained (via pred) to the current
875 * k-1 candidate so that the actual subsequence can
876 * be recovered. When a member has serial number greater
877 * that the y of all k-candidates, the klist is extended.
878 * At the end, the longest subsequence is pulled out
879 * and placed in the array J by unravel
881 * With J in hand, the matches there recorded are
882 * checked against reality to assure that no spurious
883 * matches have crept in due to hashing. If they have,
884 * they are broken, and "jackpot" is recorded--a harmless
885 * matter except that a true match for a spuriously
886 * mated line may now be unnecessarily reported as a change.
888 * Much of the complexity of the program comes simply
889 * from trying to minimize core utilization and
890 * maximize the range of doable problems by dynamically
891 * allocating what is needed and reusing what is not.
892 * The core requirements for problems larger than somewhat
893 * are (in words) 2*length(file0) + length(file1) +
894 * 3*(number of k-candidates installed), typically about
895 * 6n words for files of length n.
897 static unsigned diffreg(char * ofile1
, char * ofile2
, int flags
)
899 char *file1
= ofile1
;
900 char *file2
= ofile2
;
901 FILE *f1
= stdin
, *f2
= stdin
;
906 context_vec_ptr
= context_vec_start
- 1;
908 if (S_ISDIR(stb1
.st_mode
) != S_ISDIR(stb2
.st_mode
))
909 return (S_ISDIR(stb1
.st_mode
) ? D_MISMATCH1
: D_MISMATCH2
);
913 if (LONE_DASH(file1
) && LONE_DASH(file2
))
916 if (flags
& D_EMPTY1
)
917 f1
= xfopen(bb_dev_null
, "r");
918 else if (NOT_LONE_DASH(file1
))
919 f1
= xfopen(file1
, "r");
920 if (flags
& D_EMPTY2
)
921 f2
= xfopen(bb_dev_null
, "r");
922 else if (NOT_LONE_DASH(file2
))
923 f2
= xfopen(file2
, "r");
925 /* We can't diff non-seekable stream - we use rewind(), fseek().
926 * This can be fixed (volunteers?).
927 * Meanwhile we should check it here by stat'ing input fds,
928 * but I am lazy and check that in main() instead.
929 * Check in main won't catch "diffing fifos buried in subdirectories"
930 * failure scenario - not very likely in real life... */
932 i
= files_differ(f1
, f2
, flags
);
935 else if (i
!= 1) { /* 1 == ok */
941 if (!asciifile(f1
) || !asciifile(f2
)) {
947 prepare(0, f1
, stb1
.st_size
);
948 prepare(1, f2
, stb2
.st_size
);
950 sort(sfile
[0], slen
[0]);
951 sort(sfile
[1], slen
[1]);
953 member
= (int *) file
[1];
954 equiv(sfile
[0], slen
[0], sfile
[1], slen
[1], member
);
955 member
= xrealloc(member
, (slen
[1] + 2) * sizeof(int));
957 class = (int *) file
[0];
958 unsort(sfile
[0], slen
[0], class);
959 class = xrealloc(class, (slen
[0] + 2) * sizeof(int));
961 klist
= xmalloc((slen
[0] + 2) * sizeof(int));
964 clist
= xmalloc(clistlen
* sizeof(struct cand
));
965 i
= stone(class, slen
[0], member
, klist
);
969 J
= xrealloc(J
, (len
[0] + 2) * sizeof(int));
974 ixold
= xrealloc(ixold
, (len
[0] + 2) * sizeof(long));
975 ixnew
= xrealloc(ixnew
, (len
[1] + 2) * sizeof(long));
977 output(file1
, f1
, file2
, f2
);
985 fclose_if_not_stdin(f1
);
986 fclose_if_not_stdin(f2
);
995 #if ENABLE_FEATURE_DIFF_DIR
996 static void do_diff(char *dir1
, char *path1
, char *dir2
, char *path2
)
998 int flags
= D_HEADER
;
1000 char *fullpath1
= NULL
; /* if -N */
1001 char *fullpath2
= NULL
;
1004 fullpath1
= concat_path_file(dir1
, path1
);
1006 fullpath2
= concat_path_file(dir2
, path2
);
1008 if (!fullpath1
|| stat(fullpath1
, &stb1
) != 0) {
1010 memset(&stb1
, 0, sizeof(stb1
));
1013 fullpath1
= concat_path_file(dir1
, path2
);
1016 if (!fullpath2
|| stat(fullpath2
, &stb2
) != 0) {
1018 memset(&stb2
, 0, sizeof(stb2
));
1019 stb2
.st_mode
= stb1
.st_mode
;
1022 fullpath2
= concat_path_file(dir2
, path1
);
1026 if (stb1
.st_mode
== 0)
1027 stb1
.st_mode
= stb2
.st_mode
;
1029 if (S_ISDIR(stb1
.st_mode
) && S_ISDIR(stb2
.st_mode
)) {
1030 printf("Common subdirectories: %s and %s\n", fullpath1
, fullpath2
);
1034 if (!S_ISREG(stb1
.st_mode
) && !S_ISDIR(stb1
.st_mode
))
1036 else if (!S_ISREG(stb2
.st_mode
) && !S_ISDIR(stb2
.st_mode
))
1039 val
= diffreg(fullpath1
, fullpath2
, flags
);
1041 print_status(val
, fullpath1
, fullpath2
, NULL
);
1049 #if ENABLE_FEATURE_DIFF_DIR
1050 static int dir_strcmp(const void *p1
, const void *p2
)
1052 return strcmp(*(char *const *) p1
, *(char *const *) p2
);
1056 /* This function adds a filename to dl, the directory listing. */
1057 static int add_to_dirlist(const char *filename
,
1058 struct stat ATTRIBUTE_UNUSED
* sb
, void *userdata
,
1059 int depth ATTRIBUTE_UNUSED
)
1061 /* +2: with space for eventual trailing NULL */
1062 dl
= xrealloc(dl
, (dl_count
+2) * sizeof(dl
[0]));
1063 dl
[dl_count
] = xstrdup(filename
+ (int)(ptrdiff_t)userdata
);
1069 /* This returns a sorted directory listing. */
1070 static char **get_dir(char *path
)
1073 dl
= xzalloc(sizeof(dl
[0]));
1075 /* If -r has been set, then the recursive_action function will be
1076 * used. Unfortunately, this outputs the root directory along with
1077 * the recursed paths, so use void *userdata to specify the string
1078 * length of the root directory - '(void*)(strlen(path)+)'.
1079 * add_to_dirlist then removes root dir prefix. */
1081 if (option_mask32
& FLAG_r
) {
1082 recursive_action(path
, ACTION_RECURSE
|ACTION_FOLLOWLINKS
,
1083 add_to_dirlist
, NULL
,
1084 (void*)(strlen(path
)+1), 0);
1089 dp
= warn_opendir(path
);
1090 while ((ep
= readdir(dp
))) {
1091 if (!strcmp(ep
->d_name
, "..") || LONE_CHAR(ep
->d_name
, '.'))
1093 add_to_dirlist(ep
->d_name
, NULL
, (void*)(int)0, 0);
1098 /* Sort dl alphabetically. */
1099 qsort(dl
, dl_count
, sizeof(char *), dir_strcmp
);
1101 dl
[dl_count
] = NULL
;
1106 static void diffdir(char *p1
, char *p2
)
1108 char **dirlist1
, **dirlist2
;
1112 /* Check for trailing slashes. */
1113 dp1
= last_char_is(p1
, '/');
1116 dp2
= last_char_is(p2
, '/');
1120 /* Get directory listings for p1 and p2. */
1122 dirlist1
= get_dir(p1
);
1123 dirlist2
= get_dir(p2
);
1125 /* If -S was set, find the starting point. */
1127 while (*dirlist1
!= NULL
&& strcmp(*dirlist1
, start
) < 0)
1129 while (*dirlist2
!= NULL
&& strcmp(*dirlist2
, start
) < 0)
1131 if ((*dirlist1
== NULL
) || (*dirlist2
== NULL
))
1132 bb_error_msg(bb_msg_invalid_arg
, "NULL", "-S");
1135 /* Now that both dirlist1 and dirlist2 contain sorted directory
1136 * listings, we can start to go through dirlist1. If both listings
1137 * contain the same file, then do a normal diff. Otherwise, behaviour
1138 * is determined by whether the -N flag is set. */
1139 while (*dirlist1
!= NULL
|| *dirlist2
!= NULL
) {
1142 pos
= dp1
== NULL
? 1 : dp2
== NULL
? -1 : strcmp(dp1
, dp2
);
1144 do_diff(p1
, dp1
, p2
, dp2
);
1147 } else if (pos
< 0) {
1148 if (option_mask32
& FLAG_N
)
1149 do_diff(p1
, dp1
, p2
, NULL
);
1151 print_only(p1
, strlen(p1
) + 1, dp1
);
1154 if (option_mask32
& FLAG_N
)
1155 do_diff(p1
, NULL
, p2
, dp2
);
1157 print_only(p2
, strlen(p2
) + 1, dp2
);
1165 int diff_main(int argc
, char **argv
);
1166 int diff_main(int argc
, char **argv
)
1171 llist_t
*L_arg
= NULL
;
1173 /* exactly 2 params; collect multiple -L <label> */
1174 opt_complementary
= "=2:L::";
1175 getopt32(argc
, argv
, "abdiL:NqrsS:tTU:wu"
1176 "p" /* ignored (for compatibility) */,
1177 &L_arg
, &start
, &U_opt
);
1181 if (label1
&& label2
)
1184 label1
= L_arg
->data
;
1185 else { /* then label2 is NULL */
1187 label1
= L_arg
->data
;
1189 /* we leak L_arg here... */
1190 L_arg
= L_arg
->link
;
1192 if (option_mask32
& FLAG_U
)
1193 context
= xatoi_u(U_opt
);
1196 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1197 * driver routine. Both drivers use the contents of stb1 and stb2.
1202 if (LONE_DASH(f1
)) {
1203 fstat(STDIN_FILENO
, &stb1
);
1207 if (LONE_DASH(f2
)) {
1208 fstat(STDIN_FILENO
, &stb2
);
1212 if (gotstdin
&& (S_ISDIR(stb1
.st_mode
) || S_ISDIR(stb2
.st_mode
)))
1213 bb_error_msg_and_die("can't compare - to a directory");
1214 if (S_ISDIR(stb1
.st_mode
) && S_ISDIR(stb2
.st_mode
)) {
1215 #if ENABLE_FEATURE_DIFF_DIR
1218 bb_error_msg_and_die("directory comparison not supported");
1221 if (S_ISDIR(stb1
.st_mode
)) {
1222 f1
= concat_path_file(f1
, f2
);
1225 if (S_ISDIR(stb2
.st_mode
)) {
1226 f2
= concat_path_file(f2
, f1
);
1230 /* We can't diff e.g. stdin supplied by a pipe - we use rewind(), fseek().
1231 * This can be fixed (volunteers?) */
1232 if (!S_ISREG(stb1
.st_mode
) || !S_ISREG(stb2
.st_mode
))
1233 bb_error_msg_and_die("can't diff non-seekable stream");
1234 print_status(diffreg(f1
, f2
, 0), f1
, f2
, NULL
);