1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#include "libbb.h"
80
81#if 0
82
83#else
84#define dbg_error_msg(...) ((void)0)
85#endif
86
87enum {
88 STATUS_SAME,
89 STATUS_DIFFER,
90 STATUS_BINARY,
91};
92
93enum {
94 FLAG_a,
95 FLAG_b,
96 FLAG_d,
97 FLAG_i,
98 FLAG_L,
99 FLAG_N,
100 FLAG_q,
101 FLAG_r,
102 FLAG_s,
103 FLAG_S,
104 FLAG_t,
105 FLAG_T,
106 FLAG_U,
107 FLAG_w,
108 FLAG_u,
109 FLAG_p,
110 FLAG_B,
111 FLAG_E,
112};
113#define FLAG(x) (1 << FLAG_##x)
114
115
116typedef struct FILE_and_pos_t {
117 FILE *ft_fp;
118 off_t ft_pos;
119} FILE_and_pos_t;
120
121struct globals {
122 smallint exit_status;
123 int opt_U_context;
124 char *label[2];
125 struct stat stb[2];
126};
127#define G (*ptr_to_globals)
128#define exit_status (G.exit_status )
129#define opt_U_context (G.opt_U_context )
130#define label (G.label )
131#define stb (G.stb )
132#define INIT_G() do { \
133 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
134 opt_U_context = 3; \
135} while (0)
136
137typedef int token_t;
138
139enum {
140
141 TOK_EMPTY = 1 << 9,
142 TOK_EOF = 1 << 10,
143
144 TOK_EOL = 1 << 11,
145 TOK_SPACE = 1 << 12,
146 SHIFT_EOF = (sizeof(token_t)*8 - 8) - 1,
147 CHAR_MASK = 0x1ff,
148};
149
150
151
152
153#define TOK2CHAR(t) ((t) & CHAR_MASK)
154
155static void seek_ft(FILE_and_pos_t *ft, off_t pos)
156{
157 if (ft->ft_pos != pos) {
158 ft->ft_pos = pos;
159 fseeko(ft->ft_fp, pos, SEEK_SET);
160 }
161}
162
163
164
165
166static int read_token(FILE_and_pos_t *ft, token_t tok)
167{
168 tok |= TOK_EMPTY;
169 while (!(tok & TOK_EOL)) {
170 bool is_space;
171 int t;
172
173 t = fgetc(ft->ft_fp);
174 if (t != EOF)
175 ft->ft_pos++;
176 is_space = (t == EOF || isspace(t));
177
178
179 tok |= (t & (TOK_EOF + TOK_EOL));
180
181 if (t == '\n')
182 tok |= TOK_EOL;
183
184 if (option_mask32 & FLAG(i))
185 t = (t >= 'A' && t <= 'Z') ? t - ('A' - 'a') : t;
186
187 if ((option_mask32 & FLAG(w)) && is_space)
188 continue;
189
190
191 t &= CHAR_MASK;
192
193 if (option_mask32 & FLAG(b)) {
194
195 if (tok & TOK_SPACE) {
196 if (is_space)
197 continue;
198 tok &= ~TOK_SPACE;
199 } else if (is_space) {
200
201
202 t = TOK_SPACE + ' ';
203 }
204 }
205
206 tok &= ~(TOK_EMPTY + CHAR_MASK);
207
208 tok |= t;
209 break;
210 }
211#if 0
212 bb_error_msg("fp:%p tok:%x '%c'%s%s%s%s", fp, tok, tok & 0xff
213 , tok & TOK_EOF ? " EOF" : ""
214 , tok & TOK_EOL ? " EOL" : ""
215 , tok & TOK_EMPTY ? " EMPTY" : ""
216 , tok & TOK_SPACE ? " SPACE" : ""
217 );
218#endif
219 return tok;
220}
221
222struct cand {
223 int x;
224 int y;
225 int pred;
226};
227
228static int search(const int *c, int k, int y, const struct cand *list)
229{
230 int i, j;
231
232 if (list[c[k]].y < y)
233 return k + 1;
234
235 for (i = 0, j = k + 1;;) {
236 const int l = (i + j) >> 1;
237 if (l > i) {
238 const int t = list[c[l]].y;
239 if (t > y)
240 j = l;
241 else if (t < y)
242 i = l;
243 else
244 return l;
245 } else
246 return l + 1;
247 }
248}
249
250static unsigned isqrt(unsigned n)
251{
252 unsigned x = 1;
253 while (1) {
254 const unsigned y = x;
255 x = ((n / x) + x) >> 1;
256 if (x <= (y + 1) && x >= (y - 1))
257 return x;
258 }
259}
260
261static void stone(const int *a, int n, const int *b, int *J, int pref)
262{
263 const unsigned isq = isqrt(n);
264 const unsigned bound =
265 (option_mask32 & FLAG(d)) ? UINT_MAX : MAX(256, isq);
266 int clen = 1;
267 int clistlen = 100;
268 int k = 0;
269 struct cand *clist = xzalloc(clistlen * sizeof(clist[0]));
270 struct cand cand;
271 struct cand *q;
272 int *klist = xzalloc((n + 2) * sizeof(klist[0]));
273
274
275
276 for (cand.x = 1; cand.x <= n; cand.x++) {
277 int j = a[cand.x], oldl = 0;
278 unsigned numtries = 0;
279 if (j == 0)
280 continue;
281 cand.y = -b[j];
282 cand.pred = klist[0];
283 do {
284 int l, tc;
285 if (cand.y <= clist[cand.pred].y)
286 continue;
287 l = search(klist, k, cand.y, clist);
288 if (l != oldl + 1)
289 cand.pred = klist[l - 1];
290 if (l <= k && clist[klist[l]].y <= cand.y)
291 continue;
292 if (clen == clistlen) {
293 clistlen = clistlen * 11 / 10;
294 clist = xrealloc(clist, clistlen * sizeof(clist[0]));
295 }
296 clist[clen] = cand;
297 tc = klist[l];
298 klist[l] = clen++;
299 if (l <= k) {
300 cand.pred = tc;
301 oldl = l;
302 numtries++;
303 } else {
304 k++;
305 break;
306 }
307 } while ((cand.y = b[++j]) > 0 && numtries < bound);
308 }
309
310 for (q = clist + klist[k]; q->y; q = clist + q->pred)
311 J[q->x + pref] = q->y + pref;
312 free(klist);
313 free(clist);
314}
315
316struct line {
317
318
319
320 union {
321 unsigned serial;
322 off_t offset;
323 };
324 unsigned value;
325};
326
327static void equiv(struct line *a, int n, struct line *b, int m, int *c)
328{
329 int i = 1, j = 1;
330
331 while (i <= n && j <= m) {
332 if (a[i].value < b[j].value)
333 a[i++].value = 0;
334 else if (a[i].value == b[j].value)
335 a[i++].value = j;
336 else
337 j++;
338 }
339 while (i <= n)
340 a[i++].value = 0;
341 b[m + 1].value = 0;
342 j = 0;
343 while (++j <= m) {
344 c[j] = -b[j].serial;
345 while (b[j + 1].value == b[j].value) {
346 j++;
347 c[j] = b[j].serial;
348 }
349 }
350 c[j] = -1;
351}
352
353static void unsort(const struct line *f, int l, int *b)
354{
355 int i;
356 int *a = xmalloc((l + 1) * sizeof(a[0]));
357 for (i = 1; i <= l; i++)
358 a[f[i].serial] = f[i].value;
359 for (i = 1; i <= l; i++)
360 b[i] = a[i];
361 free(a);
362}
363
364static int line_compar(const void *a, const void *b)
365{
366#define l0 ((const struct line*)a)
367#define l1 ((const struct line*)b)
368 int r = l0->value - l1->value;
369 if (r)
370 return r;
371 return l0->serial - l1->serial;
372#undef l0
373#undef l1
374}
375
376static void fetch(FILE_and_pos_t *ft, const off_t *ix, int a, int b, int ch)
377{
378 int i, j, col;
379 for (i = a; i <= b; i++) {
380 seek_ft(ft, ix[i - 1]);
381 putchar(ch);
382 if (option_mask32 & FLAG(T))
383 putchar('\t');
384 for (j = 0, col = 0; j < ix[i] - ix[i - 1]; j++) {
385 int c = fgetc(ft->ft_fp);
386 if (c == EOF) {
387 printf("\n\\ No newline at end of file\n");
388 return;
389 }
390 ft->ft_pos++;
391 if (c == '\t' && (option_mask32 & FLAG(t)))
392 do putchar(' '); while (++col & 7);
393 else {
394 putchar(c);
395 col++;
396 }
397 }
398 }
399}
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415static NOINLINE int *create_J(FILE_and_pos_t ft[2], int nlen[2], off_t *ix[2])
416{
417 int *J, slen[2], *class, *member;
418 struct line *nfile[2], *sfile[2];
419 int pref = 0, suff = 0, i, j, delta;
420
421
422
423
424
425
426 for (i = 0; i < 2; i++) {
427 unsigned hash;
428 token_t tok;
429 size_t sz = 100;
430 nfile[i] = xmalloc((sz + 3) * sizeof(nfile[i][0]));
431
432 ft[i].ft_pos = 0;
433 fseeko(ft[i].ft_fp, 0, SEEK_SET);
434
435 nlen[i] = 0;
436
437 nfile[i][0].offset = 0;
438 goto start;
439 while (1) {
440 tok = read_token(&ft[i], tok);
441 if (!(tok & TOK_EMPTY)) {
442
443
444
445 unsigned o = hash - TOK2CHAR(tok);
446 hash = hash * 128 - o;
447 continue;
448 }
449 if (nlen[i]++ == sz) {
450 sz = sz * 3 / 2;
451 nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
452 }
453
454 nfile[i][nlen[i]].value = hash & INT_MAX;
455
456 nfile[i][nlen[i]].offset = ft[i].ft_pos;
457 if (tok & TOK_EOF) {
458
459 nfile[i][nlen[i]].offset++;
460 break;
461 }
462start:
463 hash = tok = 0;
464 }
465
466 if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
467 nlen[i]--;
468
469 ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
470 for (j = 0; j < nlen[i] + 1; j++)
471 ix[i][j] = nfile[i][j].offset;
472 }
473
474
475 for (; pref < nlen[0] && pref < nlen[1] &&
476 nfile[0][pref + 1].value == nfile[1][pref + 1].value;
477 pref++);
478 for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
479 nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
480 suff++);
481
482
483
484
485 for (j = 0; j < 2; j++) {
486 sfile[j] = nfile[j] + pref;
487 slen[j] = nlen[j] - pref - suff;
488 for (i = 0; i <= slen[j]; i++)
489 sfile[j][i].serial = i;
490 qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
491 }
492
493
494
495
496
497
498#if 0
499 member = xmalloc((slen[1] + 2) * sizeof(member[0]));
500 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
501 free(nfile[1]);
502
503 class = xmalloc((slen[0] + 1) * sizeof(class[0]));
504 for (i = 1; i <= slen[0]; i++)
505 class[sfile[0][i].serial] = sfile[0][i].value;
506 free(nfile[0]);
507#else
508 member = (int *)nfile[1];
509 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
510 member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
511
512 class = (int *)nfile[0];
513 unsort(sfile[0], slen[0], (int *)nfile[0]);
514 class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
515#endif
516 J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
517
518
519
520
521
522 for (i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
523 J[i] = i <= pref ? i :
524 i > (nlen[0] - suff) ? (i + delta) : 0;
525
526 stone(class, slen[0], member, J, pref);
527 J[nlen[0] + 1] = nlen[1] + 1;
528
529 free(class);
530 free(member);
531
532
533
534
535
536 for (i = 1; i <= nlen[0]; i++) {
537 if (!J[i])
538 continue;
539
540 seek_ft(&ft[0], ix[0][i - 1]);
541 seek_ft(&ft[1], ix[1][J[i] - 1]);
542
543 for (j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
544 token_t tok0 = 0, tok1 = 0;
545 do {
546 tok0 = read_token(&ft[0], tok0);
547 tok1 = read_token(&ft[1], tok1);
548
549 if (((tok0 ^ tok1) & TOK_EMPTY) != 0
550 || (!(tok0 & TOK_EMPTY) && TOK2CHAR(tok0) != TOK2CHAR(tok1))
551 ) {
552 J[i] = 0;
553 }
554 } while (!(tok0 & tok1 & TOK_EMPTY));
555 }
556 }
557
558 return J;
559}
560
561static bool diff(FILE* fp[2], char *file[2])
562{
563 int nlen[2];
564 off_t *ix[2];
565 FILE_and_pos_t ft[2];
566 typedef struct { int a, b; } vec_t[2];
567 vec_t *vec = NULL;
568 int i = 1, j, k, idx = -1;
569 bool anychange = false;
570 int *J;
571
572 ft[0].ft_fp = fp[0];
573 ft[1].ft_fp = fp[1];
574
575
576 J = create_J(ft, nlen, ix);
577
578 do {
579 bool nonempty = false;
580
581 while (1) {
582 vec_t v;
583
584 for (v[0].a = i; v[0].a <= nlen[0] && J[v[0].a] == J[v[0].a - 1] + 1; v[0].a++)
585 continue;
586 v[1].a = J[v[0].a - 1] + 1;
587
588 for (v[0].b = v[0].a - 1; v[0].b < nlen[0] && !J[v[0].b + 1]; v[0].b++)
589 continue;
590 v[1].b = J[v[0].b + 1] - 1;
591
592
593
594
595
596
597
598 if (v[0].a <= v[0].b || v[1].a <= v[1].b) {
599
600
601
602
603 int ct = (2 * opt_U_context) + 1;
604 if (idx >= 0
605 && v[0].a > vec[idx][0].b + ct
606 && v[1].a > vec[idx][1].b + ct
607 ) {
608 break;
609 }
610
611 for (j = 0; j < 2; j++)
612 for (k = v[j].a; k < v[j].b; k++)
613 nonempty |= (ix[j][k+1] - ix[j][k] != 1);
614
615 vec = xrealloc_vector(vec, 6, ++idx);
616 memcpy(vec[idx], v, sizeof(v));
617 }
618
619 i = v[0].b + 1;
620 if (i > nlen[0])
621 break;
622 J[v[0].b] = v[1].b;
623 }
624 if (idx < 0 || ((option_mask32 & FLAG(B)) && !nonempty))
625 goto cont;
626 if (!(option_mask32 & FLAG(q))) {
627 int lowa;
628 vec_t span, *cvp = vec;
629
630 if (!anychange) {
631
632 printf("--- %s\n", label[0] ? label[0] : file[0]);
633 printf("+++ %s\n", label[1] ? label[1] : file[1]);
634 }
635
636 printf("@@");
637 for (j = 0; j < 2; j++) {
638 int a = span[j].a = MAX(1, (*cvp)[j].a - opt_U_context);
639 int b = span[j].b = MIN(nlen[j], vec[idx][j].b + opt_U_context);
640
641 printf(" %c%d", j ? '+' : '-', MIN(a, b));
642 if (a == b)
643 continue;
644 printf(",%d", (a < b) ? b - a + 1 : 0);
645 }
646 printf(" @@\n");
647
648
649
650
651 for (lowa = span[0].a; ; lowa = (*cvp++)[0].b + 1) {
652 bool end = cvp > &vec[idx];
653 fetch(&ft[0], ix[0], lowa, end ? span[0].b : (*cvp)[0].a - 1, ' ');
654 if (end)
655 break;
656 for (j = 0; j < 2; j++)
657 fetch(&ft[j], ix[j], (*cvp)[j].a, (*cvp)[j].b, j ? '+' : '-');
658 }
659 }
660 anychange = true;
661 cont:
662 idx = -1;
663 } while (i <= nlen[0]);
664
665 free(vec);
666 free(ix[0]);
667 free(ix[1]);
668 free(J);
669 return anychange;
670}
671
672static int diffreg(char *file[2])
673{
674 FILE *fp[2] = { stdin, stdin };
675 bool binary = false, differ = false;
676 int status = STATUS_SAME, i;
677
678 for (i = 0; i < 2; i++) {
679 int fd = open_or_warn_stdin(file[i]);
680 if (fd == -1)
681 goto out;
682
683
684
685 if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
686 char name[] = "/tmp/difXXXXXX";
687 int fd_tmp = mkstemp(name);
688 if (fd_tmp < 0)
689 bb_perror_msg_and_die("mkstemp");
690 unlink(name);
691 if (bb_copyfd_eof(fd, fd_tmp) < 0)
692 xfunc_die();
693 if (fd)
694 close(fd);
695 fd = fd_tmp;
696 }
697 fp[i] = fdopen(fd, "r");
698 }
699
700 while (1) {
701 const size_t sz = COMMON_BUFSIZE / 2;
702 char *const buf0 = bb_common_bufsiz1;
703 char *const buf1 = buf0 + sz;
704 int j, k;
705 i = fread(buf0, 1, sz, fp[0]);
706 j = fread(buf1, 1, sz, fp[1]);
707 if (i != j) {
708 differ = true;
709 i = MIN(i, j);
710 }
711 if (i == 0)
712 break;
713 for (k = 0; k < i; k++) {
714 if (!buf0[k] || !buf1[k])
715 binary = true;
716 if (buf0[k] != buf1[k])
717 differ = true;
718 }
719 }
720 if (differ) {
721 if (binary && !(option_mask32 & FLAG(a)))
722 status = STATUS_BINARY;
723 else if (diff(fp, file))
724 status = STATUS_DIFFER;
725 }
726 if (status != STATUS_SAME)
727 exit_status |= 1;
728out:
729 fclose_if_not_stdin(fp[0]);
730 fclose_if_not_stdin(fp[1]);
731
732 return status;
733}
734
735static void print_status(int status, char *path[2])
736{
737 switch (status) {
738 case STATUS_BINARY:
739 case STATUS_DIFFER:
740 if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
741 printf("Files %s and %s differ\n", path[0], path[1]);
742 break;
743 case STATUS_SAME:
744 if (option_mask32 & FLAG(s))
745 printf("Files %s and %s are identical\n", path[0], path[1]);
746 break;
747 }
748}
749
750#if ENABLE_FEATURE_DIFF_DIR
751struct dlist {
752 size_t len;
753 int s, e;
754 char **dl;
755};
756
757
758static int FAST_FUNC add_to_dirlist(const char *filename,
759 struct stat *sb UNUSED_PARAM,
760 void *userdata, int depth UNUSED_PARAM)
761{
762 struct dlist *const l = userdata;
763 l->dl = xrealloc_vector(l->dl, 6, l->e);
764
765 l->dl[l->e] = xstrdup(filename + l->len + 1);
766 l->e++;
767 return TRUE;
768}
769
770
771
772
773static int FAST_FUNC skip_dir(const char *filename,
774 struct stat *sb, void *userdata,
775 int depth)
776{
777 if (!(option_mask32 & FLAG(r)) && depth) {
778 add_to_dirlist(filename, sb, userdata, depth);
779 return SKIP;
780 }
781 return TRUE;
782}
783
784static void diffdir(char *p[2], const char *s_start)
785{
786 struct dlist list[2];
787 int i;
788
789 memset(&list, 0, sizeof(list));
790 for (i = 0; i < 2; i++) {
791
792
793
794
795
796
797 list[i].len = strlen(p[i]);
798 recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
799 add_to_dirlist, skip_dir, &list[i], 0);
800
801
802
803
804
805 qsort_string_vector(list[i].dl, list[i].e);
806
807 if (!s_start)
808 continue;
809 while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
810 list[i].s++;
811 }
812
813
814
815
816 while (1) {
817 char *dp[2];
818 int pos;
819 int k;
820
821 dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
822 dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
823 if (!dp[0] && !dp[1])
824 break;
825 pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
826 k = pos > 0;
827 if (pos && !(option_mask32 & FLAG(N)))
828 printf("Only in %s: %s\n", p[k], dp[k]);
829 else {
830 char *fullpath[2], *path[2];
831
832 for (i = 0; i < 2; i++) {
833 if (pos == 0 || i == k) {
834 path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
835 stat(fullpath[i], &stb[i]);
836 } else {
837 fullpath[i] = concat_path_file(p[i], dp[1 - i]);
838 path[i] = (char *)bb_dev_null;
839 }
840 }
841 if (pos)
842 stat(fullpath[k], &stb[1 - k]);
843
844 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
845 printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
846 else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
847 printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
848 else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
849 printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
850 else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
851 if (S_ISDIR(stb[0].st_mode))
852 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
853 else
854 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
855 } else
856 print_status(diffreg(path), fullpath);
857
858 free(fullpath[0]);
859 free(fullpath[1]);
860 }
861 free(dp[k]);
862 list[k].s++;
863 if (pos == 0) {
864 free(dp[1 - k]);
865 list[1 - k].s++;
866 }
867 }
868 if (ENABLE_FEATURE_CLEAN_UP) {
869 free(list[0].dl);
870 free(list[1].dl);
871 }
872}
873#endif
874
875#if ENABLE_FEATURE_DIFF_LONG_OPTIONS
876static const char diff_longopts[] ALIGN1 =
877 "ignore-case\0" No_argument "i"
878 "ignore-tab-expansion\0" No_argument "E"
879 "ignore-space-change\0" No_argument "b"
880 "ignore-all-space\0" No_argument "w"
881 "ignore-blank-lines\0" No_argument "B"
882 "text\0" No_argument "a"
883 "unified\0" Required_argument "U"
884 "label\0" Required_argument "L"
885 "show-c-function\0" No_argument "p"
886 "brief\0" No_argument "q"
887 "expand-tabs\0" No_argument "t"
888 "initial-tab\0" No_argument "T"
889 "recursive\0" No_argument "r"
890 "new-file\0" No_argument "N"
891 "report-identical-files\0" No_argument "s"
892 "starting-file\0" Required_argument "S"
893 "minimal\0" No_argument "d"
894 ;
895#endif
896
897int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
898int diff_main(int argc UNUSED_PARAM, char **argv)
899{
900 int gotstdin = 0, i;
901 char *file[2], *s_start = NULL;
902 llist_t *L_arg = NULL;
903
904 INIT_G();
905
906
907 opt_complementary = "=2:L::U+";
908#if ENABLE_FEATURE_DIFF_LONG_OPTIONS
909 applet_long_options = diff_longopts;
910#endif
911 getopt32(argv, "abdiL:NqrsS:tTU:wupBE",
912 &L_arg, &s_start, &opt_U_context);
913 argv += optind;
914 while (L_arg)
915 label[!!label[0]] = llist_pop(&L_arg);
916 xfunc_error_retval = 2;
917 for (i = 0; i < 2; i++) {
918 file[i] = argv[i];
919
920 if (LONE_DASH(file[i])) {
921 fstat(STDIN_FILENO, &stb[i]);
922 gotstdin++;
923 } else
924 xstat(file[i], &stb[i]);
925 }
926 xfunc_error_retval = 1;
927 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
928 bb_error_msg_and_die("can't compare stdin to a directory");
929
930 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
931#if ENABLE_FEATURE_DIFF_DIR
932 diffdir(file, s_start);
933#else
934 bb_error_msg_and_die("no support for directory comparison");
935#endif
936 } else {
937 bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
938 bool dir = S_ISDIR(stb[1].st_mode);
939 if (dirfile) {
940 const char *slash = strrchr(file[!dir], '/');
941 file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
942 xstat(file[dir], &stb[dir]);
943 }
944
945 print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);
946
947 if (dirfile)
948 free(file[dir]);
949 }
950
951 return exit_status;
952}
953