diff.c 31 KB

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