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