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