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