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 38 static const char diff_version[] = "FreeBSD diff 20240307"; 39 bool lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag; 40 bool ignore_file_case, suppress_common, color, noderef; 41 static bool help = false; 42 int diff_format, diff_context, diff_algorithm, status; 43 bool diff_algorithm_set; 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 static 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" 65 enum { 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 80 static 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 127 static void checked_regcomp(char const *, regex_t *); 128 static void usage(void) __dead2; 129 static void conflicting_format(void) __dead2; 130 static void push_excludes(char *); 131 static void push_ignore_pats(char *); 132 static void read_excludes_file(char *file); 133 static void set_argstr(char **, char **); 134 static char *splice(char *, char *); 135 static bool do_color(void); 136 137 int 138 main(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 noderef = true; 372 break; 373 case OPT_VERSION: 374 printf("%s\n", diff_version); 375 exit(0); 376 default: 377 usage(); 378 break; 379 } 380 lastch = ch; 381 newarg = optind != prevoptind; 382 prevoptind = optind; 383 } 384 if (diff_format == D_UNSET && (dflags & D_PROTOTYPE) != 0) 385 diff_format = D_CONTEXT; 386 if (diff_format == D_UNSET) 387 diff_format = D_NORMAL; 388 argc -= optind; 389 argv += optind; 390 391 if (do_color()) { 392 char *p; 393 const char *env; 394 395 color = true; 396 add_code = "32"; 397 del_code = "31"; 398 env = getenv("DIFFCOLORS"); 399 if (env != NULL && *env != '\0' && (p = strdup(env))) { 400 add_code = p; 401 strsep(&p, ":"); 402 if (p != NULL) 403 del_code = p; 404 } 405 } 406 407 #ifdef __OpenBSD__ 408 if (pledge("stdio rpath tmppath", NULL) == -1) 409 err(2, "pledge"); 410 #endif 411 412 /* 413 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 414 * driver routine. Both drivers use the contents of stb1 and stb2. 415 */ 416 if (argc != 2) 417 usage(); 418 checked_regcomp(ignore_pats, &ignore_re); 419 checked_regcomp(most_recent_pat, &most_recent_re); 420 if (strcmp(argv[0], "-") == 0) { 421 fstat(STDIN_FILENO, &stb1); 422 gotstdin = 1; 423 } else if (stat(argv[0], &stb1) != 0) { 424 if (!Nflag || errno != ENOENT) 425 err(2, "%s", argv[0]); 426 dflags |= D_EMPTY1; 427 memset(&stb1, 0, sizeof(struct stat)); 428 } 429 430 if (strcmp(argv[1], "-") == 0) { 431 fstat(STDIN_FILENO, &stb2); 432 gotstdin = 1; 433 } else if (stat(argv[1], &stb2) != 0) { 434 if (!Nflag || errno != ENOENT) 435 err(2, "%s", argv[1]); 436 dflags |= D_EMPTY2; 437 memset(&stb2, 0, sizeof(stb2)); 438 stb2.st_mode = stb1.st_mode; 439 } 440 441 if (dflags & D_EMPTY1 && dflags & D_EMPTY2){ 442 warn("%s", argv[0]); 443 warn("%s", argv[1]); 444 exit(2); 445 } 446 447 if (stb1.st_mode == 0) 448 stb1.st_mode = stb2.st_mode; 449 450 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 451 errx(2, "can't compare - to a directory"); 452 set_argstr(oargv, argv); 453 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 454 if (diff_format == D_IFDEF) 455 errx(2, "-D option not supported with directories"); 456 diffdir(argv[0], argv[1], dflags); 457 } else { 458 if (S_ISDIR(stb1.st_mode)) { 459 argv[0] = splice(argv[0], argv[1]); 460 if (stat(argv[0], &stb1) == -1) 461 err(2, "%s", argv[0]); 462 } 463 if (S_ISDIR(stb2.st_mode)) { 464 argv[1] = splice(argv[1], argv[0]); 465 if (stat(argv[1], &stb2) == -1) 466 err(2, "%s", argv[1]); 467 } 468 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0], 469 argv[1], ""); 470 } 471 if (fflush(stdout) != 0) 472 err(2, "stdout"); 473 exit(status); 474 } 475 476 static void 477 checked_regcomp(char const *pattern, regex_t *comp) 478 { 479 char buf[BUFSIZ]; 480 int error; 481 482 if (pattern == NULL) 483 return; 484 485 error = regcomp(comp, pattern, REG_NEWLINE | REG_EXTENDED); 486 if (error != 0) { 487 regerror(error, comp, buf, sizeof(buf)); 488 if (*pattern != '\0') 489 errx(2, "%s: %s", pattern, buf); 490 else 491 errx(2, "%s", buf); 492 } 493 } 494 495 static void 496 set_argstr(char **av, char **ave) 497 { 498 size_t argsize; 499 char **ap; 500 501 argsize = 4 + *ave - *av + 1; 502 diffargs = xmalloc(argsize); 503 strlcpy(diffargs, "diff", argsize); 504 for (ap = av + 1; ap < ave; ap++) { 505 if (strcmp(*ap, "--") != 0) { 506 strlcat(diffargs, " ", argsize); 507 strlcat(diffargs, *ap, argsize); 508 } 509 } 510 } 511 512 /* 513 * Read in an excludes file and push each line. 514 */ 515 static void 516 read_excludes_file(char *file) 517 { 518 FILE *fp; 519 char *pattern = NULL; 520 size_t blen = 0; 521 ssize_t len; 522 523 if (strcmp(file, "-") == 0) 524 fp = stdin; 525 else if ((fp = fopen(file, "r")) == NULL) 526 err(2, "%s", file); 527 while ((len = getline(&pattern, &blen, fp)) >= 0) { 528 if ((len > 0) && (pattern[len - 1] == '\n')) 529 pattern[len - 1] = '\0'; 530 push_excludes(pattern); 531 /* we allocate a new string per line */ 532 pattern = NULL; 533 blen = 0; 534 } 535 free(pattern); 536 if (strcmp(file, "-") != 0) 537 fclose(fp); 538 } 539 540 /* 541 * Push a pattern onto the excludes list. 542 */ 543 static void 544 push_excludes(char *pattern) 545 { 546 struct excludes *entry; 547 548 entry = xmalloc(sizeof(*entry)); 549 entry->pattern = pattern; 550 entry->next = excludes_list; 551 excludes_list = entry; 552 } 553 554 static void 555 push_ignore_pats(char *pattern) 556 { 557 size_t len; 558 559 if (ignore_pats == NULL) 560 ignore_pats = xstrdup(pattern); 561 else { 562 /* old + "|" + new + NUL */ 563 len = strlen(ignore_pats) + strlen(pattern) + 2; 564 ignore_pats = xreallocarray(ignore_pats, 1, len); 565 strlcat(ignore_pats, "|", len); 566 strlcat(ignore_pats, pattern, len); 567 } 568 } 569 570 void 571 print_status(int val, char *path1, char *path2, const char *entry) 572 { 573 if (label[0] != NULL) 574 path1 = label[0]; 575 if (label[1] != NULL) 576 path2 = label[1]; 577 578 switch (val) { 579 case D_BINARY: 580 printf("Binary files %s%s and %s%s differ\n", 581 path1, entry, path2, entry); 582 break; 583 case D_DIFFER: 584 if (diff_format == D_BRIEF) 585 printf("Files %s%s and %s%s differ\n", 586 path1, entry, path2, entry); 587 break; 588 case D_SAME: 589 if (sflag) 590 printf("Files %s%s and %s%s are identical\n", 591 path1, entry, path2, entry); 592 break; 593 case D_MISMATCH1: 594 printf("File %s%s is a directory while file %s%s is a regular file\n", 595 path1, entry, path2, entry); 596 break; 597 case D_MISMATCH2: 598 printf("File %s%s is a regular file while file %s%s is a directory\n", 599 path1, entry, path2, entry); 600 break; 601 case D_SKIPPED1: 602 printf("File %s%s is not a regular file or directory and was skipped\n", 603 path1, entry); 604 break; 605 case D_SKIPPED2: 606 printf("File %s%s is not a regular file or directory and was skipped\n", 607 path2, entry); 608 break; 609 case D_ERROR: 610 break; 611 } 612 } 613 614 static void 615 usage(void) 616 { 617 (void)fprintf(help ? stdout : stderr, 618 "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 619 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 620 " [-I pattern] [-F pattern] [-L label] file1 file2\n" 621 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 622 " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" 623 " [-F pattern] -C number file1 file2\n" 624 " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" 625 " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n" 626 " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" 627 " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n" 628 " [-F pattern] -U number file1 file2\n" 629 " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" 630 " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n" 631 " [-F pattern] [-S name] [-X file] [-x pattern] dir1 dir2\n" 632 " diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n" 633 " [--ignore-blank-lines] [--ignore-case] [--minimal]\n" 634 " [--no-ignore-file-name-case] [--strip-trailing-cr]\n" 635 " [--suppress-common-lines] [--tabsize] [--text] [--width]\n" 636 " -y | --side-by-side file1 file2\n" 637 " diff [--help] [--version]\n"); 638 639 if (help) 640 exit(0); 641 else 642 exit(2); 643 } 644 645 static void 646 conflicting_format(void) 647 { 648 649 fprintf(stderr, "error: conflicting output format options.\n"); 650 usage(); 651 } 652 653 static bool 654 do_color(void) 655 { 656 const char *p, *p2; 657 658 switch (colorflag) { 659 case COLORFLAG_AUTO: 660 p = getenv("CLICOLOR"); 661 p2 = getenv("COLORTERM"); 662 if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0')) 663 return isatty(STDOUT_FILENO); 664 break; 665 case COLORFLAG_ALWAYS: 666 return (true); 667 case COLORFLAG_NEVER: 668 return (false); 669 } 670 671 return (false); 672 } 673 674 static char * 675 splice(char *dir, char *path) 676 { 677 char *tail, *buf; 678 size_t dirlen; 679 680 dirlen = strlen(dir); 681 while (dirlen != 0 && dir[dirlen - 1] == '/') 682 dirlen--; 683 if ((tail = strrchr(path, '/')) == NULL) 684 tail = path; 685 else 686 tail++; 687 xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail); 688 return (buf); 689 } 690