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