blob: 368efd38312a0957f4cc834f60d6c149f3c57eaf [file] [log] [blame]
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini diff implementation for busybox, adapted from OpenBSD diff.
4 *
5 * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com>
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00006 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
7 *
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00008 * 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.
Bernhard Reutner-Fischer14aa06f2006-05-19 13:02:27 +000011 *
Rob Landleye4386342006-04-18 20:41:51 +000012 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000013 */
14
15#include <time.h>
16#include <sys/types.h>
17#include <sys/param.h>
18#include <sys/stat.h>
19#include <ctype.h>
20#include <errno.h>
21#include <signal.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <stdarg.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/wait.h>
28#include <fcntl.h>
29#include <stddef.h>
30#include <paths.h>
31#include <dirent.h>
32#include "busybox.h"
33
34#define FSIZE_MAX 32768
35
36/*
37 * Output flags
38 */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +000039#define D_HEADER 1 /* Print a header/footer between files */
40#define D_EMPTY1 2 /* Treat first file as empty (/dev/null) */
41#define D_EMPTY2 4 /* Treat second file as empty (/dev/null) */
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000042
43/*
44 * Status values for print_status() and diffreg() return values
45 * Guide:
46 * D_SAME - files are the same
47 * D_DIFFER - files differ
48 * D_BINARY - binary files differ
49 * D_COMMON - subdirectory common to both dirs
50 * D_ONLY - file only exists in one dir
51 * D_MISMATCH1 - path1 a dir, path2 a file
52 * D_MISMATCH2 - path1 a file, path2 a dir
53 * D_ERROR - error occurred
54 * D_SKIPPED1 - skipped path1 as it is a special file
55 * D_SKIPPED2 - skipped path2 as it is a special file
56 */
57
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +000058#define D_SAME 0
59#define D_DIFFER (1<<0)
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000060#define D_BINARY (1<<1)
61#define D_COMMON (1<<2)
62#define D_ONLY (1<<3)
63#define D_MISMATCH1 (1<<4)
64#define D_MISMATCH2 (1<<5)
65#define D_ERROR (1<<6)
66#define D_SKIPPED1 (1<<7)
67#define D_SKIPPED2 (1<<8)
68
69/* Command line options */
70static unsigned long cmd_flags;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +000071
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +000072#define FLAG_a (1<<0)
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000073#define FLAG_b (1<<1)
74#define FLAG_d (1<<2)
75#define FLAG_i (1<<3)
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +000076#define FLAG_L (1<<4)
77#define FLAG_N (1<<5)
78#define FLAG_q (1<<6)
79#define FLAG_r (1<<7)
80#define FLAG_s (1<<8)
81#define FLAG_S (1<<9)
82#define FLAG_t (1<<10)
83#define FLAG_T (1<<11)
84#define FLAG_U (1<<12)
85#define FLAG_w (1<<13)
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000086
87int context, status;
88char *start, *label[2];
89struct stat stb1, stb2;
90char **dl;
91int dl_count = 0;
92
93struct cand {
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +000094 int x;
95 int y;
96 int pred;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +000097};
98
99struct line {
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000100 int serial;
101 int value;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000102} *file[2];
103
104/*
105 * The following struct is used to record change information
106 * doing a "context" or "unified" diff. (see routine "change" to
107 * understand the highly mnemonic field names)
108 */
109struct context_vec {
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000110 int a; /* start line in old file */
111 int b; /* end line in old file */
112 int c; /* start line in new file */
113 int d; /* end line in new file */
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000114};
115
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000116static int *J; /* will be overlaid on class */
117static int *class; /* will be overlaid on file[0] */
118static int *klist; /* will be overlaid on file[0] after class */
119static int *member; /* will be overlaid on file[1] */
120static int clen;
121static int len[2];
122static int pref, suff; /* length of prefix and suffix */
123static int slen[2];
124static int anychange;
125static long *ixnew; /* will be overlaid on file[1] */
126static long *ixold; /* will be overlaid on klist */
127static struct cand *clist; /* merely a free storage pot for candidates */
128static int clistlen; /* the length of clist */
129static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000130static struct context_vec *context_vec_start;
131static struct context_vec *context_vec_end;
132static struct context_vec *context_vec_ptr;
133
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000134static void print_only(const char *path, size_t dirlen, const char *entry)
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000135{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000136 if (dirlen > 1)
137 dirlen--;
138 printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000139}
140
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000141static void print_status(int val, char *path1, char *path2, char *entry)
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000142{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000143 const char *const _entry = entry ? entry : "";
Bernhard Reutner-Fischer5fb0fec2006-04-06 11:28:19 +0000144 char *_path1 = entry ? concat_path_file(path1, _entry) : path1;
145 char *_path2 = entry ? concat_path_file(path2, _entry) : path2;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000146
147 switch (val) {
148 case D_ONLY:
149 print_only(path1, strlen(path1), entry);
150 break;
151 case D_COMMON:
152 printf("Common subdirectories: %s and %s\n", _path1, _path2);
153 break;
154 case D_BINARY:
155 printf("Binary files %s and %s differ\n", _path1, _path2);
156 break;
157 case D_DIFFER:
158 if (cmd_flags & FLAG_q)
159 printf("Files %s and %s differ\n", _path1, _path2);
160 break;
161 case D_SAME:
162 if (cmd_flags & FLAG_s)
163 printf("Files %s and %s are identical\n", _path1, _path2);
164 break;
165 case D_MISMATCH1:
166 printf("File %s is a directory while file %s is a regular file\n",
167 _path1, _path2);
168 break;
169 case D_MISMATCH2:
170 printf("File %s is a regular file while file %s is a directory\n",
171 _path1, _path2);
172 break;
173 case D_SKIPPED1:
174 printf("File %s is not a regular file or directory and was skipped\n",
175 _path1);
176 break;
177 case D_SKIPPED2:
178 printf("File %s is not a regular file or directory and was skipped\n",
179 _path2);
180 break;
181 }
182 if (entry) {
183 free(_path1);
184 free(_path2);
185 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000186}
187
188/*
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000189 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
190 */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000191static int readhash(FILE * f)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000192{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000193 int i, t, space;
194 int sum;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000195
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000196 sum = 1;
197 space = 0;
198 if (!(cmd_flags & FLAG_b) && !(cmd_flags & FLAG_w)) {
199 if (FLAG_i)
200 for (i = 0; (t = getc(f)) != '\n'; i++) {
201 if (t == EOF) {
202 if (i == 0)
203 return (0);
204 break;
205 }
206 sum = sum * 127 + t;
207 } else
208 for (i = 0; (t = getc(f)) != '\n'; i++) {
209 if (t == EOF) {
210 if (i == 0)
211 return (0);
212 break;
213 }
214 sum = sum * 127 + t;
215 }
216 } else {
217 for (i = 0;;) {
218 switch (t = getc(f)) {
219 case '\t':
220 case '\r':
221 case '\v':
222 case '\f':
223 case ' ':
224 space++;
225 continue;
226 default:
227 if (space && !(cmd_flags & FLAG_w)) {
228 i++;
229 space = 0;
230 }
231 sum = sum * 127 + t;
232 i++;
233 continue;
234 case EOF:
235 if (i == 0)
236 return (0);
237 /* FALLTHROUGH */
238 case '\n':
239 break;
240 }
241 break;
242 }
243 }
244 /*
245 * There is a remote possibility that we end up with a zero sum.
246 * Zero is used as an EOF marker, so return 1 instead.
247 */
248 return (sum == 0 ? 1 : sum);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000249}
250
251
252
253/*
254 * Check to see if the given files differ.
255 * Returns 0 if they are the same, 1 if different, and -1 on error.
256 */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000257static int files_differ(FILE * f1, FILE * f2, int flags)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000258{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000259 char buf1[BUFSIZ], buf2[BUFSIZ];
260 size_t i, j;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000261
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000262 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size ||
263 (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
264 return (1);
265 while (1) {
266 i = fread(buf1, 1, sizeof(buf1), f1);
267 j = fread(buf2, 1, sizeof(buf2), f2);
268 if (i != j)
269 return (1);
270 if (i == 0 && j == 0) {
271 if (ferror(f1) || ferror(f2))
272 return (1);
273 return (0);
274 }
275 if (memcmp(buf1, buf2, i) != 0)
276 return (1);
277 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000278}
279
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000280static void prepare(int i, FILE * fd, off_t filesize)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000281{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000282 struct line *p;
283 int h;
284 size_t j, sz;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000285
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000286 rewind(fd);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000287
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000288 sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;
289 if (sz < 100)
290 sz = 100;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000291
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000292 p = xmalloc((sz + 3) * sizeof(struct line));
293 for (j = 0; (h = readhash(fd));) {
294 if (j == sz) {
295 sz = sz * 3 / 2;
296 p = xrealloc(p, (sz + 3) * sizeof(struct line));
297 }
298 p[++j].value = h;
299 }
300 len[i] = j;
301 file[i] = p;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000302}
303
304static void prune(void)
305{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000306 int i, j;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000307
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000308 for (pref = 0; pref < len[0] && pref < len[1] &&
309 file[0][pref + 1].value == file[1][pref + 1].value; pref++);
310 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
311 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
312 suff++);
313 for (j = 0; j < 2; j++) {
314 sfile[j] = file[j] + pref;
315 slen[j] = len[j] - pref - suff;
316 for (i = 0; i <= slen[j]; i++)
317 sfile[j][i].serial = i;
318 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000319}
320
321static void equiv(struct line *a, int n, struct line *b, int m, int *c)
322{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000323 int i, j;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000324
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000325 i = j = 1;
326 while (i <= n && j <= m) {
327 if (a[i].value < b[j].value)
328 a[i++].value = 0;
329 else if (a[i].value == b[j].value)
330 a[i++].value = j;
331 else
332 j++;
333 }
334 while (i <= n)
335 a[i++].value = 0;
336 b[m + 1].value = 0;
337 j = 0;
338 while (++j <= m) {
339 c[j] = -b[j].serial;
340 while (b[j + 1].value == b[j].value) {
341 j++;
342 c[j] = b[j].serial;
343 }
344 }
345 c[j] = -1;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000346}
347
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000348static int isqrt(int n)
349{
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000350 int y, x = 1;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000351
352 if (n == 0)
353 return (0);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000354
355 do {
356 y = x;
357 x = n / x;
358 x += y;
359 x /= 2;
360 } while ((x - y) > 1 || (x - y) < -1);
361
362 return (x);
363}
364
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000365static int newcand(int x, int y, int pred)
366{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000367 struct cand *q;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000368
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000369 if (clen == clistlen) {
370 clistlen = clistlen * 11 / 10;
371 clist = xrealloc(clist, clistlen * sizeof(struct cand));
372 }
373 q = clist + clen;
374 q->x = x;
375 q->y = y;
376 q->pred = pred;
377 return (clen++);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000378}
379
380
381static int search(int *c, int k, int y)
382{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000383 int i, j, l, t;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000384
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000385 if (clist[c[k]].y < y) /* quick look for typical case */
386 return (k + 1);
387 i = 0;
388 j = k + 1;
389 while (1) {
390 l = i + j;
391 if ((l >>= 1) <= i)
392 break;
393 t = clist[c[l]].y;
394 if (t > y)
395 j = l;
396 else if (t < y)
397 i = l;
398 else
399 return (l);
400 }
401 return (l + 1);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000402}
403
404
405static int stone(int *a, int n, int *b, int *c)
406{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000407 int i, k, y, j, l;
408 int oldc, tc, oldl;
409 unsigned int numtries;
410
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000411#if ENABLE_FEATURE_DIFF_MINIMAL
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000412 const unsigned int bound =
413 (cmd_flags & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000414#else
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000415 const unsigned int bound = MAX(256, isqrt(n));
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000416#endif
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000417 k = 0;
418 c[0] = newcand(0, 0, 0);
419 for (i = 1; i <= n; i++) {
420 j = a[i];
421 if (j == 0)
422 continue;
423 y = -b[j];
424 oldl = 0;
425 oldc = c[0];
426 numtries = 0;
427 do {
428 if (y <= clist[oldc].y)
429 continue;
430 l = search(c, k, y);
431 if (l != oldl + 1)
432 oldc = c[l - 1];
433 if (l <= k) {
434 if (clist[c[l]].y <= y)
435 continue;
436 tc = c[l];
437 c[l] = newcand(i, y, oldc);
438 oldc = tc;
439 oldl = l;
440 numtries++;
441 } else {
442 c[l] = newcand(i, y, oldc);
443 k++;
444 break;
445 }
446 } while ((y = b[++j]) > 0 && numtries < bound);
447 }
448 return (k);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000449}
450
451static void unravel(int p)
452{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000453 struct cand *q;
454 int i;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000455
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000456 for (i = 0; i <= len[0]; i++)
457 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0;
458 for (q = clist + p; q->y != 0; q = clist + q->pred)
459 J[q->x + pref] = q->y + pref;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000460}
461
462
463static void unsort(struct line *f, int l, int *b)
464{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000465 int *a, i;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000466
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000467 a = xmalloc((l + 1) * sizeof(int));
468 for (i = 1; i <= l; i++)
469 a[f[i].serial] = f[i].value;
470 for (i = 1; i <= l; i++)
471 b[i] = a[i];
472 free(a);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000473}
474
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000475static int skipline(FILE * f)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000476{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000477 int i, c;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000478
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000479 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
480 continue;
481 return (i);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000482}
483
484
485/*
486 * Check does double duty:
487 * 1. ferret out any fortuitous correspondences due
488 * to confounding by hashing (which result in "jackpot")
489 * 2. collect random access indexes to the two files
490 */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000491static void check(FILE * f1, FILE * f2)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000492{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000493 int i, j, jackpot, c, d;
494 long ctold, ctnew;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000495
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000496 rewind(f1);
497 rewind(f2);
498 j = 1;
499 ixold[0] = ixnew[0] = 0;
500 jackpot = 0;
501 ctold = ctnew = 0;
502 for (i = 1; i <= len[0]; i++) {
503 if (J[i] == 0) {
504 ixold[i] = ctold += skipline(f1);
505 continue;
506 }
507 while (j < J[i]) {
508 ixnew[j] = ctnew += skipline(f2);
509 j++;
510 }
511 if ((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w)
512 || (cmd_flags & FLAG_i)) {
513 while (1) {
514 c = getc(f1);
515 d = getc(f2);
516 /*
517 * GNU diff ignores a missing newline
518 * in one file if bflag || wflag.
519 */
520 if (((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w)) &&
521 ((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
522 break;
523 }
524 ctold++;
525 ctnew++;
526 if ((cmd_flags & FLAG_b) && isspace(c) && isspace(d)) {
527 do {
528 if (c == '\n')
529 break;
530 ctold++;
531 } while (isspace(c = getc(f1)));
532 do {
533 if (d == '\n')
534 break;
535 ctnew++;
536 } while (isspace(d = getc(f2)));
537 } else if (cmd_flags & FLAG_w) {
538 while (isspace(c) && c != '\n') {
539 c = getc(f1);
540 ctold++;
541 }
542 while (isspace(d) && d != '\n') {
543 d = getc(f2);
544 ctnew++;
545 }
546 }
547 if (c != d) {
548 jackpot++;
549 J[i] = 0;
550 if (c != '\n' && c != EOF)
551 ctold += skipline(f1);
552 if (d != '\n' && c != EOF)
553 ctnew += skipline(f2);
554 break;
555 }
556 if (c == '\n' || c == EOF)
557 break;
558 }
559 } else {
560 while (1) {
561 ctold++;
562 ctnew++;
563 if ((c = getc(f1)) != (d = getc(f2))) {
564 J[i] = 0;
565 if (c != '\n' && c != EOF)
566 ctold += skipline(f1);
567 if (d != '\n' && c != EOF)
568 ctnew += skipline(f2);
569 break;
570 }
571 if (c == '\n' || c == EOF)
572 break;
573 }
574 }
575 ixold[i] = ctold;
576 ixnew[j] = ctnew;
577 j++;
578 }
579 for (; j <= len[1]; j++)
580 ixnew[j] = ctnew += skipline(f2);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000581}
582
583/* shellsort CACM #201 */
584static void sort(struct line *a, int n)
585{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000586 struct line *ai, *aim, w;
587 int j, m = 0, k;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000588
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000589 if (n == 0)
590 return;
591 for (j = 1; j <= n; j *= 2)
592 m = 2 * j - 1;
593 for (m /= 2; m != 0; m /= 2) {
594 k = n - m;
595 for (j = 1; j <= k; j++) {
596 for (ai = &a[j]; ai > a; ai -= m) {
597 aim = &ai[m];
598 if (aim < ai)
599 break; /* wraparound */
600 if (aim->value > ai[0].value ||
601 (aim->value == ai[0].value && aim->serial > ai[0].serial))
602 break;
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;
609 }
610 }
611 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000612}
613
614
615static void uni_range(int a, int b)
616{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000617 if (a < b)
618 printf("%d,%d", a, b - a + 1);
619 else if (a == b)
620 printf("%d", b);
621 else
622 printf("%d,0", b);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000623}
624
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000625static int fetch(long *f, int a, int b, FILE * lb, int ch)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000626{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000627 int i, j, c, lastc, col, nc;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000628
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000629 if (a > b)
630 return (0);
631 for (i = a; i <= b; i++) {
632 fseek(lb, f[i - 1], SEEK_SET);
633 nc = f[i] - f[i - 1];
634 if (ch != '\0') {
635 putchar(ch);
636 if (cmd_flags & FLAG_T)
637 putchar('\t');
638 }
639 col = 0;
640 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
641 if ((c = getc(lb)) == EOF) {
642 puts("\n\\ No newline at end of file");
643 return (0);
644 }
645 if (c == '\t' && (cmd_flags & FLAG_t)) {
646 do {
647 putchar(' ');
648 } while (++col & 7);
649 } else {
650 putchar(c);
651 col++;
652 }
653 }
654 }
655 return (0);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000656}
657
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000658static int asciifile(FILE * f)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000659{
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000660#if ENABLE_FEATURE_DIFF_BINARY
Bernhard Reutner-Fischer5fb0fec2006-04-06 11:28:19 +0000661 unsigned char buf[BUFSIZ];
662 int i, cnt;
663#endif
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000664
665 if ((cmd_flags & FLAG_a) || f == NULL)
666 return (1);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000667
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000668#if ENABLE_FEATURE_DIFF_BINARY
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000669 rewind(f);
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000670 cnt = fread(buf, 1, sizeof(buf), f);
671 for (i = 0; i < cnt; i++) {
672 if (!isprint(buf[i]) && !isspace(buf[i])) {
673 return (0);
674 }
675 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000676#endif
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000677 return (1);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000678}
679
680/* dump accumulated "unified" diff changes */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000681static void dump_unified_vec(FILE * f1, FILE * f2)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000682{
683 struct context_vec *cvp = context_vec_start;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000684 int lowa, upb, lowc, upd;
685 int a, b, c, d;
686 char ch;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000687
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000688 if (context_vec_start > context_vec_ptr)
689 return;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000690
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000691 b = d = 0; /* gcc */
692 lowa = MAX(1, cvp->a - context);
693 upb = MIN(len[0], context_vec_ptr->b + context);
694 lowc = MAX(1, cvp->c - context);
695 upd = MIN(len[1], context_vec_ptr->d + context);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000696
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000697 fputs("@@ -", stdout);
698 uni_range(lowa, upb);
699 fputs(" +", stdout);
700 uni_range(lowc, upd);
701 fputs(" @@", stdout);
702 putchar('\n');
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000703
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000704 /*
705 * Output changes in "unified" diff format--the old and new lines
706 * are printed together.
707 */
708 for (; cvp <= context_vec_ptr; cvp++) {
709 a = cvp->a;
710 b = cvp->b;
711 c = cvp->c;
712 d = cvp->d;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000713
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000714 /*
715 * c: both new and old changes
716 * d: only changes in the old file
717 * a: only changes in the new file
718 */
719 if (a <= b && c <= d)
720 ch = 'c';
721 else
722 ch = (a <= b) ? 'd' : 'a';
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000723 if (ch == 'c' || ch == 'd') {
724 fetch(ixold, lowa, a - 1, f1, ' ');
725 fetch(ixold, a, b, f1, '-');
726 }
727 if (ch == 'a')
728 fetch(ixnew, lowc, c - 1, f2, ' ');
729 if (ch == 'c' || ch == 'a')
730 fetch(ixnew, c, d, f2, '+');
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000731 lowa = b + 1;
732 lowc = d + 1;
733 }
734 fetch(ixnew, d + 1, upd, f2, ' ');
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000735
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000736 context_vec_ptr = context_vec_start - 1;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000737}
738
739
740static void print_header(const char *file1, const char *file2)
741{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000742 if (label[0] != NULL)
743 printf("%s %s\n", "---", label[0]);
744 else
745 printf("%s %s\t%s", "---", file1, ctime(&stb1.st_mtime));
746 if (label[1] != NULL)
747 printf("%s %s\n", "+++", label[1]);
748 else
749 printf("%s %s\t%s", "+++", file2, ctime(&stb2.st_mtime));
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000750}
751
752
753
754/*
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 then 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.
760 */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000761static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
762 int b, int c, int d)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000763{
764 static size_t max_context = 64;
765
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000766 if (a > b && c > d)
767 return;
768 if (cmd_flags & FLAG_q)
769 return;
770
771 /*
772 * Allocate change records as needed.
773 */
774 if (context_vec_ptr == context_vec_end - 1) {
775 ptrdiff_t offset = context_vec_ptr - context_vec_start;
776
777 max_context <<= 1;
778 context_vec_start = xrealloc(context_vec_start,
779 max_context *
780 sizeof(struct context_vec));
781 context_vec_end = context_vec_start + max_context;
782 context_vec_ptr = context_vec_start + offset;
783 }
784 if (anychange == 0) {
785 /*
786 * Print the context/unidiff header first time through.
787 */
788 print_header(file1, file2);
789 anychange = 1;
790 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
791 c > context_vec_ptr->d + (2 * context) + 1) {
792 /*
793 * If this change is more than 'context' lines from the
794 * previous change, dump the record and reset it.
795 */
796 dump_unified_vec(f1, f2);
797 }
798 context_vec_ptr++;
799 context_vec_ptr->a = a;
800 context_vec_ptr->b = b;
801 context_vec_ptr->c = c;
802 context_vec_ptr->d = d;
803 return;
804
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000805}
806
807
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000808static void output(char *file1, FILE * f1, char *file2, FILE * f2)
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000809{
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +0000810
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000811 /* Note that j0 and j1 can't be used as they are defined in math.h.
812 * This also allows the rather amusing variable 'j00'... */
813 int m, i0, i1, j00, j01;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000814
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000815 rewind(f1);
816 rewind(f2);
817 m = len[0];
818 J[0] = 0;
819 J[m + 1] = len[1] + 1;
820 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
821 while (i0 <= m && J[i0] == J[i0 - 1] + 1)
822 i0++;
823 j00 = J[i0 - 1] + 1;
824 i1 = i0 - 1;
825 while (i1 < m && J[i1 + 1] == 0)
826 i1++;
827 j01 = J[i1 + 1] - 1;
828 J[i1] = j01;
829 change(file1, f1, file2, f2, i0, i1, j00, j01);
830 }
831 if (m == 0) {
832 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000833 }
834 if (anychange != 0) {
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000835 dump_unified_vec(f1, f2);
836 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +0000837}
838
839/*
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000840 * The following code uses an algorithm due to Harold Stone,
841 * which finds a pair of longest identical subsequences in
842 * the two files.
843 *
844 * The major goal is to generate the match vector J.
845 * J[i] is the index of the line in file1 corresponding
846 * to line i file0. J[i] = 0 if there is no
847 * such line in file1.
848 *
849 * Lines are hashed so as to work in core. All potential
850 * matches are located by sorting the lines of each file
851 * on the hash (called ``value''). In particular, this
852 * collects the equivalence classes in file1 together.
853 * Subroutine equiv replaces the value of each line in
854 * file0 by the index of the first element of its
855 * matching equivalence in (the reordered) file1.
856 * To save space equiv squeezes file1 into a single
857 * array member in which the equivalence classes
858 * are simply concatenated, except that their first
859 * members are flagged by changing sign.
860 *
861 * Next the indices that point into member are unsorted into
862 * array class according to the original order of file0.
863 *
864 * The cleverness lies in routine stone. This marches
865 * through the lines of file0, developing a vector klist
866 * of "k-candidates". At step i a k-candidate is a matched
867 * pair of lines x,y (x in file0 y in file1) such that
868 * there is a common subsequence of length k
869 * between the first i lines of file0 and the first y
870 * lines of file1, but there is no such subsequence for
871 * any smaller y. x is the earliest possible mate to y
872 * that occurs in such a subsequence.
873 *
874 * Whenever any of the members of the equivalence class of
875 * lines in file1 matable to a line in file0 has serial number
876 * less than the y of some k-candidate, that k-candidate
877 * with the smallest such y is replaced. The new
878 * k-candidate is chained (via pred) to the current
879 * k-1 candidate so that the actual subsequence can
880 * be recovered. When a member has serial number greater
881 * that the y of all k-candidates, the klist is extended.
882 * At the end, the longest subsequence is pulled out
883 * and placed in the array J by unravel
884 *
885 * With J in hand, the matches there recorded are
886 * checked against reality to assure that no spurious
887 * matches have crept in due to hashing. If they have,
888 * they are broken, and "jackpot" is recorded--a harmless
889 * matter except that a true match for a spuriously
890 * mated line may now be unnecessarily reported as a change.
891 *
892 * Much of the complexity of the program comes simply
893 * from trying to minimize core utilization and
894 * maximize the range of doable problems by dynamically
895 * allocating what is needed and reusing what is not.
896 * The core requirements for problems larger than somewhat
897 * are (in words) 2*length(file0) + length(file1) +
898 * 3*(number of k-candidates installed), typically about
899 * 6n words for files of length n.
900 */
901
902static int diffreg(char *ofile1, char *ofile2, int flags)
903{
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000904 char *file1 = ofile1;
905 char *file2 = ofile2;
906 FILE *f1 = NULL;
907 FILE *f2 = NULL;
908 int rval = D_SAME;
909 int i;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000910
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000911 anychange = 0;
912 context_vec_ptr = context_vec_start - 1;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000913
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000914 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
915 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
916 if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
917 goto closem;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000918
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000919 if (flags & D_EMPTY1)
920 f1 = bb_xfopen(bb_dev_null, "r");
921 else {
922 if (strcmp(file1, "-") == 0)
923 f1 = stdin;
924 else
925 f1 = bb_xfopen(file1, "r");
926 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000927
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000928 if (flags & D_EMPTY2)
929 f2 = bb_xfopen(bb_dev_null, "r");
930 else {
931 if (strcmp(file2, "-") == 0)
932 f2 = stdin;
933 else
934 f2 = bb_xfopen(file2, "r");
935 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000936
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000937 if ((i = files_differ(f1, f2, flags)) == 0)
938 goto closem;
939 else if (i != 1) { /* 1 == ok */
940 /* error */
941 status |= 2;
942 goto closem;
943 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000944
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000945 if (!asciifile(f1) || !asciifile(f2)) {
946 rval = D_BINARY;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000947 status |= 1;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +0000948 goto closem;
949 }
950
951 prepare(0, f1, stb1.st_size);
952 prepare(1, f2, stb2.st_size);
953 prune();
954 sort(sfile[0], slen[0]);
955 sort(sfile[1], slen[1]);
956
957 member = (int *) file[1];
958 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
959 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
960
961 class = (int *) file[0];
962 unsort(sfile[0], slen[0], class);
963 class = xrealloc(class, (slen[0] + 2) * sizeof(int));
964
965 klist = xmalloc((slen[0] + 2) * sizeof(int));
966 clen = 0;
967 clistlen = 100;
968 clist = xmalloc(clistlen * sizeof(struct cand));
969 i = stone(class, slen[0], member, klist);
970 free(member);
971 free(class);
972
973 J = xrealloc(J, (len[0] + 2) * sizeof(int));
974 unravel(klist[i]);
975 free(clist);
976 free(klist);
977
978 ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
979 ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
980 check(f1, f2);
981 output(file1, f1, file2, f2);
982
983 closem:
984 if (anychange) {
985 status |= 1;
986 if (rval == D_SAME)
987 rval = D_DIFFER;
988 }
989 if (f1 != NULL)
990 fclose(f1);
991 if (f2 != NULL)
992 fclose(f2);
993 if (file1 != ofile1)
994 free(file1);
995 if (file2 != ofile2)
996 free(file2);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +0000997 return (rval);
998}
999
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001000#if ENABLE_FEATURE_DIFF_DIR
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001001static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
1002{
1003
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001004 int flags = D_HEADER;
1005 int val;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001006
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001007 char *fullpath1 = bb_xasprintf("%s/%s", dir1, path1);
1008 char *fullpath2 = bb_xasprintf("%s/%s", dir2, path2);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001009
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001010 if (stat(fullpath1, &stb1) != 0) {
1011 flags |= D_EMPTY1;
1012 memset(&stb1, 0, sizeof(stb1));
1013 fullpath1 = bb_xasprintf("%s/%s", dir1, path2);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001014 }
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001015 if (stat(fullpath2, &stb2) != 0) {
1016 flags |= D_EMPTY2;
1017 memset(&stb2, 0, sizeof(stb2));
1018 stb2.st_mode = stb1.st_mode;
1019 fullpath2 = bb_xasprintf("%s/%s", dir2, path1);
1020 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001021
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001022 if (stb1.st_mode == 0)
1023 stb1.st_mode = stb2.st_mode;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001024
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001025 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1026 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1027 return;
1028 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001029
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001030 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1031 val = D_SKIPPED1;
1032 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1033 val = D_SKIPPED2;
1034 else
1035 val = diffreg(fullpath1, fullpath2, flags);
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001036
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001037 print_status(val, fullpath1, fullpath2, NULL);
1038}
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001039#endif
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001040
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001041#if ENABLE_FEATURE_DIFF_DIR
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001042static int dir_strcmp(const void *p1, const void *p2)
1043{
1044 return strcmp(*(char *const *) p1, *(char *const *) p2);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001045}
1046
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001047/* This function adds a filename to dl, the directory listing. */
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001048
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001049static int add_to_dirlist(const char *filename,
1050 struct stat ATTRIBUTE_UNUSED * sb, void *userdata)
1051{
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001052 dl_count++;
1053 dl = xrealloc(dl, dl_count * sizeof(char *));
1054 dl[dl_count - 1] = bb_xstrdup(filename);
1055 if (cmd_flags & FLAG_r) {
1056 int *pp = (int *) userdata;
1057 int path_len = *pp + 1;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001058
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001059 dl[dl_count - 1] = &(dl[dl_count - 1])[path_len];
1060 }
1061 return TRUE;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001062}
1063
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001064/* This returns a sorted directory listing. */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001065static char **get_dir(char *path)
1066{
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001067
1068 int i;
Bernhard Reutner-Fischer5fb0fec2006-04-06 11:28:19 +00001069 char **retval;
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001070
1071 /* If -r has been set, then the recursive_action function will be
1072 * used. Unfortunately, this outputs the root directory along with
1073 * the recursed paths, so use void *userdata to specify the string
1074 * length of the root directory. It can then be removed in
1075 * add_to_dirlist. */
1076
1077 int path_len = strlen(path);
1078 void *userdata = &path_len;
Bernhard Reutner-Fischer5fb0fec2006-04-06 11:28:19 +00001079
1080 /* Reset dl_count - there's no need to free dl as bb_xrealloc does
1081 * the job nicely. */
1082 dl_count = 0;
1083
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001084 /* Now fill dl with a listing. */
1085 if (cmd_flags & FLAG_r)
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001086 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL,
1087 userdata);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001088 else {
1089 DIR *dp;
1090 struct dirent *ep;
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00001091
1092 dp = bb_opendir(path);
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001093 while ((ep = readdir(dp))) {
1094 if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, ".")))
1095 continue;
1096 add_to_dirlist(ep->d_name, NULL, NULL);
1097 }
1098 closedir(dp);
1099 }
1100
1101 /* Sort dl alphabetically. */
1102 qsort(dl, dl_count, sizeof(char *), dir_strcmp);
1103
1104 /* Copy dl so that we can return it. */
Bernhard Reutner-Fischer5fb0fec2006-04-06 11:28:19 +00001105 retval = xmalloc(dl_count * sizeof(char *));
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001106 for (i = 0; i < dl_count; i++)
1107 retval[i] = bb_xstrdup(dl[i]);
1108
1109 return retval;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001110}
1111
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001112static void diffdir(char *p1, char *p2)
1113{
1114
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001115 char **dirlist1, **dirlist2;
1116 char *dp1, *dp2;
1117 int dirlist1_count, dirlist2_count;
1118 int pos;
1119
1120 /* Check for trailing slashes. */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001121
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001122 if (p1[strlen(p1) - 1] == '/')
1123 p1[strlen(p1) - 1] = '\0';
1124 if (p2[strlen(p2) - 1] == '/')
1125 p2[strlen(p2) - 1] = '\0';
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001126
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001127 /* Get directory listings for p1 and p2. */
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001128
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001129 dirlist1 = get_dir(p1);
1130 dirlist1_count = dl_count;
1131 dirlist1[dirlist1_count] = NULL;
1132 dirlist2 = get_dir(p2);
1133 dirlist2_count = dl_count;
1134 dirlist2[dirlist2_count] = NULL;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001135
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001136 /* If -S was set, find the starting point. */
1137 if (start) {
1138 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1139 dirlist1++;
1140 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1141 dirlist2++;
1142 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +00001143 bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001144 }
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001145
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001146 /* Now that both dirlist1 and dirlist2 contain sorted directory
1147 * listings, we can start to go through dirlist1. If both listings
1148 * contain the same file, then do a normal diff. Otherwise, behaviour
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001149 * is determined by whether the -N flag is set. */
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001150 while (*dirlist1 != NULL || *dirlist2 != NULL) {
1151 dp1 = *dirlist1;
1152 dp2 = *dirlist2;
1153 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1154 if (pos == 0) {
1155 do_diff(p1, dp1, p2, dp2);
1156 dirlist1++;
1157 dirlist2++;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001158 } else if (pos < 0) {
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001159 if (cmd_flags & FLAG_N)
1160 do_diff(p1, dp1, p2, NULL);
1161 else
1162 print_only(p1, strlen(p1) + 1, dp1);
1163 dirlist1++;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001164 } else {
Bernhard Reutner-Fischer693a9362006-04-06 08:15:24 +00001165 if (cmd_flags & FLAG_N)
1166 do_diff(p1, NULL, p2, dp2);
1167 else
1168 print_only(p2, strlen(p2) + 1, dp2);
1169 dirlist2++;
1170 }
1171 }
1172}
1173#endif
1174
1175
1176
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001177int diff_main(int argc, char **argv)
1178{
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001179 int gotstdin = 0;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001180
1181 char *U_opt;
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001182 llist_t *L_arg = NULL;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001183
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001184 bb_opt_complementally = "L::";
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001185 cmd_flags =
1186 bb_getopt_ulflags(argc, argv, "abdiL:NqrsS:tTU:wu", &L_arg, &start,
1187 &U_opt);
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001188
1189 if (cmd_flags & FLAG_L) {
1190 while (L_arg) {
1191 if (label[0] == NULL)
1192 label[0] = L_arg->data;
1193 else if (label[1] == NULL)
1194 label[1] = L_arg->data;
1195 else
1196 bb_show_usage();
1197
1198 L_arg = L_arg->link;
1199 }
1200
1201 /* If both label[0] and label[1] were set, they need to be swapped. */
1202 if (label[0] && label[1]) {
1203 char *tmp;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001204
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001205 tmp = label[1];
1206 label[1] = label[0];
1207 label[0] = tmp;
1208 }
1209 }
1210
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001211 context = 3; /* This is the default number of lines of context. */
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001212 if (cmd_flags & FLAG_U) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +00001213 context = bb_xgetlarg(U_opt, 10, 1, INT_MAX);
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001214 }
1215 argc -= optind;
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001216 argv += optind;
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001217
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001218 /*
1219 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1220 * driver routine. Both drivers use the contents of stb1 and stb2.
1221 */
1222 if (argc < 2) {
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001223 bb_error_msg("Missing filename");
1224 bb_show_usage();
1225 }
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001226 if (strcmp(argv[0], "-") == 0) {
1227 fstat(STDIN_FILENO, &stb1);
1228 gotstdin = 1;
1229 } else
1230 xstat(argv[0], &stb1);
1231 if (strcmp(argv[1], "-") == 0) {
1232 fstat(STDIN_FILENO, &stb2);
1233 gotstdin = 1;
1234 } else
1235 xstat(argv[1], &stb2);
1236 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1237 bb_error_msg_and_die("Can't compare - to a directory");
1238 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001239#if ENABLE_FEATURE_DIFF_DIR
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001240 diffdir(argv[0], argv[1]);
1241#else
1242 bb_error_msg_and_die("Directory comparison not supported");
1243#endif
Bernhard Reutner-Fischerbbc225e2006-05-29 12:12:45 +00001244 } else {
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001245 if (S_ISDIR(stb1.st_mode)) {
1246 argv[0] = concat_path_file(argv[0], argv[1]);
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +00001247 xstat(argv[0], &stb1);
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001248 }
1249 if (S_ISDIR(stb2.st_mode)) {
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001250 argv[1] = concat_path_file(argv[1], argv[0]);
Bernhard Reutner-Fischerd2c306e2006-05-29 12:10:23 +00001251 xstat(argv[1], &stb2);
Bernhard Reutner-Fischerbc142142006-04-06 16:07:08 +00001252 }
Bernhard Reutner-Fischer8f7d3892006-04-06 08:11:08 +00001253 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], NULL);
1254 }
1255 exit(status);
1256}