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> 303bbe3f67SBaptiste Daroussin #include <getopt.h> 313bbe3f67SBaptiste Daroussin #include <stdlib.h> 323bbe3f67SBaptiste Daroussin #include <stdio.h> 333bbe3f67SBaptiste Daroussin #include <string.h> 343bbe3f67SBaptiste Daroussin #include <unistd.h> 353bbe3f67SBaptiste Daroussin #include <limits.h> 363bbe3f67SBaptiste Daroussin 373bbe3f67SBaptiste Daroussin #include "diff.h" 383bbe3f67SBaptiste Daroussin #include "xmalloc.h" 393bbe3f67SBaptiste Daroussin 4064793e74SBaptiste Daroussin int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag; 4164793e74SBaptiste Daroussin int diff_format, diff_context, status, ignore_file_case, suppress_common; 4264793e74SBaptiste Daroussin int tabsize = 8, width = 130; 433bbe3f67SBaptiste Daroussin char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; 44fddcb7b8SBaptiste Daroussin char *group_format = NULL; 453bbe3f67SBaptiste Daroussin struct stat stb1, stb2; 463bbe3f67SBaptiste Daroussin struct excludes *excludes_list; 473bbe3f67SBaptiste Daroussin regex_t ignore_re; 483bbe3f67SBaptiste Daroussin 4964793e74SBaptiste Daroussin #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y" 503bbe3f67SBaptiste Daroussin enum { 513bbe3f67SBaptiste Daroussin OPT_TSIZE = CHAR_MAX + 1, 523bbe3f67SBaptiste Daroussin OPT_STRIPCR, 533bbe3f67SBaptiste Daroussin OPT_IGN_FN_CASE, 543bbe3f67SBaptiste Daroussin OPT_NO_IGN_FN_CASE, 553bbe3f67SBaptiste Daroussin OPT_NORMAL, 56901a475bSBaptiste Daroussin OPT_HORIZON_LINES, 57fddcb7b8SBaptiste Daroussin OPT_CHANGED_GROUP_FORMAT, 5864793e74SBaptiste Daroussin OPT_SUPPRESS_COMMON, 593bbe3f67SBaptiste Daroussin }; 603bbe3f67SBaptiste Daroussin 613bbe3f67SBaptiste Daroussin static struct option longopts[] = { 623bbe3f67SBaptiste Daroussin { "text", no_argument, 0, 'a' }, 633bbe3f67SBaptiste Daroussin { "ignore-space-change", no_argument, 0, 'b' }, 643bbe3f67SBaptiste Daroussin { "context", optional_argument, 0, 'C' }, 653bbe3f67SBaptiste Daroussin { "ifdef", required_argument, 0, 'D' }, 663bbe3f67SBaptiste Daroussin { "minimal", no_argument, 0, 'd' }, 673bbe3f67SBaptiste Daroussin { "ed", no_argument, 0, 'e' }, 683bbe3f67SBaptiste Daroussin { "forward-ed", no_argument, 0, 'f' }, 69ea67c7a0SJohn Baldwin { "speed-large-files", no_argument, NULL, 'H' }, 70e68edb8cSKyle Evans { "ignore-blank-lines", no_argument, 0, 'B' }, 713bbe3f67SBaptiste Daroussin { "ignore-matching-lines", required_argument, 0, 'I' }, 723bbe3f67SBaptiste Daroussin { "ignore-case", no_argument, 0, 'i' }, 733bbe3f67SBaptiste Daroussin { "paginate", no_argument, NULL, 'l' }, 743bbe3f67SBaptiste Daroussin { "label", required_argument, 0, 'L' }, 753bbe3f67SBaptiste Daroussin { "new-file", no_argument, 0, 'N' }, 763bbe3f67SBaptiste Daroussin { "rcs", no_argument, 0, 'n' }, 773bbe3f67SBaptiste Daroussin { "unidirectional-new-file", no_argument, 0, 'P' }, 783bbe3f67SBaptiste Daroussin { "show-c-function", no_argument, 0, 'p' }, 793bbe3f67SBaptiste Daroussin { "brief", no_argument, 0, 'q' }, 803bbe3f67SBaptiste Daroussin { "recursive", no_argument, 0, 'r' }, 813bbe3f67SBaptiste Daroussin { "report-identical-files", no_argument, 0, 's' }, 823bbe3f67SBaptiste Daroussin { "starting-file", required_argument, 0, 'S' }, 833bbe3f67SBaptiste Daroussin { "expand-tabs", no_argument, 0, 't' }, 843bbe3f67SBaptiste Daroussin { "initial-tab", no_argument, 0, 'T' }, 853bbe3f67SBaptiste Daroussin { "unified", optional_argument, 0, 'U' }, 863bbe3f67SBaptiste Daroussin { "ignore-all-space", no_argument, 0, 'w' }, 8764793e74SBaptiste Daroussin { "width", required_argument, 0, 'W' }, 883bbe3f67SBaptiste Daroussin { "exclude", required_argument, 0, 'x' }, 893bbe3f67SBaptiste Daroussin { "exclude-from", required_argument, 0, 'X' }, 9064793e74SBaptiste Daroussin { "side-by-side", no_argument, NULL, 'y' }, 913bbe3f67SBaptiste Daroussin { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE }, 92901a475bSBaptiste Daroussin { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES }, 933bbe3f67SBaptiste Daroussin { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE }, 943bbe3f67SBaptiste Daroussin { "normal", no_argument, NULL, OPT_NORMAL }, 953bbe3f67SBaptiste Daroussin { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR }, 96*43fbd654SKyle Evans { "tabsize", required_argument, NULL, OPT_TSIZE }, 97fddcb7b8SBaptiste Daroussin { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT}, 9864793e74SBaptiste Daroussin { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON }, 993bbe3f67SBaptiste Daroussin { NULL, 0, 0, '\0'} 1003bbe3f67SBaptiste Daroussin }; 1013bbe3f67SBaptiste Daroussin 1023bbe3f67SBaptiste Daroussin void usage(void) __dead2; 1033bbe3f67SBaptiste Daroussin void push_excludes(char *); 1043bbe3f67SBaptiste Daroussin void push_ignore_pats(char *); 1053bbe3f67SBaptiste Daroussin void read_excludes_file(char *file); 1063bbe3f67SBaptiste Daroussin void set_argstr(char **, char **); 1073bbe3f67SBaptiste Daroussin 1083bbe3f67SBaptiste Daroussin int 1093bbe3f67SBaptiste Daroussin main(int argc, char **argv) 1103bbe3f67SBaptiste Daroussin { 1113bbe3f67SBaptiste Daroussin const char *errstr = NULL; 1123bbe3f67SBaptiste Daroussin char *ep, **oargv; 1133bbe3f67SBaptiste Daroussin long l; 1143bbe3f67SBaptiste Daroussin int ch, dflags, lastch, gotstdin, prevoptind, newarg; 1153bbe3f67SBaptiste Daroussin 1163bbe3f67SBaptiste Daroussin oargv = argv; 1173bbe3f67SBaptiste Daroussin gotstdin = 0; 1183bbe3f67SBaptiste Daroussin dflags = 0; 1193bbe3f67SBaptiste Daroussin lastch = '\0'; 1203bbe3f67SBaptiste Daroussin prevoptind = 1; 1213bbe3f67SBaptiste Daroussin newarg = 1; 1223bbe3f67SBaptiste Daroussin diff_context = 3; 1233bbe3f67SBaptiste Daroussin diff_format = 0; 1243bbe3f67SBaptiste Daroussin while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 1253bbe3f67SBaptiste Daroussin switch (ch) { 1263bbe3f67SBaptiste Daroussin case '0': case '1': case '2': case '3': case '4': 1273bbe3f67SBaptiste Daroussin case '5': case '6': case '7': case '8': case '9': 1283bbe3f67SBaptiste Daroussin if (newarg) 1293bbe3f67SBaptiste Daroussin usage(); /* disallow -[0-9]+ */ 1303bbe3f67SBaptiste Daroussin else if (lastch == 'c' || lastch == 'u') 1313bbe3f67SBaptiste Daroussin diff_context = 0; 1323bbe3f67SBaptiste Daroussin else if (!isdigit(lastch) || diff_context > INT_MAX / 10) 1333bbe3f67SBaptiste Daroussin usage(); 1343bbe3f67SBaptiste Daroussin diff_context = (diff_context * 10) + (ch - '0'); 1353bbe3f67SBaptiste Daroussin break; 1363bbe3f67SBaptiste Daroussin case 'a': 1373bbe3f67SBaptiste Daroussin dflags |= D_FORCEASCII; 1383bbe3f67SBaptiste Daroussin break; 1393bbe3f67SBaptiste Daroussin case 'b': 1403bbe3f67SBaptiste Daroussin dflags |= D_FOLDBLANKS; 1413bbe3f67SBaptiste Daroussin break; 1423bbe3f67SBaptiste Daroussin case 'C': 1433bbe3f67SBaptiste Daroussin case 'c': 1443bbe3f67SBaptiste Daroussin cflag = 1; 1453bbe3f67SBaptiste Daroussin diff_format = D_CONTEXT; 1463bbe3f67SBaptiste Daroussin if (optarg != NULL) { 1473bbe3f67SBaptiste Daroussin l = strtol(optarg, &ep, 10); 1483bbe3f67SBaptiste Daroussin if (*ep != '\0' || l < 0 || l >= INT_MAX) 1493bbe3f67SBaptiste Daroussin usage(); 1503bbe3f67SBaptiste Daroussin diff_context = (int)l; 1513bbe3f67SBaptiste Daroussin } 1523bbe3f67SBaptiste Daroussin break; 1533bbe3f67SBaptiste Daroussin case 'd': 1543bbe3f67SBaptiste Daroussin dflags |= D_MINIMAL; 1553bbe3f67SBaptiste Daroussin break; 1563bbe3f67SBaptiste Daroussin case 'D': 1573bbe3f67SBaptiste Daroussin diff_format = D_IFDEF; 1583bbe3f67SBaptiste Daroussin ifdefname = optarg; 1593bbe3f67SBaptiste Daroussin break; 1603bbe3f67SBaptiste Daroussin case 'e': 1613bbe3f67SBaptiste Daroussin diff_format = D_EDIT; 1623bbe3f67SBaptiste Daroussin break; 1633bbe3f67SBaptiste Daroussin case 'f': 1643bbe3f67SBaptiste Daroussin diff_format = D_REVERSE; 1653bbe3f67SBaptiste Daroussin break; 166ea67c7a0SJohn Baldwin case 'H': 167ea67c7a0SJohn Baldwin /* ignore but needed for compatibility with GNU diff */ 168ea67c7a0SJohn Baldwin break; 1693bbe3f67SBaptiste Daroussin case 'h': 1703bbe3f67SBaptiste Daroussin /* silently ignore for backwards compatibility */ 1713bbe3f67SBaptiste Daroussin break; 172e68edb8cSKyle Evans case 'B': 173e68edb8cSKyle Evans dflags |= D_SKIPBLANKLINES; 174e68edb8cSKyle Evans break; 1753bbe3f67SBaptiste Daroussin case 'I': 1763bbe3f67SBaptiste Daroussin push_ignore_pats(optarg); 1773bbe3f67SBaptiste Daroussin break; 1783bbe3f67SBaptiste Daroussin case 'i': 1793bbe3f67SBaptiste Daroussin dflags |= D_IGNORECASE; 1803bbe3f67SBaptiste Daroussin break; 1813bbe3f67SBaptiste Daroussin case 'L': 1823bbe3f67SBaptiste Daroussin if (label[0] == NULL) 1833bbe3f67SBaptiste Daroussin label[0] = optarg; 1843bbe3f67SBaptiste Daroussin else if (label[1] == NULL) 1853bbe3f67SBaptiste Daroussin label[1] = optarg; 1863bbe3f67SBaptiste Daroussin else 1873bbe3f67SBaptiste Daroussin usage(); 1883bbe3f67SBaptiste Daroussin break; 1893bbe3f67SBaptiste Daroussin case 'l': 1903bbe3f67SBaptiste Daroussin lflag = 1; 1913bbe3f67SBaptiste Daroussin break; 1923bbe3f67SBaptiste Daroussin case 'N': 1933bbe3f67SBaptiste Daroussin Nflag = 1; 1943bbe3f67SBaptiste Daroussin break; 1953bbe3f67SBaptiste Daroussin case 'n': 1963bbe3f67SBaptiste Daroussin diff_format = D_NREVERSE; 1973bbe3f67SBaptiste Daroussin break; 1983bbe3f67SBaptiste Daroussin case 'p': 1993bbe3f67SBaptiste Daroussin if (diff_format == 0) 2003bbe3f67SBaptiste Daroussin diff_format = D_CONTEXT; 2013bbe3f67SBaptiste Daroussin dflags |= D_PROTOTYPE; 2023bbe3f67SBaptiste Daroussin break; 2033bbe3f67SBaptiste Daroussin case 'P': 2043bbe3f67SBaptiste Daroussin Pflag = 1; 2053bbe3f67SBaptiste Daroussin break; 2063bbe3f67SBaptiste Daroussin case 'r': 2073bbe3f67SBaptiste Daroussin rflag = 1; 2083bbe3f67SBaptiste Daroussin break; 2093bbe3f67SBaptiste Daroussin case 'q': 2103bbe3f67SBaptiste Daroussin diff_format = D_BRIEF; 2113bbe3f67SBaptiste Daroussin break; 2123bbe3f67SBaptiste Daroussin case 'S': 2133bbe3f67SBaptiste Daroussin start = optarg; 2143bbe3f67SBaptiste Daroussin break; 2153bbe3f67SBaptiste Daroussin case 's': 2163bbe3f67SBaptiste Daroussin sflag = 1; 2173bbe3f67SBaptiste Daroussin break; 2183bbe3f67SBaptiste Daroussin case 'T': 2193bbe3f67SBaptiste Daroussin Tflag = 1; 2203bbe3f67SBaptiste Daroussin break; 2213bbe3f67SBaptiste Daroussin case 't': 2223bbe3f67SBaptiste Daroussin dflags |= D_EXPANDTABS; 2233bbe3f67SBaptiste Daroussin break; 2243bbe3f67SBaptiste Daroussin case 'U': 2253bbe3f67SBaptiste Daroussin case 'u': 2263bbe3f67SBaptiste Daroussin diff_format = D_UNIFIED; 2273bbe3f67SBaptiste Daroussin if (optarg != NULL) { 2283bbe3f67SBaptiste Daroussin l = strtol(optarg, &ep, 10); 2293bbe3f67SBaptiste Daroussin if (*ep != '\0' || l < 0 || l >= INT_MAX) 2303bbe3f67SBaptiste Daroussin usage(); 2313bbe3f67SBaptiste Daroussin diff_context = (int)l; 2323bbe3f67SBaptiste Daroussin } 2333bbe3f67SBaptiste Daroussin break; 2343bbe3f67SBaptiste Daroussin case 'w': 2353bbe3f67SBaptiste Daroussin dflags |= D_IGNOREBLANKS; 2363bbe3f67SBaptiste Daroussin break; 23764793e74SBaptiste Daroussin case 'W': 23864793e74SBaptiste Daroussin Wflag = 1; 23964793e74SBaptiste Daroussin width = (int) strtonum(optarg, 1, INT_MAX, &errstr); 24064793e74SBaptiste Daroussin if (errstr) { 24164793e74SBaptiste Daroussin warnx("Invalid argument for width"); 24264793e74SBaptiste Daroussin usage(); 24364793e74SBaptiste Daroussin } 24464793e74SBaptiste Daroussin break; 2453bbe3f67SBaptiste Daroussin case 'X': 2463bbe3f67SBaptiste Daroussin read_excludes_file(optarg); 2473bbe3f67SBaptiste Daroussin break; 2483bbe3f67SBaptiste Daroussin case 'x': 2493bbe3f67SBaptiste Daroussin push_excludes(optarg); 2503bbe3f67SBaptiste Daroussin break; 25164793e74SBaptiste Daroussin case 'y': 25264793e74SBaptiste Daroussin diff_format = D_SIDEBYSIDE; 25364793e74SBaptiste Daroussin break; 254fddcb7b8SBaptiste Daroussin case OPT_CHANGED_GROUP_FORMAT: 255fddcb7b8SBaptiste Daroussin diff_format = D_GFORMAT; 256fddcb7b8SBaptiste Daroussin group_format = optarg; 257fddcb7b8SBaptiste Daroussin break; 258901a475bSBaptiste Daroussin case OPT_HORIZON_LINES: 259901a475bSBaptiste Daroussin break; /* XXX TODO for compatibility with GNU diff3 */ 2603bbe3f67SBaptiste Daroussin case OPT_IGN_FN_CASE: 2613bbe3f67SBaptiste Daroussin ignore_file_case = 1; 2623bbe3f67SBaptiste Daroussin break; 2633bbe3f67SBaptiste Daroussin case OPT_NO_IGN_FN_CASE: 2643bbe3f67SBaptiste Daroussin ignore_file_case = 0; 2653bbe3f67SBaptiste Daroussin break; 2663bbe3f67SBaptiste Daroussin case OPT_NORMAL: 2673bbe3f67SBaptiste Daroussin diff_format = D_NORMAL; 2683bbe3f67SBaptiste Daroussin break; 2693bbe3f67SBaptiste Daroussin case OPT_TSIZE: 2703bbe3f67SBaptiste Daroussin tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr); 2713bbe3f67SBaptiste Daroussin if (errstr) { 2723bbe3f67SBaptiste Daroussin warnx("Invalid argument for tabsize"); 2733bbe3f67SBaptiste Daroussin usage(); 2743bbe3f67SBaptiste Daroussin } 2753bbe3f67SBaptiste Daroussin break; 2763bbe3f67SBaptiste Daroussin case OPT_STRIPCR: 2773bbe3f67SBaptiste Daroussin dflags |= D_STRIPCR; 2783bbe3f67SBaptiste Daroussin break; 27964793e74SBaptiste Daroussin case OPT_SUPPRESS_COMMON: 28064793e74SBaptiste Daroussin suppress_common = 1; 28164793e74SBaptiste Daroussin break; 2823bbe3f67SBaptiste Daroussin default: 2833bbe3f67SBaptiste Daroussin usage(); 2843bbe3f67SBaptiste Daroussin break; 2853bbe3f67SBaptiste Daroussin } 2863bbe3f67SBaptiste Daroussin lastch = ch; 2873bbe3f67SBaptiste Daroussin newarg = optind != prevoptind; 2883bbe3f67SBaptiste Daroussin prevoptind = optind; 2893bbe3f67SBaptiste Daroussin } 2903bbe3f67SBaptiste Daroussin argc -= optind; 2913bbe3f67SBaptiste Daroussin argv += optind; 2923bbe3f67SBaptiste Daroussin 2933bbe3f67SBaptiste Daroussin #ifdef __OpenBSD__ 2943bbe3f67SBaptiste Daroussin if (pledge("stdio rpath tmppath", NULL) == -1) 2953bbe3f67SBaptiste Daroussin err(2, "pledge"); 2963bbe3f67SBaptiste Daroussin #endif 2973bbe3f67SBaptiste Daroussin 2983bbe3f67SBaptiste Daroussin /* 2993bbe3f67SBaptiste Daroussin * Do sanity checks, fill in stb1 and stb2 and call the appropriate 3003bbe3f67SBaptiste Daroussin * driver routine. Both drivers use the contents of stb1 and stb2. 3013bbe3f67SBaptiste Daroussin */ 3023bbe3f67SBaptiste Daroussin if (argc != 2) 3033bbe3f67SBaptiste Daroussin usage(); 3043bbe3f67SBaptiste Daroussin if (ignore_pats != NULL) { 3053bbe3f67SBaptiste Daroussin char buf[BUFSIZ]; 3063bbe3f67SBaptiste Daroussin int error; 3073bbe3f67SBaptiste Daroussin 3083bbe3f67SBaptiste Daroussin if ((error = regcomp(&ignore_re, ignore_pats, 3093bbe3f67SBaptiste Daroussin REG_NEWLINE | REG_EXTENDED)) != 0) { 3103bbe3f67SBaptiste Daroussin regerror(error, &ignore_re, buf, sizeof(buf)); 3113bbe3f67SBaptiste Daroussin if (*ignore_pats != '\0') 3123bbe3f67SBaptiste Daroussin errx(2, "%s: %s", ignore_pats, buf); 3133bbe3f67SBaptiste Daroussin else 3143bbe3f67SBaptiste Daroussin errx(2, "%s", buf); 3153bbe3f67SBaptiste Daroussin } 3163bbe3f67SBaptiste Daroussin } 3173bbe3f67SBaptiste Daroussin if (strcmp(argv[0], "-") == 0) { 3183bbe3f67SBaptiste Daroussin fstat(STDIN_FILENO, &stb1); 3193bbe3f67SBaptiste Daroussin gotstdin = 1; 3203bbe3f67SBaptiste Daroussin } else if (stat(argv[0], &stb1) != 0) 3213bbe3f67SBaptiste Daroussin err(2, "%s", argv[0]); 3223bbe3f67SBaptiste Daroussin if (strcmp(argv[1], "-") == 0) { 3233bbe3f67SBaptiste Daroussin fstat(STDIN_FILENO, &stb2); 3243bbe3f67SBaptiste Daroussin gotstdin = 1; 3253bbe3f67SBaptiste Daroussin } else if (stat(argv[1], &stb2) != 0) 3263bbe3f67SBaptiste Daroussin err(2, "%s", argv[1]); 3273bbe3f67SBaptiste Daroussin if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 3283bbe3f67SBaptiste Daroussin errx(2, "can't compare - to a directory"); 3293bbe3f67SBaptiste Daroussin set_argstr(oargv, argv); 3303bbe3f67SBaptiste Daroussin if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 3313bbe3f67SBaptiste Daroussin if (diff_format == D_IFDEF) 3323bbe3f67SBaptiste Daroussin errx(2, "-D option not supported with directories"); 3333bbe3f67SBaptiste Daroussin diffdir(argv[0], argv[1], dflags); 3343bbe3f67SBaptiste Daroussin } else { 3353bbe3f67SBaptiste Daroussin if (S_ISDIR(stb1.st_mode)) { 3363bbe3f67SBaptiste Daroussin argv[0] = splice(argv[0], argv[1]); 33712d9c0dcSBaptiste Daroussin if (stat(argv[0], &stb1) == -1) 3383bbe3f67SBaptiste Daroussin err(2, "%s", argv[0]); 3393bbe3f67SBaptiste Daroussin } 3403bbe3f67SBaptiste Daroussin if (S_ISDIR(stb2.st_mode)) { 3413bbe3f67SBaptiste Daroussin argv[1] = splice(argv[1], argv[0]); 34212d9c0dcSBaptiste Daroussin if (stat(argv[1], &stb2) == -1) 3433bbe3f67SBaptiste Daroussin err(2, "%s", argv[1]); 3443bbe3f67SBaptiste Daroussin } 3453bbe3f67SBaptiste Daroussin print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0], 3463bbe3f67SBaptiste Daroussin argv[1], ""); 3473bbe3f67SBaptiste Daroussin } 3483bbe3f67SBaptiste Daroussin exit(status); 3493bbe3f67SBaptiste Daroussin } 3503bbe3f67SBaptiste Daroussin 3513bbe3f67SBaptiste Daroussin void 3523bbe3f67SBaptiste Daroussin set_argstr(char **av, char **ave) 3533bbe3f67SBaptiste Daroussin { 3543bbe3f67SBaptiste Daroussin size_t argsize; 3553bbe3f67SBaptiste Daroussin char **ap; 3563bbe3f67SBaptiste Daroussin 3573bbe3f67SBaptiste Daroussin argsize = 4 + *ave - *av + 1; 3583bbe3f67SBaptiste Daroussin diffargs = xmalloc(argsize); 3593bbe3f67SBaptiste Daroussin strlcpy(diffargs, "diff", argsize); 3603bbe3f67SBaptiste Daroussin for (ap = av + 1; ap < ave; ap++) { 3613bbe3f67SBaptiste Daroussin if (strcmp(*ap, "--") != 0) { 3623bbe3f67SBaptiste Daroussin strlcat(diffargs, " ", argsize); 3633bbe3f67SBaptiste Daroussin strlcat(diffargs, *ap, argsize); 3643bbe3f67SBaptiste Daroussin } 3653bbe3f67SBaptiste Daroussin } 3663bbe3f67SBaptiste Daroussin } 3673bbe3f67SBaptiste Daroussin 3683bbe3f67SBaptiste Daroussin /* 3693bbe3f67SBaptiste Daroussin * Read in an excludes file and push each line. 3703bbe3f67SBaptiste Daroussin */ 3713bbe3f67SBaptiste Daroussin void 3723bbe3f67SBaptiste Daroussin read_excludes_file(char *file) 3733bbe3f67SBaptiste Daroussin { 3743bbe3f67SBaptiste Daroussin FILE *fp; 3753bbe3f67SBaptiste Daroussin char *buf, *pattern; 3763bbe3f67SBaptiste Daroussin size_t len; 3773bbe3f67SBaptiste Daroussin 3783bbe3f67SBaptiste Daroussin if (strcmp(file, "-") == 0) 3793bbe3f67SBaptiste Daroussin fp = stdin; 3803bbe3f67SBaptiste Daroussin else if ((fp = fopen(file, "r")) == NULL) 3813bbe3f67SBaptiste Daroussin err(2, "%s", file); 3823bbe3f67SBaptiste Daroussin while ((buf = fgetln(fp, &len)) != NULL) { 3833bbe3f67SBaptiste Daroussin if (buf[len - 1] == '\n') 3843bbe3f67SBaptiste Daroussin len--; 3855c8e56f6SBaptiste Daroussin if ((pattern = strndup(buf, len)) == NULL) 3865c8e56f6SBaptiste Daroussin err(2, "xstrndup"); 3873bbe3f67SBaptiste Daroussin push_excludes(pattern); 3883bbe3f67SBaptiste Daroussin } 3893bbe3f67SBaptiste Daroussin if (strcmp(file, "-") != 0) 3903bbe3f67SBaptiste Daroussin fclose(fp); 3913bbe3f67SBaptiste Daroussin } 3923bbe3f67SBaptiste Daroussin 3933bbe3f67SBaptiste Daroussin /* 3943bbe3f67SBaptiste Daroussin * Push a pattern onto the excludes list. 3953bbe3f67SBaptiste Daroussin */ 3963bbe3f67SBaptiste Daroussin void 3973bbe3f67SBaptiste Daroussin push_excludes(char *pattern) 3983bbe3f67SBaptiste Daroussin { 3993bbe3f67SBaptiste Daroussin struct excludes *entry; 4003bbe3f67SBaptiste Daroussin 4013bbe3f67SBaptiste Daroussin entry = xmalloc(sizeof(*entry)); 4023bbe3f67SBaptiste Daroussin entry->pattern = pattern; 4033bbe3f67SBaptiste Daroussin entry->next = excludes_list; 4043bbe3f67SBaptiste Daroussin excludes_list = entry; 4053bbe3f67SBaptiste Daroussin } 4063bbe3f67SBaptiste Daroussin 4073bbe3f67SBaptiste Daroussin void 4083bbe3f67SBaptiste Daroussin push_ignore_pats(char *pattern) 4093bbe3f67SBaptiste Daroussin { 4103bbe3f67SBaptiste Daroussin size_t len; 4113bbe3f67SBaptiste Daroussin 4123bbe3f67SBaptiste Daroussin if (ignore_pats == NULL) 4133bbe3f67SBaptiste Daroussin ignore_pats = xstrdup(pattern); 4143bbe3f67SBaptiste Daroussin else { 4153bbe3f67SBaptiste Daroussin /* old + "|" + new + NUL */ 4163bbe3f67SBaptiste Daroussin len = strlen(ignore_pats) + strlen(pattern) + 2; 4173bbe3f67SBaptiste Daroussin ignore_pats = xreallocarray(ignore_pats, 1, len); 4183bbe3f67SBaptiste Daroussin strlcat(ignore_pats, "|", len); 4193bbe3f67SBaptiste Daroussin strlcat(ignore_pats, pattern, len); 4203bbe3f67SBaptiste Daroussin } 4213bbe3f67SBaptiste Daroussin } 4223bbe3f67SBaptiste Daroussin 4233bbe3f67SBaptiste Daroussin void 4243bbe3f67SBaptiste Daroussin print_only(const char *path, size_t dirlen, const char *entry) 4253bbe3f67SBaptiste Daroussin { 4263bbe3f67SBaptiste Daroussin if (dirlen > 1) 4273bbe3f67SBaptiste Daroussin dirlen--; 4283bbe3f67SBaptiste Daroussin printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 4293bbe3f67SBaptiste Daroussin } 4303bbe3f67SBaptiste Daroussin 4313bbe3f67SBaptiste Daroussin void 4323bbe3f67SBaptiste Daroussin print_status(int val, char *path1, char *path2, const char *entry) 4333bbe3f67SBaptiste Daroussin { 4343bbe3f67SBaptiste Daroussin switch (val) { 4353bbe3f67SBaptiste Daroussin case D_BINARY: 4363bbe3f67SBaptiste Daroussin printf("Binary files %s%s and %s%s differ\n", 4373bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4383bbe3f67SBaptiste Daroussin break; 4393bbe3f67SBaptiste Daroussin case D_DIFFER: 4403bbe3f67SBaptiste Daroussin if (diff_format == D_BRIEF) 4413bbe3f67SBaptiste Daroussin printf("Files %s%s and %s%s differ\n", 4423bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4433bbe3f67SBaptiste Daroussin break; 4443bbe3f67SBaptiste Daroussin case D_SAME: 4453bbe3f67SBaptiste Daroussin if (sflag) 4463bbe3f67SBaptiste Daroussin printf("Files %s%s and %s%s are identical\n", 4473bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4483bbe3f67SBaptiste Daroussin break; 4493bbe3f67SBaptiste Daroussin case D_MISMATCH1: 4503bbe3f67SBaptiste Daroussin printf("File %s%s is a directory while file %s%s is a regular file\n", 4513bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4523bbe3f67SBaptiste Daroussin break; 4533bbe3f67SBaptiste Daroussin case D_MISMATCH2: 4543bbe3f67SBaptiste Daroussin printf("File %s%s is a regular file while file %s%s is a directory\n", 4553bbe3f67SBaptiste Daroussin path1, entry, path2, entry); 4563bbe3f67SBaptiste Daroussin break; 4573bbe3f67SBaptiste Daroussin case D_SKIPPED1: 4583bbe3f67SBaptiste Daroussin printf("File %s%s is not a regular file or directory and was skipped\n", 4593bbe3f67SBaptiste Daroussin path1, entry); 4603bbe3f67SBaptiste Daroussin break; 4613bbe3f67SBaptiste Daroussin case D_SKIPPED2: 4623bbe3f67SBaptiste Daroussin printf("File %s%s is not a regular file or directory and was skipped\n", 4633bbe3f67SBaptiste Daroussin path2, entry); 4643bbe3f67SBaptiste Daroussin break; 4653bbe3f67SBaptiste Daroussin } 4663bbe3f67SBaptiste Daroussin } 4673bbe3f67SBaptiste Daroussin 4683bbe3f67SBaptiste Daroussin void 4693bbe3f67SBaptiste Daroussin usage(void) 4703bbe3f67SBaptiste Daroussin { 4713bbe3f67SBaptiste Daroussin (void)fprintf(stderr, 472e68edb8cSKyle Evans "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 4733bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 4743bbe3f67SBaptiste Daroussin " [-I pattern] [-L label] file1 file2\n" 475e68edb8cSKyle Evans " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 4763bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 4773bbe3f67SBaptiste Daroussin " -C number file1 file2\n" 478e68edb8cSKyle Evans " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" 4793bbe3f67SBaptiste Daroussin " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n" 480e68edb8cSKyle Evans " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 4813bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n" 4823bbe3f67SBaptiste Daroussin " -U number file1 file2\n" 483e68edb8cSKyle Evans " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 4843bbe3f67SBaptiste Daroussin " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n" 48564793e74SBaptiste Daroussin " [-S name] [-X file] [-x pattern] dir1 dir2\n" 48664793e74SBaptiste Daroussin " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n" 48764793e74SBaptiste Daroussin " [--ignore-blank-lines] [--ignore-case] [--minimal]\n" 48864793e74SBaptiste Daroussin " [--no-ignore-file-name-case] [--strip-trailing-cr]\n" 48964793e74SBaptiste Daroussin " [--suppress-common-lines] [--tabsize] [--text] [--width]\n" 49064793e74SBaptiste Daroussin " -y | --side-by-side file1 file2\n"); 4913bbe3f67SBaptiste Daroussin 4923bbe3f67SBaptiste Daroussin exit(2); 4933bbe3f67SBaptiste Daroussin } 494