diff.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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 source tree.
  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. //config:config DIFF
  78. //config: bool "diff (13 kb)"
  79. //config: default y
  80. //config: help
  81. //config: diff compares two files or directories and outputs the
  82. //config: differences between them in a form that can be given to
  83. //config: the patch command.
  84. //config:
  85. //config:config FEATURE_DIFF_LONG_OPTIONS
  86. //config: bool "Enable long options"
  87. //config: default y
  88. //config: depends on DIFF && LONG_OPTS
  89. //config:
  90. //config:config FEATURE_DIFF_DIR
  91. //config: bool "Enable directory support"
  92. //config: default y
  93. //config: depends on DIFF
  94. //config: help
  95. //config: This option enables support for directory and subdirectory
  96. //config: comparison.
  97. //applet:IF_DIFF(APPLET(diff, BB_DIR_USR_BIN, BB_SUID_DROP))
  98. //kbuild:lib-$(CONFIG_DIFF) += diff.o
  99. //usage:#define diff_trivial_usage
  100. //usage: "[-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2"
  101. //usage:#define diff_full_usage "\n\n"
  102. //usage: "Compare files line by line and output the differences between them.\n"
  103. //usage: "This implementation supports unified diffs only.\n"
  104. //usage: "\n -a Treat all files as text"
  105. //usage: "\n -b Ignore changes in the amount of whitespace"
  106. //usage: "\n -B Ignore changes whose lines are all blank"
  107. //usage: "\n -d Try hard to find a smaller set of changes"
  108. //usage: "\n -i Ignore case differences"
  109. //usage: "\n -L Use LABEL instead of the filename in the unified header"
  110. //usage: "\n -N Treat absent files as empty"
  111. //usage: "\n -q Output only whether files differ"
  112. //usage: "\n -r Recurse"
  113. //usage: "\n -S Start with FILE when comparing directories"
  114. //usage: "\n -T Make tabs line up by prefixing a tab when necessary"
  115. //usage: "\n -s Report when two files are the same"
  116. //usage: "\n -t Expand tabs to spaces in output"
  117. //usage: "\n -U Output LINES lines of context"
  118. //usage: "\n -w Ignore all whitespace"
  119. #include "libbb.h"
  120. #include "common_bufsiz.h"
  121. #if 0
  122. # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
  123. #else
  124. # define dbg_error_msg(...) ((void)0)
  125. #endif
  126. enum { /* print_status() and diffreg() return values */
  127. STATUS_SAME, /* files are the same */
  128. STATUS_DIFFER, /* files differ */
  129. STATUS_BINARY, /* binary files differ */
  130. };
  131. enum { /* Commandline flags */
  132. FLAG_a,
  133. FLAG_b,
  134. FLAG_d,
  135. FLAG_i,
  136. FLAG_L, /* never used, handled by getopt32 */
  137. FLAG_N,
  138. FLAG_q,
  139. FLAG_r,
  140. FLAG_s,
  141. FLAG_S, /* never used, handled by getopt32 */
  142. FLAG_t,
  143. FLAG_T,
  144. FLAG_U, /* never used, handled by getopt32 */
  145. FLAG_w,
  146. FLAG_u, /* ignored, this is the default */
  147. FLAG_p, /* not implemented */
  148. FLAG_B,
  149. FLAG_E, /* not implemented */
  150. };
  151. #define FLAG(x) (1 << FLAG_##x)
  152. /* We cache file position to avoid excessive seeking */
  153. typedef struct FILE_and_pos_t {
  154. FILE *ft_fp;
  155. off_t ft_pos;
  156. } FILE_and_pos_t;
  157. struct globals {
  158. smallint exit_status;
  159. int opt_U_context;
  160. const char *other_dir;
  161. char *label[2];
  162. struct stat stb[2];
  163. };
  164. #define G (*ptr_to_globals)
  165. #define exit_status (G.exit_status )
  166. #define opt_U_context (G.opt_U_context )
  167. #define label (G.label )
  168. #define stb (G.stb )
  169. #define INIT_G() do { \
  170. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  171. opt_U_context = 3; \
  172. } while (0)
  173. typedef int token_t;
  174. enum {
  175. /* Public */
  176. TOK_EMPTY = 1 << 9, /* Line fully processed, you can proceed to the next */
  177. TOK_EOF = 1 << 10, /* File ended */
  178. /* Private (Only to be used by read_token() */
  179. TOK_EOL = 1 << 11, /* we saw EOL (sticky) */
  180. TOK_SPACE = 1 << 12, /* used -b code, means we are skipping spaces */
  181. SHIFT_EOF = (sizeof(token_t)*8 - 8) - 1,
  182. CHAR_MASK = 0x1ff, /* 8th bit is used to distinguish EOF from 0xff */
  183. };
  184. /* Restores full EOF from one 8th bit: */
  185. //#define TOK2CHAR(t) (((t) << SHIFT_EOF) >> SHIFT_EOF)
  186. /* We don't really need the above, we only need to have EOF != any_real_char: */
  187. #define TOK2CHAR(t) ((t) & CHAR_MASK)
  188. static void seek_ft(FILE_and_pos_t *ft, off_t pos)
  189. {
  190. if (ft->ft_pos != pos) {
  191. ft->ft_pos = pos;
  192. fseeko(ft->ft_fp, pos, SEEK_SET);
  193. }
  194. }
  195. /* Reads tokens from given fp, handling -b and -w flags
  196. * The user must reset tok every line start
  197. */
  198. static int read_token(FILE_and_pos_t *ft, token_t tok)
  199. {
  200. tok |= TOK_EMPTY;
  201. while (!(tok & TOK_EOL)) {
  202. bool is_space;
  203. int t;
  204. t = fgetc(ft->ft_fp);
  205. if (t != EOF)
  206. ft->ft_pos++;
  207. is_space = (t == EOF || isspace(t));
  208. /* If t == EOF (-1), set both TOK_EOF and TOK_EOL */
  209. tok |= (t & (TOK_EOF + TOK_EOL));
  210. /* Only EOL? */
  211. if (t == '\n')
  212. tok |= TOK_EOL;
  213. if (option_mask32 & FLAG(i)) /* Handcoded tolower() */
  214. t = (t >= 'A' && t <= 'Z') ? t - ('A' - 'a') : t;
  215. if ((option_mask32 & FLAG(w)) && is_space)
  216. continue;
  217. /* Trim char value to low 9 bits */
  218. t &= CHAR_MASK;
  219. if (option_mask32 & FLAG(b)) {
  220. /* Was prev char whitespace? */
  221. if (tok & TOK_SPACE) { /* yes */
  222. if (is_space) /* this one too, ignore it */
  223. continue;
  224. tok &= ~TOK_SPACE;
  225. } else if (is_space) {
  226. /* 1st whitespace char.
  227. * Set TOK_SPACE and replace char by ' ' */
  228. t = TOK_SPACE + ' ';
  229. }
  230. }
  231. /* Clear EMPTY */
  232. tok &= ~(TOK_EMPTY + CHAR_MASK);
  233. /* Assign char value (low 9 bits) and maybe set TOK_SPACE */
  234. tok |= t;
  235. break;
  236. }
  237. #if 0
  238. bb_error_msg("fp:%p tok:%x '%c'%s%s%s%s", fp, tok, tok & 0xff
  239. , tok & TOK_EOF ? " EOF" : ""
  240. , tok & TOK_EOL ? " EOL" : ""
  241. , tok & TOK_EMPTY ? " EMPTY" : ""
  242. , tok & TOK_SPACE ? " SPACE" : ""
  243. );
  244. #endif
  245. return tok;
  246. }
  247. struct cand {
  248. int x;
  249. int y;
  250. int pred;
  251. };
  252. static int search(const int *c, int k, int y, const struct cand *list)
  253. {
  254. int i, j;
  255. if (list[c[k]].y < y) /* quick look for typical case */
  256. return k + 1;
  257. for (i = 0, j = k + 1;;) {
  258. const int l = (i + j) >> 1;
  259. if (l > i) {
  260. const int t = list[c[l]].y;
  261. if (t > y)
  262. j = l;
  263. else if (t < y)
  264. i = l;
  265. else
  266. return l;
  267. } else
  268. return l + 1;
  269. }
  270. }
  271. static void stone(const int *a, int n, const int *b, int *J, int pref)
  272. {
  273. const unsigned isq = isqrt(n);
  274. const unsigned bound =
  275. (option_mask32 & FLAG(d)) ? UINT_MAX : MAX(256, isq);
  276. int clen = 1;
  277. int clistlen = 100;
  278. int k = 0;
  279. struct cand *clist = xzalloc(clistlen * sizeof(clist[0]));
  280. struct cand cand;
  281. struct cand *q;
  282. int *klist = xzalloc((n + 2) * sizeof(klist[0]));
  283. /*clist[0] = (struct cand){0}; - xzalloc did it */
  284. /*klist[0] = 0; */
  285. for (cand.x = 1; cand.x <= n; cand.x++) {
  286. int j = a[cand.x], oldl = 0;
  287. unsigned numtries = 0;
  288. if (j == 0)
  289. continue;
  290. cand.y = -b[j];
  291. cand.pred = klist[0];
  292. do {
  293. int l, tc;
  294. if (cand.y <= clist[cand.pred].y)
  295. continue;
  296. l = search(klist, k, cand.y, clist);
  297. if (l != oldl + 1)
  298. cand.pred = klist[l - 1];
  299. if (l <= k && clist[klist[l]].y <= cand.y)
  300. continue;
  301. if (clen == clistlen) {
  302. clistlen = clistlen * 11 / 10;
  303. clist = xrealloc(clist, clistlen * sizeof(clist[0]));
  304. }
  305. clist[clen] = cand;
  306. tc = klist[l];
  307. klist[l] = clen++;
  308. if (l <= k) {
  309. cand.pred = tc;
  310. oldl = l;
  311. numtries++;
  312. } else {
  313. k++;
  314. break;
  315. }
  316. } while ((cand.y = b[++j]) > 0 && numtries < bound);
  317. }
  318. /* Unravel */
  319. for (q = clist + klist[k]; q->y; q = clist + q->pred)
  320. J[q->x + pref] = q->y + pref;
  321. free(klist);
  322. free(clist);
  323. }
  324. struct line {
  325. /* 'serial' is not used in the beginning, so we reuse it
  326. * to store line offsets, thus reducing memory pressure
  327. */
  328. union {
  329. unsigned serial;
  330. off_t offset;
  331. };
  332. unsigned value;
  333. };
  334. static void equiv(struct line *a, int n, struct line *b, int m, int *c)
  335. {
  336. int i = 1, j = 1;
  337. while (i <= n && j <= m) {
  338. if (a[i].value < b[j].value)
  339. a[i++].value = 0;
  340. else if (a[i].value == b[j].value)
  341. a[i++].value = j;
  342. else
  343. j++;
  344. }
  345. while (i <= n)
  346. a[i++].value = 0;
  347. b[m + 1].value = 0;
  348. j = 0;
  349. while (++j <= m) {
  350. c[j] = -b[j].serial;
  351. while (b[j + 1].value == b[j].value) {
  352. j++;
  353. c[j] = b[j].serial;
  354. }
  355. }
  356. c[j] = -1;
  357. }
  358. static void unsort(const struct line *f, int l, int *b)
  359. {
  360. int i;
  361. int *a = xmalloc((l + 1) * sizeof(a[0]));
  362. for (i = 1; i <= l; i++)
  363. a[f[i].serial] = f[i].value;
  364. for (i = 1; i <= l; i++)
  365. b[i] = a[i];
  366. free(a);
  367. }
  368. static int line_compar(const void *a, const void *b)
  369. {
  370. #define l0 ((const struct line*)a)
  371. #define l1 ((const struct line*)b)
  372. int r = l0->value - l1->value;
  373. if (r)
  374. return r;
  375. return l0->serial - l1->serial;
  376. #undef l0
  377. #undef l1
  378. }
  379. static void fetch(FILE_and_pos_t *ft, const off_t *ix, int a, int b, int ch)
  380. {
  381. int i, j, col;
  382. for (i = a; i <= b; i++) {
  383. seek_ft(ft, ix[i - 1]);
  384. putchar(ch);
  385. if (option_mask32 & FLAG(T))
  386. putchar('\t');
  387. for (j = 0, col = 0; j < ix[i] - ix[i - 1]; j++) {
  388. int c = fgetc(ft->ft_fp);
  389. if (c == EOF) {
  390. puts("\n\\ No newline at end of file");
  391. return;
  392. }
  393. ft->ft_pos++;
  394. if (c == '\t' && (option_mask32 & FLAG(t)))
  395. do putchar(' '); while (++col & 7);
  396. else {
  397. putchar(c);
  398. col++;
  399. }
  400. }
  401. }
  402. }
  403. /* Creates the match vector J, where J[i] is the index
  404. * of the line in the new file corresponding to the line i
  405. * in the old file. Lines start at 1 instead of 0, that value
  406. * being used instead to denote no corresponding line.
  407. * This vector is dynamically allocated and must be freed by the caller.
  408. *
  409. * * fp is an input parameter, where fp[0] and fp[1] are the open
  410. * old file and new file respectively.
  411. * * nlen is an output variable, where nlen[0] and nlen[1]
  412. * gets the number of lines in the old and new file respectively.
  413. * * ix is an output variable, where ix[0] and ix[1] gets
  414. * assigned dynamically allocated vectors of the offsets of the lines
  415. * of the old and new file respectively. These must be freed by the caller.
  416. */
  417. static NOINLINE int *create_J(FILE_and_pos_t ft[2], int nlen[2], off_t *ix[2])
  418. {
  419. int *J, slen[2], *class, *member;
  420. struct line *nfile[2], *sfile[2];
  421. int pref = 0, suff = 0, i, j, delta;
  422. /* Lines of both files are hashed, and in the process
  423. * their offsets are stored in the array ix[fileno]
  424. * where fileno == 0 points to the old file, and
  425. * fileno == 1 points to the new one.
  426. */
  427. for (i = 0; i < 2; i++) {
  428. unsigned hash;
  429. token_t tok;
  430. size_t sz = 100;
  431. nfile[i] = xmalloc((sz + 3) * sizeof(nfile[i][0]));
  432. /* ft gets here without the correct position, cant use seek_ft */
  433. ft[i].ft_pos = 0;
  434. fseeko(ft[i].ft_fp, 0, SEEK_SET);
  435. nlen[i] = 0;
  436. /* We could zalloc nfile, but then zalloc starts showing in gprof at ~1% */
  437. nfile[i][0].offset = 0;
  438. goto start; /* saves code */
  439. while (1) {
  440. tok = read_token(&ft[i], tok);
  441. if (!(tok & TOK_EMPTY)) {
  442. /* Hash algorithm taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578. */
  443. /*hash = hash * 128 - hash + TOK2CHAR(tok);
  444. * gcc insists on optimizing above to "hash * 127 + ...", thus... */
  445. unsigned o = hash - TOK2CHAR(tok);
  446. hash = hash * 128 - o; /* we want SPEED here */
  447. continue;
  448. }
  449. if (nlen[i]++ == sz) {
  450. sz = sz * 3 / 2;
  451. nfile[i] = xrealloc(nfile[i], (sz + 3) * sizeof(nfile[i][0]));
  452. }
  453. /* line_compar needs hashes fit into positive int */
  454. nfile[i][nlen[i]].value = hash & INT_MAX;
  455. /* like ftello(ft[i].ft_fp) but faster (avoids lseek syscall) */
  456. nfile[i][nlen[i]].offset = ft[i].ft_pos;
  457. if (tok & TOK_EOF) {
  458. /* EOF counts as a token, so we have to adjust it here */
  459. nfile[i][nlen[i]].offset++;
  460. break;
  461. }
  462. start:
  463. hash = tok = 0;
  464. }
  465. /* Exclude lone EOF line from the end of the file, to make fetch()'s job easier */
  466. if (nfile[i][nlen[i]].offset - nfile[i][nlen[i] - 1].offset == 1)
  467. nlen[i]--;
  468. /* Now we copy the line offsets into ix */
  469. ix[i] = xmalloc((nlen[i] + 2) * sizeof(ix[i][0]));
  470. for (j = 0; j < nlen[i] + 1; j++)
  471. ix[i][j] = nfile[i][j].offset;
  472. }
  473. /* length of prefix and suffix is calculated */
  474. for (; pref < nlen[0] && pref < nlen[1] &&
  475. nfile[0][pref + 1].value == nfile[1][pref + 1].value;
  476. pref++);
  477. for (; suff < nlen[0] - pref && suff < nlen[1] - pref &&
  478. nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
  479. suff++);
  480. /* Arrays are pruned by the suffix and prefix length,
  481. * the result being sorted and stored in sfile[fileno],
  482. * and their sizes are stored in slen[fileno]
  483. */
  484. for (j = 0; j < 2; j++) {
  485. sfile[j] = nfile[j] + pref;
  486. slen[j] = nlen[j] - pref - suff;
  487. for (i = 0; i <= slen[j]; i++)
  488. sfile[j][i].serial = i;
  489. qsort(sfile[j] + 1, slen[j], sizeof(*sfile[j]), line_compar);
  490. }
  491. /* nfile arrays are reused to reduce memory pressure
  492. * The #if zeroed out section performs the same task as the
  493. * one in the #else section.
  494. * Peak memory usage is higher, but one array copy is avoided
  495. * by not using unsort()
  496. */
  497. #if 0
  498. member = xmalloc((slen[1] + 2) * sizeof(member[0]));
  499. equiv(sfile[0], slen[0], sfile[1], slen[1], member);
  500. free(nfile[1]);
  501. class = xmalloc((slen[0] + 1) * sizeof(class[0]));
  502. for (i = 1; i <= slen[0]; i++) /* Unsorting */
  503. class[sfile[0][i].serial] = sfile[0][i].value;
  504. free(nfile[0]);
  505. #else
  506. member = (int *)nfile[1];
  507. equiv(sfile[0], slen[0], sfile[1], slen[1], member);
  508. member = xrealloc(member, (slen[1] + 2) * sizeof(member[0]));
  509. class = (int *)nfile[0];
  510. unsort(sfile[0], slen[0], (int *)nfile[0]);
  511. class = xrealloc(class, (slen[0] + 2) * sizeof(class[0]));
  512. #endif
  513. J = xmalloc((nlen[0] + 2) * sizeof(J[0]));
  514. /* The elements of J which fall inside the prefix and suffix regions
  515. * are marked as unchanged, while the ones which fall outside
  516. * are initialized with 0 (no matches), so that function stone can
  517. * then assign them their right values
  518. */
  519. for (i = 0, delta = nlen[1] - nlen[0]; i <= nlen[0]; i++)
  520. J[i] = i <= pref ? i :
  521. i > (nlen[0] - suff) ? (i + delta) : 0;
  522. /* Here the magic is performed */
  523. stone(class, slen[0], member, J, pref);
  524. J[nlen[0] + 1] = nlen[1] + 1;
  525. free(class);
  526. free(member);
  527. /* Both files are rescanned, in an effort to find any lines
  528. * which, due to limitations intrinsic to any hashing algorithm,
  529. * are different but ended up confounded as the same
  530. */
  531. for (i = 1; i <= nlen[0]; i++) {
  532. if (!J[i])
  533. continue;
  534. seek_ft(&ft[0], ix[0][i - 1]);
  535. seek_ft(&ft[1], ix[1][J[i] - 1]);
  536. for (j = J[i]; i <= nlen[0] && J[i] == j; i++, j++) {
  537. token_t tok0 = 0, tok1 = 0;
  538. do {
  539. tok0 = read_token(&ft[0], tok0);
  540. 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. ) {
  544. J[i] = 0; /* Break the correspondence */
  545. }
  546. } while (!(tok0 & tok1 & TOK_EMPTY));
  547. }
  548. }
  549. return J;
  550. }
  551. static bool diff(FILE* fp[2], char *file[2])
  552. {
  553. int nlen[2];
  554. off_t *ix[2];
  555. FILE_and_pos_t ft[2];
  556. typedef struct { int a, b; } vec_t[2];
  557. vec_t *vec = NULL;
  558. int i = 1, j, k, idx = -1;
  559. bool anychange = false;
  560. int *J;
  561. ft[0].ft_fp = fp[0];
  562. ft[1].ft_fp = fp[1];
  563. /* note that ft[i].ft_pos is unintitalized, create_J()
  564. * must not assume otherwise */
  565. J = create_J(ft, nlen, ix);
  566. do {
  567. bool nonempty = false;
  568. while (1) {
  569. vec_t v;
  570. for (v[0].a = i; v[0].a <= nlen[0] && J[v[0].a] == J[v[0].a - 1] + 1; v[0].a++)
  571. continue;
  572. v[1].a = J[v[0].a - 1] + 1;
  573. for (v[0].b = v[0].a - 1; v[0].b < nlen[0] && !J[v[0].b + 1]; v[0].b++)
  574. continue;
  575. v[1].b = J[v[0].b + 1] - 1;
  576. /*
  577. * Indicate that there is a difference between lines a and b of the 'from' file
  578. * to get to lines c to d of the 'to' file. If a is greater than b then there
  579. * are no lines in the 'from' file involved and this means that there were
  580. * lines appended (beginning at b). If c is greater than d then there are
  581. * lines missing from the 'to' file.
  582. */
  583. if (v[0].a <= v[0].b || v[1].a <= v[1].b) {
  584. /*
  585. * If this change is more than 'context' lines from the
  586. * previous change, dump the record and reset it.
  587. */
  588. int ct = (2 * opt_U_context) + 1;
  589. if (idx >= 0
  590. && v[0].a > vec[idx][0].b + ct
  591. && v[1].a > vec[idx][1].b + ct
  592. ) {
  593. break;
  594. }
  595. for (j = 0; j < 2; j++)
  596. for (k = v[j].a; k <= v[j].b; k++)
  597. nonempty |= (ix[j][k] - ix[j][k - 1] != 1);
  598. vec = xrealloc_vector(vec, 6, ++idx);
  599. memcpy(vec[idx], v, sizeof(v));
  600. }
  601. i = v[0].b + 1;
  602. if (i > nlen[0])
  603. break;
  604. J[v[0].b] = v[1].b;
  605. }
  606. if (idx < 0 || ((option_mask32 & FLAG(B)) && !nonempty))
  607. goto cont;
  608. if (!(option_mask32 & FLAG(q))) {
  609. int lowa;
  610. vec_t span, *cvp = vec;
  611. if (!anychange) {
  612. /* Print the context/unidiff header first time through */
  613. printf("--- %s\n", label[0] ? label[0] : file[0]);
  614. printf("+++ %s\n", label[1] ? label[1] : file[1]);
  615. }
  616. printf("@@");
  617. for (j = 0; j < 2; j++) {
  618. int a = span[j].a = MAX(1, (*cvp)[j].a - opt_U_context);
  619. int b = span[j].b = MIN(nlen[j], vec[idx][j].b + opt_U_context);
  620. printf(" %c%d", j ? '+' : '-', MIN(a, b));
  621. if (a == b)
  622. continue;
  623. printf(",%d", (a < b) ? b - a + 1 : 0);
  624. }
  625. puts(" @@");
  626. /*
  627. * Output changes in "unified" diff format--the old and new lines
  628. * are printed together.
  629. */
  630. for (lowa = span[0].a; ; lowa = (*cvp++)[0].b + 1) {
  631. bool end = cvp > &vec[idx];
  632. fetch(&ft[0], ix[0], lowa, end ? span[0].b : (*cvp)[0].a - 1, ' ');
  633. if (end)
  634. break;
  635. for (j = 0; j < 2; j++)
  636. fetch(&ft[j], ix[j], (*cvp)[j].a, (*cvp)[j].b, j ? '+' : '-');
  637. }
  638. }
  639. anychange = true;
  640. cont:
  641. idx = -1;
  642. } while (i <= nlen[0]);
  643. free(vec);
  644. free(ix[0]);
  645. free(ix[1]);
  646. free(J);
  647. return anychange;
  648. }
  649. static int diffreg(char *file[2])
  650. {
  651. FILE *fp[2];
  652. bool binary = false, differ = false;
  653. int status = STATUS_SAME, i;
  654. fp[0] = stdin;
  655. fp[1] = stdin;
  656. for (i = 0; i < 2; i++) {
  657. int fd = STDIN_FILENO;
  658. if (!LONE_DASH(file[i])) {
  659. if (!(option_mask32 & FLAG(N))) {
  660. fd = open_or_warn(file[i], O_RDONLY);
  661. if (fd == -1)
  662. goto out;
  663. } else {
  664. /* -N: if some file does not exist compare it like empty */
  665. fd = open(file[i], O_RDONLY);
  666. if (fd == -1)
  667. fd = xopen("/dev/null", O_RDONLY);
  668. }
  669. }
  670. /* Our diff implementation is using seek.
  671. * When we meet non-seekable file, we must make a temp copy.
  672. */
  673. if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
  674. char name[] = "/tmp/difXXXXXX";
  675. int fd_tmp = xmkstemp(name);
  676. unlink(name);
  677. if (bb_copyfd_eof(fd, fd_tmp) < 0)
  678. xfunc_die();
  679. if (fd != STDIN_FILENO)
  680. close(fd);
  681. fd = fd_tmp;
  682. xlseek(fd, 0, SEEK_SET);
  683. }
  684. fp[i] = fdopen(fd, "r");
  685. }
  686. setup_common_bufsiz();
  687. while (1) {
  688. const size_t sz = COMMON_BUFSIZE / 2;
  689. char *const buf0 = bb_common_bufsiz1;
  690. char *const buf1 = buf0 + sz;
  691. int j, k;
  692. i = fread(buf0, 1, sz, fp[0]);
  693. j = fread(buf1, 1, sz, fp[1]);
  694. if (i != j) {
  695. differ = true;
  696. i = MIN(i, j);
  697. }
  698. if (i == 0)
  699. break;
  700. for (k = 0; k < i; k++) {
  701. if (!buf0[k] || !buf1[k])
  702. binary = true;
  703. if (buf0[k] != buf1[k])
  704. differ = true;
  705. }
  706. }
  707. if (differ) {
  708. if (binary && !(option_mask32 & FLAG(a)))
  709. status = STATUS_BINARY;
  710. else if (diff(fp, file))
  711. status = STATUS_DIFFER;
  712. }
  713. if (status != STATUS_SAME)
  714. exit_status |= 1;
  715. out:
  716. fclose_if_not_stdin(fp[0]);
  717. fclose_if_not_stdin(fp[1]);
  718. return status;
  719. }
  720. static void print_status(int status, char *path[2])
  721. {
  722. switch (status) {
  723. case STATUS_BINARY:
  724. case STATUS_DIFFER:
  725. if ((option_mask32 & FLAG(q)) || status == STATUS_BINARY)
  726. printf("Files %s and %s differ\n", path[0], path[1]);
  727. break;
  728. case STATUS_SAME:
  729. if (option_mask32 & FLAG(s))
  730. printf("Files %s and %s are identical\n", path[0], path[1]);
  731. break;
  732. }
  733. }
  734. #if ENABLE_FEATURE_DIFF_DIR
  735. struct dlist {
  736. size_t len;
  737. int s, e;
  738. char **dl;
  739. };
  740. /* This function adds a filename to dl, the directory listing. */
  741. static int FAST_FUNC add_to_dirlist(const char *filename,
  742. struct stat *sb UNUSED_PARAM,
  743. void *userdata, int depth UNUSED_PARAM)
  744. {
  745. struct dlist *const l = userdata;
  746. const char *file = filename + l->len;
  747. while (*file == '/')
  748. file++;
  749. l->dl = xrealloc_vector(l->dl, 6, l->e);
  750. l->dl[l->e] = xstrdup(file);
  751. l->e++;
  752. return TRUE;
  753. }
  754. /* If recursion is not set, this function adds the directory
  755. * to the list and prevents recursive_action from recursing into it.
  756. */
  757. static int FAST_FUNC skip_dir(const char *filename,
  758. struct stat *sb, void *userdata,
  759. int depth)
  760. {
  761. if (!(option_mask32 & FLAG(r)) && depth) {
  762. add_to_dirlist(filename, sb, userdata, depth);
  763. return SKIP;
  764. }
  765. if (!(option_mask32 & FLAG(N))) {
  766. /* -r without -N: no need to recurse into dirs
  767. * which do not exist on the "other side".
  768. * Testcase: diff -r /tmp /
  769. * (it would recurse deep into /proc without this code) */
  770. struct dlist *const l = userdata;
  771. filename += l->len;
  772. if (filename[0]) {
  773. struct stat osb;
  774. char *othername = concat_path_file(G.other_dir, filename);
  775. int r = stat(othername, &osb);
  776. free(othername);
  777. if (r != 0 || !S_ISDIR(osb.st_mode)) {
  778. /* other dir doesn't have similarly named
  779. * directory, don't recurse; return 1 upon
  780. * exit, just like diffutils' diff */
  781. exit_status |= 1;
  782. return SKIP;
  783. }
  784. }
  785. }
  786. return TRUE;
  787. }
  788. static void diffdir(char *p[2], const char *s_start)
  789. {
  790. struct dlist list[2];
  791. int i;
  792. memset(&list, 0, sizeof(list));
  793. for (i = 0; i < 2; i++) {
  794. /*list[i].s = list[i].e = 0; - memset did it */
  795. /*list[i].dl = NULL; */
  796. G.other_dir = p[1 - i];
  797. /* We need to trim root directory prefix.
  798. * Using list.len to specify its length,
  799. * add_to_dirlist will remove it. */
  800. list[i].len = strlen(p[i]);
  801. recursive_action(p[i], ACTION_RECURSE | ACTION_FOLLOWLINKS,
  802. add_to_dirlist, skip_dir, &list[i], 0);
  803. /* Sort dl alphabetically.
  804. * GNU diff does this ignoring any number of trailing dots.
  805. * We don't, so for us dotted files almost always are
  806. * first on the list.
  807. */
  808. qsort_string_vector(list[i].dl, list[i].e);
  809. /* If -S was set, find the starting point. */
  810. if (!s_start)
  811. continue;
  812. while (list[i].s < list[i].e && strcmp(list[i].dl[list[i].s], s_start) < 0)
  813. list[i].s++;
  814. }
  815. /* Now that both dirlist1 and dirlist2 contain sorted directory
  816. * listings, we can start to go through dirlist1. If both listings
  817. * contain the same file, then do a normal diff. Otherwise, behaviour
  818. * is determined by whether the -N flag is set. */
  819. while (1) {
  820. char *dp[2];
  821. int pos;
  822. int k;
  823. dp[0] = list[0].s < list[0].e ? list[0].dl[list[0].s] : NULL;
  824. dp[1] = list[1].s < list[1].e ? list[1].dl[list[1].s] : NULL;
  825. if (!dp[0] && !dp[1])
  826. break;
  827. pos = !dp[0] ? 1 : (!dp[1] ? -1 : strcmp(dp[0], dp[1]));
  828. k = pos > 0;
  829. if (pos && !(option_mask32 & FLAG(N))) {
  830. printf("Only in %s: %s\n", p[k], dp[k]);
  831. exit_status |= 1;
  832. } else {
  833. char *fullpath[2], *path[2]; /* if -N */
  834. for (i = 0; i < 2; i++) {
  835. if (pos == 0 || i == k) {
  836. path[i] = fullpath[i] = concat_path_file(p[i], dp[i]);
  837. stat(fullpath[i], &stb[i]);
  838. } else {
  839. fullpath[i] = concat_path_file(p[i], dp[1 - i]);
  840. path[i] = (char *)bb_dev_null;
  841. }
  842. }
  843. if (pos)
  844. stat(fullpath[k], &stb[1 - k]);
  845. if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode))
  846. printf("Common subdirectories: %s and %s\n", fullpath[0], fullpath[1]);
  847. else if (!S_ISREG(stb[0].st_mode) && !S_ISDIR(stb[0].st_mode))
  848. printf("File %s is not a regular file or directory and was skipped\n", fullpath[0]);
  849. else if (!S_ISREG(stb[1].st_mode) && !S_ISDIR(stb[1].st_mode))
  850. printf("File %s is not a regular file or directory and was skipped\n", fullpath[1]);
  851. else if (S_ISDIR(stb[0].st_mode) != S_ISDIR(stb[1].st_mode)) {
  852. if (S_ISDIR(stb[0].st_mode))
  853. printf("File %s is a %s while file %s is a %s\n", fullpath[0], "directory", fullpath[1], "regular file");
  854. else
  855. printf("File %s is a %s while file %s is a %s\n", fullpath[0], "regular file", fullpath[1], "directory");
  856. } else
  857. print_status(diffreg(path), fullpath);
  858. free(fullpath[0]);
  859. free(fullpath[1]);
  860. }
  861. free(dp[k]);
  862. list[k].s++;
  863. if (pos == 0) {
  864. free(dp[1 - k]);
  865. list[1 - k].s++;
  866. }
  867. }
  868. if (ENABLE_FEATURE_CLEAN_UP) {
  869. free(list[0].dl);
  870. free(list[1].dl);
  871. }
  872. }
  873. #endif
  874. #if ENABLE_FEATURE_DIFF_LONG_OPTIONS
  875. static const char diff_longopts[] ALIGN1 =
  876. "ignore-case\0" No_argument "i"
  877. "ignore-tab-expansion\0" No_argument "E"
  878. "ignore-space-change\0" No_argument "b"
  879. "ignore-all-space\0" No_argument "w"
  880. "ignore-blank-lines\0" No_argument "B"
  881. "text\0" No_argument "a"
  882. "unified\0" Required_argument "U"
  883. "label\0" Required_argument "L"
  884. "show-c-function\0" No_argument "p"
  885. "brief\0" No_argument "q"
  886. "expand-tabs\0" No_argument "t"
  887. "initial-tab\0" No_argument "T"
  888. "recursive\0" No_argument "r"
  889. "new-file\0" No_argument "N"
  890. "report-identical-files\0" No_argument "s"
  891. "starting-file\0" Required_argument "S"
  892. "minimal\0" No_argument "d"
  893. ;
  894. # define GETOPT32 getopt32long
  895. # define LONGOPTS ,diff_longopts
  896. #else
  897. # define GETOPT32 getopt32
  898. # define LONGOPTS
  899. #endif
  900. int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  901. int diff_main(int argc UNUSED_PARAM, char **argv)
  902. {
  903. int gotstdin = 0, i;
  904. char *file[2], *s_start = NULL;
  905. llist_t *L_arg = NULL;
  906. INIT_G();
  907. /* exactly 2 params; collect multiple -L <label>; -U N */
  908. GETOPT32(argv, "^" "abdiL:*NqrsS:tTU:+wupBE" "\0" "=2"
  909. LONGOPTS,
  910. &L_arg, &s_start, &opt_U_context);
  911. argv += optind;
  912. while (L_arg)
  913. label[!!label[0]] = llist_pop(&L_arg);
  914. /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
  915. xfunc_error_retval = 2;
  916. for (i = 0; i < 2; i++) {
  917. file[i] = argv[i];
  918. if (LONE_DASH(file[i])) {
  919. fstat(STDIN_FILENO, &stb[i]);
  920. gotstdin++;
  921. } else if (option_mask32 & FLAG(N)) {
  922. if (stat(file[i], &stb[i]))
  923. xstat("/dev/null", &stb[i]);
  924. } else {
  925. xstat(file[i], &stb[i]);
  926. }
  927. }
  928. xfunc_error_retval = 1;
  929. if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode)))
  930. bb_simple_error_msg_and_die("can't compare stdin to a directory");
  931. /* Compare metadata to check if the files are the same physical file.
  932. *
  933. * Comment from diffutils source says:
  934. * POSIX says that two files are identical if st_ino and st_dev are
  935. * the same, but many file systems incorrectly assign the same (device,
  936. * inode) pair to two distinct files, including:
  937. * GNU/Linux NFS servers that export all local file systems as a
  938. * single NFS file system, if a local device number (st_dev) exceeds
  939. * 255, or if a local inode number (st_ino) exceeds 16777215.
  940. */
  941. if (ENABLE_DESKTOP
  942. && stb[0].st_ino == stb[1].st_ino
  943. && stb[0].st_dev == stb[1].st_dev
  944. && stb[0].st_size == stb[1].st_size
  945. && stb[0].st_mtime == stb[1].st_mtime
  946. && stb[0].st_ctime == stb[1].st_ctime
  947. && stb[0].st_mode == stb[1].st_mode
  948. && stb[0].st_nlink == stb[1].st_nlink
  949. && stb[0].st_uid == stb[1].st_uid
  950. && stb[0].st_gid == stb[1].st_gid
  951. ) {
  952. /* files are physically the same; no need to compare them */
  953. return STATUS_SAME;
  954. }
  955. if (S_ISDIR(stb[0].st_mode) && S_ISDIR(stb[1].st_mode)) {
  956. #if ENABLE_FEATURE_DIFF_DIR
  957. diffdir(file, s_start);
  958. #else
  959. bb_simple_error_msg_and_die("no support for directory comparison");
  960. #endif
  961. } else {
  962. bool dirfile = S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode);
  963. bool dir = S_ISDIR(stb[1].st_mode);
  964. if (dirfile) {
  965. const char *slash = strrchr(file[!dir], '/');
  966. file[dir] = concat_path_file(file[dir], slash ? slash + 1 : file[!dir]);
  967. xstat(file[dir], &stb[dir]);
  968. }
  969. /* diffreg can get non-regular files here */
  970. print_status(gotstdin > 1 ? STATUS_SAME : diffreg(file), file);
  971. if (dirfile)
  972. free(file[dir]);
  973. }
  974. return exit_status;
  975. }