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 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);
441 if (nlen[i]++ == sz) {
443 nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
445 /* line_compar needs hashes fit into positive int */
446 nfile[i][nlen[i]].value = hash & INT_MAX;
447 /* like ftello(ft[i].ft_fp) but faster (avoids lseek syscall) */
448 nfile[i][nlen[i]].offset = ft[i].ft_pos;
450 /* EOF counts as a token, so we have to adjust it here */
451 nfile[i][nlen[i]].offset++;
457 /* Exclude lone EOF line from the end of the file, to make fetch()'s job easier */
458 if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
460 /* Now we copy the line offsets into ix */
461 ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
462 for (int j = 0; j < nlen[i] + 1; j++)
463 ix[i][j] = nfile[i][j].offset;
466 /* lenght of prefix and suffix is calculated */
467 for (; pref < nlen[0] && pref < nlen[1] &&
468 nfile[0][pref + 1].value == nfile[1][pref + 1].value;
470 for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
471 nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
473 /* Arrays are pruned by the suffix and prefix lenght,
474 * the result being sorted and stored in sfile[fileno],
475 * and their sizes are stored in slen[fileno]
477 for (int j = 0; j < 2; j++) {
478 sfile[j] = nfile[j] + pref;
479 slen[j] = nlen[j] - pref - suff;
480 for (int i = 0; i <= slen[j]; i++)
481 sfile[j][i].serial = i;
482 qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
484 /* nfile arrays are reused to reduce memory pressure
485 * The #if zeroed out section performs the same task as the
486 * one in the #else section.
487 * Peak memory usage is higher, but one array copy is avoided
488 * by not using unsort()
491 member = xmalloc((slen[1] + 2) * sizeof(member[0]));
492 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
495 class = xmalloc((slen[0] + 1) * sizeof(class[0]));
496 for (int i = 1; i <= slen[0]; i++) /* Unsorting */
497 class[sfile[0][i].serial] = sfile[0][i].value;
500 member = (int *)nfile[1];
501 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
502 member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
504 class = (int *)nfile[0];
505 unsort(sfile[0], slen[0], (int *)nfile[0]);
506 class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
508 J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
509 /* The elements of J which fall inside the prefix and suffix regions
510 * are marked as unchanged, while the ones which fall outside
511 * are initialized with 0 (no matches), so that function stone can
512 * then assign them their right values
514 for (int i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
515 J[i] = i <= pref ? i :
516 i > (nlen[0] - suff) ? (i + delta) : 0;
517 /* Here the magic is performed */
518 stone(class, slen[0], member, J, pref);
519 J[nlen[0] + 1] = nlen[1] + 1;
524 /* Both files are rescanned, in an effort to find any lines
525 * which, due to limitations intrinsic to any hashing algorithm,
526 * are different but ended up confounded as the same
528 for (int i = 1; i <= nlen[0]; i++) {
532 seek_ft(&ft[0], ix[0][i - 1]);
533 seek_ft(&ft[1], ix[1][J[i] - 1]);
535 for (int j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
536 token_t tok0 = 0, tok1 = 0;
538 tok0 = read_token(&ft[0], tok0);
539 tok1 = read_token(&ft[1], tok1);
541 if (((tok0 ^ tok1) & TOK_EMPTY) != 0 /* one is empty (not both) */
542 || (!(tok0 & TOK_EMPTY) && TOK2CHAR(tok0) != TOK2CHAR(tok1)))
543 J[i] = 0; /* Break the correspondence */
544 } while (!(tok0 & tok1 & TOK_EMPTY));
552 * The following struct is used to record change information
553 * doing a "context" or "unified" diff.
556 int a; /* start line in old file */
557 int b; /* end line in old file */
558 int c; /* start line in new file */
559 int d; /* end line in new file */
562 static bool diff(FILE_and_pos_t ft[2], char *file[2])
566 int *J = create_J(ft, nlen, ix);
568 bool anychange = false;
569 struct context_vec *vec = NULL;
574 struct context_vec v;
576 for (v.a = i; v.a <= nlen[0] && J[v.a] == J[v.a - 1] + 1; v.a++)
578 v.c = J[v.a - 1] + 1;
580 for (v.b = v.a - 1; v.b < nlen[0] && !J[v.b + 1]; v.b++)
582 v.d = J[v.b + 1] - 1;
584 * Indicate that there is a difference between lines a and b of the 'from' file
585 * to get to lines c to d of the 'to' file. If a is greater than b then there
586 * are no lines in the 'from' file involved and this means that there were
587 * lines appended (beginning at b). If c is greater than d then there are
588 * lines missing from the 'to' file.
590 if (v.a <= v.b || v.c <= v.d) {
592 * If this change is more than 'context' lines from the
593 * previous change, dump the record and reset it.
596 && v.a > vec[idx].b + (2 * opt_U_context) + 1
597 && v.c > vec[idx].d + (2 * opt_U_context) + 1
601 vec = xrealloc_vector(vec, 6, ++idx);
612 if (!(option_mask32 & FLAG(q))) {
613 struct context_vec *cvp = vec;
614 int lowa = MAX(1, cvp->a - opt_U_context);
615 int upb = MIN(nlen[0], vec[idx].b + opt_U_context);
616 int lowc = MAX(1, cvp->c - opt_U_context);
617 int upd = MIN(nlen[1], vec[idx].d + opt_U_context);
620 /* Print the context/unidiff header first time through */
621 printf("--- %s\n", label[0] ?: file[0]);
622 printf("+++ %s\n", label[1] ?: file[1]);
626 uni_range(lowa, upb);
628 uni_range(lowc, upd);
632 * Output changes in "unified" diff format--the old and new lines
633 * are printed together.
636 bool end = cvp > &vec[idx];
637 fetch(&ft[0], ix[0], lowa, end ? upb : cvp->a - 1, ' ');
640 fetch(&ft[0], ix[0], cvp->a, cvp->b, '-');
641 fetch(&ft[1], ix[1], cvp->c, cvp->d, '+');
647 } while (i <= nlen[0]);
656 static int diffreg(char *file[2])
658 FILE_and_pos_t ft[2];
659 bool binary = false, differ = false;
660 int status = STATUS_SAME;
662 for (int i = 0; i < 2; i++) {
663 int fd = open_or_warn_stdin(file[i]);
666 /* Our diff implementation is using seek.
667 * When we meet non-seekable file, we must make a temp copy.
670 if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
671 char name[] = "/tmp/difXXXXXX";
672 int fd_tmp = mkstemp(name);
674 bb_perror_msg_and_die("mkstemp");
676 ft[i].ft_pos = bb_copyfd_eof(fd, fd_tmp);
677 /* error message is printed by bb_copyfd_eof */
678 if (ft[i].ft_pos < 0)
681 if (fd) /* Prevents closing of stdin */
685 ft[i].ft_fp = fdopen(fd, "r");
689 const size_t sz = COMMON_BUFSIZE / 2;
690 char *const buf0 = bb_common_bufsiz1;
691 char *const buf1 = buf0 + sz;
693 i = fread(buf0, 1, sz, ft[0].ft_fp);
695 j = fread(buf1, 1, sz, ft[1].ft_fp);
703 for (int k = 0; k < i; k++) {
704 if (!buf0[k] || !buf1[k])
706 if (buf0[k] != buf1[k])
711 if (binary && !(option_mask32 & FLAG(a)))
712 status = STATUS_BINARY;
713 else if (diff(ft, file))
714 status = STATUS_DIFFER;
716 if (status != STATUS_SAME)
719 fclose_if_not_stdin(ft[0].ft_fp);
720 fclose_if_not_stdin(ft[1].ft_fp);
725 static void print_status(int status, char *path[2])
730 if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
731 printf("Files %s and %s differ\n", path[0], path[1]);
734 if (option_mask32 & FLAG(s))
735 printf("Files %s and %s are identical\n", path[0], path[1]);
740 #if ENABLE_FEATURE_DIFF_DIR
747 /* This function adds a filename to dl, the directory listing. */
748 static int FAST_FUNC add_to_dirlist(const char *filename,
749 struct stat *sb UNUSED_PARAM,
750 void *userdata, int depth UNUSED_PARAM)
752 struct dlist *const l = userdata;
753 l->dl = xrealloc_vector(l->dl, 6, l->e);
754 /* + 1 skips "/" after dirname */
755 l->dl[l->e] = xstrdup(filename + l->len + 1);
760 /* If recursion is not set, this function adds the directory
761 * to the list and prevents recursive_action from recursing into it.
763 static int FAST_FUNC skip_dir(const char *filename,
764 struct stat *sb, void *userdata,
767 if (!(option_mask32 & FLAG(r)) && depth) {
768 add_to_dirlist(filename, sb, userdata, depth);
774 static void diffdir(char *p[2], const char *s_start)
776 struct dlist list[2];
778 memset(&list, 0, sizeof(list));
779 for (int i = 0; i < 2; i++) {
780 /*list[i].s = list[i].e = 0; - memset did it */
781 /*list[i].dl = NULL; */
783 /* We need to trim root directory prefix.
784 * Using list.len to specify its length,
785 * add_to_dirlist will remove it. */
786 list[i].len = strlen(p[i]);
787 recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
788 add_to_dirlist, skip_dir, &list[i], 0);
789 /* Sort dl alphabetically.
790 * GNU diff does this ignoring any number of trailing dots.
791 * We don't, so for us dotted files almost always are
794 qsort_string_vector(list[i].dl, list[i].e);
795 /* If -S was set, find the starting point. */
798 while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
801 /* Now that both dirlist1 and dirlist2 contain sorted directory
802 * listings, we can start to go through dirlist1. If both listings
803 * contain the same file, then do a normal diff. Otherwise, behaviour
804 * is determined by whether the -N flag is set. */
810 dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
811 dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
812 if (!dp[0] && !dp[1])
814 pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
816 if (pos && !(option_mask32 & FLAG(N)))
817 printf("Only in %s: %s\n", p[k], dp[k]);
819 char *fullpath[2], *path[2]; /* if -N */
821 for (int i = 0; i < 2; i++) {
822 if (pos == 0 || i == k) {
823 path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
824 stat(fullpath[i], &stb[i]);
826 fullpath[i] = concat_path_file(p[i], dp[1 - i]);
827 path[i] = (char *)bb_dev_null;
831 stat(fullpath[k], &stb[1 - k]);
833 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
834 printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
835 else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
836 printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
837 else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
838 printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
839 else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
840 if (S_ISDIR(stb[0].st_mode))
841 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
843 printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
845 print_status(diffreg(path), fullpath);
857 if (ENABLE_FEATURE_CLEAN_UP) {
864 int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
865 int diff_main(int argc UNUSED_PARAM, char **argv)
868 char *file[2], *s_start = NULL;
869 llist_t *L_arg = NULL;
873 /* exactly 2 params; collect multiple -L <label>; -U N */
874 opt_complementary = "=2:L::U+";
875 getopt32(argv, "abdiL:NqrsS:tTU:wu"
876 "p" /* ignored (for compatibility) */,
877 &L_arg, &s_start, &opt_U_context);
880 if (label[0] && label[1])
882 if (label[0]) /* then label[1] is NULL */
884 label[0] = llist_pop(&L_arg);
886 xfunc_error_retval = 2;
887 for (int i = 0; i < 2; i++) {
889 /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
890 if (LONE_DASH(file[i])) {
891 fstat(STDIN_FILENO, &stb[i]);
894 xstat(file[i], &stb[i]);
896 xfunc_error_retval = 1;
897 if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
898 bb_error_msg_and_die("can't compare stdin to a directory");
900 if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
901 #if ENABLE_FEATURE_DIFF_DIR
902 diffdir(file, s_start);
904 bb_error_msg_and_die("no support for directory comparison");
907 bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
908 bool dir = S_ISDIR(stb[1].st_mode);
910 const char *slash = strrchr(file[!dir], '/');
911 file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
912 xstat(file[dir], &stb[dir]);
914 /* diffreg can get non-regular files here */
915 print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);