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