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 <limits.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "diff.h"
36#include "xmalloc.h"
37
38static const char diff_version[] = "FreeBSD diff 20240307";
39bool	 lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag;
40bool	 ignore_file_case, suppress_common, color, noderef;
41static bool help = false;
42int	 diff_format, diff_context, diff_algorithm, status;
43bool	 diff_algorithm_set;
44int	 tabsize = 8, width = 130;
45static int	colorflag = COLORFLAG_NEVER;
46char	*start, *ifdefname, *diffargs, *label[2];
47char	*ignore_pats, *most_recent_pat;
48char	*group_format = NULL;
49const char	*add_code, *del_code;
50struct stat stb1, stb2;
51struct excludes *excludes_list;
52regex_t	 ignore_re, most_recent_re;
53
54static 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"
65enum {
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
80static 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
127static void checked_regcomp(char const *, regex_t *);
128static void usage(void) __dead2;
129static void conflicting_format(void) __dead2;
130static void push_excludes(char *);
131static void push_ignore_pats(char *);
132static void read_excludes_file(char *file);
133static void set_argstr(char **, char **);
134static char *splice(char *, char *);
135static bool do_color(void);
136
137int
138main(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
477static void
478checked_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
496static void
497set_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 */
516static void
517read_excludes_file(char *file)
518{
519	FILE *fp;
520	char *pattern = NULL;
521	size_t blen = 0;
522	ssize_t len;
523
524	if (strcmp(file, "-") == 0)
525		fp = stdin;
526	else if ((fp = fopen(file, "r")) == NULL)
527		err(2, "%s", file);
528	while ((len = getline(&pattern, &blen, fp)) >= 0) {
529		if ((len > 0) && (pattern[len - 1] == '\n'))
530			pattern[len - 1] = '\0';
531		push_excludes(pattern);
532		/* we allocate a new string per line */
533		pattern = NULL;
534		blen = 0;
535	}
536	free(pattern);
537	if (strcmp(file, "-") != 0)
538		fclose(fp);
539}
540
541/*
542 * Push a pattern onto the excludes list.
543 */
544static void
545push_excludes(char *pattern)
546{
547	struct excludes *entry;
548
549	entry = xmalloc(sizeof(*entry));
550	entry->pattern = pattern;
551	entry->next = excludes_list;
552	excludes_list = entry;
553}
554
555static void
556push_ignore_pats(char *pattern)
557{
558	size_t len;
559
560	if (ignore_pats == NULL)
561		ignore_pats = xstrdup(pattern);
562	else {
563		/* old + "|" + new + NUL */
564		len = strlen(ignore_pats) + strlen(pattern) + 2;
565		ignore_pats = xreallocarray(ignore_pats, 1, len);
566		strlcat(ignore_pats, "|", len);
567		strlcat(ignore_pats, pattern, len);
568	}
569}
570
571void
572print_status(int val, char *path1, char *path2, const char *entry)
573{
574	if (label[0] != NULL)
575		path1 = label[0];
576	if (label[1] != NULL)
577		path2 = label[1];
578
579	switch (val) {
580	case D_BINARY:
581		printf("Binary files %s%s and %s%s differ\n",
582		    path1, entry, path2, entry);
583		break;
584	case D_DIFFER:
585		if (diff_format == D_BRIEF)
586			printf("Files %s%s and %s%s differ\n",
587			    path1, entry, path2, entry);
588		break;
589	case D_SAME:
590		if (sflag)
591			printf("Files %s%s and %s%s are identical\n",
592			    path1, entry, path2, entry);
593		break;
594	case D_MISMATCH1:
595		printf("File %s%s is a directory while file %s%s is a regular file\n",
596		    path1, entry, path2, entry);
597		break;
598	case D_MISMATCH2:
599		printf("File %s%s is a regular file while file %s%s is a directory\n",
600		    path1, entry, path2, entry);
601		break;
602	case D_SKIPPED1:
603		printf("File %s%s is not a regular file or directory and was skipped\n",
604		    path1, entry);
605		break;
606	case D_SKIPPED2:
607		printf("File %s%s is not a regular file or directory and was skipped\n",
608		    path2, entry);
609		break;
610	case D_ERROR:
611		break;
612	}
613}
614
615static void
616usage(void)
617{
618	(void)fprintf(help ? stdout : stderr,
619	    "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
620	    "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
621	    "            [-I pattern] [-F pattern] [-L label] file1 file2\n"
622	    "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
623	    "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
624	    "            [-F pattern] -C number file1 file2\n"
625	    "       diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
626	    "            [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
627	    "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
628	    "            [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
629	    "            [-F pattern] -U number file1 file2\n"
630	    "       diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
631	    "            [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
632	    "            [-F pattern] [-S name] [-X file] [-x pattern] dir1 dir2\n"
633	    "       diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
634	    "            [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
635	    "            [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
636	    "            [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
637	    "            -y | --side-by-side file1 file2\n"
638	    "       diff [--help] [--version]\n");
639
640	if (help)
641		exit(0);
642	else
643		exit(2);
644}
645
646static void
647conflicting_format(void)
648{
649
650	fprintf(stderr, "error: conflicting output format options.\n");
651	usage();
652}
653
654static bool
655do_color(void)
656{
657	const char *p, *p2;
658
659	switch (colorflag) {
660	case COLORFLAG_AUTO:
661		p = getenv("CLICOLOR");
662		p2 = getenv("COLORTERM");
663		if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0'))
664			return isatty(STDOUT_FILENO);
665		break;
666	case COLORFLAG_ALWAYS:
667		return (true);
668	case COLORFLAG_NEVER:
669		return (false);
670	}
671
672	return (false);
673}
674
675static char *
676splice(char *dir, char *path)
677{
678	char *tail, *buf;
679	size_t dirlen;
680
681	dirlen = strlen(dir);
682	while (dirlen != 0 && dir[dirlen - 1] == '/')
683	    dirlen--;
684	if ((tail = strrchr(path, '/')) == NULL)
685		tail = path;
686	else
687		tail++;
688	xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
689	return (buf);
690}
691