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