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.39 (Berkeley) 06/02/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 *, size_t)); 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 /* 74 * Start out with more than 1K of path space, and enough, in any 75 * case, to hold the user's paths. 76 */ 77 if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN))) 78 goto mem1; 79 80 /* Allocate/initialize root's parent. */ 81 if ((parent = fts_alloc(sp, "", 0)) == NULL) 82 goto mem2; 83 parent->fts_level = FTS_ROOTPARENTLEVEL; 84 85 /* Allocate/initialize root(s). */ 86 for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) { 87 /* Don't allow zero-length paths. */ 88 if ((len = strlen(*argv)) == 0) { 89 errno = ENOENT; 90 goto mem3; 91 } 92 if (maxlen < len) 93 maxlen = len; 94 95 p = fts_alloc(sp, *argv, len); 96 p->fts_level = FTS_ROOTLEVEL; 97 p->fts_parent = parent; 98 p->fts_accpath = p->fts_name; 99 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); 100 101 /* Command-line "." and ".." are real directories. */ 102 if (p->fts_info == FTS_DOT) 103 p->fts_info = FTS_D; 104 105 /* 106 * If comparison routine supplied, traverse in sorted 107 * order; otherwise traverse in the order specified. 108 */ 109 if (compar) { 110 p->fts_link = root; 111 root = p; 112 } else { 113 p->fts_link = NULL; 114 if (root == NULL) 115 tmp = root = p; 116 else { 117 tmp->fts_link = p; 118 tmp = p; 119 } 120 } 121 } 122 if (compar && nitems > 1) 123 root = fts_sort(sp, root, nitems); 124 125 /* 126 * Allocate a dummy pointer and make fts_read think that we've just 127 * finished the node before the root(s); set p->fts_info to FTS_INIT 128 * so that everything about the "current" node is ignored. 129 */ 130 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) 131 goto mem3; 132 sp->fts_cur->fts_link = root; 133 sp->fts_cur->fts_info = FTS_INIT; 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: fts_lfree(root); 148 free(parent); 149 mem2: free(sp->fts_path); 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 * but set a flag so we don't chdir after the post-order visit. 563 * We won't be able to stat anything, but we can still return the 564 * names themselves. Note, that since fts_read won't be able to 565 * chdir into the directory, it will have to return different path 566 * names than before, i.e. "a/b" instead of "b". Since the node 567 * has already been visited in pre-order, have to wait until the 568 * post-order visit to return the error. There is a special case 569 * here, if there was nothing to stat then it's not an error to 570 * not be able to stat. This is all fairly nasty. If a program 571 * needed sorted entries or stat information, they had better be 572 * checking FTS_NS on the returned nodes. 573 */ 574 if (nlinks || type == BREAD) 575 if (FCHDIR(sp, dirfd(dirp))) { 576 if (nlinks && type == BREAD) 577 cur->fts_errno = errno; 578 cur->fts_flags |= FTS_DONTCHDIR; 579 descend = 0; 580 cderrno = errno; 581 } else { 582 descend = 1; 583 cderrno = 0; 584 } 585 else 586 descend = 0; 587 588 /* 589 * Figure out the max file name length that can be stored in the 590 * current path -- the inner loop allocates more path as necessary. 591 * We really wouldn't have to do the maxlen calculations here, we 592 * could do them in fts_read before returning the path, but it's a 593 * lot easier here since the length is part of the dirent structure. 594 * 595 * If not changing directories set a pointer so that can just append 596 * each new name into the path. 597 */ 598 maxlen = sp->fts_pathlen - cur->fts_pathlen - 1; 599 len = NAPPEND(cur); 600 if (ISSET(FTS_NOCHDIR)) { 601 cp = sp->fts_path + len; 602 *cp++ = '/'; 603 } 604 605 level = cur->fts_level + 1; 606 607 /* Read the directory, attaching each entry to the `link' pointer. */ 608 adjaddr = NULL; 609 for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) { 610 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 611 continue; 612 613 if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL) 614 goto mem1; 615 if (dp->d_namlen > maxlen) { 616 if (fts_palloc(sp, (size_t)dp->d_namlen)) { 617 /* 618 * No more memory for path or structures. Save 619 * errno, free up the current structure and the 620 * structures already allocated. 621 */ 622 mem1: saved_errno = errno; 623 if (p) 624 free(p); 625 fts_lfree(head); 626 (void)closedir(dirp); 627 errno = saved_errno; 628 cur->fts_info = FTS_ERR; 629 SET(FTS_STOP); 630 return (NULL); 631 } 632 adjaddr = sp->fts_path; 633 maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1; 634 } 635 636 p->fts_pathlen = len + dp->d_namlen + 1; 637 p->fts_parent = sp->fts_cur; 638 p->fts_level = level; 639 640 if (cderrno) { 641 if (nlinks) { 642 p->fts_info = FTS_NS; 643 p->fts_errno = cderrno; 644 } else 645 p->fts_info = FTS_NSOK; 646 p->fts_accpath = cur->fts_accpath; 647 } else if (nlinks) { 648 /* Build a file name for fts_stat to stat. */ 649 if (ISSET(FTS_NOCHDIR)) { 650 p->fts_accpath = p->fts_path; 651 bcopy(p->fts_name, cp, p->fts_namelen + 1); 652 } else 653 p->fts_accpath = p->fts_name; 654 p->fts_info = fts_stat(sp, p, 0); 655 if (nlinks > 0 && (p->fts_info == FTS_D || 656 p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) 657 --nlinks; 658 } else { 659 p->fts_accpath = 660 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 661 p->fts_info = FTS_NSOK; 662 } 663 664 /* We walk in directory order so "ls -f" doesn't get upset. */ 665 p->fts_link = NULL; 666 if (head == NULL) 667 head = tail = p; 668 else { 669 tail->fts_link = p; 670 tail = p; 671 } 672 ++nitems; 673 } 674 (void)closedir(dirp); 675 676 /* 677 * If had to realloc the path, adjust the addresses for the rest 678 * of the tree. 679 */ 680 if (adjaddr) 681 fts_padjust(sp, adjaddr); 682 683 /* 684 * If not changing directories, reset the path back to original 685 * state. 686 */ 687 if (ISSET(FTS_NOCHDIR)) { 688 if (cp - 1 > sp->fts_path) 689 --cp; 690 *cp = '\0'; 691 } 692 693 /* 694 * If descended after called from fts_children or called from 695 * fts_read and didn't find anything, get back. If can't get 696 * back, done. 697 */ 698 if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) { 699 cur->fts_info = FTS_ERR; 700 SET(FTS_STOP); 701 return (NULL); 702 } 703 704 /* If didn't find anything, return NULL. */ 705 if (!nitems) { 706 if (type == BREAD) 707 cur->fts_info = FTS_DP; 708 return (NULL); 709 } 710 711 /* Sort the entries. */ 712 if (sp->fts_compar && nitems > 1) 713 head = fts_sort(sp, head, nitems); 714 return (head); 715 } 716 717 static u_short 718 fts_stat(sp, p, follow) 719 FTS *sp; 720 register FTSENT *p; 721 int follow; 722 { 723 register FTSENT *t; 724 register dev_t dev; 725 register ino_t ino; 726 struct stat *sbp, sb; 727 int saved_errno; 728 729 /* If user needs stat info, stat buffer already allocated. */ 730 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; 731 732 /* 733 * If doing a logical walk, or application requested FTS_FOLLOW, do 734 * a stat(2). If that fails, check for a non-existent symlink. If 735 * fail, set the errno from the stat call. 736 */ 737 if (ISSET(FTS_LOGICAL) || follow) { 738 if (stat(p->fts_accpath, sbp)) { 739 saved_errno = errno; 740 if (!lstat(p->fts_accpath, sbp)) { 741 errno = 0; 742 return (FTS_SLNONE); 743 } 744 p->fts_errno = saved_errno; 745 goto err; 746 } 747 } else if (lstat(p->fts_accpath, sbp)) { 748 p->fts_errno = errno; 749 err: bzero(sbp, sizeof(struct stat)); 750 return (FTS_NS); 751 } 752 753 if (S_ISDIR(sbp->st_mode)) { 754 /* 755 * Set the device/inode. Used to find cycles and check for 756 * crossing mount points. Also remember the link count, used 757 * in fts_build to limit the number of stat calls. It is 758 * understood that these fields are only referenced if fts_info 759 * is set to FTS_D. 760 */ 761 dev = p->fts_dev = sbp->st_dev; 762 ino = p->fts_ino = sbp->st_ino; 763 p->fts_nlink = sbp->st_nlink; 764 765 if (ISDOT(p->fts_name)) 766 return (FTS_DOT); 767 768 /* 769 * Cycle detection is done by brute force when the directory 770 * is first encountered. If the tree gets deep enough or the 771 * number of symbolic links to directories is high enough, 772 * something faster might be worthwhile. 773 */ 774 for (t = p->fts_parent; 775 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) 776 if (ino == t->fts_ino && dev == t->fts_dev) { 777 p->fts_cycle = t; 778 return (FTS_DC); 779 } 780 return (FTS_D); 781 } 782 if (S_ISLNK(sbp->st_mode)) 783 return (FTS_SL); 784 if (S_ISREG(sbp->st_mode)) 785 return (FTS_F); 786 return (FTS_DEFAULT); 787 } 788 789 static FTSENT * 790 fts_sort(sp, head, nitems) 791 FTS *sp; 792 FTSENT *head; 793 register int nitems; 794 { 795 register FTSENT **ap, *p; 796 797 /* 798 * Construct an array of pointers to the structures and call qsort(3). 799 * Reassemble the array in the order returned by qsort. If unable to 800 * sort for memory reasons, return the directory entries in their 801 * current order. Allocate enough space for the current needs plus 802 * 40 so don't realloc one entry at a time. 803 */ 804 if (nitems > sp->fts_nitems) { 805 sp->fts_nitems = nitems + 40; 806 if ((sp->fts_array = realloc(sp->fts_array, 807 (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) { 808 sp->fts_nitems = 0; 809 return (head); 810 } 811 } 812 for (ap = sp->fts_array, p = head; p; p = p->fts_link) 813 *ap++ = p; 814 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); 815 for (head = *(ap = sp->fts_array); --nitems; ++ap) 816 ap[0]->fts_link = ap[1]; 817 ap[0]->fts_link = NULL; 818 return (head); 819 } 820 821 static FTSENT * 822 fts_alloc(sp, name, namelen) 823 FTS *sp; 824 char *name; 825 register int namelen; 826 { 827 register FTSENT *p; 828 size_t len; 829 830 /* 831 * The file name is a variable length array and no stat structure is 832 * necessary if the user has set the nostat bit. Allocate the FTSENT 833 * structure, the file name and the stat structure in one chunk, but 834 * be careful that the stat structure is reasonably aligned. Since the 835 * fts_name field is declared to be of size 1, the fts_name pointer is 836 * namelen + 2 before the first possible address of the stat structure. 837 */ 838 len = sizeof(FTSENT) + namelen; 839 if (!ISSET(FTS_NOSTAT)) 840 len += sizeof(struct stat) + ALIGNBYTES; 841 if ((p = malloc(len)) == NULL) 842 return (NULL); 843 844 /* Copy the name plus the trailing NULL. */ 845 bcopy(name, p->fts_name, namelen + 1); 846 847 if (!ISSET(FTS_NOSTAT)) 848 p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2); 849 p->fts_namelen = namelen; 850 p->fts_path = sp->fts_path; 851 p->fts_errno = 0; 852 p->fts_flags = 0; 853 p->fts_instr = FTS_NOINSTR; 854 p->fts_number = 0; 855 p->fts_pointer = NULL; 856 return (p); 857 } 858 859 static void 860 fts_lfree(head) 861 register FTSENT *head; 862 { 863 register FTSENT *p; 864 865 /* Free a linked list of structures. */ 866 while (p = head) { 867 head = head->fts_link; 868 free(p); 869 } 870 } 871 872 /* 873 * Allow essentially unlimited paths; find, rm, ls should all work on any tree. 874 * Most systems will allow creation of paths much longer than MAXPATHLEN, even 875 * though the kernel won't resolve them. Add the size (not just what's needed) 876 * plus 256 bytes so don't realloc the path 2 bytes at a time. 877 */ 878 static int 879 fts_palloc(sp, more) 880 FTS *sp; 881 size_t more; 882 { 883 sp->fts_pathlen += more + 256; 884 sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen); 885 return (sp->fts_path == NULL); 886 } 887 888 /* 889 * When the path is realloc'd, have to fix all of the pointers in structures 890 * already returned. 891 */ 892 static void 893 fts_padjust(sp, addr) 894 FTS *sp; 895 void *addr; 896 { 897 FTSENT *p; 898 899 #define ADJUST(p) { \ 900 (p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \ 901 (p)->fts_path = addr; \ 902 } 903 /* Adjust the current set of children. */ 904 for (p = sp->fts_child; p; p = p->fts_link) 905 ADJUST(p); 906 907 /* Adjust the rest of the tree. */ 908 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 909 ADJUST(p); 910 p = p->fts_link ? p->fts_link : p->fts_parent; 911 } 912 } 913