xref: /freebsd/usr.bin/diff/diff.c (revision 5f757f3f)
1 /*	$OpenBSD: diff.c,v 1.67 2019/06/28 13:35:00 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
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.
9  *
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.
17  *
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.
21  */
22 
23 #include <sys/stat.h>
24 
25 #include <ctype.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <limits.h>
34 
35 #include "diff.h"
36 #include "xmalloc.h"
37 
38 static const char diff_version[] = "FreeBSD diff 20240307";
39 bool	 lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag;
40 bool	 ignore_file_case, suppress_common, color, noderef;
41 static bool help = false;
42 int	 diff_format, diff_context, diff_algorithm, status;
43 bool	 diff_algorithm_set;
44 int	 tabsize = 8, width = 130;
45 static int	colorflag = COLORFLAG_NEVER;
46 char	*start, *ifdefname, *diffargs, *label[2];
47 char	*ignore_pats, *most_recent_pat;
48 char	*group_format = NULL;
49 const char	*add_code, *del_code;
50 struct stat stb1, stb2;
51 struct excludes *excludes_list;
52 regex_t	 ignore_re, most_recent_re;
53 
54 static struct algorithm {
55 	const char *name;
56 	int id;
57 } algorithms[] = {
58 	{"stone", D_DIFFSTONE},
59 	{"myers", D_DIFFMYERS},
60 	{"patience", D_DIFFPATIENCE},
61 	{NULL, D_DIFFNONE}
62 };
63 
64 #define	OPTIONS	"0123456789A:aBbC:cdD:efF:HhI:iL:lnNPpqrS:sTtU:uwW:X:x:y"
65 enum {
66 	OPT_TSIZE = CHAR_MAX + 1,
67 	OPT_STRIPCR,
68 	OPT_IGN_FN_CASE,
69 	OPT_NO_IGN_FN_CASE,
70 	OPT_NORMAL,
71 	OPT_HELP,
72 	OPT_HORIZON_LINES,
73 	OPT_CHANGED_GROUP_FORMAT,
74 	OPT_SUPPRESS_COMMON,
75 	OPT_COLOR,
76 	OPT_NO_DEREFERENCE,
77 	OPT_VERSION,
78 };
79 
80 static struct option longopts[] = {
81 	{ "algorithm",			required_argument,	0,	'A' },
82 	{ "text",			no_argument,		0,	'a' },
83 	{ "ignore-space-change",	no_argument,		0,	'b' },
84 	{ "context",			optional_argument,	0,	'C' },
85 	{ "ifdef",			required_argument,	0,	'D' },
86 	{ "minimal",			no_argument,		0,	'd' },
87 	{ "ed",				no_argument,		0,	'e' },
88 	{ "forward-ed",			no_argument,		0,	'f' },
89 	{ "show-function-line",		required_argument,	0,	'F' },
90 	{ "speed-large-files",		no_argument,		NULL,	'H' },
91 	{ "ignore-blank-lines",		no_argument,		0,	'B' },
92 	{ "ignore-matching-lines",	required_argument,	0,	'I' },
93 	{ "ignore-case",		no_argument,		0,	'i' },
94 	{ "paginate",			no_argument,		NULL,	'l' },
95 	{ "label",			required_argument,	0,	'L' },
96 	{ "new-file",			no_argument,		0,	'N' },
97 	{ "rcs",			no_argument,		0,	'n' },
98 	{ "unidirectional-new-file",	no_argument,		0,	'P' },
99 	{ "show-c-function",		no_argument,		0,	'p' },
100 	{ "brief",			no_argument,		0,	'q' },
101 	{ "recursive",			no_argument,		0,	'r' },
102 	{ "report-identical-files",	no_argument,		0,	's' },
103 	{ "starting-file",		required_argument,	0,	'S' },
104 	{ "expand-tabs",		no_argument,		0,	't' },
105 	{ "initial-tab",		no_argument,		0,	'T' },
106 	{ "unified",			optional_argument,	0,	'U' },
107 	{ "ignore-all-space",		no_argument,		0,	'w' },
108 	{ "width",			required_argument,	0,	'W' },
109 	{ "exclude",			required_argument,	0,	'x' },
110 	{ "exclude-from",		required_argument,	0,	'X' },
111 	{ "side-by-side",		no_argument,		NULL,	'y' },
112 	{ "ignore-file-name-case",	no_argument,		NULL,	OPT_IGN_FN_CASE },
113 	{ "help",			no_argument,		NULL,	OPT_HELP},
114 	{ "horizon-lines",		required_argument,	NULL,	OPT_HORIZON_LINES },
115 	{ "no-dereference",		no_argument,		NULL,	OPT_NO_DEREFERENCE},
116 	{ "no-ignore-file-name-case",	no_argument,		NULL,	OPT_NO_IGN_FN_CASE },
117 	{ "normal",			no_argument,		NULL,	OPT_NORMAL },
118 	{ "strip-trailing-cr",		no_argument,		NULL,	OPT_STRIPCR },
119 	{ "tabsize",			required_argument,	NULL,	OPT_TSIZE },
120 	{ "changed-group-format",	required_argument,	NULL,	OPT_CHANGED_GROUP_FORMAT},
121 	{ "suppress-common-lines",	no_argument,		NULL,	OPT_SUPPRESS_COMMON },
122 	{ "color",			optional_argument,	NULL,	OPT_COLOR },
123 	{ "version",			no_argument,		NULL,	OPT_VERSION},
124 	{ NULL,				0,			0,	'\0'}
125 };
126 
127 static void checked_regcomp(char const *, regex_t *);
128 static void usage(void) __dead2;
129 static void conflicting_format(void) __dead2;
130 static void push_excludes(char *);
131 static void push_ignore_pats(char *);
132 static void read_excludes_file(char *file);
133 static void set_argstr(char **, char **);
134 static char *splice(char *, char *);
135 static bool do_color(void);
136 
137 int
138 main(int argc, char **argv)
139 {
140 	const char *errstr = NULL;
141 	char *ep, **oargv;
142 	long  l;
143 	int   ch, dflags, lastch, gotstdin, prevoptind, newarg;
144 
145 	oargv = argv;
146 	gotstdin = 0;
147 	dflags = 0;
148 	lastch = '\0';
149 	prevoptind = 1;
150 	newarg = 1;
151 	diff_context = 3;
152 	diff_format = D_UNSET;
153 	diff_algorithm = D_DIFFMYERS;
154 	diff_algorithm_set = false;
155 #define	FORMAT_MISMATCHED(type)	\
156 	(diff_format != D_UNSET && diff_format != (type))
157 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
158 		switch (ch) {
159 		case '0': case '1': case '2': case '3': case '4':
160 		case '5': case '6': case '7': case '8': case '9':
161 			if (newarg)
162 				usage();	/* disallow -[0-9]+ */
163 			else if (lastch == 'c' || lastch == 'u')
164 				diff_context = 0;
165 			else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
166 				usage();
167 			diff_context = (diff_context * 10) + (ch - '0');
168 			break;
169 		case 'A':
170 			diff_algorithm = D_DIFFNONE;
171 			for (struct algorithm *a = algorithms; a->name;a++) {
172 				if(strcasecmp(optarg, a->name) == 0) {
173 					diff_algorithm = a->id;
174 					diff_algorithm_set = true;
175 					break;
176 				}
177 			}
178 
179 			if (diff_algorithm == D_DIFFNONE) {
180 				printf("unknown algorithm: %s\n", optarg);
181 				usage();
182 			}
183 			break;
184 		case 'a':
185 			dflags |= D_FORCEASCII;
186 			break;
187 		case 'b':
188 			dflags |= D_FOLDBLANKS;
189 			break;
190 		case 'C':
191 		case 'c':
192 			if (FORMAT_MISMATCHED(D_CONTEXT))
193 				conflicting_format();
194 			cflag = true;
195 			diff_format = D_CONTEXT;
196 			if (optarg != NULL) {
197 				l = strtol(optarg, &ep, 10);
198 				if (*ep != '\0' || l < 0 || l >= INT_MAX)
199 					usage();
200 				diff_context = (int)l;
201 			}
202 			break;
203 		case 'd':
204 			dflags |= D_MINIMAL;
205 			break;
206 		case 'D':
207 			if (FORMAT_MISMATCHED(D_IFDEF))
208 				conflicting_format();
209 			diff_format = D_IFDEF;
210 			ifdefname = optarg;
211 			break;
212 		case 'e':
213 			if (FORMAT_MISMATCHED(D_EDIT))
214 				conflicting_format();
215 			diff_format = D_EDIT;
216 			break;
217 		case 'f':
218 			if (FORMAT_MISMATCHED(D_REVERSE))
219 				conflicting_format();
220 			diff_format = D_REVERSE;
221 			break;
222 		case 'H':
223 			/* ignore but needed for compatibility with GNU diff */
224 			break;
225 		case 'h':
226 			/* silently ignore for backwards compatibility */
227 			break;
228 		case 'B':
229 			dflags |= D_SKIPBLANKLINES;
230 			break;
231 		case 'F':
232 			if (dflags & D_PROTOTYPE)
233 				conflicting_format();
234 			dflags |= D_MATCHLAST;
235 			most_recent_pat = xstrdup(optarg);
236 			break;
237 		case 'I':
238 			push_ignore_pats(optarg);
239 			break;
240 		case 'i':
241 			dflags |= D_IGNORECASE;
242 			break;
243 		case 'L':
244 			if (label[0] == NULL)
245 				label[0] = optarg;
246 			else if (label[1] == NULL)
247 				label[1] = optarg;
248 			else
249 				usage();
250 			break;
251 		case 'l':
252 			lflag = true;
253 			break;
254 		case 'N':
255 			Nflag = true;
256 			break;
257 		case 'n':
258 			if (FORMAT_MISMATCHED(D_NREVERSE))
259 				conflicting_format();
260 			diff_format = D_NREVERSE;
261 			break;
262 		case 'p':
263 			if (dflags & D_MATCHLAST)
264 				conflicting_format();
265 			dflags |= D_PROTOTYPE;
266 			break;
267 		case 'P':
268 			Pflag = true;
269 			break;
270 		case 'r':
271 			rflag = true;
272 			break;
273 		case 'q':
274 			if (FORMAT_MISMATCHED(D_BRIEF))
275 				conflicting_format();
276 			diff_format = D_BRIEF;
277 			break;
278 		case 'S':
279 			start = optarg;
280 			break;
281 		case 's':
282 			sflag = true;
283 			break;
284 		case 'T':
285 			Tflag = true;
286 			break;
287 		case 't':
288 			dflags |= D_EXPANDTABS;
289 			break;
290 		case 'U':
291 		case 'u':
292 			if (FORMAT_MISMATCHED(D_UNIFIED))
293 				conflicting_format();
294 			diff_format = D_UNIFIED;
295 			if (optarg != NULL) {
296 				l = strtol(optarg, &ep, 10);
297 				if (*ep != '\0' || l < 0 || l >= INT_MAX)
298 					usage();
299 				diff_context = (int)l;
300 			}
301 			break;
302 		case 'w':
303 			dflags |= D_IGNOREBLANKS;
304 			break;
305 		case 'W':
306 			width = (int) strtonum(optarg, 1, INT_MAX, &errstr);
307 			if (errstr) {
308 				warnx("Invalid argument for width");
309 				usage();
310 			}
311 			break;
312 		case 'X':
313 			read_excludes_file(optarg);
314 			break;
315 		case 'x':
316 			push_excludes(optarg);
317 			break;
318 		case 'y':
319 			if (FORMAT_MISMATCHED(D_SIDEBYSIDE))
320 				conflicting_format();
321 			diff_format = D_SIDEBYSIDE;
322 			break;
323 		case OPT_CHANGED_GROUP_FORMAT:
324 			if (FORMAT_MISMATCHED(D_GFORMAT))
325 				conflicting_format();
326 			diff_format = D_GFORMAT;
327 			group_format = optarg;
328 			break;
329 		case OPT_HELP:
330 			help = true;
331 			usage();
332 			break;
333 		case OPT_HORIZON_LINES:
334 			break; /* XXX TODO for compatibility with GNU diff3 */
335 		case OPT_IGN_FN_CASE:
336 			ignore_file_case = true;
337 			break;
338 		case OPT_NO_IGN_FN_CASE:
339 			ignore_file_case = false;
340 			break;
341 		case OPT_NORMAL:
342 			if (FORMAT_MISMATCHED(D_NORMAL))
343 				conflicting_format();
344 			diff_format = D_NORMAL;
345 			break;
346 		case OPT_TSIZE:
347 			tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
348 			if (errstr) {
349 				warnx("Invalid argument for tabsize");
350 				usage();
351 			}
352 			break;
353 		case OPT_STRIPCR:
354 			dflags |= D_STRIPCR;
355 			break;
356 		case OPT_SUPPRESS_COMMON:
357 			suppress_common = 1;
358 			break;
359 		case OPT_COLOR:
360 			if (optarg == NULL || strncmp(optarg, "auto", 4) == 0)
361 				colorflag = COLORFLAG_AUTO;
362 			else if (strncmp(optarg, "always", 6) == 0)
363 				colorflag = COLORFLAG_ALWAYS;
364 			else if (strncmp(optarg, "never", 5) == 0)
365 				colorflag = COLORFLAG_NEVER;
366 			else
367 				errx(2, "unsupported --color value '%s' (must be always, auto, or never)",
368 					optarg);
369 			break;
370 		case OPT_NO_DEREFERENCE:
371 			rflag = true;
372 			noderef = true;
373 			break;
374 		case OPT_VERSION:
375 			printf("%s\n", diff_version);
376 			exit(0);
377 		default:
378 			usage();
379 			break;
380 		}
381 		lastch = ch;
382 		newarg = optind != prevoptind;
383 		prevoptind = optind;
384 	}
385 	if (diff_format == D_UNSET && (dflags & D_PROTOTYPE) != 0)
386 		diff_format = D_CONTEXT;
387 	if (diff_format == D_UNSET)
388 		diff_format = D_NORMAL;
389 	argc -= optind;
390 	argv += optind;
391 
392 	if (do_color()) {
393 		char *p;
394 		const char *env;
395 
396 		color = true;
397 		add_code = "32";
398 		del_code = "31";
399 		env = getenv("DIFFCOLORS");
400 		if (env != NULL && *env != '\0' && (p = strdup(env))) {
401 			add_code = p;
402 			strsep(&p, ":");
403 			if (p != NULL)
404 				del_code = p;
405 		}
406 	}
407 
408 #ifdef __OpenBSD__
409 	if (pledge("stdio rpath tmppath", NULL) == -1)
410 		err(2, "pledge");
411 #endif
412 
413 	/*
414 	 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
415 	 * driver routine.  Both drivers use the contents of stb1 and stb2.
416 	 */
417 	if (argc != 2)
418 		usage();
419 	checked_regcomp(ignore_pats, &ignore_re);
420 	checked_regcomp(most_recent_pat, &most_recent_re);
421 	if (strcmp(argv[0], "-") == 0) {
422 		fstat(STDIN_FILENO, &stb1);
423 		gotstdin = 1;
424 	} else if (stat(argv[0], &stb1) != 0) {
425 		if (!Nflag || errno != ENOENT)
426 			err(2, "%s", argv[0]);
427 		dflags |= D_EMPTY1;
428 		memset(&stb1, 0, sizeof(struct stat));
429 	}
430 
431 	if (strcmp(argv[1], "-") == 0) {
432 		fstat(STDIN_FILENO, &stb2);
433 		gotstdin = 1;
434 	} else if (stat(argv[1], &stb2) != 0) {
435 		if (!Nflag || errno != ENOENT)
436 			err(2, "%s", argv[1]);
437 		dflags |= D_EMPTY2;
438 		memset(&stb2, 0, sizeof(stb2));
439 		stb2.st_mode = stb1.st_mode;
440 	}
441 
442 	if (dflags & D_EMPTY1 && dflags & D_EMPTY2){
443 		warn("%s", argv[0]);
444 		warn("%s", argv[1]);
445 		exit(2);
446 	}
447 
448 	if (stb1.st_mode == 0)
449 		stb1.st_mode = stb2.st_mode;
450 
451 	if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
452 		errx(2, "can't compare - to a directory");
453 	set_argstr(oargv, argv);
454 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
455 		if (diff_format == D_IFDEF)
456 			errx(2, "-D option not supported with directories");
457 		diffdir(argv[0], argv[1], dflags);
458 	} else {
459 		if (S_ISDIR(stb1.st_mode)) {
460 			argv[0] = splice(argv[0], argv[1]);
461 			if (stat(argv[0], &stb1) == -1)
462 				err(2, "%s", argv[0]);
463 		}
464 		if (S_ISDIR(stb2.st_mode)) {
465 			argv[1] = splice(argv[1], argv[0]);
466 			if (stat(argv[1], &stb2) == -1)
467 				err(2, "%s", argv[1]);
468 		}
469 		print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
470 		    argv[1], "");
471 	}
472 	if (fflush(stdout) != 0)
473 		err(2, "stdout");
474 	exit(status);
475 }
476 
477 static void
478 checked_regcomp(char const *pattern, regex_t *comp)
479 {
480 	char buf[BUFSIZ];
481 	int error;
482 
483 	if (pattern == NULL)
484 		return;
485 
486 	error = regcomp(comp, pattern, REG_NEWLINE | REG_EXTENDED);
487 	if (error != 0) {
488 		regerror(error, comp, buf, sizeof(buf));
489 		if (*pattern != '\0')
490 			errx(2, "%s: %s", pattern, buf);
491 		else
492 			errx(2, "%s", buf);
493 	}
494 }
495 
496 static void
497 set_argstr(char **av, char **ave)
498 {
499 	size_t argsize;
500 	char **ap;
501 
502 	argsize = 4 + *ave - *av + 1;
503 	diffargs = xmalloc(argsize);
504 	strlcpy(diffargs, "diff", argsize);
505 	for (ap = av + 1; ap < ave; ap++) {
506 		if (strcmp(*ap, "--") != 0) {
507 			strlcat(diffargs, " ", argsize);
508 			strlcat(diffargs, *ap, argsize);
509 		}
510 	}
511 }
512 
513 /*
514  * Read in an excludes file and push each line.
515  */
516 static void
517 read_excludes_file(char *file)
518 {
519 	FILE *fp;
520 	char *buf, *pattern;
521 	size_t len;
522 
523 	if (strcmp(file, "-") == 0)
524 		fp = stdin;
525 	else if ((fp = fopen(file, "r")) == NULL)
526 		err(2, "%s", file);
527 	while ((buf = fgetln(fp, &len)) != NULL) {
528 		if (buf[len - 1] == '\n')
529 			len--;
530 		if ((pattern = strndup(buf, len)) == NULL)
531 			err(2, "xstrndup");
532 		push_excludes(pattern);
533 	}
534 	if (strcmp(file, "-") != 0)
535 		fclose(fp);
536 }
537 
538 /*
539  * Push a pattern onto the excludes list.
540  */
541 static void
542 push_excludes(char *pattern)
543 {
544 	struct excludes *entry;
545 
546 	entry = xmalloc(sizeof(*entry));
547 	entry->pattern = pattern;
548 	entry->next = excludes_list;
549 	excludes_list = entry;
550 }
551 
552 static void
553 push_ignore_pats(char *pattern)
554 {
555 	size_t len;
556 
557 	if (ignore_pats == NULL)
558 		ignore_pats = xstrdup(pattern);
559 	else {
560 		/* old + "|" + new + NUL */
561 		len = strlen(ignore_pats) + strlen(pattern) + 2;
562 		ignore_pats = xreallocarray(ignore_pats, 1, len);
563 		strlcat(ignore_pats, "|", len);
564 		strlcat(ignore_pats, pattern, len);
565 	}
566 }
567 
568 void
569 print_status(int val, char *path1, char *path2, const char *entry)
570 {
571 	if (label[0] != NULL)
572 		path1 = label[0];
573 	if (label[1] != NULL)
574 		path2 = label[1];
575 
576 	switch (val) {
577 	case D_BINARY:
578 		printf("Binary files %s%s and %s%s differ\n",
579 		    path1, entry, path2, entry);
580 		break;
581 	case D_DIFFER:
582 		if (diff_format == D_BRIEF)
583 			printf("Files %s%s and %s%s differ\n",
584 			    path1, entry, path2, entry);
585 		break;
586 	case D_SAME:
587 		if (sflag)
588 			printf("Files %s%s and %s%s are identical\n",
589 			    path1, entry, path2, entry);
590 		break;
591 	case D_MISMATCH1:
592 		printf("File %s%s is a directory while file %s%s is a regular file\n",
593 		    path1, entry, path2, entry);
594 		break;
595 	case D_MISMATCH2:
596 		printf("File %s%s is a regular file while file %s%s is a directory\n",
597 		    path1, entry, path2, entry);
598 		break;
599 	case D_SKIPPED1:
600 		printf("File %s%s is not a regular file or directory and was skipped\n",
601 		    path1, entry);
602 		break;
603 	case D_SKIPPED2:
604 		printf("File %s%s is not a regular file or directory and was skipped\n",
605 		    path2, entry);
606 		break;
607 	case D_ERROR:
608 		break;
609 	}
610 }
611 
612 static void
613 usage(void)
614 {
615 	(void)fprintf(help ? stdout : stderr,
616 	    "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
617 	    "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
618 	    "            [-I pattern] [-F pattern] [-L label] file1 file2\n"
619 	    "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
620 	    "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
621 	    "            [-F pattern] -C number file1 file2\n"
622 	    "       diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
623 	    "            [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
624 	    "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
625 	    "            [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
626 	    "            [-F pattern] -U number file1 file2\n"
627 	    "       diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
628 	    "            [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
629 	    "            [-F pattern] [-S name] [-X file] [-x pattern] dir1 dir2\n"
630 	    "       diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
631 	    "            [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
632 	    "            [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
633 	    "            [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
634 	    "            -y | --side-by-side file1 file2\n"
635 	    "       diff [--help] [--version]\n");
636 
637 	if (help)
638 		exit(0);
639 	else
640 		exit(2);
641 }
642 
643 static void
644 conflicting_format(void)
645 {
646 
647 	fprintf(stderr, "error: conflicting output format options.\n");
648 	usage();
649 }
650 
651 static bool
652 do_color(void)
653 {
654 	const char *p, *p2;
655 
656 	switch (colorflag) {
657 	case COLORFLAG_AUTO:
658 		p = getenv("CLICOLOR");
659 		p2 = getenv("COLORTERM");
660 		if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0'))
661 			return isatty(STDOUT_FILENO);
662 		break;
663 	case COLORFLAG_ALWAYS:
664 		return (true);
665 	case COLORFLAG_NEVER:
666 		return (false);
667 	}
668 
669 	return (false);
670 }
671 
672 static char *
673 splice(char *dir, char *path)
674 {
675 	char *tail, *buf;
676 	size_t dirlen;
677 
678 	dirlen = strlen(dir);
679 	while (dirlen != 0 && dir[dirlen - 1] == '/')
680 	    dirlen--;
681 	if ((tail = strrchr(path, '/')) == NULL)
682 		tail = path;
683 	else
684 		tail++;
685 	xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
686 	return (buf);
687 }
688