xref: /csrg-svn/lib/libc/gen/fts.c (revision 47220)
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*47220Sbostic static char sccsid[] = "@(#)fts.c	5.16 (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;
6547212Sbostic 	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 		}
9647212Sbostic 		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 {
14039800Sbostic 	register int len;
14139800Sbostic 	register char *cp;
14239800Sbostic 
14339800Sbostic 	/*
144*47220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
145*47220Sbostic 	 * actually enter the directory until after the preorder visit, set
146*47220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
147*47220Sbostic 	 * 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;
15947212Sbostic 	p->fts_info = fts_stat(sp, p, 0);
16047194Sbostic 	return(1);
16139800Sbostic }
16239800Sbostic 
16345600Sbostic fts_close(sp)
16439800Sbostic 	FTS *sp;
16539800Sbostic {
16639800Sbostic 	register FTSENT *freep, *p;
16739800Sbostic 	int saved_errno;
16839800Sbostic 
16942281Sbostic 	if (sp->fts_cur) {
17042281Sbostic 		/*
17145600Sbostic 		 * This still works if we haven't read anything -- the dummy
17242281Sbostic 		 * structure points to the root list, so we step through to
17342281Sbostic 		 * the end of the root list which has a valid parent pointer.
17442281Sbostic 		 */
17547212Sbostic 		for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
17642281Sbostic 			freep = p;
17742281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
17847194Sbostic 			free(freep);
17939800Sbostic 		}
18047194Sbostic 		free(p);
18142281Sbostic 	}
18239800Sbostic 
18345600Sbostic 	/* Free up child linked list, sort array, path buffer. */
18442273Sbostic 	if (sp->fts_child)
18542273Sbostic 		fts_lfree(sp->fts_child);
18642273Sbostic 	if (sp->fts_array)
18747194Sbostic 		free(sp->fts_array);
18847194Sbostic 	free(sp->fts_path);
18939800Sbostic 
19047194Sbostic 	/* Return to original directory, save errno if necessary. */
19145600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
19247194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
19347194Sbostic 		(void)close(sp->fts_rfd);
19439800Sbostic 	}
19539800Sbostic 
19645600Sbostic 	/* Free up the stream pointer. */
19747194Sbostic 	free(sp);
19839800Sbostic 
19945600Sbostic 	/* Set errno and return. */
20045600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
20139800Sbostic 		errno = saved_errno;
20239800Sbostic 		return(-1);
20339800Sbostic 	}
20439800Sbostic 	return(0);
20539800Sbostic }
20639800Sbostic 
20747194Sbostic /*
20847194Sbostic  * Special case a root of "/" so that slashes aren't appended causing
20947194Sbostic  * paths to be written as "//foo".
21047194Sbostic  */
21147194Sbostic #define	NAPPEND(p) \
21247212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
21347194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
21447194Sbostic 
21539800Sbostic FTSENT *
21645600Sbostic fts_read(sp)
21739800Sbostic 	register FTS *sp;
21839800Sbostic {
21942299Sbostic 	register FTSENT *p, *tmp;
22039800Sbostic 	register int instr;
22147194Sbostic 	register char *t;
22239800Sbostic 
22345600Sbostic 	/* If finished or unrecoverable error, return NULL. */
22447194Sbostic 	if (!sp->fts_cur || ISSET(FTS_STOP))
22539800Sbostic 		return(NULL);
22639800Sbostic 
22745600Sbostic 	/* Set current node pointer. */
22842273Sbostic 	p = sp->fts_cur;
22939800Sbostic 
23045600Sbostic 	/* Save and zero out user instructions. */
23142273Sbostic 	instr = p->fts_instr;
23247194Sbostic 	p->fts_instr = FTS_NOINSTR;
23339800Sbostic 
23445600Sbostic 	/* If used fts_link pointer for cycle detection, restore it. */
23542273Sbostic 	if (sp->fts_savelink) {
23642273Sbostic 		p->fts_link = sp->fts_savelink;
23742273Sbostic 		sp->fts_savelink = NULL;
23839800Sbostic 	}
23939800Sbostic 
24047194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
24139800Sbostic 	if (instr == FTS_AGAIN) {
24245600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
24339800Sbostic 		return(p);
24439800Sbostic 	}
24539800Sbostic 
24647194Sbostic 	/*
24747194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
24847194Sbostic 	 * SLNONE and recover.
24947194Sbostic 	 */
25047194Sbostic 	if (instr == FTS_FOLLOW &&
25147194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
25245600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
25342299Sbostic 		return(p);
25442299Sbostic 	}
25542299Sbostic 
25645600Sbostic 	/* Directory in pre-order. */
25742299Sbostic 	if (p->fts_info == FTS_D) {
25845600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
25947194Sbostic 		if (instr == FTS_SKIP ||
26047194Sbostic 		    ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
26142273Sbostic 			if (sp->fts_child) {
26242273Sbostic 				fts_lfree(sp->fts_child);
26342273Sbostic 				sp->fts_child = NULL;
26439800Sbostic 			}
26542301Sbostic 			p->fts_info = FTS_DP;
26642301Sbostic 			return(p);
26742299Sbostic 		}
26842299Sbostic 
26947194Sbostic 		/*
27047194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
27147194Sbostic 		 * the read fails for any reason, or the directory is empty,
27247194Sbostic 		 * the fts_info field of the current node is set by fts_build.
273*47220Sbostic 		 * If have already read and now fail to chdir, whack the list
274*47220Sbostic 		 * to make the names come out right, and set the parent state
275*47220Sbostic 		 * so the application will eventually get an error condition.
276*47220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
277*47220Sbostic 		 * at the root node -- if so, we have to get back or the root
278*47220Sbostic 		 * node may be inaccessible.
27947194Sbostic 		 */
28047194Sbostic 		if (sp->fts_child) {
28142299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
28247194Sbostic 				p->fts_parent->fts_cderr = errno;
28347194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
28447194Sbostic 					p->fts_accpath =
28547194Sbostic 					    p->fts_parent->fts_accpath;
28642299Sbostic 			}
287*47220Sbostic 		} else if (!(sp->fts_child = fts_build(sp, BREAD))) {
288*47220Sbostic 			if ISSET(FTS_STOP)
289*47220Sbostic 				return(NULL);
290*47220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
291*47220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
292*47220Sbostic 				SET(FTS_STOP);
293*47220Sbostic 				return(NULL);
294*47220Sbostic 			}
295*47220Sbostic 			return(p);
296*47220Sbostic 		}
29747194Sbostic 		p = sp->fts_child;
29842299Sbostic 		sp->fts_child = NULL;
29947194Sbostic 		goto name;
30039800Sbostic 	}
30139800Sbostic 
30245600Sbostic 	/* Move to next node on this level. */
30342299Sbostic next:	tmp = p;
30442299Sbostic 	if (p = p->fts_link) {
30547194Sbostic 		free(tmp);
30647194Sbostic 
30747194Sbostic 		/* If reached the top, load the paths for the next root. */
30847212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
309*47220Sbostic 			if (!fts_load(sp, p))
31047194Sbostic 				return(NULL);
31147194Sbostic 			return(sp->fts_cur = p);
31247194Sbostic 		}
31342299Sbostic 
31447194Sbostic 		/* User may have called fts_set on the node. */
31547194Sbostic 		if (p->fts_instr == FTS_SKIP)
31647194Sbostic 			goto next;
31747194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
31847194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
31947194Sbostic 			p->fts_instr = FTS_NOINSTR;
32047194Sbostic 		}
32142299Sbostic 
32247194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
32347194Sbostic 		*t++ = '/';
32447194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
32542273Sbostic 		return(sp->fts_cur = p);
32639800Sbostic 	}
32739800Sbostic 
328*47220Sbostic 	/* Move up to the parent node. */
32942299Sbostic 	p = tmp->fts_parent;
33047194Sbostic 	free(tmp);
33142299Sbostic 
33247212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
33339800Sbostic 		/*
33445600Sbostic 		 * Done; free everything up and set errno to 0 so the user
33539800Sbostic 		 * can distinguish between error and EOF.
33639800Sbostic 		 */
33747194Sbostic 		free(p);
33839800Sbostic 		errno = 0;
33942273Sbostic 		return(sp->fts_cur = NULL);
34039800Sbostic 	}
34139800Sbostic 
34242273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
34347194Sbostic 
34447194Sbostic 	/*
345*47220Sbostic 	 * Cd back up to the parent directory.  If at a root node, have to cd
346*47220Sbostic 	 * back to the original place, otherwise may not be able to access the
347*47220Sbostic 	 * original node on post-order.
34847194Sbostic 	 */
349*47220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
350*47220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
351*47220Sbostic 			SET(FTS_STOP);
352*47220Sbostic 			return(NULL);
353*47220Sbostic 		}
354*47220Sbostic 	}
355*47220Sbostic 	else if (CHDIR(sp, "..")) {
356*47220Sbostic 		SET(FTS_STOP);
357*47220Sbostic 		return(NULL);
358*47220Sbostic 	}
359*47220Sbostic 
360*47220Sbostic 	/*
361*47220Sbostic 	 * If had a chdir error when trying to get into the directory, set the
362*47220Sbostic 	 * info field to reflect this, and restore errno.  The error indicator
363*47220Sbostic 	 * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
364*47220Sbostic 	 * works.
365*47220Sbostic 	 */
36647194Sbostic 	if (p->fts_cderr) {
36747194Sbostic 		errno = p->fts_cderr;
36847194Sbostic 		p->fts_cderr = 0;
36942273Sbostic 		p->fts_info = FTS_ERR;
370*47220Sbostic 	} else
37142273Sbostic 		p->fts_info = FTS_DP;
37242273Sbostic 	return(sp->fts_cur = p);
37339800Sbostic }
37439800Sbostic 
37539800Sbostic /*
376*47220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
37739800Sbostic  * implementation; it would be necessary if anyone wanted to add global
37845600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
37945600Sbostic  * reasons.
38039800Sbostic  */
38139800Sbostic /* ARGSUSED */
38245600Sbostic fts_set(sp, p, instr)
38339800Sbostic 	FTS *sp;
38439800Sbostic 	FTSENT *p;
38539800Sbostic 	int instr;
38639800Sbostic {
38742273Sbostic 	p->fts_instr = instr;
38839800Sbostic 	return(0);
38939800Sbostic }
39039800Sbostic 
39139800Sbostic FTSENT *
39245600Sbostic fts_children(sp)
39339800Sbostic 	register FTS *sp;
39439800Sbostic {
39542299Sbostic 	register FTSENT *p;
39642299Sbostic 	int fd;
39742299Sbostic 
39845600Sbostic 	/* Set current node pointer. */
39945600Sbostic 	p = sp->fts_cur;
40045600Sbostic 
40139800Sbostic 	/*
40245600Sbostic 	 * Set errno to 0 so that user can tell the difference between an
40347194Sbostic 	 * error and a directory without entries.  If not a directory being
40447194Sbostic 	 * visited in *pre-order*, or we've already had fatal errors, return
40547194Sbostic 	 * immediately.
40639800Sbostic 	 */
40739800Sbostic 	errno = 0;
40847194Sbostic 	if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
40939800Sbostic 		return(NULL);
41045600Sbostic 
41145600Sbostic 	/* Free up any previous child list. */
41242273Sbostic 	if (sp->fts_child)
41342273Sbostic 		fts_lfree(sp->fts_child);
41442299Sbostic 
41542299Sbostic 	/*
41645600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
41747194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
41847194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
41947194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
42047194Sbostic 	 * fts_read will work.
42142299Sbostic 	 */
42247212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
42345600Sbostic 	    ISSET(FTS_NOCHDIR))
42442299Sbostic 		return(sp->fts_child = fts_build(sp, BCHILD));
42542299Sbostic 
42642299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
42742299Sbostic 		return(NULL);
42842299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
42942299Sbostic 	if (fchdir(fd))
43042299Sbostic 		return(NULL);
43142299Sbostic 	(void)close(fd);
43242299Sbostic 	return(sp->fts_child);
43339800Sbostic }
43439800Sbostic 
43547194Sbostic /*
43647194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
43747194Sbostic  * idea is to build the linked list of entries that are used by fts_children
43847194Sbostic  * and fts_read.  There are lots of special cases.
43947194Sbostic  *
44047194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
44147194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
44247194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
44347194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
44447194Sbostic  * directories and for any nodes after the directories in the parent node have
44547194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
44647194Sbostic  */
44739800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
44839800Sbostic 
44947155Sbostic static FTSENT *
45042299Sbostic fts_build(sp, type)
45139800Sbostic 	register FTS *sp;
45242299Sbostic 	int type;
45339800Sbostic {
45439800Sbostic 	register struct dirent *dp;
45539800Sbostic 	register FTSENT *p, *head;
45639800Sbostic 	register int nitems;
45747194Sbostic 	FTSENT *cur;
45839800Sbostic 	DIR *dirp;
45947194Sbostic 	int cderr, descend, len, level, maxlen, nlinks, saved_errno;
46039800Sbostic 	char *cp;
46139800Sbostic 
46245600Sbostic 	/* Set current node pointer. */
46347194Sbostic 	cur = sp->fts_cur;
46445600Sbostic 
46547194Sbostic 	/*
46647194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
46747194Sbostic 	 * If being called from fts_read, set the fts_info field.
46847194Sbostic 	 */
46947194Sbostic 	if (!(dirp = opendir(cur->fts_accpath))) {
47047194Sbostic 		if (type == BREAD)
47147194Sbostic 			cur->fts_info = FTS_DNR;
47239800Sbostic 		return(NULL);
47339800Sbostic 	}
47439800Sbostic 
47539800Sbostic 	/*
47647194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
47747194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
47847194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
47939800Sbostic 	 */
48042273Sbostic 	nlinks =
48145600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
48247194Sbostic 	    cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
48339800Sbostic 
48447194Sbostic 	/*
48547194Sbostic 	 * If we're going to need to stat anything or we want to descend
48647194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
48747194Sbostic 	 * We won't be able to stat anything, but we can still return the
48847194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
48947194Sbostic 	 * chdir into the directory, it will have to return different path
49047194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
49147194Sbostic 	 * has already been visited in pre-order, have to wait until the
49247194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
49347194Sbostic 	 * If a program needed sorted entries or stat information, they had
49447194Sbostic 	 * better be checking FTS_NS on the returned nodes.
49547194Sbostic 	 */
49645600Sbostic 	if (nlinks || type == BREAD)
49747194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
49847194Sbostic 			if (type == BREAD)
49947194Sbostic 				cur->fts_cderr = errno;
50047194Sbostic 			descend = nlinks = 0;
50147194Sbostic 			cderr = 1;
50247194Sbostic 		} else {
50345600Sbostic 			descend = 1;
50447194Sbostic 			cderr = 0;
50539962Sbostic 		}
50647194Sbostic 	else
50747194Sbostic 		descend = 0;
50839962Sbostic 
50947194Sbostic 	/*
51047194Sbostic 	 * Figure out the max file name length that can be stored in the
51147194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
51247194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
51347194Sbostic 	 * could do them in fts_read before returning the path, but it's a
51447194Sbostic 	 * lot easier here since the length is part of the dirent structure.
51547194Sbostic 	 *
51647194Sbostic 	 * If not changing directories set a pointer so that we can just
51747194Sbostic 	 * append each new name into the path.
51847194Sbostic 	 */
51947194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
52047194Sbostic 	len = NAPPEND(cur);
52147194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
52247194Sbostic 		cp = sp->fts_path + len;
52347194Sbostic 		*cp++ = '/';
52447194Sbostic 	}
52540939Sbostic 
52647194Sbostic 	level = cur->fts_level + 1;
52740939Sbostic 
52847194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
52939800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
53047194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
53139800Sbostic 			continue;
53239800Sbostic 
53347194Sbostic 		if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
53439800Sbostic 			goto mem1;
53539800Sbostic 		if (dp->d_namlen > maxlen) {
53645600Sbostic 			if (!fts_path(sp, (int)dp->d_namlen)) {
53747194Sbostic 				/*
53847194Sbostic 				 * No more memory for path or structures.  Save
53947194Sbostic 				 * errno, free up the current structure and the
54047194Sbostic 				 * structures already allocated.
54147194Sbostic 				 */
54247194Sbostic mem1:				saved_errno = errno;
54347194Sbostic 				if (p)
54447194Sbostic 					free(p);
54547194Sbostic 				fts_lfree(head);
54647194Sbostic 				(void)closedir(dirp);
54739800Sbostic 				errno = saved_errno;
54847194Sbostic 				cur->fts_info = FTS_ERR;
54947194Sbostic 				SET(FTS_STOP);
55039800Sbostic 				return(NULL);
55139800Sbostic 			}
55242273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
55339800Sbostic 		}
55439800Sbostic 
55542273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
55642273Sbostic 		p->fts_parent = sp->fts_cur;
55742273Sbostic 		p->fts_level = level;
55839800Sbostic 
55939800Sbostic 		if (nlinks) {
56047194Sbostic 			/* Build a file name for fts_stat to stat. */
56147194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
56247194Sbostic 				p->fts_accpath = p->fts_path;
56342273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
56447194Sbostic 			} else
56547194Sbostic 				p->fts_accpath = p->fts_name;
56645600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
56747194Sbostic 			if (nlinks > 0 && p->fts_info == FTS_D)
56839800Sbostic 				--nlinks;
56947194Sbostic 		} else if (cderr) {
57047194Sbostic 			p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
57147194Sbostic 			p->fts_accpath = cur->fts_accpath;
57247194Sbostic 		} else {
57347194Sbostic 			p->fts_accpath =
57447194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
57547194Sbostic 			p->fts_info = FTS_NSOK;
57647194Sbostic 		}
57739800Sbostic 
57842273Sbostic 		p->fts_link = head;
57939800Sbostic 		head = p;
58039800Sbostic 		++nitems;
58139800Sbostic 	}
58239800Sbostic 	(void)closedir(dirp);
58339800Sbostic 
58447194Sbostic 	/*
58547194Sbostic 	 * If not changing directories, reset the path back to original
58647194Sbostic 	 * state.
58747194Sbostic 	 */
58847194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
58947194Sbostic 		if (cp - 1 > sp->fts_path)
59047194Sbostic 			--cp;
59147194Sbostic 		*cp = '\0';
59247194Sbostic 	}
59342299Sbostic 
59442299Sbostic 	/*
59547194Sbostic 	 * If descended after called from fts_children or called from
59647194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
59747194Sbostic 	 * back, we're done.
59842299Sbostic 	 */
59942299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
60047194Sbostic 		cur->fts_info = FTS_ERR;
60147194Sbostic 		SET(FTS_STOP);
60239962Sbostic 		return(NULL);
60339962Sbostic 	}
60439962Sbostic 
60547194Sbostic 	/* If we didn't find anything, just do the post-order visit */
60639800Sbostic 	if (!nitems) {
60742299Sbostic 		if (type == BREAD)
60847194Sbostic 			cur->fts_info = FTS_DP;
60939800Sbostic 		return(NULL);
61039800Sbostic 	}
61139800Sbostic 
61247194Sbostic 	/* Sort the entries. */
61342273Sbostic 	if (sp->fts_compar && nitems > 1)
61445600Sbostic 		head = fts_sort(sp, head, nitems);
61539800Sbostic 	return(head);
61639800Sbostic }
61739800Sbostic 
61845600Sbostic static u_short
61945600Sbostic fts_stat(sp, p, follow)
62045600Sbostic 	FTS *sp;
62139800Sbostic 	register FTSENT *p;
62245600Sbostic 	int follow;
62339800Sbostic {
62447194Sbostic 	int saved_errno;
62547194Sbostic 
62639800Sbostic 	/*
62745600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
62847194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
62947194Sbostic 	 * fail, return the errno from the stat call.
63039800Sbostic 	 */
63145600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
63245600Sbostic 		if (stat(p->fts_accpath, &p->fts_statb)) {
63347194Sbostic 			saved_errno = errno;
63447194Sbostic 			if (!lstat(p->fts_accpath, &p->fts_statb)) {
63547194Sbostic 				errno = 0;
63645600Sbostic 				return(FTS_SLNONE);
63747194Sbostic 			}
63847194Sbostic 			errno = saved_errno;
63947194Sbostic 			bzero(&p->fts_statb, sizeof(struct stat));
64047194Sbostic 			return(FTS_NS);
64145600Sbostic 		}
64245600Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb)) {
64347194Sbostic 		bzero(&p->fts_statb, sizeof(struct stat));
64445600Sbostic 		return(FTS_NS);
64545600Sbostic 	}
64639800Sbostic 
64747194Sbostic 	/*
64847194Sbostic 	 * Cycle detection is done as soon as we find a directory.  Detection
64947194Sbostic 	 * is by brute force; if the tree gets deep enough or the number of
65047194Sbostic 	 * symbolic links to directories high enough something faster might
65147194Sbostic 	 * be worthwhile.
65247194Sbostic 	 */
65347194Sbostic 	if (S_ISDIR(p->fts_statb.st_mode)) {
65447194Sbostic 		register FTSENT *t;
65547194Sbostic 		register dev_t dev;
65647194Sbostic 		register ino_t ino;
65747194Sbostic 
65847194Sbostic 		dev = p->fts_statb.st_dev;
65947194Sbostic 		ino = p->fts_statb.st_ino;
66047212Sbostic 		for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
66147194Sbostic 		    t = t->fts_parent)
66247194Sbostic 			if (ino == t->fts_statb.st_ino &&
66347194Sbostic 			    dev == t->fts_statb.st_dev) {
66447194Sbostic 				sp->fts_savelink = p->fts_link;
66547194Sbostic 				p->fts_link = t;
66647194Sbostic 				return(FTS_DC);
66747194Sbostic 			}
66839800Sbostic 		return(FTS_D);
66947194Sbostic 	}
67045600Sbostic 	if (S_ISLNK(p->fts_statb.st_mode))
67139800Sbostic 		return(FTS_SL);
67245600Sbostic 	if (S_ISREG(p->fts_statb.st_mode))
67339800Sbostic 		return(FTS_F);
67440939Sbostic 	return(FTS_DEFAULT);
67539800Sbostic }
67639800Sbostic 
67739800Sbostic #define	R(type, nelem, ptr) \
67842299Sbostic 	(type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
67939800Sbostic 
68039800Sbostic static FTSENT *
68145600Sbostic fts_sort(sp, head, nitems)
68245600Sbostic 	FTS *sp;
68339800Sbostic 	FTSENT *head;
68439800Sbostic 	register int nitems;
68539800Sbostic {
68639800Sbostic 	register FTSENT **ap, *p;
68739800Sbostic 
68839800Sbostic 	/*
68945600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
69039800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
69139800Sbostic 	 * sort for memory reasons, return the directory entries in their
69239800Sbostic 	 * current order.  Allocate enough space for the current needs plus
69339800Sbostic 	 * 40 so we don't realloc one entry at a time.
69439800Sbostic 	 */
69545600Sbostic 	if (nitems > sp->fts_nitems) {
69645600Sbostic 		sp->fts_nitems = nitems + 40;
69745600Sbostic 		if (!(sp->fts_array =
69845600Sbostic 		    R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
69945600Sbostic 			sp->fts_nitems = 0;
70039800Sbostic 			return(head);
70139800Sbostic 		}
70239800Sbostic 	}
70345600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
70439800Sbostic 		*ap++ = p;
70545600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
70645600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
70742273Sbostic 		ap[0]->fts_link = ap[1];
70842273Sbostic 	ap[0]->fts_link = NULL;
70939800Sbostic 	return(head);
71039800Sbostic }
71139800Sbostic 
71239800Sbostic static FTSENT *
71345600Sbostic fts_alloc(sp, name, len)
71445600Sbostic 	FTS *sp;
71539800Sbostic 	char *name;
71639800Sbostic 	register int len;
71739800Sbostic {
71839800Sbostic 	register FTSENT *p;
71939800Sbostic 
72039800Sbostic 	/*
72145600Sbostic 	 * Variable sized structures; the name is the last element so
72247194Sbostic 	 * we allocate enough extra space after the structure to store
72347194Sbostic 	 * it.
72439800Sbostic 	 */
72542299Sbostic 	if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
72639800Sbostic 		return(NULL);
72742273Sbostic 	bcopy(name, p->fts_name, len + 1);
72842273Sbostic 	p->fts_namelen = len;
72945600Sbostic 	p->fts_path = sp->fts_path;
73047194Sbostic 	p->fts_instr = FTS_NOINSTR;
73147194Sbostic 	p->fts_cderr = 0;
73245600Sbostic 	p->fts_number = 0;
73345600Sbostic 	p->fts_pointer = NULL;
73439800Sbostic 	return(p);
73539800Sbostic }
73639800Sbostic 
73745600Sbostic static void
73839800Sbostic fts_lfree(head)
73939800Sbostic 	register FTSENT *head;
74039800Sbostic {
74139800Sbostic 	register FTSENT *p;
74239800Sbostic 
74345600Sbostic 	/* Free a linked list of structures. */
74439800Sbostic 	while (p = head) {
74542273Sbostic 		head = head->fts_link;
74647194Sbostic 		free(p);
74739800Sbostic 	}
74839800Sbostic }
74939800Sbostic 
75039800Sbostic /*
75145600Sbostic  * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
75245600Sbostic  * work on any tree.  Most systems will allow creation of paths much longer
75345600Sbostic  * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
75445600Sbostic  * 128 bytes to the requested size so that we don't realloc the path 2 bytes
75545600Sbostic  * at a time.
75639800Sbostic  */
75742299Sbostic static char *
75845600Sbostic fts_path(sp, size)
75945600Sbostic 	FTS *sp;
76039800Sbostic 	int size;
76139800Sbostic {
76245600Sbostic 	sp->fts_pathlen += size + 128;
76347155Sbostic 	return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
76447155Sbostic }
765