1 /* $OpenBSD: diff.c,v 1.62 2015/10/05 20:15:00 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * Sponsored in part by the Defense Advanced Research Projects 19 * Agency (DARPA) and Air Force Research Laboratory, Air Force 20 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 21 */ 22 23 #include <sys/stat.h> 24 25 #include <ctype.h> 26 #include <err.h> 27 #include <errno.h> 28 #include <getopt.h> 29 #include <stdlib.h> 30 #include <stdio.h> 31 #include <stdarg.h> 32 #include <string.h> 33 #include <unistd.h> 34 #include <limits.h> 35 36 #include "diff.h" 37 #include "xmalloc.h" 38 39 int Nflag, Pflag, rflag, sflag, Tflag; 40 int diff_format, diff_context, status; 41 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; 42 struct stat stb1, stb2; 43 struct excludes *excludes_list; 44 regex_t ignore_re; 45 46 #define OPTIONS "0123456789abC:cdD:efhI:iL:lnNPpqrS:sTtU:uwX:x:" 47 static struct option longopts[] = { 48 { "text", no_argument, 0, 'a' }, 49 { "ignore-space-change", no_argument, 0, 'b' }, 50 { "context", optional_argument, 0, 'C' }, 51 { "ifdef", required_argument, 0, 'D' }, 52 { "minimal", no_argument, 0, 'd' }, 53 { "ed", no_argument, 0, 'e' }, 54 { "forward-ed", no_argument, 0, 'f' }, 55 { "ignore-matching-lines", required_argument, 0, 'I' }, 56 { "ignore-case", no_argument, 0, 'i' }, 57 { "label", required_argument, 0, 'L' }, 58 { "new-file", no_argument, 0, 'N' }, 59 { "rcs", no_argument, 0, 'n' }, 60 { "unidirectional-new-file", no_argument, 0, 'P' }, 61 { "show-c-function", no_argument, 0, 'p' }, 62 { "brief", no_argument, 0, 'q' }, 63 { "recursive", no_argument, 0, 'r' }, 64 { "report-identical-files", no_argument, 0, 's' }, 65 { "starting-file", required_argument, 0, 'S' }, 66 { "expand-tabs", no_argument, 0, 't' }, 67 { "initial-tab", no_argument, 0, 'T' }, 68 { "unified", optional_argument, 0, 'U' }, 69 { "ignore-all-space", no_argument, 0, 'w' }, 70 { "exclude", required_argument, 0, 'x' }, 71 { "exclude-from", required_argument, 0, 'X' }, 72 { NULL, 0, 0, '\0'} 73 }; 74 75 __dead void usage(void); 76 void push_excludes(char *); 77 void push_ignore_pats(char *); 78 void read_excludes_file(char *file); 79 void set_argstr(char **, char **); 80 81 int 82 main(int argc, char **argv) 83 { 84 char *ep, **oargv; 85 long l; 86 int ch, dflags, lastch, gotstdin, prevoptind, newarg; 87 88 oargv = argv; 89 gotstdin = 0; 90 dflags = 0; 91 lastch = '\0'; 92 prevoptind = 1; 93 newarg = 1; 94 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 95 switch (ch) { 96 case '0': case '1': case '2': case '3': case '4': 97 case '5': case '6': case '7': case '8': case '9': 98 if (newarg) 99 usage(); /* disallow -[0-9]+ */ 100 else if (lastch == 'c' || lastch == 'u') 101 diff_context = 0; 102 else if (!isdigit(lastch) || diff_context > INT_MAX / 10) 103 usage(); 104 diff_context = (diff_context * 10) + (ch - '0'); 105 break; 106 case 'a': 107 dflags |= D_FORCEASCII; 108 break; 109 case 'b': 110 dflags |= D_FOLDBLANKS; 111 break; 112 case 'C': 113 case 'c': 114 diff_format = D_CONTEXT; 115 if (optarg != NULL) { 116 l = strtol(optarg, &ep, 10); 117 if (*ep != '\0' || l < 0 || l >= INT_MAX) 118 usage(); 119 diff_context = (int)l; 120 } else 121 diff_context = 3; 122 break; 123 case 'd': 124 dflags |= D_MINIMAL; 125 break; 126 case 'D': 127 diff_format = D_IFDEF; 128 ifdefname = optarg; 129 break; 130 case 'e': 131 diff_format = D_EDIT; 132 break; 133 case 'f': 134 diff_format = D_REVERSE; 135 break; 136 case 'h': 137 /* silently ignore for backwards compatibility */ 138 break; 139 case 'I': 140 push_ignore_pats(optarg); 141 break; 142 case 'i': 143 dflags |= D_IGNORECASE; 144 break; 145 case 'L': 146 if (label[0] == NULL) 147 label[0] = optarg; 148 else if (label[1] == NULL) 149 label[1] = optarg; 150 else 151 usage(); 152 break; 153 case 'N': 154 Nflag = 1; 155 break; 156 case 'n': 157 diff_format = D_NREVERSE; 158 break; 159 case 'p': 160 dflags |= D_PROTOTYPE; 161 break; 162 case 'P': 163 Pflag = 1; 164 break; 165 case 'r': 166 rflag = 1; 167 break; 168 case 'q': 169 diff_format = D_BRIEF; 170 break; 171 case 'S': 172 start = optarg; 173 break; 174 case 's': 175 sflag = 1; 176 break; 177 case 'T': 178 Tflag = 1; 179 break; 180 case 't': 181 dflags |= D_EXPANDTABS; 182 break; 183 case 'U': 184 case 'u': 185 diff_format = D_UNIFIED; 186 if (optarg != NULL) { 187 l = strtol(optarg, &ep, 10); 188 if (*ep != '\0' || l < 0 || l >= INT_MAX) 189 usage(); 190 diff_context = (int)l; 191 } else 192 diff_context = 3; 193 break; 194 case 'w': 195 dflags |= D_IGNOREBLANKS; 196 break; 197 case 'X': 198 read_excludes_file(optarg); 199 break; 200 case 'x': 201 push_excludes(optarg); 202 break; 203 default: 204 usage(); 205 break; 206 } 207 lastch = ch; 208 newarg = optind != prevoptind; 209 prevoptind = optind; 210 } 211 argc -= optind; 212 argv += optind; 213 214 if (getenv("TMPDIR")) { 215 if (tame("stdio rpath wpath cpath", NULL) == -1) 216 err(1, "tame"); 217 } else { 218 if (tame("stdio rpath tmppath", NULL) == -1) 219 err(1, "tame"); 220 } 221 222 /* 223 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 224 * driver routine. Both drivers use the contents of stb1 and stb2. 225 */ 226 if (argc != 2) 227 usage(); 228 if (ignore_pats != NULL) { 229 char buf[BUFSIZ]; 230 int error; 231 232 if ((error = regcomp(&ignore_re, ignore_pats, 233 REG_NEWLINE | REG_EXTENDED)) != 0) { 234 regerror(error, &ignore_re, buf, sizeof(buf)); 235 if (*ignore_pats != '\0') 236 errx(2, "%s: %s", ignore_pats, buf); 237 else 238 errx(2, "%s", buf); 239 } 240 } 241 if (strcmp(argv[0], "-") == 0) { 242 fstat(STDIN_FILENO, &stb1); 243 gotstdin = 1; 244 } else if (stat(argv[0], &stb1) != 0) 245 err(2, "%s", argv[0]); 246 if (strcmp(argv[1], "-") == 0) { 247 fstat(STDIN_FILENO, &stb2); 248 gotstdin = 1; 249 } else if (stat(argv[1], &stb2) != 0) 250 err(2, "%s", argv[1]); 251 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 252 errx(2, "can't compare - to a directory"); 253 set_argstr(oargv, argv); 254 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 255 if (diff_format == D_IFDEF) 256 errx(2, "-D option not supported with directories"); 257 diffdir(argv[0], argv[1], dflags); 258 } else { 259 if (S_ISDIR(stb1.st_mode)) { 260 argv[0] = splice(argv[0], argv[1]); 261 if (stat(argv[0], &stb1) < 0) 262 err(2, "%s", argv[0]); 263 } 264 if (S_ISDIR(stb2.st_mode)) { 265 argv[1] = splice(argv[1], argv[0]); 266 if (stat(argv[1], &stb2) < 0) 267 err(2, "%s", argv[1]); 268 } 269 print_status(diffreg(argv[0], argv[1], dflags), argv[0], argv[1], 270 ""); 271 } 272 exit(status); 273 } 274 275 void 276 set_argstr(char **av, char **ave) 277 { 278 size_t argsize; 279 char **ap; 280 281 argsize = 4 + *ave - *av + 1; 282 diffargs = xmalloc(argsize); 283 strlcpy(diffargs, "diff", argsize); 284 for (ap = av + 1; ap < ave; ap++) { 285 if (strcmp(*ap, "--") != 0) { 286 strlcat(diffargs, " ", argsize); 287 strlcat(diffargs, *ap, argsize); 288 } 289 } 290 } 291 292 /* 293 * Read in an excludes file and push each line. 294 */ 295 void 296 read_excludes_file(char *file) 297 { 298 FILE *fp; 299 char *buf, *pattern; 300 size_t len; 301 302 if (strcmp(file, "-") == 0) 303 fp = stdin; 304 else if ((fp = fopen(file, "r")) == NULL) 305 err(2, "%s", file); 306 while ((buf = fgetln(fp, &len)) != NULL) { 307 if (buf[len - 1] == '\n') 308 len--; 309 pattern = xmalloc(len + 1); 310 memcpy(pattern, buf, len); 311 pattern[len] = '\0'; 312 push_excludes(pattern); 313 } 314 if (strcmp(file, "-") != 0) 315 fclose(fp); 316 } 317 318 /* 319 * Push a pattern onto the excludes list. 320 */ 321 void 322 push_excludes(char *pattern) 323 { 324 struct excludes *entry; 325 326 entry = xmalloc(sizeof(*entry)); 327 entry->pattern = pattern; 328 entry->next = excludes_list; 329 excludes_list = entry; 330 } 331 332 void 333 push_ignore_pats(char *pattern) 334 { 335 size_t len; 336 337 if (ignore_pats == NULL) 338 ignore_pats = xstrdup(pattern); 339 else { 340 /* old + "|" + new + NUL */ 341 len = strlen(ignore_pats) + strlen(pattern) + 2; 342 ignore_pats = xreallocarray(ignore_pats, 1, len); 343 strlcat(ignore_pats, "|", len); 344 strlcat(ignore_pats, pattern, len); 345 } 346 } 347 348 void 349 print_only(const char *path, size_t dirlen, const char *entry) 350 { 351 if (dirlen > 1) 352 dirlen--; 353 printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 354 } 355 356 void 357 print_status(int val, char *path1, char *path2, char *entry) 358 { 359 switch (val) { 360 case D_BINARY: 361 printf("Binary files %s%s and %s%s differ\n", 362 path1, entry, path2, entry); 363 break; 364 case D_DIFFER: 365 if (diff_format == D_BRIEF) 366 printf("Files %s%s and %s%s differ\n", 367 path1, entry, path2, entry); 368 break; 369 case D_SAME: 370 if (sflag) 371 printf("Files %s%s and %s%s are identical\n", 372 path1, entry, path2, entry); 373 break; 374 case D_MISMATCH1: 375 printf("File %s%s is a directory while file %s%s is a regular file\n", 376 path1, entry, path2, entry); 377 break; 378 case D_MISMATCH2: 379 printf("File %s%s is a regular file while file %s%s is a directory\n", 380 path1, entry, path2, entry); 381 break; 382 case D_SKIPPED1: 383 printf("File %s%s is not a regular file or directory and was skipped\n", 384 path1, entry); 385 break; 386 case D_SKIPPED2: 387 printf("File %s%s is not a regular file or directory and was skipped\n", 388 path2, entry); 389 break; 390 } 391 } 392 393 __dead void 394 usage(void) 395 { 396 (void)fprintf(stderr, 397 "usage: diff [-abdipTtw] [-c | -e | -f | -n | -q | -u] [-I pattern] [-L label]\n" 398 " file1 file2\n" 399 " diff [-abdipTtw] [-I pattern] [-L label] -C number file1 file2\n" 400 " diff [-abditw] [-I pattern] -D string file1 file2\n" 401 " diff [-abdipTtw] [-I pattern] [-L label] -U number file1 file2\n" 402 " diff [-abdiNPprsTtw] [-c | -e | -f | -n | -q | -u] [-I pattern]\n" 403 " [-L label] [-S name] [-X file] [-x pattern] dir1 dir2\n"); 404 405 exit(2); 406 } 407