xref: /csrg-svn/lib/libc/gen/fts.c (revision 52278)
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*52278Sbostic static char sccsid[] = "@(#)fts.c	5.28 (Berkeley) 01/31/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>
1752209Sbostic #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 *));
2652209Sbostic static void	 fts_padjust __P((FTS *, void *));
2752209Sbostic static int	 fts_palloc __P((FTS *, int));
2852061Sbostic static FTSENT	*fts_sort __P((FTS *, FTSENT *, int));
2952061Sbostic static u_short	 fts_stat __P((FTS *, FTSENT *, int));
3039800Sbostic 
3152209Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
3252209Sbostic 
3345600Sbostic #define	ISSET(opt)	(sp->fts_options & opt)
3445600Sbostic #define	SET(opt)	(sp->fts_options |= opt)
3539800Sbostic 
3645600Sbostic #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
3745600Sbostic #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
3839800Sbostic 
3942299Sbostic /* fts_build flags */
4045600Sbostic #define	BCHILD		1		/* from fts_children */
4145600Sbostic #define	BREAD		2		/* from fts_read */
4242299Sbostic 
4339800Sbostic FTS *
4445600Sbostic fts_open(argv, options, compar)
4547155Sbostic 	char * const *argv;
4639800Sbostic 	register int options;
4747194Sbostic 	int (*compar)();
4839800Sbostic {
4939800Sbostic 	register FTS *sp;
5039800Sbostic 	register FTSENT *p, *root;
5139800Sbostic 	register int nitems, maxlen;
5239800Sbostic 	FTSENT *parent, *tmp;
5347194Sbostic 	int len;
5439800Sbostic 
5545600Sbostic 	/* Allocate/initialize the stream */
5652150Sbostic 	if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
5752150Sbostic 		return (NULL);
5839800Sbostic 	bzero(sp, sizeof(FTS));
5942273Sbostic 	sp->fts_compar = compar;
6042273Sbostic 	sp->fts_options = options;
6139800Sbostic 
6245600Sbostic 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
6345600Sbostic 	if (ISSET(FTS_LOGICAL))
6445600Sbostic 		SET(FTS_NOCHDIR);
6545600Sbostic 
6645600Sbostic 	/* Allocate/initialize root's parent. */
6752155Sbostic 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
6839800Sbostic 		goto mem1;
6947212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
7039800Sbostic 
7145600Sbostic 	/* Allocate/initialize root(s). */
7252160Sbostic 	for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) {
7352160Sbostic 		/* Don't allow zero-length paths. */
7452209Sbostic 		if ((len = strlen(*argv)) == 0) {
7547194Sbostic 			errno = ENOENT;
7643070Sbostic 			goto mem2;
7747194Sbostic 		}
7847194Sbostic 		if (maxlen < len)
7947194Sbostic 			maxlen = len;
8052209Sbostic 
8147194Sbostic 		p = fts_alloc(sp, *argv, len);
8249519Sbostic 		p->fts_level = FTS_ROOTLEVEL;
8349519Sbostic 		p->fts_parent = parent;
8452218Sbostic 		p->fts_accpath = p->fts_name;
8552209Sbostic 		p->fts_info = fts_stat(sp, p, 0);
8652209Sbostic 
8752209Sbostic 		/* Command-line "." and ".." are real directories. */
8852209Sbostic 		if (p->fts_info == FTS_DOT)
8952209Sbostic 			p->fts_info = FTS_D;
9052209Sbostic 
9143070Sbostic 		/*
9245600Sbostic 		 * If comparison routine supplied, traverse in sorted
9343070Sbostic 		 * order; otherwise traverse in the order specified.
9443070Sbostic 		 */
9543070Sbostic 		if (compar) {
9643070Sbostic 			p->fts_link = root;
9743070Sbostic 			root = p;
9843070Sbostic 		} else {
9943070Sbostic 			p->fts_link = NULL;
10052155Sbostic 			if (root == NULL)
10143070Sbostic 				tmp = root = p;
10243070Sbostic 			else {
10343070Sbostic 				tmp->fts_link = p;
10443070Sbostic 				tmp = p;
10539800Sbostic 			}
10639800Sbostic 		}
10739800Sbostic 	}
10843070Sbostic 	if (compar && nitems > 1)
10945600Sbostic 		root = fts_sort(sp, root, nitems);
11039800Sbostic 
11142281Sbostic 	/*
11245600Sbostic 	 * Allocate a dummy pointer and make fts_read think that we've just
11352160Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
11442281Sbostic 	 * so that everything about the "current" node is ignored.
11542281Sbostic 	 */
11652155Sbostic 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
11742281Sbostic 		goto mem2;
11842281Sbostic 	sp->fts_cur->fts_link = root;
11952160Sbostic 	sp->fts_cur->fts_info = FTS_INIT;
12042281Sbostic 
12152160Sbostic 	/*
12252209Sbostic 	 * Start out with more than 1K of path space, and enough, in any
12352160Sbostic 	 * case, to hold the user's paths.
12452160Sbostic 	 */
12552209Sbostic 	if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN)))
12642281Sbostic 		goto mem3;
12739800Sbostic 
12839800Sbostic 	/*
12945600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
13042299Sbostic 	 * that we can get back here; this could be avoided for some paths,
13142299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
13242299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
13342299Sbostic 	 * descriptor we run anyway, just more slowly.
13439800Sbostic 	 */
13547194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
13645600Sbostic 		SET(FTS_NOCHDIR);
13739800Sbostic 
13852150Sbostic 	return (sp);
13939800Sbostic 
14047194Sbostic mem3:	free(sp->fts_cur);
14139800Sbostic mem2:	fts_lfree(root);
14247194Sbostic 	free(parent);
14347194Sbostic mem1:	free(sp);
14452150Sbostic 	return (NULL);
14539800Sbostic }
14639800Sbostic 
14747404Sbostic static void
14845600Sbostic fts_load(sp, p)
14945600Sbostic 	FTS *sp;
15039800Sbostic 	register FTSENT *p;
15139800Sbostic {
15239800Sbostic 	register int len;
15339800Sbostic 	register char *cp;
15439800Sbostic 
15539800Sbostic 	/*
15647220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
15747220Sbostic 	 * actually enter the directory until after the preorder visit, set
15847220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
15952209Sbostic 	 * place and the user can access the first node.  From fts_open it's
16052209Sbostic 	 * known that the path will fit.
16139800Sbostic 	 */
16242273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
16345600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
16442273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
16539800Sbostic 		len = strlen(++cp);
16642273Sbostic 		bcopy(cp, p->fts_name, len + 1);
16742273Sbostic 		p->fts_namelen = len;
16839800Sbostic 	}
16945600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
17052209Sbostic 	sp->fts_dev = p->fts_dev;
17139800Sbostic }
17239800Sbostic 
17352209Sbostic int
17445600Sbostic fts_close(sp)
17539800Sbostic 	FTS *sp;
17639800Sbostic {
17739800Sbostic 	register FTSENT *freep, *p;
17839800Sbostic 	int saved_errno;
17939800Sbostic 
18052209Sbostic 	/*
18152209Sbostic 	 * This still works if we haven't read anything -- the dummy structure
18252209Sbostic 	 * points to the root list, so we step through to the end of the root
18352209Sbostic 	 * list which has a valid parent pointer.
18452209Sbostic 	 */
18542281Sbostic 	if (sp->fts_cur) {
18652209Sbostic 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
18742281Sbostic 			freep = p;
18842281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
18947194Sbostic 			free(freep);
19039800Sbostic 		}
19147194Sbostic 		free(p);
19242281Sbostic 	}
19339800Sbostic 
19445600Sbostic 	/* Free up child linked list, sort array, path buffer. */
19542273Sbostic 	if (sp->fts_child)
19642273Sbostic 		fts_lfree(sp->fts_child);
19742273Sbostic 	if (sp->fts_array)
19847194Sbostic 		free(sp->fts_array);
19947194Sbostic 	free(sp->fts_path);
20039800Sbostic 
20147194Sbostic 	/* Return to original directory, save errno if necessary. */
20245600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
20347194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
20447194Sbostic 		(void)close(sp->fts_rfd);
20539800Sbostic 	}
20639800Sbostic 
20745600Sbostic 	/* Free up the stream pointer. */
20847194Sbostic 	free(sp);
20939800Sbostic 
21045600Sbostic 	/* Set errno and return. */
21145600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
21239800Sbostic 		errno = saved_errno;
21352150Sbostic 		return (-1);
21439800Sbostic 	}
21552150Sbostic 	return (0);
21639800Sbostic }
21739800Sbostic 
21847194Sbostic /*
21952160Sbostic  * Special case a root of "/" so that slashes aren't appended which would
22052160Sbostic  * cause paths to be written as "//foo".
22147194Sbostic  */
22247194Sbostic #define	NAPPEND(p) \
22347212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
22447194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
22547194Sbostic 
22639800Sbostic FTSENT *
22745600Sbostic fts_read(sp)
22839800Sbostic 	register FTS *sp;
22939800Sbostic {
23042299Sbostic 	register FTSENT *p, *tmp;
23139800Sbostic 	register int instr;
23247194Sbostic 	register char *t;
23339800Sbostic 
23445600Sbostic 	/* If finished or unrecoverable error, return NULL. */
23552155Sbostic 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
23652150Sbostic 		return (NULL);
23739800Sbostic 
23845600Sbostic 	/* Set current node pointer. */
23942273Sbostic 	p = sp->fts_cur;
24039800Sbostic 
24145600Sbostic 	/* Save and zero out user instructions. */
24242273Sbostic 	instr = p->fts_instr;
24347194Sbostic 	p->fts_instr = FTS_NOINSTR;
24439800Sbostic 
24547194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
24639800Sbostic 	if (instr == FTS_AGAIN) {
24745600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
24852150Sbostic 		return (p);
24939800Sbostic 	}
25039800Sbostic 
25147194Sbostic 	/*
25247194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
25347194Sbostic 	 * SLNONE and recover.
254*52278Sbostic 	 *
255*52278Sbostic 	 * XXX
256*52278Sbostic 	 * Have to open a file descriptor to '.' so we can get back.
25747194Sbostic 	 */
25847194Sbostic 	if (instr == FTS_FOLLOW &&
25947194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
26045600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
26152150Sbostic 		return (p);
26242299Sbostic 	}
26342299Sbostic 
26445600Sbostic 	/* Directory in pre-order. */
26542299Sbostic 	if (p->fts_info == FTS_D) {
26645600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
26747194Sbostic 		if (instr == FTS_SKIP ||
26852209Sbostic 		    ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
26942273Sbostic 			if (sp->fts_child) {
27042273Sbostic 				fts_lfree(sp->fts_child);
27142273Sbostic 				sp->fts_child = NULL;
27239800Sbostic 			}
27342301Sbostic 			p->fts_info = FTS_DP;
27452150Sbostic 			return (p);
27542299Sbostic 		}
27642299Sbostic 
27747194Sbostic 		/*
27847194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
27947194Sbostic 		 * the read fails for any reason, or the directory is empty,
28047194Sbostic 		 * the fts_info field of the current node is set by fts_build.
28147220Sbostic 		 * If have already read and now fail to chdir, whack the list
28247220Sbostic 		 * to make the names come out right, and set the parent state
28347220Sbostic 		 * so the application will eventually get an error condition.
28447220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
28547220Sbostic 		 * at the root node -- if so, we have to get back or the root
28647220Sbostic 		 * node may be inaccessible.
28747194Sbostic 		 */
28847194Sbostic 		if (sp->fts_child) {
28942299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
290*52278Sbostic 				p->fts_errno = errno;
29147194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
29247194Sbostic 					p->fts_accpath =
29347194Sbostic 					    p->fts_parent->fts_accpath;
29442299Sbostic 			}
29552155Sbostic 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
29647220Sbostic 			if ISSET(FTS_STOP)
29752150Sbostic 				return (NULL);
29847220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
29947220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
30047220Sbostic 				SET(FTS_STOP);
30152150Sbostic 				return (NULL);
30247220Sbostic 			}
30352150Sbostic 			return (p);
30447220Sbostic 		}
30547194Sbostic 		p = sp->fts_child;
30642299Sbostic 		sp->fts_child = NULL;
30747194Sbostic 		goto name;
30839800Sbostic 	}
30939800Sbostic 
310*52278Sbostic 	/* Move to the next node on this level. */
31142299Sbostic next:	tmp = p;
31242299Sbostic 	if (p = p->fts_link) {
31347194Sbostic 		free(tmp);
31447194Sbostic 
31547194Sbostic 		/* If reached the top, load the paths for the next root. */
31647212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
31747404Sbostic 			fts_load(sp, p);
31852150Sbostic 			return (sp->fts_cur = p);
31947194Sbostic 		}
32042299Sbostic 
32147194Sbostic 		/* User may have called fts_set on the node. */
32247194Sbostic 		if (p->fts_instr == FTS_SKIP)
32347194Sbostic 			goto next;
324*52278Sbostic 		/*
325*52278Sbostic 		 * XXX
326*52278Sbostic 		 * This may not be able to return to the current directory.
327*52278Sbostic 		 */
32847194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
32947194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
33047194Sbostic 			p->fts_instr = FTS_NOINSTR;
33147194Sbostic 		}
33242299Sbostic 
33347194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
33447194Sbostic 		*t++ = '/';
33547194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
33652150Sbostic 		return (sp->fts_cur = p);
33739800Sbostic 	}
33839800Sbostic 
33947220Sbostic 	/* Move up to the parent node. */
34042299Sbostic 	p = tmp->fts_parent;
34147194Sbostic 	free(tmp);
34242299Sbostic 
34347212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
34439800Sbostic 		/*
34545600Sbostic 		 * Done; free everything up and set errno to 0 so the user
34639800Sbostic 		 * can distinguish between error and EOF.
34739800Sbostic 		 */
34847194Sbostic 		free(p);
34939800Sbostic 		errno = 0;
35052150Sbostic 		return (sp->fts_cur = NULL);
35139800Sbostic 	}
35239800Sbostic 
35342273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
35447194Sbostic 
35547194Sbostic 	/*
356*52278Sbostic 	 * If at a root node, have to cd back to the starting point, otherwise
357*52278Sbostic 	 * may not be able to access the original node on post-order.  If not
358*52278Sbostic 	 * a root node, cd up to the parent directory.  Note that errors are
359*52278Sbostic 	 * assumed to be caused by an inability to cd to the directory in the
360*52278Sbostic 	 * first place.
36147194Sbostic 	 */
36247220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
36347220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
36447220Sbostic 			SET(FTS_STOP);
36552150Sbostic 			return (NULL);
36647220Sbostic 		}
367*52278Sbostic 		p->fts_info = FTS_DP;
368*52278Sbostic 	} else if (p->fts_errno)
36942273Sbostic 		p->fts_info = FTS_ERR;
370*52278Sbostic 	else {
371*52278Sbostic 		if (CHDIR(sp, "..")) {
372*52278Sbostic 			SET(FTS_STOP);
373*52278Sbostic 			return (NULL);
374*52278Sbostic 		}
37542273Sbostic 		p->fts_info = FTS_DP;
376*52278Sbostic 	}
37752150Sbostic 	return (sp->fts_cur = p);
37839800Sbostic }
37939800Sbostic 
38039800Sbostic /*
38147220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
38239800Sbostic  * implementation; it would be necessary if anyone wanted to add global
38345600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
38445600Sbostic  * reasons.
38539800Sbostic  */
38639800Sbostic /* ARGSUSED */
38752209Sbostic int
38845600Sbostic fts_set(sp, p, instr)
38939800Sbostic 	FTS *sp;
39039800Sbostic 	FTSENT *p;
39139800Sbostic 	int instr;
39239800Sbostic {
39342273Sbostic 	p->fts_instr = instr;
39452150Sbostic 	return (0);
39539800Sbostic }
39639800Sbostic 
39739800Sbostic FTSENT *
39845600Sbostic fts_children(sp)
39939800Sbostic 	register FTS *sp;
40039800Sbostic {
40142299Sbostic 	register FTSENT *p;
40242299Sbostic 	int fd;
40342299Sbostic 
40445600Sbostic 	/* Set current node pointer. */
40545600Sbostic 	p = sp->fts_cur;
40645600Sbostic 
40739800Sbostic 	/*
40852160Sbostic 	 * Errno set to 0 so user can distinguish empty directory from
40952160Sbostic 	 * an error.
41039800Sbostic 	 */
41139800Sbostic 	errno = 0;
41252160Sbostic 
41352160Sbostic 	/* Fatal errors stop here. */
41452160Sbostic 	if (ISSET(FTS_STOP))
41552150Sbostic 		return (NULL);
41645600Sbostic 
41752160Sbostic 	/* Return logical hierarchy of user's arguments. */
41852160Sbostic 	if (p->fts_info == FTS_INIT)
41952160Sbostic 		return (p->fts_link);
42052160Sbostic 
421*52278Sbostic /* XXX why FTS_DNR?? */
42252160Sbostic 	 /* If not a directory being visited in pre-order, stop here. */
42352160Sbostic 	if (p->fts_info != FTS_D && p->fts_info != FTS_DNR)
42452160Sbostic 		return (NULL);
42552160Sbostic 
42645600Sbostic 	/* Free up any previous child list. */
42742273Sbostic 	if (sp->fts_child)
42842273Sbostic 		fts_lfree(sp->fts_child);
42942299Sbostic 
43042299Sbostic 	/*
43145600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
43247194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
43347194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
43447194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
43547194Sbostic 	 * fts_read will work.
43642299Sbostic 	 */
43747212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
43845600Sbostic 	    ISSET(FTS_NOCHDIR))
43952150Sbostic 		return (sp->fts_child = fts_build(sp, BCHILD));
44042299Sbostic 
44142299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
44252150Sbostic 		return (NULL);
44342299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
44442299Sbostic 	if (fchdir(fd))
44552150Sbostic 		return (NULL);
44642299Sbostic 	(void)close(fd);
44752150Sbostic 	return (sp->fts_child);
44839800Sbostic }
44939800Sbostic 
45047194Sbostic /*
45147194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
45247194Sbostic  * idea is to build the linked list of entries that are used by fts_children
45347194Sbostic  * and fts_read.  There are lots of special cases.
45447194Sbostic  *
45547194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
45647194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
45747194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
45847194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
45947194Sbostic  * directories and for any nodes after the directories in the parent node have
46047194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
46147194Sbostic  */
46247155Sbostic static FTSENT *
46342299Sbostic fts_build(sp, type)
46439800Sbostic 	register FTS *sp;
46542299Sbostic 	int type;
46639800Sbostic {
46739800Sbostic 	register struct dirent *dp;
46839800Sbostic 	register FTSENT *p, *head;
46939800Sbostic 	register int nitems;
47047194Sbostic 	FTSENT *cur;
47139800Sbostic 	DIR *dirp;
47252209Sbostic 	void *adjaddr;
473*52278Sbostic 	int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
47439800Sbostic 	char *cp;
47539800Sbostic 
47645600Sbostic 	/* Set current node pointer. */
47747194Sbostic 	cur = sp->fts_cur;
47845600Sbostic 
47947194Sbostic 	/*
48047194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
48147194Sbostic 	 * If being called from fts_read, set the fts_info field.
48247194Sbostic 	 */
48352155Sbostic 	if ((dirp = opendir(cur->fts_accpath)) == NULL) {
48452206Sbostic 		if (type == BREAD) {
48547194Sbostic 			cur->fts_info = FTS_DNR;
48652206Sbostic 			cur->fts_errno = errno;
48752206Sbostic 		}
48852150Sbostic 		return (NULL);
48939800Sbostic 	}
49039800Sbostic 
49139800Sbostic 	/*
49247194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
49347194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
49447194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
49539800Sbostic 	 */
49642273Sbostic 	nlinks =
49745600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
49852209Sbostic 	    cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
49939800Sbostic 
50047194Sbostic 	/*
50147194Sbostic 	 * If we're going to need to stat anything or we want to descend
50247194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
50347194Sbostic 	 * We won't be able to stat anything, but we can still return the
50447194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
50547194Sbostic 	 * chdir into the directory, it will have to return different path
50647194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
50747194Sbostic 	 * has already been visited in pre-order, have to wait until the
50847194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
50947194Sbostic 	 * If a program needed sorted entries or stat information, they had
51047194Sbostic 	 * better be checking FTS_NS on the returned nodes.
51147194Sbostic 	 */
51245600Sbostic 	if (nlinks || type == BREAD)
51347194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
51447194Sbostic 			if (type == BREAD)
51552160Sbostic 				cur->fts_errno = errno;
51647194Sbostic 			descend = nlinks = 0;
517*52278Sbostic 			cderrno = errno;
51847194Sbostic 		} else {
51945600Sbostic 			descend = 1;
520*52278Sbostic 			cderrno = 0;
52139962Sbostic 		}
52247194Sbostic 	else
52347194Sbostic 		descend = 0;
52439962Sbostic 
52547194Sbostic 	/*
52647194Sbostic 	 * Figure out the max file name length that can be stored in the
52747194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
52847194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
52947194Sbostic 	 * could do them in fts_read before returning the path, but it's a
53047194Sbostic 	 * lot easier here since the length is part of the dirent structure.
53147194Sbostic 	 *
53252209Sbostic 	 * If not changing directories set a pointer so that can just append
53352209Sbostic 	 * each new name into the path.
53447194Sbostic 	 */
53547194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
53647194Sbostic 	len = NAPPEND(cur);
53747194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
53847194Sbostic 		cp = sp->fts_path + len;
53947194Sbostic 		*cp++ = '/';
54047194Sbostic 	}
54140939Sbostic 
54247194Sbostic 	level = cur->fts_level + 1;
54340939Sbostic 
54447194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
54552209Sbostic 	adjaddr = NULL;
54639800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
54747194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
54839800Sbostic 			continue;
54939800Sbostic 
55052155Sbostic 		if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
55139800Sbostic 			goto mem1;
55239800Sbostic 		if (dp->d_namlen > maxlen) {
55352209Sbostic 			if (fts_palloc(sp, (int)dp->d_namlen)) {
55447194Sbostic 				/*
55547194Sbostic 				 * No more memory for path or structures.  Save
55647194Sbostic 				 * errno, free up the current structure and the
55747194Sbostic 				 * structures already allocated.
55847194Sbostic 				 */
55947194Sbostic mem1:				saved_errno = errno;
56047194Sbostic 				if (p)
56147194Sbostic 					free(p);
56247194Sbostic 				fts_lfree(head);
56347194Sbostic 				(void)closedir(dirp);
56439800Sbostic 				errno = saved_errno;
56547194Sbostic 				cur->fts_info = FTS_ERR;
56647194Sbostic 				SET(FTS_STOP);
56752150Sbostic 				return (NULL);
56839800Sbostic 			}
56952209Sbostic 			adjaddr = sp->fts_path;
57042273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
57139800Sbostic 		}
57239800Sbostic 
57342273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
57442273Sbostic 		p->fts_parent = sp->fts_cur;
57542273Sbostic 		p->fts_level = level;
57639800Sbostic 
57739800Sbostic 		if (nlinks) {
57847194Sbostic 			/* Build a file name for fts_stat to stat. */
57947194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
58047194Sbostic 				p->fts_accpath = p->fts_path;
58142273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
58247194Sbostic 			} else
58347194Sbostic 				p->fts_accpath = p->fts_name;
58445600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
58552209Sbostic 			if (nlinks > 0 && (p->fts_info == FTS_D ||
58652209Sbostic 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
58739800Sbostic 				--nlinks;
588*52278Sbostic 		} else if (cderrno) {
589*52278Sbostic 			if (ISSET(FTS_NOSTAT))
590*52278Sbostic 				p->fts_info = FTS_NSOK;
591*52278Sbostic 		        else {
592*52278Sbostic 				p->fts_info = FTS_NS;
593*52278Sbostic 				p->fts_errno = cderrno;
594*52278Sbostic 			}
59547194Sbostic 			p->fts_accpath = cur->fts_accpath;
59647194Sbostic 		} else {
59747194Sbostic 			p->fts_accpath =
59847194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
59947194Sbostic 			p->fts_info = FTS_NSOK;
60047194Sbostic 		}
60139800Sbostic 
60242273Sbostic 		p->fts_link = head;
60339800Sbostic 		head = p;
60439800Sbostic 		++nitems;
60539800Sbostic 	}
60639800Sbostic 	(void)closedir(dirp);
60739800Sbostic 
60847194Sbostic 	/*
60952209Sbostic 	 * If had to realloc the path, adjust the addresses for the rest
61052209Sbostic 	 * of the tree.
61152209Sbostic 	 */
61252209Sbostic 	if (adjaddr)
61352209Sbostic 		fts_padjust(sp, adjaddr);
61452209Sbostic 
61552209Sbostic 	/*
61647194Sbostic 	 * If not changing directories, reset the path back to original
61747194Sbostic 	 * state.
61847194Sbostic 	 */
61947194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
62047194Sbostic 		if (cp - 1 > sp->fts_path)
62147194Sbostic 			--cp;
62247194Sbostic 		*cp = '\0';
62347194Sbostic 	}
62442299Sbostic 
62542299Sbostic 	/*
62647194Sbostic 	 * If descended after called from fts_children or called from
62747194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
62852209Sbostic 	 * back, done.
62942299Sbostic 	 */
63042299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
63147194Sbostic 		cur->fts_info = FTS_ERR;
63247194Sbostic 		SET(FTS_STOP);
63352150Sbostic 		return (NULL);
63439962Sbostic 	}
63539962Sbostic 
63652209Sbostic 	/* If didn't find anything, just do the post-order visit */
63739800Sbostic 	if (!nitems) {
63842299Sbostic 		if (type == BREAD)
63947194Sbostic 			cur->fts_info = FTS_DP;
64052150Sbostic 		return (NULL);
64139800Sbostic 	}
64239800Sbostic 
64347194Sbostic 	/* Sort the entries. */
64442273Sbostic 	if (sp->fts_compar && nitems > 1)
64545600Sbostic 		head = fts_sort(sp, head, nitems);
64652150Sbostic 	return (head);
64739800Sbostic }
64839800Sbostic 
64945600Sbostic static u_short
65045600Sbostic fts_stat(sp, p, follow)
65145600Sbostic 	FTS *sp;
65239800Sbostic 	register FTSENT *p;
65345600Sbostic 	int follow;
65439800Sbostic {
65552160Sbostic 	register FTSENT *t;
65652160Sbostic 	register dev_t dev;
65752160Sbostic 	register ino_t ino;
65852209Sbostic 	struct stat *sbp, sb;
65947194Sbostic 	int saved_errno;
66047194Sbostic 
66152209Sbostic 	/* If user needs stat info, stat buffer already allocated. */
66252209Sbostic 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
66352209Sbostic 
66439800Sbostic 	/*
66545600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
66647194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
66752160Sbostic 	 * fail, set the errno from the stat call.
66839800Sbostic 	 */
66945600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
67052209Sbostic 		if (stat(p->fts_accpath, sbp)) {
67147194Sbostic 			saved_errno = errno;
67252209Sbostic 			if (!lstat(p->fts_accpath, sbp)) {
67347194Sbostic 				errno = 0;
67452150Sbostic 				return (FTS_SLNONE);
67547194Sbostic 			}
67652160Sbostic 			p->fts_errno = saved_errno;
67752209Sbostic 			goto err;
67845600Sbostic 		}
67952209Sbostic 	} else if (lstat(p->fts_accpath, sbp)) {
68052160Sbostic 		p->fts_errno = errno;
68152209Sbostic err:		bzero(sbp, sizeof(struct stat));
68252150Sbostic 		return (FTS_NS);
68345600Sbostic 	}
68439800Sbostic 
68552209Sbostic 	if (S_ISDIR(sbp->st_mode)) {
68652209Sbostic 		if (ISDOT(p->fts_name))
68752209Sbostic 			return (FTS_DOT);
68852209Sbostic 		/*
68952209Sbostic 		 * Set the device/inode.  Used to find cycles and check for
69052209Sbostic 		 * crossing mount points.  Also remember the link count, used
69152209Sbostic 		 * in fts_build to limit the number of stat calls.  It is
69252209Sbostic 		 * understood that these fields are only referenced if fts_info
69352209Sbostic 		 * is set to FTS_D.
69452209Sbostic 		 */
69552209Sbostic 		dev = p->fts_dev = sbp->st_dev;
69652209Sbostic 		ino = p->fts_ino = sbp->st_ino;
69752209Sbostic 		p->fts_nlink = sbp->st_nlink;
69852209Sbostic 
69952209Sbostic 		/*
70052209Sbostic 		 * Cycle detection is done by brute force when the directory
70152209Sbostic 		 * is first encountered.  If the tree gets deep enough or the
70252209Sbostic 		 * number of symbolic links to directories is high enough,
70352209Sbostic 		 * something faster might be worthwhile.
70452209Sbostic 		 */
70552209Sbostic 		for (t = p->fts_parent;
70652209Sbostic 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
70752209Sbostic 			if (ino == t->fts_ino && dev == t->fts_dev) {
70852073Sbostic 				p->fts_cycle = t;
70952150Sbostic 				return (FTS_DC);
71047194Sbostic 			}
71152150Sbostic 		return (FTS_D);
71247194Sbostic 	}
71352209Sbostic 	if (S_ISLNK(sbp->st_mode))
71452150Sbostic 		return (FTS_SL);
71552209Sbostic 	if (S_ISREG(sbp->st_mode))
71652150Sbostic 		return (FTS_F);
71752150Sbostic 	return (FTS_DEFAULT);
71839800Sbostic }
71939800Sbostic 
72039800Sbostic static FTSENT *
72145600Sbostic fts_sort(sp, head, nitems)
72245600Sbostic 	FTS *sp;
72339800Sbostic 	FTSENT *head;
72439800Sbostic 	register int nitems;
72539800Sbostic {
72639800Sbostic 	register FTSENT **ap, *p;
72739800Sbostic 
72839800Sbostic 	/*
72945600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
73039800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
73139800Sbostic 	 * sort for memory reasons, return the directory entries in their
73239800Sbostic 	 * current order.  Allocate enough space for the current needs plus
73352209Sbostic 	 * 40 so don't realloc one entry at a time.
73439800Sbostic 	 */
73545600Sbostic 	if (nitems > sp->fts_nitems) {
73645600Sbostic 		sp->fts_nitems = nitems + 40;
73752209Sbostic 		if ((sp->fts_array = realloc(sp->fts_array,
73852209Sbostic 		    (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
73945600Sbostic 			sp->fts_nitems = 0;
74052150Sbostic 			return (head);
74139800Sbostic 		}
74239800Sbostic 	}
74345600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
74439800Sbostic 		*ap++ = p;
74545600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
74645600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
74742273Sbostic 		ap[0]->fts_link = ap[1];
74842273Sbostic 	ap[0]->fts_link = NULL;
74952150Sbostic 	return (head);
75039800Sbostic }
75139800Sbostic 
75239800Sbostic static FTSENT *
75345600Sbostic fts_alloc(sp, name, len)
75445600Sbostic 	FTS *sp;
75539800Sbostic 	char *name;
75639800Sbostic 	register int len;
75739800Sbostic {
75839800Sbostic 	register FTSENT *p;
75952209Sbostic 	int needstat;
76039800Sbostic 
76139800Sbostic 	/*
76252209Sbostic 	 * Variable sized structures.  The stat structure isn't necessary
76352209Sbostic 	 * if the user doesn't need it, and the name is variable length.
76452209Sbostic 	 * Allocate enough extra space after the structure to store them.
76539800Sbostic 	 */
76652209Sbostic 	needstat = ISSET(FTS_NOSTAT) ? 0 : sizeof(struct stat);
76752209Sbostic 	if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL)
76852150Sbostic 		return (NULL);
76942273Sbostic 	bcopy(name, p->fts_name, len + 1);
77052209Sbostic 	if (needstat)
77152209Sbostic 		p->fts_statp = (struct stat *)(p->fts_name + len + 1);
77242273Sbostic 	p->fts_namelen = len;
77345600Sbostic 	p->fts_path = sp->fts_path;
77447194Sbostic 	p->fts_instr = FTS_NOINSTR;
77552160Sbostic 	p->fts_errno = 0;
77645600Sbostic 	p->fts_number = 0;
77752209Sbostic #ifdef NOT_NECESSARY
77845600Sbostic 	p->fts_pointer = NULL;
77952209Sbostic #endif
78052150Sbostic 	return (p);
78139800Sbostic }
78239800Sbostic 
78345600Sbostic static void
78439800Sbostic fts_lfree(head)
78539800Sbostic 	register FTSENT *head;
78639800Sbostic {
78739800Sbostic 	register FTSENT *p;
78839800Sbostic 
78945600Sbostic 	/* Free a linked list of structures. */
79039800Sbostic 	while (p = head) {
79142273Sbostic 		head = head->fts_link;
79247194Sbostic 		free(p);
79339800Sbostic 	}
79439800Sbostic }
79539800Sbostic 
79639800Sbostic /*
79752209Sbostic  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
79852209Sbostic  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
79952209Sbostic  * though the kernel won't resolve them.  Add the size (not just what's needed)
80052209Sbostic  * plus 256 bytes so don't realloc the path 2 bytes at a time.
80139800Sbostic  */
80252209Sbostic static int
80352209Sbostic fts_palloc(sp, more)
80445600Sbostic 	FTS *sp;
80552209Sbostic 	int more;
80639800Sbostic {
80752209Sbostic 
80852209Sbostic 	sp->fts_pathlen += more + 256;
80952209Sbostic 	sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
81052209Sbostic 	return (sp->fts_path == NULL);
81147155Sbostic }
81252209Sbostic 
81352209Sbostic /*
81452209Sbostic  * When the path is realloc'd, have to fix all of the pointers in structures
81552209Sbostic  * already returned.
81652209Sbostic  */
81752209Sbostic static void
81852209Sbostic fts_padjust(sp, addr)
81952209Sbostic 	FTS *sp;
82052209Sbostic 	void *addr;
82152209Sbostic {
82252209Sbostic 	FTSENT *p;
82352209Sbostic 
82452209Sbostic #define	ADJUST(p) { \
82552209Sbostic 	(p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \
82652209Sbostic 	(p)->fts_path = addr; \
82752209Sbostic }
82852209Sbostic 	/* Adjust the current set of children. */
82952209Sbostic 	for (p = sp->fts_child; p; p = p->fts_link)
83052209Sbostic 		ADJUST(p);
83152209Sbostic 
83252209Sbostic 	/* Adjust the rest of the tree. */
83352209Sbostic 	for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
83452209Sbostic 		ADJUST(p);
83552209Sbostic 		p = p->fts_link ? p->fts_link : p->fts_parent;
83652209Sbostic 	}
83752209Sbostic }
838