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