3
0

diff.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini diff implementation for busybox, adapted from OpenBSD diff.
  4. *
  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>
  8. *
  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.
  12. *
  13. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  14. */
  15. /*
  16. * The following code uses an algorithm due to Harold Stone,
  17. * which finds a pair of longest identical subsequences in
  18. * the two files.
  19. *
  20. * The major goal is to generate the match vector J.
  21. * J[i] is the index of the line in file1 corresponding
  22. * to line i in file0. J[i] = 0 if there is no
  23. * such line in file1.
  24. *
  25. * Lines are hashed so as to work in core. All potential
  26. * matches are located by sorting the lines of each file
  27. * on the hash (called "value"). In particular, this
  28. * collects the equivalence classes in file1 together.
  29. * Subroutine equiv replaces the value of each line in
  30. * file0 by the index of the first element of its
  31. * matching equivalence in (the reordered) file1.
  32. * To save space equiv squeezes file1 into a single
  33. * array member in which the equivalence classes
  34. * are simply concatenated, except that their first
  35. * members are flagged by changing sign.
  36. *
  37. * Next the indices that point into member are unsorted into
  38. * array class according to the original order of file0.
  39. *
  40. * The cleverness lies in routine stone. This marches
  41. * through the lines of file0, developing a vector klist
  42. * of "k-candidates". At step i a k-candidate is a matched
  43. * pair of lines x,y (x in file0, y in file1) such that
  44. * there is a common subsequence of length k
  45. * between the first i lines of file0 and the first y
  46. * lines of file1, but there is no such subsequence for
  47. * any smaller y. x is the earliest possible mate to y
  48. * that occurs in such a subsequence.
  49. *
  50. * Whenever any of the members of the equivalence class of
  51. * lines in file1 matable to a line in file0 has serial number
  52. * less than the y of some k-candidate, that k-candidate
  53. * with the smallest such y is replaced. The new
  54. * k-candidate is chained (via pred) to the current
  55. * k-1 candidate so that the actual subsequence can
  56. * be recovered. When a member has serial number greater
  57. * that the y of all k-candidates, the klist is extended.
  58. * At the end, the longest subsequence is pulled out
  59. * and placed in the array J by unravel
  60. *
  61. * With J in hand, the matches there recorded are
  62. * checked against reality to assure that no spurious
  63. * matches have crept in due to hashing. If they have,
  64. * they are broken, and "jackpot" is recorded--a harmless
  65. * matter except that a true match for a spuriously
  66. * mated line may now be unnecessarily reported as a change.
  67. *
  68. * Much of the complexity of the program comes simply
  69. * from trying to minimize core utilization and
  70. * maximize the range of doable problems by dynamically
  71. * allocating what is needed and reusing what is not.
  72. * The core requirements for problems larger than somewhat
  73. * are (in words) 2*length(file0) + length(file1) +
  74. * 3*(number of k-candidates installed), typically about
  75. * 6n words for files of length n.
  76. */
  77. #include "libbb.h"
  78. #if 0
  79. //#define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
  80. #else
  81. #define dbg_error_msg(...) ((void)0)
  82. #endif
  83. enum { /* print_status() and diffreg() return values */
  84. STATUS_SAME, /* files are the same */
  85. STATUS_DIFFER, /* files differ */
  86. STATUS_BINARY, /* binary files differ */
  87. };
  88. enum { /* Commandline flags */
  89. FLAG_a,
  90. FLAG_b,
  91. FLAG_d,
  92. FLAG_i,
  93. FLAG_L, /* never used, handled by getopt32 */
  94. FLAG_N,
  95. FLAG_q,
  96. FLAG_r,
  97. FLAG_s,
  98. FLAG_S, /* never used, handled by getopt32 */
  99. FLAG_t,
  100. FLAG_T,
  101. FLAG_U, /* never used, handled by getopt32 */
  102. FLAG_w,
  103. FLAG_u, /* ignored, this is the default */
  104. FLAG_p, /* not implemented */
  105. FLAG_B,
  106. FLAG_E, /* not implemented */
  107. };
  108. #define FLAG(x) (1 << FLAG_##x)
  109. /* We cache file position to avoid excessive seeking */
  110. typedef struct FILE_and_pos_t {
  111. FILE *ft_fp;
  112. off_t ft_pos;
  113. } FILE_and_pos_t;
  114. struct globals {
  115. smallint exit_status;
  116. int opt_U_context;
  117. char *label[2];
  118. struct stat stb[2];
  119. };
  120. #define G (*ptr_to_globals)
  121. #define exit_status (G.exit_status )
  122. #define opt_U_context (G.opt_U_context )
  123. #define label (G.label )
  124. #define stb (G.stb )
  125. #define INIT_G() do { \
  126. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  127. opt_U_context = 3; \
  128. } while (0)
  129. typedef int token_t;
  130. enum {
  131. /* Public */
  132. TOK_EMPTY = 1 << 9, /* Line fully processed, you can proceed to the next */
  133. TOK_EOF = 1 << 10, /* File ended */
  134. /* Private (Only to be used by read_token() */
  135. TOK_EOL = 1 << 11, /* we saw EOL (sticky) */
  136. TOK_SPACE = 1 << 12, /* used -b code, means we are skipping spaces */
  137. SHIFT_EOF = (sizeof(token_t)*8 - 8) - 1,
  138. CHAR_MASK = 0x1ff, /* 8th bit is used to distinguish EOF from 0xff */
  139. };
  140. /* Restores full EOF from one 8th bit: */
  141. //#define TOK2CHAR(t) (((t) << SHIFT_EOF) >> SHIFT_EOF)
  142. /* We don't really need the above, we only need to have EOF != any_real_char: */
  143. #define TOK2CHAR(t) ((t) & CHAR_MASK)
  144. static void seek_ft(FILE_and_pos_t *ft, off_t pos)
  145. {
  146. if (ft->ft_pos != pos) {
  147. ft->ft_pos = pos;
  148. fseeko(ft->ft_fp, pos, SEEK_SET);
  149. }
  150. }
  151. /* Reads tokens from given fp, handling -b and -w flags
  152. * The user must reset tok every line start
  153. */
  154. static int read_token(FILE_and_pos_t *ft, token_t tok)
  155. {
  156. tok |= TOK_EMPTY;
  157. while (!(tok & TOK_EOL)) {
  158. bool is_space;
  159. int t;
  160. t = fgetc(ft->ft_fp);
  161. if (t != EOF)
  162. ft->ft_pos++;
  163. is_space = (t == EOF || isspace(t));
  164. /* If t == EOF (-1), set both TOK_EOF and TOK_EOL */
  165. tok |= (t & (TOK_EOF + TOK_EOL));
  166. /* Only EOL? */
  167. if (t == '\n')
  168. tok |= TOK_EOL;
  169. if (option_mask32 & FLAG(i)) /* Handcoded tolower() */
  170. t = (t >= 'A' && t <= 'Z') ? t - ('A' - 'a') : t;
  171. if ((option_mask32 & FLAG(w)) && is_space)
  172. continue;
  173. /* Trim char value to low 9 bits */
  174. t &= CHAR_MASK;
  175. if (option_mask32 & FLAG(b)) {
  176. /* Was prev char whitespace? */
  177. if (tok & TOK_SPACE) { /* yes */
  178. if (is_space) /* this one too, ignore it */
  179. continue;
  180. tok &= ~TOK_SPACE;
  181. } else if (is_space) {
  182. /* 1st whitespace char.
  183. * Set TOK_SPACE and replace char by ' ' */
  184. t = TOK_SPACE + ' ';
  185. }
  186. }
  187. /* Clear EMPTY */
  188. tok &= ~(TOK_EMPTY + CHAR_MASK);
  189. /* Assign char value (low 9 bits) and maybe set TOK_SPACE */
  190. tok |= t;
  191. break;
  192. }
  193. #if 0
  194. bb_error_msg("fp:%p tok:%x '%c'%s%s%s%s", fp, tok, tok & 0xff
  195. , tok & TOK_EOF ? " EOF" : ""
  196. , tok & TOK_EOL ? " EOL" : ""
  197. , tok & TOK_EMPTY ? " EMPTY" : ""
  198. , tok & TOK_SPACE ? " SPACE" : ""
  199. );
  200. #endif
  201. return tok;
  202. }
  203. struct cand {
  204. int x;
  205. int y;
  206. int pred;
  207. };
  208. static int search(const int *c, int k, int y, const struct cand *list)
  209. {
  210. int i, j;
  211. if (list[c[k]].y < y) /* quick look for typical case */
  212. return k + 1;
  213. for (i = 0, j = k + 1;;) {
  214. const int l = (i + j) >> 1;
  215. if (l > i) {
  216. const int t = list[c[l]].y;
  217. if (t > y)
  218. j = l;
  219. else if (t < y)
  220. i = l;
  221. else
  222. return l;
  223. } else
  224. return l + 1;
  225. }
  226. }
  227. static unsigned isqrt(unsigned n)
  228. {
  229. unsigned x = 1;
  230. while (1) {
  231. const unsigned y = x;
  232. x = ((n / x) + x) >> 1;
  233. if (x <= (y + 1) && x >= (y - 1))
  234. return x;
  235. }
  236. }
  237. static void stone(const int *a, int n, const int *b, int *J, int pref)
  238. {
  239. const unsigned isq = isqrt(n);
  240. const unsigned bound =
  241. (option_mask32 & FLAG(d)) ? UINT_MAX : MAX(256, isq);
  242. int clen = 1;
  243. int clistlen = 100;
  244. int k = 0;
  245. struct cand *clist = xzalloc(clistlen * sizeof(clist[0]));
  246. struct cand cand;
  247. struct cand *q;
  248. int *klist = xzalloc((n + 2) * sizeof(klist[0]));
  249. /*clist[0] = (struct cand){0}; - xzalloc did it */
  250. /*klist[0] = 0; */
  251. for (cand.x = 1; cand.x <= n; cand.x++) {
  252. int j = a[cand.x], oldl = 0;
  253. unsigned numtries = 0;
  254. if (j == 0)
  255. continue;
  256. cand.y = -b[j];
  257. cand.pred = klist[0];
  258. do {
  259. int l, tc;
  260. if (cand.y <= clist[cand.pred].y)
  261. continue;
  262. l = search(klist, k, cand.y, clist);
  263. if (l != oldl + 1)
  264. cand.pred = klist[l - 1];
  265. if (l <= k && clist[klist[l]].y <= cand.y)
  266. continue;
  267. if (clen == clistlen) {
  268. clistlen = clistlen * 11 / 10;
  269. clist = xrealloc(clist, clistlen * sizeof(clist[0]));
  270. }
  271. clist[clen] = cand;
  272. tc = klist[l];
  273. klist[l] = clen++;
  274. if (l <= k) {
  275. cand.pred = tc;
  276. oldl = l;
  277. numtries++;
  278. } else {
  279. k++;
  280. break;
  281. }
  282. } while ((cand.y = b[++j]) > 0 && numtries < bound);
  283. }
  284. /* Unravel */
  285. for (q = clist + klist[k]; q->y; q = clist + q->pred)
  286. J[q->x + pref] = q->y + pref;
  287. free(klist);
  288. free(clist);
  289. }
  290. struct line {
  291. /* 'serial' is not used in the begining, so we reuse it
  292. * to store line offsets, thus reducing memory pressure
  293. */
  294. union {
  295. unsigned serial;
  296. off_t offset;
  297. };
  298. unsigned value;
  299. };
  300. static void equiv(struct line *a, int n, struct line *b, int m, int *c)
  301. {
  302. int i = 1, j = 1;
  303. while (i <= n && j <= m) {
  304. if (a[i].value < b[j].value)
  305. a[i++].value = 0;
  306. else if (a[i].value == b[j].value)
  307. a[i++].value = j;
  308. else
  309. j++;
  310. }
  311. while (i <= n)
  312. a[i++].value = 0;
  313. b[m + 1].value = 0;
  314. j = 0;
  315. while (++j <= m) {
  316. c[j] = -b[j].serial;
  317. while (b[j + 1].value == b[j].value) {
  318. j++;
  319. c[j] = b[j].serial;
  320. }
  321. }
  322. c[j] = -1;
  323. }
  324. static void unsort(const struct line *f, int l, int *b)
  325. {
  326. int i;
  327. int *a = xmalloc((l + 1) * sizeof(a[0]));
  328. for (i = 1; i <= l; i++)
  329. a[f[i].serial] = f[i].value;
  330. for (i = 1; i <= l; i++)
  331. b[i] = a[i];
  332. free(a);
  333. }
  334. static int line_compar(const void *a, const void *b)
  335. {
  336. #define l0 ((const struct line*)a)
  337. #define l1 ((const struct line*)b)
  338. int r = l0->value - l1->value;
  339. if (r)
  340. return r;
  341. return l0->serial - l1->serial;
  342. #undef l0
  343. #undef l1
  344. }
  345. static void fetch(FILE_and_pos_t *ft, const off_t *ix, int a, int b, int ch)
  346. {
  347. int i, j, col;
  348. for (i = a; i <= b; i++) {
  349. seek_ft(ft, ix[i - 1]);
  350. putchar(ch);
  351. if (option_mask32 & FLAG(T))
  352. putchar('\t');
  353. for (j = 0, col = 0; j < ix[i] - ix[i - 1]; j++) {
  354. int c = fgetc(ft->ft_fp);
  355. if (c == EOF) {
  356. printf("\n\\ No newline at end of file\n");
  357. return;
  358. }
  359. ft->ft_pos++;
  360. if (c == '\t' && (option_mask32 & FLAG(t)))
  361. do putchar(' '); while (++col & 7);
  362. else {
  363. putchar(c);
  364. col++;
  365. }
  366. }
  367. }
  368. }
  369. /* Creates the match vector J, where J[i] is the index
  370. * of the line in the new file corresponding to the line i
  371. * in the old file. Lines start at 1 instead of 0, that value
  372. * being used instead to denote no corresponding line.
  373. * This vector is dynamically allocated and must be freed by the caller.
  374. *
  375. * * fp is an input parameter, where fp[0] and fp[1] are the open
  376. * old file and new file respectively.
  377. * * nlen is an output variable, where nlen[0] and nlen[1]
  378. * gets the number of lines in the old and new file respectively.
  379. * * ix is an output variable, where ix[0] and ix[1] gets
  380. * assigned dynamically allocated vectors of the offsets of the lines
  381. * of the old and new file respectively. These must be freed by the caller.
  382. */
  383. static NOINLINE int *create_J(FILE_and_pos_t ft[2], int nlen[2], off_t *ix[2])
  384. {
  385. int *J, slen[2], *class, *member;
  386. struct line *nfile[2], *sfile[2];
  387. int pref = 0, suff = 0, i, j, delta;
  388. /* Lines of both files are hashed, and in the process
  389. * their offsets are stored in the array ix[fileno]
  390. * where fileno == 0 points to the old file, and
  391. * fileno == 1 points to the new one.
  392. */
  393. for (i = 0; i < 2; i++) {
  394. unsigned hash;
  395. token_t tok;
  396. size_t sz = 100;
  397. nfile[i] = xmalloc((sz + 3) * sizeof(nfile[i][0]));
  398. /* ft gets here without the correct position, cant use seek_ft */
  399. ft[i].ft_pos = 0;
  400. fseeko(ft[i].ft_fp, 0, SEEK_SET);
  401. nlen[i] = 0;
  402. /* We could zalloc nfile, but then zalloc starts showing in gprof at ~1% */
  403. nfile[i][0].offset = 0;
  404. goto start; /* saves code */
  405. while (1) {
  406. tok = read_token(&ft[i], tok);
  407. if (!(tok & TOK_EMPTY)) {
  408. /* Hash algorithm taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578. */
  409. /*hash = hash * 128 - hash + TOK2CHAR(tok);
  410. * gcc insists on optimizing above to "hash * 127 + ...", thus... */
  411. unsigned o = hash - TOK2CHAR(tok);
  412. hash = hash * 128 - o; /* we want SPEED here */
  413. continue;
  414. }
  415. if (nlen[i]++ == sz) {
  416. sz = sz * 3 / 2;
  417. nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
  418. }
  419. /* line_compar needs hashes fit into positive int */
  420. nfile[i][nlen[i]].value = hash & INT_MAX;
  421. /* like ftello(ft[i].ft_fp) but faster (avoids lseek syscall) */
  422. nfile[i][nlen[i]].offset = ft[i].ft_pos;
  423. if (tok & TOK_EOF) {
  424. /* EOF counts as a token, so we have to adjust it here */
  425. nfile[i][nlen[i]].offset++;
  426. break;
  427. }
  428. start:
  429. hash = tok = 0;
  430. }
  431. /* Exclude lone EOF line from the end of the file, to make fetch()'s job easier */
  432. if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
  433. nlen[i]--;
  434. /* Now we copy the line offsets into ix */
  435. ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
  436. for (j = 0; j < nlen[i] + 1; j++)
  437. ix[i][j] = nfile[i][j].offset;
  438. }
  439. /* length of prefix and suffix is calculated */
  440. for (; pref < nlen[0] && pref < nlen[1] &&
  441. nfile[0][pref + 1].value == nfile[1][pref + 1].value;
  442. pref++);
  443. for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
  444. nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
  445. suff++);
  446. /* Arrays are pruned by the suffix and prefix lenght,
  447. * the result being sorted and stored in sfile[fileno],
  448. * and their sizes are stored in slen[fileno]
  449. */
  450. for (j = 0; j < 2; j++) {
  451. sfile[j] = nfile[j] + pref;
  452. slen[j] = nlen[j] - pref - suff;
  453. for (i = 0; i <= slen[j]; i++)
  454. sfile[j][i].serial = i;
  455. qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
  456. }
  457. /* nfile arrays are reused to reduce memory pressure
  458. * The #if zeroed out section performs the same task as the
  459. * one in the #else section.
  460. * Peak memory usage is higher, but one array copy is avoided
  461. * by not using unsort()
  462. */
  463. #if 0
  464. member = xmalloc((slen[1] + 2) * sizeof(member[0]));
  465. equiv(sfile[0], slen[0], sfile[1], slen[1], member);
  466. free(nfile[1]);
  467. class = xmalloc((slen[0] + 1) * sizeof(class[0]));
  468. for (i = 1; i <= slen[0]; i++) /* Unsorting */
  469. class[sfile[0][i].serial] = sfile[0][i].value;
  470. free(nfile[0]);
  471. #else
  472. member = (int *)nfile[1];
  473. equiv(sfile[0], slen[0], sfile[1], slen[1], member);
  474. member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
  475. class = (int *)nfile[0];
  476. unsort(sfile[0], slen[0], (int *)nfile[0]);
  477. class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
  478. #endif
  479. J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
  480. /* The elements of J which fall inside the prefix and suffix regions
  481. * are marked as unchanged, while the ones which fall outside
  482. * are initialized with 0 (no matches), so that function stone can
  483. * then assign them their right values
  484. */
  485. for (i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
  486. J[i] = i <= pref ? i :
  487. i > (nlen[0] - suff) ? (i + delta) : 0;
  488. /* Here the magic is performed */
  489. stone(class, slen[0], member, J, pref);
  490. J[nlen[0] + 1] = nlen[1] + 1;
  491. free(class);
  492. free(member);
  493. /* Both files are rescanned, in an effort to find any lines
  494. * which, due to limitations intrinsic to any hashing algorithm,
  495. * are different but ended up confounded as the same
  496. */
  497. for (i = 1; i <= nlen[0]; i++) {
  498. if (!J[i])
  499. continue;
  500. seek_ft(&ft[0], ix[0][i - 1]);
  501. seek_ft(&ft[1], ix[1][J[i] - 1]);
  502. for (j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
  503. token_t tok0 = 0, tok1 = 0;
  504. do {
  505. tok0 = read_token(&ft[0], tok0);
  506. tok1 = read_token(&ft[1], tok1);
  507. if (((tok0 ^ tok1) & TOK_EMPTY) != 0 /* one is empty (not both) */
  508. || (!(tok0 & TOK_EMPTY) && TOK2CHAR(tok0) != TOK2CHAR(tok1))
  509. ) {
  510. J[i] = 0; /* Break the correspondence */
  511. }
  512. } while (!(tok0 & tok1 & TOK_EMPTY));
  513. }
  514. }
  515. return J;
  516. }
  517. static bool diff(FILE* fp[2], char *file[2])
  518. {
  519. int nlen[2];
  520. off_t *ix[2];
  521. FILE_and_pos_t ft[2];
  522. typedef struct { int a, b; } vec_t[2];
  523. vec_t *vec = NULL;
  524. int i = 1, j, k, idx = -1;
  525. bool anychange = false;
  526. int *J;
  527. ft[0].ft_fp = fp[0];
  528. ft[1].ft_fp = fp[1];
  529. /* note that ft[i].ft_pos is unintitalized, create_J()
  530. * must not assume otherwise */
  531. J = create_J(ft, nlen, ix);
  532. do {
  533. bool nonempty = false;
  534. while (1) {
  535. vec_t v;
  536. for (v[0].a = i; v[0].a <= nlen[0] && J[v[0].a] == J[v[0].a - 1] + 1; v[0].a++)
  537. continue;
  538. v[1].a = J[v[0].a - 1] + 1;
  539. for (v[0].b = v[0].a - 1; v[0].b < nlen[0] && !J[v[0].b + 1]; v[0].b++)
  540. continue;
  541. v[1].b = J[v[0].b + 1] - 1;
  542. /*
  543. * Indicate that there is a difference between lines a and b of the 'from' file
  544. * to get to lines c to d of the 'to' file. If a is greater than b then there
  545. * are no lines in the 'from' file involved and this means that there were
  546. * lines appended (beginning at b). If c is greater than d then there are
  547. * lines missing from the 'to' file.
  548. */
  549. if (v[0].a <= v[0].b || v[1].a <= v[1].b) {
  550. /*
  551. * If this change is more than 'context' lines from the
  552. * previous change, dump the record and reset it.
  553. */
  554. int ct = (2 * opt_U_context) + 1;
  555. if (idx >= 0
  556. && v[0].a > vec[idx][0].b + ct
  557. && v[1].a > vec[idx][1].b + ct
  558. ) {
  559. break;
  560. }
  561. for (j = 0; j < 2; j++)
  562. for (k = v[j].a; k < v[j].b; k++)
  563. nonempty |= (ix[j][k+1] - ix[j][k] != 1);
  564. vec = xrealloc_vector(vec, 6, ++idx);
  565. memcpy(vec[idx], v, sizeof(v));
  566. }
  567. i = v[0].b + 1;
  568. if (i > nlen[0])
  569. break;
  570. J[v[0].b] = v[1].b;
  571. }
  572. if (idx < 0 || ((option_mask32 & FLAG(B)) && !nonempty))
  573. goto cont;
  574. if (!(option_mask32 & FLAG(q))) {
  575. int lowa;
  576. vec_t span, *cvp = vec;
  577. if (!anychange) {
  578. /* Print the context/unidiff header first time through */
  579. printf("--- %s\n", label[0] ? label[0] : file[0]);
  580. printf("+++ %s\n", label[1] ? label[1] : file[1]);
  581. }
  582. printf("@@");
  583. for (j = 0; j < 2; j++) {
  584. int a = span[j].a = MAX(1, (*cvp)[j].a - opt_U_context);
  585. int b = span[j].b = MIN(nlen[j], vec[idx][j].b + opt_U_context);
  586. printf(" %c%d", j ? '+' : '-', MIN(a, b));
  587. if (a == b)
  588. continue;
  589. printf(",%d", (a < b) ? b - a + 1 : 0);
  590. }
  591. printf(" @@\n");
  592. /*
  593. * Output changes in "unified" diff format--the old and new lines
  594. * are printed together.
  595. */
  596. for (lowa = span[0].a; ; lowa = (*cvp++)[0].b + 1) {
  597. bool end = cvp > &vec[idx];
  598. fetch(&ft[0], ix[0], lowa, end ? span[0].b : (*cvp)[0].a - 1, ' ');
  599. if (end)
  600. break;
  601. for (j = 0; j < 2; j++)
  602. fetch(&ft[j], ix[j], (*cvp)[j].a, (*cvp)[j].b, j ? '+' : '-');
  603. }
  604. }
  605. anychange = true;
  606. cont:
  607. idx = -1;
  608. } while (i <= nlen[0]);
  609. free(vec);
  610. free(ix[0]);
  611. free(ix[1]);
  612. free(J);
  613. return anychange;
  614. }
  615. static int diffreg(char *file[2])
  616. {
  617. FILE *fp[2] = { stdin, stdin };
  618. bool binary = false, differ = false;
  619. int status = STATUS_SAME, i;
  620. for (i = 0; i < 2; i++) {
  621. int fd = open_or_warn_stdin(file[i]);
  622. if (fd == -1)
  623. goto out;
  624. /* Our diff implementation is using seek.
  625. * When we meet non-seekable file, we must make a temp copy.
  626. */
  627. if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
  628. char name[] = "/tmp/difXXXXXX";
  629. int fd_tmp = mkstemp(name);
  630. if (fd_tmp < 0)
  631. bb_perror_msg_and_die("mkstemp");
  632. unlink(name);
  633. if (bb_copyfd_eof(fd, fd_tmp) < 0)
  634. xfunc_die();
  635. if (fd) /* Prevents closing of stdin */
  636. close(fd);
  637. fd = fd_tmp;
  638. }
  639. fp[i] = fdopen(fd, "r");
  640. }
  641. while (1) {
  642. const size_t sz = COMMON_BUFSIZE / 2;
  643. char *const buf0 = bb_common_bufsiz1;
  644. char *const buf1 = buf0 + sz;
  645. int j, k;
  646. i = fread(buf0, 1, sz, fp[0]);
  647. j = fread(buf1, 1, sz, fp[1]);
  648. if (i != j) {
  649. differ = true;
  650. i = MIN(i, j);
  651. }
  652. if (i == 0)
  653. break;
  654. for (k = 0; k < i; k++) {
  655. if (!buf0[k] || !buf1[k])
  656. binary = true;
  657. if (buf0[k] != buf1[k])
  658. differ = true;
  659. }
  660. }
  661. if (differ) {
  662. if (binary && !(option_mask32 & FLAG(a)))
  663. status = STATUS_BINARY;
  664. else if (diff(fp, file))
  665. status = STATUS_DIFFER;
  666. }
  667. if (status != STATUS_SAME)
  668. exit_status |= 1;
  669. out:
  670. fclose_if_not_stdin(fp[0]);
  671. fclose_if_not_stdin(fp[1]);
  672. return status;
  673. }
  674. static void print_status(int status, char *path[2])
  675. {
  676. switch (status) {
  677. case STATUS_BINARY:
  678. case STATUS_DIFFER:
  679. if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
  680. printf("Files %s and %s differ\n", path[0], path[1]);
  681. break;
  682. case STATUS_SAME:
  683. if (option_mask32 & FLAG(s))
  684. printf("Files %s and %s are identical\n", path[0], path[1]);
  685. break;
  686. }
  687. }
  688. #if ENABLE_FEATURE_DIFF_DIR
  689. struct dlist {
  690. size_t len;
  691. int s, e;
  692. char **dl;
  693. };
  694. /* This function adds a filename to dl, the directory listing. */
  695. static int FAST_FUNC add_to_dirlist(const char *filename,
  696. struct stat *sb UNUSED_PARAM,
  697. void *userdata, int depth UNUSED_PARAM)
  698. {
  699. struct dlist *const l = userdata;
  700. l->dl = xrealloc_vector(l->dl, 6, l->e);
  701. /* + 1 skips "/" after dirname */
  702. l->dl[l->e] = xstrdup(filename + l->len + 1);
  703. l->e++;
  704. return TRUE;
  705. }
  706. /* If recursion is not set, this function adds the directory
  707. * to the list and prevents recursive_action from recursing into it.
  708. */
  709. static int FAST_FUNC skip_dir(const char *filename,
  710. struct stat *sb, void *userdata,
  711. int depth)
  712. {
  713. if (!(option_mask32 & FLAG(r)) && depth) {
  714. add_to_dirlist(filename, sb, userdata, depth);
  715. return SKIP;
  716. }
  717. return TRUE;
  718. }
  719. static void diffdir(char *p[2], const char *s_start)
  720. {
  721. struct dlist list[2];
  722. int i;
  723. memset(&list, 0, sizeof(list));
  724. for (i = 0; i < 2; i++) {
  725. /*list[i].s = list[i].e = 0; - memset did it */
  726. /*list[i].dl = NULL; */
  727. /* We need to trim root directory prefix.
  728. * Using list.len to specify its length,
  729. * add_to_dirlist will remove it. */
  730. list[i].len = strlen(p[i]);
  731. recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
  732. add_to_dirlist, skip_dir, &list[i], 0);
  733. /* Sort dl alphabetically.
  734. * GNU diff does this ignoring any number of trailing dots.
  735. * We don't, so for us dotted files almost always are
  736. * first on the list.
  737. */
  738. qsort_string_vector(list[i].dl, list[i].e);
  739. /* If -S was set, find the starting point. */
  740. if (!s_start)
  741. continue;
  742. while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
  743. list[i].s++;
  744. }
  745. /* Now that both dirlist1 and dirlist2 contain sorted directory
  746. * listings, we can start to go through dirlist1. If both listings
  747. * contain the same file, then do a normal diff. Otherwise, behaviour
  748. * is determined by whether the -N flag is set. */
  749. while (1) {
  750. char *dp[2];
  751. int pos;
  752. int k;
  753. dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
  754. dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
  755. if (!dp[0] && !dp[1])
  756. break;
  757. pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
  758. k = pos > 0;
  759. if (pos && !(option_mask32 & FLAG(N)))
  760. printf("Only in %s: %s\n", p[k], dp[k]);
  761. else {
  762. char *fullpath[2], *path[2]; /* if -N */
  763. for (i = 0; i < 2; i++) {
  764. if (pos == 0 || i == k) {
  765. path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
  766. stat(fullpath[i], &stb[i]);
  767. } else {
  768. fullpath[i] = concat_path_file(p[i], dp[1 - i]);
  769. path[i] = (char *)bb_dev_null;
  770. }
  771. }
  772. if (pos)
  773. stat(fullpath[k], &stb[1 - k]);
  774. if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
  775. printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
  776. else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
  777. printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
  778. else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
  779. printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
  780. else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
  781. if (S_ISDIR(stb[0].st_mode))
  782. printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
  783. else
  784. printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
  785. } else
  786. print_status(diffreg(path), fullpath);
  787. free(fullpath[0]);
  788. free(fullpath[1]);
  789. }
  790. free(dp[k]);
  791. list[k].s++;
  792. if (pos == 0) {
  793. free(dp[1 - k]);
  794. list[1 - k].s++;
  795. }
  796. }
  797. if (ENABLE_FEATURE_CLEAN_UP) {
  798. free(list[0].dl);
  799. free(list[1].dl);
  800. }
  801. }
  802. #endif
  803. #if ENABLE_FEATURE_DIFF_LONG_OPTIONS
  804. static const char diff_longopts[] ALIGN1 =
  805. "ignore-case\0" No_argument "i"
  806. "ignore-tab-expansion\0" No_argument "E"
  807. "ignore-space-change\0" No_argument "b"
  808. "ignore-all-space\0" No_argument "w"
  809. "ignore-blank-lines\0" No_argument "B"
  810. "text\0" No_argument "a"
  811. "unified\0" Required_argument "U"
  812. "label\0" Required_argument "L"
  813. "show-c-function\0" No_argument "p"
  814. "brief\0" No_argument "q"
  815. "expand-tabs\0" No_argument "t"
  816. "initial-tab\0" No_argument "T"
  817. "recursive\0" No_argument "r"
  818. "new-file\0" No_argument "N"
  819. "report-identical-files\0" No_argument "s"
  820. "starting-file\0" Required_argument "S"
  821. "minimal\0" No_argument "d"
  822. ;
  823. #endif
  824. int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  825. int diff_main(int argc UNUSED_PARAM, char **argv)
  826. {
  827. int gotstdin = 0, i;
  828. char *file[2], *s_start = NULL;
  829. llist_t *L_arg = NULL;
  830. INIT_G();
  831. /* exactly 2 params; collect multiple -L <label>; -U N */
  832. opt_complementary = "=2:L::U+";
  833. #if ENABLE_FEATURE_DIFF_LONG_OPTIONS
  834. applet_long_options = diff_longopts;
  835. #endif
  836. getopt32(argv, "abdiL:NqrsS:tTU:wupBE",
  837. &L_arg, &s_start, &opt_U_context);
  838. argv += optind;
  839. while (L_arg)
  840. label[!!label[0]] = llist_pop(&L_arg);
  841. xfunc_error_retval = 2;
  842. for (i = 0; i < 2; i++) {
  843. file[i] = argv[i];
  844. /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
  845. if (LONE_DASH(file[i])) {
  846. fstat(STDIN_FILENO, &stb[i]);
  847. gotstdin++;
  848. } else
  849. xstat(file[i], &stb[i]);
  850. }
  851. xfunc_error_retval = 1;
  852. if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
  853. bb_error_msg_and_die("can't compare stdin to a directory");
  854. if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
  855. #if ENABLE_FEATURE_DIFF_DIR
  856. diffdir(file, s_start);
  857. #else
  858. bb_error_msg_and_die("no support for directory comparison");
  859. #endif
  860. } else {
  861. bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
  862. bool dir = S_ISDIR(stb[1].st_mode);
  863. if (dirfile) {
  864. const char *slash = strrchr(file[!dir], '/');
  865. file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
  866. xstat(file[dir], &stb[dir]);
  867. }
  868. /* diffreg can get non-regular files here */
  869. print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);
  870. if (dirfile)
  871. free(file[dir]);
  872. }
  873. return exit_status;
  874. }