- add and use bb_opendir(), bb_xopendir().
[oweals/busybox.git] / coreutils / diff.c
1 /* 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>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /*
11  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
12  *
13  * Permission to
14  * use, copy, modify, and distribute this software for any
15  * purpose with or without fee is hereby granted, provided that the above
16  * copyright notice and this permission notice appear in all copies.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  *
26  * Sponsored in part by the Defense Advanced Research Projects
27  * Agency (DARPA) and Air Force Research Laboratory, Air Force
28  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
29  */
30
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/wait.h>
44 #include <fcntl.h>
45 #include <stddef.h>
46 #include <paths.h>
47 #include <dirent.h>
48 #include "busybox.h"
49
50 #define FSIZE_MAX 32768
51
52 /*
53  * Output flags
54  */
55 #define D_HEADER        1        /* Print a header/footer between files */
56 #define D_EMPTY1        2        /* Treat first file as empty (/dev/null) */
57 #define D_EMPTY2        4        /* Treat second file as empty (/dev/null) */
58
59 /*
60  * Status values for print_status() and diffreg() return values
61  * Guide:
62  * D_SAME - files are the same
63  * D_DIFFER - files differ
64  * D_BINARY - binary files differ
65  * D_COMMON - subdirectory common to both dirs
66  * D_ONLY - file only exists in one dir
67  * D_MISMATCH1 - path1 a dir, path2 a file
68  * D_MISMATCH2 - path1 a file, path2 a dir
69  * D_ERROR - error occurred
70  * D_SKIPPED1 - skipped path1 as it is a special file
71  * D_SKIPPED2 - skipped path2 as it is a special file
72  */
73
74 #define D_SAME          0
75 #define D_DIFFER        (1<<0)
76 #define D_BINARY        (1<<1)
77 #define D_COMMON        (1<<2)
78 #define D_ONLY          (1<<3)
79 #define D_MISMATCH1     (1<<4)
80 #define D_MISMATCH2     (1<<5)
81 #define D_ERROR         (1<<6)
82 #define D_SKIPPED1      (1<<7)
83 #define D_SKIPPED2      (1<<8)
84
85 /* Command line options */
86 static unsigned long cmd_flags;
87 #define FLAG_a  (1<<0)
88 #define FLAG_b  (1<<1)
89 #define FLAG_d  (1<<2)
90 #define FLAG_i  (1<<3)
91 #define FLAG_L  (1<<4)
92 #define FLAG_N  (1<<5)
93 #define FLAG_q  (1<<6)
94 #define FLAG_r  (1<<7)
95 #define FLAG_s  (1<<8)
96 #define FLAG_S  (1<<9)
97 #define FLAG_t  (1<<10)
98 #define FLAG_T  (1<<11)
99 #define FLAG_U  (1<<12)
100 #define FLAG_w  (1<<13)
101
102 int context, status;
103 char *start, *label[2];
104 struct stat stb1, stb2;
105 char **dl;
106 int dl_count = 0;
107
108 struct cand {
109         int x;
110         int y;
111         int pred;
112 };
113
114 struct line {
115         int serial;
116         int value;
117 } *file[2];
118
119 /*
120  * The following struct is used to record change information
121  * doing a "context" or "unified" diff.  (see routine "change" to
122  * understand the highly mnemonic field names)
123  */
124 struct context_vec {
125         int a;                        /* start line in old file */
126         int b;                        /* end line in old file */
127         int c;                        /* start line in new file */
128         int d;                        /* end line in new file */
129 };
130
131 static int  *J;                 /* will be overlaid on class */
132 static int  *class;             /* will be overlaid on file[0] */
133 static int  *klist;             /* will be overlaid on file[0] after class */
134 static int  *member;            /* will be overlaid on file[1] */
135 static int   clen;
136 static int   len[2];
137 static int   pref, suff;        /* length of prefix and suffix */
138 static int   slen[2];
139 static int   anychange;
140 static long *ixnew;             /* will be overlaid on file[1] */
141 static long *ixold;             /* will be overlaid on klist */
142 static struct cand *clist;      /* merely a free storage pot for candidates */
143 static int   clistlen;          /* the length of clist */
144 static struct line *sfile[2];   /* shortened by pruning common prefix/suffix */
145 static struct context_vec *context_vec_start;
146 static struct context_vec *context_vec_end;
147 static struct context_vec *context_vec_ptr;
148
149 static void print_only(const char *path, size_t dirlen, const char *entry)
150 {
151         if (dirlen > 1)
152                 dirlen--;
153         printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
154 }
155
156 static void print_status(int val, char *path1, char *path2, char *entry)
157 {
158         const char * const _entry = entry ? entry : "";
159         char *_path1 = entry ? concat_path_file(path1, _entry) : path1;
160         char *_path2 = entry ? concat_path_file(path2, _entry) : path2;
161         switch (val) {
162         case D_ONLY:
163                 print_only(path1, strlen(path1), entry);
164                 break;
165         case D_COMMON:
166                 printf("Common subdirectories: %s and %s\n", _path1, _path2);
167                 break;
168         case D_BINARY:
169                 printf("Binary files %s and %s differ\n", _path1, _path2);
170                 break;
171         case D_DIFFER:
172                 if (cmd_flags & FLAG_q)
173                         printf("Files %s and %s differ\n", _path1, _path2);
174                 break;
175         case D_SAME:
176                 if (cmd_flags & FLAG_s)
177                         printf("Files %s and %s are identical\n", _path1, _path2);
178                 break;
179         case D_MISMATCH1:
180                 printf("File %s is a directory while file %s is a regular file\n",
181                     _path1, _path2);
182                 break;
183         case D_MISMATCH2:
184                 printf("File %s is a regular file while file %s is a directory\n",
185                     _path1, _path2);
186                 break;
187         case D_SKIPPED1:
188                 printf("File %s is not a regular file or directory and was skipped\n",
189                     _path1);
190                 break;
191         case D_SKIPPED2:
192                 printf("File %s is not a regular file or directory and was skipped\n",
193                     _path2);
194                 break;
195         }
196                 if (entry) {
197                         free(_path1);
198                         free(_path2);
199                 }
200 }
201
202 /*
203  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
204  */
205 static int readhash(FILE *f)
206 {
207         int i, t, space;
208         int sum;
209
210         sum = 1;
211         space = 0;
212         if (!(cmd_flags & FLAG_b) && !(cmd_flags & FLAG_w)) {
213                 if (FLAG_i)
214                         for (i = 0; (t = getc(f)) != '\n'; i++) {
215                                 if (t == EOF) {
216                                         if (i == 0)
217                                                 return (0);
218                                         break;
219                                 }
220                                 sum = sum * 127 + t;
221                         }
222                 else
223                         for (i = 0; (t = getc(f)) != '\n'; i++) {
224                                 if (t == EOF) {
225                                         if (i == 0)
226                                                 return (0);
227                                         break;
228                                 }
229                                 sum = sum * 127 + t;
230                         }
231         } else {
232                 for (i = 0;;) {
233                         switch (t = getc(f)) {
234                         case '\t':
235                         case '\r':
236                         case '\v':
237                         case '\f':
238                         case ' ':
239                                 space++;
240                                 continue;
241                         default:
242                                 if (space && !(cmd_flags & FLAG_w)) {
243                                         i++;
244                                         space = 0;
245                                 }
246                                 sum = sum * 127 + t;
247                                 i++;
248                                 continue;
249                         case EOF:
250                                 if (i == 0)
251                                         return (0);
252                                 /* FALLTHROUGH */
253                         case '\n':
254                                 break;
255                         }
256                         break;
257                 }
258         }
259         /*
260          * There is a remote possibility that we end up with a zero sum.
261          * Zero is used as an EOF marker, so return 1 instead.
262          */
263         return (sum == 0 ? 1 : sum);
264 }
265
266
267
268 /*
269  * Check to see if the given files differ.
270  * Returns 0 if they are the same, 1 if different, and -1 on error.
271  */
272 static int files_differ(FILE *f1, FILE *f2, int flags)
273 {
274         char buf1[BUFSIZ], buf2[BUFSIZ];
275         size_t i, j;
276
277         if ((flags & (D_EMPTY1|D_EMPTY2)) || stb1.st_size != stb2.st_size ||
278             (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
279                 return (1);
280         while(1) {
281                 i = fread(buf1, 1, sizeof(buf1), f1);
282                 j = fread(buf2, 1, sizeof(buf2), f2);
283                 if (i != j)
284                         return (1);
285                 if (i == 0 && j == 0) {
286                         if (ferror(f1) || ferror(f2))
287                                 return (1);
288                         return (0);
289                 }
290                 if (memcmp(buf1, buf2, i) != 0)
291                         return (1);
292         }
293 }
294
295 static void prepare(int i, FILE *fd, off_t filesize)
296 {
297         struct line *p;
298         int h;
299         size_t j, sz;
300
301         rewind(fd);
302
303         sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;
304         if (sz < 100)
305                 sz = 100;
306
307         p = xmalloc((sz + 3) * sizeof(struct line));
308         for (j = 0; (h = readhash(fd));) {
309                 if (j == sz) {
310                         sz = sz * 3 / 2;
311                         p = xrealloc(p, (sz + 3) * sizeof(struct line));
312                 }
313                 p[++j].value = h;
314         }
315         len[i] = j;
316         file[i] = p;
317 }
318
319 static void prune(void)
320 {
321         int i, j;
322
323         for (pref = 0; pref < len[0] && pref < len[1] &&
324             file[0][pref + 1].value == file[1][pref + 1].value;
325             pref++)
326                 ;
327         for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
328             file[0][len[0] - suff].value == file[1][len[1] - suff].value;
329             suff++)
330                 ;
331         for (j = 0; j < 2; j++) {
332                 sfile[j] = file[j] + pref;
333                 slen[j] = len[j] - pref - suff;
334                 for (i = 0; i <= slen[j]; i++)
335                         sfile[j][i].serial = i;
336         }
337 }
338
339 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
340 {
341         int i, j;
342
343         i = j = 1;
344         while (i <= n && j <= m) {
345                 if (a[i].value < b[j].value)
346                         a[i++].value = 0;
347                 else if (a[i].value == b[j].value)
348                         a[i++].value = j;
349                 else
350                         j++;
351         }
352         while (i <= n)
353                 a[i++].value = 0;
354         b[m + 1].value = 0;
355         j = 0;
356         while (++j <= m) {
357                 c[j] = -b[j].serial;
358                 while (b[j + 1].value == b[j].value) {
359                         j++;
360                         c[j] = b[j].serial;
361                 }
362         }
363         c[j] = -1;
364 }
365
366 static int isqrt(int n) {
367         int y, x = 1;
368         if (n == 0) return(0);
369
370         do {
371                 y = x;
372                 x = n / x;
373                 x += y;
374                 x /= 2;
375         } while ((x - y) > 1 || (x - y) < -1);
376
377         return (x);
378 }
379
380 static int newcand(int x, int y, int pred)
381 {
382         struct cand *q;
383
384         if (clen == clistlen) {
385                 clistlen = clistlen * 11 / 10;
386                 clist = xrealloc(clist, clistlen * sizeof(struct cand));
387         }
388         q = clist + clen;
389         q->x = x;
390         q->y = y;
391         q->pred = pred;
392         return (clen++);
393 }
394
395
396 static int search(int *c, int k, int y)
397 {
398         int i, j, l, t;
399
400         if (clist[c[k]].y < y)        /* quick look for typical case */
401                 return (k + 1);
402         i = 0;
403         j = k + 1;
404         while (1) {
405                 l = i + j;
406                 if ((l >>= 1) <= i)
407                         break;
408                 t = clist[c[l]].y;
409                 if (t > y)
410                         j = l;
411                 else if (t < y)
412                         i = l;
413                 else
414                         return (l);
415         }
416         return (l + 1);
417 }
418
419
420 static int stone(int *a, int n, int *b, int *c)
421 {
422         int i, k, y, j, l;
423         int oldc, tc, oldl;
424         u_int numtries;
425 #if ENABLE_FEATURE_DIFF_MINIMAL
426                 const u_int bound = (cmd_flags & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
427 #else
428                 const u_int bound = MAX(256, isqrt(n));
429 #endif
430         k = 0;
431         c[0] = newcand(0, 0, 0);
432         for (i = 1; i <= n; i++) {
433                 j = a[i];
434                 if (j == 0)
435                         continue;
436                 y = -b[j];
437                 oldl = 0;
438                 oldc = c[0];
439                 numtries = 0;
440                 do {
441                         if (y <= clist[oldc].y)
442                                 continue;
443                         l = search(c, k, y);
444                         if (l != oldl + 1)
445                                 oldc = c[l - 1];
446                         if (l <= k) {
447                                 if (clist[c[l]].y <= y)
448                                         continue;
449                                 tc = c[l];
450                                 c[l] = newcand(i, y, oldc);
451                                 oldc = tc;
452                                 oldl = l;
453                                 numtries++;
454                         } else {
455                                 c[l] = newcand(i, y, oldc);
456                                 k++;
457                                 break;
458                         }
459                 } while ((y = b[++j]) > 0 && numtries < bound);
460         }
461         return (k);
462 }
463
464 static void unravel(int p)
465 {
466         struct cand *q;
467         int i;
468
469         for (i = 0; i <= len[0]; i++)
470                 J[i] = i <= pref ? i :
471                     i > len[0] - suff ? i + len[1] - len[0] : 0;
472         for (q = clist + p; q->y != 0; q = clist + q->pred)
473                 J[q->x + pref] = q->y + pref;
474 }
475
476
477 static void unsort(struct line *f, int l, int *b)
478 {
479         int *a, i;
480
481         a = xmalloc((l + 1) * sizeof(int));
482         for (i = 1; i <= l; i++)
483                 a[f[i].serial] = f[i].value;
484         for (i = 1; i <= l; i++)
485                 b[i] = a[i];
486         free(a);
487 }
488
489 static int skipline(FILE *f)
490 {
491         int i, c;
492
493         for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
494                 continue;
495         return (i);
496 }
497
498
499 /*
500  * Check does double duty:
501  *  1.  ferret out any fortuitous correspondences due
502  *      to confounding by hashing (which result in "jackpot")
503  *  2.  collect random access indexes to the two files
504  */
505 static void check(FILE *f1, FILE *f2)
506 {
507         int i, j, jackpot, c, d;
508         long ctold, ctnew;
509
510         rewind(f1);
511         rewind(f2);
512         j = 1;
513         ixold[0] = ixnew[0] = 0;
514         jackpot = 0;
515         ctold = ctnew = 0;
516         for (i = 1; i <= len[0]; i++) {
517                 if (J[i] == 0) {
518                         ixold[i] = ctold += skipline(f1);
519                         continue;
520                 }
521                 while (j < J[i]) {
522                         ixnew[j] = ctnew += skipline(f2);
523                         j++;
524                 }
525                 if ((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w) || (cmd_flags & FLAG_i)) {
526                         while (1) {
527                                 c = getc(f1);
528                                 d = getc(f2);
529                                 /*
530                                  * GNU diff ignores a missing newline
531                                  * in one file if bflag || wflag.
532                                  */
533                                 if (((cmd_flags & FLAG_b) || (cmd_flags & FLAG_w)) &&
534                                     ((c == EOF && d == '\n') ||
535                                     (c == '\n' && d == EOF))) {
536                                         break;
537                                 }
538                                 ctold++;
539                                 ctnew++;
540                                 if ((cmd_flags & FLAG_b) && isspace(c) && isspace(d)) {
541                                         do {
542                                                 if (c == '\n')
543                                                         break;
544                                                 ctold++;
545                                         } while (isspace(c = getc(f1)));
546                                         do {
547                                                 if (d == '\n')
548                                                         break;
549                                                 ctnew++;
550                                         } while (isspace(d = getc(f2)));
551                                 } else if (cmd_flags & FLAG_w) {
552                                         while (isspace(c) && c != '\n') {
553                                                 c = getc(f1);
554                                                 ctold++;
555                                         }
556                                         while (isspace(d) && d != '\n') {
557                                                 d = getc(f2);
558                                                 ctnew++;
559                                         }
560                                 }
561                                 if (c != d) {
562                                         jackpot++;
563                                         J[i] = 0;
564                                         if (c != '\n' && c != EOF)
565                                                 ctold += skipline(f1);
566                                         if (d != '\n' && c != EOF)
567                                                 ctnew += skipline(f2);
568                                         break;
569                                 }
570                                 if (c == '\n' || c == EOF)
571                                         break;
572                         }
573                 } else {
574                         while (1) {
575                                 ctold++;
576                                 ctnew++;
577                                 if ((c = getc(f1)) != (d = getc(f2))) {
578                                         J[i] = 0;
579                                         if (c != '\n' && c != EOF)
580                                                 ctold += skipline(f1);
581                                         if (d != '\n' && c != EOF)
582                                                 ctnew += skipline(f2);
583                                         break;
584                                 }
585                                 if (c == '\n' || c == EOF)
586                                         break;
587                         }
588                 }
589                 ixold[i] = ctold;
590                 ixnew[j] = ctnew;
591                 j++;
592         }
593         for (; j <= len[1]; j++)
594                 ixnew[j] = ctnew += skipline(f2);
595 }
596
597 /* shellsort CACM #201 */
598 static void sort(struct line *a, int n)
599 {
600         struct line *ai, *aim, w;
601         int j, m = 0, k;
602
603         if (n == 0)
604                 return;
605         for (j = 1; j <= n; j *= 2)
606                 m = 2 * j - 1;
607         for (m /= 2; m != 0; m /= 2) {
608                 k = n - m;
609                 for (j = 1; j <= k; j++) {
610                         for (ai = &a[j]; ai > a; ai -= m) {
611                                 aim = &ai[m];
612                                 if (aim < ai)
613                                         break;        /* wraparound */
614                                 if (aim->value > ai[0].value ||
615                                     (aim->value == ai[0].value &&
616                                         aim->serial > ai[0].serial))
617                                         break;
618                                 w.value = ai[0].value;
619                                 ai[0].value = aim->value;
620                                 aim->value = w.value;
621                                 w.serial = ai[0].serial;
622                                 ai[0].serial = aim->serial;
623                                 aim->serial = w.serial;
624                         }
625                 }
626         }
627 }
628
629
630 static void uni_range(int a, int b)
631 {
632         if (a < b)
633                 printf("%d,%d", a, b - a + 1);
634         else if (a == b)
635                 printf("%d", b);
636         else
637                 printf("%d,0", b);
638 }
639
640 static int fetch(long *f, int a, int b, FILE *lb, int ch)
641 {
642         int i, j, c, lastc, col, nc;
643
644         if (a > b)
645                 return (0);
646         for (i = a; i <= b; i++) {
647                 fseek(lb, f[i - 1], SEEK_SET);
648                 nc = f[i] - f[i - 1];
649                 if (ch != '\0') {
650                         putchar(ch);
651                         if (cmd_flags & FLAG_T)
652                                 putchar('\t');
653                 }
654                 col = 0;
655                 for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
656                         if ((c = getc(lb)) == EOF) {
657                                 puts("\n\\ No newline at end of file");
658                                 return (0);
659                         }
660                         if (c == '\t' && (cmd_flags & FLAG_t)) {
661                                 do {
662                                         putchar(' ');
663                                 } while (++col & 7);
664                         } else {
665                                 putchar(c);
666                                 col++;
667                         }
668                 }
669         }
670         return (0);
671 }
672
673 static int asciifile(FILE *f)
674 {
675 #if ENABLE_FEATURE_DIFF_BINARY
676         unsigned char buf[BUFSIZ];
677         int i, cnt;
678 #endif
679
680         if ((cmd_flags & FLAG_a) || f == NULL)
681                 return (1);
682
683 #if ENABLE_FEATURE_DIFF_BINARY
684         rewind(f);
685         cnt = fread(buf, 1, sizeof(buf), f);
686         for (i = 0; i < cnt; i++) {
687                 if (!isprint(buf[i]) && !isspace(buf[i])) {
688                         return (0);
689                 }
690         }
691 #endif
692         return (1);
693 }
694
695 /* dump accumulated "unified" diff changes */
696 static void dump_unified_vec(FILE *f1, FILE *f2)
697 {
698         struct context_vec *cvp = context_vec_start;
699         int lowa, upb, lowc, upd;
700         int a, b, c, d;
701         char ch;
702
703         if (context_vec_start > context_vec_ptr)
704                 return;
705
706         b = d = 0;                /* gcc */
707         lowa = MAX(1, cvp->a - context);
708         upb = MIN(len[0], context_vec_ptr->b + context);
709         lowc = MAX(1, cvp->c - context);
710         upd = MIN(len[1], context_vec_ptr->d + context);
711
712         fputs("@@ -", stdout);
713         uni_range(lowa, upb);
714         fputs(" +", stdout);
715         uni_range(lowc, upd);
716         fputs(" @@", stdout);
717         putchar('\n');
718
719         /*
720          * Output changes in "unified" diff format--the old and new lines
721          * are printed together.
722          */
723         for (; cvp <= context_vec_ptr; cvp++) {
724                 a = cvp->a;
725                 b = cvp->b;
726                 c = cvp->c;
727                 d = cvp->d;
728
729                 /*
730                  * c: both new and old changes
731                  * d: only changes in the old file
732                  * a: only changes in the new file
733                  */
734                 if (a <= b && c <= d)
735                         ch = 'c';
736                 else
737                         ch = (a <= b) ? 'd' : 'a';
738 #if 0
739                 switch (ch) {
740                 case 'c':
741                         fetch(ixold, lowa, a - 1, f1, ' ');
742                         fetch(ixold, a, b, f1, '-');
743                         fetch(ixnew, c, d, f2, '+');
744                         break;
745                 case 'd':
746                         fetch(ixold, lowa, a - 1, f1, ' ');
747                         fetch(ixold, a, b, f1, '-');
748                         break;
749                 case 'a':
750                         fetch(ixnew, lowc, c - 1, f2, ' ');
751                         fetch(ixnew, c, d, f2, '+');
752                         break;
753                 }
754 #else
755                 if (ch == 'c' || ch == 'd') {
756                         fetch(ixold, lowa, a - 1, f1, ' ');
757                         fetch(ixold, a, b, f1, '-');
758                                 }
759                 if (ch == 'a')
760                         fetch(ixnew, lowc, c - 1, f2, ' ');
761                 if (ch == 'c' || ch == 'a')
762                         fetch(ixnew, c, d, f2, '+');
763 #endif
764                 lowa = b + 1;
765                 lowc = d + 1;
766         }
767         fetch(ixnew, d + 1, upd, f2, ' ');
768
769         context_vec_ptr = context_vec_start - 1;
770 }
771
772
773 static void print_header(const char *file1, const char *file2)
774 {
775         if (label[0] != NULL)
776                 printf("%s %s\n", "---",
777                     label[0]);
778         else
779                 printf("%s %s\t%s", "---",
780                     file1, ctime(&stb1.st_mtime));
781         if (label[1] != NULL)
782                 printf("%s %s\n", "+++",
783                     label[1]);
784         else
785                 printf("%s %s\t%s", "+++",
786                     file2, ctime(&stb2.st_mtime));
787 }
788
789
790
791 /*
792  * Indicate that there is a difference between lines a and b of the from file
793  * to get to lines c to d of the to file.  If a is greater then b then there
794  * are no lines in the from file involved and this means that there were
795  * lines appended (beginning at b).  If c is greater than d then there are
796  * lines missing from the to file.
797  */
798 static void change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d)
799 {
800         static size_t max_context = 64;
801
802         if (a > b && c > d)     return;
803         if (cmd_flags & FLAG_q) return;
804         
805         /*
806         * Allocate change records as needed.
807         */
808                 if (context_vec_ptr == context_vec_end - 1) {
809                         ptrdiff_t offset = context_vec_ptr - context_vec_start;
810                         max_context <<= 1;
811                         context_vec_start = xrealloc(context_vec_start,
812                             max_context * sizeof(struct context_vec));
813                         context_vec_end = context_vec_start + max_context;
814                         context_vec_ptr = context_vec_start + offset;
815                 }
816                 if (anychange == 0) {
817                         /*
818                          * Print the context/unidiff header first time through.
819                          */
820                         print_header(file1, file2);
821                         anychange = 1;
822                 } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
823                     c > context_vec_ptr->d + (2 * context) + 1) {
824                         /*
825                          * If this change is more than 'context' lines from the
826                          * previous change, dump the record and reset it.
827                          */
828                         dump_unified_vec(f1, f2);
829                 }
830                 context_vec_ptr++;
831                 context_vec_ptr->a = a;
832                 context_vec_ptr->b = b;
833                 context_vec_ptr->c = c;
834                 context_vec_ptr->d = d;
835                 return;
836         
837 }
838
839
840 static void output(char *file1, FILE *f1, char *file2, FILE *f2)
841 {
842
843                 /* Note that j0 and j1 can't be used as they are defined in math.h.
844                  * This also allows the rather amusing variable 'j00'... */
845                 int m, i0, i1, j00, j01;
846
847         rewind(f1);
848         rewind(f2);
849         m = len[0];
850         J[0] = 0;
851         J[m + 1] = len[1] + 1;
852         for (i0 = 1; i0 <= m; i0 = i1 + 1) {
853                         while (i0 <= m && J[i0] == J[i0 - 1] + 1)
854                                 i0++;
855                         j00 = J[i0 - 1] + 1;
856                         i1 = i0 - 1;
857                         while (i1 < m && J[i1 + 1] == 0)
858                                 i1++;
859                         j01 = J[i1 + 1] - 1;
860                         J[i1] = j01;
861                         change(file1, f1, file2, f2, i0, i1, j00, j01);
862         }
863         if (m == 0) {
864                 change(file1, f1, file2, f2, 1, 0, 1, len[1]);
865         }
866         if (anychange != 0) {
867                 dump_unified_vec(f1, f2);
868         }
869 }
870
871 /*
872  *      The following code uses an algorithm due to Harold Stone, 
873  *      which finds a pair of longest identical subsequences in 
874  *      the two files.
875  *
876  *      The major goal is to generate the match vector J.
877  *      J[i] is the index of the line in file1 corresponding
878  *      to line i file0. J[i] = 0 if there is no
879  *      such line in file1.
880  *
881  *      Lines are hashed so as to work in core. All potential
882  *      matches are located by sorting the lines of each file
883  *      on the hash (called ``value''). In particular, this
884  *      collects the equivalence classes in file1 together.
885  *      Subroutine equiv replaces the value of each line in
886  *      file0 by the index of the first element of its
887  *      matching equivalence in (the reordered) file1.
888  *      To save space equiv squeezes file1 into a single
889  *      array member in which the equivalence classes
890  *      are simply concatenated, except that their first
891  *      members are flagged by changing sign.
892  *
893  *      Next the indices that point into member are unsorted into
894  *      array class according to the original order of file0.
895  *
896  *      The cleverness lies in routine stone. This marches
897  *      through the lines of file0, developing a vector klist
898  *      of "k-candidates". At step i a k-candidate is a matched
899  *      pair of lines x,y (x in file0 y in file1) such that
900  *      there is a common subsequence of length k
901  *      between the first i lines of file0 and the first y
902  *      lines of file1, but there is no such subsequence for
903  *      any smaller y. x is the earliest possible mate to y
904  *      that occurs in such a subsequence.
905  *
906  *      Whenever any of the members of the equivalence class of
907  *      lines in file1 matable to a line in file0 has serial number
908  *      less than the y of some k-candidate, that k-candidate
909  *      with the smallest such y is replaced. The new
910  *      k-candidate is chained (via pred) to the current
911  *      k-1 candidate so that the actual subsequence can
912  *      be recovered. When a member has serial number greater
913  *      that the y of all k-candidates, the klist is extended.
914  *      At the end, the longest subsequence is pulled out
915  *      and placed in the array J by unravel
916  *
917  *      With J in hand, the matches there recorded are
918  *      checked against reality to assure that no spurious
919  *      matches have crept in due to hashing. If they have,
920  *      they are broken, and "jackpot" is recorded--a harmless
921  *      matter except that a true match for a spuriously
922  *      mated line may now be unnecessarily reported as a change.
923  *
924  *      Much of the complexity of the program comes simply
925  *      from trying to minimize core utilization and
926  *      maximize the range of doable problems by dynamically
927  *      allocating what is needed and reusing what is not.
928  *      The core requirements for problems larger than somewhat
929  *      are (in words) 2*length(file0) + length(file1) +
930  *      3*(number of k-candidates installed),  typically about
931  *      6n words for files of length n.
932  */
933
934 static int diffreg(char *ofile1, char *ofile2, int flags)
935 {
936         char *file1 = ofile1;
937         char *file2 = ofile2;
938         FILE *f1 = NULL;
939         FILE *f2 = NULL;
940         int rval = D_SAME;
941         int i;
942
943         anychange = 0;
944         context_vec_ptr = context_vec_start - 1;
945                 
946         if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
947                 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
948         if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
949                 goto closem;
950
951         if (flags & D_EMPTY1)
952                 f1 = bb_xfopen(_PATH_DEVNULL, "r");
953         else {
954                 if (strcmp(file1, "-") == 0)
955                         f1 = stdin;
956                 else
957                         f1 = bb_xfopen(file1, "r");
958         }
959
960         if (flags & D_EMPTY2)
961                 f2 = bb_xfopen(_PATH_DEVNULL, "r");
962         else {
963                 if (strcmp(file2, "-") == 0)
964                         f2 = stdin;
965                 else
966                         f2 = bb_xfopen(file2, "r");
967         }
968         
969         if ((i=files_differ(f1, f2, flags)) == 0)
970                 goto closem;
971         else if (i != 1) {/* 1 == ok */
972                 /* error */
973                 status |= 2;
974                 goto closem;
975         }
976
977         if (!asciifile(f1) || !asciifile(f2)) {
978                 rval = D_BINARY;
979                 status |= 1;
980                 goto closem;
981         }
982
983         prepare(0, f1, stb1.st_size);
984         prepare(1, f2, stb2.st_size);
985         prune();
986         sort(sfile[0], slen[0]);
987         sort(sfile[1], slen[1]);
988
989         member = (int *)file[1];
990         equiv(sfile[0], slen[0], sfile[1], slen[1], member);
991         member = xrealloc(member, (slen[1] + 2) * sizeof(int));
992
993         class = (int *)file[0];
994         unsort(sfile[0], slen[0], class);
995         class = xrealloc(class, (slen[0] + 2) * sizeof(int));
996
997         klist = xmalloc((slen[0] + 2) * sizeof(int));
998         clen = 0;
999         clistlen = 100;
1000         clist = xmalloc(clistlen * sizeof(struct cand));
1001         i = stone(class, slen[0], member, klist);
1002         free(member);
1003         free(class);
1004
1005         J = xrealloc(J, (len[0] + 2) * sizeof(int));
1006         unravel(klist[i]);
1007         free(clist);
1008         free(klist);
1009
1010         ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
1011         ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
1012         check(f1, f2);
1013         output(file1, f1, file2, f2);
1014
1015 closem:
1016         if (anychange) {
1017                 status |= 1;
1018                 if (rval == D_SAME)
1019                         rval = D_DIFFER;
1020         }
1021         if (f1 != NULL)
1022                 fclose(f1);
1023         if (f2 != NULL)
1024                 fclose(f2);
1025         if (file1 != ofile1)
1026                 free(file1);
1027         if (file2 != ofile2)
1028                 free(file2);
1029         return (rval);
1030 }
1031
1032 #if ENABLE_FEATURE_DIFF_DIR
1033 static void do_diff (char *dir1, char *path1, char *dir2, char *path2) {
1034         
1035         int flags = D_HEADER;
1036         int val;
1037         
1038         char *fullpath1 = bb_xasprintf("%s/%s", dir1, path1);
1039         char *fullpath2 = bb_xasprintf("%s/%s", dir2, path2);
1040
1041         if (stat(fullpath1, &stb1) != 0) {
1042                 flags |= D_EMPTY1;
1043                 memset(&stb1, 0, sizeof(stb1));
1044                 fullpath1 = bb_xasprintf("%s/%s", dir1, path2);
1045         }
1046         if (stat(fullpath2, &stb2) != 0) {
1047                 flags |= D_EMPTY2;
1048                 memset(&stb2, 0, sizeof(stb2));
1049                 stb2.st_mode = stb1.st_mode;
1050                 fullpath2 = bb_xasprintf("%s/%s", dir2, path1);
1051         }
1052
1053         if (stb1.st_mode == 0)
1054                 stb1.st_mode = stb2.st_mode;
1055         
1056         if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1057                 printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
1058                 return;
1059         }
1060
1061         if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
1062                 val = D_SKIPPED1;
1063         else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
1064                 val = D_SKIPPED2;
1065         else
1066                 val = diffreg(fullpath1, fullpath2, flags);
1067         
1068         print_status(val, fullpath1, fullpath2, NULL);
1069 }
1070 #endif
1071
1072 #if ENABLE_FEATURE_DIFF_DIR
1073 static int dir_strcmp(const void *p1, const void *p2) {
1074         return strcmp(*(char * const *)p1, *(char * const *)p2);
1075 }
1076
1077 /* This function adds a filename to dl, the directory listing. */
1078
1079 static int add_to_dirlist (const char *filename,
1080                 struct stat ATTRIBUTE_UNUSED *sb, void *userdata) {
1081         dl_count++;
1082         dl = xrealloc(dl, dl_count * sizeof(char *));
1083         dl[dl_count - 1] = bb_xstrdup(filename);
1084         if (cmd_flags & FLAG_r) {
1085                 int *pp = (int *) userdata;
1086                 int path_len = *pp + 1;
1087                 dl[dl_count - 1] = &(dl[dl_count - 1])[path_len];
1088         }
1089         return TRUE;
1090 }
1091
1092 /* This returns a sorted directory listing. */
1093 static char **get_dir(char *path) {
1094
1095         int i;
1096         char **retval;
1097
1098         /* If -r has been set, then the recursive_action function will be
1099          * used. Unfortunately, this outputs the root directory along with
1100          * the recursed paths, so use void *userdata to specify the string
1101          * length of the root directory. It can then be removed in
1102          * add_to_dirlist. */
1103
1104         int path_len = strlen(path);
1105         void *userdata = &path_len;
1106
1107         /* Reset dl_count - there's no need to free dl as bb_xrealloc does
1108          * the job nicely. */
1109         dl_count = 0;
1110
1111         /* Now fill dl with a listing. */
1112         if (cmd_flags & FLAG_r)
1113                 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL, userdata);
1114         else {
1115                 DIR *dp;
1116                 struct dirent *ep;
1117
1118                 dp = bb_opendir(path);
1119                 while ((ep = readdir(dp))) {
1120                         if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, ".")))
1121                                 continue;
1122                         add_to_dirlist(ep->d_name, NULL, NULL);
1123                 }
1124                 closedir(dp);
1125         }
1126
1127         /* Sort dl alphabetically. */
1128         qsort(dl, dl_count, sizeof(char *), dir_strcmp);
1129
1130         /* Copy dl so that we can return it. */
1131         retval = xmalloc(dl_count * sizeof(char *));
1132         for (i = 0; i < dl_count; i++)
1133                 retval[i] = bb_xstrdup(dl[i]);
1134
1135         return retval;
1136 }
1137
1138 static void diffdir (char *p1, char *p2) {
1139         
1140         char **dirlist1, **dirlist2;
1141         char *dp1, *dp2;
1142         int dirlist1_count, dirlist2_count;
1143         int pos;
1144
1145         /* Check for trailing slashes. */
1146         
1147         if (p1[strlen(p1) - 1] == '/')
1148                 p1[strlen(p1) - 1] = '\0';
1149         if (p2[strlen(p2) - 1] == '/')
1150                 p2[strlen(p2) - 1] = '\0';
1151         
1152         /* Get directory listings for p1 and p2. */
1153         
1154         dirlist1 = get_dir(p1);
1155         dirlist1_count = dl_count;
1156         dirlist1[dirlist1_count] = NULL;
1157         dirlist2 = get_dir(p2);
1158         dirlist2_count = dl_count;
1159         dirlist2[dirlist2_count] = NULL;
1160         
1161         /* If -S was set, find the starting point. */
1162         if (start) {
1163                 while (*dirlist1 != NULL && strcmp(*dirlist1, start) < 0)
1164                         dirlist1++;
1165                 while (*dirlist2 != NULL && strcmp(*dirlist2, start) < 0)
1166                         dirlist2++;
1167                 if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
1168                         bb_error_msg("Invalid argument to -S");
1169         }
1170         
1171         /* Now that both dirlist1 and dirlist2 contain sorted directory
1172          * listings, we can start to go through dirlist1. If both listings
1173          * contain the same file, then do a normal diff. Otherwise, behaviour
1174          * is determined by whether the -N flag is set. */      
1175         while (*dirlist1 != NULL || *dirlist2 != NULL) {
1176                 dp1 = *dirlist1;
1177                 dp2 = *dirlist2;
1178                 pos = dp1 == NULL ? 1 : dp2 == NULL ? -1 : strcmp(dp1, dp2);
1179                 if (pos == 0) {
1180                         do_diff(p1, dp1, p2, dp2);
1181                         dirlist1++;
1182                         dirlist2++;
1183                 }
1184                 else if (pos < 0) {
1185                         if (cmd_flags & FLAG_N)
1186                                 do_diff(p1, dp1, p2, NULL);
1187                         else
1188                                 print_only(p1, strlen(p1) + 1, dp1);
1189                         dirlist1++;
1190                 }
1191                 else {
1192                         if (cmd_flags & FLAG_N)
1193                                 do_diff(p1, NULL, p2, dp2);
1194                         else
1195                                 print_only(p2, strlen(p2) + 1, dp2);
1196                         dirlist2++;
1197                 }
1198         }
1199 }
1200 #endif
1201
1202
1203
1204 int diff_main(int argc, char **argv) {
1205         char *ep;
1206         int gotstdin = 0;
1207
1208         char *U_opt;
1209         llist_t *L_arg = NULL;
1210         bb_opt_complementally = "L::";
1211         cmd_flags = bb_getopt_ulflags(argc, argv, "abdiL:NqrsS:tTU:wu", &L_arg, &start, &U_opt);
1212
1213         if (cmd_flags & FLAG_L) {
1214                 while (L_arg) {
1215                         if (label[0] == NULL)
1216                                 label[0] = L_arg->data;
1217                         else if (label[1] == NULL)
1218                                 label[1] = L_arg->data;
1219                         else
1220                                 bb_show_usage();
1221
1222                         L_arg = L_arg->link;
1223                 }
1224
1225                 /* If both label[0] and label[1] were set, they need to be swapped. */
1226                 if (label[0] && label[1]) {
1227                         char *tmp;
1228                         tmp = label[1];
1229                         label[1] = label[0];
1230                         label[0] = tmp;
1231                 }
1232         }
1233
1234         context = 3;    /* This is the default number of lines of context. */
1235         if (cmd_flags & FLAG_U) {
1236                 context = strtol(U_opt, &ep, 10);
1237                 if (context == 0) {
1238                         bb_error_msg("Invalid context length");
1239                         bb_show_usage();
1240                 }
1241         }
1242         argc -= optind;
1243         argv += optind;
1244
1245         /*
1246          * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1247          * driver routine.  Both drivers use the contents of stb1 and stb2.
1248          */
1249         if (argc < 2) {
1250                 bb_error_msg("Missing filename");
1251                 bb_show_usage();
1252         }
1253         if (strcmp(argv[0], "-") == 0) {
1254                 fstat(STDIN_FILENO, &stb1);
1255                 gotstdin = 1;
1256         } else if (stat(argv[0], &stb1) != 0)
1257                 bb_perror_msg_and_die("Couldn't stat %s", argv[0]);
1258         if (strcmp(argv[1], "-") == 0) {
1259                 fstat(STDIN_FILENO, &stb2);
1260                 gotstdin = 1;
1261         } else if (stat(argv[1], &stb2) != 0)
1262                 bb_perror_msg_and_die("Couldn't stat %s", argv[1]);
1263         if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1264                 bb_error_msg_and_die("Can't compare - to a directory");
1265         if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1266 #if ENABLE_FEATURE_DIFF_DIR
1267                 diffdir(argv[0], argv[1]);
1268 #else
1269                 bb_error_msg_and_die("Directory comparison not supported");
1270 #endif
1271         }
1272         else {
1273                 if (S_ISDIR(stb1.st_mode)) {
1274                         argv[0] = concat_path_file(argv[0], argv[1]);
1275                         if (stat(argv[0], &stb1) < 0)
1276                                 bb_perror_msg_and_die("Couldn't stat %s", argv[0]);
1277                 }
1278                 if (S_ISDIR(stb2.st_mode)) {
1279                         argv[1] = concat_path_file(argv[1], argv[0]);
1280                         if (stat(argv[1], &stb2) < 0)
1281                                 bb_perror_msg_and_die("Couldn't stat %s", argv[1]);
1282                 }
1283                 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], NULL);
1284         }
1285         exit(status);
1286 }
1287