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