xref: /csrg-svn/lib/libc/gen/fts.c (revision 47404)
142273Sbostic /*-
242273Sbostic  * Copyright (c) 1990 The Regents of the University of California.
339800Sbostic  * All rights reserved.
439800Sbostic  *
542273Sbostic  * %sccs.include.redist.c%
639800Sbostic  */
739800Sbostic 
839800Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*47404Sbostic static char sccsid[] = "@(#)fts.c	5.18 (Berkeley) 03/14/91";
1039800Sbostic #endif /* LIBC_SCCS and not lint */
1139800Sbostic 
1247194Sbostic #include <sys/cdefs.h>
1339800Sbostic #include <sys/param.h>
1439800Sbostic #include <sys/stat.h>
1542299Sbostic #include <fcntl.h>
1639800Sbostic #include <dirent.h>
1739800Sbostic #include <errno.h>
1847194Sbostic #include "fts.h"
1947194Sbostic #include <stdlib.h>
2042273Sbostic #include <string.h>
2147155Sbostic #include <unistd.h>
2239800Sbostic 
2347194Sbostic static FTSENT *fts_alloc(), *fts_build(), *fts_sort();
24*47404Sbostic static void fts_load(), fts_lfree();
2547155Sbostic static u_short fts_stat();
2647155Sbostic static char *fts_path();
2739800Sbostic 
2845600Sbostic #define	ISSET(opt)	(sp->fts_options & opt)
2945600Sbostic #define	SET(opt)	(sp->fts_options |= opt)
3039800Sbostic 
3145600Sbostic #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
3245600Sbostic #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
3339800Sbostic 
3442299Sbostic /* fts_build flags */
3545600Sbostic #define	BCHILD		1		/* from fts_children */
3645600Sbostic #define	BREAD		2		/* from fts_read */
3742299Sbostic 
3839800Sbostic FTS *
3945600Sbostic fts_open(argv, options, compar)
4047155Sbostic 	char * const *argv;
4139800Sbostic 	register int options;
4247194Sbostic 	int (*compar)();
4339800Sbostic {
4439800Sbostic 	register FTS *sp;
4539800Sbostic 	register FTSENT *p, *root;
4639800Sbostic 	register int nitems, maxlen;
4739800Sbostic 	FTSENT *parent, *tmp;
4847194Sbostic 	int len;
4939800Sbostic 
5045600Sbostic 	/* Allocate/initialize the stream */
5145600Sbostic 	if (!(sp = (FTS *)malloc((u_int)sizeof(FTS))))
5239800Sbostic 		return(NULL);
5339800Sbostic 	bzero(sp, sizeof(FTS));
5442273Sbostic 	sp->fts_compar = compar;
5542273Sbostic 	sp->fts_options = options;
5639800Sbostic 
5745600Sbostic 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
5845600Sbostic 	if (ISSET(FTS_LOGICAL))
5945600Sbostic 		SET(FTS_NOCHDIR);
6045600Sbostic 
6145600Sbostic 	/* Allocate/initialize root's parent. */
6245600Sbostic 	if (!(parent = fts_alloc(sp, "", 0)))
6339800Sbostic 		goto mem1;
6447212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
6539800Sbostic 
6645600Sbostic 	/* Allocate/initialize root(s). */
6743070Sbostic 	maxlen = -1;
6843070Sbostic 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
6947194Sbostic 		if (!(len = strlen(*argv))) {
7047194Sbostic 			errno = ENOENT;
7143070Sbostic 			goto mem2;
7247194Sbostic 		}
7347194Sbostic 		if (maxlen < len)
7447194Sbostic 			maxlen = len;
7547194Sbostic 		p = fts_alloc(sp, *argv, len);
7643070Sbostic 		/*
7745600Sbostic 		 * If comparison routine supplied, traverse in sorted
7843070Sbostic 		 * order; otherwise traverse in the order specified.
7943070Sbostic 		 */
8043070Sbostic 		if (compar) {
8143070Sbostic 			p->fts_link = root;
8243070Sbostic 			root = p;
8343070Sbostic 			p->fts_accpath = p->fts_name;
8443075Sbostic 			if (!(options & FTS_NOSTAT))
8545600Sbostic 				p->fts_info = fts_stat(sp, p, 0);
8643070Sbostic 		} else {
8743070Sbostic 			p->fts_link = NULL;
8843070Sbostic 			if (!root)
8943070Sbostic 				tmp = root = p;
9043070Sbostic 			else {
9143070Sbostic 				tmp->fts_link = p;
9243070Sbostic 				tmp = p;
9339800Sbostic 			}
9439800Sbostic 		}
9547212Sbostic 		p->fts_level = FTS_ROOTLEVEL;
9643070Sbostic 		p->fts_parent = parent;
9739800Sbostic 	}
9843070Sbostic 	if (compar && nitems > 1)
9945600Sbostic 		root = fts_sort(sp, root, nitems);
10039800Sbostic 
10142281Sbostic 	/*
10245600Sbostic 	 * Allocate a dummy pointer and make fts_read think that we've just
10342281Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_NS
10442281Sbostic 	 * so that everything about the "current" node is ignored.
10542281Sbostic 	 */
10645600Sbostic 	if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
10742281Sbostic 		goto mem2;
10842281Sbostic 	sp->fts_cur->fts_link = root;
10942281Sbostic 	sp->fts_cur->fts_info = FTS_NS;
11042281Sbostic 
11145600Sbostic 	/* Start out with at least 1K+ of path space. */
11245600Sbostic 	if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
11342281Sbostic 		goto mem3;
11439800Sbostic 
11539800Sbostic 	/*
11645600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
11742299Sbostic 	 * that we can get back here; this could be avoided for some paths,
11842299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
11942299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
12042299Sbostic 	 * descriptor we run anyway, just more slowly.
12139800Sbostic 	 */
12247194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
12345600Sbostic 		SET(FTS_NOCHDIR);
12439800Sbostic 
12539800Sbostic 	return(sp);
12639800Sbostic 
12747194Sbostic mem3:	free(sp->fts_cur);
12839800Sbostic mem2:	fts_lfree(root);
12947194Sbostic 	free(parent);
13047194Sbostic mem1:	free(sp);
13139800Sbostic 	return(NULL);
13239800Sbostic }
13339800Sbostic 
134*47404Sbostic static void
13545600Sbostic fts_load(sp, p)
13645600Sbostic 	FTS *sp;
13739800Sbostic 	register FTSENT *p;
13839800Sbostic {
13939800Sbostic 	register int len;
14039800Sbostic 	register char *cp;
14139800Sbostic 
14239800Sbostic 	/*
14347220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
14447220Sbostic 	 * actually enter the directory until after the preorder visit, set
14547220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
14647220Sbostic 	 * place and the user can access the first node.
14739800Sbostic 	 */
14842273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
14945600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
15042273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
15139800Sbostic 		len = strlen(++cp);
15242273Sbostic 		bcopy(cp, p->fts_name, len + 1);
15342273Sbostic 		p->fts_namelen = len;
15439800Sbostic 	}
15545600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
15647194Sbostic 
15747403Sbostic 	p->fts_info = fts_stat(sp, p, 0);
15847194Sbostic 	sp->rdev = p->fts_statb.st_dev;
15939800Sbostic }
16039800Sbostic 
16145600Sbostic fts_close(sp)
16239800Sbostic 	FTS *sp;
16339800Sbostic {
16439800Sbostic 	register FTSENT *freep, *p;
16539800Sbostic 	int saved_errno;
16639800Sbostic 
16742281Sbostic 	if (sp->fts_cur) {
16842281Sbostic 		/*
16945600Sbostic 		 * This still works if we haven't read anything -- the dummy
17042281Sbostic 		 * structure points to the root list, so we step through to
17142281Sbostic 		 * the end of the root list which has a valid parent pointer.
17242281Sbostic 		 */
17347212Sbostic 		for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
17442281Sbostic 			freep = p;
17542281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
17647194Sbostic 			free(freep);
17739800Sbostic 		}
17847194Sbostic 		free(p);
17942281Sbostic 	}
18039800Sbostic 
18145600Sbostic 	/* Free up child linked list, sort array, path buffer. */
18242273Sbostic 	if (sp->fts_child)
18342273Sbostic 		fts_lfree(sp->fts_child);
18442273Sbostic 	if (sp->fts_array)
18547194Sbostic 		free(sp->fts_array);
18647194Sbostic 	free(sp->fts_path);
18739800Sbostic 
18847194Sbostic 	/* Return to original directory, save errno if necessary. */
18945600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
19047194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
19147194Sbostic 		(void)close(sp->fts_rfd);
19239800Sbostic 	}
19339800Sbostic 
19445600Sbostic 	/* Free up the stream pointer. */
19547194Sbostic 	free(sp);
19639800Sbostic 
19745600Sbostic 	/* Set errno and return. */
19845600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
19939800Sbostic 		errno = saved_errno;
20039800Sbostic 		return(-1);
20139800Sbostic 	}
20239800Sbostic 	return(0);
20339800Sbostic }
20439800Sbostic 
20547194Sbostic /*
20647194Sbostic  * Special case a root of "/" so that slashes aren't appended causing
20747194Sbostic  * paths to be written as "//foo".
20847194Sbostic  */
20947194Sbostic #define	NAPPEND(p) \
21047212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
21147194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
21247194Sbostic 
21339800Sbostic FTSENT *
21445600Sbostic fts_read(sp)
21539800Sbostic 	register FTS *sp;
21639800Sbostic {
21742299Sbostic 	register FTSENT *p, *tmp;
21839800Sbostic 	register int instr;
21947194Sbostic 	register char *t;
22039800Sbostic 
22145600Sbostic 	/* If finished or unrecoverable error, return NULL. */
22247194Sbostic 	if (!sp->fts_cur || ISSET(FTS_STOP))
22339800Sbostic 		return(NULL);
22439800Sbostic 
22545600Sbostic 	/* Set current node pointer. */
22642273Sbostic 	p = sp->fts_cur;
22739800Sbostic 
22845600Sbostic 	/* Save and zero out user instructions. */
22942273Sbostic 	instr = p->fts_instr;
23047194Sbostic 	p->fts_instr = FTS_NOINSTR;
23139800Sbostic 
23245600Sbostic 	/* If used fts_link pointer for cycle detection, restore it. */
23342273Sbostic 	if (sp->fts_savelink) {
23442273Sbostic 		p->fts_link = sp->fts_savelink;
23542273Sbostic 		sp->fts_savelink = NULL;
23639800Sbostic 	}
23739800Sbostic 
23847194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
23939800Sbostic 	if (instr == FTS_AGAIN) {
24045600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
24139800Sbostic 		return(p);
24239800Sbostic 	}
24339800Sbostic 
24447194Sbostic 	/*
24547194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
24647194Sbostic 	 * SLNONE and recover.
24747194Sbostic 	 */
24847194Sbostic 	if (instr == FTS_FOLLOW &&
24947194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
25045600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
25142299Sbostic 		return(p);
25242299Sbostic 	}
25342299Sbostic 
25445600Sbostic 	/* Directory in pre-order. */
25542299Sbostic 	if (p->fts_info == FTS_D) {
25645600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
25747194Sbostic 		if (instr == FTS_SKIP ||
25847194Sbostic 		    ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
25942273Sbostic 			if (sp->fts_child) {
26042273Sbostic 				fts_lfree(sp->fts_child);
26142273Sbostic 				sp->fts_child = NULL;
26239800Sbostic 			}
26342301Sbostic 			p->fts_info = FTS_DP;
26442301Sbostic 			return(p);
26542299Sbostic 		}
26642299Sbostic 
26747194Sbostic 		/*
26847194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
26947194Sbostic 		 * the read fails for any reason, or the directory is empty,
27047194Sbostic 		 * the fts_info field of the current node is set by fts_build.
27147220Sbostic 		 * If have already read and now fail to chdir, whack the list
27247220Sbostic 		 * to make the names come out right, and set the parent state
27347220Sbostic 		 * so the application will eventually get an error condition.
27447220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
27547220Sbostic 		 * at the root node -- if so, we have to get back or the root
27647220Sbostic 		 * node may be inaccessible.
27747194Sbostic 		 */
27847194Sbostic 		if (sp->fts_child) {
27942299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
28047194Sbostic 				p->fts_parent->fts_cderr = errno;
28147194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
28247194Sbostic 					p->fts_accpath =
28347194Sbostic 					    p->fts_parent->fts_accpath;
28442299Sbostic 			}
28547220Sbostic 		} else if (!(sp->fts_child = fts_build(sp, BREAD))) {
28647220Sbostic 			if ISSET(FTS_STOP)
28747220Sbostic 				return(NULL);
28847220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
28947220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
29047220Sbostic 				SET(FTS_STOP);
29147220Sbostic 				return(NULL);
29247220Sbostic 			}
29347220Sbostic 			return(p);
29447220Sbostic 		}
29547194Sbostic 		p = sp->fts_child;
29642299Sbostic 		sp->fts_child = NULL;
29747194Sbostic 		goto name;
29839800Sbostic 	}
29939800Sbostic 
30045600Sbostic 	/* Move to next node on this level. */
30142299Sbostic next:	tmp = p;
30242299Sbostic 	if (p = p->fts_link) {
30347194Sbostic 		free(tmp);
30447194Sbostic 
30547194Sbostic 		/* If reached the top, load the paths for the next root. */
30647212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
307*47404Sbostic 			fts_load(sp, p);
30847194Sbostic 			return(sp->fts_cur = p);
30947194Sbostic 		}
31042299Sbostic 
31147194Sbostic 		/* User may have called fts_set on the node. */
31247194Sbostic 		if (p->fts_instr == FTS_SKIP)
31347194Sbostic 			goto next;
31447194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
31547194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
31647194Sbostic 			p->fts_instr = FTS_NOINSTR;
31747194Sbostic 		}
31842299Sbostic 
31947194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
32047194Sbostic 		*t++ = '/';
32147194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
32242273Sbostic 		return(sp->fts_cur = p);
32339800Sbostic 	}
32439800Sbostic 
32547220Sbostic 	/* Move up to the parent node. */
32642299Sbostic 	p = tmp->fts_parent;
32747194Sbostic 	free(tmp);
32842299Sbostic 
32947212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
33039800Sbostic 		/*
33145600Sbostic 		 * Done; free everything up and set errno to 0 so the user
33239800Sbostic 		 * can distinguish between error and EOF.
33339800Sbostic 		 */
33447194Sbostic 		free(p);
33539800Sbostic 		errno = 0;
33642273Sbostic 		return(sp->fts_cur = NULL);
33739800Sbostic 	}
33839800Sbostic 
33942273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
34047194Sbostic 
34147194Sbostic 	/*
34247220Sbostic 	 * Cd back up to the parent directory.  If at a root node, have to cd
34347220Sbostic 	 * back to the original place, otherwise may not be able to access the
34447220Sbostic 	 * original node on post-order.
34547194Sbostic 	 */
34647220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
34747220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
34847220Sbostic 			SET(FTS_STOP);
34947220Sbostic 			return(NULL);
35047220Sbostic 		}
35147220Sbostic 	}
35247220Sbostic 	else if (CHDIR(sp, "..")) {
35347220Sbostic 		SET(FTS_STOP);
35447220Sbostic 		return(NULL);
35547220Sbostic 	}
35647220Sbostic 
35747220Sbostic 	/*
35847220Sbostic 	 * If had a chdir error when trying to get into the directory, set the
35947220Sbostic 	 * info field to reflect this, and restore errno.  The error indicator
36047220Sbostic 	 * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
36147220Sbostic 	 * works.
36247220Sbostic 	 */
36347194Sbostic 	if (p->fts_cderr) {
36447194Sbostic 		errno = p->fts_cderr;
36547194Sbostic 		p->fts_cderr = 0;
36642273Sbostic 		p->fts_info = FTS_ERR;
36747220Sbostic 	} else
36842273Sbostic 		p->fts_info = FTS_DP;
36942273Sbostic 	return(sp->fts_cur = p);
37039800Sbostic }
37139800Sbostic 
37239800Sbostic /*
37347220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
37439800Sbostic  * implementation; it would be necessary if anyone wanted to add global
37545600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
37645600Sbostic  * reasons.
37739800Sbostic  */
37839800Sbostic /* ARGSUSED */
37945600Sbostic fts_set(sp, p, instr)
38039800Sbostic 	FTS *sp;
38139800Sbostic 	FTSENT *p;
38239800Sbostic 	int instr;
38339800Sbostic {
38442273Sbostic 	p->fts_instr = instr;
38539800Sbostic 	return(0);
38639800Sbostic }
38739800Sbostic 
38839800Sbostic FTSENT *
38945600Sbostic fts_children(sp)
39039800Sbostic 	register FTS *sp;
39139800Sbostic {
39242299Sbostic 	register FTSENT *p;
39342299Sbostic 	int fd;
39442299Sbostic 
39545600Sbostic 	/* Set current node pointer. */
39645600Sbostic 	p = sp->fts_cur;
39745600Sbostic 
39839800Sbostic 	/*
39945600Sbostic 	 * Set errno to 0 so that user can tell the difference between an
40047194Sbostic 	 * error and a directory without entries.  If not a directory being
40147194Sbostic 	 * visited in *pre-order*, or we've already had fatal errors, return
40247194Sbostic 	 * immediately.
40339800Sbostic 	 */
40439800Sbostic 	errno = 0;
40547194Sbostic 	if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
40639800Sbostic 		return(NULL);
40745600Sbostic 
40845600Sbostic 	/* Free up any previous child list. */
40942273Sbostic 	if (sp->fts_child)
41042273Sbostic 		fts_lfree(sp->fts_child);
41142299Sbostic 
41242299Sbostic 	/*
41345600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
41447194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
41547194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
41647194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
41747194Sbostic 	 * fts_read will work.
41842299Sbostic 	 */
41947212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
42045600Sbostic 	    ISSET(FTS_NOCHDIR))
42142299Sbostic 		return(sp->fts_child = fts_build(sp, BCHILD));
42242299Sbostic 
42342299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
42442299Sbostic 		return(NULL);
42542299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
42642299Sbostic 	if (fchdir(fd))
42742299Sbostic 		return(NULL);
42842299Sbostic 	(void)close(fd);
42942299Sbostic 	return(sp->fts_child);
43039800Sbostic }
43139800Sbostic 
43247194Sbostic /*
43347194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
43447194Sbostic  * idea is to build the linked list of entries that are used by fts_children
43547194Sbostic  * and fts_read.  There are lots of special cases.
43647194Sbostic  *
43747194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
43847194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
43947194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
44047194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
44147194Sbostic  * directories and for any nodes after the directories in the parent node have
44247194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
44347194Sbostic  */
44439800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
44539800Sbostic 
44647155Sbostic static FTSENT *
44742299Sbostic fts_build(sp, type)
44839800Sbostic 	register FTS *sp;
44942299Sbostic 	int type;
45039800Sbostic {
45139800Sbostic 	register struct dirent *dp;
45239800Sbostic 	register FTSENT *p, *head;
45339800Sbostic 	register int nitems;
45447194Sbostic 	FTSENT *cur;
45539800Sbostic 	DIR *dirp;
45647194Sbostic 	int cderr, descend, len, level, maxlen, nlinks, saved_errno;
45739800Sbostic 	char *cp;
45839800Sbostic 
45945600Sbostic 	/* Set current node pointer. */
46047194Sbostic 	cur = sp->fts_cur;
46145600Sbostic 
46247194Sbostic 	/*
46347194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
46447194Sbostic 	 * If being called from fts_read, set the fts_info field.
46547194Sbostic 	 */
46647194Sbostic 	if (!(dirp = opendir(cur->fts_accpath))) {
46747194Sbostic 		if (type == BREAD)
46847194Sbostic 			cur->fts_info = FTS_DNR;
46939800Sbostic 		return(NULL);
47039800Sbostic 	}
47139800Sbostic 
47239800Sbostic 	/*
47347194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
47447194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
47547194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
47639800Sbostic 	 */
47742273Sbostic 	nlinks =
47845600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
47947194Sbostic 	    cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
48039800Sbostic 
48147194Sbostic 	/*
48247194Sbostic 	 * If we're going to need to stat anything or we want to descend
48347194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
48447194Sbostic 	 * We won't be able to stat anything, but we can still return the
48547194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
48647194Sbostic 	 * chdir into the directory, it will have to return different path
48747194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
48847194Sbostic 	 * has already been visited in pre-order, have to wait until the
48947194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
49047194Sbostic 	 * If a program needed sorted entries or stat information, they had
49147194Sbostic 	 * better be checking FTS_NS on the returned nodes.
49247194Sbostic 	 */
49345600Sbostic 	if (nlinks || type == BREAD)
49447194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
49547194Sbostic 			if (type == BREAD)
49647194Sbostic 				cur->fts_cderr = errno;
49747194Sbostic 			descend = nlinks = 0;
49847194Sbostic 			cderr = 1;
49947194Sbostic 		} else {
50045600Sbostic 			descend = 1;
50147194Sbostic 			cderr = 0;
50239962Sbostic 		}
50347194Sbostic 	else
50447194Sbostic 		descend = 0;
50539962Sbostic 
50647194Sbostic 	/*
50747194Sbostic 	 * Figure out the max file name length that can be stored in the
50847194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
50947194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
51047194Sbostic 	 * could do them in fts_read before returning the path, but it's a
51147194Sbostic 	 * lot easier here since the length is part of the dirent structure.
51247194Sbostic 	 *
51347194Sbostic 	 * If not changing directories set a pointer so that we can just
51447194Sbostic 	 * append each new name into the path.
51547194Sbostic 	 */
51647194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
51747194Sbostic 	len = NAPPEND(cur);
51847194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
51947194Sbostic 		cp = sp->fts_path + len;
52047194Sbostic 		*cp++ = '/';
52147194Sbostic 	}
52240939Sbostic 
52347194Sbostic 	level = cur->fts_level + 1;
52440939Sbostic 
52547194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
52639800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
52747194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
52839800Sbostic 			continue;
52939800Sbostic 
53047194Sbostic 		if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
53139800Sbostic 			goto mem1;
53239800Sbostic 		if (dp->d_namlen > maxlen) {
53345600Sbostic 			if (!fts_path(sp, (int)dp->d_namlen)) {
53447194Sbostic 				/*
53547194Sbostic 				 * No more memory for path or structures.  Save
53647194Sbostic 				 * errno, free up the current structure and the
53747194Sbostic 				 * structures already allocated.
53847194Sbostic 				 */
53947194Sbostic mem1:				saved_errno = errno;
54047194Sbostic 				if (p)
54147194Sbostic 					free(p);
54247194Sbostic 				fts_lfree(head);
54347194Sbostic 				(void)closedir(dirp);
54439800Sbostic 				errno = saved_errno;
54547194Sbostic 				cur->fts_info = FTS_ERR;
54647194Sbostic 				SET(FTS_STOP);
54739800Sbostic 				return(NULL);
54839800Sbostic 			}
54942273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
55039800Sbostic 		}
55139800Sbostic 
55242273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
55342273Sbostic 		p->fts_parent = sp->fts_cur;
55442273Sbostic 		p->fts_level = level;
55539800Sbostic 
55639800Sbostic 		if (nlinks) {
55747194Sbostic 			/* Build a file name for fts_stat to stat. */
55847194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
55947194Sbostic 				p->fts_accpath = p->fts_path;
56042273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
56147194Sbostic 			} else
56247194Sbostic 				p->fts_accpath = p->fts_name;
56345600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
56447194Sbostic 			if (nlinks > 0 && p->fts_info == FTS_D)
56539800Sbostic 				--nlinks;
56647194Sbostic 		} else if (cderr) {
56747194Sbostic 			p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
56847194Sbostic 			p->fts_accpath = cur->fts_accpath;
56947194Sbostic 		} else {
57047194Sbostic 			p->fts_accpath =
57147194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
57247194Sbostic 			p->fts_info = FTS_NSOK;
57347194Sbostic 		}
57439800Sbostic 
57542273Sbostic 		p->fts_link = head;
57639800Sbostic 		head = p;
57739800Sbostic 		++nitems;
57839800Sbostic 	}
57939800Sbostic 	(void)closedir(dirp);
58039800Sbostic 
58147194Sbostic 	/*
58247194Sbostic 	 * If not changing directories, reset the path back to original
58347194Sbostic 	 * state.
58447194Sbostic 	 */
58547194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
58647194Sbostic 		if (cp - 1 > sp->fts_path)
58747194Sbostic 			--cp;
58847194Sbostic 		*cp = '\0';
58947194Sbostic 	}
59042299Sbostic 
59142299Sbostic 	/*
59247194Sbostic 	 * If descended after called from fts_children or called from
59347194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
59447194Sbostic 	 * back, we're done.
59542299Sbostic 	 */
59642299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
59747194Sbostic 		cur->fts_info = FTS_ERR;
59847194Sbostic 		SET(FTS_STOP);
59939962Sbostic 		return(NULL);
60039962Sbostic 	}
60139962Sbostic 
60247194Sbostic 	/* If we didn't find anything, just do the post-order visit */
60339800Sbostic 	if (!nitems) {
60442299Sbostic 		if (type == BREAD)
60547194Sbostic 			cur->fts_info = FTS_DP;
60639800Sbostic 		return(NULL);
60739800Sbostic 	}
60839800Sbostic 
60947194Sbostic 	/* Sort the entries. */
61042273Sbostic 	if (sp->fts_compar && nitems > 1)
61145600Sbostic 		head = fts_sort(sp, head, nitems);
61239800Sbostic 	return(head);
61339800Sbostic }
61439800Sbostic 
61545600Sbostic static u_short
61645600Sbostic fts_stat(sp, p, follow)
61745600Sbostic 	FTS *sp;
61839800Sbostic 	register FTSENT *p;
61945600Sbostic 	int follow;
62039800Sbostic {
62147194Sbostic 	int saved_errno;
62247194Sbostic 
62339800Sbostic 	/*
62445600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
62547194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
62647194Sbostic 	 * fail, return the errno from the stat call.
62739800Sbostic 	 */
62845600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
62945600Sbostic 		if (stat(p->fts_accpath, &p->fts_statb)) {
63047194Sbostic 			saved_errno = errno;
63147194Sbostic 			if (!lstat(p->fts_accpath, &p->fts_statb)) {
63247194Sbostic 				errno = 0;
63345600Sbostic 				return(FTS_SLNONE);
63447194Sbostic 			}
63547194Sbostic 			errno = saved_errno;
63647194Sbostic 			bzero(&p->fts_statb, sizeof(struct stat));
63747194Sbostic 			return(FTS_NS);
63845600Sbostic 		}
63945600Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb)) {
64047194Sbostic 		bzero(&p->fts_statb, sizeof(struct stat));
64145600Sbostic 		return(FTS_NS);
64245600Sbostic 	}
64339800Sbostic 
64447194Sbostic 	/*
64547194Sbostic 	 * Cycle detection is done as soon as we find a directory.  Detection
64647194Sbostic 	 * is by brute force; if the tree gets deep enough or the number of
64747194Sbostic 	 * symbolic links to directories high enough something faster might
64847194Sbostic 	 * be worthwhile.
64947194Sbostic 	 */
65047194Sbostic 	if (S_ISDIR(p->fts_statb.st_mode)) {
65147194Sbostic 		register FTSENT *t;
65247194Sbostic 		register dev_t dev;
65347194Sbostic 		register ino_t ino;
65447194Sbostic 
65547194Sbostic 		dev = p->fts_statb.st_dev;
65647194Sbostic 		ino = p->fts_statb.st_ino;
65747212Sbostic 		for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
65847194Sbostic 		    t = t->fts_parent)
65947194Sbostic 			if (ino == t->fts_statb.st_ino &&
66047194Sbostic 			    dev == t->fts_statb.st_dev) {
66147194Sbostic 				sp->fts_savelink = p->fts_link;
66247194Sbostic 				p->fts_link = t;
66347194Sbostic 				return(FTS_DC);
66447194Sbostic 			}
66539800Sbostic 		return(FTS_D);
66647194Sbostic 	}
66745600Sbostic 	if (S_ISLNK(p->fts_statb.st_mode))
66839800Sbostic 		return(FTS_SL);
66945600Sbostic 	if (S_ISREG(p->fts_statb.st_mode))
67039800Sbostic 		return(FTS_F);
67140939Sbostic 	return(FTS_DEFAULT);
67239800Sbostic }
67339800Sbostic 
67439800Sbostic #define	R(type, nelem, ptr) \
67542299Sbostic 	(type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
67639800Sbostic 
67739800Sbostic static FTSENT *
67845600Sbostic fts_sort(sp, head, nitems)
67945600Sbostic 	FTS *sp;
68039800Sbostic 	FTSENT *head;
68139800Sbostic 	register int nitems;
68239800Sbostic {
68339800Sbostic 	register FTSENT **ap, *p;
68439800Sbostic 
68539800Sbostic 	/*
68645600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
68739800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
68839800Sbostic 	 * sort for memory reasons, return the directory entries in their
68939800Sbostic 	 * current order.  Allocate enough space for the current needs plus
69039800Sbostic 	 * 40 so we don't realloc one entry at a time.
69139800Sbostic 	 */
69245600Sbostic 	if (nitems > sp->fts_nitems) {
69345600Sbostic 		sp->fts_nitems = nitems + 40;
69445600Sbostic 		if (!(sp->fts_array =
69545600Sbostic 		    R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
69645600Sbostic 			sp->fts_nitems = 0;
69739800Sbostic 			return(head);
69839800Sbostic 		}
69939800Sbostic 	}
70045600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
70139800Sbostic 		*ap++ = p;
70245600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
70345600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
70442273Sbostic 		ap[0]->fts_link = ap[1];
70542273Sbostic 	ap[0]->fts_link = NULL;
70639800Sbostic 	return(head);
70739800Sbostic }
70839800Sbostic 
70939800Sbostic static FTSENT *
71045600Sbostic fts_alloc(sp, name, len)
71145600Sbostic 	FTS *sp;
71239800Sbostic 	char *name;
71339800Sbostic 	register int len;
71439800Sbostic {
71539800Sbostic 	register FTSENT *p;
71639800Sbostic 
71739800Sbostic 	/*
71845600Sbostic 	 * Variable sized structures; the name is the last element so
71947194Sbostic 	 * we allocate enough extra space after the structure to store
72047194Sbostic 	 * it.
72139800Sbostic 	 */
72242299Sbostic 	if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
72339800Sbostic 		return(NULL);
72442273Sbostic 	bcopy(name, p->fts_name, len + 1);
72542273Sbostic 	p->fts_namelen = len;
72645600Sbostic 	p->fts_path = sp->fts_path;
72747194Sbostic 	p->fts_instr = FTS_NOINSTR;
72847194Sbostic 	p->fts_cderr = 0;
72945600Sbostic 	p->fts_number = 0;
73045600Sbostic 	p->fts_pointer = NULL;
73139800Sbostic 	return(p);
73239800Sbostic }
73339800Sbostic 
73445600Sbostic static void
73539800Sbostic fts_lfree(head)
73639800Sbostic 	register FTSENT *head;
73739800Sbostic {
73839800Sbostic 	register FTSENT *p;
73939800Sbostic 
74045600Sbostic 	/* Free a linked list of structures. */
74139800Sbostic 	while (p = head) {
74242273Sbostic 		head = head->fts_link;
74347194Sbostic 		free(p);
74439800Sbostic 	}
74539800Sbostic }
74639800Sbostic 
74739800Sbostic /*
74845600Sbostic  * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
74945600Sbostic  * work on any tree.  Most systems will allow creation of paths much longer
75045600Sbostic  * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
75145600Sbostic  * 128 bytes to the requested size so that we don't realloc the path 2 bytes
75245600Sbostic  * at a time.
75339800Sbostic  */
75442299Sbostic static char *
75545600Sbostic fts_path(sp, size)
75645600Sbostic 	FTS *sp;
75739800Sbostic 	int size;
75839800Sbostic {
75945600Sbostic 	sp->fts_pathlen += size + 128;
76047155Sbostic 	return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
76147155Sbostic }
762