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