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