112d9c0dcSBaptiste Daroussin /* $OpenBSD: diff.c,v 1.67 2019/06/28 13:35:00 deraadt Exp $ */ 23bbe3f67SBaptiste Daroussin 33bbe3f67SBaptiste Daroussin /* 43bbe3f67SBaptiste Daroussin * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> 53bbe3f67SBaptiste Daroussin * 63bbe3f67SBaptiste Daroussin * Permission to use, copy, modify, and distribute this software for any 73bbe3f67SBaptiste Daroussin * purpose with or without fee is hereby granted, provided that the above 83bbe3f67SBaptiste Daroussin * copyright notice and this permission notice appear in all copies. 93bbe3f67SBaptiste Daroussin * 103bbe3f67SBaptiste Daroussin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 113bbe3f67SBaptiste Daroussin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 123bbe3f67SBaptiste Daroussin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 133bbe3f67SBaptiste Daroussin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 143bbe3f67SBaptiste Daroussin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 153bbe3f67SBaptiste Daroussin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 163bbe3f67SBaptiste Daroussin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 173bbe3f67SBaptiste Daroussin * 183bbe3f67SBaptiste Daroussin * Sponsored in part by the Defense Advanced Research Projects 193bbe3f67SBaptiste Daroussin * Agency (DARPA) and Air Force Research Laboratory, Air Force 203bbe3f67SBaptiste Daroussin * Materiel Command, USAF, under agreement number F39502-99-1-0512. 213bbe3f67SBaptiste Daroussin */ 223bbe3f67SBaptiste Daroussin 233bbe3f67SBaptiste Daroussin #include <sys/cdefs.h> 243bbe3f67SBaptiste Daroussin __FBSDID("$FreeBSD$"); 253bbe3f67SBaptiste Daroussin 263bbe3f67SBaptiste Daroussin #include <sys/stat.h> 273bbe3f67SBaptiste Daroussin 283bbe3f67SBaptiste Daroussin #include <ctype.h> 293bbe3f67SBaptiste Daroussin #include <err.h> 30*2816b999SBaptiste Daroussin #include <errno.h> 313bbe3f67SBaptiste Daroussin #include <getopt.h> 323bbe3f67SBaptiste Daroussin #include <stdlib.h> 333bbe3f67SBaptiste Daroussin #include <stdio.h> 343bbe3f67SBaptiste Daroussin #include <string.h> 353bbe3f67SBaptiste Daroussin #include <unistd.h> 363bbe3f67SBaptiste Daroussin #include <limits.h> 373bbe3f67SBaptiste Daroussin 383bbe3f67SBaptiste Daroussin #include "diff.h" 393bbe3f67SBaptiste Daroussin #include "xmalloc.h" 403bbe3f67SBaptiste Daroussin 4164793e74SBaptiste Daroussin int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag; 4264793e74SBaptiste Daroussin int diff_format, diff_context, status, ignore_file_case, suppress_common; 4364793e74SBaptiste Daroussin int tabsize = 8, width = 130; 443bbe3f67SBaptiste Daroussin char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; 45fddcb7b8SBaptiste Daroussin char *group_format = NULL; 463bbe3f67SBaptiste Daroussin struct stat stb1, stb2; 473bbe3f67SBaptiste Daroussin struct excludes *excludes_list; 483bbe3f67SBaptiste Daroussin regex_t ignore_re; 493bbe3f67SBaptiste Daroussin 5064793e74SBaptiste Daroussin #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y" 513bbe3f67SBaptiste Daroussin enum { 523bbe3f67SBaptiste Daroussin OPT_TSIZE = CHAR_MAX + 1, 533bbe3f67SBaptiste Daroussin OPT_STRIPCR, 543bbe3f67SBaptiste Daroussin OPT_IGN_FN_CASE, 553bbe3f67SBaptiste Daroussin OPT_NO_IGN_FN_CASE, 563bbe3f67SBaptiste Daroussin OPT_NORMAL, 57901a475bSBaptiste Daroussin OPT_HORIZON_LINES, 58fddcb7b8SBaptiste Daroussin OPT_CHANGED_GROUP_FORMAT, 5964793e74SBaptiste Daroussin OPT_SUPPRESS_COMMON, 603bbe3f67SBaptiste Daroussin }; 613bbe3f67SBaptiste Daroussin 623bbe3f67SBaptiste Daroussin static struct option longopts[] = { 633bbe3f67SBaptiste Daroussin { "text", no_argument, 0, 'a' }, 643bbe3f67SBaptiste Daroussin { "ignore-space-change", no_argument, 0, 'b' }, 653bbe3f67SBaptiste Daroussin { "context", optional_argument, 0, 'C' }, 663bbe3f67SBaptiste Daroussin { "ifdef", required_argument, 0, 'D' }, 673bbe3f67SBaptiste Daroussin { "minimal", no_argument, 0, 'd' }, 683bbe3f67SBaptiste Daroussin { "ed", no_argument, 0, 'e' }, 693bbe3f67SBaptiste Daroussin { "forward-ed", no_argument, 0, 'f' }, 70ea67c7a0SJohn Baldwin { "speed-large-files", no_argument, NULL, 'H' }, 71e68edb8cSKyle Evans { "ignore-blank-lines", no_argument, 0, 'B' }, 723bbe3f67SBaptiste Daroussin { "ignore-matching-lines", required_argument, 0, 'I' }, 733bbe3f67SBaptiste Daroussin { "ignore-case", no_argument, 0, 'i' }, 743bbe3f67SBaptiste Daroussin { "paginate", no_argument, NULL, 'l' }, 753bbe3f67SBaptiste Daroussin { "label", required_argument, 0, 'L' }, 763bbe3f67SBaptiste Daroussin { "new-file", no_argument, 0, 'N' }, 773bbe3f67SBaptiste Daroussin { "rcs", no_argument, 0, 'n' }, 783bbe3f67SBaptiste Daroussin { "unidirectional-new-file", no_argument, 0, 'P' }, 793bbe3f67SBaptiste Daroussin { "show-c-function", no_argument, 0, 'p' }, 803bbe3f67SBaptiste Daroussin { "brief", no_argument, 0, 'q' }, 813bbe3f67SBaptiste Daroussin { "recursive", no_argument, 0, 'r' }, 823bbe3f67SBaptiste Daroussin { "report-identical-files", no_argument, 0, 's' }, 833bbe3f67SBaptiste Daroussin { "starting-file", required_argument, 0, 'S' }, 843bbe3f67SBaptiste Daroussin { "expand-tabs", no_argument, 0, 't' }, 853bbe3f67SBaptiste Daroussin { "initial-tab", no_argument, 0, 'T' }, 863bbe3f67SBaptiste Daroussin { "unified", optional_argument, 0, 'U' }, 873bbe3f67SBaptiste Daroussin { "ignore-all-space", no_argument, 0, 'w' }, 8864793e74SBaptiste Daroussin { "width", required_argument, 0, 'W' }, 893bbe3f67SBaptiste Daroussin { "exclude", required_argument, 0, 'x' }, 903bbe3f67SBaptiste Daroussin { "exclude-from", required_argument, 0, 'X' }, 9164793e74SBaptiste Daroussin { "side-by-side", no_argument, NULL, 'y' }, 923bbe3f67SBaptiste Daroussin { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE }, 93901a475bSBaptiste Daroussin { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES }, 943bbe3f67SBaptiste Daroussin { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE }, 953bbe3f67SBaptiste Daroussin { "normal", no_argument, NULL, OPT_NORMAL }, 963bbe3f67SBaptiste Daroussin { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR }, 9743fbd654SKyle Evans { "tabsize", required_argument, NULL, OPT_TSIZE }, 98fddcb7b8SBaptiste Daroussin { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT}, 9964793e74SBaptiste Daroussin { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON }, 1003bbe3f67SBaptiste Daroussin { NULL, 0, 0, '\0'} 1013bbe3f67SBaptiste Daroussin }; 1023bbe3f67SBaptiste Daroussin 1033bbe3f67SBaptiste Daroussin void usage(void) __dead2; 104db9cccfbSKyle Evans void conflicting_format(void) __dead2; 1053bbe3f67SBaptiste Daroussin void push_excludes(char *); 1063bbe3f67SBaptiste Daroussin void push_ignore_pats(char *); 1073bbe3f67SBaptiste Daroussin void read_excludes_file(char *file); 1083bbe3f67SBaptiste Daroussin void set_argstr(char **, char **); 1093bbe3f67SBaptiste Daroussin 1103bbe3f67SBaptiste Daroussin int 1113bbe3f67SBaptiste Daroussin main(int argc, char **argv) 1123bbe3f67SBaptiste Daroussin { 1133bbe3f67SBaptiste Daroussin const char *errstr = NULL; 1143bbe3f67SBaptiste Daroussin char *ep, **oargv; 1153bbe3f67SBaptiste Daroussin long l; 1163bbe3f67SBaptiste Daroussin int ch, dflags, lastch, gotstdin, prevoptind, newarg; 1173bbe3f67SBaptiste Daroussin 1183bbe3f67SBaptiste Daroussin oargv = argv; 1193bbe3f67SBaptiste Daroussin gotstdin = 0; 1203bbe3f67SBaptiste Daroussin dflags = 0; 1213bbe3f67SBaptiste Daroussin lastch = '\0'; 1223bbe3f67SBaptiste Daroussin prevoptind = 1; 1233bbe3f67SBaptiste Daroussin newarg = 1; 1243bbe3f67SBaptiste Daroussin diff_context = 3; 125db9cccfbSKyle Evans diff_format = D_UNSET; 12667c90765SKyle Evans #define FORMAT_MISMATCHED(type) \ 12767c90765SKyle Evans (diff_format != D_UNSET && diff_format != (type)) 1283bbe3f67SBaptiste Daroussin while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 1293bbe3f67SBaptiste Daroussin switch (ch) { 1303bbe3f67SBaptiste Daroussin case '0': case '1': case '2': case '3': case '4': 1313bbe3f67SBaptiste Daroussin case '5': case '6': case '7': case '8': case '9': 1323bbe3f67SBaptiste Daroussin if (newarg) 1333bbe3f67SBaptiste Daroussin usage(); /* disallow -[0-9]+ */ 1343bbe3f67SBaptiste Daroussin else if (lastch == 'c' || lastch == 'u') 1353bbe3f67SBaptiste Daroussin diff_context = 0; 1363bbe3f67SBaptiste Daroussin else if (!isdigit(lastch) || diff_context > INT_MAX / 10) 1373bbe3f67SBaptiste Daroussin usage(); 1383bbe3f67SBaptiste Daroussin diff_context = (diff_context * 10) + (ch - '0'); 1393bbe3f67SBaptiste Daroussin break; 1403bbe3f67SBaptiste Daroussin case 'a': 1413bbe3f67SBaptiste Daroussin dflags |= D_FORCEASCII; 1423bbe3f67SBaptiste Daroussin break; 1433bbe3f67SBaptiste Daroussin case 'b': 1443bbe3f67SBaptiste Daroussin dflags |= D_FOLDBLANKS; 1453bbe3f67SBaptiste Daroussin break; 1463bbe3f67SBaptiste Daroussin case 'C': 1473bbe3f67SBaptiste Daroussin case 'c': 14867c90765SKyle Evans if (FORMAT_MISMATCHED(D_CONTEXT)) 149db9cccfbSKyle Evans conflicting_format(); 1503bbe3f67SBaptiste Daroussin cflag = 1; 1513bbe3f67SBaptiste Daroussin diff_format = D_CONTEXT; 1523bbe3f67SBaptiste Daroussin if (optarg != NULL) { 1533bbe3f67SBaptiste Daroussin l = strtol(optarg, &ep, 10); 1543bbe3f67SBaptiste Daroussin if (*ep != '\0' || l < 0 || l >= INT_MAX) 1553bbe3f67SBaptiste Daroussin usage(); 1563bbe3f67SBaptiste Daroussin diff_context = (int)l; 1573bbe3f67SBaptiste Daroussin } 1583bbe3f67SBaptiste Daroussin break; 1593bbe3f67SBaptiste Daroussin case 'd': 1603bbe3f67SBaptiste Daroussin dflags |= D_MINIMAL; 1613bbe3f67SBaptiste Daroussin break; 1623bbe3f67SBaptiste Daroussin case 'D': 16367c90765SKyle Evans if (FORMAT_MISMATCHED(D_IFDEF)) 164db9cccfbSKyle Evans conflicting_format(); 1653bbe3f67SBaptiste Daroussin diff_format = D_IFDEF; 1663bbe3f67SBaptiste Daroussin ifdefname = optarg; 1673bbe3f67SBaptiste Daroussin break; 1683bbe3f67SBaptiste Daroussin case 'e': 16967c90765SKyle Evans if (FORMAT_MISMATCHED(D_EDIT)) 170db9cccfbSKyle Evans conflicting_format(); 1713bbe3f67SBaptiste Daroussin diff_format = D_EDIT; 1723bbe3f67SBaptiste Daroussin break; 1733bbe3f67SBaptiste Daroussin case 'f': 17467c90765SKyle Evans if (FORMAT_MISMATCHED(D_REVERSE)) 175db9cccfbSKyle Evans conflicting_format(); 1763bbe3f67SBaptiste Daroussin diff_format = D_REVERSE; 1773bbe3f67SBaptiste Daroussin break; 178ea67c7a0SJohn Baldwin case 'H': 179ea67c7a0SJohn Baldwin /* ignore but needed for compatibility with GNU diff */ 180ea67c7a0SJohn Baldwin break; 1813bbe3f67SBaptiste Daroussin case 'h': 1823bbe3f67SBaptiste Daroussin /* silently ignore for backwards compatibility */ 1833bbe3f67SBaptiste Daroussin break; 184e68edb8cSKyle Evans case 'B': 185e68edb8cSKyle Evans dflags |= D_SKIPBLANKLINES; 186e68edb8cSKyle Evans break; 1873bbe3f67SBaptiste Daroussin case 'I': 1883bbe3f67SBaptiste Daroussin push_ignore_pats(optarg); 1893bbe3f67SBaptiste Daroussin break; 1903bbe3f67SBaptiste Daroussin case 'i': 1913bbe3f67SBaptiste Daroussin dflags |= D_IGNORECASE; 1923bbe3f67SBaptiste Daroussin break; 1933bbe3f67SBaptiste Daroussin case 'L': 1943bbe3f67SBaptiste Daroussin if (label[0] == NULL) 1953bbe3f67SBaptiste Daroussin label[0] = optarg; 1963bbe3f67SBaptiste Daroussin else if (label[1] == NULL) 1973bbe3f67SBaptiste Daroussin label[1] = optarg; 1983bbe3f67SBaptiste Daroussin else 1993bbe3f67SBaptiste Daroussin usage(); 2003bbe3f67SBaptiste Daroussin break; 2013bbe3f67SBaptiste Daroussin case 'l': 2023bbe3f67SBaptiste Daroussin lflag = 1; 2033bbe3f67SBaptiste Daroussin break; 2043bbe3f67SBaptiste Daroussin case 'N': 2053bbe3f67SBaptiste Daroussin Nflag = 1; 2063bbe3f67SBaptiste Daroussin break; 2073bbe3f67SBaptiste Daroussin case 'n': 20867c90765SKyle Evans if (FORMAT_MISMATCHED(D_NREVERSE)) 209db9cccfbSKyle Evans conflicting_format(); 2103bbe3f67SBaptiste Daroussin diff_format = D_NREVERSE; 2113bbe3f67SBaptiste Daroussin break; 2123bbe3f67SBaptiste Daroussin case 'p': 21367c90765SKyle Evans /* 21467c90765SKyle Evans * If it's not unset and it's not set to context or 21567c90765SKyle Evans * unified, we'll error out here as a conflicting 21667c90765SKyle Evans * format. If it's unset, we'll go ahead and set it to 21767c90765SKyle Evans * context. 21867c90765SKyle Evans */ 21967c90765SKyle Evans if (FORMAT_MISMATCHED(D_CONTEXT) && 22067c90765SKyle Evans FORMAT_MISMATCHED(D_UNIFIED)) 22167c90765SKyle Evans conflicting_format(); 222db9cccfbSKyle Evans if (diff_format == D_UNSET) 2233bbe3f67SBaptiste Daroussin diff_format = D_CONTEXT; 2243bbe3f67SBaptiste Daroussin dflags |= D_PROTOTYPE; 2253bbe3f67SBaptiste Daroussin break; 2263bbe3f67SBaptiste Daroussin case 'P': 2273bbe3f67SBaptiste Daroussin Pflag = 1; 2283bbe3f67SBaptiste Daroussin break; 2293bbe3f67SBaptiste Daroussin case 'r': 2303bbe3f67SBaptiste Daroussin rflag = 1; 2313bbe3f67SBaptiste Daroussin break; 2323bbe3f67SBaptiste Daroussin case 'q': 23367c90765SKyle Evans if (FORMAT_MISMATCHED(D_BRIEF)) 234db9cccfbSKyle Evans conflicting_format(); 2353bbe3f67SBaptiste Daroussin diff_format = D_BRIEF; 2363bbe3f67SBaptiste Daroussin break; 2373bbe3f67SBaptiste Daroussin case 'S': 2383bbe3f67SBaptiste Daroussin start = optarg; 2393bbe3f67SBaptiste Daroussin break; 2403bbe3f67SBaptiste Daroussin case 's': 2413bbe3f67SBaptiste Daroussin sflag = 1; 2423bbe3f67SBaptiste Daroussin break; 2433bbe3f67SBaptiste Daroussin case 'T': 2443bbe3f67SBaptiste Daroussin Tflag = 1; 2453bbe3f67SBaptiste Daroussin break; 2463bbe3f67SBaptiste Daroussin case 't': 2473bbe3f67SBaptiste Daroussin dflags |= D_EXPANDTABS; 2483bbe3f67SBaptiste Daroussin break; 2493bbe3f67SBaptiste Daroussin case 'U': 2503bbe3f67SBaptiste Daroussin case 'u': 25167c90765SKyle Evans if (FORMAT_MISMATCHED(D_UNIFIED)) 252db9cccfbSKyle Evans conflicting_format(); 2533bbe3f67SBaptiste Daroussin diff_format = D_UNIFIED; 2543bbe3f67SBaptiste Daroussin if (optarg != NULL) { 2553bbe3f67SBaptiste Daroussin l = strtol(optarg, &ep, 10); 2563bbe3f67SBaptiste Daroussin if (*ep != '\0' || l < 0 || l >= INT_MAX) 2573bbe3f67SBaptiste Daroussin usage(); 2583bbe3f67SBaptiste Daroussin diff_context = (int)l; 2593bbe3f67SBaptiste Daroussin } 2603bbe3f67SBaptiste Daroussin break; 2613bbe3f67SBaptiste Daroussin case 'w': 2623bbe3f67SBaptiste Daroussin dflags |= D_IGNOREBLANKS; 2633bbe3f67SBaptiste Daroussin break; 26464793e74SBaptiste Daroussin case 'W': 26564793e74SBaptiste Daroussin Wflag = 1; 26664793e74SBaptiste Daroussin width = (int) strtonum(optarg, 1, INT_MAX, &errstr); 26764793e74SBaptiste Daroussin if (errstr) { 26864793e74SBaptiste Daroussin warnx("Invalid argument for width"); 26964793e74SBaptiste Daroussin usage(); 27064793e74SBaptiste Daroussin } 27164793e74SBaptiste Daroussin break; 2723bbe3f67SBaptiste Daroussin case 'X': 2733bbe3f67SBaptiste Daroussin read_excludes_file(optarg); 2743bbe3f67SBaptiste Daroussin break; 2753bbe3f67SBaptiste Daroussin case 'x': 2763bbe3f67SBaptiste Daroussin push_excludes(optarg); 2773bbe3f67SBaptiste Daroussin break; 27864793e74SBaptiste Daroussin case 'y': 27967c90765SKyle Evans if (FORMAT_MISMATCHED(D_SIDEBYSIDE)) 280db9cccfbSKyle Evans conflicting_format(); 28164793e74SBaptiste Daroussin diff_format = D_SIDEBYSIDE; 28264793e74SBaptiste Daroussin break; 283fddcb7b8SBaptiste Daroussin case OPT_CHANGED_GROUP_FORMAT: 28467c90765SKyle Evans if (FORMAT_MISMATCHED(D_GFORMAT)) 285db9cccfbSKyle Evans conflicting_format(); 286fddcb7b8SBaptiste Daroussin diff_format = D_GFORMAT; 287fddcb7b8SBaptiste Daroussin group_format = optarg; 288fddcb7b8SBaptiste Daroussin break; 289901a475bSBaptiste Daroussin case OPT_HORIZON_LINES: 290901a475bSBaptiste Daroussin break; /* XXX TODO for compatibility with GNU diff3 */ 2913bbe3f67SBaptiste Daroussin case OPT_IGN_FN_CASE: 2923bbe3f67SBaptiste Daroussin ignore_file_case = 1; 2933bbe3f67SBaptiste Daroussin break; 2943bbe3f67SBaptiste Daroussin case OPT_NO_IGN_FN_CASE: 2953bbe3f67SBaptiste Daroussin ignore_file_case = 0; 2963bbe3f67SBaptiste Daroussin break; 2973bbe3f67SBaptiste Daroussin case OPT_NORMAL: 29867c90765SKyle Evans if (FORMAT_MISMATCHED(D_NORMAL)) 299db9cccfbSKyle Evans conflicting_format(); 3003bbe3f67SBaptiste Daroussin diff_format = D_NORMAL; 3013bbe3f67SBaptiste Daroussin break; 3023bbe3f67SBaptiste Daroussin case OPT_TSIZE: 3033bbe3f67SBaptiste Daroussin tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr); 3043bbe3f67SBaptiste Daroussin if (errstr) { 3053bbe3f67SBaptiste Daroussin warnx("Invalid argument for tabsize"); 3063bbe3f67SBaptiste Daroussin usage(); 3073bbe3f67SBaptiste Daroussin } 3083bbe3f67SBaptiste Daroussin break; 3093bbe3f67SBaptiste Daroussin case OPT_STRIPCR: 3103bbe3f67SBaptiste Daroussin dflags |= D_STRIPCR; 3113bbe3f67SBaptiste Daroussin break; 31264793e74SBaptiste Daroussin case OPT_SUPPRESS_COMMON: 31364793e74SBaptiste Daroussin suppress_common = 1; 31464793e74SBaptiste Daroussin break; 3153bbe3f67SBaptiste Daroussin default: 3163bbe3f67SBaptiste Daroussin usage(); 3173bbe3f67SBaptiste Daroussin break; 3183bbe3f67SBaptiste Daroussin } 3193bbe3f67SBaptiste Daroussin lastch = ch; 3203bbe3f67SBaptiste Daroussin newarg = optind != prevoptind; 3213bbe3f67SBaptiste Daroussin prevoptind = optind; 3223bbe3f67SBaptiste Daroussin } 323db9cccfbSKyle Evans if (diff_format == D_UNSET) 324db9cccfbSKyle Evans diff_format = D_NORMAL; 3253bbe3f67SBaptiste Daroussin argc -= optind; 3263bbe3f67SBaptiste Daroussin argv += optind; 3273bbe3f67SBaptiste Daroussin 3283bbe3f67SBaptiste Daroussin #ifdef __OpenBSD__ 3293bbe3f67SBaptiste Daroussin if (pledge("stdio rpath tmppath", NULL) == -1) 3303bbe3f67SBaptiste Daroussin err(2, "pledge"); 3313bbe3f67SBaptiste Daroussin #endif 3323bbe3f67SBaptiste Daroussin 3333bbe3f67SBaptiste Daroussin /* 3343bbe3f67SBaptiste Daroussin * Do sanity checks, fill in stb1 and stb2 and call the appropriate 3353bbe3f67SBaptiste Daroussin * driver routine. Both drivers use the contents of stb1 and stb2. 3363bbe3f67SBaptiste Daroussin */ 3373bbe3f67SBaptiste Daroussin if (argc != 2) 3383bbe3f67SBaptiste Daroussin usage(); 3393bbe3f67SBaptiste Daroussin if (ignore_pats != NULL) { 3403bbe3f67SBaptiste Daroussin char buf[BUFSIZ]; 3413bbe3f67SBaptiste Daroussin int error; 3423bbe3f67SBaptiste Daroussin 3433bbe3f67SBaptiste Daroussin if ((error = regcomp(&ignore_re, ignore_pats, 3443bbe3f67SBaptiste Daroussin REG_NEWLINE | REG_EXTENDED)) != 0) { 3453bbe3f67SBaptiste Daroussin regerror(error, &ignore_re, buf, sizeof(buf)); 3463bbe3f67SBaptiste Daroussin if (*ignore_pats != '\0') 3473bbe3f67SBaptiste Daroussin errx(2, "%s: %s", ignore_pats, buf); 3483bbe3f67SBaptiste Daroussin else 3493bbe3f67SBaptiste Daroussin errx(2, "%s", buf); 3503bbe3f67SBaptiste Daroussin } 3513bbe3f67SBaptiste Daroussin } 3523bbe3f67SBaptiste Daroussin if (strcmp(argv[0], "-") == 0) { 3533bbe3f67SBaptiste Daroussin fstat(STDIN_FILENO, &stb1); 3543bbe3f67SBaptiste Daroussin gotstdin = 1; 355*2816b999SBaptiste Daroussin } else if (stat(argv[0], &stb1) != 0) { 356*2816b999SBaptiste Daroussin if (!Nflag || errno != ENOENT) 3573bbe3f67SBaptiste Daroussin err(2, "%s", argv[0]); 358*2816b999SBaptiste Daroussin dflags |= D_EMPTY1; 359*2816b999SBaptiste Daroussin memset(&stb1, 0, sizeof(struct stat)); 360*2816b999SBaptiste Daroussin } 361*2816b999SBaptiste Daroussin 3623bbe3f67SBaptiste Daroussin if (strcmp(argv[1], "-") == 0) { 3633bbe3f67SBaptiste Daroussin fstat(STDIN_FILENO, &stb2); 3643bbe3f67SBaptiste Daroussin gotstdin = 1; 365*2816b999SBaptiste Daroussin } else if (stat(argv[1], &stb2) != 0) { 366*2816b999SBaptiste Daroussin if (!Nflag || errno != ENOENT) 3673bbe3f67SBaptiste Daroussin err(2, "%s", argv[1]); 368*2816b999SBaptiste Daroussin dflags |= D_EMPTY2; 369*2816b999SBaptiste Daroussin memset(&stb2, 0, sizeof(stb2)); 370*2816b999SBaptiste Daroussin stb2.st_mode = stb1.st_mode; 371*2816b999SBaptiste Daroussin } 372*2816b999SBaptiste Daroussin 373*2816b999SBaptiste Daroussin if (dflags & D_EMPTY1 && dflags & D_EMPTY2){ 374*2816b999SBaptiste Daroussin warn("%s", argv[0]); 375*2816b999SBaptiste Daroussin warn("%s", argv[1]); 376*2816b999SBaptiste Daroussin exit(2); 377*2816b999SBaptiste Daroussin } 378*2816b999SBaptiste Daroussin 379*2816b999SBaptiste Daroussin if (stb1.st_mode == 0) 380*2816b999SBaptiste Daroussin stb1.st_mode = stb2.st_mode; 381*2816b999SBaptiste Daroussin 3823bbe3f67SBaptiste Daroussin if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 3833bbe3f67SBaptiste Daroussin errx(2, "can't compare - to a directory"); 3843bbe3f67SBaptiste Daroussin set_argstr(oargv, argv); 3853bbe3f67SBaptiste Daroussin if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 3863bbe3f67SBaptiste Daroussin if (diff_format == D_IFDEF) 3873bbe3f67SBaptiste Daroussin errx(2, "-D option not supported with directories"); 3883bbe3f67SBaptiste Daroussin diffdir(argv[0], argv[1], dflags); 3893bbe3f67SBaptiste Daroussin } else { 3903bbe3f67SBaptiste Daroussin if (S_ISDIR(stb1.st_mode)) { 3913bbe3f67SBaptiste Daroussin argv[0] = splice(argv[0], argv[1]); 39212d9c0dcSBaptiste Daroussin if (stat(argv[0], &stb1) == -1) 3933bbe3f67SBaptiste Daroussin err(2, "%s", argv[0]); 3943bbe3f67SBaptiste Daroussin } 3953bbe3f67SBaptiste Daroussin if (S_ISDIR(stb2.st_mode)) { 3963bbe3f67SBaptiste Daroussin argv[1] = splice(argv[1], argv[0]); 39712d9c0dcSBaptiste Daroussin if (stat(argv[1], &stb2) == -1) 3983bbe3f67SBaptiste Daroussin err(2, "%s", argv[1]); 3993bbe3f67SBaptiste Daroussin } 4003bbe3f67SBaptiste Daroussin print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0], 4013bbe3f67SBaptiste Daroussin argv[1], ""); 4023bbe3f67SBaptiste Daroussin } 4033bbe3f67SBaptiste Daroussin exit(status); 4043bbe3f67SBaptiste Daroussin } 4053bbe3f67SBaptiste Daroussin 4063bbe3f67SBaptiste Daroussin void 4073bbe3f67SBaptiste Daroussin set_argstr(char **av, char **ave) 4083bbe3f67SBaptiste Daroussin { 4093bbe3f67SBaptiste Daroussin size_t argsize; 4103bbe3f67SBaptiste Daroussin char **ap; 4113bbe3f67SBaptiste Daroussin 4123bbe3f67SBaptiste Daroussin argsize = 4 + *ave - *av + 1; 4133bbe3f67SBaptiste Daroussin diffargs = xmalloc(argsize); 4143bbe3f67SBaptiste Daroussin strlcpy(diffargs, "diff", argsize); 4153bbe3f67SBaptiste Daroussin for (ap = av + 1; ap < ave; ap++) { 4163bbe3f67SBaptiste Daroussin if (strcmp(*ap, "--") != 0) { 4173bbe3f67SBaptiste Daroussin strlcat(diffargs, " ", argsize); 4183bbe3f67SBaptiste Daroussin strlcat(diffargs, *ap, argsize); 4193bbe3f67SBaptiste Daroussin } 4203bbe3f67SBaptiste Daroussin } 4213bbe3f67SBaptiste Daroussin } 4223bbe3f67SBaptiste Daroussin 4233bbe3f67SBaptiste Daroussin /* 4243bbe3f67SBaptiste Daroussin * Read in an excludes file and push each line. 4253bbe3f67SBaptiste Daroussin */ 4263bbe3f67SBaptiste Daroussin void 4273bbe3f67SBaptiste Daroussin read_excludes_file(char *file) 4283bbe3f67SBaptiste Daroussin { 4293bbe3f67SBaptiste Daroussin FILE *fp; 4303bbe3f67SBaptiste Daroussin char *buf, *pattern; 4313bbe3f67SBaptiste Daroussin size_t len; 4323bbe3f67SBaptiste Daroussin 4333bbe3f67SBaptiste Daroussin if (strcmp(file, "-") == 0) 4343bbe3f67SBaptiste Daroussin fp = stdin; 4353bbe3f67SBaptiste Daroussin else if ((fp = fopen(file, "r")) == NULL) 4363bbe3f67SBaptiste Daroussin err(2, "%s", file); 4373bbe3f67SBaptiste Daroussin while ((buf = fgetln(fp, &len)) != NULL) { 4383bbe3f67SBaptiste Daroussin if (buf[len - 1] == '\n') 4393bbe3f67SBaptiste Daroussin len--; 4405c8e56f6SBaptiste Daroussin if ((pattern = strndup(buf, len)) == NULL) 4415c8e56f6SBaptiste Daroussin err(2, "xstrndup"); 4423bbe3f67SBaptiste Daroussin push_excludes(pattern); 4433bbe3f67SBaptiste Daroussin } 4443bbe3f67SBaptiste Daroussin if (strcmp(file, "-") != 0) 4453bbe3f67SBaptiste Daroussin fclose(fp); 4463bbe3f67SBaptiste Daroussin } 4473bbe3f67SBaptiste Daroussin 4483bbe3f67SBaptiste Daroussin /* 4493bbe3f67SBaptiste Daroussin * Push a pattern onto the excludes list. 4503bbe3f67SBaptiste Daroussin */ 4513bbe3f67SBaptiste Daroussin void 4523bbe3f67SBaptiste Daroussin push_excludes(char *pattern) 4533bbe3f67SBaptiste Daroussin { 4543bbe3f67SBaptiste Daroussin struct excludes *entry; 4553bbe3f67SBaptiste Daroussin 4563bbe3f67SBaptiste Daroussin entry = xmalloc(sizeof(*entry)); 4573bbe3f67SBaptiste Daroussin entry->pattern = pattern; 4583bbe3f67SBaptiste Daroussin entry->next = excludes_list; 4593bbe3f67SBaptiste Daroussin excludes_list = entry; 4603bbe3f67SBaptiste Daroussin } 4613bbe3f67SBaptiste Daroussin 4623bbe3f67SBaptiste Daroussin void 4633bbe3f67SBaptiste Daroussin push_ignore_pats(char *pattern) 4643bbe3f67SBaptiste Daroussin { 4653bbe3f67SBaptiste Daroussin size_t len; 4663bbe3f67SBaptiste Daroussin 4673bbe3f67SBaptiste Daroussin if (ignore_pats == NULL) 4683bbe3f67SBaptiste Daroussin ignore_pats = xstrdup(pattern); 4693bbe3f67SBaptiste Daroussin else { 4703bbe3f67SBaptiste Daroussin /* old + "|" + new + NUL */ 4713bbe3f67SBaptiste Daroussin len = strlen(ignore_pats) + strlen(pattern) + 2; 4723bbe3f67SBaptiste Daroussin ignore_pats = xreallocarray(ignore_pats, 1, len); 4733bbe3f67SBaptiste Daroussin strlcat(ignore_pats, "|", len); 4743bbe3f67SBaptiste Daroussin strlcat(ignore_pats, pattern, len); 4753bbe3f67SBaptiste Daroussin } 4763bbe3f67SBaptiste Daroussin } 4773bbe3f67SBaptiste Daroussin 4783bbe3f67SBaptiste Daroussin void 4793bbe3f67SBaptiste Daroussin print_only(const char *path, size_t dirlen, const char *entry) 4803bbe3f67SBaptiste Daroussin { 4813bbe3f67SBaptiste Daroussin if (dirlen > 1) 4823bbe3f67SBaptiste Daroussin dirlen--; 4833bbe3f67SBaptiste Daroussin printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 4843bbe3f67SBaptiste Daroussin } 4853bbe3f67SBaptiste Daroussin 4863bbe3f67SBaptiste Daroussin void 4873bbe3f67SBaptiste Daroussin print_status(int val, char *path1, char *path2, const char *entry) 4883bbe3f67SBaptiste Daroussin { 489f096ed98SBaptiste Daroussin if (label[0] != NULL) path1 = label[0]; 490f096ed98SBaptiste Daroussin if (label[1] != NULL) path2 = label[1]; 491f096ed98SBaptiste Daroussin 4923bbe3f67SBaptiste Daroussin switch (val) { 4933bbe3f67SBaptiste Daroussin case D_BINARY: 4943bbe3f67SBaptiste Daroussin printf("Binary files %s%s and %s%s differ\n", 4953bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4963bbe3f67SBaptiste Daroussin break; 4973bbe3f67SBaptiste Daroussin case D_DIFFER: 4983bbe3f67SBaptiste Daroussin if (diff_format == D_BRIEF) 4993bbe3f67SBaptiste Daroussin printf("Files %s%s and %s%s differ\n", 5003bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 5013bbe3f67SBaptiste Daroussin break; 5023bbe3f67SBaptiste Daroussin case D_SAME: 5033bbe3f67SBaptiste Daroussin if (sflag) 5043bbe3f67SBaptiste Daroussin printf("Files %s%s and %s%s are identical\n", 5053bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 5063bbe3f67SBaptiste Daroussin break; 5073bbe3f67SBaptiste Daroussin case D_MISMATCH1: 5083bbe3f67SBaptiste Daroussin printf("File %s%s is a directory while file %s%s is a regular file\n", 5093bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 5103bbe3f67SBaptiste Daroussin break; 5113bbe3f67SBaptiste Daroussin case D_MISMATCH2: 5123bbe3f67SBaptiste Daroussin printf("File %s%s is a regular file while file %s%s is a directory\n", 5133bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 5143bbe3f67SBaptiste Daroussin break; 5153bbe3f67SBaptiste Daroussin case D_SKIPPED1: 5163bbe3f67SBaptiste Daroussin printf("File %s%s is not a regular file or directory and was skipped\n", 5173bbe3f67SBaptiste Daroussin path1, entry); 5183bbe3f67SBaptiste Daroussin break; 5193bbe3f67SBaptiste Daroussin case D_SKIPPED2: 5203bbe3f67SBaptiste Daroussin printf("File %s%s is not a regular file or directory and was skipped\n", 5213bbe3f67SBaptiste Daroussin path2, entry); 5223bbe3f67SBaptiste Daroussin break; 5233bbe3f67SBaptiste Daroussin } 5243bbe3f67SBaptiste Daroussin } 5253bbe3f67SBaptiste Daroussin 5263bbe3f67SBaptiste Daroussin void 5273bbe3f67SBaptiste Daroussin usage(void) 5283bbe3f67SBaptiste Daroussin { 5293bbe3f67SBaptiste Daroussin (void)fprintf(stderr, 530e68edb8cSKyle Evans "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 5313bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 5323bbe3f67SBaptiste Daroussin " [-I pattern] [-L label] file1 file2\n" 533e68edb8cSKyle Evans " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 5343bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 5353bbe3f67SBaptiste Daroussin " -C number file1 file2\n" 536e68edb8cSKyle Evans " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" 5373bbe3f67SBaptiste Daroussin " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n" 538e68edb8cSKyle Evans " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 5393bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n" 5403bbe3f67SBaptiste Daroussin " -U number file1 file2\n" 541e68edb8cSKyle Evans " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 5423bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n" 54364793e74SBaptiste Daroussin " [-S name] [-X file] [-x pattern] dir1 dir2\n" 54464793e74SBaptiste Daroussin " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n" 54564793e74SBaptiste Daroussin " [--ignore-blank-lines] [--ignore-case] [--minimal]\n" 54664793e74SBaptiste Daroussin " [--no-ignore-file-name-case] [--strip-trailing-cr]\n" 54764793e74SBaptiste Daroussin " [--suppress-common-lines] [--tabsize] [--text] [--width]\n" 54864793e74SBaptiste Daroussin " -y | --side-by-side file1 file2\n"); 5493bbe3f67SBaptiste Daroussin 5503bbe3f67SBaptiste Daroussin exit(2); 5513bbe3f67SBaptiste Daroussin } 552db9cccfbSKyle Evans 553db9cccfbSKyle Evans void 554db9cccfbSKyle Evans conflicting_format(void) 555db9cccfbSKyle Evans { 556db9cccfbSKyle Evans 557db9cccfbSKyle Evans fprintf(stderr, "error: conflicting output format options.\n"); 558db9cccfbSKyle Evans usage(); 559db9cccfbSKyle Evans } 560