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