xref: /csrg-svn/lib/libc/gen/fts.c (revision 47212)
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*47212Sbostic static char sccsid[] = "@(#)fts.c	5.15 (Berkeley) 03/11/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();
2447194Sbostic static void fts_lfree();
2547194Sbostic static int fts_load();
2647155Sbostic static u_short fts_stat();
2747155Sbostic static char *fts_path();
2839800Sbostic 
2945600Sbostic #define	ISSET(opt)	(sp->fts_options & opt)
3045600Sbostic #define	SET(opt)	(sp->fts_options |= opt)
3139800Sbostic 
3245600Sbostic #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
3345600Sbostic #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
3439800Sbostic 
3542299Sbostic /* fts_build flags */
3645600Sbostic #define	BCHILD		1		/* from fts_children */
3745600Sbostic #define	BREAD		2		/* from fts_read */
3842299Sbostic 
3939800Sbostic FTS *
4045600Sbostic fts_open(argv, options, compar)
4147155Sbostic 	char * const *argv;
4239800Sbostic 	register int options;
4347194Sbostic 	int (*compar)();
4439800Sbostic {
4539800Sbostic 	register FTS *sp;
4639800Sbostic 	register FTSENT *p, *root;
4739800Sbostic 	register int nitems, maxlen;
4839800Sbostic 	FTSENT *parent, *tmp;
4947194Sbostic 	int len;
5039800Sbostic 
5145600Sbostic 	/* Allocate/initialize the stream */
5245600Sbostic 	if (!(sp = (FTS *)malloc((u_int)sizeof(FTS))))
5339800Sbostic 		return(NULL);
5439800Sbostic 	bzero(sp, sizeof(FTS));
5542273Sbostic 	sp->fts_compar = compar;
5642273Sbostic 	sp->fts_options = options;
5739800Sbostic 
5845600Sbostic 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
5945600Sbostic 	if (ISSET(FTS_LOGICAL))
6045600Sbostic 		SET(FTS_NOCHDIR);
6145600Sbostic 
6245600Sbostic 	/* Allocate/initialize root's parent. */
6345600Sbostic 	if (!(parent = fts_alloc(sp, "", 0)))
6439800Sbostic 		goto mem1;
65*47212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
6639800Sbostic 
6745600Sbostic 	/* Allocate/initialize root(s). */
6843070Sbostic 	maxlen = -1;
6943070Sbostic 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
7047194Sbostic 		if (!(len = strlen(*argv))) {
7147194Sbostic 			errno = ENOENT;
7243070Sbostic 			goto mem2;
7347194Sbostic 		}
7447194Sbostic 		if (maxlen < len)
7547194Sbostic 			maxlen = len;
7647194Sbostic 		p = fts_alloc(sp, *argv, len);
7743070Sbostic 		/*
7845600Sbostic 		 * If comparison routine supplied, traverse in sorted
7943070Sbostic 		 * order; otherwise traverse in the order specified.
8043070Sbostic 		 */
8143070Sbostic 		if (compar) {
8243070Sbostic 			p->fts_link = root;
8343070Sbostic 			root = p;
8443070Sbostic 			p->fts_accpath = p->fts_name;
8543075Sbostic 			if (!(options & FTS_NOSTAT))
8645600Sbostic 				p->fts_info = fts_stat(sp, p, 0);
8743070Sbostic 		} else {
8843070Sbostic 			p->fts_link = NULL;
8943070Sbostic 			if (!root)
9043070Sbostic 				tmp = root = p;
9143070Sbostic 			else {
9243070Sbostic 				tmp->fts_link = p;
9343070Sbostic 				tmp = p;
9439800Sbostic 			}
9539800Sbostic 		}
96*47212Sbostic 		p->fts_level = FTS_ROOTLEVEL;
9743070Sbostic 		p->fts_parent = parent;
9839800Sbostic 	}
9943070Sbostic 	if (compar && nitems > 1)
10045600Sbostic 		root = fts_sort(sp, root, nitems);
10139800Sbostic 
10242281Sbostic 	/*
10345600Sbostic 	 * Allocate a dummy pointer and make fts_read think that we've just
10442281Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_NS
10542281Sbostic 	 * so that everything about the "current" node is ignored.
10642281Sbostic 	 */
10745600Sbostic 	if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
10842281Sbostic 		goto mem2;
10942281Sbostic 	sp->fts_cur->fts_link = root;
11042281Sbostic 	sp->fts_cur->fts_info = FTS_NS;
11142281Sbostic 
11245600Sbostic 	/* Start out with at least 1K+ of path space. */
11345600Sbostic 	if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
11442281Sbostic 		goto mem3;
11539800Sbostic 
11639800Sbostic 	/*
11745600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
11842299Sbostic 	 * that we can get back here; this could be avoided for some paths,
11942299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
12042299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
12142299Sbostic 	 * descriptor we run anyway, just more slowly.
12239800Sbostic 	 */
12347194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
12445600Sbostic 		SET(FTS_NOCHDIR);
12539800Sbostic 
12639800Sbostic 	return(sp);
12739800Sbostic 
12847194Sbostic mem3:	free(sp->fts_cur);
12939800Sbostic mem2:	fts_lfree(root);
13047194Sbostic 	free(parent);
13147194Sbostic mem1:	free(sp);
13239800Sbostic 	return(NULL);
13339800Sbostic }
13439800Sbostic 
13547194Sbostic static
13645600Sbostic fts_load(sp, p)
13745600Sbostic 	FTS *sp;
13839800Sbostic 	register FTSENT *p;
13939800Sbostic {
14047194Sbostic 	static int need_to_cd;
14139800Sbostic 	register int len;
14239800Sbostic 	register char *cp;
14339800Sbostic 
14439800Sbostic 	/*
14547194Sbostic 	 * If changing the root level node, load the stream structure for the
14647194Sbostic 	 * next traversal; set the fts_accpath field specially so the chdir
14747194Sbostic 	 * gets done to the right place and the user can access the first node.
14839800Sbostic 	 */
14942273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
15045600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
15142273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
15239800Sbostic 		len = strlen(++cp);
15342273Sbostic 		bcopy(cp, p->fts_name, len + 1);
15442273Sbostic 		p->fts_namelen = len;
15539800Sbostic 	}
15645600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
15747194Sbostic 
15847194Sbostic 	sp->rdev = p->fts_statb.st_dev;
15947194Sbostic 
16047194Sbostic 	/*
16147194Sbostic 	 * If not the first time, and it's not an absolute pathname, get back
16247194Sbostic 	 * to starting directory.  If that fails, we're dead.
16347194Sbostic 	 */
16447194Sbostic 	if (need_to_cd && p->fts_path[0] != '/' && FCHDIR(sp, sp->fts_rfd))
16547194Sbostic 		return(0);
16647194Sbostic 	need_to_cd = 1;
16747194Sbostic 
168*47212Sbostic 	p->fts_info = fts_stat(sp, p, 0);
16947194Sbostic 	return(1);
17039800Sbostic }
17139800Sbostic 
17245600Sbostic fts_close(sp)
17339800Sbostic 	FTS *sp;
17439800Sbostic {
17539800Sbostic 	register FTSENT *freep, *p;
17639800Sbostic 	int saved_errno;
17739800Sbostic 
17842281Sbostic 	if (sp->fts_cur) {
17942281Sbostic 		/*
18045600Sbostic 		 * This still works if we haven't read anything -- the dummy
18142281Sbostic 		 * structure points to the root list, so we step through to
18242281Sbostic 		 * the end of the root list which has a valid parent pointer.
18342281Sbostic 		 */
184*47212Sbostic 		for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
18542281Sbostic 			freep = p;
18642281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
18747194Sbostic 			free(freep);
18839800Sbostic 		}
18947194Sbostic 		free(p);
19042281Sbostic 	}
19139800Sbostic 
19245600Sbostic 	/* Free up child linked list, sort array, path buffer. */
19342273Sbostic 	if (sp->fts_child)
19442273Sbostic 		fts_lfree(sp->fts_child);
19542273Sbostic 	if (sp->fts_array)
19647194Sbostic 		free(sp->fts_array);
19747194Sbostic 	free(sp->fts_path);
19839800Sbostic 
19947194Sbostic 	/* Return to original directory, save errno if necessary. */
20045600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
20147194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
20247194Sbostic 		(void)close(sp->fts_rfd);
20339800Sbostic 	}
20439800Sbostic 
20545600Sbostic 	/* Free up the stream pointer. */
20647194Sbostic 	free(sp);
20739800Sbostic 
20845600Sbostic 	/* Set errno and return. */
20945600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
21039800Sbostic 		errno = saved_errno;
21139800Sbostic 		return(-1);
21239800Sbostic 	}
21339800Sbostic 	return(0);
21439800Sbostic }
21539800Sbostic 
21647194Sbostic /*
21747194Sbostic  * Special case a root of "/" so that slashes aren't appended causing
21847194Sbostic  * paths to be written as "//foo".
21947194Sbostic  */
22047194Sbostic #define	NAPPEND(p) \
221*47212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
22247194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
22347194Sbostic 
22439800Sbostic FTSENT *
22545600Sbostic fts_read(sp)
22639800Sbostic 	register FTS *sp;
22739800Sbostic {
22842299Sbostic 	register FTSENT *p, *tmp;
22939800Sbostic 	register int instr;
23047194Sbostic 	register char *t;
23139800Sbostic 
23245600Sbostic 	/* If finished or unrecoverable error, return NULL. */
23347194Sbostic 	if (!sp->fts_cur || ISSET(FTS_STOP))
23439800Sbostic 		return(NULL);
23539800Sbostic 
23645600Sbostic 	/* Set current node pointer. */
23742273Sbostic 	p = sp->fts_cur;
23839800Sbostic 
23945600Sbostic 	/* Save and zero out user instructions. */
24042273Sbostic 	instr = p->fts_instr;
24147194Sbostic 	p->fts_instr = FTS_NOINSTR;
24239800Sbostic 
24345600Sbostic 	/* If used fts_link pointer for cycle detection, restore it. */
24442273Sbostic 	if (sp->fts_savelink) {
24542273Sbostic 		p->fts_link = sp->fts_savelink;
24642273Sbostic 		sp->fts_savelink = NULL;
24739800Sbostic 	}
24839800Sbostic 
24947194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
25039800Sbostic 	if (instr == FTS_AGAIN) {
25145600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
25239800Sbostic 		return(p);
25339800Sbostic 	}
25439800Sbostic 
25547194Sbostic 	/*
25647194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
25747194Sbostic 	 * SLNONE and recover.
25847194Sbostic 	 */
25947194Sbostic 	if (instr == FTS_FOLLOW &&
26047194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
26145600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
26242299Sbostic 		return(p);
26342299Sbostic 	}
26442299Sbostic 
26545600Sbostic 	/* Directory in pre-order. */
26642299Sbostic 	if (p->fts_info == FTS_D) {
26745600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
26847194Sbostic 		if (instr == FTS_SKIP ||
26947194Sbostic 		    ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
27042273Sbostic 			if (sp->fts_child) {
27142273Sbostic 				fts_lfree(sp->fts_child);
27242273Sbostic 				sp->fts_child = NULL;
27339800Sbostic 			}
27442301Sbostic 			p->fts_info = FTS_DP;
27542301Sbostic 			return(p);
27642299Sbostic 		}
27742299Sbostic 
27847194Sbostic 		/*
27947194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
28047194Sbostic 		 * the read fails for any reason, or the directory is empty,
28147194Sbostic 		 * the fts_info field of the current node is set by fts_build.
28247194Sbostic 		 * If have already read and fail to chdir, do some quick
28347194Sbostic 		 * whacking to make the names come out right, and set the
28447194Sbostic 		 * parent state so the application will eventually get an
28547194Sbostic 		 * error condition.
28647194Sbostic 		 */
28747194Sbostic 		if (sp->fts_child) {
28842299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
28947194Sbostic 				p->fts_parent->fts_cderr = errno;
29047194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
29147194Sbostic 					p->fts_accpath =
29247194Sbostic 					    p->fts_parent->fts_accpath;
29342299Sbostic 			}
29447194Sbostic 		} else if (!(sp->fts_child = fts_build(sp, BREAD)))
29547194Sbostic 			return(ISSET(FTS_STOP) ? NULL : p);
29647194Sbostic 		p = sp->fts_child;
29742299Sbostic 		sp->fts_child = NULL;
29847194Sbostic 		goto name;
29939800Sbostic 	}
30039800Sbostic 
30145600Sbostic 	/* Move to next node on this level. */
30242299Sbostic next:	tmp = p;
30342299Sbostic 	if (p = p->fts_link) {
30447194Sbostic 		free(tmp);
30547194Sbostic 
30647194Sbostic 		/* If reached the top, load the paths for the next root. */
307*47212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
30847194Sbostic 			if (!fts_load(sp, p)) {
30947194Sbostic 				SET(FTS_STOP);
31047194Sbostic 				return(NULL);
31142299Sbostic 			}
31247194Sbostic 			return(sp->fts_cur = p);
31347194Sbostic 		}
31442299Sbostic 
31547194Sbostic 		/* User may have called fts_set on the node. */
31647194Sbostic 		if (p->fts_instr == FTS_SKIP)
31747194Sbostic 			goto next;
31847194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
31947194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
32047194Sbostic 			p->fts_instr = FTS_NOINSTR;
32147194Sbostic 		}
32242299Sbostic 
32347194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
32447194Sbostic 		*t++ = '/';
32547194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
32642273Sbostic 		return(sp->fts_cur = p);
32739800Sbostic 	}
32839800Sbostic 
32945600Sbostic 	/* Move to parent. */
33042299Sbostic 	p = tmp->fts_parent;
33147194Sbostic 	free(tmp);
33242299Sbostic 
333*47212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
33439800Sbostic 		/*
33545600Sbostic 		 * Done; free everything up and set errno to 0 so the user
33639800Sbostic 		 * can distinguish between error and EOF.
33739800Sbostic 		 */
33847194Sbostic 		free(p);
33939800Sbostic 		errno = 0;
34042273Sbostic 		return(sp->fts_cur = NULL);
34139800Sbostic 	}
34239800Sbostic 
34342273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
34447194Sbostic 
34547194Sbostic 	/*
34647194Sbostic 	 * If had a chdir error, set the info field to reflect this, and
34747194Sbostic 	 * restore errno.  The error indicator has to be reset to 0 so that
34847194Sbostic 	 * if the user does an FTS_AGAIN, it all works.  Can't cd up on
34947194Sbostic 	 * the post-order visit to the root node, otherwise will be in the
35047194Sbostic 	 * wrong directory.
35147194Sbostic 	 */
35247194Sbostic 	if (p->fts_cderr) {
35347194Sbostic 		errno = p->fts_cderr;
35447194Sbostic 		p->fts_cderr = 0;
35542273Sbostic 		p->fts_info = FTS_ERR;
35647194Sbostic 	} else {
357*47212Sbostic 		if (p->fts_level != FTS_ROOTLEVEL && CHDIR(sp, "..")) {
35847194Sbostic 			SET(FTS_STOP);
35947194Sbostic 			return(NULL);
36047194Sbostic 		}
36142273Sbostic 		p->fts_info = FTS_DP;
36247194Sbostic 	}
36342273Sbostic 	return(sp->fts_cur = p);
36439800Sbostic }
36539800Sbostic 
36639800Sbostic /*
36745600Sbostic  * fts_set takes the stream as an argument although it's not used in this
36839800Sbostic  * implementation; it would be necessary if anyone wanted to add global
36945600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
37045600Sbostic  * reasons.
37139800Sbostic  */
37239800Sbostic /* ARGSUSED */
37345600Sbostic fts_set(sp, p, instr)
37439800Sbostic 	FTS *sp;
37539800Sbostic 	FTSENT *p;
37639800Sbostic 	int instr;
37739800Sbostic {
37842273Sbostic 	p->fts_instr = instr;
37939800Sbostic 	return(0);
38039800Sbostic }
38139800Sbostic 
38239800Sbostic FTSENT *
38345600Sbostic fts_children(sp)
38439800Sbostic 	register FTS *sp;
38539800Sbostic {
38642299Sbostic 	register FTSENT *p;
38742299Sbostic 	int fd;
38842299Sbostic 
38945600Sbostic 	/* Set current node pointer. */
39045600Sbostic 	p = sp->fts_cur;
39145600Sbostic 
39239800Sbostic 	/*
39345600Sbostic 	 * Set errno to 0 so that user can tell the difference between an
39447194Sbostic 	 * error and a directory without entries.  If not a directory being
39547194Sbostic 	 * visited in *pre-order*, or we've already had fatal errors, return
39647194Sbostic 	 * immediately.
39739800Sbostic 	 */
39839800Sbostic 	errno = 0;
39947194Sbostic 	if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
40039800Sbostic 		return(NULL);
40145600Sbostic 
40245600Sbostic 	/* Free up any previous child list. */
40342273Sbostic 	if (sp->fts_child)
40442273Sbostic 		fts_lfree(sp->fts_child);
40542299Sbostic 
40642299Sbostic 	/*
40745600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
40847194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
40947194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
41047194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
41147194Sbostic 	 * fts_read will work.
41242299Sbostic 	 */
413*47212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
41445600Sbostic 	    ISSET(FTS_NOCHDIR))
41542299Sbostic 		return(sp->fts_child = fts_build(sp, BCHILD));
41642299Sbostic 
41742299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
41842299Sbostic 		return(NULL);
41942299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
42042299Sbostic 	if (fchdir(fd))
42142299Sbostic 		return(NULL);
42242299Sbostic 	(void)close(fd);
42342299Sbostic 	return(sp->fts_child);
42439800Sbostic }
42539800Sbostic 
42647194Sbostic /*
42747194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
42847194Sbostic  * idea is to build the linked list of entries that are used by fts_children
42947194Sbostic  * and fts_read.  There are lots of special cases.
43047194Sbostic  *
43147194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
43247194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
43347194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
43447194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
43547194Sbostic  * directories and for any nodes after the directories in the parent node have
43647194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
43747194Sbostic  */
43839800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
43939800Sbostic 
44047155Sbostic static FTSENT *
44142299Sbostic fts_build(sp, type)
44239800Sbostic 	register FTS *sp;
44342299Sbostic 	int type;
44439800Sbostic {
44539800Sbostic 	register struct dirent *dp;
44639800Sbostic 	register FTSENT *p, *head;
44739800Sbostic 	register int nitems;
44847194Sbostic 	FTSENT *cur;
44939800Sbostic 	DIR *dirp;
45047194Sbostic 	int cderr, descend, len, level, maxlen, nlinks, saved_errno;
45139800Sbostic 	char *cp;
45239800Sbostic 
45345600Sbostic 	/* Set current node pointer. */
45447194Sbostic 	cur = sp->fts_cur;
45545600Sbostic 
45647194Sbostic 	/*
45747194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
45847194Sbostic 	 * If being called from fts_read, set the fts_info field.
45947194Sbostic 	 */
46047194Sbostic 	if (!(dirp = opendir(cur->fts_accpath))) {
46147194Sbostic 		if (type == BREAD)
46247194Sbostic 			cur->fts_info = FTS_DNR;
46339800Sbostic 		return(NULL);
46439800Sbostic 	}
46539800Sbostic 
46639800Sbostic 	/*
46747194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
46847194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
46947194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
47039800Sbostic 	 */
47142273Sbostic 	nlinks =
47245600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
47347194Sbostic 	    cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
47439800Sbostic 
47547194Sbostic 	/*
47647194Sbostic 	 * If we're going to need to stat anything or we want to descend
47747194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
47847194Sbostic 	 * We won't be able to stat anything, but we can still return the
47947194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
48047194Sbostic 	 * chdir into the directory, it will have to return different path
48147194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
48247194Sbostic 	 * has already been visited in pre-order, have to wait until the
48347194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
48447194Sbostic 	 * If a program needed sorted entries or stat information, they had
48547194Sbostic 	 * better be checking FTS_NS on the returned nodes.
48647194Sbostic 	 */
48745600Sbostic 	if (nlinks || type == BREAD)
48847194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
48947194Sbostic 			if (type == BREAD)
49047194Sbostic 				cur->fts_cderr = errno;
49147194Sbostic 			descend = nlinks = 0;
49247194Sbostic 			cderr = 1;
49347194Sbostic 		} else {
49445600Sbostic 			descend = 1;
49547194Sbostic 			cderr = 0;
49639962Sbostic 		}
49747194Sbostic 	else
49847194Sbostic 		descend = 0;
49939962Sbostic 
50047194Sbostic 	/*
50147194Sbostic 	 * Figure out the max file name length that can be stored in the
50247194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
50347194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
50447194Sbostic 	 * could do them in fts_read before returning the path, but it's a
50547194Sbostic 	 * lot easier here since the length is part of the dirent structure.
50647194Sbostic 	 *
50747194Sbostic 	 * If not changing directories set a pointer so that we can just
50847194Sbostic 	 * append each new name into the path.
50947194Sbostic 	 */
51047194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
51147194Sbostic 	len = NAPPEND(cur);
51247194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
51347194Sbostic 		cp = sp->fts_path + len;
51447194Sbostic 		*cp++ = '/';
51547194Sbostic 	}
51640939Sbostic 
51747194Sbostic 	level = cur->fts_level + 1;
51840939Sbostic 
51947194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
52039800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
52147194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
52239800Sbostic 			continue;
52339800Sbostic 
52447194Sbostic 		if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
52539800Sbostic 			goto mem1;
52639800Sbostic 		if (dp->d_namlen > maxlen) {
52745600Sbostic 			if (!fts_path(sp, (int)dp->d_namlen)) {
52847194Sbostic 				/*
52947194Sbostic 				 * No more memory for path or structures.  Save
53047194Sbostic 				 * errno, free up the current structure and the
53147194Sbostic 				 * structures already allocated.
53247194Sbostic 				 */
53347194Sbostic mem1:				saved_errno = errno;
53447194Sbostic 				if (p)
53547194Sbostic 					free(p);
53647194Sbostic 				fts_lfree(head);
53747194Sbostic 				(void)closedir(dirp);
53839800Sbostic 				errno = saved_errno;
53947194Sbostic 				cur->fts_info = FTS_ERR;
54047194Sbostic 				SET(FTS_STOP);
54139800Sbostic 				return(NULL);
54239800Sbostic 			}
54342273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
54439800Sbostic 		}
54539800Sbostic 
54642273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
54742273Sbostic 		p->fts_parent = sp->fts_cur;
54842273Sbostic 		p->fts_level = level;
54939800Sbostic 
55039800Sbostic 		if (nlinks) {
55147194Sbostic 			/* Build a file name for fts_stat to stat. */
55247194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
55347194Sbostic 				p->fts_accpath = p->fts_path;
55442273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
55547194Sbostic 			} else
55647194Sbostic 				p->fts_accpath = p->fts_name;
55745600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
55847194Sbostic 			if (nlinks > 0 && p->fts_info == FTS_D)
55939800Sbostic 				--nlinks;
56047194Sbostic 		} else if (cderr) {
56147194Sbostic 			p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
56247194Sbostic 			p->fts_accpath = cur->fts_accpath;
56347194Sbostic 		} else {
56447194Sbostic 			p->fts_accpath =
56547194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
56647194Sbostic 			p->fts_info = FTS_NSOK;
56747194Sbostic 		}
56839800Sbostic 
56942273Sbostic 		p->fts_link = head;
57039800Sbostic 		head = p;
57139800Sbostic 		++nitems;
57239800Sbostic 	}
57339800Sbostic 	(void)closedir(dirp);
57439800Sbostic 
57547194Sbostic 	/*
57647194Sbostic 	 * If not changing directories, reset the path back to original
57747194Sbostic 	 * state.
57847194Sbostic 	 */
57947194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
58047194Sbostic 		if (cp - 1 > sp->fts_path)
58147194Sbostic 			--cp;
58247194Sbostic 		*cp = '\0';
58347194Sbostic 	}
58442299Sbostic 
58542299Sbostic 	/*
58647194Sbostic 	 * If descended after called from fts_children or called from
58747194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
58847194Sbostic 	 * back, we're done.
58942299Sbostic 	 */
59042299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
59147194Sbostic 		cur->fts_info = FTS_ERR;
59247194Sbostic 		SET(FTS_STOP);
59339962Sbostic 		return(NULL);
59439962Sbostic 	}
59539962Sbostic 
59647194Sbostic 	/* If we didn't find anything, just do the post-order visit */
59739800Sbostic 	if (!nitems) {
59842299Sbostic 		if (type == BREAD)
59947194Sbostic 			cur->fts_info = FTS_DP;
60039800Sbostic 		return(NULL);
60139800Sbostic 	}
60239800Sbostic 
60347194Sbostic 	/* Sort the entries. */
60442273Sbostic 	if (sp->fts_compar && nitems > 1)
60545600Sbostic 		head = fts_sort(sp, head, nitems);
60639800Sbostic 	return(head);
60739800Sbostic }
60839800Sbostic 
60945600Sbostic static u_short
61045600Sbostic fts_stat(sp, p, follow)
61145600Sbostic 	FTS *sp;
61239800Sbostic 	register FTSENT *p;
61345600Sbostic 	int follow;
61439800Sbostic {
61547194Sbostic 	int saved_errno;
61647194Sbostic 
61739800Sbostic 	/*
61845600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
61947194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
62047194Sbostic 	 * fail, return the errno from the stat call.
62139800Sbostic 	 */
62245600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
62345600Sbostic 		if (stat(p->fts_accpath, &p->fts_statb)) {
62447194Sbostic 			saved_errno = errno;
62547194Sbostic 			if (!lstat(p->fts_accpath, &p->fts_statb)) {
62647194Sbostic 				errno = 0;
62745600Sbostic 				return(FTS_SLNONE);
62847194Sbostic 			}
62947194Sbostic 			errno = saved_errno;
63047194Sbostic 			bzero(&p->fts_statb, sizeof(struct stat));
63147194Sbostic 			return(FTS_NS);
63245600Sbostic 		}
63345600Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb)) {
63447194Sbostic 		bzero(&p->fts_statb, sizeof(struct stat));
63545600Sbostic 		return(FTS_NS);
63645600Sbostic 	}
63739800Sbostic 
63847194Sbostic 	/*
63947194Sbostic 	 * Cycle detection is done as soon as we find a directory.  Detection
64047194Sbostic 	 * is by brute force; if the tree gets deep enough or the number of
64147194Sbostic 	 * symbolic links to directories high enough something faster might
64247194Sbostic 	 * be worthwhile.
64347194Sbostic 	 */
64447194Sbostic 	if (S_ISDIR(p->fts_statb.st_mode)) {
64547194Sbostic 		register FTSENT *t;
64647194Sbostic 		register dev_t dev;
64747194Sbostic 		register ino_t ino;
64847194Sbostic 
64947194Sbostic 		dev = p->fts_statb.st_dev;
65047194Sbostic 		ino = p->fts_statb.st_ino;
651*47212Sbostic 		for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
65247194Sbostic 		    t = t->fts_parent)
65347194Sbostic 			if (ino == t->fts_statb.st_ino &&
65447194Sbostic 			    dev == t->fts_statb.st_dev) {
65547194Sbostic 				sp->fts_savelink = p->fts_link;
65647194Sbostic 				p->fts_link = t;
65747194Sbostic 				return(FTS_DC);
65847194Sbostic 			}
65939800Sbostic 		return(FTS_D);
66047194Sbostic 	}
66145600Sbostic 	if (S_ISLNK(p->fts_statb.st_mode))
66239800Sbostic 		return(FTS_SL);
66345600Sbostic 	if (S_ISREG(p->fts_statb.st_mode))
66439800Sbostic 		return(FTS_F);
66540939Sbostic 	return(FTS_DEFAULT);
66639800Sbostic }
66739800Sbostic 
66839800Sbostic #define	R(type, nelem, ptr) \
66942299Sbostic 	(type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
67039800Sbostic 
67139800Sbostic static FTSENT *
67245600Sbostic fts_sort(sp, head, nitems)
67345600Sbostic 	FTS *sp;
67439800Sbostic 	FTSENT *head;
67539800Sbostic 	register int nitems;
67639800Sbostic {
67739800Sbostic 	register FTSENT **ap, *p;
67839800Sbostic 
67939800Sbostic 	/*
68045600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
68139800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
68239800Sbostic 	 * sort for memory reasons, return the directory entries in their
68339800Sbostic 	 * current order.  Allocate enough space for the current needs plus
68439800Sbostic 	 * 40 so we don't realloc one entry at a time.
68539800Sbostic 	 */
68645600Sbostic 	if (nitems > sp->fts_nitems) {
68745600Sbostic 		sp->fts_nitems = nitems + 40;
68845600Sbostic 		if (!(sp->fts_array =
68945600Sbostic 		    R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
69045600Sbostic 			sp->fts_nitems = 0;
69139800Sbostic 			return(head);
69239800Sbostic 		}
69339800Sbostic 	}
69445600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
69539800Sbostic 		*ap++ = p;
69645600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
69745600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
69842273Sbostic 		ap[0]->fts_link = ap[1];
69942273Sbostic 	ap[0]->fts_link = NULL;
70039800Sbostic 	return(head);
70139800Sbostic }
70239800Sbostic 
70339800Sbostic static FTSENT *
70445600Sbostic fts_alloc(sp, name, len)
70545600Sbostic 	FTS *sp;
70639800Sbostic 	char *name;
70739800Sbostic 	register int len;
70839800Sbostic {
70939800Sbostic 	register FTSENT *p;
71039800Sbostic 
71139800Sbostic 	/*
71245600Sbostic 	 * Variable sized structures; the name is the last element so
71347194Sbostic 	 * we allocate enough extra space after the structure to store
71447194Sbostic 	 * it.
71539800Sbostic 	 */
71642299Sbostic 	if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
71739800Sbostic 		return(NULL);
71842273Sbostic 	bcopy(name, p->fts_name, len + 1);
71942273Sbostic 	p->fts_namelen = len;
72045600Sbostic 	p->fts_path = sp->fts_path;
72147194Sbostic 	p->fts_instr = FTS_NOINSTR;
72247194Sbostic 	p->fts_cderr = 0;
72345600Sbostic 	p->fts_number = 0;
72445600Sbostic 	p->fts_pointer = NULL;
72539800Sbostic 	return(p);
72639800Sbostic }
72739800Sbostic 
72845600Sbostic static void
72939800Sbostic fts_lfree(head)
73039800Sbostic 	register FTSENT *head;
73139800Sbostic {
73239800Sbostic 	register FTSENT *p;
73339800Sbostic 
73445600Sbostic 	/* Free a linked list of structures. */
73539800Sbostic 	while (p = head) {
73642273Sbostic 		head = head->fts_link;
73747194Sbostic 		free(p);
73839800Sbostic 	}
73939800Sbostic }
74039800Sbostic 
74139800Sbostic /*
74245600Sbostic  * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
74345600Sbostic  * work on any tree.  Most systems will allow creation of paths much longer
74445600Sbostic  * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
74545600Sbostic  * 128 bytes to the requested size so that we don't realloc the path 2 bytes
74645600Sbostic  * at a time.
74739800Sbostic  */
74842299Sbostic static char *
74945600Sbostic fts_path(sp, size)
75045600Sbostic 	FTS *sp;
75139800Sbostic 	int size;
75239800Sbostic {
75345600Sbostic 	sp->fts_pathlen += size + 128;
75447155Sbostic 	return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
75547155Sbostic }
756