1 /* vi: set sw=4 ts=4: */
3 * Mini diff implementation for busybox, adapted from OpenBSD diff.
5 * Copyright (C) 2010 by Matheus Izvekov <mizvekov@gmail.com>
6 * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com>
7 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
9 * Sponsored in part by the Defense Advanced Research Projects
10 * Agency (DARPA) and Air Force Research Laboratory, Air Force
11 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 * The following code uses an algorithm due to Harold Stone,
18 * which finds a pair of longest identical subsequences in
21 * The major goal is to generate the match vector J.
22 * J[i] is the index of the line in file1 corresponding
23 * to line i in file0. J[i] = 0 if there is no
26 * Lines are hashed so as to work in core. All potential
27 * matches are located by sorting the lines of each file
28 * on the hash (called "value"). In particular, this
29 * collects the equivalence classes in file1 together.
30 * Subroutine equiv replaces the value of each line in
31 * file0 by the index of the first element of its
32 * matching equivalence in (the reordered) file1.
33 * To save space equiv squeezes file1 into a single
34 * array member in which the equivalence classes
35 * are simply concatenated, except that their first
36 * members are flagged by changing sign.
38 * Next the indices that point into member are unsorted into
39 * array class according to the original order of file0.
41 * The cleverness lies in routine stone. This marches
42 * through the lines of file0, developing a vector klist
43 * of "k-candidates". At step i a k-candidate is a matched
44 * pair of lines x,y (x in file0, y in file1) such that
45 * there is a common subsequence of length k
46 * between the first i lines of file0 and the first y
47 * lines of file1, but there is no such subsequence for
48 * any smaller y. x is the earliest possible mate to y
49 * that occurs in such a subsequence.
51 * Whenever any of the members of the equivalence class of
52 * lines in file1 matable to a line in file0 has serial number
53 * less than the y of some k-candidate, that k-candidate
54 * with the smallest such y is replaced. The new
55 * k-candidate is chained (via pred) to the current
56 * k-1 candidate so that the actual subsequence can
57 * be recovered. When a member has serial number greater
58 * that the y of all k-candidates, the klist is extended.
59 * At the end, the longest subsequence is pulled out
60 * and placed in the array J by unravel
62 * With J in hand, the matches there recorded are
63 * checked against reality to assure that no spurious
64 * matches have crept in due to hashing. If they have,
65 * they are broken, and "jackpot" is recorded--a harmless
66 * matter except that a true match for a spuriously
67 * mated line may now be unnecessarily reported as a change.
69 * Much of the complexity of the program comes simply
70 * from trying to minimize core utilization and
71 * maximize the range of doable problems by dynamically
72 * allocating what is needed and reusing what is not.
73 * The core requirements for problems larger than somewhat
74 * are (in words) 2*length(file0) + length(file1) +
75 * 3*(number of k-candidates installed), typically about
76 * 6n words for files of length n.
82 //#define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
84 #define dbg_error_msg(...) ((void)0)
87 enum { /* print_status() and diffreg() return values */
88 STATUS_SAME, /* files are the same */
89 STATUS_DIFFER, /* files differ */
90 STATUS_BINARY, /* binary files differ */
93 enum { /* Commandline flags */
109 #define FLAG(x) (1 << FLAG_##x)
111 /* We cache file position to avoid excessive seeking */
112 typedef struct FILE_and_pos_t {
118 smallint exit_status;
123 #define G (*ptr_to_globals)
124 #define exit_status (G.exit_status )
125 #define opt_U_context (G.opt_U_context )
126 #define label (G.label )
128 #define INIT_G() do { \
129 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
137 TOK_EMPTY = 1 << 9, /* Line fully processed, you can proceed to the next */
138 TOK_EOF = 1 << 10, /* File ended */
139 /* Private (Only to be used by read_token() */
140 TOK_EOL = 1 << 11, /* we saw EOL (sticky) */
141 TOK_SPACE = 1 << 12, /* used -b code, means we are skipping spaces */
142 SHIFT_EOF = (sizeof(token_t)*8 - 8) - 1,
143 CHAR_MASK = 0x1ff, /* 8th bit is used to distinguish EOF from 0xff */
146 /* Restores full EOF from one 8th bit: */
147 //#define TOK2CHAR(t) (((t) << SHIFT_EOF) >> SHIFT_EOF)
148 /* We don't really need the above, we only need to have EOF != any_real_char: */
149 #define TOK2CHAR(t) ((t) & CHAR_MASK)
151 static void seek_ft(FILE_and_pos_t *ft, off_t pos)
153 if (ft->ft_pos != pos) {
155 fseeko(ft->ft_fp, pos, SEEK_SET);
159 /* Reads tokens from given fp, handling -b and -w flags
160 * The user must reset tok every line start
162 static int read_token(FILE_and_pos_t *ft, token_t tok)
165 while (!(tok & TOK_EOL)) {
169 t = fgetc(ft->ft_fp);
172 is_space = (t == EOF || isspace(t));
174 /* If t == EOF (-1), set both TOK_EOF and TOK_EOL */
175 tok |= (t & (TOK_EOF + TOK_EOL));
180 if ((option_mask32 & FLAG(w)) && is_space)
183 /* Trim char value to low 9 bits */
186 if (option_mask32 & FLAG(b)) {
187 /* Was prev char whitespace? */
188 if (tok & TOK_SPACE) { /* yes */
189 if (is_space) /* this one too, ignore it */
192 } else if (is_space) {
193 /* 1st whitespace char.
194 * Set TOK_SPACE and replace char by ' ' */
199 tok &= ~(TOK_EMPTY + CHAR_MASK);
200 /* Assign char value (low 9 bits) and maybe set TOK_SPACE */
205 bb_error_msg("fp:%p tok:%x '%c'%s%s%s%s", fp, tok, tok & 0xff
206 , tok & TOK_EOF ? " EOF" : ""
207 , tok & TOK_EOL ? " EOL" : ""
208 , tok & TOK_EMPTY ? " EMPTY" : ""
209 , tok & TOK_SPACE ? " SPACE" : ""
221 static int search(const int *c, int k, int y, const struct cand *list)
223 if (list[c[k]].y < y) /* quick look for typical case */
226 for (int i = 0, j = k + 1;;) {
227 const int l = (i + j) >> 1;
229 const int t = list[c[l]].y;
241 static unsigned isqrt(unsigned n)
245 const unsigned y = x;
246 x = ((n / x) + x) >> 1;
247 if (x <= (y + 1) && x >= (y - 1))
252 static void stone(const int *a, int n, const int *b, int *J, int pref)
254 const unsigned isq = isqrt(n);
255 const unsigned bound =
256 (option_mask32 & FLAG(d)) ? UINT_MAX : MAX(256, isq);
260 struct cand *clist = xzalloc(clistlen * sizeof(clist[0]));
261 int *klist = xzalloc((n + 2) * sizeof(klist[0]));
262 /*clist[0] = (struct cand){0}; - xzalloc did it */
265 for (struct cand cand = {1}; cand.x <= n; cand.x++) {
266 int j = a[cand.x], oldl = 0;
267 unsigned numtries = 0;
271 cand.pred = klist[0];
274 if (cand.y <= clist[cand.pred].y)
276 l = search(klist, k, cand.y, clist);
278 cand.pred = klist[l - 1];
279 if (l <= k && clist[klist[l]].y <= cand.y)
281 if (clen == clistlen) {
282 clistlen = clistlen * 11 / 10;
283 clist = xrealloc(clist, clistlen * sizeof(clist[0]));
296 } while ((cand.y = b[++j]) > 0 && numtries < bound);
299 for (struct cand *q = clist + klist[k]; q->y; q = clist + q->pred)
300 J[q->x + pref] = q->y + pref;
306 /* 'serial' is not used in the begining, so we reuse it
307 * to store line offsets, thus reducing memory pressure
316 static void equiv(struct line *a, int n, struct line *b, int m, int *c)
320 while (i <= n && j <= m) {
321 if (a[i].value < b[j].value)
323 else if (a[i].value == b[j].value)
334 while (b[j + 1].value == b[j].value) {
342 static void unsort(const struct line *f, int l, int *b)
344 int *a = xmalloc((l + 1) * sizeof(a[0]));
345 for (int i = 1; i <= l; i++)
346 a[f[i].serial] = f[i].value;
347 for (int i = 1; i <= l; i++)
352 static int line_compar(const void *a, const void *b)
354 #define l0 ((const struct line*)a)
355 #define l1 ((const struct line*)b)
356 int r = l0->value - l1->value;
359 return l0->serial - l1->serial;
364 static void uni_range(int a, int b)
367 printf("%d,%d", a, b - a + 1);
374 static void fetch(FILE_and_pos_t *ft, const off_t *ix, int a, int b, int ch)
376 for (int i = a; i <= b; i++) {
377 seek_ft(ft, ix[i - 1]);
379 if (option_mask32 & FLAG(T))
381 for (int j = 0, col = 0; j < ix[i] - ix[i - 1]; j++) {
382 int c = fgetc(ft->ft_fp);
384 printf("\n\\ No newline at end of file\n");
388 if (c == '\t' && (option_mask32 & FLAG(t)))
389 do putchar(' '); while (++col & 7);
398 /* Creates the match vector J, where J[i] is the index
399 * of the line in the new file corresponding to the line i
400 * in the old file. Lines start at 1 instead of 0, that value
401 * being used instead to denote no corresponding line.
402 * This vector is dynamically allocated and must be freed by the caller.
404 * * fp is an input parameter, where fp[0] and fp[1] are the open
405 * old file and new file respectively.
406 * * nlen is an output variable, where nlen[0] and nlen[1]
407 * gets the number of lines in the old and new file respectively.
408 * * ix is an output variable, where ix[0] and ix[1] gets
409 * assigned dynamically allocated vectors of the offsets of the lines
410 * of the old and new file respectively. These must be freed by the caller.
412 static NOINLINE int *create_J(FILE_and_pos_t ft[2], int nlen[2], off_t *ix[2])
414 int *J, slen[2], *class, *member;
415 struct line *nfile[2], *sfile[2];
416 int pref = 0, suff = 0;
418 /* Lines of both files are hashed, and in the process
419 * their offsets are stored in the array ix[fileno]
420 * where fileno == 0 points to the old file, and
421 * fileno == 1 points to the new one.
423 for (int i = 0; i < 2; i++) {
427 nfile[i] = xmalloc((sz + 3) * sizeof(nfile[i][0]));
431 /* We could zalloc nfile, but then zalloc starts showing in gprof at ~1% */
432 nfile[i][0].offset = 0;
433 goto start; /* saves code */
435 tok = read_token(&ft[i], tok);
436 if (!(tok & TOK_EMPTY)) {
437 /* Hash algorithm taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578. */
438 /*hash = hash * 128 - hash + TOK2CHAR(tok);
439 * gcc insists on optimizing above to "hash * 127 + ...", thus... */
440 unsigned o = hash - TOK2CHAR(tok);
441 hash = hash * 128 - o; /* we want SPEED here */
444 if (nlen[i]++ == sz) {
446 nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
448 /* line_compar needs hashes fit into positive int */
449 nfile[i][nlen[i]].value = hash & INT_MAX;
450 /* like ftello(ft[i].ft_fp) but faster (avoids lseek syscall) */
451 nfile[i][nlen[i]].offset = ft[i].ft_pos;
453 /* EOF counts as a token, so we have to adjust it here */
454 nfile[i][nlen[i]].offset++;
460 /* Exclude lone EOF line from the end of the file, to make fetch()'s job easier */
461 if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
463 /* Now we copy the line offsets into ix */
464 ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
465 for (int j = 0; j < nlen[i] + 1; j++)
466 ix[i][j] = nfile[i][j].offset;
469 /* lenght of prefix and suffix is calculated */
470 for (; pref < nlen[0] && pref < nlen[1] &&
471 nfile[0][pref + 1].value == nfile[1][pref + 1].value;
473 for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
474 nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
476 /* Arrays are pruned by the suffix and prefix lenght,
477 * the result being sorted and stored in sfile[fileno],
478 * and their sizes are stored in slen[fileno]
480 for (int j = 0; j < 2; j++) {
481 sfile[j] = nfile[j] + pref;
482 slen[j] = nlen[j] - pref - suff;
483 for (int i = 0; i <= slen[j]; i++)
484 sfile[j][i].serial = i;
485 qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
487 /* nfile arrays are reused to reduce memory pressure
488 * The #if zeroed out section performs the same task as the
489 * one in the #else section.
490 * Peak memory usage is higher, but one array copy is avoided
491 * by not using unsort()
494 member = xmalloc((slen[1] + 2) * sizeof(member[0]));
495 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
498 class = xmalloc((slen[0] + 1) * sizeof(class[0]));
499 for (int i = 1; i <= slen[0]; i++) /* Unsorting */
500 class[sfile[0][i].serial] = sfile[0][i].value;
503 member = (int *)nfile[1];
504 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
505 member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
507 class = (int *)nfile[0];
508 unsort(sfile[0], slen[0], (int *)nfile[0]);
509 class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
511 J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
512 /* The elements of J which fall inside the prefix and suffix regions
513 * are marked as unchanged, while the ones which fall outside
514 * are initialized with 0 (no matches), so that function stone can
515 * then assign them their right values
517 for (int i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
518 J[i] = i <= pref ? i :
519 i > (nlen[0] - suff) ? (i + delta) : 0;
520 /* Here the magic is performed */
521 stone(class, slen[0], member, J, pref);
522 J[nlen[0] + 1] = nlen[1] + 1;
527 /* Both files are rescanned, in an effort to find any lines
528 * which, due to limitations intrinsic to any hashing algorithm,
529 * are different but ended up confounded as the same
531 for (int i = 1; i <= nlen[0]; i++) {
535 seek_ft(&ft[0], ix[0][i - 1]);
536 seek_ft(&ft[1], ix[1][J[i] - 1]);
538 for (int j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
539 token_t tok0 = 0, tok1 = 0;
541 tok0 = read_token(&ft[0], tok0);
542 tok1 = read_token(&ft[1], tok1);
544 if (((tok0 ^ tok1) & TOK_EMPTY) != 0 /* one is empty (not both) */
545 || (!(tok0 & TOK_EMPTY) && TOK2CHAR(tok0) != TOK2CHAR(tok1))
547 J[i] = 0; /* Break the correspondence */
549 } while (!(tok0 & tok1 & TOK_EMPTY));
557 * The following struct is used to record change information
558 * doing a "context" or "unified" diff.
561 int a; /* start line in old file */
562 int b; /* end line in old file */
563 int c; /* start line in new file */
564 int d; /* end line in new file */
567 static bool diff(FILE_and_pos_t ft[2], char *file[2])
571 int *J = create_J(ft, nlen, ix);
573 bool anychange = false;
574 struct context_vec *vec = NULL;
579 struct context_vec v;
581 for (v.a = i; v.a <= nlen[0] && J[v.a] == J[v.a - 1] + 1; v.a++)
583 v.c = J[v.a - 1] + 1;
585 for (v.b = v.a - 1; v.b < nlen[0] && !J[v.b + 1]; v.b++)
587 v.d = J[v.b + 1] - 1;
589 * Indicate that there is a difference between lines a and b of the 'from' file
590 * to get to lines c to d of the 'to' file. If a is greater than b then there
591 * are no lines in the 'from' file involved and this means that there were
592 * lines appended (beginning at b). If c is greater than d then there are
593 * lines missing from the 'to' file.
595 if (v.a <= v.b || v.c <= v.d) {
597 * If this change is more than 'context' lines from the
598 * previous change, dump the record and reset it.
601 && v.a > vec[idx].b + (2 * opt_U_context) + 1
602 && v.c > vec[idx].d + (2 * opt_U_context) + 1
606 vec = xrealloc_vector(vec, 6, ++idx);
617 if (!(option_mask32 & FLAG(q))) {
618 struct context_vec *cvp = vec;
619 int lowa = MAX(1, cvp->a - opt_U_context);
620 int upb = MIN(nlen[0], vec[idx].b + opt_U_context);
621 int lowc = MAX(1, cvp->c - opt_U_context);
622 int upd = MIN(nlen[1], vec[idx].d + opt_U_context);
625 /* Print the context/unidiff header first time through */
626 printf("--- %s\n", label[0] ?: file[0]);
627 printf("+++ %s\n", label[1] ?: file[1]);
631 uni_range(lowa, upb);
633 uni_range(lowc, upd);
637 * Output changes in "unified" diff format--the old and new lines
638 * are printed together.
641 bool end = cvp > &vec[idx];
642 fetch(&ft[0], ix[0], lowa, end ? upb : cvp->a - 1, ' ');
645 fetch(&ft[0], ix[0], cvp->a, cvp->b, '-');
646 fetch(&ft[1], ix[1], cvp->c, cvp->d, '+');
652 } while (i <= nlen[0]);
661 static int diffreg(char *file[2])
663 FILE_and_pos_t ft[2];
664 bool binary = false, differ = false;
665 int status = STATUS_SAME;
667 for (int i = 0; i < 2; i++) {
668 int fd = open_or_warn_stdin(file[i]);
671 /* Our diff implementation is using seek.
672 * When we meet non-seekable file, we must make a temp copy.
675 if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
676 char name[] = "/tmp/difXXXXXX";
677 int fd_tmp = mkstemp(name);
679 bb_perror_msg_and_die("mkstemp");
681 ft[i].ft_pos = bb_copyfd_eof(fd, fd_tmp);
682 /* error message is printed by bb_copyfd_eof */
683 if (ft[i].ft_pos < 0)
686 if (fd) /* Prevents closing of stdin */
690 ft[i].ft_fp = fdopen(fd, "r");
694 const size_t sz = COMMON_BUFSIZE / 2;
695 char *const buf0 = bb_common_bufsiz1;
696 char *const buf1 = buf0 + sz;
698 i = fread(buf0, 1, sz, ft[0].ft_fp);
700 j = fread(buf1, 1, sz, ft[1].ft_fp);
708 for (int k = 0; k < i; k++) {
709 if (!buf0[k] || !buf1[k])
711 if (buf0[k] != buf1[k])
716 if (binary && !(option_mask32 & FLAG(a)))
717 status = STATUS_BINARY;
718 else if (diff(ft, file))
719 status = STATUS_DIFFER;
721 if (status != STATUS_SAME)
724 fclose_if_not_stdin(ft[0].ft_fp);
725 fclose_if_not_stdin(ft[1].ft_fp);
730 static void print_status(int status, char *path[2])
735 if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
736 printf("Files %s and %s differ\n", path[0], path[1]);
739 if (option_mask32 & FLAG(s))
740 printf("Files %s and %s are identical\n", path[0], path[1]);
745 #if ENABLE_FEATURE_DIFF_DIR
752 /* This function adds a filename to dl, the directory listing. */
753 static int FAST_FUNC add_to_dirlist(const char *filename,
754 struct stat *sb UNUSED_PARAM,
755 void *userdata, int depth UNUSED_PARAM)
757 struct dlist *const l = userdata;
758 l->dl = xrealloc_vector(l->dl, 6, l->e);
759 /* + 1 skips "/" after dirname */
760 l->dl[l->e] = xstrdup(filename + l->len + 1);
765 /* If recursion is not set, this function adds the directory
766 * to the list and prevents recursive_action from recursing into it.
768 static int FAST_FUNC skip_dir(const char *filename,
769 struct stat *sb, void *userdata,
772 if (!(option_mask32 & FLAG(r)) && depth) {
773 add_to_dirlist(filename, sb, userdata, depth);
779 static void diffdir(char *p[2], const char *s_start)
781 struct dlist list[2];
783 memset(&list, 0, sizeof(list));
784 for (int i = 0; i < 2; i++) {
785 /*list[i].s = list[i].e = 0; - memset did it */
786 /*list[i].dl = NULL; */
788 /* We need to trim root directory prefix.
789 * Using list.len to specify its length,
790 * add_to_dirlist will remove it. */
791 list[i].len = strlen(p[i]);
792 recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
793 add_to_dirlist, skip_dir, &list[i], 0);
794 /* Sort dl alphabetically.
795 * GNU diff does this ignoring any number of trailing dots.
796 * We don't, so for us dotted files almost always are
799 qsort_string_vector(list[i].dl, list[i].e);
800 /* If -S was set, find the starting point. */
803 while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
806 /* Now that both dirlist1 and dirlist2 contain sorted directory
807 * listings, we can start to go through dirlist1. If both listings
808 * contain the same file, then do a normal diff. Otherwise, behaviour
809 * is determined by whether the -N flag is set. */
815 dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
816 dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
817 if (!dp[0] && !dp[1])
819 pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
821 if (pos && !(option_mask32 & FLAG(N)))
822 printf("Only in %s: %s\n", p[k], dp[k]);
824 char *fullpath[2], *path[2]; /* if -N */
826 for (int i = 0; i < 2; i++) {
827 if (pos == 0 || i == k) {
828 path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
829 stat(fullpath[i], &stb[i]);
831 fullpath[i] = concat_path_file(p[i], dp[1 - i]);
832 path[i] = (char *)bb_dev_null;
836 stat(fullpath[k], &stb[1 - k]);
838 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
839 printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
840 else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
841 printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
842 else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
843 printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
844 else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
845 if (S_ISDIR(stb[0].st_mode))
846 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
848 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
850 print_status(diffreg(path), fullpath);
862 if (ENABLE_FEATURE_CLEAN_UP) {
869 int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
870 int diff_main(int argc UNUSED_PARAM, char **argv)
873 char *file[2], *s_start = NULL;
874 llist_t *L_arg = NULL;
878 /* exactly 2 params; collect multiple -L <label>; -U N */
879 opt_complementary = "=2:L::U+";
880 getopt32(argv, "abdiL:NqrsS:tTU:wu"
881 "p" /* ignored (for compatibility) */,
882 &L_arg, &s_start, &opt_U_context);
885 if (label[0] && label[1])
887 if (label[0]) /* then label[1] is NULL */
889 label[0] = llist_pop(&L_arg);
891 xfunc_error_retval = 2;
892 for (int i = 0; i < 2; i++) {
894 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
895 if (LONE_DASH(file[i])) {
896 fstat(STDIN_FILENO, &stb[i]);
899 xstat(file[i], &stb[i]);
901 xfunc_error_retval = 1;
902 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
903 bb_error_msg_and_die("can't compare stdin to a directory");
905 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
906 #if ENABLE_FEATURE_DIFF_DIR
907 diffdir(file, s_start);
909 bb_error_msg_and_die("no support for directory comparison");
912 bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
913 bool dir = S_ISDIR(stb[1].st_mode);
915 const char *slash = strrchr(file[!dir], '/');
916 file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
917 xstat(file[dir], &stb[dir]);
919 /* diffreg can get non-regular files here */
920 print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);