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 char *group_format = NULL;
45 struct stat stb1, stb2;
46 struct excludes *excludes_list;
49 #define OPTIONS "0123456789aBbC:cdD:efhI:iL:lnNPpqrS:sTtU:uwX:x:"
51 OPT_TSIZE = CHAR_MAX + 1,
57 OPT_SPEED_LARGE_FILES,
58 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 { "ignore-matching-lines", required_argument, 0, 'I' },
70 { "ignore-case", no_argument, 0, 'i' },
71 { "paginate", no_argument, NULL, 'l' },
72 { "label", required_argument, 0, 'L' },
73 { "new-file", no_argument, 0, 'N' },
74 { "rcs", no_argument, 0, 'n' },
75 { "unidirectional-new-file", no_argument, 0, 'P' },
76 { "show-c-function", no_argument, 0, 'p' },
77 { "brief", no_argument, 0, 'q' },
78 { "recursive", no_argument, 0, 'r' },
79 { "report-identical-files", no_argument, 0, 's' },
80 { "starting-file", required_argument, 0, 'S' },
81 { "expand-tabs", no_argument, 0, 't' },
82 { "initial-tab", no_argument, 0, 'T' },
83 { "unified", optional_argument, 0, 'U' },
84 { "ignore-all-space", no_argument, 0, 'w' },
85 { "exclude", required_argument, 0, 'x' },
86 { "exclude-from", required_argument, 0, 'X' },
87 { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE },
88 { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES },
89 { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE },
90 { "normal", no_argument, NULL, OPT_NORMAL },
91 { "speed-large-files", no_argument, NULL, OPT_SPEED_LARGE_FILES},
92 { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR },
93 { "tabsize", optional_argument, NULL, OPT_TSIZE },
94 { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT},
98 void usage(void) __dead2;
99 void push_excludes(char *);
100 void push_ignore_pats(char *);
101 void read_excludes_file(char *file);
102 void set_argstr(char **, char **);
105 main(int argc, char **argv)
107 const char *errstr = NULL;
110 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
120 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
122 case '0': case '1': case '2': case '3': case '4':
123 case '5': case '6': case '7': case '8': case '9':
125 usage(); /* disallow -[0-9]+ */
126 else if (lastch == 'c' || lastch == 'u')
128 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
130 diff_context = (diff_context * 10) + (ch - '0');
133 dflags |= D_FORCEASCII;
136 dflags |= D_FOLDBLANKS;
141 diff_format = D_CONTEXT;
142 if (optarg != NULL) {
143 l = strtol(optarg, &ep, 10);
144 if (*ep != '\0' || l < 0 || l >= INT_MAX)
146 diff_context = (int)l;
153 diff_format = D_IFDEF;
157 diff_format = D_EDIT;
160 diff_format = D_REVERSE;
163 /* silently ignore for backwards compatibility */
166 push_ignore_pats(optarg);
169 dflags |= D_IGNORECASE;
172 if (label[0] == NULL)
174 else if (label[1] == NULL)
186 diff_format = D_NREVERSE;
189 if (diff_format == 0)
190 diff_format = D_CONTEXT;
191 dflags |= D_PROTOTYPE;
200 diff_format = D_BRIEF;
212 dflags |= D_EXPANDTABS;
216 diff_format = D_UNIFIED;
217 if (optarg != NULL) {
218 l = strtol(optarg, &ep, 10);
219 if (*ep != '\0' || l < 0 || l >= INT_MAX)
221 diff_context = (int)l;
225 dflags |= D_IGNOREBLANKS;
228 read_excludes_file(optarg);
231 push_excludes(optarg);
233 case OPT_CHANGED_GROUP_FORMAT:
234 diff_format = D_GFORMAT;
235 group_format = optarg;
237 case OPT_HORIZON_LINES:
238 break; /* XXX TODO for compatibility with GNU diff3 */
239 case OPT_IGN_FN_CASE:
240 ignore_file_case = 1;
242 case OPT_NO_IGN_FN_CASE:
243 ignore_file_case = 0;
246 diff_format = D_NORMAL;
249 tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
251 warnx("Invalid argument for tabsize");
255 case OPT_SPEED_LARGE_FILES:
256 break; /* ignore but needed for compatibility with GNU diff */
265 newarg = optind != prevoptind;
272 if (pledge("stdio rpath tmppath", NULL) == -1)
277 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
278 * driver routine. Both drivers use the contents of stb1 and stb2.
282 if (ignore_pats != NULL) {
286 if ((error = regcomp(&ignore_re, ignore_pats,
287 REG_NEWLINE | REG_EXTENDED)) != 0) {
288 regerror(error, &ignore_re, buf, sizeof(buf));
289 if (*ignore_pats != '\0')
290 errx(2, "%s: %s", ignore_pats, buf);
295 if (strcmp(argv[0], "-") == 0) {
296 fstat(STDIN_FILENO, &stb1);
298 } else if (stat(argv[0], &stb1) != 0)
299 err(2, "%s", argv[0]);
300 if (strcmp(argv[1], "-") == 0) {
301 fstat(STDIN_FILENO, &stb2);
303 } else if (stat(argv[1], &stb2) != 0)
304 err(2, "%s", argv[1]);
305 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
306 errx(2, "can't compare - to a directory");
307 set_argstr(oargv, argv);
308 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
309 if (diff_format == D_IFDEF)
310 errx(2, "-D option not supported with directories");
311 diffdir(argv[0], argv[1], dflags);
313 if (S_ISDIR(stb1.st_mode)) {
314 argv[0] = splice(argv[0], argv[1]);
315 if (stat(argv[0], &stb1) < 0)
316 err(2, "%s", argv[0]);
318 if (S_ISDIR(stb2.st_mode)) {
319 argv[1] = splice(argv[1], argv[0]);
320 if (stat(argv[1], &stb2) < 0)
321 err(2, "%s", argv[1]);
323 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
330 set_argstr(char **av, char **ave)
335 argsize = 4 + *ave - *av + 1;
336 diffargs = xmalloc(argsize);
337 strlcpy(diffargs, "diff", argsize);
338 for (ap = av + 1; ap < ave; ap++) {
339 if (strcmp(*ap, "--") != 0) {
340 strlcat(diffargs, " ", argsize);
341 strlcat(diffargs, *ap, argsize);
347 * Read in an excludes file and push each line.
350 read_excludes_file(char *file)
356 if (strcmp(file, "-") == 0)
358 else if ((fp = fopen(file, "r")) == NULL)
360 while ((buf = fgetln(fp, &len)) != NULL) {
361 if (buf[len - 1] == '\n')
363 if ((pattern = strndup(buf, len)) == NULL)
365 push_excludes(pattern);
367 if (strcmp(file, "-") != 0)
372 * Push a pattern onto the excludes list.
375 push_excludes(char *pattern)
377 struct excludes *entry;
379 entry = xmalloc(sizeof(*entry));
380 entry->pattern = pattern;
381 entry->next = excludes_list;
382 excludes_list = entry;
386 push_ignore_pats(char *pattern)
390 if (ignore_pats == NULL)
391 ignore_pats = xstrdup(pattern);
393 /* old + "|" + new + NUL */
394 len = strlen(ignore_pats) + strlen(pattern) + 2;
395 ignore_pats = xreallocarray(ignore_pats, 1, len);
396 strlcat(ignore_pats, "|", len);
397 strlcat(ignore_pats, pattern, len);
402 print_only(const char *path, size_t dirlen, const char *entry)
406 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
410 print_status(int val, char *path1, char *path2, const char *entry)
414 printf("Binary files %s%s and %s%s differ\n",
415 path1, entry, path2, entry);
418 if (diff_format == D_BRIEF)
419 printf("Files %s%s and %s%s differ\n",
420 path1, entry, path2, entry);
424 printf("Files %s%s and %s%s are identical\n",
425 path1, entry, path2, entry);
428 printf("File %s%s is a directory while file %s%s is a regular file\n",
429 path1, entry, path2, entry);
432 printf("File %s%s is a regular file while file %s%s is a directory\n",
433 path1, entry, path2, entry);
436 printf("File %s%s is not a regular file or directory and was skipped\n",
440 printf("File %s%s is not a regular file or directory and was skipped\n",
449 (void)fprintf(stderr,
450 "usage: diff [-abdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
451 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
452 " [-I pattern] [-L label] file1 file2\n"
453 " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
454 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
455 " -C number file1 file2\n"
456 " diff [-abdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
457 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
458 " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
459 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
460 " -U number file1 file2\n"
461 " diff [-abdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
462 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
463 " [-S name] [-X file] [-x pattern] dir1 dir2\n");