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