1 /* $OpenBSD: diff.c,v 1.65 2015/12/29 19:04:46 gsoares 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;
41 int diff_format, diff_context, status, ignore_file_case;
43 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
44 struct stat stb1, stb2;
45 struct excludes *excludes_list;
48 #define OPTIONS "0123456789aBbC:cdD:efhI:iL:lnNPpqrS:sTtU:uwX:x:"
50 OPT_TSIZE = CHAR_MAX + 1,
56 OPT_SPEED_LARGE_FILES,
59 static struct option longopts[] = {
60 { "text", no_argument, 0, 'a' },
61 { "ignore-space-change", no_argument, 0, 'b' },
62 { "context", optional_argument, 0, 'C' },
63 { "ifdef", required_argument, 0, 'D' },
64 { "minimal", no_argument, 0, 'd' },
65 { "ed", no_argument, 0, 'e' },
66 { "forward-ed", no_argument, 0, 'f' },
67 { "ignore-matching-lines", required_argument, 0, 'I' },
68 { "ignore-case", no_argument, 0, 'i' },
69 { "paginate", no_argument, NULL, 'l' },
70 { "label", required_argument, 0, 'L' },
71 { "new-file", no_argument, 0, 'N' },
72 { "rcs", no_argument, 0, 'n' },
73 { "unidirectional-new-file", no_argument, 0, 'P' },
74 { "show-c-function", no_argument, 0, 'p' },
75 { "brief", no_argument, 0, 'q' },
76 { "recursive", no_argument, 0, 'r' },
77 { "report-identical-files", no_argument, 0, 's' },
78 { "starting-file", required_argument, 0, 'S' },
79 { "expand-tabs", no_argument, 0, 't' },
80 { "initial-tab", no_argument, 0, 'T' },
81 { "unified", optional_argument, 0, 'U' },
82 { "ignore-all-space", no_argument, 0, 'w' },
83 { "exclude", required_argument, 0, 'x' },
84 { "exclude-from", required_argument, 0, 'X' },
85 { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE },
86 { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES },
87 { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE },
88 { "normal", no_argument, NULL, OPT_NORMAL },
89 { "speed-large-files", no_argument, NULL, OPT_SPEED_LARGE_FILES},
90 { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR },
91 { "tabsize", optional_argument, NULL, OPT_TSIZE },
95 void usage(void) __dead2;
96 void push_excludes(char *);
97 void push_ignore_pats(char *);
98 void read_excludes_file(char *file);
99 void set_argstr(char **, char **);
102 main(int argc, char **argv)
104 const char *errstr = NULL;
107 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
117 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
119 case '0': case '1': case '2': case '3': case '4':
120 case '5': case '6': case '7': case '8': case '9':
122 usage(); /* disallow -[0-9]+ */
123 else if (lastch == 'c' || lastch == 'u')
125 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
127 diff_context = (diff_context * 10) + (ch - '0');
130 dflags |= D_FORCEASCII;
133 dflags |= D_FOLDBLANKS;
138 diff_format = D_CONTEXT;
139 if (optarg != NULL) {
140 l = strtol(optarg, &ep, 10);
141 if (*ep != '\0' || l < 0 || l >= INT_MAX)
143 diff_context = (int)l;
150 diff_format = D_IFDEF;
154 diff_format = D_EDIT;
157 diff_format = D_REVERSE;
160 /* silently ignore for backwards compatibility */
163 push_ignore_pats(optarg);
166 dflags |= D_IGNORECASE;
169 if (label[0] == NULL)
171 else if (label[1] == NULL)
183 diff_format = D_NREVERSE;
186 if (diff_format == 0)
187 diff_format = D_CONTEXT;
188 dflags |= D_PROTOTYPE;
197 diff_format = D_BRIEF;
209 dflags |= D_EXPANDTABS;
213 diff_format = D_UNIFIED;
214 if (optarg != NULL) {
215 l = strtol(optarg, &ep, 10);
216 if (*ep != '\0' || l < 0 || l >= INT_MAX)
218 diff_context = (int)l;
222 dflags |= D_IGNOREBLANKS;
225 read_excludes_file(optarg);
228 push_excludes(optarg);
230 case OPT_HORIZON_LINES:
231 break; /* XXX TODO for compatibility with GNU diff3 */
232 case OPT_IGN_FN_CASE:
233 ignore_file_case = 1;
235 case OPT_NO_IGN_FN_CASE:
236 ignore_file_case = 0;
239 diff_format = D_NORMAL;
242 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
244 warnx("Invalid argument for tabsize");
248 case OPT_SPEED_LARGE_FILES:
249 break; /* ignore but needed for compatibility with GNU diff */
258 newarg = optind != prevoptind;
265 if (pledge("stdio rpath tmppath", NULL) == -1)
270 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
271 * driver routine. Both drivers use the contents of stb1 and stb2.
275 if (ignore_pats != NULL) {
279 if ((error = regcomp(&ignore_re, ignore_pats,
280 REG_NEWLINE | REG_EXTENDED)) != 0) {
281 regerror(error, &ignore_re, buf, sizeof(buf));
282 if (*ignore_pats != '\0')
283 errx(2, "%s: %s", ignore_pats, buf);
288 if (strcmp(argv[0], "-") == 0) {
289 fstat(STDIN_FILENO, &stb1);
291 } else if (stat(argv[0], &stb1) != 0)
292 err(2, "%s", argv[0]);
293 if (strcmp(argv[1], "-") == 0) {
294 fstat(STDIN_FILENO, &stb2);
296 } else if (stat(argv[1], &stb2) != 0)
297 err(2, "%s", argv[1]);
298 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
299 errx(2, "can't compare - to a directory");
300 set_argstr(oargv, argv);
301 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
302 if (diff_format == D_IFDEF)
303 errx(2, "-D option not supported with directories");
304 diffdir(argv[0], argv[1], dflags);
306 if (S_ISDIR(stb1.st_mode)) {
307 argv[0] = splice(argv[0], argv[1]);
308 if (stat(argv[0], &stb1) < 0)
309 err(2, "%s", argv[0]);
311 if (S_ISDIR(stb2.st_mode)) {
312 argv[1] = splice(argv[1], argv[0]);
313 if (stat(argv[1], &stb2) < 0)
314 err(2, "%s", argv[1]);
316 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
323 set_argstr(char **av, char **ave)
328 argsize = 4 + *ave - *av + 1;
329 diffargs = xmalloc(argsize);
330 strlcpy(diffargs, "diff", argsize);
331 for (ap = av + 1; ap < ave; ap++) {
332 if (strcmp(*ap, "--") != 0) {
333 strlcat(diffargs, " ", argsize);
334 strlcat(diffargs, *ap, argsize);
340 * Read in an excludes file and push each line.
343 read_excludes_file(char *file)
349 if (strcmp(file, "-") == 0)
351 else if ((fp = fopen(file, "r")) == NULL)
353 while ((buf = fgetln(fp, &len)) != NULL) {
354 if (buf[len - 1] == '\n')
356 if ((pattern = strndup(buf, len)) == NULL)
358 push_excludes(pattern);
360 if (strcmp(file, "-") != 0)
365 * Push a pattern onto the excludes list.
368 push_excludes(char *pattern)
370 struct excludes *entry;
372 entry = xmalloc(sizeof(*entry));
373 entry->pattern = pattern;
374 entry->next = excludes_list;
375 excludes_list = entry;
379 push_ignore_pats(char *pattern)
383 if (ignore_pats == NULL)
384 ignore_pats = xstrdup(pattern);
386 /* old + "|" + new + NUL */
387 len = strlen(ignore_pats) + strlen(pattern) + 2;
388 ignore_pats = xreallocarray(ignore_pats, 1, len);
389 strlcat(ignore_pats, "|", len);
390 strlcat(ignore_pats, pattern, len);
395 print_only(const char *path, size_t dirlen, const char *entry)
399 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
403 print_status(int val, char *path1, char *path2, const char *entry)
407 printf("Binary files %s%s and %s%s differ\n",
408 path1, entry, path2, entry);
411 if (diff_format == D_BRIEF)
412 printf("Files %s%s and %s%s differ\n",
413 path1, entry, path2, entry);
417 printf("Files %s%s and %s%s are identical\n",
418 path1, entry, path2, entry);
421 printf("File %s%s is a directory while file %s%s is a regular file\n",
422 path1, entry, path2, entry);
425 printf("File %s%s is a regular file while file %s%s is a directory\n",
426 path1, entry, path2, entry);
429 printf("File %s%s is not a regular file or directory and was skipped\n",
433 printf("File %s%s is not a regular file or directory and was skipped\n",
442 (void)fprintf(stderr,
443 "usage: diff [-abdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
444 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
445 " [-I pattern] [-L label] file1 file2\n"
446 " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
447 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
448 " -C number file1 file2\n"
449 " diff [-abdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
450 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
451 " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
452 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
453 " -U number file1 file2\n"
454 " diff [-abdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
455 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
456 " [-S name] [-X file] [-x pattern] dir1 dir2\n");