aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatheus Izvekov <mizvekov@gmail.com>2010-01-18 04:57:17 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-01-18 04:57:17 +0100
commitd4a7728dc3b37e2956034f18fc26c04bc0aa2b0e (patch)
treeb08fde45be76ab4afb1c9af5df1177007c5b111e
parentf3fc9ac166d2413d331e3189a132a693608695ba (diff)
downloadbusybox-w32-d4a7728dc3b37e2956034f18fc26c04bc0aa2b0e.tar.gz
busybox-w32-d4a7728dc3b37e2956034f18fc26c04bc0aa2b0e.tar.bz2
busybox-w32-d4a7728dc3b37e2956034f18fc26c04bc0aa2b0e.zip
diff: rewrite. much smaller and more correct
function old new delta diffreg 1815 3156 +1341 diff_main 860 1194 +334 read_token - 161 +161 skip_dir - 44 +44 seek_ft - 31 +31 fetch 362 392 +30 line_compar - 21 +21 add_to_dirlist 65 61 -4 print_only 16 - -16 skipline 51 - -51 newcand 85 - -85 asciifile 89 - -89 print_status 178 73 -105 make_temp 148 - -148 files_differ 168 - -168 get_recursive_dirlist 182 - -182 sort 226 - -226 prepare 283 - -283 change 283 - -283 do_diff 416 - -416 dump_unified_vec 457 - -457 check 1512 595 -917 ------------------------------------------------------------------------------ (add/remove: 4/12 grow/shrink: 3/3 up/down: 1962/-3430) Total: -1468 bytes Signed-off-by: Matheus Izvekov <mizvekov@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/Config.in16
-rw-r--r--editors/diff.c1840
-rwxr-xr-xtestsuite/diff.tests3
3 files changed, 709 insertions, 1150 deletions
diff --git a/editors/Config.in b/editors/Config.in
index 7dbc9b6da..e1285f4ea 100644
--- a/editors/Config.in
+++ b/editors/Config.in
@@ -35,14 +35,6 @@ config DIFF
35 differences between them in a form that can be given to 35 differences between them in a form that can be given to
36 the patch command. 36 the patch command.
37 37
38config FEATURE_DIFF_BINARY
39 bool "Enable checks for binary files"
40 default y
41 depends on DIFF
42 help
43 This option enables support for checking for binary files
44 before a comparison is carried out.
45
46config FEATURE_DIFF_DIR 38config FEATURE_DIFF_DIR
47 bool "Enable directory support" 39 bool "Enable directory support"
48 default y 40 default y
@@ -51,14 +43,6 @@ config FEATURE_DIFF_DIR
51 This option enables support for directory and subdirectory 43 This option enables support for directory and subdirectory
52 comparison. 44 comparison.
53 45
54config FEATURE_DIFF_MINIMAL
55 bool "Enable -d option to find smaller sets of changes"
56 default n
57 depends on DIFF
58 help
59 Enabling this option allows the use of -d to make diff
60 try hard to find the smallest possible set of changes.
61
62config ED 46config ED
63 bool "ed" 47 bool "ed"
64 default n 48 default n
diff --git a/editors/diff.c b/editors/diff.c
index 745ef0a33..af6917a03 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -2,6 +2,7 @@
2/* 2/*
3 * Mini diff implementation for busybox, adapted from OpenBSD diff. 3 * Mini diff implementation for busybox, adapted from OpenBSD diff.
4 * 4 *
5 * Copyright (C) 2010 by Matheus Izvekov <mizvekov@gmail.com>
5 * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com> 6 * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com>
6 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> 7 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
7 * 8 *
@@ -12,386 +13,310 @@
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 */ 14 */
14 15
15#include "libbb.h"
16
17#define dbg_error_msg(...) ((void)0)
18//#define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
19
20// #define FSIZE_MAX 32768
21
22/* NOINLINEs added to prevent gcc from merging too much into diffreg()
23 * (it bites more than it can (efficiently) chew). */
24
25/*
26 * Output flags
27 */
28enum {
29 /* Print a header/footer between files */
30 /* D_HEADER = 1, - unused */
31 /* Treat file as empty (/dev/null) */
32 D_EMPTY1 = 2 * ENABLE_FEATURE_DIFF_DIR,
33 D_EMPTY2 = 4 * ENABLE_FEATURE_DIFF_DIR,
34};
35
36/* 16/*
37 * Status values for print_status() and diffreg() return values 17 * The following code uses an algorithm due to Harold Stone,
38 * Guide: 18 * which finds a pair of longest identical subsequences in
39 * D_SAME - files are the same 19 * the two files.
40 * D_DIFFER - files differ 20 *
41 * D_BINARY - binary files differ 21 * The major goal is to generate the match vector J.
42 * D_COMMON - subdirectory common to both dirs 22 * J[i] is the index of the line in file1 corresponding
43 * D_ONLY - file only exists in one dir 23 * to line i in file0. J[i] = 0 if there is no
44 * D_ISDIR1 - path1 a dir, path2 a file 24 * such line in file1.
45 * D_ISDIR2 - path1 a file, path2 a dir 25 *
46 * D_ERROR - error occurred 26 * Lines are hashed so as to work in core. All potential
47 * D_SKIPPED1 - skipped path1 as it is a special file 27 * matches are located by sorting the lines of each file
48 * D_SKIPPED2 - skipped path2 as it is a special file 28 * on the hash (called "value"). In particular, this
29 * collects the equivalence classes in file1 together.
30 * Subroutine equiv replaces the value of each line in
31 * file0 by the index of the first element of its
32 * matching equivalence in (the reordered) file1.
33 * To save space equiv squeezes file1 into a single
34 * array member in which the equivalence classes
35 * are simply concatenated, except that their first
36 * members are flagged by changing sign.
37 *
38 * Next the indices that point into member are unsorted into
39 * array class according to the original order of file0.
40 *
41 * The cleverness lies in routine stone. This marches
42 * through the lines of file0, developing a vector klist
43 * of "k-candidates". At step i a k-candidate is a matched
44 * pair of lines x,y (x in file0, y in file1) such that
45 * there is a common subsequence of length k
46 * between the first i lines of file0 and the first y
47 * lines of file1, but there is no such subsequence for
48 * any smaller y. x is the earliest possible mate to y
49 * that occurs in such a subsequence.
50 *
51 * Whenever any of the members of the equivalence class of
52 * lines in file1 matable to a line in file0 has serial number
53 * less than the y of some k-candidate, that k-candidate
54 * with the smallest such y is replaced. The new
55 * k-candidate is chained (via pred) to the current
56 * k-1 candidate so that the actual subsequence can
57 * be recovered. When a member has serial number greater
58 * that the y of all k-candidates, the klist is extended.
59 * At the end, the longest subsequence is pulled out
60 * and placed in the array J by unravel
61 *
62 * With J in hand, the matches there recorded are
63 * checked against reality to assure that no spurious
64 * matches have crept in due to hashing. If they have,
65 * they are broken, and "jackpot" is recorded--a harmless
66 * matter except that a true match for a spuriously
67 * mated line may now be unnecessarily reported as a change.
68 *
69 * Much of the complexity of the program comes simply
70 * from trying to minimize core utilization and
71 * maximize the range of doable problems by dynamically
72 * allocating what is needed and reusing what is not.
73 * The core requirements for problems larger than somewhat
74 * are (in words) 2*length(file0) + length(file1) +
75 * 3*(number of k-candidates installed), typically about
76 * 6n words for files of length n.
49 */ 77 */
50#define D_SAME 0
51#define D_DIFFER (1 << 0)
52#define D_BINARY (1 << 1)
53#define D_COMMON (1 << 2)
54/*#define D_ONLY (1 << 3) - unused */
55#define D_ISDIR1 (1 << 4)
56#define D_ISDIR2 (1 << 5)
57#define D_ERROR (1 << 6)
58#define D_SKIPPED1 (1 << 7)
59#define D_SKIPPED2 (1 << 8)
60
61/* Command line options */
62#define FLAG_a (1 << 0)
63#define FLAG_b (1 << 1)
64#define FLAG_d (1 << 2)
65#define FLAG_i (1 << 3)
66#define FLAG_L (1 << 4)
67#define FLAG_N (1 << 5)
68#define FLAG_q (1 << 6)
69#define FLAG_r (1 << 7)
70#define FLAG_s (1 << 8)
71#define FLAG_S (1 << 9)
72#define FLAG_t (1 << 10)
73#define FLAG_T (1 << 11)
74#define FLAG_U (1 << 12)
75#define FLAG_w (1 << 13)
76 78
79#include "libbb.h"
77 80
78struct cand { 81#if 0
79 int x; 82//#define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
80 int y; 83#else
81 int pred; 84#define dbg_error_msg(...) ((void)0)
82}; 85#endif
83 86
84struct line { 87enum { /* print_status() and diffreg() return values */
85 int serial; 88 STATUS_SAME, /* files are the same */
86 int value; 89 STATUS_DIFFER, /* files differ */
90 STATUS_BINARY, /* binary files differ */
87}; 91};
88 92
89/* 93enum { /* Commandline flags */
90 * The following struct is used to record change information 94 FLAG_a,
91 * doing a "context" or "unified" diff. (see routine "change" to 95 FLAG_b,
92 * understand the highly mnemonic field names) 96 FLAG_d,
93 */ 97 FLAG_i, /* unused */
94struct context_vec { 98 FLAG_L, /* unused */
95 int a; /* start line in old file */ 99 FLAG_N,
96 int b; /* end line in old file */ 100 FLAG_q,
97 int c; /* start line in new file */ 101 FLAG_r,
98 int d; /* end line in new file */ 102 FLAG_s,
103 FLAG_S, /* unused */
104 FLAG_t,
105 FLAG_T,
106 FLAG_U, /* unused */
107 FLAG_w,
99}; 108};
109#define FLAG(x) (1 << FLAG_##x)
100 110
101 111/* We cache file position to avoid excessive seeking */
102#define g_read_buf bb_common_bufsiz1 112typedef struct FILE_and_pos_t {
113 FILE *ft_fp;
114 off_t ft_pos;
115} FILE_and_pos_t;
103 116
104struct globals { 117struct globals {
105 bool anychange;
106 smallint exit_status; 118 smallint exit_status;
107 int opt_U_context; 119 int opt_U_context;
108 int context_idx; 120 char *label[2];
109 IF_FEATURE_DIFF_DIR(int dl_count;) 121 struct stat stb[2];
110 IF_FEATURE_DIFF_DIR(char **dl;)
111 char *opt_S_start;
112 const char *label1;
113 const char *label2;
114 int *J; /* will be overlaid on class */
115 int clen;
116 int pref, suff; /* length of prefix and suffix */
117 int nlen[2];
118 int slen[2];
119 int clistlen; /* the length of clist */
120 struct cand *clist; /* merely a free storage pot for candidates */
121 long *ixnew; /* will be overlaid on nfile[1] */
122 long *ixold; /* will be overlaid on klist */
123 struct line *nfile[2];
124 struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
125 struct context_vec *context_vector;
126 char *tempname1, *tempname2;
127 struct stat stb1, stb2;
128}; 122};
129#define G (*ptr_to_globals) 123#define G (*ptr_to_globals)
130#define anychange (G.anychange )
131#define exit_status (G.exit_status ) 124#define exit_status (G.exit_status )
132#define opt_U_context (G.opt_U_context ) 125#define opt_U_context (G.opt_U_context )
133#define context_idx (G.context_idx ) 126#define label (G.label )
134#define dl_count (G.dl_count ) 127#define stb (G.stb )
135#define dl (G.dl )
136#define opt_S_start (G.opt_S_start )
137#define label1 (G.label1 )
138#define label2 (G.label2 )
139#define J (G.J )
140#define clen (G.clen )
141#define pref (G.pref )
142#define suff (G.suff )
143#define nlen (G.nlen )
144#define slen (G.slen )
145#define clistlen (G.clistlen )
146#define clist (G.clist )
147#define ixnew (G.ixnew )
148#define ixold (G.ixold )
149#define nfile (G.nfile )
150#define sfile (G.sfile )
151#define context_vector (G.context_vector )
152#define stb1 (G.stb1 )
153#define stb2 (G.stb2 )
154#define tempname1 (G.tempname1 )
155#define tempname2 (G.tempname2 )
156#define INIT_G() do { \ 128#define INIT_G() do { \
157 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ 129 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
158 opt_U_context = 3; \ 130 opt_U_context = 3; \
159 context_vector = xrealloc_vector(context_vector, 6, 0); \
160} while (0) 131} while (0)
161 132
133typedef int token_t;
162 134
163#if ENABLE_FEATURE_DIFF_DIR 135enum {
164static void print_only(const char *path, const char *entry) 136 /* Public */
165{ 137 TOK_EMPTY = 1 << 9, /* Line fully processed, you can proceed to the next */
166 printf("Only in %s: %s\n", path, entry); 138 TOK_EOF = 1 << 10, /* File ended */
167} 139 /* Private (Only to be used by read_token() */
168#endif 140 TOK_EOL = 1 << 11, /* we saw EOL (sticky) */
141 TOK_SPACE = 1 << 12, /* used -b code, means we are skipping spaces */
142 SHIFT_EOF = (sizeof(token_t)*8 - 8) - 1,
143 CHAR_MASK = 0x1ff, /* 8th bit is used to distinguish EOF from 0xff */
144};
169 145
146/* Restores full EOF from one 8th bit: */
147//#define TOK2CHAR(t) (((t) << SHIFT_EOF) >> SHIFT_EOF)
148/* We don't really need the above, we only need to have EOF != any_real_char: */
149#define TOK2CHAR(t) ((t) & CHAR_MASK)
170 150
171static void print_status(int val, char *_path1, char *_path2) 151static void seek_ft(FILE_and_pos_t *ft, off_t pos)
172{ 152{
173 /*const char *const _entry = entry ? entry : "";*/ 153 if (ft->ft_pos != pos) {
174 /*char *const _path1 = entry ? concat_path_file(path1, _entry) : path1;*/ 154 ft->ft_pos = pos;
175 /*char *const _path2 = entry ? concat_path_file(path2, _entry) : path2;*/ 155 fseeko(ft->ft_fp, pos, SEEK_SET);
176
177 switch (val) {
178/* case D_ONLY:
179 print_only(path1, entry);
180 break;
181*/
182 case D_COMMON:
183 printf("Common subdirectories: %s and %s\n", _path1, _path2);
184 break;
185 case D_BINARY:
186 printf("Binary files %s and %s differ\n", _path1, _path2);
187 break;
188 case D_DIFFER:
189 if (option_mask32 & FLAG_q)
190 printf("Files %s and %s differ\n", _path1, _path2);
191 break;
192 case D_SAME:
193 if (option_mask32 & FLAG_s)
194 printf("Files %s and %s are identical\n", _path1, _path2);
195 break;
196 case D_ISDIR1:
197 printf("File %s is a %s while file %s is a %s\n",
198 _path1, "directory", _path2, "regular file");
199 break;
200 case D_ISDIR2:
201 printf("File %s is a %s while file %s is a %s\n",
202 _path1, "regular file", _path2, "directory");
203 break;
204 case D_SKIPPED1:
205 printf("File %s is not a regular file or directory and was skipped\n",
206 _path1);
207 break;
208 case D_SKIPPED2:
209 printf("File %s is not a regular file or directory and was skipped\n",
210 _path2);
211 break;
212 }
213/*
214 if (entry) {
215 free(_path1);
216 free(_path2);
217 } 156 }
218*/
219} 157}
220 158
221 159/* Reads tokens from given fp, handling -b and -w flags
222/* Read line, return its nonzero hash. Return 0 if EOF. 160 * The user must reset tok every line start
223 *
224 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
225 */ 161 */
226static ALWAYS_INLINE int fiddle_sum(int sum, int t) 162static int read_token(FILE_and_pos_t *ft, token_t tok)
227{ 163{
228 return sum * 127 + t; 164 tok |= TOK_EMPTY;
229} 165 while (!(tok & TOK_EOL)) {
230static int readhash(FILE *fp) 166 bool is_space;
231{ 167 int t;
232 int i, t; 168
233 int sum; 169 t = fgetc(ft->ft_fp);
234 170 if (t != EOF)
235 sum = 1; 171 ft->ft_pos++;
236 i = 0; 172 is_space = (t == EOF || isspace(t));
237 if (!(option_mask32 & (FLAG_b | FLAG_w))) { 173
238 while ((t = getc(fp)) != '\n') { 174 /* If t == EOF (-1), set both TOK_EOF and TOK_EOL */
239 if (t == EOF) { 175 tok |= (t & (TOK_EOF + TOK_EOL));
240 if (i == 0) 176 /* Only EOL? */
241 return 0; 177 if (t == '\n')
242 break; 178 tok |= TOK_EOL;
243 } 179
244 sum = fiddle_sum(sum, t); 180 if ((option_mask32 & FLAG(w)) && is_space)
245 i = 1; 181 continue;
246 }
247 } else {
248 int space = 0;
249 182
250 while (1) { 183 /* Trim char value to low 9 bits */
251 t = getc(fp); 184 t &= CHAR_MASK;
252 switch (t) { 185
253 case '\t': 186 if (option_mask32 & FLAG(b)) {
254 case '\r': 187 /* Was prev char whitespace? */
255 case '\v': 188 if (tok & TOK_SPACE) { /* yes */
256 case '\f': 189 if (is_space) /* this one too, ignore it */
257 case ' ': 190 continue;
258 space = 1; 191 tok &= ~TOK_SPACE;
259 continue; 192 } else if (is_space) {
260 default: 193 /* 1st whitespace char.
261 if (space && !(option_mask32 & FLAG_w)) { 194 * Set TOK_SPACE and replace char by ' ' */
262 i = 1; 195 t = TOK_SPACE + ' ';
263 space = 0;
264 }
265 sum = fiddle_sum(sum, t);
266 i = 1;
267 continue;
268 case EOF:
269 if (i == 0)
270 return 0;
271 /* FALLTHROUGH */
272 case '\n':
273 break;
274 } 196 }
275 break;
276 } 197 }
198 /* Clear EMPTY */
199 tok &= ~(TOK_EMPTY + CHAR_MASK);
200 /* Assign char value (low 9 bits) and maybe set TOK_SPACE */
201 tok |= t;
202 break;
277 } 203 }
278 /* 204#if 0
279 * There is a remote possibility that we end up with a zero sum. 205 bb_error_msg("fp:%p tok:%x '%c'%s%s%s%s", fp, tok, tok & 0xff
280 * Zero is used as an EOF marker, so return 1 instead. 206 , tok & TOK_EOF ? " EOF" : ""
281 */ 207 , tok & TOK_EOL ? " EOL" : ""
282 return (sum == 0 ? 1 : sum); 208 , tok & TOK_EMPTY ? " EMPTY" : ""
209 , tok & TOK_SPACE ? " SPACE" : ""
210 );
211#endif
212 return tok;
283} 213}
284 214
215struct cand {
216 int x;
217 int y;
218 int pred;
219};
285 220
286/* Our diff implementation is using seek. 221static int search(const int *c, int k, int y, const struct cand *list)
287 * When we meet non-seekable file, we must make a temp copy.
288 */
289static char *make_temp(FILE *f, struct stat *sb)
290{ 222{
291 char *name; 223 if (list[c[k]].y < y) /* quick look for typical case */
292 int fd; 224 return k + 1;
293
294 if (S_ISREG(sb->st_mode) || S_ISBLK(sb->st_mode))
295 return NULL;
296 name = xstrdup("/tmp/difXXXXXX");
297 fd = mkstemp(name);
298 if (fd < 0)
299 bb_perror_msg_and_die("mkstemp");
300 if (bb_copyfd_eof(fileno(f), fd) < 0) {
301 clean_up:
302 unlink(name);
303 xfunc_die(); /* error message is printed by bb_copyfd_eof */
304 }
305 fstat(fd, sb);
306 close(fd);
307 if (freopen(name, "r+", f) == NULL) {
308 bb_perror_msg("freopen");
309 goto clean_up;
310 }
311 return name;
312}
313
314 225
315/* 226 for (int i = 0, j = k + 1;;) {
316 * Check to see if the given files differ. 227 const int l = (i + j) >> 1;
317 * Returns 0 if they are the same, 1 if different, and -1 on error. 228 if (l > i) {
318 */ 229 const int t = list[c[l]].y;
319static NOINLINE int files_differ(FILE *f1, FILE *f2) 230 if (t > y)
320{ 231 j = l;
321 size_t i, j; 232 else if (t < y)
322 233 i = l;
323 /* Prevent making copies for "/dev/null" (too common) */ 234 else
324 /* Deal with input from pipes etc */ 235 return l;
325 tempname1 = make_temp(f1, &stb1); 236 } else
326 tempname2 = make_temp(f2, &stb2); 237 return l + 1;
327 if (stb1.st_size != stb2.st_size) {
328 return 1;
329 }
330 while (1) {
331 i = fread(g_read_buf, 1, COMMON_BUFSIZE/2, f1);
332 j = fread(g_read_buf + COMMON_BUFSIZE/2, 1, COMMON_BUFSIZE/2, f2);
333 if (i != j)
334 return 1;
335 if (i == 0)
336 return (ferror(f1) || ferror(f2)) ? -1 : 0;
337 if (memcmp(g_read_buf,
338 g_read_buf + COMMON_BUFSIZE/2, i) != 0)
339 return 1;
340 } 238 }
341} 239}
342 240
343 241static unsigned isqrt(unsigned n)
344static void prepare(int i, FILE *fp /*, off_t filesize*/)
345{ 242{
346 struct line *p; 243 unsigned x = 1;
347 int h; 244 while (1) {
348 size_t j, sz; 245 const unsigned y = x;
349 246 x = ((n / x) + x) >> 1;
350 rewind(fp); 247 if (x <= (y + 1) && x >= (y - 1))
351 248 return x;
352 /*sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;*/
353 /*if (sz < 100)*/
354 sz = 100;
355
356 p = xmalloc((sz + 3) * sizeof(p[0]));
357 j = 0;
358 while ((h = readhash(fp)) != 0) { /* while not EOF */
359 if (j == sz) {
360 sz = sz * 3 / 2;
361 p = xrealloc(p, (sz + 3) * sizeof(p[0]));
362 }
363 p[++j].value = h;
364 } 249 }
365 nlen[i] = j;
366 nfile[i] = p;
367} 250}
368 251
369 252static void stone(const int *a, int n, const int *b, int *J, int pref)
370static void prune(void)
371{ 253{
372 int i, j; 254 const unsigned isq = isqrt(n);
373 255 const unsigned bound =
374 for (pref = 0; pref < nlen[0] && pref < nlen[1] && 256 (option_mask32 & FLAG(d)) ? UINT_MAX : MAX(256, isq);
375 nfile[0][pref + 1].value == nfile[1][pref + 1].value; pref++) 257 int clen = 1;
376 continue; 258 int clistlen = 100;
377 for (suff = 0; suff < nlen[0] - pref && suff < nlen[1] - pref && 259 int k = 0;
378 nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value; 260 struct cand *clist = xzalloc(clistlen * sizeof(clist[0]));
379 suff++) 261 int *klist = xzalloc((n + 2) * sizeof(klist[0]));
380 continue; 262 /*clist[0] = (struct cand){0}; - xzalloc did it */
381 for (j = 0; j < 2; j++) { 263 /*klist[0] = 0; */
382 sfile[j] = nfile[j] + pref; 264
383 slen[j] = nlen[j] - pref - suff; 265 for (struct cand cand = {1}; cand.x <= n; cand.x++) {
384 for (i = 0; i <= slen[j]; i++) 266 int j = a[cand.x], oldl = 0;
385 sfile[j][i].serial = i; 267 unsigned numtries = 0;
268 if (j == 0)
269 continue;
270 cand.y = -b[j];
271 cand.pred = klist[0];
272 do {
273 int l, tc;
274 if (cand.y <= clist[cand.pred].y)
275 continue;
276 l = search(klist, k, cand.y, clist);
277 if (l != oldl + 1)
278 cand.pred = klist[l - 1];
279 if (l <= k && clist[klist[l]].y <= cand.y)
280 continue;
281 if (clen == clistlen) {
282 clistlen = clistlen * 11 / 10;
283 clist = xrealloc(clist, clistlen * sizeof(clist[0]));
284 }
285 clist[clen] = cand;
286 tc = klist[l];
287 klist[l] = clen++;
288 if (l <= k) {
289 cand.pred = tc;
290 oldl = l;
291 numtries++;
292 } else {
293 k++;
294 break;
295 }
296 } while ((cand.y = b[++j]) > 0 && numtries < bound);
386 } 297 }
298 /* Unravel */
299 for (struct cand *q = clist + klist[k]; q->y; q = clist + q->pred)
300 J[q->x + pref] = q->y + pref;
301 free(klist);
302 free(clist);
387} 303}
388 304
305struct line {
306 /* 'serial' is not used in the begining, so we reuse it
307 * to store line offsets, thus reducing memory pressure
308 */
309 union {
310 unsigned serial;
311 off_t offset;
312 };
313 unsigned value;
314};
389 315
390static void equiv(struct line *a, int n, struct line *b, int m, int *c) 316static void equiv(struct line *a, int n, struct line *b, int m, int *c)
391{ 317{
392 int i, j; 318 int i = 1, j = 1;
393 319
394 i = j = 1;
395 while (i <= n && j <= m) { 320 while (i <= n && j <= m) {
396 if (a[i].value < b[j].value) 321 if (a[i].value < b[j].value)
397 a[i++].value = 0; 322 a[i++].value = 0;
@@ -414,286 +339,28 @@ static void equiv(struct line *a, int n, struct line *b, int m, int *c)
414 c[j] = -1; 339 c[j] = -1;
415} 340}
416 341
417 342static void unsort(const struct line *f, int l, int *b)
418static int isqrt(int n)
419{
420 int y, x;
421
422 if (n == 0)
423 return 0;
424 x = 1;
425 do {
426 y = x;
427 x = n / x;
428 x += y;
429 x /= 2;
430 } while ((x - y) > 1 || (x - y) < -1);
431
432 return x;
433}
434
435
436static int newcand(int x, int y, int pred)
437{ 343{
438 struct cand *q; 344 int *a = xmalloc((l + 1) * sizeof(a[0]));
439 345 for (int i = 1; i <= l; i++)
440 if (clen == clistlen) {
441 clistlen = clistlen * 11 / 10;
442 clist = xrealloc(clist, clistlen * sizeof(struct cand));
443 }
444 q = clist + clen;
445 q->x = x;
446 q->y = y;
447 q->pred = pred;
448 return clen++;
449}
450
451
452static int search(int *c, int k, int y)
453{
454 int i, j, l, t;
455
456 if (clist[c[k]].y < y) /* quick look for typical case */
457 return k + 1;
458 i = 0;
459 j = k + 1;
460 while (1) {
461 l = i + j;
462 if ((l >>= 1) <= i)
463 break;
464 t = clist[c[l]].y;
465 if (t > y)
466 j = l;
467 else if (t < y)
468 i = l;
469 else
470 return l;
471 }
472 return l + 1;
473}
474
475
476static int stone(int *a, int n, int *b, int *c)
477{
478 int i, k, y, j, l;
479 int oldc, tc, oldl;
480 unsigned numtries;
481 int isq = isqrt(n);
482#if ENABLE_FEATURE_DIFF_MINIMAL
483 const unsigned bound =
484 (option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isq);
485#else
486 const unsigned bound = MAX(256, isq);
487#endif
488
489 k = 0;
490 c[0] = newcand(0, 0, 0);
491 for (i = 1; i <= n; i++) {
492 j = a[i];
493 if (j == 0)
494 continue;
495 y = -b[j];
496 oldl = 0;
497 oldc = c[0];
498 numtries = 0;
499 do {
500 if (y <= clist[oldc].y)
501 continue;
502 l = search(c, k, y);
503 if (l != oldl + 1)
504 oldc = c[l - 1];
505 if (l <= k) {
506 if (clist[c[l]].y <= y)
507 continue;
508 tc = c[l];
509 c[l] = newcand(i, y, oldc);
510 oldc = tc;
511 oldl = l;
512 numtries++;
513 } else {
514 c[l] = newcand(i, y, oldc);
515 k++;
516 break;
517 }
518 } while ((y = b[++j]) > 0 && numtries < bound);
519 }
520 return k;
521}
522
523
524static void unravel(int p)
525{
526 struct cand *q;
527 int i;
528
529 for (i = 0; i <= nlen[0]; i++)
530 J[i] = i <= pref ? i : i > nlen[0] - suff ? i + nlen[1] - nlen[0] : 0;
531 for (q = clist + p; q->y != 0; q = clist + q->pred)
532 J[q->x + pref] = q->y + pref;
533}
534
535
536static void unsort(struct line *f, int l, int *b)
537{
538 int *a, i;
539
540 a = xmalloc((l + 1) * sizeof(int));
541 for (i = 1; i <= l; i++)
542 a[f[i].serial] = f[i].value; 346 a[f[i].serial] = f[i].value;
543 for (i = 1; i <= l; i++) 347 for (int i = 1; i <= l; i++)
544 b[i] = a[i]; 348 b[i] = a[i];
545 free(a); 349 free(a);
546} 350}
547 351
548 352static int line_compar(const void *a, const void *b)
549static int skipline(FILE *f)
550{
551 int i, c;
552
553 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
554 continue;
555 return i;
556}
557
558
559/*
560 * Check does double duty:
561 * 1. ferret out any fortuitous correspondences due
562 * to confounding by hashing (which result in "jackpot")
563 * 2. collect random access indexes to the two files
564 */
565static NOINLINE void check(FILE *f1, FILE *f2)
566{
567 int i, j, jackpot, c, d;
568 long ctold, ctnew;
569
570 rewind(f1);
571 rewind(f2);
572 j = 1;
573 ixold[0] = ixnew[0] = 0;
574 jackpot = 0;
575 ctold = ctnew = 0;
576 for (i = 1; i <= nlen[0]; i++) {
577 if (J[i] == 0) {
578 ixold[i] = ctold += skipline(f1);
579 continue;
580 }
581 while (j < J[i]) {
582 ixnew[j] = ctnew += skipline(f2);
583 j++;
584 }
585 if (option_mask32 & (FLAG_b | FLAG_w | FLAG_i)) {
586 while (1) {
587 c = getc(f1);
588 d = getc(f2);
589 /*
590 * GNU diff ignores a missing newline
591 * in one file if bflag || wflag.
592 */
593 if ((option_mask32 & (FLAG_b | FLAG_w))
594 && ((c == EOF && d == '\n') || (c == '\n' && d == EOF))
595 ) {
596 break;
597 }
598 ctold++;
599 ctnew++;
600 if ((option_mask32 & FLAG_b) && isspace(c) && isspace(d)) {
601 do {
602 if (c == '\n')
603 break;
604 ctold++;
605 c = getc(f1);
606 } while (isspace(c));
607 do {
608 if (d == '\n')
609 break;
610 ctnew++;
611 d = getc(f2);
612 } while (isspace(d));
613 } else if (option_mask32 & FLAG_w) {
614 while (isspace(c) && c != '\n') {
615 c = getc(f1);
616 ctold++;
617 }
618 while (isspace(d) && d != '\n') {
619 d = getc(f2);
620 ctnew++;
621 }
622 }
623 if (c != d) {
624 jackpot++;
625 J[i] = 0;
626 if (c != '\n' && c != EOF)
627 ctold += skipline(f1);
628 if (d != '\n' && c != EOF)
629 ctnew += skipline(f2);
630 break;
631 }
632 if (c == '\n' || c == EOF)
633 break;
634 }
635 } else {
636 while (1) {
637 ctold++;
638 ctnew++;
639 c = getc(f1);
640 d = getc(f2);
641 if (c != d) {
642 J[i] = 0;
643 if (c != '\n' && c != EOF)
644 ctold += skipline(f1);
645/* was buggy? "if (d != '\n' && c != EOF)" */
646 if (d != '\n' && d != EOF)
647 ctnew += skipline(f2);
648 break;
649 }
650 if (c == '\n' || c == EOF)
651 break;
652 }
653 }
654 ixold[i] = ctold;
655 ixnew[j] = ctnew;
656 j++;
657 }
658 for (; j <= nlen[1]; j++)
659 ixnew[j] = ctnew += skipline(f2);
660}
661
662
663/* shellsort CACM #201 */
664static void sort(struct line *a, int n)
665{ 353{
666 struct line *ai, *aim, w; 354#define l0 ((const struct line*)a)
667 int j, m = 0, k; 355#define l1 ((const struct line*)b)
668 356 int r = l0->value - l1->value;
669 if (n == 0) 357 if (r)
670 return; 358 return r;
671 for (j = 1; j <= n; j *= 2) 359 return l0->serial - l1->serial;
672 m = 2 * j - 1; 360#undef l0
673 for (m /= 2; m != 0; m /= 2) { 361#undef l1
674 k = n - m;
675 for (j = 1; j <= k; j++) {
676 for (ai = &a[j]; ai > a; ai -= m) {
677 aim = &ai[m];
678 if (aim < ai)
679 break; /* wraparound */
680 if (aim->value > ai[0].value
681 || (aim->value == ai[0].value && aim->serial > ai[0].serial)
682 ) {
683 break;
684 }
685 w.value = ai[0].value;
686 ai[0].value = aim->value;
687 aim->value = w.value;
688 w.serial = ai[0].serial;
689 ai[0].serial = aim->serial;
690 aim->serial = w.serial;
691 }
692 }
693 }
694} 362}
695 363
696
697static void uni_range(int a, int b) 364static void uni_range(int a, int b)
698{ 365{
699 if (a < b) 366 if (a < b)
@@ -704,33 +371,23 @@ static void uni_range(int a, int b)
704 printf("%d,0", b); 371 printf("%d,0", b);
705} 372}
706 373
707 374static void fetch(FILE_and_pos_t *ft, const off_t *ix, int a, int b, int ch)
708static void fetch(long *f, int a, int b, FILE *lb, int ch)
709{ 375{
710 int i, j, c, lastc, col, nc; 376 for (int i = a; i <= b; i++) {
711 377 seek_ft(ft, ix[i - 1]);
712 if (a > b) 378 putchar(ch);
713 return; 379 if (option_mask32 & FLAG(T))
714 for (i = a; i <= b; i++) { 380 putchar('\t');
715 fseek(lb, f[i - 1], SEEK_SET); 381 for (int j = 0, col = 0; j < ix[i] - ix[i - 1]; j++) {
716 nc = f[i] - f[i - 1]; 382 int c = fgetc(ft->ft_fp);
717 if (ch != '\0') {
718 putchar(ch);
719 if (option_mask32 & FLAG_T)
720 putchar('\t');
721 }
722 col = 0;
723 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
724 c = getc(lb);
725 if (c == EOF) { 383 if (c == EOF) {
726 printf("\n\\ No newline at end of file\n"); 384 printf("\n\\ No newline at end of file\n");
727 return; 385 return;
728 } 386 }
729 if (c == '\t' && (option_mask32 & FLAG_t)) { 387 ft->ft_pos++;
730 do { 388 if (c == '\t' && (option_mask32 & FLAG(t)))
731 putchar(' '); 389 do putchar(' '); while (++col & 7);
732 } while (++col & 7); 390 else {
733 } else {
734 putchar(c); 391 putchar(c);
735 col++; 392 col++;
736 } 393 }
@@ -738,540 +395,477 @@ static void fetch(long *f, int a, int b, FILE *lb, int ch)
738 } 395 }
739} 396}
740 397
741 398/* Creates the match vector J, where J[i] is the index
742#if ENABLE_FEATURE_DIFF_BINARY 399 * of the line in the new file corresponding to the line i
743static int asciifile(FILE *f) 400 * in the old file. Lines start at 1 instead of 0, that value
401 * being used instead to denote no corresponding line.
402 * This vector is dynamically allocated and must be freed by the caller.
403 *
404 * * fp is an input parameter, where fp[0] and fp[1] are the open
405 * old file and new file respectively.
406 * * nlen is an output variable, where nlen[0] and nlen[1]
407 * gets the number of lines in the old and new file respectively.
408 * * ix is an output variable, where ix[0] and ix[1] gets
409 * assigned dynamically allocated vectors of the offsets of the lines
410 * of the old and new file respectively. These must be freed by the caller.
411 */
412static int *create_J(FILE_and_pos_t ft[2], int nlen[2], off_t *ix[2])
744{ 413{
745 int i, cnt; 414 int *J, slen[2], *class, *member;
746 415 struct line *nfile[2], *sfile[2];
747 if (option_mask32 & FLAG_a) 416 int pref = 0, suff = 0;
748 return 1; 417
749 rewind(f); 418 /* Lines of both files are hashed, and in the process
750 cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f); 419 * their offsets are stored in the array ix[fileno]
751 for (i = 0; i < cnt; i++) { 420 * where fileno == 0 points to the old file, and
752 if (!isprint_asciionly(g_read_buf[i]) 421 * fileno == 1 points to the new one.
753 && !isspace(g_read_buf[i]) 422 */
754 ) { 423 for (int i = 0; i < 2; i++) {
755 return 0; 424 unsigned hash;
425 token_t tok;
426 size_t sz = 100;
427 nfile[i] = xmalloc((sz + 3) * sizeof(nfile[i][0]));
428 seek_ft(&ft[i], 0);
429
430 nlen[i] = 0;
431 /* We could zalloc nfile, but then zalloc starts showing in gprof at ~1% */
432 nfile[i][0].offset = 0;
433 goto start; /* saves code */
434 while (1) {
435 tok = read_token(&ft[i], tok);
436 if (!(tok & TOK_EMPTY)) {
437 /* Hash algorithm taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578. */
438 hash = hash * 128 - hash + TOK2CHAR(tok);
439 continue;
440 }
441 if (nlen[i]++ == sz) {
442 sz = sz * 3 / 2;
443 nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
444 }
445 /* line_compar needs hashes fit into positive int */
446 nfile[i][nlen[i]].value = hash & INT_MAX;
447 /* like ftello(ft[i].ft_fp) but faster (avoids lseek syscall) */
448 nfile[i][nlen[i]].offset = ft[i].ft_pos;
449 if (tok & TOK_EOF) {
450 /* EOF counts as a token, so we have to adjust it here */
451 nfile[i][nlen[i]].offset++;
452 break;
453 }
454start:
455 hash = tok = 0;
756 } 456 }
457 /* Exclude lone EOF line from the end of the file, to make fetch()'s job easier */
458 if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
459 nlen[i]--;
460 /* Now we copy the line offsets into ix */
461 ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
462 for (int j = 0; j < nlen[i] + 1; j++)
463 ix[i][j] = nfile[i][j].offset;
464 }
465
466 /* lenght of prefix and suffix is calculated */
467 for (; pref < nlen[0] && pref < nlen[1] &&
468 nfile[0][pref + 1].value == nfile[1][pref + 1].value;
469 pref++);
470 for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
471 nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
472 suff++);
473 /* Arrays are pruned by the suffix and prefix lenght,
474 * the result being sorted and stored in sfile[fileno],
475 * and their sizes are stored in slen[fileno]
476 */
477 for (int j = 0; j < 2; j++) {
478 sfile[j] = nfile[j] + pref;
479 slen[j] = nlen[j] - pref - suff;
480 for (int i = 0; i <= slen[j]; i++)
481 sfile[j][i].serial = i;
482 qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
757 } 483 }
758 return 1; 484 /* nfile arrays are reused to reduce memory pressure
759} 485 * The #if zeroed out section performs the same task as the
760#else 486 * one in the #else section.
761#define asciifile(f) 1 487 * Peak memory usage is higher, but one array copy is avoided
762#endif 488 * by not using unsort()
763
764
765/* dump accumulated "unified" diff changes */
766static void dump_unified_vec(FILE *f1, FILE *f2)
767{
768 struct context_vec *cvp = context_vector;
769 int lowa, upb, lowc, upd;
770 int a, b, c, d;
771 char ch;
772
773 if (context_idx < 0)
774 return;
775
776 dbg_error_msg("dumping %d context_vecs", context_idx+1);
777
778 b = d = 0; /* gcc */
779 lowa = MAX(1, cvp->a - opt_U_context);
780 upb = MIN(nlen[0], context_vector[context_idx].b + opt_U_context);
781 lowc = MAX(1, cvp->c - opt_U_context);
782 upd = MIN(nlen[1], context_vector[context_idx].d + opt_U_context);
783
784 printf("@@ -");
785 uni_range(lowa, upb);
786 printf(" +");
787 uni_range(lowc, upd);
788 printf(" @@\n");
789
790 /*
791 * Output changes in "unified" diff format--the old and new lines
792 * are printed together.
793 */ 489 */
794 for (; cvp <= &context_vector[context_idx]; cvp++) {
795 a = cvp->a;
796 b = cvp->b;
797 c = cvp->c;
798 d = cvp->d;
799
800 /*
801 * c: both new and old changes
802 * d: only changes in the old file
803 * a: only changes in the new file
804 */
805 if (a <= b && c <= d)
806 ch = 'c';
807 else
808 ch = (a <= b) ? 'd' : 'a';
809#if 0 490#if 0
810 switch (ch) { 491 member = xmalloc((slen[1] + 2) * sizeof(member[0]));
811 case 'c': 492 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
812// fetch() seeks! 493 free(nfile[1]);
813 fetch(ixold, lowa, a - 1, f1, ' '); 494
814 fetch(ixold, a, b, f1, '-'); 495 class = xmalloc((slen[0] + 1) * sizeof(class[0]));
815 fetch(ixnew, c, d, f2, '+'); 496 for (int i = 1; i <= slen[0]; i++) /* Unsorting */
816 break; 497 class[sfile[0][i].serial] = sfile[0][i].value;
817 case 'd': 498 free(nfile[0]);
818 fetch(ixold, lowa, a - 1, f1, ' ');
819 fetch(ixold, a, b, f1, '-');
820 break;
821 case 'a':
822 fetch(ixnew, lowc, c - 1, f2, ' ');
823 fetch(ixnew, c, d, f2, '+');
824 break;
825 }
826#else 499#else
827 if (ch == 'c' || ch == 'd') { 500 member = (int *)nfile[1];
828 fetch(ixold, lowa, a - 1, f1, ' '); 501 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
829 fetch(ixold, a, b, f1, '-'); 502 member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
830 }
831 if (ch == 'a')
832 fetch(ixnew, lowc, c - 1, f2, ' ');
833 if (ch == 'c' || ch == 'a')
834 fetch(ixnew, c, d, f2, '+');
835#endif
836 lowa = b + 1;
837 lowc = d + 1;
838 }
839 fetch(ixnew, d + 1, upd, f2, ' ');
840 503
841 context_idx = -1; 504 class = (int *)nfile[0];
842} 505 unsort(sfile[0], slen[0], (int *)nfile[0]);
506 class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
507#endif
508 J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
509 /* The elements of J which fall inside the prefix and suffix regions
510 * are marked as unchanged, while the ones which fall outside
511 * are initialized with 0 (no matches), so that function stone can
512 * then assign them their right values
513 */
514 for (int i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
515 J[i] = i <= pref ? i :
516 i > (nlen[0] - suff) ? (i + delta) : 0;
517 /* Here the magic is performed */
518 stone(class, slen[0], member, J, pref);
519 J[nlen[0] + 1] = nlen[1] + 1;
843 520
521 free(class);
522 free(member);
844 523
845static void print_header(const char *file1, const char *file2) 524 /* Both files are rescanned, in an effort to find any lines
846{ 525 * which, due to limitations intrinsic to any hashing algorithm,
847 if (label1) 526 * are different but ended up confounded as the same
848 printf("--- %s\n", label1); 527 */
849 else 528 for (int i = 1; i <= nlen[0]; i++) {
850 printf("--- %s\t%s", file1, ctime(&stb1.st_mtime)); 529 if (!J[i])
851 if (label2) 530 continue;
852 printf("+++ %s\n", label2);
853 else
854 printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
855}
856 531
532 seek_ft(&ft[0], ix[0][i - 1]);
533 seek_ft(&ft[1], ix[1][J[i] - 1]);
857 534
858/* 535 for (int j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
859 * Indicate that there is a difference between lines a and b of the from file 536 token_t tok0 = 0, tok1 = 0;
860 * to get to lines c to d of the to file. If a is greater than b then there 537 do {
861 * are no lines in the from file involved and this means that there were 538 tok0 = read_token(&ft[0], tok0);
862 * lines appended (beginning at b). If c is greater than d then there are 539 tok1 = read_token(&ft[1], tok1);
863 * lines missing from the to file.
864 */
865static void change(const char *file1, FILE *f1, const char *file2, FILE *f2,
866 int a, int b, int c, int d)
867{
868 if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
869//compat BUG: "diff -ub F1 F2" will output nothing, but will exit 1
870//if F1 and F2 differ only in whitespace. "standard" diff exits 0.
871//This is the place where this erroneous exitcode is set:
872 dbg_error_msg("%d: abcd:%d,%d,%d,%d, anychange=1", __LINE__, a,b,c,d);
873 anychange = 1;
874 return;
875 }
876 540
877 if (anychange == 0) { 541 if (((tok0 ^ tok1) & TOK_EMPTY) != 0 /* one is empty (not both) */
878 /* 542 || (!(tok0 & TOK_EMPTY) && TOK2CHAR(tok0) != TOK2CHAR(tok1)))
879 * Print the context/unidiff header first time through. 543 J[i] = 0; /* Break the correspondence */
880 */ 544 } while (!(tok0 & tok1 & TOK_EMPTY));
881 print_header(file1, file2); 545 }
882 } else if (a > context_vector[context_idx].b + (2 * opt_U_context) + 1
883 && c > context_vector[context_idx].d + (2 * opt_U_context) + 1
884 ) {
885 /*
886 * If this change is more than 'context' lines from the
887 * previous change, dump the record and reset it.
888 */
889// dump_unified_vec() seeks!
890 dump_unified_vec(f1, f2);
891 } 546 }
892 context_idx++;
893 context_vector = xrealloc_vector(context_vector, 6, context_idx);
894 context_vector[context_idx].a = a;
895 context_vector[context_idx].b = b;
896 context_vector[context_idx].c = c;
897 context_vector[context_idx].d = d;
898 dbg_error_msg("new context_vec[%d]:%d,%d,%d,%d", context_idx, a,b,c,d);
899 anychange = 1;
900}
901 547
902 548 return J;
903static void output(const char *file1, FILE *f1, const char *file2, FILE *f2)
904{
905 /* Note that j0 and j1 can't be used as they are defined in math.h.
906 * This also allows the rather amusing variable 'j00'... */
907 int m, i0, i1, j00, j01;
908
909 rewind(f1);
910 rewind(f2);
911 m = nlen[0];
912 J[0] = 0;
913 J[m + 1] = nlen[1] + 1;
914 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
915 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
916 i0++;
917 j00 = J[i0 - 1] + 1;
918 i1 = i0 - 1;
919 while (i1 < m && J[i1 + 1] == 0)
920 i1++;
921 j01 = J[i1 + 1] - 1;
922 J[i1] = j01;
923// change() seeks!
924 change(file1, f1, file2, f2, i0, i1, j00, j01);
925 }
926 if (m == 0) {
927// change() seeks!
928 change(file1, f1, file2, f2, 1, 0, 1, nlen[1]);
929 }
930 if (anychange != 0 && !(option_mask32 & FLAG_q)) {
931// dump_unified_vec() seeks!
932 dump_unified_vec(f1, f2);
933 }
934} 549}
935 550
936/* 551/*
937 * The following code uses an algorithm due to Harold Stone, 552 * The following struct is used to record change information
938 * which finds a pair of longest identical subsequences in 553 * doing a "context" or "unified" diff.
939 * the two files.
940 *
941 * The major goal is to generate the match vector J.
942 * J[i] is the index of the line in file1 corresponding
943 * to line i in file0. J[i] = 0 if there is no
944 * such line in file1.
945 *
946 * Lines are hashed so as to work in core. All potential
947 * matches are located by sorting the lines of each file
948 * on the hash (called "value"). In particular, this
949 * collects the equivalence classes in file1 together.
950 * Subroutine equiv replaces the value of each line in
951 * file0 by the index of the first element of its
952 * matching equivalence in (the reordered) file1.
953 * To save space equiv squeezes file1 into a single
954 * array member in which the equivalence classes
955 * are simply concatenated, except that their first
956 * members are flagged by changing sign.
957 *
958 * Next the indices that point into member are unsorted into
959 * array class according to the original order of file0.
960 *
961 * The cleverness lies in routine stone. This marches
962 * through the lines of file0, developing a vector klist
963 * of "k-candidates". At step i a k-candidate is a matched
964 * pair of lines x,y (x in file0, y in file1) such that
965 * there is a common subsequence of length k
966 * between the first i lines of file0 and the first y
967 * lines of file1, but there is no such subsequence for
968 * any smaller y. x is the earliest possible mate to y
969 * that occurs in such a subsequence.
970 *
971 * Whenever any of the members of the equivalence class of
972 * lines in file1 matable to a line in file0 has serial number
973 * less than the y of some k-candidate, that k-candidate
974 * with the smallest such y is replaced. The new
975 * k-candidate is chained (via pred) to the current
976 * k-1 candidate so that the actual subsequence can
977 * be recovered. When a member has serial number greater
978 * that the y of all k-candidates, the klist is extended.
979 * At the end, the longest subsequence is pulled out
980 * and placed in the array J by unravel
981 *
982 * With J in hand, the matches there recorded are
983 * checked against reality to assure that no spurious
984 * matches have crept in due to hashing. If they have,
985 * they are broken, and "jackpot" is recorded--a harmless
986 * matter except that a true match for a spuriously
987 * mated line may now be unnecessarily reported as a change.
988 *
989 * Much of the complexity of the program comes simply
990 * from trying to minimize core utilization and
991 * maximize the range of doable problems by dynamically
992 * allocating what is needed and reusing what is not.
993 * The core requirements for problems larger than somewhat
994 * are (in words) 2*length(file0) + length(file1) +
995 * 3*(number of k-candidates installed), typically about
996 * 6n words for files of length n.
997 */ 554 */
998/* NB: files can be not REGular. The only sure thing that they 555struct context_vec {
999 * are not both DIRectories. */ 556 int a; /* start line in old file */
1000static unsigned diffreg(const char *file1, const char *file2, int flags) 557 int b; /* end line in old file */
558 int c; /* start line in new file */
559 int d; /* end line in new file */
560};
561
562static bool diff(FILE_and_pos_t ft[2], char *file[2])
1001{ 563{
1002 int *member; /* will be overlaid on nfile[1] */ 564 int nlen[2];
1003 int *class; /* will be overlaid on nfile[0] */ 565 off_t *ix[2];
1004 int *klist; /* will be overlaid on nfile[0] after class */ 566 int *J = create_J(ft, nlen, ix);
1005 FILE *f1;
1006 FILE *f2;
1007 unsigned rval;
1008 int i;
1009
1010 anychange = 0;
1011 context_idx = -1;
1012 tempname1 = tempname2 = NULL;
1013
1014 /* Is any of them a directory? Then it's simple */
1015 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
1016 return (S_ISDIR(stb1.st_mode) ? D_ISDIR1 : D_ISDIR2);
1017
1018 /* None of them are directories */
1019 rval = D_SAME;
1020
1021 if (flags & D_EMPTY1)
1022 /* can't be stdin, but xfopen_stdin() is smaller code */
1023 file1 = bb_dev_null;
1024 f1 = xfopen_stdin(file1);
1025 if (flags & D_EMPTY2)
1026 file2 = bb_dev_null;
1027 f2 = xfopen_stdin(file2);
1028
1029 /* NB: if D_EMPTY1/2 is set, other file is always a regular file,
1030 * not pipe/fifo/chardev/etc - D_EMPTY is used by "diff -r" only,
1031 * and it never diffs non-ordinary files in subdirs. */
1032 if (!(flags & (D_EMPTY1 | D_EMPTY2))) {
1033 /* Quick check whether they are different */
1034 /* NB: copies non-REG files to tempfiles and fills tempname1/2 */
1035 i = files_differ(f1, f2);
1036 if (i != 1) { /* not different? */
1037 if (i != 0) /* error? */
1038 exit_status |= 2;
1039 goto closem;
1040 }
1041 }
1042 567
1043 if (!asciifile(f1) || !asciifile(f2)) { 568 bool anychange = false;
1044 rval = D_BINARY; 569 struct context_vec *vec = NULL;
1045 exit_status |= 1; 570 int idx = -1, i = 1;
1046 goto closem;
1047 }
1048 571
1049// Rewind inside! 572 do {
1050 prepare(0, f1 /*, stb1.st_size*/); 573 while (1) {
1051 prepare(1, f2 /*, stb2.st_size*/); 574 struct context_vec v;
1052 prune();
1053 sort(sfile[0], slen[0]);
1054 sort(sfile[1], slen[1]);
1055 575
1056 member = (int *) nfile[1]; 576 for (v.a = i; v.a <= nlen[0] && J[v.a] == J[v.a - 1] + 1; v.a++)
1057 equiv(sfile[0], slen[0], sfile[1], slen[1], member); 577 continue;
1058//TODO: xrealloc_vector? 578 v.c = J[v.a - 1] + 1;
1059 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
1060
1061 class = (int *) nfile[0];
1062 unsort(sfile[0], slen[0], class);
1063 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
1064
1065 klist = xmalloc((slen[0] + 2) * sizeof(int));
1066 clen = 0;
1067 clistlen = 100;
1068 clist = xmalloc(clistlen * sizeof(struct cand));
1069 i = stone(class, slen[0], member, klist);
1070 free(member);
1071 free(class);
1072 579
1073 J = xrealloc(J, (nlen[0] + 2) * sizeof(int)); 580 for (v.b = v.a - 1; v.b < nlen[0] && !J[v.b + 1]; v.b++)
1074 unravel(klist[i]); 581 continue;
1075 free(clist); 582 v.d = J[v.b + 1] - 1;
1076 free(klist); 583 /*
584 * Indicate that there is a difference between lines a and b of the 'from' file
585 * to get to lines c to d of the 'to' file. If a is greater than b then there
586 * are no lines in the 'from' file involved and this means that there were
587 * lines appended (beginning at b). If c is greater than d then there are
588 * lines missing from the 'to' file.
589 */
590 if (v.a <= v.b || v.c <= v.d) {
591 /*
592 * If this change is more than 'context' lines from the
593 * previous change, dump the record and reset it.
594 */
595 if (idx >= 0
596 && v.a > vec[idx].b + (2 * opt_U_context) + 1
597 && v.c > vec[idx].d + (2 * opt_U_context) + 1
598 ) {
599 break;
600 }
601 vec = xrealloc_vector(vec, 6, ++idx);
602 vec[idx] = v;
603 }
604
605 i = v.b + 1;
606 if (i > nlen[0])
607 break;
608 J[v.b] = v.d;
609 }
610 if (idx < 0)
611 continue;
612 if (!(option_mask32 & FLAG(q))) {
613 struct context_vec *cvp = vec;
614 int lowa = MAX(1, cvp->a - opt_U_context);
615 int upb = MIN(nlen[0], vec[idx].b + opt_U_context);
616 int lowc = MAX(1, cvp->c - opt_U_context);
617 int upd = MIN(nlen[1], vec[idx].d + opt_U_context);
618
619 if (!anychange) {
620 /* Print the context/unidiff header first time through */
621 printf("--- %s\n", label[0] ?: file[0]);
622 printf("+++ %s\n", label[1] ?: file[1]);
623 }
1077 624
1078 ixold = xrealloc(ixold, (nlen[0] + 2) * sizeof(long)); 625 printf("@@ -");
1079 ixnew = xrealloc(ixnew, (nlen[1] + 2) * sizeof(long)); 626 uni_range(lowa, upb);
1080// Rewind inside! 627 printf(" +");
1081 check(f1, f2); 628 uni_range(lowc, upd);
1082// Rewind inside! 629 printf(" @@\n");
1083 output(file1, f1, file2, f2);
1084 630
1085 closem: 631 /*
1086 if (anychange) { 632 * Output changes in "unified" diff format--the old and new lines
1087 exit_status |= 1; 633 * are printed together.
1088 dbg_error_msg("exit_status|=1 = %d", exit_status); 634 */
1089 if (rval == D_SAME) 635 while (1) {
1090 rval = D_DIFFER; 636 bool end = cvp > &vec[idx];
1091 } 637 fetch(&ft[0], ix[0], lowa, end ? upb : cvp->a - 1, ' ');
1092 fclose_if_not_stdin(f1); 638 if (end)
1093 fclose_if_not_stdin(f2); 639 break;
1094 if (tempname1) { 640 fetch(&ft[0], ix[0], cvp->a, cvp->b, '-');
1095 unlink(tempname1); 641 fetch(&ft[1], ix[1], cvp->c, cvp->d, '+');
1096 free(tempname1); 642 lowa = cvp++->b + 1;
1097 } 643 }
1098 if (tempname2) { 644 }
1099 unlink(tempname2); 645 idx = -1;
1100 free(tempname2); 646 anychange = true;
1101 } 647 } while (i <= nlen[0]);
1102 return rval; 648
649 free(vec);
650 free(ix[0]);
651 free(ix[1]);
652 free(J);
653 return anychange;
1103} 654}
1104 655
1105 656static int diffreg(char *file[2])
1106#if ENABLE_FEATURE_DIFF_DIR
1107static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1108{ 657{
1109 int flags = 0; /*D_HEADER;*/ 658 FILE_and_pos_t ft[2];
1110 int val; 659 bool binary = false, differ = false;
1111 char *fullpath1 = NULL; /* if -N */ 660 int status = STATUS_SAME;
1112 char *fullpath2 = NULL; 661
1113 662 for (int i = 0; i < 2; i++) {
1114 if (path1) 663 int fd = open_or_warn_stdin(file[i]);
1115 fullpath1 = concat_path_file(dir1, path1); 664 if (fd == -1)
1116 if (path2) 665 xfunc_die();
1117 fullpath2 = concat_path_file(dir2, path2); 666 /* Our diff implementation is using seek.
1118 667 * When we meet non-seekable file, we must make a temp copy.
1119 if (!fullpath1 || stat(fullpath1, &stb1) != 0) { 668 */
1120 flags |= D_EMPTY1; 669 ft[i].ft_pos = 0;
1121 memset(&stb1, 0, sizeof(stb1)); 670 if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
1122 if (path2) { 671 char name[] = "/tmp/difXXXXXX";
1123 free(fullpath1); 672 int fd_tmp = mkstemp(name);
1124 fullpath1 = concat_path_file(dir1, path2); 673 if (fd_tmp < 0)
674 bb_perror_msg_and_die("mkstemp");
675 unlink(name);
676 ft[i].ft_pos = bb_copyfd_eof(fd, fd_tmp);
677 /* error message is printed by bb_copyfd_eof */
678 if (ft[i].ft_pos < 0)
679 xfunc_die();
680 fstat(fd, &stb[i]);
681 if (fd) /* Prevents closing of stdin */
682 close(fd);
683 fd = fd_tmp;
1125 } 684 }
685 ft[i].ft_fp = fdopen(fd, "r");
1126 } 686 }
1127 if (!fullpath2 || stat(fullpath2, &stb2) != 0) { 687
1128 flags |= D_EMPTY2; 688 while (1) {
1129 memset(&stb2, 0, sizeof(stb2)); 689 const size_t sz = COMMON_BUFSIZE / 2;
1130 stb2.st_mode = stb1.st_mode; 690 char *const buf0 = bb_common_bufsiz1;
1131 if (path1) { 691 char *const buf1 = buf0 + sz;
1132 free(fullpath2); 692 int i, j;
1133 fullpath2 = concat_path_file(dir2, path1); 693 i = fread(buf0, 1, sz, ft[0].ft_fp);
694 ft[0].ft_pos += i;
695 j = fread(buf1, 1, sz, ft[1].ft_fp);
696 ft[1].ft_pos += j;
697 if (i != j) {
698 differ = true;
699 i = MIN(i, j);
700 }
701 if (i == 0)
702 break;
703 for (int k = 0; k < i; k++) {
704 if (!buf0[k] || !buf1[k])
705 binary = true;
706 if (buf0[k] != buf1[k])
707 differ = true;
1134 } 708 }
1135 } 709 }
1136 710 if (differ) {
1137 if (stb1.st_mode == 0) 711 if (binary && !(option_mask32 & FLAG(a)))
1138 stb1.st_mode = stb2.st_mode; 712 status = STATUS_BINARY;
1139 713 else if (diff(ft, file))
1140 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 714 status = STATUS_DIFFER;
1141 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1142 goto ret;
1143 } 715 }
716 if (status != STATUS_SAME)
717 exit_status |= 1;
1144 718
1145 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode)) 719 fclose_if_not_stdin(ft[0].ft_fp);
1146 val = D_SKIPPED1; 720 fclose_if_not_stdin(ft[1].ft_fp);
1147 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1148 val = D_SKIPPED2;
1149 else {
1150 /* Both files are either REGular or DIRectories */
1151 val = diffreg(fullpath1, fullpath2, flags);
1152 }
1153 721
1154 print_status(val, fullpath1, fullpath2 /*, NULL*/); 722 return status;
1155 ret:
1156 free(fullpath1);
1157 free(fullpath2);
1158} 723}
1159#endif
1160 724
725static void print_status(int status, char *path[2])
726{
727 switch (status) {
728 case STATUS_BINARY:
729 case STATUS_DIFFER:
730 if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
731 printf("Files %s and %s differ\n", path[0], path[1]);
732 break;
733 case STATUS_SAME:
734 if (option_mask32 & FLAG(s))
735 printf("Files %s and %s are identical\n", path[0], path[1]);
736 break;
737 }
738}
1161 739
1162#if ENABLE_FEATURE_DIFF_DIR 740#if ENABLE_FEATURE_DIFF_DIR
741struct dlist {
742 size_t len;
743 int s, e;
744 char **dl;
745};
746
1163/* This function adds a filename to dl, the directory listing. */ 747/* This function adds a filename to dl, the directory listing. */
1164static int FAST_FUNC add_to_dirlist(const char *filename, 748static int FAST_FUNC add_to_dirlist(const char *filename,
1165 struct stat *sb UNUSED_PARAM, 749 struct stat *sb UNUSED_PARAM,
1166 void *userdata, 750 void *userdata, int depth UNUSED_PARAM)
1167 int depth UNUSED_PARAM)
1168{ 751{
1169 dl = xrealloc_vector(dl, 5, dl_count); 752 struct dlist *const l = userdata;
1170 dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata); 753 l->dl = xrealloc_vector(l->dl, 6, l->e);
1171 dl_count++; 754 /* + 1 skips "/" after dirname */
755 l->dl[l->e] = xstrdup(filename + l->len + 1);
756 l->e++;
1172 return TRUE; 757 return TRUE;
1173} 758}
1174 759
1175 760/* If recursion is not set, this function adds the directory
1176/* This returns a sorted directory listing. */ 761 * to the list and prevents recursive_action from recursing into it.
1177static char **get_recursive_dirlist(char *path) 762 */
763static int FAST_FUNC skip_dir(const char *filename,
764 struct stat *sb, void *userdata,
765 int depth)
1178{ 766{
1179 dl_count = 0; 767 if (!(option_mask32 & FLAG(r)) && depth) {
1180 dl = xzalloc(sizeof(dl[0])); 768 add_to_dirlist(filename, sb, userdata, depth);
1181 769 return SKIP;
1182 /* We need to trim root directory prefix.
1183 * Using void *userdata to specify its length,
1184 * add_to_dirlist will remove it. */
1185 if (option_mask32 & FLAG_r) {
1186 recursive_action(path, ACTION_RECURSE|ACTION_FOLLOWLINKS,
1187 add_to_dirlist, /* file_action */
1188 NULL, /* dir_action */
1189 (void*)(ptrdiff_t)(strlen(path) + 1),
1190 0);
1191 } else {
1192 DIR *dp;
1193 struct dirent *ep;
1194
1195 dp = warn_opendir(path);
1196 while ((ep = readdir(dp))) {
1197 if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
1198 continue;
1199 add_to_dirlist(ep->d_name, NULL, (void*)(int)0, 0);
1200 }
1201 closedir(dp);
1202 } 770 }
1203 771 return TRUE;
1204 /* Sort dl alphabetically. */
1205 qsort_string_vector(dl, dl_count);
1206
1207 dl[dl_count] = NULL;
1208 return dl;
1209} 772}
1210 773
1211 774static void diffdir(char *p[2], const char *s_start)
1212static void diffdir(char *p1, char *p2)
1213{ 775{
1214 char **dirlist1, **dirlist2; 776 struct dlist list[2];
1215 char *dp1, *dp2; 777
1216 int pos; 778 memset(&list, 0, sizeof(list));
1217 779 for (int i = 0; i < 2; i++) {
1218 /* Check for trailing slashes. */ 780 /*list[i].s = list[i].e = 0; - memset did it */
1219 dp1 = last_char_is(p1, '/'); 781 /*list[i].dl = NULL; */
1220 if (dp1 != NULL) 782
1221 *dp1 = '\0'; 783 /* We need to trim root directory prefix.
1222 dp2 = last_char_is(p2, '/'); 784 * Using list.len to specify its length,
1223 if (dp2 != NULL) 785 * add_to_dirlist will remove it. */
1224 *dp2 = '\0'; 786 list[i].len = strlen(p[i]);
1225 787 recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
1226 /* Get directory listings for p1 and p2. */ 788 add_to_dirlist, skip_dir, &list[i], 0);
1227 dirlist1 = get_recursive_dirlist(p1); 789 /* Sort dl alphabetically.
1228 dirlist2 = get_recursive_dirlist(p2); 790 * GNU diff does this ignoring any number of trailing dots.
1229 791 * We don't, so for us dotted files almost always are
1230 /* If -S was set, find the starting point. */ 792 * first on the list.
1231 if (opt_S_start) { 793 */
1232 while (*dirlist1 != NULL && strcmp(*dirlist1, opt_S_start) < 0) 794 qsort_string_vector(list[i].dl, list[i].e);
1233 dirlist1++; 795 /* If -S was set, find the starting point. */
1234 while (*dirlist2 != NULL && strcmp(*dirlist2, opt_S_start) < 0) 796 if (!s_start)
1235 dirlist2++; 797 continue;
1236 if ((*dirlist1 == NULL) || (*dirlist2 == NULL)) 798 while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
1237 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S"); 799 list[i].s++;
1238 } 800 }
1239
1240 /* Now that both dirlist1 and dirlist2 contain sorted directory 801 /* Now that both dirlist1 and dirlist2 contain sorted directory
1241 * listings, we can start to go through dirlist1. If both listings 802 * listings, we can start to go through dirlist1. If both listings
1242 * contain the same file, then do a normal diff. Otherwise, behaviour 803 * contain the same file, then do a normal diff. Otherwise, behaviour
1243 * is determined by whether the -N flag is set. */ 804 * is determined by whether the -N flag is set. */
1244 while (*dirlist1 != NULL || *dirlist2 != NULL) { 805 while (1) {
1245 dp1 = *dirlist1; 806 char *dp[2];
1246 dp2 = *dirlist2; 807 int pos;
1247 pos = dp1 == NULL ? 1 : (dp2 == NULL ? -1 : strcmp(dp1, dp2)); 808 int k;
809
810 dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
811 dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
812 if (!dp[0] && !dp[1])
813 break;
814 pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
815 k = pos > 0;
816 if (pos && !(option_mask32 & FLAG(N)))
817 printf("Only in %s: %s\n", p[k], dp[k]);
818 else {
819 char *fullpath[2], *path[2]; /* if -N */
820
821 for (int i = 0; i < 2; i++) {
822 if (pos == 0 || i == k) {
823 path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
824 stat(fullpath[i], &stb[i]);
825 } else {
826 fullpath[i] = concat_path_file(p[i], dp[1 - i]);
827 path[i] = (char *)bb_dev_null;
828 }
829 }
830 if (pos)
831 stat(fullpath[k], &stb[1 - k]);
832
833 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
834 printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
835 else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
836 printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
837 else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
838 printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
839 else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
840 if (S_ISDIR(stb[0].st_mode))
841 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
842 else
843 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
844 } else
845 print_status(diffreg(path), fullpath);
846
847 free(fullpath[0]);
848 free(fullpath[1]);
849 }
850 free(dp[k]);
851 list[k].s++;
1248 if (pos == 0) { 852 if (pos == 0) {
1249 do_diff(p1, dp1, p2, dp2); 853 free(dp[1 - k]);
1250 dirlist1++; 854 list[1 - k].s++;
1251 dirlist2++;
1252 } else if (pos < 0) {
1253 if (option_mask32 & FLAG_N)
1254 do_diff(p1, dp1, p2, NULL);
1255 else
1256 print_only(p1, dp1);
1257 dirlist1++;
1258 } else {
1259 if (option_mask32 & FLAG_N)
1260 do_diff(p1, NULL, p2, dp2);
1261 else
1262 print_only(p2, dp2);
1263 dirlist2++;
1264 } 855 }
1265 } 856 }
857 if (ENABLE_FEATURE_CLEAN_UP) {
858 free(list[0].dl);
859 free(list[1].dl);
860 }
1266} 861}
1267#endif 862#endif
1268 863
1269
1270int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 864int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1271int diff_main(int argc UNUSED_PARAM, char **argv) 865int diff_main(int argc UNUSED_PARAM, char **argv)
1272{ 866{
1273 int gotstdin = 0; 867 int gotstdin = 0;
1274 char *f1, *f2; 868 char *file[2], *s_start = NULL;
1275 llist_t *L_arg = NULL; 869 llist_t *L_arg = NULL;
1276 870
1277 INIT_G(); 871 INIT_G();
@@ -1280,65 +874,49 @@ int diff_main(int argc UNUSED_PARAM, char **argv)
1280 opt_complementary = "=2:L::U+"; 874 opt_complementary = "=2:L::U+";
1281 getopt32(argv, "abdiL:NqrsS:tTU:wu" 875 getopt32(argv, "abdiL:NqrsS:tTU:wu"
1282 "p" /* ignored (for compatibility) */, 876 "p" /* ignored (for compatibility) */,
1283 &L_arg, &opt_S_start, &opt_U_context); 877 &L_arg, &s_start, &opt_U_context);
1284 /*argc -= optind;*/
1285 argv += optind; 878 argv += optind;
1286 while (L_arg) { 879 while (L_arg) {
1287 if (label1 && label2) 880 if (label[0] && label[1])
1288 bb_show_usage(); 881 bb_show_usage();
1289 if (label1) /* then label2 is NULL */ 882 if (label[0]) /* then label[1] is NULL */
1290 label2 = label1; 883 label[1] = label[0];
1291 label1 = llist_pop(&L_arg); 884 label[0] = llist_pop(&L_arg);
1292 } 885 }
1293
1294 /*
1295 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1296 * driver routine. Both drivers use the contents of stb1 and stb2.
1297 */
1298 f1 = argv[0];
1299 f2 = argv[1];
1300 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
1301 xfunc_error_retval = 2; 886 xfunc_error_retval = 2;
1302 if (LONE_DASH(f1)) { 887 for (int i = 0; i < 2; i++) {
1303 fstat(STDIN_FILENO, &stb1); 888 file[i] = argv[i];
1304 gotstdin++; 889 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
1305 } else 890 if (LONE_DASH(file[i])) {
1306 xstat(f1, &stb1); 891 fstat(STDIN_FILENO, &stb[i]);
1307 if (LONE_DASH(f2)) { 892 gotstdin++;
1308 fstat(STDIN_FILENO, &stb2); 893 } else
1309 gotstdin++; 894 xstat(file[i], &stb[i]);
1310 } else 895 }
1311 xstat(f2, &stb2);
1312 xfunc_error_retval = 1; 896 xfunc_error_retval = 1;
1313 897 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
1314 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1315 bb_error_msg_and_die("can't compare stdin to a directory"); 898 bb_error_msg_and_die("can't compare stdin to a directory");
1316 899
1317 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 900 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
1318#if ENABLE_FEATURE_DIFF_DIR 901#if ENABLE_FEATURE_DIFF_DIR
1319 diffdir(f1, f2); 902 diffdir(file, s_start);
1320 return exit_status;
1321#else 903#else
1322 bb_error_msg_and_die("no support for directory comparison"); 904 bb_error_msg_and_die("no support for directory comparison");
1323#endif 905#endif
1324 } 906 } else {
907 bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
908 bool dir = S_ISDIR(stb[1].st_mode);
909 if (dirfile) {
910 const char *slash = strrchr(file[!dir], '/');
911 file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
912 xstat(file[dir], &stb[dir]);
913 }
914 /* diffreg can get non-regular files here */
915 print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);
1325 916
1326 if (S_ISDIR(stb1.st_mode)) { /* "diff dir file" */ 917 if (dirfile)
1327 /* NB: "diff dir dir2/dir3/file" must become 918 free(file[dir]);
1328 * "diff dir/file dir2/dir3/file" */
1329 char *slash = strrchr(f2, '/');
1330 f1 = concat_path_file(f1, slash ? slash + 1 : f2);
1331 xstat(f1, &stb1);
1332 }
1333 if (S_ISDIR(stb2.st_mode)) {
1334 char *slash = strrchr(f1, '/');
1335 f2 = concat_path_file(f2, slash ? slash + 1 : f1);
1336 xstat(f2, &stb2);
1337 } 919 }
1338 920
1339 /* diffreg can get non-regular files here,
1340 * they are not both DIRestories */
1341 print_status((gotstdin > 1 ? D_SAME : diffreg(f1, f2, 0)),
1342 f1, f2 /*, NULL*/);
1343 return exit_status; 921 return exit_status;
1344} 922}
diff --git a/testsuite/diff.tests b/testsuite/diff.tests
index 06bf163d0..f7bde2f5b 100755
--- a/testsuite/diff.tests
+++ b/testsuite/diff.tests
@@ -44,21 +44,18 @@ testing "diff of stdin, twice" \
44 "" \ 44 "" \
45 "stdin" 45 "stdin"
46 46
47test x"$SKIP_KNOWN_BUGS" = x"" && \
48testing "diff -b treats EOF as whitespace" \ 47testing "diff -b treats EOF as whitespace" \
49 'diff -ub - input; echo $?' \ 48 'diff -ub - input; echo $?' \
50 "0\n" \ 49 "0\n" \
51 "abc" \ 50 "abc" \
52 "abc " 51 "abc "
53 52
54test x"$SKIP_KNOWN_BUGS" = x"" && \
55testing "diff -b treats all spaces as equal" \ 53testing "diff -b treats all spaces as equal" \
56 'diff -ub - input; echo $?' \ 54 'diff -ub - input; echo $?' \
57 "0\n" \ 55 "0\n" \
58 "a \t c\n" \ 56 "a \t c\n" \
59 "a\t \tc\n" 57 "a\t \tc\n"
60 58
61test x"$SKIP_KNOWN_BUGS" = x"" && \
62testing "diff always takes context from old file" \ 59testing "diff always takes context from old file" \
63 "diff -ub - input | $TRIM_TAB" \ 60 "diff -ub - input | $TRIM_TAB" \
64"\ 61"\