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