1 /* $OpenBSD: diff.c,v 1.67 2019/06/28 13:35:00 deraadt 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/cdefs.h> 24 __FBSDID("$FreeBSD$"); 25 26 #include <sys/stat.h> 27 28 #include <ctype.h> 29 #include <err.h> 30 #include <getopt.h> 31 #include <stdlib.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <unistd.h> 35 #include <limits.h> 36 37 #include "diff.h" 38 #include "xmalloc.h" 39 40 int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag; 41 int diff_format, diff_context, status, ignore_file_case, suppress_common; 42 int tabsize = 8, width = 130; 43 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; 44 char *group_format = NULL; 45 struct stat stb1, stb2; 46 struct excludes *excludes_list; 47 regex_t ignore_re; 48 49 #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y" 50 enum { 51 OPT_TSIZE = CHAR_MAX + 1, 52 OPT_STRIPCR, 53 OPT_IGN_FN_CASE, 54 OPT_NO_IGN_FN_CASE, 55 OPT_NORMAL, 56 OPT_HORIZON_LINES, 57 OPT_CHANGED_GROUP_FORMAT, 58 OPT_SUPPRESS_COMMON, 59 }; 60 61 static struct option longopts[] = { 62 { "text", no_argument, 0, 'a' }, 63 { "ignore-space-change", no_argument, 0, 'b' }, 64 { "context", optional_argument, 0, 'C' }, 65 { "ifdef", required_argument, 0, 'D' }, 66 { "minimal", no_argument, 0, 'd' }, 67 { "ed", no_argument, 0, 'e' }, 68 { "forward-ed", no_argument, 0, 'f' }, 69 { "speed-large-files", no_argument, NULL, 'H' }, 70 { "ignore-blank-lines", no_argument, 0, 'B' }, 71 { "ignore-matching-lines", required_argument, 0, 'I' }, 72 { "ignore-case", no_argument, 0, 'i' }, 73 { "paginate", no_argument, NULL, 'l' }, 74 { "label", required_argument, 0, 'L' }, 75 { "new-file", no_argument, 0, 'N' }, 76 { "rcs", no_argument, 0, 'n' }, 77 { "unidirectional-new-file", no_argument, 0, 'P' }, 78 { "show-c-function", no_argument, 0, 'p' }, 79 { "brief", no_argument, 0, 'q' }, 80 { "recursive", no_argument, 0, 'r' }, 81 { "report-identical-files", no_argument, 0, 's' }, 82 { "starting-file", required_argument, 0, 'S' }, 83 { "expand-tabs", no_argument, 0, 't' }, 84 { "initial-tab", no_argument, 0, 'T' }, 85 { "unified", optional_argument, 0, 'U' }, 86 { "ignore-all-space", no_argument, 0, 'w' }, 87 { "width", required_argument, 0, 'W' }, 88 { "exclude", required_argument, 0, 'x' }, 89 { "exclude-from", required_argument, 0, 'X' }, 90 { "side-by-side", no_argument, NULL, 'y' }, 91 { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE }, 92 { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES }, 93 { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE }, 94 { "normal", no_argument, NULL, OPT_NORMAL }, 95 { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR }, 96 { "tabsize", required_argument, NULL, OPT_TSIZE }, 97 { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT}, 98 { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON }, 99 { NULL, 0, 0, '\0'} 100 }; 101 102 void usage(void) __dead2; 103 void conflicting_format(void) __dead2; 104 void push_excludes(char *); 105 void push_ignore_pats(char *); 106 void read_excludes_file(char *file); 107 void set_argstr(char **, char **); 108 109 int 110 main(int argc, char **argv) 111 { 112 const char *errstr = NULL; 113 char *ep, **oargv; 114 long l; 115 int ch, dflags, lastch, gotstdin, prevoptind, newarg; 116 117 oargv = argv; 118 gotstdin = 0; 119 dflags = 0; 120 lastch = '\0'; 121 prevoptind = 1; 122 newarg = 1; 123 diff_context = 3; 124 diff_format = D_UNSET; 125 #define FORMAT_MISMATCHED(type) \ 126 (diff_format != D_UNSET && diff_format != (type)) 127 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 128 switch (ch) { 129 case '0': case '1': case '2': case '3': case '4': 130 case '5': case '6': case '7': case '8': case '9': 131 if (newarg) 132 usage(); /* disallow -[0-9]+ */ 133 else if (lastch == 'c' || lastch == 'u') 134 diff_context = 0; 135 else if (!isdigit(lastch) || diff_context > INT_MAX / 10) 136 usage(); 137 diff_context = (diff_context * 10) + (ch - '0'); 138 break; 139 case 'a': 140 dflags |= D_FORCEASCII; 141 break; 142 case 'b': 143 dflags |= D_FOLDBLANKS; 144 break; 145 case 'C': 146 case 'c': 147 if (FORMAT_MISMATCHED(D_CONTEXT)) 148 conflicting_format(); 149 cflag = 1; 150 diff_format = D_CONTEXT; 151 if (optarg != NULL) { 152 l = strtol(optarg, &ep, 10); 153 if (*ep != '\0' || l < 0 || l >= INT_MAX) 154 usage(); 155 diff_context = (int)l; 156 } 157 break; 158 case 'd': 159 dflags |= D_MINIMAL; 160 break; 161 case 'D': 162 if (FORMAT_MISMATCHED(D_IFDEF)) 163 conflicting_format(); 164 diff_format = D_IFDEF; 165 ifdefname = optarg; 166 break; 167 case 'e': 168 if (FORMAT_MISMATCHED(D_EDIT)) 169 conflicting_format(); 170 diff_format = D_EDIT; 171 break; 172 case 'f': 173 if (FORMAT_MISMATCHED(D_REVERSE)) 174 conflicting_format(); 175 diff_format = D_REVERSE; 176 break; 177 case 'H': 178 /* ignore but needed for compatibility with GNU diff */ 179 break; 180 case 'h': 181 /* silently ignore for backwards compatibility */ 182 break; 183 case 'B': 184 dflags |= D_SKIPBLANKLINES; 185 break; 186 case 'I': 187 push_ignore_pats(optarg); 188 break; 189 case 'i': 190 dflags |= D_IGNORECASE; 191 break; 192 case 'L': 193 if (label[0] == NULL) 194 label[0] = optarg; 195 else if (label[1] == NULL) 196 label[1] = optarg; 197 else 198 usage(); 199 break; 200 case 'l': 201 lflag = 1; 202 break; 203 case 'N': 204 Nflag = 1; 205 break; 206 case 'n': 207 if (FORMAT_MISMATCHED(D_NREVERSE)) 208 conflicting_format(); 209 diff_format = D_NREVERSE; 210 break; 211 case 'p': 212 /* 213 * If it's not unset and it's not set to context or 214 * unified, we'll error out here as a conflicting 215 * format. If it's unset, we'll go ahead and set it to 216 * context. 217 */ 218 if (FORMAT_MISMATCHED(D_CONTEXT) && 219 FORMAT_MISMATCHED(D_UNIFIED)) 220 conflicting_format(); 221 if (diff_format == D_UNSET) 222 diff_format = D_CONTEXT; 223 dflags |= D_PROTOTYPE; 224 break; 225 case 'P': 226 Pflag = 1; 227 break; 228 case 'r': 229 rflag = 1; 230 break; 231 case 'q': 232 if (FORMAT_MISMATCHED(D_BRIEF)) 233 conflicting_format(); 234 diff_format = D_BRIEF; 235 break; 236 case 'S': 237 start = optarg; 238 break; 239 case 's': 240 sflag = 1; 241 break; 242 case 'T': 243 Tflag = 1; 244 break; 245 case 't': 246 dflags |= D_EXPANDTABS; 247 break; 248 case 'U': 249 case 'u': 250 if (FORMAT_MISMATCHED(D_UNIFIED)) 251 conflicting_format(); 252 diff_format = D_UNIFIED; 253 if (optarg != NULL) { 254 l = strtol(optarg, &ep, 10); 255 if (*ep != '\0' || l < 0 || l >= INT_MAX) 256 usage(); 257 diff_context = (int)l; 258 } 259 break; 260 case 'w': 261 dflags |= D_IGNOREBLANKS; 262 break; 263 case 'W': 264 Wflag = 1; 265 width = (int) strtonum(optarg, 1, INT_MAX, &errstr); 266 if (errstr) { 267 warnx("Invalid argument for width"); 268 usage(); 269 } 270 break; 271 case 'X': 272 read_excludes_file(optarg); 273 break; 274 case 'x': 275 push_excludes(optarg); 276 break; 277 case 'y': 278 if (FORMAT_MISMATCHED(D_SIDEBYSIDE)) 279 conflicting_format(); 280 diff_format = D_SIDEBYSIDE; 281 break; 282 case OPT_CHANGED_GROUP_FORMAT: 283 if (FORMAT_MISMATCHED(D_GFORMAT)) 284 conflicting_format(); 285 diff_format = D_GFORMAT; 286 group_format = optarg; 287 break; 288 case OPT_HORIZON_LINES: 289 break; /* XXX TODO for compatibility with GNU diff3 */ 290 case OPT_IGN_FN_CASE: 291 ignore_file_case = 1; 292 break; 293 case OPT_NO_IGN_FN_CASE: 294 ignore_file_case = 0; 295 break; 296 case OPT_NORMAL: 297 if (FORMAT_MISMATCHED(D_NORMAL)) 298 conflicting_format(); 299 diff_format = D_NORMAL; 300 break; 301 case OPT_TSIZE: 302 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr); 303 if (errstr) { 304 warnx("Invalid argument for tabsize"); 305 usage(); 306 } 307 break; 308 case OPT_STRIPCR: 309 dflags |= D_STRIPCR; 310 break; 311 case OPT_SUPPRESS_COMMON: 312 suppress_common = 1; 313 break; 314 default: 315 usage(); 316 break; 317 } 318 lastch = ch; 319 newarg = optind != prevoptind; 320 prevoptind = optind; 321 } 322 if (diff_format == D_UNSET) 323 diff_format = D_NORMAL; 324 argc -= optind; 325 argv += optind; 326 327 #ifdef __OpenBSD__ 328 if (pledge("stdio rpath tmppath", NULL) == -1) 329 err(2, "pledge"); 330 #endif 331 332 /* 333 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 334 * driver routine. Both drivers use the contents of stb1 and stb2. 335 */ 336 if (argc != 2) 337 usage(); 338 if (ignore_pats != NULL) { 339 char buf[BUFSIZ]; 340 int error; 341 342 if ((error = regcomp(&ignore_re, ignore_pats, 343 REG_NEWLINE | REG_EXTENDED)) != 0) { 344 regerror(error, &ignore_re, buf, sizeof(buf)); 345 if (*ignore_pats != '\0') 346 errx(2, "%s: %s", ignore_pats, buf); 347 else 348 errx(2, "%s", buf); 349 } 350 } 351 if (strcmp(argv[0], "-") == 0) { 352 fstat(STDIN_FILENO, &stb1); 353 gotstdin = 1; 354 } else if (stat(argv[0], &stb1) != 0) 355 err(2, "%s", argv[0]); 356 if (strcmp(argv[1], "-") == 0) { 357 fstat(STDIN_FILENO, &stb2); 358 gotstdin = 1; 359 } else if (stat(argv[1], &stb2) != 0) 360 err(2, "%s", argv[1]); 361 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 362 errx(2, "can't compare - to a directory"); 363 set_argstr(oargv, argv); 364 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 365 if (diff_format == D_IFDEF) 366 errx(2, "-D option not supported with directories"); 367 diffdir(argv[0], argv[1], dflags); 368 } else { 369 if (S_ISDIR(stb1.st_mode)) { 370 argv[0] = splice(argv[0], argv[1]); 371 if (stat(argv[0], &stb1) == -1) 372 err(2, "%s", argv[0]); 373 } 374 if (S_ISDIR(stb2.st_mode)) { 375 argv[1] = splice(argv[1], argv[0]); 376 if (stat(argv[1], &stb2) == -1) 377 err(2, "%s", argv[1]); 378 } 379 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0], 380 argv[1], ""); 381 } 382 exit(status); 383 } 384 385 void 386 set_argstr(char **av, char **ave) 387 { 388 size_t argsize; 389 char **ap; 390 391 argsize = 4 + *ave - *av + 1; 392 diffargs = xmalloc(argsize); 393 strlcpy(diffargs, "diff", argsize); 394 for (ap = av + 1; ap < ave; ap++) { 395 if (strcmp(*ap, "--") != 0) { 396 strlcat(diffargs, " ", argsize); 397 strlcat(diffargs, *ap, argsize); 398 } 399 } 400 } 401 402 /* 403 * Read in an excludes file and push each line. 404 */ 405 void 406 read_excludes_file(char *file) 407 { 408 FILE *fp; 409 char *buf, *pattern; 410 size_t len; 411 412 if (strcmp(file, "-") == 0) 413 fp = stdin; 414 else if ((fp = fopen(file, "r")) == NULL) 415 err(2, "%s", file); 416 while ((buf = fgetln(fp, &len)) != NULL) { 417 if (buf[len - 1] == '\n') 418 len--; 419 if ((pattern = strndup(buf, len)) == NULL) 420 err(2, "xstrndup"); 421 push_excludes(pattern); 422 } 423 if (strcmp(file, "-") != 0) 424 fclose(fp); 425 } 426 427 /* 428 * Push a pattern onto the excludes list. 429 */ 430 void 431 push_excludes(char *pattern) 432 { 433 struct excludes *entry; 434 435 entry = xmalloc(sizeof(*entry)); 436 entry->pattern = pattern; 437 entry->next = excludes_list; 438 excludes_list = entry; 439 } 440 441 void 442 push_ignore_pats(char *pattern) 443 { 444 size_t len; 445 446 if (ignore_pats == NULL) 447 ignore_pats = xstrdup(pattern); 448 else { 449 /* old + "|" + new + NUL */ 450 len = strlen(ignore_pats) + strlen(pattern) + 2; 451 ignore_pats = xreallocarray(ignore_pats, 1, len); 452 strlcat(ignore_pats, "|", len); 453 strlcat(ignore_pats, pattern, len); 454 } 455 } 456 457 void 458 print_only(const char *path, size_t dirlen, const char *entry) 459 { 460 if (dirlen > 1) 461 dirlen--; 462 printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 463 } 464 465 void 466 print_status(int val, char *path1, char *path2, const char *entry) 467 { 468 switch (val) { 469 case D_BINARY: 470 printf("Binary files %s%s and %s%s differ\n", 471 path1, entry, path2, entry); 472 break; 473 case D_DIFFER: 474 if (diff_format == D_BRIEF) 475 printf("Files %s%s and %s%s differ\n", 476 path1, entry, path2, entry); 477 break; 478 case D_SAME: 479 if (sflag) 480 printf("Files %s%s and %s%s are identical\n", 481 path1, entry, path2, entry); 482 break; 483 case D_MISMATCH1: 484 printf("File %s%s is a directory while file %s%s is a regular file\n", 485 path1, entry, path2, entry); 486 break; 487 case D_MISMATCH2: 488 printf("File %s%s is a regular file while file %s%s is a directory\n", 489 path1, entry, path2, entry); 490 break; 491 case D_SKIPPED1: 492 printf("File %s%s is not a regular file or directory and was skipped\n", 493 path1, entry); 494 break; 495 case D_SKIPPED2: 496 printf("File %s%s is not a regular file or directory and was skipped\n", 497 path2, entry); 498 break; 499 } 500 } 501 502 void 503 usage(void) 504 { 505 (void)fprintf(stderr, 506 "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 507 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 508 " [-I pattern] [-L label] file1 file2\n" 509 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 510 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 511 " -C number file1 file2\n" 512 " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" 513 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n" 514 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 515 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n" 516 " -U number file1 file2\n" 517 " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 518 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n" 519 " [-S name] [-X file] [-x pattern] dir1 dir2\n" 520 " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n" 521 " [--ignore-blank-lines] [--ignore-case] [--minimal]\n" 522 " [--no-ignore-file-name-case] [--strip-trailing-cr]\n" 523 " [--suppress-common-lines] [--tabsize] [--text] [--width]\n" 524 " -y | --side-by-side file1 file2\n"); 525 526 exit(2); 527 } 528 529 void 530 conflicting_format(void) 531 { 532 533 fprintf(stderr, "error: conflicting output format options.\n"); 534 usage(); 535 } 536