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$");
40 int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag;
41 int diff_format, diff_context, status, ignore_file_case, suppress_common;
42 int tabsize = 8, width = 130;
43 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
44 char *group_format = NULL;
45 struct stat stb1, stb2;
46 struct excludes *excludes_list;
49 #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y"
51 OPT_TSIZE = CHAR_MAX + 1,
57 OPT_CHANGED_GROUP_FORMAT,
61 static struct option longopts[] = {
62 { "text", no_argument, 0, 'a' },
63 { "ignore-space-change", no_argument, 0, 'b' },
64 { "context", optional_argument, 0, 'C' },
65 { "ifdef", required_argument, 0, 'D' },
66 { "minimal", no_argument, 0, 'd' },
67 { "ed", no_argument, 0, 'e' },
68 { "forward-ed", no_argument, 0, 'f' },
69 { "speed-large-files", no_argument, NULL, 'H' },
70 { "ignore-blank-lines", no_argument, 0, 'B' },
71 { "ignore-matching-lines", required_argument, 0, 'I' },
72 { "ignore-case", no_argument, 0, 'i' },
73 { "paginate", no_argument, NULL, 'l' },
74 { "label", required_argument, 0, 'L' },
75 { "new-file", no_argument, 0, 'N' },
76 { "rcs", no_argument, 0, 'n' },
77 { "unidirectional-new-file", no_argument, 0, 'P' },
78 { "show-c-function", no_argument, 0, 'p' },
79 { "brief", no_argument, 0, 'q' },
80 { "recursive", no_argument, 0, 'r' },
81 { "report-identical-files", no_argument, 0, 's' },
82 { "starting-file", required_argument, 0, 'S' },
83 { "expand-tabs", no_argument, 0, 't' },
84 { "initial-tab", no_argument, 0, 'T' },
85 { "unified", optional_argument, 0, 'U' },
86 { "ignore-all-space", no_argument, 0, 'w' },
87 { "width", required_argument, 0, 'W' },
88 { "exclude", required_argument, 0, 'x' },
89 { "exclude-from", required_argument, 0, 'X' },
90 { "side-by-side", no_argument, NULL, 'y' },
91 { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE },
92 { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES },
93 { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE },
94 { "normal", no_argument, NULL, OPT_NORMAL },
95 { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR },
96 { "tabsize", required_argument, NULL, OPT_TSIZE },
97 { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT},
98 { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON },
102 void usage(void) __dead2;
103 void conflicting_format(void) __dead2;
104 void push_excludes(char *);
105 void push_ignore_pats(char *);
106 void read_excludes_file(char *file);
107 void set_argstr(char **, char **);
110 main(int argc, char **argv)
112 const char *errstr = NULL;
115 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
124 diff_format = D_UNSET;
125 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
127 case '0': case '1': case '2': case '3': case '4':
128 case '5': case '6': case '7': case '8': case '9':
130 usage(); /* disallow -[0-9]+ */
131 else if (lastch == 'c' || lastch == 'u')
133 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
135 diff_context = (diff_context * 10) + (ch - '0');
138 dflags |= D_FORCEASCII;
141 dflags |= D_FOLDBLANKS;
145 if (diff_format != D_UNSET)
146 conflicting_format();
148 diff_format = D_CONTEXT;
149 if (optarg != NULL) {
150 l = strtol(optarg, &ep, 10);
151 if (*ep != '\0' || l < 0 || l >= INT_MAX)
153 diff_context = (int)l;
160 if (diff_format != D_UNSET)
161 conflicting_format();
162 diff_format = D_IFDEF;
166 if (diff_format != D_UNSET)
167 conflicting_format();
168 diff_format = D_EDIT;
171 if (diff_format != D_UNSET)
172 conflicting_format();
173 diff_format = D_REVERSE;
176 /* ignore but needed for compatibility with GNU diff */
179 /* silently ignore for backwards compatibility */
182 dflags |= D_SKIPBLANKLINES;
185 push_ignore_pats(optarg);
188 dflags |= D_IGNORECASE;
191 if (label[0] == NULL)
193 else if (label[1] == NULL)
205 if (diff_format != D_UNSET)
206 conflicting_format();
207 diff_format = D_NREVERSE;
210 if (diff_format == D_UNSET)
211 diff_format = D_CONTEXT;
212 dflags |= D_PROTOTYPE;
221 if (diff_format != D_UNSET)
222 conflicting_format();
223 diff_format = D_BRIEF;
235 dflags |= D_EXPANDTABS;
239 if (diff_format != D_UNSET)
240 conflicting_format();
241 diff_format = D_UNIFIED;
242 if (optarg != NULL) {
243 l = strtol(optarg, &ep, 10);
244 if (*ep != '\0' || l < 0 || l >= INT_MAX)
246 diff_context = (int)l;
250 dflags |= D_IGNOREBLANKS;
254 width = (int) strtonum(optarg, 1, INT_MAX, &errstr);
256 warnx("Invalid argument for width");
261 read_excludes_file(optarg);
264 push_excludes(optarg);
267 if (diff_format != D_UNSET)
268 conflicting_format();
269 diff_format = D_SIDEBYSIDE;
271 case OPT_CHANGED_GROUP_FORMAT:
272 if (diff_format != D_UNSET)
273 conflicting_format();
274 diff_format = D_GFORMAT;
275 group_format = optarg;
277 case OPT_HORIZON_LINES:
278 break; /* XXX TODO for compatibility with GNU diff3 */
279 case OPT_IGN_FN_CASE:
280 ignore_file_case = 1;
282 case OPT_NO_IGN_FN_CASE:
283 ignore_file_case = 0;
286 if (diff_format != D_UNSET)
287 conflicting_format();
288 diff_format = D_NORMAL;
291 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
293 warnx("Invalid argument for tabsize");
300 case OPT_SUPPRESS_COMMON:
308 newarg = optind != prevoptind;
311 if (diff_format == D_UNSET)
312 diff_format = D_NORMAL;
317 if (pledge("stdio rpath tmppath", NULL) == -1)
322 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
323 * driver routine. Both drivers use the contents of stb1 and stb2.
327 if (ignore_pats != NULL) {
331 if ((error = regcomp(&ignore_re, ignore_pats,
332 REG_NEWLINE | REG_EXTENDED)) != 0) {
333 regerror(error, &ignore_re, buf, sizeof(buf));
334 if (*ignore_pats != '\0')
335 errx(2, "%s: %s", ignore_pats, buf);
340 if (strcmp(argv[0], "-") == 0) {
341 fstat(STDIN_FILENO, &stb1);
343 } else if (stat(argv[0], &stb1) != 0)
344 err(2, "%s", argv[0]);
345 if (strcmp(argv[1], "-") == 0) {
346 fstat(STDIN_FILENO, &stb2);
348 } else if (stat(argv[1], &stb2) != 0)
349 err(2, "%s", argv[1]);
350 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
351 errx(2, "can't compare - to a directory");
352 set_argstr(oargv, argv);
353 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
354 if (diff_format == D_IFDEF)
355 errx(2, "-D option not supported with directories");
356 diffdir(argv[0], argv[1], dflags);
358 if (S_ISDIR(stb1.st_mode)) {
359 argv[0] = splice(argv[0], argv[1]);
360 if (stat(argv[0], &stb1) == -1)
361 err(2, "%s", argv[0]);
363 if (S_ISDIR(stb2.st_mode)) {
364 argv[1] = splice(argv[1], argv[0]);
365 if (stat(argv[1], &stb2) == -1)
366 err(2, "%s", argv[1]);
368 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
375 set_argstr(char **av, char **ave)
380 argsize = 4 + *ave - *av + 1;
381 diffargs = xmalloc(argsize);
382 strlcpy(diffargs, "diff", argsize);
383 for (ap = av + 1; ap < ave; ap++) {
384 if (strcmp(*ap, "--") != 0) {
385 strlcat(diffargs, " ", argsize);
386 strlcat(diffargs, *ap, argsize);
392 * Read in an excludes file and push each line.
395 read_excludes_file(char *file)
401 if (strcmp(file, "-") == 0)
403 else if ((fp = fopen(file, "r")) == NULL)
405 while ((buf = fgetln(fp, &len)) != NULL) {
406 if (buf[len - 1] == '\n')
408 if ((pattern = strndup(buf, len)) == NULL)
410 push_excludes(pattern);
412 if (strcmp(file, "-") != 0)
417 * Push a pattern onto the excludes list.
420 push_excludes(char *pattern)
422 struct excludes *entry;
424 entry = xmalloc(sizeof(*entry));
425 entry->pattern = pattern;
426 entry->next = excludes_list;
427 excludes_list = entry;
431 push_ignore_pats(char *pattern)
435 if (ignore_pats == NULL)
436 ignore_pats = xstrdup(pattern);
438 /* old + "|" + new + NUL */
439 len = strlen(ignore_pats) + strlen(pattern) + 2;
440 ignore_pats = xreallocarray(ignore_pats, 1, len);
441 strlcat(ignore_pats, "|", len);
442 strlcat(ignore_pats, pattern, len);
447 print_only(const char *path, size_t dirlen, const char *entry)
451 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
455 print_status(int val, char *path1, char *path2, const char *entry)
459 printf("Binary files %s%s and %s%s differ\n",
460 path1, entry, path2, entry);
463 if (diff_format == D_BRIEF)
464 printf("Files %s%s and %s%s differ\n",
465 path1, entry, path2, entry);
469 printf("Files %s%s and %s%s are identical\n",
470 path1, entry, path2, entry);
473 printf("File %s%s is a directory while file %s%s is a regular file\n",
474 path1, entry, path2, entry);
477 printf("File %s%s is a regular file while file %s%s is a directory\n",
478 path1, entry, path2, entry);
481 printf("File %s%s is not a regular file or directory and was skipped\n",
485 printf("File %s%s is not a regular file or directory and was skipped\n",
494 (void)fprintf(stderr,
495 "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
496 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
497 " [-I pattern] [-L label] file1 file2\n"
498 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
499 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
500 " -C number file1 file2\n"
501 " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
502 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
503 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
504 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
505 " -U number file1 file2\n"
506 " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
507 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
508 " [-S name] [-X file] [-x pattern] dir1 dir2\n"
509 " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
510 " [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
511 " [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
512 " [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
513 " -y | --side-by-side file1 file2\n");
519 conflicting_format(void)
522 fprintf(stderr, "error: conflicting output format options.\n");