Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

870 lines
23 KiB

  1. /* Copyright (c) 1990 The Regents of the University of California.
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. All advertising materials mentioning features or use of this software
  13. * must display the following acknowledgement:
  14. * This product includes software developed by the University of
  15. * California, Berkeley and its contributors.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #ifdef DF_POSIX
  33. #include <misc.h>
  34. #endif
  35. #if defined(LIBC_SCCS) && !defined(lint)
  36. static char sccsid[] = "@(#)fts.c 5.19 (Berkeley) 5/9/91";
  37. #endif /* LIBC_SCCS and not lint */
  38. #include <sys/cdefs.h>
  39. #ifdef _POSIX_SOURCE //DF_DSC POSIX does not need this
  40. #else // only MAXPATHLEN was found there
  41. #include <sys/param.h> // and it wants machine directory stuff
  42. #endif
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <dirent.h>
  46. #include <errno.h>
  47. #include "fts.h"
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. extern void bzero();
  52. extern void bcopy();
  53. extern char *rindex();
  54. extern int lstat();
  55. static FTSENT *fts_alloc(), *fts_build(), *fts_sort();
  56. static void fts_load(), fts_lfree();
  57. static u_short fts_stat();
  58. static char *fts_path();
  59. #define ISSET(opt) (sp->fts_options & opt)
  60. #define SET(opt) (sp->fts_options |= opt)
  61. #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
  62. #ifdef _POSIX_SOURCE //DF_DSC fchdir not in POSIX
  63. #else
  64. #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
  65. #endif
  66. /* fts_build flags */
  67. #define BCHILD 1 /* from fts_children */
  68. #define BREAD 2 /* from fts_read */
  69. FTS *
  70. fts_open(argv, options, compar)
  71. char * const *argv;
  72. register int options;
  73. int (*compar)(const FTSENT *, const FTSENT *);
  74. {
  75. register FTS *sp;
  76. register FTSENT *p, *root;
  77. register int nitems, maxlen;
  78. FTSENT *parent, *tmp;
  79. int len;
  80. /* Allocate/initialize the stream */
  81. if (!(sp = (FTS *)malloc((u_int)sizeof(FTS))))
  82. return(NULL);
  83. bzero(sp, sizeof(FTS));
  84. sp->fts_compar = compar;
  85. sp->fts_options = options;
  86. /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
  87. if (ISSET(FTS_LOGICAL))
  88. SET(FTS_NOCHDIR);
  89. // force NOCHDIR because chdir("..") fails for large trees //DF_DSC
  90. SET(FTS_NOCHDIR);
  91. /* Allocate/initialize root's parent. */
  92. if (!(parent = fts_alloc(sp, "", 0)))
  93. goto mem1;
  94. parent->fts_level = FTS_ROOTPARENTLEVEL;
  95. /* Allocate/initialize root(s). */
  96. maxlen = -1;
  97. for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
  98. if (!(len = strlen(*argv))) {
  99. errno = ENOENT;
  100. goto mem2;
  101. }
  102. if (maxlen < len)
  103. maxlen = len;
  104. p = fts_alloc(sp, *argv, len);
  105. p->fts_level = FTS_ROOTLEVEL;
  106. p->fts_parent = parent;
  107. /*
  108. * If comparison routine supplied, traverse in sorted
  109. * order; otherwise traverse in the order specified.
  110. */
  111. if (compar) {
  112. p->fts_link = root;
  113. root = p;
  114. p->fts_accpath = p->fts_name;
  115. if (!(options & FTS_NOSTAT))
  116. p->fts_info = fts_stat(sp, p, 0);
  117. } else {
  118. p->fts_link = NULL;
  119. if (!root)
  120. tmp = root = p;
  121. else {
  122. tmp->fts_link = p;
  123. tmp = p;
  124. }
  125. }
  126. }
  127. if (compar && nitems > 1)
  128. root = fts_sort(sp, root, nitems);
  129. /*
  130. * Allocate a dummy pointer and make fts_read think that we've just
  131. * finished the node before the root(s); set p->fts_info to FTS_NS
  132. * so that everything about the "current" node is ignored.
  133. */
  134. if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
  135. goto mem2;
  136. sp->fts_cur->fts_link = root;
  137. sp->fts_cur->fts_info = FTS_NS;
  138. /* Start out with at least 1K+ of path space. */
  139. #ifdef _POSIX_SOURCE //DF_POSIX POSIX defines PATH_MAX
  140. if (!fts_path(sp, __max(maxlen, PATH_MAX)))
  141. goto mem3;
  142. if ((sp->fts_rpath = malloc(PATH_MAX)) == NULL)
  143. goto mem4;
  144. #else
  145. if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
  146. goto mem3;
  147. #endif
  148. /*
  149. * If using chdir(2), grab a file descriptor pointing to dot to insure
  150. * that we can get back here; this could be avoided for some paths,
  151. * but almost certainly not worth the effort. Slashes, symbolic links,
  152. * and ".." are all fairly nasty problems. Note, if we can't get the
  153. * descriptor we run anyway, just more slowly.
  154. */
  155. #ifdef _POSIX_SOURCE //DF_DSC use getpwd because no fchdir for later
  156. if (!ISSET(FTS_NOCHDIR) && (getcwd(sp->fts_rpath, PATH_MAX)) == NULL)
  157. #else
  158. if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
  159. #endif
  160. SET(FTS_NOCHDIR);
  161. return(sp);
  162. #ifdef _POSIX_SOURCE //DF_DSC
  163. mem4: free(sp->fts_path);
  164. #endif
  165. mem3: free(sp->fts_cur);
  166. mem2: fts_lfree(root);
  167. free(parent);
  168. mem1: free(sp);
  169. return(NULL);
  170. }
  171. static void
  172. fts_load(sp, p)
  173. FTS *sp;
  174. register FTSENT *p;
  175. {
  176. register int len;
  177. register char *cp;
  178. /*
  179. * Load the stream structure for the next traversal. Since we don't
  180. * actually enter the directory until after the preorder visit, set
  181. * the fts_accpath field specially so the chdir gets done to the right
  182. * place and the user can access the first node.
  183. */
  184. len = p->fts_pathlen = p->fts_namelen;
  185. bcopy(p->fts_name, sp->fts_path, len + 1);
  186. if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
  187. len = strlen(++cp);
  188. bcopy(cp, p->fts_name, len + 1);
  189. p->fts_namelen = (short)len;
  190. }
  191. p->fts_accpath = p->fts_path = sp->fts_path;
  192. p->fts_info = fts_stat(sp, p, 0);
  193. sp->rdev = p->fts_statb.st_dev;
  194. }
  195. fts_close(sp)
  196. FTS *sp;
  197. {
  198. register FTSENT *freep, *p;
  199. int saved_errno;
  200. if (sp->fts_cur) {
  201. /*
  202. * This still works if we haven't read anything -- the dummy
  203. * structure points to the root list, so we step through to
  204. * the end of the root list which has a valid parent pointer.
  205. */
  206. for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
  207. freep = p;
  208. p = p->fts_link ? p->fts_link : p->fts_parent;
  209. free(freep);
  210. }
  211. free(p);
  212. }
  213. /* Free up child linked list, sort array, path buffer. */
  214. if (sp->fts_child)
  215. fts_lfree(sp->fts_child);
  216. if (sp->fts_array)
  217. free(sp->fts_array);
  218. free(sp->fts_path);
  219. /* Return to original directory, save errno if necessary. */
  220. if (!ISSET(FTS_NOCHDIR)) {
  221. #ifdef _POSIX_SOURCE
  222. saved_errno = chdir(sp->fts_rpath) ? errno : 0;
  223. free(sp->fts_rpath);
  224. #else
  225. saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
  226. (void)close(sp->fts_rfd);
  227. #endif
  228. }
  229. /* Free up the stream pointer. */
  230. free(sp);
  231. /* Set errno and return. */
  232. if (!ISSET(FTS_NOCHDIR) && saved_errno) {
  233. errno = saved_errno;
  234. return(-1);
  235. }
  236. return(0);
  237. }
  238. /*
  239. * Special case a root of "/" so that slashes aren't appended causing
  240. * paths to be written as "//foo".
  241. */
  242. #define NAPPEND(p) \
  243. (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
  244. p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
  245. FTSENT *
  246. fts_read(sp)
  247. register FTS *sp;
  248. {
  249. register FTSENT *p, *tmp;
  250. register int instr;
  251. register char *t;
  252. /* If finished or unrecoverable error, return NULL. */
  253. if (!sp->fts_cur || ISSET(FTS_STOP))
  254. return(NULL);
  255. /* Set current node pointer. */
  256. p = sp->fts_cur;
  257. /* Save and zero out user instructions. */
  258. instr = p->fts_instr;
  259. p->fts_instr = FTS_NOINSTR;
  260. /* If used fts_link pointer for cycle detection, restore it. */
  261. if (sp->fts_savelink) {
  262. p->fts_link = sp->fts_savelink;
  263. sp->fts_savelink = NULL;
  264. }
  265. /* Any type of file may be re-visited; re-stat and re-turn. */
  266. if (instr == FTS_AGAIN) {
  267. p->fts_info = fts_stat(sp, p, 0);
  268. return(p);
  269. }
  270. /*
  271. * Following a symlink -- SLNONE test allows application to see
  272. * SLNONE and recover.
  273. */
  274. if (instr == FTS_FOLLOW &&
  275. (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
  276. p->fts_info = fts_stat(sp, p, 1);
  277. return(p);
  278. }
  279. /* Directory in pre-order. */
  280. if (p->fts_info == FTS_D) {
  281. /* If skipped or crossed mount point, do post-order visit. */
  282. if (instr == FTS_SKIP ||
  283. ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
  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. * Cd to the subdirectory, reading it if haven't already. If
  293. * the read fails for any reason, or the directory is empty,
  294. * the fts_info field of the current node is set by fts_build.
  295. * If have already read and now fail to chdir, whack the list
  296. * to make the names come out right, and set the parent state
  297. * so the application will eventually get an error condition.
  298. * If haven't read and fail to chdir, check to see if we're
  299. * at the root node -- if so, we have to get back or the root
  300. * node may be inaccessible.
  301. */
  302. if (sp->fts_child) {
  303. if (CHDIR(sp, p->fts_accpath)) {
  304. p->fts_parent->fts_cderr = errno;
  305. for (p = sp->fts_child; p; p = p->fts_link)
  306. p->fts_accpath =
  307. p->fts_parent->fts_accpath;
  308. }
  309. } else if (!(sp->fts_child = fts_build(sp, BREAD))) {
  310. if ISSET(FTS_STOP)
  311. return(NULL);
  312. #ifdef _POSIX_SOURCE
  313. if (p->fts_level == FTS_ROOTLEVEL &&
  314. CHDIR (sp, sp->fts_rpath)) {
  315. SET(FTS_STOP);
  316. return(NULL);
  317. }
  318. #else
  319. if (p->fts_level == FTS_ROOTLEVEL &&
  320. FCHDIR(sp, sp->fts_rfd)) {
  321. SET(FTS_STOP);
  322. return(NULL);
  323. }
  324. #endif
  325. return(p);
  326. }
  327. p = sp->fts_child;
  328. sp->fts_child = NULL;
  329. goto name;
  330. }
  331. /* Move to next node on this level. */
  332. next: tmp = p;
  333. if (p = p->fts_link) {
  334. free(tmp);
  335. /* If reached the top, load the paths for the next root. */
  336. if (p->fts_level == FTS_ROOTLEVEL) {
  337. fts_load(sp, p);
  338. return(sp->fts_cur = p);
  339. }
  340. /* User may have called fts_set on the node. */
  341. if (p->fts_instr == FTS_SKIP)
  342. goto next;
  343. if (p->fts_instr == FTS_FOLLOW) {
  344. p->fts_info = fts_stat(sp, p, 1);
  345. p->fts_instr = FTS_NOINSTR;
  346. }
  347. name: t = sp->fts_path + NAPPEND(p->fts_parent);
  348. *t++ = '/';
  349. bcopy(p->fts_name, t, p->fts_namelen + 1);
  350. return(sp->fts_cur = p);
  351. }
  352. /* Move up to the parent node. */
  353. p = tmp->fts_parent;
  354. free(tmp);
  355. if (p->fts_level == FTS_ROOTPARENTLEVEL) {
  356. /*
  357. * Done; free everything up and set errno to 0 so the user
  358. * can distinguish between error and EOF.
  359. */
  360. free(p);
  361. errno = 0;
  362. return(sp->fts_cur = NULL);
  363. }
  364. sp->fts_path[p->fts_pathlen] = '\0';
  365. /*
  366. * Cd back up to the parent directory. If at a root node, have to cd
  367. * back to the original place, otherwise may not be able to access the
  368. * original node on post-order.
  369. */
  370. if (p->fts_level == FTS_ROOTLEVEL) {
  371. #ifdef _POSIX_SOURCE
  372. if (CHDIR (sp, sp->fts_rpath)) {
  373. #else
  374. if (FCHDIR(sp, sp->fts_rfd)) {
  375. #endif
  376. SET(FTS_STOP);
  377. return(NULL);
  378. }
  379. }
  380. else if (CHDIR(sp, "..")) {
  381. SET(FTS_STOP);
  382. return(NULL);
  383. }
  384. /*
  385. * If had a chdir error when trying to get into the directory, set the
  386. * info field to reflect this, and restore errno. The error indicator
  387. * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
  388. * works.
  389. */
  390. if (p->fts_cderr) {
  391. errno = p->fts_cderr;
  392. p->fts_cderr = 0;
  393. p->fts_info = FTS_ERR;
  394. } else
  395. p->fts_info = FTS_DP;
  396. return(sp->fts_cur = p);
  397. }
  398. /*
  399. * Fts_set takes the stream as an argument although it's not used in this
  400. * implementation; it would be necessary if anyone wanted to add global
  401. * semantics to fts using fts_set. An error return is allowed for similar
  402. * reasons.
  403. */
  404. /* ARGSUSED */
  405. fts_set(sp, p, instr)
  406. FTS *sp;
  407. FTSENT *p;
  408. int instr;
  409. {
  410. p->fts_instr = (u_short)instr;
  411. return(0);
  412. }
  413. FTSENT *
  414. fts_children(sp)
  415. register FTS *sp;
  416. {
  417. register FTSENT *p;
  418. int fd;
  419. #ifdef _POSIX_SOURCE //DF_DSC
  420. char this_path [PATH_MAX];
  421. #endif
  422. /* Set current node pointer. */
  423. p = sp->fts_cur;
  424. /*
  425. * Set errno to 0 so that user can tell the difference between an
  426. * error and a directory without entries. If not a directory being
  427. * visited in *pre-order*, or we've already had fatal errors, return
  428. * immediately.
  429. */
  430. errno = 0;
  431. if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
  432. return(NULL);
  433. /* Free up any previous child list. */
  434. if (sp->fts_child)
  435. fts_lfree(sp->fts_child);
  436. /*
  437. * If using chdir on a relative path and called BEFORE fts_read does
  438. * its chdir to the root of a traversal, we can lose -- we need to
  439. * chdir into the subdirectory, and we don't know where the current
  440. * directory is, so we can't get back so that the upcoming chdir by
  441. * fts_read will work.
  442. */
  443. if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
  444. ISSET(FTS_NOCHDIR))
  445. return(sp->fts_child = fts_build(sp, BCHILD));
  446. #ifdef _POSIX_SOURCE //DF_DSC Because no fchdir
  447. if ((getcwd(this_path, PATH_MAX)) == NULL)
  448. #else
  449. if ((fd = open(".", O_RDONLY, 0)) < 0)
  450. #endif
  451. return(NULL);
  452. sp->fts_child = fts_build(sp, BCHILD);
  453. #ifdef _POSIX_SOURCE
  454. if (chdir(this_path))
  455. return(NULL);
  456. #else
  457. if (fchdir(fd))
  458. return(NULL);
  459. (void)close(fd);
  460. #endif
  461. return(sp->fts_child);
  462. }
  463. /*
  464. * This is the tricky part -- do not casually change *anything* in here. The
  465. * idea is to build the linked list of entries that are used by fts_children
  466. * and fts_read. There are lots of special cases.
  467. *
  468. * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
  469. * set and it's a physical walk (so that symbolic links can't be directories),
  470. * we assume that the number of subdirectories in a node is equal to the number
  471. * of links to the parent. This allows stat calls to be skipped in any leaf
  472. * directories and for any nodes after the directories in the parent node have
  473. * been found. This empirically cuts the stat calls by about 2/3.
  474. */
  475. #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
  476. static FTSENT *
  477. fts_build(sp, type)
  478. register FTS *sp;
  479. int type;
  480. {
  481. register struct dirent *dp;
  482. register FTSENT *p, *head;
  483. register int nitems;
  484. FTSENT *cur;
  485. DIR *dirp;
  486. int cderr, descend, len, level, maxlen, nlinks, saved_errno;
  487. char *cp;
  488. /* Set current node pointer. */
  489. cur = sp->fts_cur;
  490. /*
  491. * Open the directory for reading. If this fails, we're done.
  492. * If being called from fts_read, set the fts_info field.
  493. */
  494. if (!(dirp = opendir(cur->fts_accpath))) {
  495. if (type == BREAD)
  496. cur->fts_info = FTS_DNR;
  497. return(NULL);
  498. }
  499. /*
  500. * Nlinks is the number of possible entries of type directory in the
  501. * directory if we're cheating on stat calls, 0 if we're not doing
  502. * any stat calls at all, -1 if we're doing stats on everything.
  503. */
  504. nlinks =
  505. ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
  506. cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
  507. /*
  508. * If we're going to need to stat anything or we want to descend
  509. * and stay in the directory, chdir. If this fails we keep going.
  510. * We won't be able to stat anything, but we can still return the
  511. * names themselves. Note, that since fts_read won't be able to
  512. * chdir into the directory, it will have to return different path
  513. * names than before, i.e. "a/b" instead of "b". Since the node
  514. * has already been visited in pre-order, have to wait until the
  515. * post-order visit to return the error. This is all fairly nasty.
  516. * If a program needed sorted entries or stat information, they had
  517. * better be checking FTS_NS on the returned nodes.
  518. */
  519. if (nlinks || type == BREAD)
  520. #ifdef _POSIX_SOURCE
  521. if (CHDIR(sp, cur->fts_accpath)) {
  522. #else
  523. if (FCHDIR(sp, dirfd(dirp))) {
  524. #endif
  525. if (type == BREAD)
  526. cur->fts_cderr = errno;
  527. descend = nlinks = 0;
  528. cderr = 1;
  529. } else {
  530. descend = 1;
  531. cderr = 0;
  532. }
  533. else
  534. descend = 0;
  535. /*
  536. * Figure out the max file name length that can be stored in the
  537. * current path -- the inner loop allocates more path as necessary.
  538. * We really wouldn't have to do the maxlen calculations here, we
  539. * could do them in fts_read before returning the path, but it's a
  540. * lot easier here since the length is part of the dirent structure.
  541. *
  542. * If not changing directories set a pointer so that we can just
  543. * append each new name into the path.
  544. */
  545. maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
  546. len = NAPPEND(cur);
  547. if (ISSET(FTS_NOCHDIR)) {
  548. cp = sp->fts_path + len;
  549. *cp++ = '/';
  550. }
  551. level = cur->fts_level + 1;
  552. /* Read the directory, attaching each entry to the `link' pointer. */
  553. for (head = NULL, nitems = 0; dp = readdir(dirp);) {
  554. if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
  555. continue;
  556. #ifdef _POSIX_SOURCE
  557. if (!(p = fts_alloc(sp, dp->d_name, strlen (dp->d_name))))
  558. #else
  559. if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
  560. #endif
  561. goto mem1;
  562. #ifdef _POSIX_SOURCE
  563. if (strlen (dp->d_name) > (unsigned)maxlen) {
  564. #else
  565. if (dp->d_namlen > maxlen) {
  566. #endif
  567. #ifdef _POSIX_SOURCE
  568. if (!fts_path(sp, strlen (dp->d_name))) {
  569. #else
  570. if (!fts_path(sp, (int)dp->d_namlen)) {
  571. #endif
  572. /*
  573. * No more memory for path or structures. Save
  574. * errno, free up the current structure and the
  575. * structures already allocated.
  576. */
  577. mem1: saved_errno = errno;
  578. if (p)
  579. free(p);
  580. fts_lfree(head);
  581. (void)closedir(dirp);
  582. errno = saved_errno;
  583. cur->fts_info = FTS_ERR;
  584. SET(FTS_STOP);
  585. return(NULL);
  586. }
  587. maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
  588. }
  589. #ifdef _POSIX_SOURCE
  590. p->fts_pathlen = len + strlen (dp->d_name) + 1;
  591. #else
  592. p->fts_pathlen = len + dp->d_namlen + 1;
  593. #endif
  594. p->fts_parent = sp->fts_cur;
  595. p->fts_level = (short)level;
  596. if (nlinks) {
  597. /* Build a file name for fts_stat to stat. */
  598. if (ISSET(FTS_NOCHDIR)) {
  599. p->fts_accpath = p->fts_path;
  600. bcopy(p->fts_name, cp, p->fts_namelen + 1);
  601. } else
  602. p->fts_accpath = p->fts_name;
  603. p->fts_info = fts_stat(sp, p, 0);
  604. if (nlinks > 0 && p->fts_info == FTS_D)
  605. --nlinks;
  606. } else if (cderr) {
  607. p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
  608. p->fts_accpath = cur->fts_accpath;
  609. } else {
  610. p->fts_accpath =
  611. ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
  612. p->fts_info = FTS_NSOK;
  613. }
  614. p->fts_link = head;
  615. head = p;
  616. ++nitems;
  617. }
  618. (void)closedir(dirp);
  619. /*
  620. * If not changing directories, reset the path back to original
  621. * state.
  622. */
  623. if (ISSET(FTS_NOCHDIR)) {
  624. if (cp - 1 > sp->fts_path)
  625. --cp;
  626. *cp = '\0';
  627. }
  628. /*
  629. * If descended after called from fts_children or called from
  630. * fts_read and didn't find anything, get back. If can't get
  631. * back, we're done.
  632. */
  633. if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
  634. cur->fts_info = FTS_ERR;
  635. SET(FTS_STOP);
  636. return(NULL);
  637. }
  638. /* If we didn't find anything, just do the post-order visit */
  639. if (!nitems) {
  640. if (type == BREAD)
  641. cur->fts_info = FTS_DP;
  642. return(NULL);
  643. }
  644. /* Sort the entries. */
  645. if (sp->fts_compar && nitems > 1)
  646. head = fts_sort(sp, head, nitems);
  647. return(head);
  648. }
  649. static u_short
  650. fts_stat(sp, p, follow)
  651. FTS *sp;
  652. register FTSENT *p;
  653. int follow;
  654. {
  655. int saved_errno;
  656. /*
  657. * If doing a logical walk, or application requested FTS_FOLLOW, do
  658. * a stat(2). If that fails, check for a non-existent symlink. If
  659. * fail, return the errno from the stat call.
  660. */
  661. if (ISSET(FTS_LOGICAL) || follow) {
  662. if (stat(p->fts_accpath, &p->fts_statb)) {
  663. saved_errno = errno;
  664. if (!lstat(p->fts_accpath, &p->fts_statb)) {
  665. errno = 0;
  666. return(FTS_SLNONE);
  667. }
  668. errno = saved_errno;
  669. bzero(&p->fts_statb, sizeof(struct stat));
  670. return(FTS_NS);
  671. }
  672. } else if (lstat(p->fts_accpath, &p->fts_statb)) {
  673. bzero(&p->fts_statb, sizeof(struct stat));
  674. return(FTS_NS);
  675. }
  676. /*
  677. * Cycle detection is done as soon as we find a directory. Detection
  678. * is by brute force; if the tree gets deep enough or the number of
  679. * symbolic links to directories high enough something faster might
  680. * be worthwhile.
  681. */
  682. if (S_ISDIR(p->fts_statb.st_mode)) {
  683. register FTSENT *t;
  684. register dev_t dev;
  685. register ino_t ino;
  686. dev = p->fts_statb.st_dev;
  687. ino = p->fts_statb.st_ino;
  688. for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
  689. t = t->fts_parent)
  690. if (ino == t->fts_statb.st_ino &&
  691. dev == t->fts_statb.st_dev) {
  692. sp->fts_savelink = p->fts_link;
  693. p->fts_link = t;
  694. return(FTS_DC);
  695. }
  696. return(FTS_D);
  697. }
  698. #ifndef _POSIX_SOURCE
  699. if (S_ISLNK(p->fts_statb.st_mode))
  700. return(FTS_SL);
  701. #endif
  702. if (S_ISREG(p->fts_statb.st_mode))
  703. return(FTS_F);
  704. return(FTS_DEFAULT);
  705. }
  706. #define R(type, nelem, ptr) \
  707. (type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
  708. static FTSENT *
  709. fts_sort(sp, head, nitems)
  710. FTS *sp;
  711. FTSENT *head;
  712. register int nitems;
  713. {
  714. register FTSENT **ap, *p;
  715. /*
  716. * Construct an array of pointers to the structures and call qsort(3).
  717. * Reassemble the array in the order returned by qsort. If unable to
  718. * sort for memory reasons, return the directory entries in their
  719. * current order. Allocate enough space for the current needs plus
  720. * 40 so we don't realloc one entry at a time.
  721. */
  722. if (nitems > sp->fts_nitems) {
  723. sp->fts_nitems = nitems + 40;
  724. if (!(sp->fts_array =
  725. R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
  726. sp->fts_nitems = 0;
  727. return(head);
  728. }
  729. }
  730. for (ap = sp->fts_array, p = head; p; p = p->fts_link)
  731. *ap++ = p;
  732. qsort(sp->fts_array, nitems, sizeof(FTSENT *), (int (*)(const void *, const void *))sp->fts_compar);
  733. for (head = *(ap = sp->fts_array); --nitems; ++ap)
  734. ap[0]->fts_link = ap[1];
  735. ap[0]->fts_link = NULL;
  736. return(head);
  737. }
  738. static FTSENT *
  739. fts_alloc(sp, name, len)
  740. FTS *sp;
  741. char *name;
  742. register int len;
  743. {
  744. register FTSENT *p;
  745. /*
  746. * Variable sized structures; the name is the last element so
  747. * we allocate enough extra space after the structure to store
  748. * it.
  749. */
  750. if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
  751. return(NULL);
  752. bcopy(name, p->fts_name, len + 1);
  753. p->fts_namelen = (short)len;
  754. p->fts_path = sp->fts_path;
  755. p->fts_instr = FTS_NOINSTR;
  756. p->fts_cderr = 0;
  757. p->fts_number = 0;
  758. p->fts_pointer = NULL;
  759. return(p);
  760. }
  761. static void
  762. fts_lfree(head)
  763. register FTSENT *head;
  764. {
  765. register FTSENT *p;
  766. /* Free a linked list of structures. */
  767. while (p = head) {
  768. head = head->fts_link;
  769. free(p);
  770. }
  771. }
  772. /*
  773. * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
  774. * work on any tree. Most systems will allow creation of paths much longer
  775. * than MAXPATHLEN, even though the kernel won't resolve them. Add an extra
  776. * 128 bytes to the requested size so that we don't realloc the path 2 bytes
  777. * at a time.
  778. */
  779. static char *
  780. fts_path(sp, size)
  781. FTS *sp;
  782. int size;
  783. {
  784. sp->fts_pathlen += size + 128;
  785. return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
  786. }