1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)fts.c 5.36 (Berkeley) 03/30/92"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #include <dirent.h> 16 #include <errno.h> 17 #include <fts.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <unistd.h> 21 22 static FTSENT *fts_alloc __P((FTS *, char *, int)); 23 static FTSENT *fts_build __P((FTS *, int)); 24 static void fts_lfree __P((FTSENT *)); 25 static void fts_load __P((FTS *, FTSENT *)); 26 static void fts_padjust __P((FTS *, void *)); 27 static int fts_palloc __P((FTS *, int)); 28 static FTSENT *fts_sort __P((FTS *, FTSENT *, int)); 29 static u_short fts_stat __P((FTS *, FTSENT *, int)); 30 31 #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2])) 32 33 #define ISSET(opt) (sp->fts_options & opt) 34 #define SET(opt) (sp->fts_options |= opt) 35 36 #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 37 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 38 39 /* fts_build flags */ 40 #define BCHILD 1 /* fts_children */ 41 #define BNAMES 2 /* fts_children, names only */ 42 #define BREAD 3 /* fts_read */ 43 44 FTS * 45 fts_open(argv, options, compar) 46 char * const *argv; 47 register int options; 48 int (*compar)(); 49 { 50 register FTS *sp; 51 register FTSENT *p, *root; 52 register int nitems, maxlen; 53 FTSENT *parent, *tmp; 54 int len; 55 56 /* Options check. */ 57 if (options & ~FTS_OPTIONMASK) { 58 errno = EINVAL; 59 return (NULL); 60 } 61 62 /* Allocate/initialize the stream */ 63 if ((sp = malloc((u_int)sizeof(FTS))) == NULL) 64 return (NULL); 65 bzero(sp, sizeof(FTS)); 66 sp->fts_compar = compar; 67 sp->fts_options = options; 68 69 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 70 if (ISSET(FTS_LOGICAL)) 71 SET(FTS_NOCHDIR); 72 73 /* Allocate/initialize root's parent. */ 74 if ((parent = fts_alloc(sp, "", 0)) == NULL) 75 goto mem1; 76 parent->fts_level = FTS_ROOTPARENTLEVEL; 77 78 /* Allocate/initialize root(s). */ 79 for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) { 80 /* Don't allow zero-length paths. */ 81 if ((len = strlen(*argv)) == 0) { 82 errno = ENOENT; 83 goto mem2; 84 } 85 if (maxlen < len) 86 maxlen = len; 87 88 p = fts_alloc(sp, *argv, len); 89 p->fts_level = FTS_ROOTLEVEL; 90 p->fts_parent = parent; 91 p->fts_accpath = p->fts_name; 92 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); 93 94 /* Command-line "." and ".." are real directories. */ 95 if (p->fts_info == FTS_DOT) 96 p->fts_info = FTS_D; 97 98 /* 99 * If comparison routine supplied, traverse in sorted 100 * order; otherwise traverse in the order specified. 101 */ 102 if (compar) { 103 p->fts_link = root; 104 root = p; 105 } else { 106 p->fts_link = NULL; 107 if (root == NULL) 108 tmp = root = p; 109 else { 110 tmp->fts_link = p; 111 tmp = p; 112 } 113 } 114 } 115 if (compar && nitems > 1) 116 root = fts_sort(sp, root, nitems); 117 118 /* 119 * Allocate a dummy pointer and make fts_read think that we've just 120 * finished the node before the root(s); set p->fts_info to FTS_INIT 121 * so that everything about the "current" node is ignored. 122 */ 123 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) 124 goto mem2; 125 sp->fts_cur->fts_link = root; 126 sp->fts_cur->fts_info = FTS_INIT; 127 128 /* 129 * Start out with more than 1K of path space, and enough, in any 130 * case, to hold the user's paths. 131 */ 132 if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN))) 133 goto mem3; 134 135 /* 136 * If using chdir(2), grab a file descriptor pointing to dot to insure 137 * that we can get back here; this could be avoided for some paths, 138 * but almost certainly not worth the effort. Slashes, symbolic links, 139 * and ".." are all fairly nasty problems. Note, if we can't get the 140 * descriptor we run anyway, just more slowly. 141 */ 142 if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) 143 SET(FTS_NOCHDIR); 144 145 return (sp); 146 147 mem3: free(sp->fts_cur); 148 mem2: fts_lfree(root); 149 free(parent); 150 mem1: free(sp); 151 return (NULL); 152 } 153 154 static void 155 fts_load(sp, p) 156 FTS *sp; 157 register FTSENT *p; 158 { 159 register int len; 160 register char *cp; 161 162 /* 163 * Load the stream structure for the next traversal. Since we don't 164 * actually enter the directory until after the preorder visit, set 165 * the fts_accpath field specially so the chdir gets done to the right 166 * place and the user can access the first node. From fts_open it's 167 * known that the path will fit. 168 */ 169 len = p->fts_pathlen = p->fts_namelen; 170 bcopy(p->fts_name, sp->fts_path, len + 1); 171 if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 172 len = strlen(++cp); 173 bcopy(cp, p->fts_name, len + 1); 174 p->fts_namelen = len; 175 } 176 p->fts_accpath = p->fts_path = sp->fts_path; 177 sp->fts_dev = p->fts_dev; 178 } 179 180 int 181 fts_close(sp) 182 FTS *sp; 183 { 184 register FTSENT *freep, *p; 185 int saved_errno; 186 187 /* 188 * This still works if we haven't read anything -- the dummy structure 189 * points to the root list, so we step through to the end of the root 190 * list which has a valid parent pointer. 191 */ 192 if (sp->fts_cur) { 193 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 194 freep = p; 195 p = p->fts_link ? p->fts_link : p->fts_parent; 196 free(freep); 197 } 198 free(p); 199 } 200 201 /* Free up child linked list, sort array, path buffer. */ 202 if (sp->fts_child) 203 fts_lfree(sp->fts_child); 204 if (sp->fts_array) 205 free(sp->fts_array); 206 free(sp->fts_path); 207 208 /* Return to original directory, save errno if necessary. */ 209 if (!ISSET(FTS_NOCHDIR)) { 210 saved_errno = fchdir(sp->fts_rfd) ? errno : 0; 211 (void)close(sp->fts_rfd); 212 } 213 214 /* Free up the stream pointer. */ 215 free(sp); 216 217 /* Set errno and return. */ 218 if (!ISSET(FTS_NOCHDIR) && saved_errno) { 219 errno = saved_errno; 220 return (-1); 221 } 222 return (0); 223 } 224 225 /* 226 * Special case a root of "/" so that slashes aren't appended which would 227 * cause paths to be written as "//foo". 228 */ 229 #define NAPPEND(p) \ 230 (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \ 231 p->fts_path[0] == '/' ? 0 : p->fts_pathlen) 232 233 FTSENT * 234 fts_read(sp) 235 register FTS *sp; 236 { 237 register FTSENT *p, *tmp; 238 register int instr; 239 register char *t; 240 int saved_errno; 241 242 /* If finished or unrecoverable error, return NULL. */ 243 if (sp->fts_cur == NULL || ISSET(FTS_STOP)) 244 return (NULL); 245 246 /* Set current node pointer. */ 247 p = sp->fts_cur; 248 249 /* Save and zero out user instructions. */ 250 instr = p->fts_instr; 251 p->fts_instr = FTS_NOINSTR; 252 253 /* Any type of file may be re-visited; re-stat and re-turn. */ 254 if (instr == FTS_AGAIN) { 255 p->fts_info = fts_stat(sp, p, 0); 256 return (p); 257 } 258 259 /* 260 * Following a symlink -- SLNONE test allows application to see 261 * SLNONE and recover. If indirecting through a symlink, have 262 * keep a pointer to current location. If unable to get that 263 * pointer, follow fails. 264 */ 265 if (instr == FTS_FOLLOW && 266 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { 267 p->fts_info = fts_stat(sp, p, 1); 268 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) 269 if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) { 270 p->fts_errno = errno; 271 p->fts_info = FTS_ERR; 272 } else 273 p->fts_flags |= FTS_SYMFOLLOW; 274 return (p); 275 } 276 277 /* Directory in pre-order. */ 278 if (p->fts_info == FTS_D) { 279 /* If skipped or crossed mount point, do post-order visit. */ 280 if (instr == FTS_SKIP || 281 ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) { 282 if (p->fts_flags & FTS_SYMFOLLOW) 283 (void)close(p->fts_symfd); 284 if (sp->fts_child) { 285 fts_lfree(sp->fts_child); 286 sp->fts_child = NULL; 287 } 288 p->fts_info = FTS_DP; 289 return (p); 290 } 291 292 /* Rebuild if only read the names and now traversing. */ 293 if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) { 294 sp->fts_options &= ~FTS_NAMEONLY; 295 fts_lfree(sp->fts_child); 296 sp->fts_child = NULL; 297 } 298 299 /* 300 * Cd to the subdirectory. 301 * 302 * If have already read and now fail to chdir, whack the list 303 * to make the names come out right, and set the parent errno 304 * so the application will eventually get an error condition. 305 * Set the FTS_DONTCHDIR flag so that when we logically change 306 * directories back to the parent we don't do a chdir. 307 * 308 * If haven't read do so. If the read fails, fts_build sets 309 * FTS_STOP or the fts_info field of the node. 310 */ 311 if (sp->fts_child) { 312 if (CHDIR(sp, p->fts_accpath)) { 313 p->fts_errno = errno; 314 p->fts_flags |= FTS_DONTCHDIR; 315 for (p = sp->fts_child; p; p = p->fts_link) 316 p->fts_accpath = 317 p->fts_parent->fts_accpath; 318 } 319 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { 320 if (ISSET(FTS_STOP)) 321 return (NULL); 322 return (p); 323 } 324 p = sp->fts_child; 325 sp->fts_child = NULL; 326 goto name; 327 } 328 329 /* Move to the next node on this level. */ 330 next: tmp = p; 331 if (p = p->fts_link) { 332 free(tmp); 333 334 /* If reached the top, load the paths for the next root. */ 335 if (p->fts_level == FTS_ROOTLEVEL) { 336 fts_load(sp, p); 337 return (sp->fts_cur = p); 338 } 339 340 /* 341 * User may have called fts_set on the node. If skipped, 342 * ignore. If followed, get a file descriptor so we can 343 * get back if necessary. 344 */ 345 if (p->fts_instr == FTS_SKIP) 346 goto next; 347 if (p->fts_instr == FTS_FOLLOW) { 348 p->fts_info = fts_stat(sp, p, 1); 349 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) 350 if ((p->fts_symfd = 351 open(".", O_RDONLY, 0)) < 0) { 352 p->fts_errno = errno; 353 p->fts_info = FTS_ERR; 354 } else 355 p->fts_flags |= FTS_SYMFOLLOW; 356 p->fts_instr = FTS_NOINSTR; 357 } 358 359 name: t = sp->fts_path + NAPPEND(p->fts_parent); 360 *t++ = '/'; 361 bcopy(p->fts_name, t, p->fts_namelen + 1); 362 return (sp->fts_cur = p); 363 } 364 365 /* Move up to the parent node. */ 366 p = tmp->fts_parent; 367 free(tmp); 368 369 if (p->fts_level == FTS_ROOTPARENTLEVEL) { 370 /* 371 * Done; free everything up and set errno to 0 so the user 372 * can distinguish between error and EOF. 373 */ 374 free(p); 375 errno = 0; 376 return (sp->fts_cur = NULL); 377 } 378 379 /* Nul terminate the pathname. */ 380 sp->fts_path[p->fts_pathlen] = '\0'; 381 382 /* 383 * Return to the parent directory. If at a root node or came through 384 * a symlink, go back through the file descriptor. Otherwise, cd up 385 * one directory. 386 */ 387 if (p->fts_level == FTS_ROOTLEVEL) { 388 if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) { 389 SET(FTS_STOP); 390 return (NULL); 391 } 392 } else if (p->fts_flags & FTS_SYMFOLLOW) { 393 if (FCHDIR(sp, p->fts_symfd)) { 394 saved_errno = errno; 395 (void)close(p->fts_symfd); 396 errno = saved_errno; 397 SET(FTS_STOP); 398 return (NULL); 399 } 400 (void)close(p->fts_symfd); 401 } else if (!(p->fts_flags & FTS_DONTCHDIR)) { 402 if (CHDIR(sp, "..")) { 403 SET(FTS_STOP); 404 return (NULL); 405 } 406 } 407 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; 408 return (sp->fts_cur = p); 409 } 410 411 /* 412 * Fts_set takes the stream as an argument although it's not used in this 413 * implementation; it would be necessary if anyone wanted to add global 414 * semantics to fts using fts_set. An error return is allowed for similar 415 * reasons. 416 */ 417 /* ARGSUSED */ 418 int 419 fts_set(sp, p, instr) 420 FTS *sp; 421 FTSENT *p; 422 int instr; 423 { 424 if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && 425 instr != FTS_NOINSTR && instr != FTS_SKIP) { 426 errno = EINVAL; 427 return (1); 428 } 429 p->fts_instr = instr; 430 return (0); 431 } 432 433 FTSENT * 434 fts_children(sp, instr) 435 register FTS *sp; 436 int instr; 437 { 438 register FTSENT *p; 439 int fd; 440 441 if (instr && instr != FTS_NAMEONLY) { 442 errno = EINVAL; 443 return (NULL); 444 } 445 446 /* Set current node pointer. */ 447 p = sp->fts_cur; 448 449 /* 450 * Errno set to 0 so user can distinguish empty directory from 451 * an error. 452 */ 453 errno = 0; 454 455 /* Fatal errors stop here. */ 456 if (ISSET(FTS_STOP)) 457 return (NULL); 458 459 /* Return logical hierarchy of user's arguments. */ 460 if (p->fts_info == FTS_INIT) 461 return (p->fts_link); 462 463 /* 464 * If not a directory being visited in pre-order, stop here. Could 465 * allow FTS_DNR, assuming the user has fixed the problem, but the 466 * same effect is available with FTS_AGAIN. 467 */ 468 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) 469 return (NULL); 470 471 /* Free up any previous child list. */ 472 if (sp->fts_child) 473 fts_lfree(sp->fts_child); 474 475 if (instr == FTS_NAMEONLY) { 476 sp->fts_options |= FTS_NAMEONLY; 477 instr = BNAMES; 478 } else 479 instr = BCHILD; 480 481 /* 482 * If using chdir on a relative path and called BEFORE fts_read does 483 * its chdir to the root of a traversal, we can lose -- we need to 484 * chdir into the subdirectory, and we don't know where the current 485 * directory is, so we can't get back so that the upcoming chdir by 486 * fts_read will work. 487 */ 488 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || 489 ISSET(FTS_NOCHDIR)) 490 return (sp->fts_child = fts_build(sp, instr)); 491 492 if ((fd = open(".", O_RDONLY, 0)) < 0) 493 return (NULL); 494 sp->fts_child = fts_build(sp, instr); 495 if (fchdir(fd)) 496 return (NULL); 497 (void)close(fd); 498 return (sp->fts_child); 499 } 500 501 /* 502 * This is the tricky part -- do not casually change *anything* in here. The 503 * idea is to build the linked list of entries that are used by fts_children 504 * and fts_read. There are lots of special cases. 505 * 506 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is 507 * set and it's a physical walk (so that symbolic links can't be directories), 508 * we assume that the number of subdirectories in a node is equal to the number 509 * of links to the parent. This allows stat calls to be skipped in any leaf 510 * directories and for any nodes after the directories in the parent node have 511 * been found. This empirically cuts the stat calls by about 2/3. 512 */ 513 static FTSENT * 514 fts_build(sp, type) 515 register FTS *sp; 516 int type; 517 { 518 register struct dirent *dp; 519 register FTSENT *p, *head; 520 register int nitems; 521 FTSENT *cur, *tail; 522 DIR *dirp; 523 void *adjaddr; 524 int cderrno, descend, len, level, maxlen, nlinks, saved_errno; 525 char *cp; 526 527 /* Set current node pointer. */ 528 cur = sp->fts_cur; 529 530 /* 531 * Open the directory for reading. If this fails, we're done. 532 * If being called from fts_read, set the fts_info field. 533 */ 534 if ((dirp = opendir(cur->fts_accpath)) == NULL) { 535 if (type == BREAD) { 536 cur->fts_info = FTS_DNR; 537 cur->fts_errno = errno; 538 } 539 return (NULL); 540 } 541 542 /* 543 * Nlinks is the number of possible entries of type directory in the 544 * directory if we're cheating on stat calls, 0 if we're not doing 545 * any stat calls at all, -1 if we're doing stats on everything. 546 */ 547 if (type == BNAMES) 548 nlinks = 0; 549 else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) 550 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); 551 else 552 nlinks = -1; 553 554 #ifdef notdef 555 (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); 556 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", 557 ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); 558 #endif 559 /* 560 * If we're going to need to stat anything or we want to descend 561 * and stay in the directory, chdir. If this fails we keep going. 562 * We won't be able to stat anything, but we can still return the 563 * names themselves. Note, that since fts_read won't be able to 564 * chdir into the directory, it will have to return different path 565 * names than before, i.e. "a/b" instead of "b". Since the node 566 * has already been visited in pre-order, have to wait until the 567 * post-order visit to return the error. There is a special case 568 * here, if there was nothing to stat then it's not an error to 569 * not be able to stat. This is all fairly nasty. If a program 570 * needed sorted entries or stat information, they had better be 571 * checking FTS_NS on the returned nodes. 572 */ 573 if (nlinks || type == BREAD) 574 if (FCHDIR(sp, dirfd(dirp))) { 575 if (nlinks && type == BREAD) 576 cur->fts_errno = errno; 577 descend = 0; 578 cderrno = errno; 579 } else { 580 descend = 1; 581 cderrno = 0; 582 } 583 else 584 descend = 0; 585 586 /* 587 * Figure out the max file name length that can be stored in the 588 * current path -- the inner loop allocates more path as necessary. 589 * We really wouldn't have to do the maxlen calculations here, we 590 * could do them in fts_read before returning the path, but it's a 591 * lot easier here since the length is part of the dirent structure. 592 * 593 * If not changing directories set a pointer so that can just append 594 * each new name into the path. 595 */ 596 maxlen = sp->fts_pathlen - cur->fts_pathlen - 1; 597 len = NAPPEND(cur); 598 if (ISSET(FTS_NOCHDIR)) { 599 cp = sp->fts_path + len; 600 *cp++ = '/'; 601 } 602 603 level = cur->fts_level + 1; 604 605 /* Read the directory, attaching each entry to the `link' pointer. */ 606 adjaddr = NULL; 607 for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) { 608 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 609 continue; 610 611 if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL) 612 goto mem1; 613 if (dp->d_namlen > maxlen) { 614 if (fts_palloc(sp, (int)dp->d_namlen)) { 615 /* 616 * No more memory for path or structures. Save 617 * errno, free up the current structure and the 618 * structures already allocated. 619 */ 620 mem1: saved_errno = errno; 621 if (p) 622 free(p); 623 fts_lfree(head); 624 (void)closedir(dirp); 625 errno = saved_errno; 626 cur->fts_info = FTS_ERR; 627 SET(FTS_STOP); 628 return (NULL); 629 } 630 adjaddr = sp->fts_path; 631 maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1; 632 } 633 634 p->fts_pathlen = len + dp->d_namlen + 1; 635 p->fts_parent = sp->fts_cur; 636 p->fts_level = level; 637 638 if (cderrno) { 639 if (nlinks) { 640 p->fts_info = FTS_NS; 641 p->fts_errno = cderrno; 642 } else 643 p->fts_info = FTS_NSOK; 644 p->fts_accpath = cur->fts_accpath; 645 } else if (nlinks) { 646 /* Build a file name for fts_stat to stat. */ 647 if (ISSET(FTS_NOCHDIR)) { 648 p->fts_accpath = p->fts_path; 649 bcopy(p->fts_name, cp, p->fts_namelen + 1); 650 } else 651 p->fts_accpath = p->fts_name; 652 p->fts_info = fts_stat(sp, p, 0); 653 if (nlinks > 0 && (p->fts_info == FTS_D || 654 p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) 655 --nlinks; 656 } else { 657 p->fts_accpath = 658 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 659 p->fts_info = FTS_NSOK; 660 } 661 662 /* We walk in directory order so "ls -f" doesn't get upset. */ 663 p->fts_link = NULL; 664 if (head == NULL) 665 head = tail = p; 666 else { 667 tail->fts_link = p; 668 tail = p; 669 } 670 ++nitems; 671 } 672 (void)closedir(dirp); 673 674 /* 675 * If had to realloc the path, adjust the addresses for the rest 676 * of the tree. 677 */ 678 if (adjaddr) 679 fts_padjust(sp, adjaddr); 680 681 /* 682 * If not changing directories, reset the path back to original 683 * state. 684 */ 685 if (ISSET(FTS_NOCHDIR)) { 686 if (cp - 1 > sp->fts_path) 687 --cp; 688 *cp = '\0'; 689 } 690 691 /* 692 * If descended after called from fts_children or called from 693 * fts_read and didn't find anything, get back. If can't get 694 * back, done. 695 */ 696 if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) { 697 cur->fts_info = FTS_ERR; 698 SET(FTS_STOP); 699 return (NULL); 700 } 701 702 /* If didn't find anything, return NULL. */ 703 if (!nitems) { 704 if (type == BREAD) 705 cur->fts_info = FTS_DP; 706 return (NULL); 707 } 708 709 /* Sort the entries. */ 710 if (sp->fts_compar && nitems > 1) 711 head = fts_sort(sp, head, nitems); 712 return (head); 713 } 714 715 static u_short 716 fts_stat(sp, p, follow) 717 FTS *sp; 718 register FTSENT *p; 719 int follow; 720 { 721 register FTSENT *t; 722 register dev_t dev; 723 register ino_t ino; 724 struct stat *sbp, sb; 725 int saved_errno; 726 727 /* If user needs stat info, stat buffer already allocated. */ 728 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; 729 730 /* 731 * If doing a logical walk, or application requested FTS_FOLLOW, do 732 * a stat(2). If that fails, check for a non-existent symlink. If 733 * fail, set the errno from the stat call. 734 */ 735 if (ISSET(FTS_LOGICAL) || follow) { 736 if (stat(p->fts_accpath, sbp)) { 737 saved_errno = errno; 738 if (!lstat(p->fts_accpath, sbp)) { 739 errno = 0; 740 return (FTS_SLNONE); 741 } 742 p->fts_errno = saved_errno; 743 goto err; 744 } 745 } else if (lstat(p->fts_accpath, sbp)) { 746 p->fts_errno = errno; 747 err: bzero(sbp, sizeof(struct stat)); 748 return (FTS_NS); 749 } 750 751 if (S_ISDIR(sbp->st_mode)) { 752 /* 753 * Set the device/inode. Used to find cycles and check for 754 * crossing mount points. Also remember the link count, used 755 * in fts_build to limit the number of stat calls. It is 756 * understood that these fields are only referenced if fts_info 757 * is set to FTS_D. 758 */ 759 dev = p->fts_dev = sbp->st_dev; 760 ino = p->fts_ino = sbp->st_ino; 761 p->fts_nlink = sbp->st_nlink; 762 763 if (ISDOT(p->fts_name)) 764 return (FTS_DOT); 765 766 /* 767 * Cycle detection is done by brute force when the directory 768 * is first encountered. If the tree gets deep enough or the 769 * number of symbolic links to directories is high enough, 770 * something faster might be worthwhile. 771 */ 772 for (t = p->fts_parent; 773 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) 774 if (ino == t->fts_ino && dev == t->fts_dev) { 775 p->fts_cycle = t; 776 return (FTS_DC); 777 } 778 return (FTS_D); 779 } 780 if (S_ISLNK(sbp->st_mode)) 781 return (FTS_SL); 782 if (S_ISREG(sbp->st_mode)) 783 return (FTS_F); 784 return (FTS_DEFAULT); 785 } 786 787 static FTSENT * 788 fts_sort(sp, head, nitems) 789 FTS *sp; 790 FTSENT *head; 791 register int nitems; 792 { 793 register FTSENT **ap, *p; 794 795 /* 796 * Construct an array of pointers to the structures and call qsort(3). 797 * Reassemble the array in the order returned by qsort. If unable to 798 * sort for memory reasons, return the directory entries in their 799 * current order. Allocate enough space for the current needs plus 800 * 40 so don't realloc one entry at a time. 801 */ 802 if (nitems > sp->fts_nitems) { 803 sp->fts_nitems = nitems + 40; 804 if ((sp->fts_array = realloc(sp->fts_array, 805 (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) { 806 sp->fts_nitems = 0; 807 return (head); 808 } 809 } 810 for (ap = sp->fts_array, p = head; p; p = p->fts_link) 811 *ap++ = p; 812 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); 813 for (head = *(ap = sp->fts_array); --nitems; ++ap) 814 ap[0]->fts_link = ap[1]; 815 ap[0]->fts_link = NULL; 816 return (head); 817 } 818 819 static FTSENT * 820 fts_alloc(sp, name, len) 821 FTS *sp; 822 char *name; 823 register int len; 824 { 825 register FTSENT *p; 826 int needstat; 827 828 /* 829 * Variable sized structures. The stat structure isn't necessary 830 * if the user doesn't need it, and the name is variable length. 831 * Allocate enough extra space after the structure to store them. 832 */ 833 needstat = ISSET(FTS_NOSTAT) ? 0 : ALIGN(sizeof(struct stat)); 834 if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL) 835 return (NULL); 836 bcopy(name, p->fts_name, len + 1); 837 if (needstat) 838 p->fts_statp = (struct stat *)ALIGN(p->fts_name + len + 1); 839 p->fts_namelen = len; 840 p->fts_path = sp->fts_path; 841 p->fts_errno = 0; 842 p->fts_flags = 0; 843 p->fts_instr = FTS_NOINSTR; 844 p->fts_number = 0; 845 p->fts_pointer = NULL; 846 return (p); 847 } 848 849 static void 850 fts_lfree(head) 851 register FTSENT *head; 852 { 853 register FTSENT *p; 854 855 /* Free a linked list of structures. */ 856 while (p = head) { 857 head = head->fts_link; 858 free(p); 859 } 860 } 861 862 /* 863 * Allow essentially unlimited paths; find, rm, ls should all work on any tree. 864 * Most systems will allow creation of paths much longer than MAXPATHLEN, even 865 * though the kernel won't resolve them. Add the size (not just what's needed) 866 * plus 256 bytes so don't realloc the path 2 bytes at a time. 867 */ 868 static int 869 fts_palloc(sp, more) 870 FTS *sp; 871 int more; 872 { 873 874 sp->fts_pathlen += more + 256; 875 sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen); 876 return (sp->fts_path == NULL); 877 } 878 879 /* 880 * When the path is realloc'd, have to fix all of the pointers in structures 881 * already returned. 882 */ 883 static void 884 fts_padjust(sp, addr) 885 FTS *sp; 886 void *addr; 887 { 888 FTSENT *p; 889 890 #define ADJUST(p) { \ 891 (p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \ 892 (p)->fts_path = addr; \ 893 } 894 /* Adjust the current set of children. */ 895 for (p = sp->fts_child; p; p = p->fts_link) 896 ADJUST(p); 897 898 /* Adjust the rest of the tree. */ 899 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 900 ADJUST(p); 901 p = p->fts_link ? p->fts_link : p->fts_parent; 902 } 903 } 904