1 /* $OpenBSD: diff.c,v 1.67 2019/06/28 13:35:00 deraadt Exp $ */
4 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
41 int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag;
42 int diff_format, diff_context, status, ignore_file_case, suppress_common;
43 int tabsize = 8, width = 130;
44 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
45 char *group_format = NULL;
46 struct stat stb1, stb2;
47 struct excludes *excludes_list;
50 #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y"
52 OPT_TSIZE = CHAR_MAX + 1,
58 OPT_CHANGED_GROUP_FORMAT,
62 static struct option longopts[] = {
63 { "text", no_argument, 0, 'a' },
64 { "ignore-space-change", no_argument, 0, 'b' },
65 { "context", optional_argument, 0, 'C' },
66 { "ifdef", required_argument, 0, 'D' },
67 { "minimal", no_argument, 0, 'd' },
68 { "ed", no_argument, 0, 'e' },
69 { "forward-ed", no_argument, 0, 'f' },
70 { "speed-large-files", no_argument, NULL, 'H' },
71 { "ignore-blank-lines", no_argument, 0, 'B' },
72 { "ignore-matching-lines", required_argument, 0, 'I' },
73 { "ignore-case", no_argument, 0, 'i' },
74 { "paginate", no_argument, NULL, 'l' },
75 { "label", required_argument, 0, 'L' },
76 { "new-file", no_argument, 0, 'N' },
77 { "rcs", no_argument, 0, 'n' },
78 { "unidirectional-new-file", no_argument, 0, 'P' },
79 { "show-c-function", no_argument, 0, 'p' },
80 { "brief", no_argument, 0, 'q' },
81 { "recursive", no_argument, 0, 'r' },
82 { "report-identical-files", no_argument, 0, 's' },
83 { "starting-file", required_argument, 0, 'S' },
84 { "expand-tabs", no_argument, 0, 't' },
85 { "initial-tab", no_argument, 0, 'T' },
86 { "unified", optional_argument, 0, 'U' },
87 { "ignore-all-space", no_argument, 0, 'w' },
88 { "width", required_argument, 0, 'W' },
89 { "exclude", required_argument, 0, 'x' },
90 { "exclude-from", required_argument, 0, 'X' },
91 { "side-by-side", no_argument, NULL, 'y' },
92 { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE },
93 { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES },
94 { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE },
95 { "normal", no_argument, NULL, OPT_NORMAL },
96 { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR },
97 { "tabsize", required_argument, NULL, OPT_TSIZE },
98 { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT},
99 { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON },
103 void usage(void) __dead2;
104 void conflicting_format(void) __dead2;
105 void push_excludes(char *);
106 void push_ignore_pats(char *);
107 void read_excludes_file(char *file);
108 void set_argstr(char **, char **);
111 main(int argc, char **argv)
113 const char *errstr = NULL;
116 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
125 diff_format = D_UNSET;
126 #define FORMAT_MISMATCHED(type) \
127 (diff_format != D_UNSET && diff_format != (type))
128 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
130 case '0': case '1': case '2': case '3': case '4':
131 case '5': case '6': case '7': case '8': case '9':
133 usage(); /* disallow -[0-9]+ */
134 else if (lastch == 'c' || lastch == 'u')
136 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
138 diff_context = (diff_context * 10) + (ch - '0');
141 dflags |= D_FORCEASCII;
144 dflags |= D_FOLDBLANKS;
148 if (FORMAT_MISMATCHED(D_CONTEXT))
149 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)
156 diff_context = (int)l;
163 if (FORMAT_MISMATCHED(D_IFDEF))
164 conflicting_format();
165 diff_format = D_IFDEF;
169 if (FORMAT_MISMATCHED(D_EDIT))
170 conflicting_format();
171 diff_format = D_EDIT;
174 if (FORMAT_MISMATCHED(D_REVERSE))
175 conflicting_format();
176 diff_format = D_REVERSE;
179 /* ignore but needed for compatibility with GNU diff */
182 /* silently ignore for backwards compatibility */
185 dflags |= D_SKIPBLANKLINES;
188 push_ignore_pats(optarg);
191 dflags |= D_IGNORECASE;
194 if (label[0] == NULL)
196 else if (label[1] == NULL)
208 if (FORMAT_MISMATCHED(D_NREVERSE))
209 conflicting_format();
210 diff_format = D_NREVERSE;
213 dflags |= D_PROTOTYPE;
222 if (FORMAT_MISMATCHED(D_BRIEF))
223 conflicting_format();
224 diff_format = D_BRIEF;
236 dflags |= D_EXPANDTABS;
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)
247 diff_context = (int)l;
251 dflags |= D_IGNOREBLANKS;
255 width = (int) strtonum(optarg, 1, INT_MAX, &errstr);
257 warnx("Invalid argument for width");
262 read_excludes_file(optarg);
265 push_excludes(optarg);
268 if (FORMAT_MISMATCHED(D_SIDEBYSIDE))
269 conflicting_format();
270 diff_format = D_SIDEBYSIDE;
272 case OPT_CHANGED_GROUP_FORMAT:
273 if (FORMAT_MISMATCHED(D_GFORMAT))
274 conflicting_format();
275 diff_format = D_GFORMAT;
276 group_format = optarg;
278 case OPT_HORIZON_LINES:
279 break; /* XXX TODO for compatibility with GNU diff3 */
280 case OPT_IGN_FN_CASE:
281 ignore_file_case = 1;
283 case OPT_NO_IGN_FN_CASE:
284 ignore_file_case = 0;
287 if (FORMAT_MISMATCHED(D_NORMAL))
288 conflicting_format();
289 diff_format = D_NORMAL;
292 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
294 warnx("Invalid argument for tabsize");
301 case OPT_SUPPRESS_COMMON:
309 newarg = optind != prevoptind;
312 if (diff_format == D_UNSET && (dflags & D_PROTOTYPE) != 0)
313 diff_format = D_CONTEXT;
314 if (diff_format == D_UNSET)
315 diff_format = D_NORMAL;
320 if (pledge("stdio rpath tmppath", NULL) == -1)
325 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
326 * driver routine. Both drivers use the contents of stb1 and stb2.
330 if (ignore_pats != NULL) {
334 if ((error = regcomp(&ignore_re, ignore_pats,
335 REG_NEWLINE | REG_EXTENDED)) != 0) {
336 regerror(error, &ignore_re, buf, sizeof(buf));
337 if (*ignore_pats != '\0')
338 errx(2, "%s: %s", ignore_pats, buf);
343 if (strcmp(argv[0], "-") == 0) {
344 fstat(STDIN_FILENO, &stb1);
346 } else if (stat(argv[0], &stb1) != 0) {
347 if (!Nflag || errno != ENOENT)
348 err(2, "%s", argv[0]);
350 memset(&stb1, 0, sizeof(struct stat));
353 if (strcmp(argv[1], "-") == 0) {
354 fstat(STDIN_FILENO, &stb2);
356 } else if (stat(argv[1], &stb2) != 0) {
357 if (!Nflag || errno != ENOENT)
358 err(2, "%s", argv[1]);
360 memset(&stb2, 0, sizeof(stb2));
361 stb2.st_mode = stb1.st_mode;
364 if (dflags & D_EMPTY1 && dflags & D_EMPTY2){
370 if (stb1.st_mode == 0)
371 stb1.st_mode = stb2.st_mode;
373 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
374 errx(2, "can't compare - to a directory");
375 set_argstr(oargv, argv);
376 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
377 if (diff_format == D_IFDEF)
378 errx(2, "-D option not supported with directories");
379 diffdir(argv[0], argv[1], dflags);
381 if (S_ISDIR(stb1.st_mode)) {
382 argv[0] = splice(argv[0], argv[1]);
383 if (stat(argv[0], &stb1) == -1)
384 err(2, "%s", argv[0]);
386 if (S_ISDIR(stb2.st_mode)) {
387 argv[1] = splice(argv[1], argv[0]);
388 if (stat(argv[1], &stb2) == -1)
389 err(2, "%s", argv[1]);
391 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
398 set_argstr(char **av, char **ave)
403 argsize = 4 + *ave - *av + 1;
404 diffargs = xmalloc(argsize);
405 strlcpy(diffargs, "diff", argsize);
406 for (ap = av + 1; ap < ave; ap++) {
407 if (strcmp(*ap, "--") != 0) {
408 strlcat(diffargs, " ", argsize);
409 strlcat(diffargs, *ap, argsize);
415 * Read in an excludes file and push each line.
418 read_excludes_file(char *file)
424 if (strcmp(file, "-") == 0)
426 else if ((fp = fopen(file, "r")) == NULL)
428 while ((buf = fgetln(fp, &len)) != NULL) {
429 if (buf[len - 1] == '\n')
431 if ((pattern = strndup(buf, len)) == NULL)
433 push_excludes(pattern);
435 if (strcmp(file, "-") != 0)
440 * Push a pattern onto the excludes list.
443 push_excludes(char *pattern)
445 struct excludes *entry;
447 entry = xmalloc(sizeof(*entry));
448 entry->pattern = pattern;
449 entry->next = excludes_list;
450 excludes_list = entry;
454 push_ignore_pats(char *pattern)
458 if (ignore_pats == NULL)
459 ignore_pats = xstrdup(pattern);
461 /* old + "|" + new + NUL */
462 len = strlen(ignore_pats) + strlen(pattern) + 2;
463 ignore_pats = xreallocarray(ignore_pats, 1, len);
464 strlcat(ignore_pats, "|", len);
465 strlcat(ignore_pats, pattern, len);
470 print_only(const char *path, size_t dirlen, const char *entry)
474 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
478 print_status(int val, char *path1, char *path2, const char *entry)
480 if (label[0] != NULL) path1 = label[0];
481 if (label[1] != NULL) path2 = label[1];
485 printf("Binary files %s%s and %s%s differ\n",
486 path1, entry, path2, entry);
489 if (diff_format == D_BRIEF)
490 printf("Files %s%s and %s%s differ\n",
491 path1, entry, path2, entry);
495 printf("Files %s%s and %s%s are identical\n",
496 path1, entry, path2, entry);
499 printf("File %s%s is a directory while file %s%s is a regular file\n",
500 path1, entry, path2, entry);
503 printf("File %s%s is a regular file while file %s%s is a directory\n",
504 path1, entry, path2, entry);
507 printf("File %s%s is not a regular file or directory and was skipped\n",
511 printf("File %s%s is not a regular file or directory and was skipped\n",
520 (void)fprintf(stderr,
521 "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
522 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
523 " [-I pattern] [-L label] file1 file2\n"
524 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
525 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
526 " -C number file1 file2\n"
527 " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
528 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
529 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
530 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
531 " -U number file1 file2\n"
532 " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
533 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
534 " [-S name] [-X file] [-x pattern] dir1 dir2\n"
535 " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
536 " [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
537 " [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
538 " [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
539 " -y | --side-by-side file1 file2\n");
545 conflicting_format(void)
548 fprintf(stderr, "error: conflicting output format options.\n");