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
19 /* NOINLINEs added to prevent gcc from merging too much into diffreg()
20 * (it bites more than it can (efficiently) chew). */
26 /* Print a header/footer between files */
27 /* D_HEADER = 1, - unused */
28 /* Treat file as empty (/dev/null) */
29 D_EMPTY1
= 2 * ENABLE_FEATURE_DIFF_DIR
,
30 D_EMPTY2
= 4 * ENABLE_FEATURE_DIFF_DIR
,
34 * Status values for print_status() and diffreg() return values
36 * D_SAME - files are the same
37 * D_DIFFER - files differ
38 * D_BINARY - binary files differ
39 * D_COMMON - subdirectory common to both dirs
40 * D_ONLY - file only exists in one dir
41 * D_ISDIR1 - path1 a dir, path2 a file
42 * D_ISDIR2 - path1 a file, path2 a dir
43 * D_ERROR - error occurred
44 * D_SKIPPED1 - skipped path1 as it is a special file
45 * D_SKIPPED2 - skipped path2 as it is a special file
48 #define D_DIFFER (1 << 0)
49 #define D_BINARY (1 << 1)
50 #define D_COMMON (1 << 2)
51 /*#define D_ONLY (1 << 3) - unused */
52 #define D_ISDIR1 (1 << 4)
53 #define D_ISDIR2 (1 << 5)
54 #define D_ERROR (1 << 6)
55 #define D_SKIPPED1 (1 << 7)
56 #define D_SKIPPED2 (1 << 8)
58 /* Command line options */
59 #define FLAG_a (1 << 0)
60 #define FLAG_b (1 << 1)
61 #define FLAG_d (1 << 2)
62 #define FLAG_i (1 << 3)
63 #define FLAG_L (1 << 4)
64 #define FLAG_N (1 << 5)
65 #define FLAG_q (1 << 6)
66 #define FLAG_r (1 << 7)
67 #define FLAG_s (1 << 8)
68 #define FLAG_S (1 << 9)
69 #define FLAG_t (1 << 10)
70 #define FLAG_T (1 << 11)
71 #define FLAG_U (1 << 12)
72 #define FLAG_w (1 << 13)
87 * The following struct is used to record change information
88 * doing a "context" or "unified" diff. (see routine "change" to
89 * understand the highly mnemonic field names)
92 int a
; /* start line in old file */
93 int b
; /* end line in old file */
94 int c
; /* start line in new file */
95 int d
; /* end line in new file */
99 #define g_read_buf bb_common_bufsiz1
103 smallint exit_status
;
105 size_t max_context
; /* size of context_vec_start */
106 USE_FEATURE_DIFF_DIR(int dl_count
;)
107 USE_FEATURE_DIFF_DIR(char **dl
;)
111 int *J
; /* will be overlaid on class */
113 int pref
, suff
; /* length of prefix and suffix */
116 int clistlen
; /* the length of clist */
117 struct cand
*clist
; /* merely a free storage pot for candidates */
118 long *ixnew
; /* will be overlaid on nfile[1] */
119 long *ixold
; /* will be overlaid on klist */
120 struct line
*nfile
[2];
121 struct line
*sfile
[2]; /* shortened by pruning common prefix/suffix */
122 struct context_vec
*context_vec_start
;
123 struct context_vec
*context_vec_end
;
124 struct context_vec
*context_vec_ptr
;
125 char *tempname1
, *tempname2
;
126 struct stat stb1
, stb2
;
128 #define G (*ptr_to_globals)
129 #define anychange (G.anychange )
130 #define exit_status (G.exit_status )
131 #define opt_U_context (G.opt_U_context )
132 #define max_context (G.max_context )
133 #define dl_count (G.dl_count )
135 #define opt_S_start (G.opt_S_start )
136 #define label1 (G.label1 )
137 #define label2 (G.label2 )
139 #define clen (G.clen )
140 #define pref (G.pref )
141 #define suff (G.suff )
142 #define nlen (G.nlen )
143 #define slen (G.slen )
144 #define clistlen (G.clistlen )
145 #define clist (G.clist )
146 #define ixnew (G.ixnew )
147 #define ixold (G.ixold )
148 #define nfile (G.nfile )
149 #define sfile (G.sfile )
150 #define context_vec_start (G.context_vec_start )
151 #define context_vec_end (G.context_vec_end )
152 #define context_vec_ptr (G.context_vec_ptr )
153 #define stb1 (G.stb1 )
154 #define stb2 (G.stb2 )
155 #define tempname1 (G.tempname1 )
156 #define tempname2 (G.tempname2 )
157 #define INIT_G() do { \
158 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
164 #if ENABLE_FEATURE_DIFF_DIR
165 static void print_only(const char *path
, const char *entry
)
167 printf("Only in %s: %s\n", path
, entry
);
172 static void print_status(int val
, char *_path1
, char *_path2
)
174 /*const char *const _entry = entry ? entry : "";*/
175 /*char *const _path1 = entry ? concat_path_file(path1, _entry) : path1;*/
176 /*char *const _path2 = entry ? concat_path_file(path2, _entry) : path2;*/
180 print_only(path1, entry);
184 printf("Common subdirectories: %s and %s\n", _path1
, _path2
);
187 printf("Binary files %s and %s differ\n", _path1
, _path2
);
190 if (option_mask32
& FLAG_q
)
191 printf("Files %s and %s differ\n", _path1
, _path2
);
194 if (option_mask32
& FLAG_s
)
195 printf("Files %s and %s are identical\n", _path1
, _path2
);
198 printf("File %s is a %s while file %s is a %s\n",
199 _path1
, "directory", _path2
, "regular file");
202 printf("File %s is a %s while file %s is a %s\n",
203 _path1
, "regular file", _path2
, "directory");
206 printf("File %s is not a regular file or directory and was skipped\n",
210 printf("File %s is not a regular file or directory and was skipped\n",
223 /* Read line, return its nonzero hash. Return 0 if EOF.
225 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
227 static ALWAYS_INLINE
int fiddle_sum(int sum
, int t
)
229 return sum
* 127 + t
;
231 static int readhash(FILE *fp
)
239 if (!(option_mask32
& (FLAG_b
| FLAG_w
))) {
240 while ((t
= getc(fp
)) != '\n') {
246 sum
= fiddle_sum(sum
, t
);
251 switch (t
= getc(fp
)) {
260 if (space
&& !(option_mask32
& FLAG_w
)) {
264 sum
= fiddle_sum(sum
, t
);
278 * There is a remote possibility that we end up with a zero sum.
279 * Zero is used as an EOF marker, so return 1 instead.
281 return (sum
== 0 ? 1 : sum
);
285 /* Our diff implementation is using seek.
286 * When we meet non-seekable file, we must make a temp copy.
288 static char *make_temp(FILE *f
, struct stat
*sb
)
293 if (S_ISREG(sb
->st_mode
) || S_ISBLK(sb
->st_mode
))
295 name
= xstrdup("/tmp/difXXXXXX");
298 bb_perror_msg_and_die("mkstemp");
299 if (bb_copyfd_eof(fileno(f
), fd
) < 0) {
302 xfunc_die(); /* error message is printed by bb_copyfd_eof */
306 if (freopen(name
, "r+", f
) == NULL
) {
307 bb_perror_msg("freopen");
315 * Check to see if the given files differ.
316 * Returns 0 if they are the same, 1 if different, and -1 on error.
318 static NOINLINE
int files_differ(FILE *f1
, FILE *f2
)
322 /* Prevent making copies for "/dev/null" (too common) */
323 /* Deal with input from pipes etc */
324 tempname1
= make_temp(f1
, &stb1
);
325 tempname2
= make_temp(f2
, &stb2
);
326 if (stb1
.st_size
!= stb2
.st_size
) {
330 i
= fread(g_read_buf
, 1, COMMON_BUFSIZE
/2, f1
);
331 j
= fread(g_read_buf
+ COMMON_BUFSIZE
/2, 1, COMMON_BUFSIZE
/2, f2
);
335 return (ferror(f1
) || ferror(f2
)) ? -1 : 0;
336 if (memcmp(g_read_buf
,
337 g_read_buf
+ COMMON_BUFSIZE
/2, i
) != 0)
343 static void prepare(int i
, FILE *fp
/*, off_t filesize*/)
351 /*sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;*/
355 p
= xmalloc((sz
+ 3) * sizeof(p
[0]));
357 while ((h
= readhash(fp
)) != 0) { /* while not EOF */
360 p
= xrealloc(p
, (sz
+ 3) * sizeof(p
[0]));
369 static void prune(void)
373 for (pref
= 0; pref
< nlen
[0] && pref
< nlen
[1] &&
374 nfile
[0][pref
+ 1].value
== nfile
[1][pref
+ 1].value
; pref
++)
376 for (suff
= 0; suff
< nlen
[0] - pref
&& suff
< nlen
[1] - pref
&&
377 nfile
[0][nlen
[0] - suff
].value
== nfile
[1][nlen
[1] - suff
].value
;
380 for (j
= 0; j
< 2; j
++) {
381 sfile
[j
] = nfile
[j
] + pref
;
382 slen
[j
] = nlen
[j
] - pref
- suff
;
383 for (i
= 0; i
<= slen
[j
]; i
++)
384 sfile
[j
][i
].serial
= i
;
389 static void equiv(struct line
*a
, int n
, struct line
*b
, int m
, int *c
)
394 while (i
<= n
&& j
<= m
) {
395 if (a
[i
].value
< b
[j
].value
)
397 else if (a
[i
].value
== b
[j
].value
)
408 while (b
[j
+ 1].value
== b
[j
].value
) {
417 static int isqrt(int n
)
429 } while ((x
- y
) > 1 || (x
- y
) < -1);
435 static int newcand(int x
, int y
, int pred
)
439 if (clen
== clistlen
) {
440 clistlen
= clistlen
* 11 / 10;
441 clist
= xrealloc(clist
, clistlen
* sizeof(struct cand
));
451 static int search(int *c
, int k
, int y
)
455 if (clist
[c
[k
]].y
< y
) /* quick look for typical case */
475 static int stone(int *a
, int n
, int *b
, int *c
)
479 unsigned int numtries
;
480 #if ENABLE_FEATURE_DIFF_MINIMAL
481 const unsigned int bound
=
482 (option_mask32
& FLAG_d
) ? UINT_MAX
: MAX(256, isqrt(n
));
484 const unsigned int bound
= MAX(256, isqrt(n
));
488 c
[0] = newcand(0, 0, 0);
489 for (i
= 1; i
<= n
; i
++) {
498 if (y
<= clist
[oldc
].y
)
504 if (clist
[c
[l
]].y
<= y
)
507 c
[l
] = newcand(i
, y
, oldc
);
512 c
[l
] = newcand(i
, y
, oldc
);
516 } while ((y
= b
[++j
]) > 0 && numtries
< bound
);
522 static void unravel(int p
)
527 for (i
= 0; i
<= nlen
[0]; i
++)
528 J
[i
] = i
<= pref
? i
: i
> nlen
[0] - suff
? i
+ nlen
[1] - nlen
[0] : 0;
529 for (q
= clist
+ p
; q
->y
!= 0; q
= clist
+ q
->pred
)
530 J
[q
->x
+ pref
] = q
->y
+ pref
;
534 static void unsort(struct line
*f
, int l
, int *b
)
538 a
= xmalloc((l
+ 1) * sizeof(int));
539 for (i
= 1; i
<= l
; i
++)
540 a
[f
[i
].serial
] = f
[i
].value
;
541 for (i
= 1; i
<= l
; i
++)
547 static int skipline(FILE *f
)
551 for (i
= 1; (c
= getc(f
)) != '\n' && c
!= EOF
; i
++)
558 * Check does double duty:
559 * 1. ferret out any fortuitous correspondences due
560 * to confounding by hashing (which result in "jackpot")
561 * 2. collect random access indexes to the two files
563 static NOINLINE
void check(FILE *f1
, FILE *f2
)
565 int i
, j
, jackpot
, c
, d
;
571 ixold
[0] = ixnew
[0] = 0;
574 for (i
= 1; i
<= nlen
[0]; i
++) {
576 ixold
[i
] = ctold
+= skipline(f1
);
580 ixnew
[j
] = ctnew
+= skipline(f2
);
583 if (option_mask32
& (FLAG_b
| FLAG_w
| FLAG_i
)) {
588 * GNU diff ignores a missing newline
589 * in one file if bflag || wflag.
591 if ((option_mask32
& (FLAG_b
| FLAG_w
))
592 && ((c
== EOF
&& d
== '\n') || (c
== '\n' && d
== EOF
))
598 if ((option_mask32
& FLAG_b
) && isspace(c
) && isspace(d
)) {
604 } while (isspace(c
));
610 } while (isspace(d
));
611 } else if (option_mask32
& FLAG_w
) {
612 while (isspace(c
) && c
!= '\n') {
616 while (isspace(d
) && d
!= '\n') {
624 if (c
!= '\n' && c
!= EOF
)
625 ctold
+= skipline(f1
);
626 if (d
!= '\n' && c
!= EOF
)
627 ctnew
+= skipline(f2
);
630 if (c
== '\n' || c
== EOF
)
641 if (c
!= '\n' && c
!= EOF
)
642 ctold
+= skipline(f1
);
643 /* was buggy? "if (d != '\n' && c != EOF)" */
644 if (d
!= '\n' && d
!= EOF
)
645 ctnew
+= skipline(f2
);
648 if (c
== '\n' || c
== EOF
)
656 for (; j
<= nlen
[1]; j
++)
657 ixnew
[j
] = ctnew
+= skipline(f2
);
661 /* shellsort CACM #201 */
662 static void sort(struct line
*a
, int n
)
664 struct line
*ai
, *aim
, w
;
669 for (j
= 1; j
<= n
; j
*= 2)
671 for (m
/= 2; m
!= 0; m
/= 2) {
673 for (j
= 1; j
<= k
; j
++) {
674 for (ai
= &a
[j
]; ai
> a
; ai
-= m
) {
677 break; /* wraparound */
678 if (aim
->value
> ai
[0].value
679 || (aim
->value
== ai
[0].value
&& aim
->serial
> ai
[0].serial
)
683 w
.value
= ai
[0].value
;
684 ai
[0].value
= aim
->value
;
685 aim
->value
= w
.value
;
686 w
.serial
= ai
[0].serial
;
687 ai
[0].serial
= aim
->serial
;
688 aim
->serial
= w
.serial
;
695 static void uni_range(int a
, int b
)
698 printf("%d,%d", a
, b
- a
+ 1);
706 static void fetch(long *f
, int a
, int b
, FILE *lb
, int ch
)
708 int i
, j
, c
, lastc
, col
, nc
;
712 for (i
= a
; i
<= b
; i
++) {
713 fseek(lb
, f
[i
- 1], SEEK_SET
);
714 nc
= f
[i
] - f
[i
- 1];
717 if (option_mask32
& FLAG_T
)
721 for (j
= 0, lastc
= '\0'; j
< nc
; j
++, lastc
= c
) {
724 printf("\n\\ No newline at end of file\n");
727 if (c
== '\t' && (option_mask32
& FLAG_t
)) {
740 #if ENABLE_FEATURE_DIFF_BINARY
741 static int asciifile(FILE *f
)
745 if (option_mask32
& FLAG_a
)
748 cnt
= fread(g_read_buf
, 1, COMMON_BUFSIZE
, f
);
749 for (i
= 0; i
< cnt
; i
++) {
750 if (!isprint(g_read_buf
[i
])
751 && !isspace(g_read_buf
[i
])
759 #define asciifile(f) 1
763 /* dump accumulated "unified" diff changes */
764 static void dump_unified_vec(FILE *f1
, FILE *f2
)
766 struct context_vec
*cvp
= context_vec_start
;
767 int lowa
, upb
, lowc
, upd
;
771 if (context_vec_start
> context_vec_ptr
)
775 lowa
= MAX(1, cvp
->a
- opt_U_context
);
776 upb
= MIN(nlen
[0], context_vec_ptr
->b
+ opt_U_context
);
777 lowc
= MAX(1, cvp
->c
- opt_U_context
);
778 upd
= MIN(nlen
[1], context_vec_ptr
->d
+ opt_U_context
);
781 uni_range(lowa
, upb
);
783 uni_range(lowc
, upd
);
787 * Output changes in "unified" diff format--the old and new lines
788 * are printed together.
790 for (; cvp
<= context_vec_ptr
; cvp
++) {
797 * c: both new and old changes
798 * d: only changes in the old file
799 * a: only changes in the new file
801 if (a
<= b
&& c
<= d
)
804 ch
= (a
<= b
) ? 'd' : 'a';
809 fetch(ixold
, lowa
, a
- 1, f1
, ' ');
810 fetch(ixold
, a
, b
, f1
, '-');
811 fetch(ixnew
, c
, d
, f2
, '+');
814 fetch(ixold
, lowa
, a
- 1, f1
, ' ');
815 fetch(ixold
, a
, b
, f1
, '-');
818 fetch(ixnew
, lowc
, c
- 1, f2
, ' ');
819 fetch(ixnew
, c
, d
, f2
, '+');
823 if (ch
== 'c' || ch
== 'd') {
824 fetch(ixold
, lowa
, a
- 1, f1
, ' ');
825 fetch(ixold
, a
, b
, f1
, '-');
828 fetch(ixnew
, lowc
, c
- 1, f2
, ' ');
829 if (ch
== 'c' || ch
== 'a')
830 fetch(ixnew
, c
, d
, f2
, '+');
835 fetch(ixnew
, d
+ 1, upd
, f2
, ' ');
837 context_vec_ptr
= context_vec_start
- 1;
841 static void print_header(const char *file1
, const char *file2
)
844 printf("--- %s\n", label1
);
846 printf("--- %s\t%s", file1
, ctime(&stb1
.st_mtime
));
848 printf("+++ %s\n", label2
);
850 printf("+++ %s\t%s", file2
, ctime(&stb2
.st_mtime
));
855 * Indicate that there is a difference between lines a and b of the from file
856 * to get to lines c to d of the to file. If a is greater than b then there
857 * are no lines in the from file involved and this means that there were
858 * lines appended (beginning at b). If c is greater than d then there are
859 * lines missing from the to file.
861 static void change(const char *file1
, FILE *f1
, const char *file2
, FILE *f2
,
862 int a
, int b
, int c
, int d
)
864 if ((a
> b
&& c
> d
) || (option_mask32
& FLAG_q
)) {
870 * Allocate change records as needed.
872 if (context_vec_ptr
== context_vec_end
- 1) {
873 ptrdiff_t offset
= context_vec_ptr
- context_vec_start
;
876 context_vec_start
= xrealloc(context_vec_start
,
877 max_context
* sizeof(struct context_vec
));
878 context_vec_end
= context_vec_start
+ max_context
;
879 context_vec_ptr
= context_vec_start
+ offset
;
881 if (anychange
== 0) {
883 * Print the context/unidiff header first time through.
885 print_header(file1
, file2
);
886 } else if (a
> context_vec_ptr
->b
+ (2 * opt_U_context
) + 1
887 && c
> context_vec_ptr
->d
+ (2 * opt_U_context
) + 1
890 * If this change is more than 'context' lines from the
891 * previous change, dump the record and reset it.
893 // dump_unified_vec() seeks!
894 dump_unified_vec(f1
, f2
);
897 context_vec_ptr
->a
= a
;
898 context_vec_ptr
->b
= b
;
899 context_vec_ptr
->c
= c
;
900 context_vec_ptr
->d
= d
;
905 static void output(const char *file1
, FILE *f1
, const char *file2
, FILE *f2
)
907 /* Note that j0 and j1 can't be used as they are defined in math.h.
908 * This also allows the rather amusing variable 'j00'... */
909 int m
, i0
, i1
, j00
, j01
;
915 J
[m
+ 1] = nlen
[1] + 1;
916 for (i0
= 1; i0
<= m
; i0
= i1
+ 1) {
917 while (i0
<= m
&& J
[i0
] == J
[i0
- 1] + 1)
921 while (i1
< m
&& J
[i1
+ 1] == 0)
926 change(file1
, f1
, file2
, f2
, i0
, i1
, j00
, j01
);
930 change(file1
, f1
, file2
, f2
, 1, 0, 1, nlen
[1]);
932 if (anychange
!= 0 && !(option_mask32
& FLAG_q
)) {
933 // dump_unified_vec() seeks!
934 dump_unified_vec(f1
, f2
);
939 * The following code uses an algorithm due to Harold Stone,
940 * which finds a pair of longest identical subsequences in
943 * The major goal is to generate the match vector J.
944 * J[i] is the index of the line in file1 corresponding
945 * to line i in file0. J[i] = 0 if there is no
946 * such line in file1.
948 * Lines are hashed so as to work in core. All potential
949 * matches are located by sorting the lines of each file
950 * on the hash (called "value"). In particular, this
951 * collects the equivalence classes in file1 together.
952 * Subroutine equiv replaces the value of each line in
953 * file0 by the index of the first element of its
954 * matching equivalence in (the reordered) file1.
955 * To save space equiv squeezes file1 into a single
956 * array member in which the equivalence classes
957 * are simply concatenated, except that their first
958 * members are flagged by changing sign.
960 * Next the indices that point into member are unsorted into
961 * array class according to the original order of file0.
963 * The cleverness lies in routine stone. This marches
964 * through the lines of file0, developing a vector klist
965 * of "k-candidates". At step i a k-candidate is a matched
966 * pair of lines x,y (x in file0, y in file1) such that
967 * there is a common subsequence of length k
968 * between the first i lines of file0 and the first y
969 * lines of file1, but there is no such subsequence for
970 * any smaller y. x is the earliest possible mate to y
971 * that occurs in such a subsequence.
973 * Whenever any of the members of the equivalence class of
974 * lines in file1 matable to a line in file0 has serial number
975 * less than the y of some k-candidate, that k-candidate
976 * with the smallest such y is replaced. The new
977 * k-candidate is chained (via pred) to the current
978 * k-1 candidate so that the actual subsequence can
979 * be recovered. When a member has serial number greater
980 * that the y of all k-candidates, the klist is extended.
981 * At the end, the longest subsequence is pulled out
982 * and placed in the array J by unravel
984 * With J in hand, the matches there recorded are
985 * checked against reality to assure that no spurious
986 * matches have crept in due to hashing. If they have,
987 * they are broken, and "jackpot" is recorded--a harmless
988 * matter except that a true match for a spuriously
989 * mated line may now be unnecessarily reported as a change.
991 * Much of the complexity of the program comes simply
992 * from trying to minimize core utilization and
993 * maximize the range of doable problems by dynamically
994 * allocating what is needed and reusing what is not.
995 * The core requirements for problems larger than somewhat
996 * are (in words) 2*length(file0) + length(file1) +
997 * 3*(number of k-candidates installed), typically about
998 * 6n words for files of length n.
1000 /* NB: files can be not REGular. The only sure thing that they
1001 * are not both DIRectories. */
1002 static unsigned diffreg(const char *file1
, const char *file2
, int flags
)
1004 int *member
; /* will be overlaid on nfile[1] */
1005 int *class; /* will be overlaid on nfile[0] */
1006 int *klist
; /* will be overlaid on nfile[0] after class */
1013 context_vec_ptr
= context_vec_start
- 1;
1014 tempname1
= tempname2
= NULL
;
1016 /* Is any of them a directory? Then it's simple */
1017 if (S_ISDIR(stb1
.st_mode
) != S_ISDIR(stb2
.st_mode
))
1018 return (S_ISDIR(stb1
.st_mode
) ? D_ISDIR1
: D_ISDIR2
);
1020 /* None of them are directories */
1023 if (flags
& D_EMPTY1
)
1024 /* can't be stdin, but xfopen_stdin() is smaller code */
1025 file1
= bb_dev_null
;
1026 f1
= xfopen_stdin(file1
);
1027 if (flags
& D_EMPTY2
)
1028 file2
= bb_dev_null
;
1029 f2
= xfopen_stdin(file2
);
1031 /* NB: if D_EMPTY1/2 is set, other file is always a regular file,
1032 * not pipe/fifo/chardev/etc - D_EMPTY is used by "diff -r" only,
1033 * and it never diffs non-ordinary files in subdirs. */
1034 if (!(flags
& (D_EMPTY1
| D_EMPTY2
))) {
1035 /* Quick check whether they are different */
1036 /* NB: copies non-REG files to tempfiles and fills tempname1/2 */
1037 i
= files_differ(f1
, f2
);
1038 if (i
!= 1) { /* not different? */
1039 if (i
!= 0) /* error? */
1045 if (!asciifile(f1
) || !asciifile(f2
)) {
1052 prepare(0, f1
/*, stb1.st_size*/);
1053 prepare(1, f2
/*, stb2.st_size*/);
1055 sort(sfile
[0], slen
[0]);
1056 sort(sfile
[1], slen
[1]);
1058 member
= (int *) nfile
[1];
1059 equiv(sfile
[0], slen
[0], sfile
[1], slen
[1], member
);
1060 //TODO: xrealloc_vector?
1061 member
= xrealloc(member
, (slen
[1] + 2) * sizeof(int));
1063 class = (int *) nfile
[0];
1064 unsort(sfile
[0], slen
[0], class);
1065 class = xrealloc(class, (slen
[0] + 2) * sizeof(int));
1067 klist
= xmalloc((slen
[0] + 2) * sizeof(int));
1070 clist
= xmalloc(clistlen
* sizeof(struct cand
));
1071 i
= stone(class, slen
[0], member
, klist
);
1075 J
= xrealloc(J
, (nlen
[0] + 2) * sizeof(int));
1080 ixold
= xrealloc(ixold
, (nlen
[0] + 2) * sizeof(long));
1081 ixnew
= xrealloc(ixnew
, (nlen
[1] + 2) * sizeof(long));
1085 output(file1
, f1
, file2
, f2
);
1093 fclose_if_not_stdin(f1
);
1094 fclose_if_not_stdin(f2
);
1107 #if ENABLE_FEATURE_DIFF_DIR
1108 static void do_diff(char *dir1
, char *path1
, char *dir2
, char *path2
)
1110 int flags
= 0; /*D_HEADER;*/
1112 char *fullpath1
= NULL
; /* if -N */
1113 char *fullpath2
= NULL
;
1116 fullpath1
= concat_path_file(dir1
, path1
);
1118 fullpath2
= concat_path_file(dir2
, path2
);
1120 if (!fullpath1
|| stat(fullpath1
, &stb1
) != 0) {
1122 memset(&stb1
, 0, sizeof(stb1
));
1125 fullpath1
= concat_path_file(dir1
, path2
);
1128 if (!fullpath2
|| stat(fullpath2
, &stb2
) != 0) {
1130 memset(&stb2
, 0, sizeof(stb2
));
1131 stb2
.st_mode
= stb1
.st_mode
;
1134 fullpath2
= concat_path_file(dir2
, path1
);
1138 if (stb1
.st_mode
== 0)
1139 stb1
.st_mode
= stb2
.st_mode
;
1141 if (S_ISDIR(stb1
.st_mode
) && S_ISDIR(stb2
.st_mode
)) {
1142 printf("Common subdirectories: %s and %s\n", fullpath1
, fullpath2
);
1146 if (!S_ISREG(stb1
.st_mode
) && !S_ISDIR(stb1
.st_mode
))
1148 else if (!S_ISREG(stb2
.st_mode
) && !S_ISDIR(stb2
.st_mode
))
1151 /* Both files are either REGular or DIRectories */
1152 val
= diffreg(fullpath1
, fullpath2
, flags
);
1155 print_status(val
, fullpath1
, fullpath2
/*, NULL*/);
1163 #if ENABLE_FEATURE_DIFF_DIR
1164 /* This function adds a filename to dl, the directory listing. */
1165 static int FAST_FUNC
add_to_dirlist(const char *filename
,
1166 struct stat
*sb UNUSED_PARAM
,
1168 int depth UNUSED_PARAM
)
1170 dl
= xrealloc_vector(dl
, 5, dl_count
);
1171 dl
[dl_count
] = xstrdup(filename
+ (int)(ptrdiff_t)userdata
);
1177 /* This returns a sorted directory listing. */
1178 static char **get_recursive_dirlist(char *path
)
1181 dl
= xzalloc(sizeof(dl
[0]));
1183 /* We need to trim root directory prefix.
1184 * Using void *userdata to specify its length,
1185 * add_to_dirlist will remove it. */
1186 if (option_mask32
& FLAG_r
) {
1187 recursive_action(path
, ACTION_RECURSE
|ACTION_FOLLOWLINKS
,
1188 add_to_dirlist
, /* file_action */
1189 NULL
, /* dir_action */
1190 (void*)(ptrdiff_t)(strlen(path
) + 1),
1196 dp
= warn_opendir(path
);
1197 while ((ep
= readdir(dp
))) {
1198 if (!strcmp(ep
->d_name
, "..") || LONE_CHAR(ep
->d_name
, '.'))
1200 add_to_dirlist(ep
->d_name
, NULL
, (void*)(int)0, 0);
1205 /* Sort dl alphabetically. */
1206 qsort_string_vector(dl
, dl_count
);
1208 dl
[dl_count
] = NULL
;
1213 static void diffdir(char *p1
, char *p2
)
1215 char **dirlist1
, **dirlist2
;
1219 /* Check for trailing slashes. */
1220 dp1
= last_char_is(p1
, '/');
1223 dp2
= last_char_is(p2
, '/');
1227 /* Get directory listings for p1 and p2. */
1228 dirlist1
= get_recursive_dirlist(p1
);
1229 dirlist2
= get_recursive_dirlist(p2
);
1231 /* If -S was set, find the starting point. */
1233 while (*dirlist1
!= NULL
&& strcmp(*dirlist1
, opt_S_start
) < 0)
1235 while (*dirlist2
!= NULL
&& strcmp(*dirlist2
, opt_S_start
) < 0)
1237 if ((*dirlist1
== NULL
) || (*dirlist2
== NULL
))
1238 bb_error_msg(bb_msg_invalid_arg
, "NULL", "-S");
1241 /* Now that both dirlist1 and dirlist2 contain sorted directory
1242 * listings, we can start to go through dirlist1. If both listings
1243 * contain the same file, then do a normal diff. Otherwise, behaviour
1244 * is determined by whether the -N flag is set. */
1245 while (*dirlist1
!= NULL
|| *dirlist2
!= NULL
) {
1248 pos
= dp1
== NULL
? 1 : (dp2
== NULL
? -1 : strcmp(dp1
, dp2
));
1250 do_diff(p1
, dp1
, p2
, dp2
);
1253 } else if (pos
< 0) {
1254 if (option_mask32
& FLAG_N
)
1255 do_diff(p1
, dp1
, p2
, NULL
);
1257 print_only(p1
, dp1
);
1260 if (option_mask32
& FLAG_N
)
1261 do_diff(p1
, NULL
, p2
, dp2
);
1263 print_only(p2
, dp2
);
1271 int diff_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
1272 int diff_main(int argc UNUSED_PARAM
, char **argv
)
1276 llist_t
*L_arg
= NULL
;
1280 /* exactly 2 params; collect multiple -L <label>; -U N */
1281 opt_complementary
= "=2:L::U+";
1282 getopt32(argv
, "abdiL:NqrsS:tTU:wu"
1283 "p" /* ignored (for compatibility) */,
1284 &L_arg
, &opt_S_start
, &opt_U_context
);
1288 if (label1
&& label2
)
1290 if (label1
) /* then label2 is NULL */
1292 label1
= llist_pop(&L_arg
);
1296 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1297 * driver routine. Both drivers use the contents of stb1 and stb2.
1301 if (LONE_DASH(f1
)) {
1302 fstat(STDIN_FILENO
, &stb1
);
1306 if (LONE_DASH(f2
)) {
1307 fstat(STDIN_FILENO
, &stb2
);
1312 if (gotstdin
&& (S_ISDIR(stb1
.st_mode
) || S_ISDIR(stb2
.st_mode
)))
1313 bb_error_msg_and_die("can't compare stdin to a directory");
1315 if (S_ISDIR(stb1
.st_mode
) && S_ISDIR(stb2
.st_mode
)) {
1316 #if ENABLE_FEATURE_DIFF_DIR
1320 bb_error_msg_and_die("no support for directory comparison");
1324 if (S_ISDIR(stb1
.st_mode
)) { /* "diff dir file" */
1325 /* NB: "diff dir dir2/dir3/file" must become
1326 * "diff dir/file dir2/dir3/file" */
1327 char *slash
= strrchr(f2
, '/');
1328 f1
= concat_path_file(f1
, slash
? slash
+ 1 : f2
);
1331 if (S_ISDIR(stb2
.st_mode
)) {
1332 char *slash
= strrchr(f1
, '/');
1333 f2
= concat_path_file(f2
, slash
? slash
+ 1 : f1
);
1337 /* diffreg can get non-regular files here,
1338 * they are not both DIRestories */
1339 print_status((gotstdin
> 1 ? D_SAME
: diffreg(f1
, f2
, 0)),