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