]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/diff/diff.c
ar(1): Fix grammar error in write.c
[FreeBSD/FreeBSD.git] / usr.bin / diff / diff.c
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 #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
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                                 warnx("Invalid argument for width");
281                                 usage();
282                         }
283                         break;
284                 case 'X':
285                         read_excludes_file(optarg);
286                         break;
287                 case 'x':
288                         push_excludes(optarg);
289                         break;
290                 case 'y':
291                         if (FORMAT_MISMATCHED(D_SIDEBYSIDE))
292                                 conflicting_format();
293                         diff_format = D_SIDEBYSIDE;
294                         break;
295                 case OPT_CHANGED_GROUP_FORMAT:
296                         if (FORMAT_MISMATCHED(D_GFORMAT))
297                                 conflicting_format();
298                         diff_format = D_GFORMAT;
299                         group_format = optarg;
300                         break;
301                 case OPT_HELP:
302                         help = true;
303                         usage();
304                         break;
305                 case OPT_HORIZON_LINES:
306                         break; /* XXX TODO for compatibility with GNU diff3 */
307                 case OPT_IGN_FN_CASE:
308                         ignore_file_case = true;
309                         break;
310                 case OPT_NO_IGN_FN_CASE:
311                         ignore_file_case = false;
312                         break;
313                 case OPT_NORMAL:
314                         if (FORMAT_MISMATCHED(D_NORMAL))
315                                 conflicting_format();
316                         diff_format = D_NORMAL;
317                         break;
318                 case OPT_TSIZE:
319                         tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
320                         if (errstr) {
321                                 warnx("Invalid argument for tabsize");
322                                 usage();
323                         }
324                         break;
325                 case OPT_STRIPCR:
326                         dflags |= D_STRIPCR;
327                         break;
328                 case OPT_SUPPRESS_COMMON:
329                         suppress_common = 1;
330                         break;
331                 case OPT_COLOR:
332                         if (optarg == NULL || strncmp(optarg, "auto", 4) == 0)
333                                 colorflag = COLORFLAG_AUTO;
334                         else if (strncmp(optarg, "always", 6) == 0)
335                                 colorflag = COLORFLAG_ALWAYS;
336                         else if (strncmp(optarg, "never", 5) == 0)
337                                 colorflag = COLORFLAG_NEVER;
338                         else
339                                 errx(2, "unsupported --color value '%s' (must be always, auto, or never)",
340                                         optarg);
341                         break;
342                 case OPT_NO_DEREFERENCE:
343                         rflag = true;
344                         noderef = true;
345                         break;
346                 case OPT_VERSION:
347                         printf("%s\n", diff_version);
348                         exit(0);
349                 default:
350                         usage();
351                         break;
352                 }
353                 lastch = ch;
354                 newarg = optind != prevoptind;
355                 prevoptind = optind;
356         }
357         if (diff_format == D_UNSET && (dflags & D_PROTOTYPE) != 0)
358                 diff_format = D_CONTEXT;
359         if (diff_format == D_UNSET)
360                 diff_format = D_NORMAL;
361         argc -= optind;
362         argv += optind;
363
364         if (do_color()) {
365                 char *p;
366                 const char *env;
367
368                 color = true;
369                 add_code = "32";
370                 del_code = "31";
371                 env = getenv("DIFFCOLORS");
372                 if (env != NULL && *env != '\0' && (p = strdup(env))) {
373                         add_code = p;
374                         strsep(&p, ":");
375                         if (p != NULL)
376                                 del_code = p;
377                 }
378         }
379
380 #ifdef __OpenBSD__
381         if (pledge("stdio rpath tmppath", NULL) == -1)
382                 err(2, "pledge");
383 #endif
384
385         /*
386          * Do sanity checks, fill in stb1 and stb2 and call the appropriate
387          * driver routine.  Both drivers use the contents of stb1 and stb2.
388          */
389         if (argc != 2)
390                 usage();
391         checked_regcomp(ignore_pats, &ignore_re);
392         checked_regcomp(most_recent_pat, &most_recent_re);
393         if (strcmp(argv[0], "-") == 0) {
394                 fstat(STDIN_FILENO, &stb1);
395                 gotstdin = 1;
396         } else if (stat(argv[0], &stb1) != 0) {
397                 if (!Nflag || errno != ENOENT)
398                         err(2, "%s", argv[0]);
399                 dflags |= D_EMPTY1;
400                 memset(&stb1, 0, sizeof(struct stat));
401         }
402
403         if (strcmp(argv[1], "-") == 0) {
404                 fstat(STDIN_FILENO, &stb2);
405                 gotstdin = 1;
406         } else if (stat(argv[1], &stb2) != 0) {
407                 if (!Nflag || errno != ENOENT)
408                         err(2, "%s", argv[1]);
409                 dflags |= D_EMPTY2;
410                 memset(&stb2, 0, sizeof(stb2));
411                 stb2.st_mode = stb1.st_mode;
412         }
413
414         if (dflags & D_EMPTY1 && dflags & D_EMPTY2){
415                 warn("%s", argv[0]);
416                 warn("%s", argv[1]);
417                 exit(2);
418         }
419
420         if (stb1.st_mode == 0)
421                 stb1.st_mode = stb2.st_mode;
422
423         if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
424                 errx(2, "can't compare - to a directory");
425         set_argstr(oargv, argv);
426         if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
427                 if (diff_format == D_IFDEF)
428                         errx(2, "-D option not supported with directories");
429                 diffdir(argv[0], argv[1], dflags);
430         } else {
431                 if (S_ISDIR(stb1.st_mode)) {
432                         argv[0] = splice(argv[0], argv[1]);
433                         if (stat(argv[0], &stb1) == -1)
434                                 err(2, "%s", argv[0]);
435                 }
436                 if (S_ISDIR(stb2.st_mode)) {
437                         argv[1] = splice(argv[1], argv[0]);
438                         if (stat(argv[1], &stb2) == -1)
439                                 err(2, "%s", argv[1]);
440                 }
441                 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
442                     argv[1], "");
443         }
444         exit(status);
445 }
446
447 static void
448 checked_regcomp(char const *pattern, regex_t *comp)
449 {
450         char buf[BUFSIZ];
451         int error;
452
453         if (pattern == NULL)
454                 return;
455
456         error = regcomp(comp, pattern, REG_NEWLINE | REG_EXTENDED);
457         if (error != 0) {
458                 regerror(error, comp, buf, sizeof(buf));
459                 if (*pattern != '\0')
460                         errx(2, "%s: %s", pattern, buf);
461                 else
462                         errx(2, "%s", buf);
463         }
464 }
465
466 static void
467 set_argstr(char **av, char **ave)
468 {
469         size_t argsize;
470         char **ap;
471
472         argsize = 4 + *ave - *av + 1;
473         diffargs = xmalloc(argsize);
474         strlcpy(diffargs, "diff", argsize);
475         for (ap = av + 1; ap < ave; ap++) {
476                 if (strcmp(*ap, "--") != 0) {
477                         strlcat(diffargs, " ", argsize);
478                         strlcat(diffargs, *ap, argsize);
479                 }
480         }
481 }
482
483 /*
484  * Read in an excludes file and push each line.
485  */
486 static void
487 read_excludes_file(char *file)
488 {
489         FILE *fp;
490         char *buf, *pattern;
491         size_t len;
492
493         if (strcmp(file, "-") == 0)
494                 fp = stdin;
495         else if ((fp = fopen(file, "r")) == NULL)
496                 err(2, "%s", file);
497         while ((buf = fgetln(fp, &len)) != NULL) {
498                 if (buf[len - 1] == '\n')
499                         len--;
500                 if ((pattern = strndup(buf, len)) == NULL)
501                         err(2, "xstrndup");
502                 push_excludes(pattern);
503         }
504         if (strcmp(file, "-") != 0)
505                 fclose(fp);
506 }
507
508 /*
509  * Push a pattern onto the excludes list.
510  */
511 static void
512 push_excludes(char *pattern)
513 {
514         struct excludes *entry;
515
516         entry = xmalloc(sizeof(*entry));
517         entry->pattern = pattern;
518         entry->next = excludes_list;
519         excludes_list = entry;
520 }
521
522 static void
523 push_ignore_pats(char *pattern)
524 {
525         size_t len;
526
527         if (ignore_pats == NULL)
528                 ignore_pats = xstrdup(pattern);
529         else {
530                 /* old + "|" + new + NUL */
531                 len = strlen(ignore_pats) + strlen(pattern) + 2;
532                 ignore_pats = xreallocarray(ignore_pats, 1, len);
533                 strlcat(ignore_pats, "|", len);
534                 strlcat(ignore_pats, pattern, len);
535         }
536 }
537
538 void
539 print_status(int val, char *path1, char *path2, const char *entry)
540 {
541         if (label[0] != NULL)
542                 path1 = label[0];
543         if (label[1] != NULL)
544                 path2 = label[1];
545
546         switch (val) {
547         case D_BINARY:
548                 printf("Binary files %s%s and %s%s differ\n",
549                     path1, entry, path2, entry);
550                 break;
551         case D_DIFFER:
552                 if (diff_format == D_BRIEF)
553                         printf("Files %s%s and %s%s differ\n",
554                             path1, entry, path2, entry);
555                 break;
556         case D_SAME:
557                 if (sflag)
558                         printf("Files %s%s and %s%s are identical\n",
559                             path1, entry, path2, entry);
560                 break;
561         case D_MISMATCH1:
562                 printf("File %s%s is a directory while file %s%s is a regular file\n",
563                     path1, entry, path2, entry);
564                 break;
565         case D_MISMATCH2:
566                 printf("File %s%s is a regular file while file %s%s is a directory\n",
567                     path1, entry, path2, entry);
568                 break;
569         case D_SKIPPED1:
570                 printf("File %s%s is not a regular file or directory and was skipped\n",
571                     path1, entry);
572                 break;
573         case D_SKIPPED2:
574                 printf("File %s%s is not a regular file or directory and was skipped\n",
575                     path2, entry);
576                 break;
577         case D_ERROR:
578                 break;
579         }
580 }
581
582 static void
583 usage(void)
584 {
585         (void)fprintf(help ? stdout : stderr,
586             "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
587             "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
588             "            [-I pattern] [-F pattern] [-L label] file1 file2\n"
589             "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
590             "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
591             "            [-F pattern] -C number file1 file2\n"
592             "       diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
593             "            [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
594             "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
595             "            [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
596             "            [-F pattern] -U number file1 file2\n"
597             "       diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
598             "            [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
599             "            [-F pattern] [-S name] [-X file] [-x pattern] dir1 dir2\n"
600             "       diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
601             "            [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
602             "            [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
603             "            [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
604             "            -y | --side-by-side file1 file2\n"
605             "       diff [--help] [--version]\n");
606
607         if (help)
608                 exit(0);
609         else
610                 exit(2);
611 }
612
613 static void
614 conflicting_format(void)
615 {
616
617         fprintf(stderr, "error: conflicting output format options.\n");
618         usage();
619 }
620
621 static bool
622 do_color(void)
623 {
624         const char *p, *p2;
625
626         switch (colorflag) {
627         case COLORFLAG_AUTO:
628                 p = getenv("CLICOLOR");
629                 p2 = getenv("COLORTERM");
630                 if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0'))
631                         return isatty(STDOUT_FILENO);
632                 break;
633         case COLORFLAG_ALWAYS:
634                 return (true);
635         case COLORFLAG_NEVER:
636                 return (false);
637         }
638
639         return (false);
640 }
641
642 static char *
643 splice(char *dir, char *path)
644 {
645         char *tail, *buf;
646         size_t dirlen;
647
648         dirlen = strlen(dir);
649         while (dirlen != 0 && dir[dirlen - 1] == '/')
650             dirlen--;
651         if ((tail = strrchr(path, '/')) == NULL)
652                 tail = path;
653         else
654                 tail++;
655         xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
656         return (buf);
657 }