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