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