xref: /csrg-svn/lib/libc/gen/fts.c (revision 52150)
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*52150Sbostic static char sccsid[] = "@(#)fts.c	5.22 (Berkeley) 01/08/92";
1039800Sbostic #endif /* LIBC_SCCS and not lint */
1139800Sbostic 
1239800Sbostic #include <sys/param.h>
1339800Sbostic #include <sys/stat.h>
1442299Sbostic #include <fcntl.h>
1539800Sbostic #include <dirent.h>
1639800Sbostic #include <errno.h>
1747194Sbostic #include "fts.h"
1847194Sbostic #include <stdlib.h>
1942273Sbostic #include <string.h>
2047155Sbostic #include <unistd.h>
2139800Sbostic 
2252061Sbostic static FTSENT	*fts_alloc __P((FTS *, char *, int));
2352061Sbostic static FTSENT	*fts_build __P((FTS *, int));
2452061Sbostic static void	 fts_lfree __P((FTSENT *));
2552061Sbostic static void	 fts_load __P((FTS *, FTSENT *));
2652061Sbostic static char	*fts_path __P((FTS *, int));
2752061Sbostic static FTSENT	*fts_sort __P((FTS *, FTSENT *, int));
2852061Sbostic static u_short	 fts_stat __P((FTS *, FTSENT *, int));
2939800Sbostic 
3045600Sbostic #define	ISSET(opt)	(sp->fts_options & opt)
3145600Sbostic #define	SET(opt)	(sp->fts_options |= opt)
3239800Sbostic 
3345600Sbostic #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
3445600Sbostic #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
3539800Sbostic 
3642299Sbostic /* fts_build flags */
3745600Sbostic #define	BCHILD		1		/* from fts_children */
3845600Sbostic #define	BREAD		2		/* from fts_read */
3942299Sbostic 
4039800Sbostic FTS *
4145600Sbostic fts_open(argv, options, compar)
4247155Sbostic 	char * const *argv;
4339800Sbostic 	register int options;
4447194Sbostic 	int (*compar)();
4539800Sbostic {
4639800Sbostic 	register FTS *sp;
4739800Sbostic 	register FTSENT *p, *root;
4839800Sbostic 	register int nitems, maxlen;
4939800Sbostic 	FTSENT *parent, *tmp;
5047194Sbostic 	int len;
5139800Sbostic 
5245600Sbostic 	/* Allocate/initialize the stream */
53*52150Sbostic 	if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
54*52150Sbostic 		return (NULL);
5539800Sbostic 	bzero(sp, sizeof(FTS));
5642273Sbostic 	sp->fts_compar = compar;
5742273Sbostic 	sp->fts_options = options;
5839800Sbostic 
5945600Sbostic 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
6045600Sbostic 	if (ISSET(FTS_LOGICAL))
6145600Sbostic 		SET(FTS_NOCHDIR);
6245600Sbostic 
6345600Sbostic 	/* Allocate/initialize root's parent. */
6445600Sbostic 	if (!(parent = fts_alloc(sp, "", 0)))
6539800Sbostic 		goto mem1;
6647212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
6739800Sbostic 
6845600Sbostic 	/* Allocate/initialize root(s). */
6943070Sbostic 	maxlen = -1;
7043070Sbostic 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
7147194Sbostic 		if (!(len = strlen(*argv))) {
7247194Sbostic 			errno = ENOENT;
7343070Sbostic 			goto mem2;
7447194Sbostic 		}
7547194Sbostic 		if (maxlen < len)
7647194Sbostic 			maxlen = len;
7747194Sbostic 		p = fts_alloc(sp, *argv, len);
7849519Sbostic 		p->fts_level = FTS_ROOTLEVEL;
7949519Sbostic 		p->fts_parent = parent;
8043070Sbostic 		/*
8145600Sbostic 		 * If comparison routine supplied, traverse in sorted
8243070Sbostic 		 * order; otherwise traverse in the order specified.
8343070Sbostic 		 */
8443070Sbostic 		if (compar) {
8543070Sbostic 			p->fts_link = root;
8643070Sbostic 			root = p;
8743070Sbostic 			p->fts_accpath = p->fts_name;
8843075Sbostic 			if (!(options & FTS_NOSTAT))
8945600Sbostic 				p->fts_info = fts_stat(sp, p, 0);
9043070Sbostic 		} else {
9143070Sbostic 			p->fts_link = NULL;
9243070Sbostic 			if (!root)
9343070Sbostic 				tmp = root = p;
9443070Sbostic 			else {
9543070Sbostic 				tmp->fts_link = p;
9643070Sbostic 				tmp = p;
9739800Sbostic 			}
9839800Sbostic 		}
9939800Sbostic 	}
10043070Sbostic 	if (compar && nitems > 1)
10145600Sbostic 		root = fts_sort(sp, root, nitems);
10239800Sbostic 
10342281Sbostic 	/*
10445600Sbostic 	 * Allocate a dummy pointer and make fts_read think that we've just
10542281Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_NS
10642281Sbostic 	 * so that everything about the "current" node is ignored.
10742281Sbostic 	 */
10845600Sbostic 	if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
10942281Sbostic 		goto mem2;
11042281Sbostic 	sp->fts_cur->fts_link = root;
11142281Sbostic 	sp->fts_cur->fts_info = FTS_NS;
11242281Sbostic 
11345600Sbostic 	/* Start out with at least 1K+ of path space. */
11445600Sbostic 	if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
11542281Sbostic 		goto mem3;
11639800Sbostic 
11739800Sbostic 	/*
11845600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
11942299Sbostic 	 * that we can get back here; this could be avoided for some paths,
12042299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
12142299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
12242299Sbostic 	 * descriptor we run anyway, just more slowly.
12339800Sbostic 	 */
12447194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
12545600Sbostic 		SET(FTS_NOCHDIR);
12639800Sbostic 
127*52150Sbostic 	return (sp);
12839800Sbostic 
12947194Sbostic mem3:	free(sp->fts_cur);
13039800Sbostic mem2:	fts_lfree(root);
13147194Sbostic 	free(parent);
13247194Sbostic mem1:	free(sp);
133*52150Sbostic 	return (NULL);
13439800Sbostic }
13539800Sbostic 
13647404Sbostic static void
13745600Sbostic fts_load(sp, p)
13845600Sbostic 	FTS *sp;
13939800Sbostic 	register FTSENT *p;
14039800Sbostic {
14139800Sbostic 	register int len;
14239800Sbostic 	register char *cp;
14339800Sbostic 
14439800Sbostic 	/*
14547220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
14647220Sbostic 	 * actually enter the directory until after the preorder visit, set
14747220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
14847220Sbostic 	 * place and the user can access the first node.
14939800Sbostic 	 */
15042273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
15145600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
15242273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
15339800Sbostic 		len = strlen(++cp);
15442273Sbostic 		bcopy(cp, p->fts_name, len + 1);
15542273Sbostic 		p->fts_namelen = len;
15639800Sbostic 	}
15745600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
15847194Sbostic 
15947403Sbostic 	p->fts_info = fts_stat(sp, p, 0);
16047194Sbostic 	sp->rdev = p->fts_statb.st_dev;
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;
202*52150Sbostic 		return (-1);
20339800Sbostic 	}
204*52150Sbostic 	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))
225*52150Sbostic 		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 
23447194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
23539800Sbostic 	if (instr == FTS_AGAIN) {
23645600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
237*52150Sbostic 		return (p);
23839800Sbostic 	}
23939800Sbostic 
24047194Sbostic 	/*
24147194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
24247194Sbostic 	 * SLNONE and recover.
24347194Sbostic 	 */
24447194Sbostic 	if (instr == FTS_FOLLOW &&
24547194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
24645600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
247*52150Sbostic 		return (p);
24842299Sbostic 	}
24942299Sbostic 
25045600Sbostic 	/* Directory in pre-order. */
25142299Sbostic 	if (p->fts_info == FTS_D) {
25245600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
25347194Sbostic 		if (instr == FTS_SKIP ||
25447194Sbostic 		    ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
25542273Sbostic 			if (sp->fts_child) {
25642273Sbostic 				fts_lfree(sp->fts_child);
25742273Sbostic 				sp->fts_child = NULL;
25839800Sbostic 			}
25942301Sbostic 			p->fts_info = FTS_DP;
260*52150Sbostic 			return (p);
26142299Sbostic 		}
26242299Sbostic 
26347194Sbostic 		/*
26447194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
26547194Sbostic 		 * the read fails for any reason, or the directory is empty,
26647194Sbostic 		 * the fts_info field of the current node is set by fts_build.
26747220Sbostic 		 * If have already read and now fail to chdir, whack the list
26847220Sbostic 		 * to make the names come out right, and set the parent state
26947220Sbostic 		 * so the application will eventually get an error condition.
27047220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
27147220Sbostic 		 * at the root node -- if so, we have to get back or the root
27247220Sbostic 		 * node may be inaccessible.
27347194Sbostic 		 */
27447194Sbostic 		if (sp->fts_child) {
27542299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
27647194Sbostic 				p->fts_parent->fts_cderr = errno;
27747194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
27847194Sbostic 					p->fts_accpath =
27947194Sbostic 					    p->fts_parent->fts_accpath;
28042299Sbostic 			}
28147220Sbostic 		} else if (!(sp->fts_child = fts_build(sp, BREAD))) {
28247220Sbostic 			if ISSET(FTS_STOP)
283*52150Sbostic 				return (NULL);
28447220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
28547220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
28647220Sbostic 				SET(FTS_STOP);
287*52150Sbostic 				return (NULL);
28847220Sbostic 			}
289*52150Sbostic 			return (p);
29047220Sbostic 		}
29147194Sbostic 		p = sp->fts_child;
29242299Sbostic 		sp->fts_child = NULL;
29347194Sbostic 		goto name;
29439800Sbostic 	}
29539800Sbostic 
29645600Sbostic 	/* Move to next node on this level. */
29742299Sbostic next:	tmp = p;
29842299Sbostic 	if (p = p->fts_link) {
29947194Sbostic 		free(tmp);
30047194Sbostic 
30147194Sbostic 		/* If reached the top, load the paths for the next root. */
30247212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
30347404Sbostic 			fts_load(sp, p);
304*52150Sbostic 			return (sp->fts_cur = p);
30547194Sbostic 		}
30642299Sbostic 
30747194Sbostic 		/* User may have called fts_set on the node. */
30847194Sbostic 		if (p->fts_instr == FTS_SKIP)
30947194Sbostic 			goto next;
31047194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
31147194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
31247194Sbostic 			p->fts_instr = FTS_NOINSTR;
31347194Sbostic 		}
31442299Sbostic 
31547194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
31647194Sbostic 		*t++ = '/';
31747194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
318*52150Sbostic 		return (sp->fts_cur = p);
31939800Sbostic 	}
32039800Sbostic 
32147220Sbostic 	/* Move up to the parent node. */
32242299Sbostic 	p = tmp->fts_parent;
32347194Sbostic 	free(tmp);
32442299Sbostic 
32547212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
32639800Sbostic 		/*
32745600Sbostic 		 * Done; free everything up and set errno to 0 so the user
32839800Sbostic 		 * can distinguish between error and EOF.
32939800Sbostic 		 */
33047194Sbostic 		free(p);
33139800Sbostic 		errno = 0;
332*52150Sbostic 		return (sp->fts_cur = NULL);
33339800Sbostic 	}
33439800Sbostic 
33542273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
33647194Sbostic 
33747194Sbostic 	/*
33847220Sbostic 	 * Cd back up to the parent directory.  If at a root node, have to cd
33947220Sbostic 	 * back to the original place, otherwise may not be able to access the
34047220Sbostic 	 * original node on post-order.
34147194Sbostic 	 */
34247220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
34347220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
34447220Sbostic 			SET(FTS_STOP);
345*52150Sbostic 			return (NULL);
34647220Sbostic 		}
34747220Sbostic 	}
34847220Sbostic 	else if (CHDIR(sp, "..")) {
34947220Sbostic 		SET(FTS_STOP);
350*52150Sbostic 		return (NULL);
35147220Sbostic 	}
35247220Sbostic 
35347220Sbostic 	/*
35447220Sbostic 	 * If had a chdir error when trying to get into the directory, set the
35547220Sbostic 	 * info field to reflect this, and restore errno.  The error indicator
35647220Sbostic 	 * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
35747220Sbostic 	 * works.
35847220Sbostic 	 */
35947194Sbostic 	if (p->fts_cderr) {
36047194Sbostic 		errno = p->fts_cderr;
36147194Sbostic 		p->fts_cderr = 0;
36242273Sbostic 		p->fts_info = FTS_ERR;
36347220Sbostic 	} else
36442273Sbostic 		p->fts_info = FTS_DP;
365*52150Sbostic 	return (sp->fts_cur = p);
36639800Sbostic }
36739800Sbostic 
36839800Sbostic /*
36947220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
37039800Sbostic  * implementation; it would be necessary if anyone wanted to add global
37145600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
37245600Sbostic  * reasons.
37339800Sbostic  */
37439800Sbostic /* ARGSUSED */
37545600Sbostic fts_set(sp, p, instr)
37639800Sbostic 	FTS *sp;
37739800Sbostic 	FTSENT *p;
37839800Sbostic 	int instr;
37939800Sbostic {
38042273Sbostic 	p->fts_instr = instr;
381*52150Sbostic 	return (0);
38239800Sbostic }
38339800Sbostic 
38439800Sbostic FTSENT *
38545600Sbostic fts_children(sp)
38639800Sbostic 	register FTS *sp;
38739800Sbostic {
38842299Sbostic 	register FTSENT *p;
38942299Sbostic 	int fd;
39042299Sbostic 
39145600Sbostic 	/* Set current node pointer. */
39245600Sbostic 	p = sp->fts_cur;
39345600Sbostic 
39439800Sbostic 	/*
39545600Sbostic 	 * Set errno to 0 so that user can tell the difference between an
39647194Sbostic 	 * error and a directory without entries.  If not a directory being
39747194Sbostic 	 * visited in *pre-order*, or we've already had fatal errors, return
39847194Sbostic 	 * immediately.
39939800Sbostic 	 */
40039800Sbostic 	errno = 0;
40147194Sbostic 	if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
402*52150Sbostic 		return (NULL);
40345600Sbostic 
40445600Sbostic 	/* Free up any previous child list. */
40542273Sbostic 	if (sp->fts_child)
40642273Sbostic 		fts_lfree(sp->fts_child);
40742299Sbostic 
40842299Sbostic 	/*
40945600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
41047194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
41147194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
41247194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
41347194Sbostic 	 * fts_read will work.
41442299Sbostic 	 */
41547212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
41645600Sbostic 	    ISSET(FTS_NOCHDIR))
417*52150Sbostic 		return (sp->fts_child = fts_build(sp, BCHILD));
41842299Sbostic 
41942299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
420*52150Sbostic 		return (NULL);
42142299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
42242299Sbostic 	if (fchdir(fd))
423*52150Sbostic 		return (NULL);
42442299Sbostic 	(void)close(fd);
425*52150Sbostic 	return (sp->fts_child);
42639800Sbostic }
42739800Sbostic 
42847194Sbostic /*
42947194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
43047194Sbostic  * idea is to build the linked list of entries that are used by fts_children
43147194Sbostic  * and fts_read.  There are lots of special cases.
43247194Sbostic  *
43347194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
43447194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
43547194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
43647194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
43747194Sbostic  * directories and for any nodes after the directories in the parent node have
43847194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
43947194Sbostic  */
44039800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
44139800Sbostic 
44247155Sbostic static FTSENT *
44342299Sbostic fts_build(sp, type)
44439800Sbostic 	register FTS *sp;
44542299Sbostic 	int type;
44639800Sbostic {
44739800Sbostic 	register struct dirent *dp;
44839800Sbostic 	register FTSENT *p, *head;
44939800Sbostic 	register int nitems;
45047194Sbostic 	FTSENT *cur;
45139800Sbostic 	DIR *dirp;
45247194Sbostic 	int cderr, descend, len, level, maxlen, nlinks, saved_errno;
45339800Sbostic 	char *cp;
45439800Sbostic 
45545600Sbostic 	/* Set current node pointer. */
45647194Sbostic 	cur = sp->fts_cur;
45745600Sbostic 
45847194Sbostic 	/*
45947194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
46047194Sbostic 	 * If being called from fts_read, set the fts_info field.
46147194Sbostic 	 */
46247194Sbostic 	if (!(dirp = opendir(cur->fts_accpath))) {
46347194Sbostic 		if (type == BREAD)
46447194Sbostic 			cur->fts_info = FTS_DNR;
465*52150Sbostic 		return (NULL);
46639800Sbostic 	}
46739800Sbostic 
46839800Sbostic 	/*
46947194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
47047194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
47147194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
47239800Sbostic 	 */
47342273Sbostic 	nlinks =
47445600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
47547194Sbostic 	    cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
47639800Sbostic 
47747194Sbostic 	/*
47847194Sbostic 	 * If we're going to need to stat anything or we want to descend
47947194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
48047194Sbostic 	 * We won't be able to stat anything, but we can still return the
48147194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
48247194Sbostic 	 * chdir into the directory, it will have to return different path
48347194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
48447194Sbostic 	 * has already been visited in pre-order, have to wait until the
48547194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
48647194Sbostic 	 * If a program needed sorted entries or stat information, they had
48747194Sbostic 	 * better be checking FTS_NS on the returned nodes.
48847194Sbostic 	 */
48945600Sbostic 	if (nlinks || type == BREAD)
49047194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
49147194Sbostic 			if (type == BREAD)
49247194Sbostic 				cur->fts_cderr = errno;
49347194Sbostic 			descend = nlinks = 0;
49447194Sbostic 			cderr = 1;
49547194Sbostic 		} else {
49645600Sbostic 			descend = 1;
49747194Sbostic 			cderr = 0;
49839962Sbostic 		}
49947194Sbostic 	else
50047194Sbostic 		descend = 0;
50139962Sbostic 
50247194Sbostic 	/*
50347194Sbostic 	 * Figure out the max file name length that can be stored in the
50447194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
50547194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
50647194Sbostic 	 * could do them in fts_read before returning the path, but it's a
50747194Sbostic 	 * lot easier here since the length is part of the dirent structure.
50847194Sbostic 	 *
50947194Sbostic 	 * If not changing directories set a pointer so that we can just
51047194Sbostic 	 * append each new name into the path.
51147194Sbostic 	 */
51247194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
51347194Sbostic 	len = NAPPEND(cur);
51447194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
51547194Sbostic 		cp = sp->fts_path + len;
51647194Sbostic 		*cp++ = '/';
51747194Sbostic 	}
51840939Sbostic 
51947194Sbostic 	level = cur->fts_level + 1;
52040939Sbostic 
52147194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
52239800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
52347194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
52439800Sbostic 			continue;
52539800Sbostic 
52647194Sbostic 		if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
52739800Sbostic 			goto mem1;
52839800Sbostic 		if (dp->d_namlen > maxlen) {
52945600Sbostic 			if (!fts_path(sp, (int)dp->d_namlen)) {
53047194Sbostic 				/*
53147194Sbostic 				 * No more memory for path or structures.  Save
53247194Sbostic 				 * errno, free up the current structure and the
53347194Sbostic 				 * structures already allocated.
53447194Sbostic 				 */
53547194Sbostic mem1:				saved_errno = errno;
53647194Sbostic 				if (p)
53747194Sbostic 					free(p);
53847194Sbostic 				fts_lfree(head);
53947194Sbostic 				(void)closedir(dirp);
54039800Sbostic 				errno = saved_errno;
54147194Sbostic 				cur->fts_info = FTS_ERR;
54247194Sbostic 				SET(FTS_STOP);
543*52150Sbostic 				return (NULL);
54439800Sbostic 			}
54542273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
54639800Sbostic 		}
54739800Sbostic 
54842273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
54942273Sbostic 		p->fts_parent = sp->fts_cur;
55042273Sbostic 		p->fts_level = level;
55139800Sbostic 
55239800Sbostic 		if (nlinks) {
55347194Sbostic 			/* Build a file name for fts_stat to stat. */
55447194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
55547194Sbostic 				p->fts_accpath = p->fts_path;
55642273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
55747194Sbostic 			} else
55847194Sbostic 				p->fts_accpath = p->fts_name;
55945600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
56047194Sbostic 			if (nlinks > 0 && p->fts_info == FTS_D)
56139800Sbostic 				--nlinks;
56247194Sbostic 		} else if (cderr) {
56347194Sbostic 			p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
56447194Sbostic 			p->fts_accpath = cur->fts_accpath;
56547194Sbostic 		} else {
56647194Sbostic 			p->fts_accpath =
56747194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
56847194Sbostic 			p->fts_info = FTS_NSOK;
56947194Sbostic 		}
57039800Sbostic 
57142273Sbostic 		p->fts_link = head;
57239800Sbostic 		head = p;
57339800Sbostic 		++nitems;
57439800Sbostic 	}
57539800Sbostic 	(void)closedir(dirp);
57639800Sbostic 
57747194Sbostic 	/*
57847194Sbostic 	 * If not changing directories, reset the path back to original
57947194Sbostic 	 * state.
58047194Sbostic 	 */
58147194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
58247194Sbostic 		if (cp - 1 > sp->fts_path)
58347194Sbostic 			--cp;
58447194Sbostic 		*cp = '\0';
58547194Sbostic 	}
58642299Sbostic 
58742299Sbostic 	/*
58847194Sbostic 	 * If descended after called from fts_children or called from
58947194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
59047194Sbostic 	 * back, we're done.
59142299Sbostic 	 */
59242299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
59347194Sbostic 		cur->fts_info = FTS_ERR;
59447194Sbostic 		SET(FTS_STOP);
595*52150Sbostic 		return (NULL);
59639962Sbostic 	}
59739962Sbostic 
59847194Sbostic 	/* If we didn't find anything, just do the post-order visit */
59939800Sbostic 	if (!nitems) {
60042299Sbostic 		if (type == BREAD)
60147194Sbostic 			cur->fts_info = FTS_DP;
602*52150Sbostic 		return (NULL);
60339800Sbostic 	}
60439800Sbostic 
60547194Sbostic 	/* Sort the entries. */
60642273Sbostic 	if (sp->fts_compar && nitems > 1)
60745600Sbostic 		head = fts_sort(sp, head, nitems);
608*52150Sbostic 	return (head);
60939800Sbostic }
61039800Sbostic 
61145600Sbostic static u_short
61245600Sbostic fts_stat(sp, p, follow)
61345600Sbostic 	FTS *sp;
61439800Sbostic 	register FTSENT *p;
61545600Sbostic 	int follow;
61639800Sbostic {
61747194Sbostic 	int saved_errno;
61847194Sbostic 
61939800Sbostic 	/*
62045600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
62147194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
62247194Sbostic 	 * fail, return the errno from the stat call.
62339800Sbostic 	 */
62445600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
62545600Sbostic 		if (stat(p->fts_accpath, &p->fts_statb)) {
62647194Sbostic 			saved_errno = errno;
62747194Sbostic 			if (!lstat(p->fts_accpath, &p->fts_statb)) {
62847194Sbostic 				errno = 0;
629*52150Sbostic 				return (FTS_SLNONE);
63047194Sbostic 			}
63147194Sbostic 			errno = saved_errno;
63247194Sbostic 			bzero(&p->fts_statb, sizeof(struct stat));
633*52150Sbostic 			return (FTS_NS);
63445600Sbostic 		}
63545600Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb)) {
63647194Sbostic 		bzero(&p->fts_statb, sizeof(struct stat));
637*52150Sbostic 		return (FTS_NS);
63845600Sbostic 	}
63939800Sbostic 
64047194Sbostic 	/*
64147194Sbostic 	 * Cycle detection is done as soon as we find a directory.  Detection
64247194Sbostic 	 * is by brute force; if the tree gets deep enough or the number of
64347194Sbostic 	 * symbolic links to directories high enough something faster might
64447194Sbostic 	 * be worthwhile.
64547194Sbostic 	 */
64647194Sbostic 	if (S_ISDIR(p->fts_statb.st_mode)) {
64747194Sbostic 		register FTSENT *t;
64847194Sbostic 		register dev_t dev;
64947194Sbostic 		register ino_t ino;
65047194Sbostic 
65147194Sbostic 		dev = p->fts_statb.st_dev;
65247194Sbostic 		ino = p->fts_statb.st_ino;
65352073Sbostic 		for (t = p->fts_parent; t->fts_level >= FTS_ROOTLEVEL;
65447194Sbostic 		    t = t->fts_parent)
65547194Sbostic 			if (ino == t->fts_statb.st_ino &&
65647194Sbostic 			    dev == t->fts_statb.st_dev) {
65752073Sbostic 				p->fts_cycle = t;
658*52150Sbostic 				return (FTS_DC);
65947194Sbostic 			}
660*52150Sbostic 		return (FTS_D);
66147194Sbostic 	}
66245600Sbostic 	if (S_ISLNK(p->fts_statb.st_mode))
663*52150Sbostic 		return (FTS_SL);
66445600Sbostic 	if (S_ISREG(p->fts_statb.st_mode))
665*52150Sbostic 		return (FTS_F);
666*52150Sbostic 	return (FTS_DEFAULT);
66739800Sbostic }
66839800Sbostic 
66939800Sbostic #define	R(type, nelem, ptr) \
670*52150Sbostic 	realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
67139800Sbostic 
67239800Sbostic static FTSENT *
67345600Sbostic fts_sort(sp, head, nitems)
67445600Sbostic 	FTS *sp;
67539800Sbostic 	FTSENT *head;
67639800Sbostic 	register int nitems;
67739800Sbostic {
67839800Sbostic 	register FTSENT **ap, *p;
67939800Sbostic 
68039800Sbostic 	/*
68145600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
68239800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
68339800Sbostic 	 * sort for memory reasons, return the directory entries in their
68439800Sbostic 	 * current order.  Allocate enough space for the current needs plus
68539800Sbostic 	 * 40 so we don't realloc one entry at a time.
68639800Sbostic 	 */
68745600Sbostic 	if (nitems > sp->fts_nitems) {
68845600Sbostic 		sp->fts_nitems = nitems + 40;
68945600Sbostic 		if (!(sp->fts_array =
69045600Sbostic 		    R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
69145600Sbostic 			sp->fts_nitems = 0;
692*52150Sbostic 			return (head);
69339800Sbostic 		}
69439800Sbostic 	}
69545600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
69639800Sbostic 		*ap++ = p;
69745600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
69845600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
69942273Sbostic 		ap[0]->fts_link = ap[1];
70042273Sbostic 	ap[0]->fts_link = NULL;
701*52150Sbostic 	return (head);
70239800Sbostic }
70339800Sbostic 
70439800Sbostic static FTSENT *
70545600Sbostic fts_alloc(sp, name, len)
70645600Sbostic 	FTS *sp;
70739800Sbostic 	char *name;
70839800Sbostic 	register int len;
70939800Sbostic {
71039800Sbostic 	register FTSENT *p;
71139800Sbostic 
71239800Sbostic 	/*
71345600Sbostic 	 * Variable sized structures; the name is the last element so
71447194Sbostic 	 * we allocate enough extra space after the structure to store
71547194Sbostic 	 * it.
71639800Sbostic 	 */
717*52150Sbostic 	if ((p = malloc((size_t)(sizeof(FTSENT) + len))) == NULL)
718*52150Sbostic 		return (NULL);
71942273Sbostic 	bcopy(name, p->fts_name, len + 1);
72042273Sbostic 	p->fts_namelen = len;
72145600Sbostic 	p->fts_path = sp->fts_path;
72247194Sbostic 	p->fts_instr = FTS_NOINSTR;
72347194Sbostic 	p->fts_cderr = 0;
72445600Sbostic 	p->fts_number = 0;
72545600Sbostic 	p->fts_pointer = NULL;
726*52150Sbostic 	return (p);
72739800Sbostic }
72839800Sbostic 
72945600Sbostic static void
73039800Sbostic fts_lfree(head)
73139800Sbostic 	register FTSENT *head;
73239800Sbostic {
73339800Sbostic 	register FTSENT *p;
73439800Sbostic 
73545600Sbostic 	/* Free a linked list of structures. */
73639800Sbostic 	while (p = head) {
73742273Sbostic 		head = head->fts_link;
73847194Sbostic 		free(p);
73939800Sbostic 	}
74039800Sbostic }
74139800Sbostic 
74239800Sbostic /*
74345600Sbostic  * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
74445600Sbostic  * work on any tree.  Most systems will allow creation of paths much longer
74545600Sbostic  * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
74645600Sbostic  * 128 bytes to the requested size so that we don't realloc the path 2 bytes
74745600Sbostic  * at a time.
74839800Sbostic  */
74942299Sbostic static char *
75045600Sbostic fts_path(sp, size)
75145600Sbostic 	FTS *sp;
75239800Sbostic 	int size;
75339800Sbostic {
75445600Sbostic 	sp->fts_pathlen += size + 128;
755*52150Sbostic 	return (sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
75647155Sbostic }
757