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 push_excludes(char *);
104 void push_ignore_pats(char *);
105 void read_excludes_file(char *file);
106 void set_argstr(char **, char **);
109 main(int argc, char **argv)
111 const char *errstr = NULL;
114 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
124 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
126 case '0': case '1': case '2': case '3': case '4':
127 case '5': case '6': case '7': case '8': case '9':
129 usage(); /* disallow -[0-9]+ */
130 else if (lastch == 'c' || lastch == 'u')
132 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
134 diff_context = (diff_context * 10) + (ch - '0');
137 dflags |= D_FORCEASCII;
140 dflags |= D_FOLDBLANKS;
145 diff_format = D_CONTEXT;
146 if (optarg != NULL) {
147 l = strtol(optarg, &ep, 10);
148 if (*ep != '\0' || l < 0 || l >= INT_MAX)
150 diff_context = (int)l;
157 diff_format = D_IFDEF;
161 diff_format = D_EDIT;
164 diff_format = D_REVERSE;
167 /* ignore but needed for compatibility with GNU diff */
170 /* silently ignore for backwards compatibility */
173 dflags |= D_SKIPBLANKLINES;
176 push_ignore_pats(optarg);
179 dflags |= D_IGNORECASE;
182 if (label[0] == NULL)
184 else if (label[1] == NULL)
196 diff_format = D_NREVERSE;
199 if (diff_format == 0)
200 diff_format = D_CONTEXT;
201 dflags |= D_PROTOTYPE;
210 diff_format = D_BRIEF;
222 dflags |= D_EXPANDTABS;
226 diff_format = D_UNIFIED;
227 if (optarg != NULL) {
228 l = strtol(optarg, &ep, 10);
229 if (*ep != '\0' || l < 0 || l >= INT_MAX)
231 diff_context = (int)l;
235 dflags |= D_IGNOREBLANKS;
239 width = (int) strtonum(optarg, 1, INT_MAX, &errstr);
241 warnx("Invalid argument for width");
246 read_excludes_file(optarg);
249 push_excludes(optarg);
252 diff_format = D_SIDEBYSIDE;
254 case OPT_CHANGED_GROUP_FORMAT:
255 diff_format = D_GFORMAT;
256 group_format = optarg;
258 case OPT_HORIZON_LINES:
259 break; /* XXX TODO for compatibility with GNU diff3 */
260 case OPT_IGN_FN_CASE:
261 ignore_file_case = 1;
263 case OPT_NO_IGN_FN_CASE:
264 ignore_file_case = 0;
267 diff_format = D_NORMAL;
270 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
272 warnx("Invalid argument for tabsize");
279 case OPT_SUPPRESS_COMMON:
287 newarg = optind != prevoptind;
294 if (pledge("stdio rpath tmppath", NULL) == -1)
299 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
300 * driver routine. Both drivers use the contents of stb1 and stb2.
304 if (ignore_pats != NULL) {
308 if ((error = regcomp(&ignore_re, ignore_pats,
309 REG_NEWLINE | REG_EXTENDED)) != 0) {
310 regerror(error, &ignore_re, buf, sizeof(buf));
311 if (*ignore_pats != '\0')
312 errx(2, "%s: %s", ignore_pats, buf);
317 if (strcmp(argv[0], "-") == 0) {
318 fstat(STDIN_FILENO, &stb1);
320 } else if (stat(argv[0], &stb1) != 0)
321 err(2, "%s", argv[0]);
322 if (strcmp(argv[1], "-") == 0) {
323 fstat(STDIN_FILENO, &stb2);
325 } else if (stat(argv[1], &stb2) != 0)
326 err(2, "%s", argv[1]);
327 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
328 errx(2, "can't compare - to a directory");
329 set_argstr(oargv, argv);
330 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
331 if (diff_format == D_IFDEF)
332 errx(2, "-D option not supported with directories");
333 diffdir(argv[0], argv[1], dflags);
335 if (S_ISDIR(stb1.st_mode)) {
336 argv[0] = splice(argv[0], argv[1]);
337 if (stat(argv[0], &stb1) == -1)
338 err(2, "%s", argv[0]);
340 if (S_ISDIR(stb2.st_mode)) {
341 argv[1] = splice(argv[1], argv[0]);
342 if (stat(argv[1], &stb2) == -1)
343 err(2, "%s", argv[1]);
345 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
352 set_argstr(char **av, char **ave)
357 argsize = 4 + *ave - *av + 1;
358 diffargs = xmalloc(argsize);
359 strlcpy(diffargs, "diff", argsize);
360 for (ap = av + 1; ap < ave; ap++) {
361 if (strcmp(*ap, "--") != 0) {
362 strlcat(diffargs, " ", argsize);
363 strlcat(diffargs, *ap, argsize);
369 * Read in an excludes file and push each line.
372 read_excludes_file(char *file)
378 if (strcmp(file, "-") == 0)
380 else if ((fp = fopen(file, "r")) == NULL)
382 while ((buf = fgetln(fp, &len)) != NULL) {
383 if (buf[len - 1] == '\n')
385 if ((pattern = strndup(buf, len)) == NULL)
387 push_excludes(pattern);
389 if (strcmp(file, "-") != 0)
394 * Push a pattern onto the excludes list.
397 push_excludes(char *pattern)
399 struct excludes *entry;
401 entry = xmalloc(sizeof(*entry));
402 entry->pattern = pattern;
403 entry->next = excludes_list;
404 excludes_list = entry;
408 push_ignore_pats(char *pattern)
412 if (ignore_pats == NULL)
413 ignore_pats = xstrdup(pattern);
415 /* old + "|" + new + NUL */
416 len = strlen(ignore_pats) + strlen(pattern) + 2;
417 ignore_pats = xreallocarray(ignore_pats, 1, len);
418 strlcat(ignore_pats, "|", len);
419 strlcat(ignore_pats, pattern, len);
424 print_only(const char *path, size_t dirlen, const char *entry)
428 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
432 print_status(int val, char *path1, char *path2, const char *entry)
436 printf("Binary files %s%s and %s%s differ\n",
437 path1, entry, path2, entry);
440 if (diff_format == D_BRIEF)
441 printf("Files %s%s and %s%s differ\n",
442 path1, entry, path2, entry);
446 printf("Files %s%s and %s%s are identical\n",
447 path1, entry, path2, entry);
450 printf("File %s%s is a directory while file %s%s is a regular file\n",
451 path1, entry, path2, entry);
454 printf("File %s%s is a regular file while file %s%s is a directory\n",
455 path1, entry, path2, entry);
458 printf("File %s%s is not a regular file or directory and was skipped\n",
462 printf("File %s%s is not a regular file or directory and was skipped\n",
471 (void)fprintf(stderr,
472 "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
473 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
474 " [-I pattern] [-L label] file1 file2\n"
475 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
476 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
477 " -C number file1 file2\n"
478 " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
479 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
480 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
481 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
482 " -U number file1 file2\n"
483 " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
484 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
485 " [-S name] [-X file] [-x pattern] dir1 dir2\n"
486 " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
487 " [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
488 " [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
489 " [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
490 " -y | --side-by-side file1 file2\n");