xref: /csrg-svn/lib/libc/gen/fts.c (revision 52206)
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*52206Sbostic static char sccsid[] = "@(#)fts.c	5.25 (Berkeley) 01/14/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 */
5352150Sbostic 	if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
5452150Sbostic 		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. */
6452155Sbostic 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
6539800Sbostic 		goto mem1;
6647212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
6739800Sbostic 
6845600Sbostic 	/* Allocate/initialize root(s). */
6952160Sbostic 	for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) {
7052160Sbostic 		/* Don't allow zero-length paths. */
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;
9252155Sbostic 			if (root == NULL)
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
10552160Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
10642281Sbostic 	 * so that everything about the "current" node is ignored.
10742281Sbostic 	 */
10852155Sbostic 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
10942281Sbostic 		goto mem2;
11042281Sbostic 	sp->fts_cur->fts_link = root;
11152160Sbostic 	sp->fts_cur->fts_info = FTS_INIT;
11242281Sbostic 
11352160Sbostic 	/*
11452160Sbostic 	 * Start out with at least 1K+ of path space, but enough, in any
11552160Sbostic 	 * case, to hold the user's paths.
11652160Sbostic 	 */
11752160Sbostic 	if (!fts_path(sp, MAX(maxlen + 1, MAXPATHLEN)))
11842281Sbostic 		goto mem3;
11939800Sbostic 
12039800Sbostic 	/*
12145600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
12242299Sbostic 	 * that we can get back here; this could be avoided for some paths,
12342299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
12442299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
12542299Sbostic 	 * descriptor we run anyway, just more slowly.
12639800Sbostic 	 */
12747194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
12845600Sbostic 		SET(FTS_NOCHDIR);
12939800Sbostic 
13052150Sbostic 	return (sp);
13139800Sbostic 
13247194Sbostic mem3:	free(sp->fts_cur);
13339800Sbostic mem2:	fts_lfree(root);
13447194Sbostic 	free(parent);
13547194Sbostic mem1:	free(sp);
13652150Sbostic 	return (NULL);
13739800Sbostic }
13839800Sbostic 
13947404Sbostic static void
14045600Sbostic fts_load(sp, p)
14145600Sbostic 	FTS *sp;
14239800Sbostic 	register FTSENT *p;
14339800Sbostic {
14439800Sbostic 	register int len;
14539800Sbostic 	register char *cp;
14639800Sbostic 
14739800Sbostic 	/*
14847220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
14947220Sbostic 	 * actually enter the directory until after the preorder visit, set
15047220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
15147220Sbostic 	 * place and the user can access the first node.
15239800Sbostic 	 */
15342273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
15445600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
15542273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
15639800Sbostic 		len = strlen(++cp);
15742273Sbostic 		bcopy(cp, p->fts_name, len + 1);
15842273Sbostic 		p->fts_namelen = len;
15939800Sbostic 	}
16045600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
16147194Sbostic 
16247403Sbostic 	p->fts_info = fts_stat(sp, p, 0);
16347194Sbostic 	sp->rdev = p->fts_statb.st_dev;
16439800Sbostic }
16539800Sbostic 
16645600Sbostic fts_close(sp)
16739800Sbostic 	FTS *sp;
16839800Sbostic {
16939800Sbostic 	register FTSENT *freep, *p;
17039800Sbostic 	int saved_errno;
17139800Sbostic 
17242281Sbostic 	if (sp->fts_cur) {
17342281Sbostic 		/*
17445600Sbostic 		 * This still works if we haven't read anything -- the dummy
17542281Sbostic 		 * structure points to the root list, so we step through to
17642281Sbostic 		 * the end of the root list which has a valid parent pointer.
17742281Sbostic 		 */
17847212Sbostic 		for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
17942281Sbostic 			freep = p;
18042281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
18147194Sbostic 			free(freep);
18239800Sbostic 		}
18347194Sbostic 		free(p);
18442281Sbostic 	}
18539800Sbostic 
18645600Sbostic 	/* Free up child linked list, sort array, path buffer. */
18742273Sbostic 	if (sp->fts_child)
18842273Sbostic 		fts_lfree(sp->fts_child);
18942273Sbostic 	if (sp->fts_array)
19047194Sbostic 		free(sp->fts_array);
19147194Sbostic 	free(sp->fts_path);
19239800Sbostic 
19347194Sbostic 	/* Return to original directory, save errno if necessary. */
19445600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
19547194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
19647194Sbostic 		(void)close(sp->fts_rfd);
19739800Sbostic 	}
19839800Sbostic 
19945600Sbostic 	/* Free up the stream pointer. */
20047194Sbostic 	free(sp);
20139800Sbostic 
20245600Sbostic 	/* Set errno and return. */
20345600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
20439800Sbostic 		errno = saved_errno;
20552150Sbostic 		return (-1);
20639800Sbostic 	}
20752150Sbostic 	return (0);
20839800Sbostic }
20939800Sbostic 
21047194Sbostic /*
21152160Sbostic  * Special case a root of "/" so that slashes aren't appended which would
21252160Sbostic  * cause paths to be written as "//foo".
21347194Sbostic  */
21447194Sbostic #define	NAPPEND(p) \
21547212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
21647194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
21747194Sbostic 
21839800Sbostic FTSENT *
21945600Sbostic fts_read(sp)
22039800Sbostic 	register FTS *sp;
22139800Sbostic {
22242299Sbostic 	register FTSENT *p, *tmp;
22339800Sbostic 	register int instr;
22447194Sbostic 	register char *t;
22539800Sbostic 
22645600Sbostic 	/* If finished or unrecoverable error, return NULL. */
22752155Sbostic 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
22852150Sbostic 		return (NULL);
22939800Sbostic 
23045600Sbostic 	/* Set current node pointer. */
23142273Sbostic 	p = sp->fts_cur;
23239800Sbostic 
23345600Sbostic 	/* Save and zero out user instructions. */
23442273Sbostic 	instr = p->fts_instr;
23547194Sbostic 	p->fts_instr = FTS_NOINSTR;
23639800Sbostic 
23747194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
23839800Sbostic 	if (instr == FTS_AGAIN) {
23945600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
24052150Sbostic 		return (p);
24139800Sbostic 	}
24239800Sbostic 
24347194Sbostic 	/*
24447194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
24547194Sbostic 	 * SLNONE and recover.
24647194Sbostic 	 */
24747194Sbostic 	if (instr == FTS_FOLLOW &&
24847194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
24945600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
25052150Sbostic 		return (p);
25142299Sbostic 	}
25242299Sbostic 
25345600Sbostic 	/* Directory in pre-order. */
25442299Sbostic 	if (p->fts_info == FTS_D) {
25545600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
25647194Sbostic 		if (instr == FTS_SKIP ||
25747194Sbostic 		    ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
25842273Sbostic 			if (sp->fts_child) {
25942273Sbostic 				fts_lfree(sp->fts_child);
26042273Sbostic 				sp->fts_child = NULL;
26139800Sbostic 			}
26242301Sbostic 			p->fts_info = FTS_DP;
26352150Sbostic 			return (p);
26442299Sbostic 		}
26542299Sbostic 
26647194Sbostic 		/*
26747194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
26847194Sbostic 		 * the read fails for any reason, or the directory is empty,
26947194Sbostic 		 * the fts_info field of the current node is set by fts_build.
27047220Sbostic 		 * If have already read and now fail to chdir, whack the list
27147220Sbostic 		 * to make the names come out right, and set the parent state
27247220Sbostic 		 * so the application will eventually get an error condition.
27347220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
27447220Sbostic 		 * at the root node -- if so, we have to get back or the root
27547220Sbostic 		 * node may be inaccessible.
27647194Sbostic 		 */
27747194Sbostic 		if (sp->fts_child) {
27842299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
27952160Sbostic 				p->fts_parent->fts_errno = errno;
28047194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
28147194Sbostic 					p->fts_accpath =
28247194Sbostic 					    p->fts_parent->fts_accpath;
28342299Sbostic 			}
28452155Sbostic 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
28547220Sbostic 			if ISSET(FTS_STOP)
28652150Sbostic 				return (NULL);
28747220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
28847220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
28947220Sbostic 				SET(FTS_STOP);
29052150Sbostic 				return (NULL);
29147220Sbostic 			}
29252150Sbostic 			return (p);
29347220Sbostic 		}
29447194Sbostic 		p = sp->fts_child;
29542299Sbostic 		sp->fts_child = NULL;
29647194Sbostic 		goto name;
29739800Sbostic 	}
29839800Sbostic 
29945600Sbostic 	/* Move to next node on this level. */
30042299Sbostic next:	tmp = p;
30142299Sbostic 	if (p = p->fts_link) {
30247194Sbostic 		free(tmp);
30347194Sbostic 
30447194Sbostic 		/* If reached the top, load the paths for the next root. */
30547212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
30647404Sbostic 			fts_load(sp, p);
30752150Sbostic 			return (sp->fts_cur = p);
30847194Sbostic 		}
30942299Sbostic 
31047194Sbostic 		/* User may have called fts_set on the node. */
31147194Sbostic 		if (p->fts_instr == FTS_SKIP)
31247194Sbostic 			goto next;
31347194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
31447194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
31547194Sbostic 			p->fts_instr = FTS_NOINSTR;
31647194Sbostic 		}
31742299Sbostic 
31847194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
31947194Sbostic 		*t++ = '/';
32047194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
32152150Sbostic 		return (sp->fts_cur = p);
32239800Sbostic 	}
32339800Sbostic 
32447220Sbostic 	/* Move up to the parent node. */
32542299Sbostic 	p = tmp->fts_parent;
32647194Sbostic 	free(tmp);
32742299Sbostic 
32847212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
32939800Sbostic 		/*
33045600Sbostic 		 * Done; free everything up and set errno to 0 so the user
33139800Sbostic 		 * can distinguish between error and EOF.
33239800Sbostic 		 */
33347194Sbostic 		free(p);
33439800Sbostic 		errno = 0;
33552150Sbostic 		return (sp->fts_cur = NULL);
33639800Sbostic 	}
33739800Sbostic 
33842273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
33947194Sbostic 
34047194Sbostic 	/*
34147220Sbostic 	 * Cd back up to the parent directory.  If at a root node, have to cd
34247220Sbostic 	 * back to the original place, otherwise may not be able to access the
34347220Sbostic 	 * original node on post-order.
34447194Sbostic 	 */
34547220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
34647220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
34747220Sbostic 			SET(FTS_STOP);
34852150Sbostic 			return (NULL);
34947220Sbostic 		}
35047220Sbostic 	}
35147220Sbostic 	else if (CHDIR(sp, "..")) {
35247220Sbostic 		SET(FTS_STOP);
35352150Sbostic 		return (NULL);
35447220Sbostic 	}
35547220Sbostic 
35647220Sbostic 	/*
35747220Sbostic 	 * If had a chdir error when trying to get into the directory, set the
35847220Sbostic 	 * info field to reflect this, and restore errno.  The error indicator
35947220Sbostic 	 * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
36047220Sbostic 	 * works.
36147220Sbostic 	 */
36252160Sbostic 	if (p->fts_errno) {
36352160Sbostic 		errno = p->fts_errno;
36452160Sbostic 		p->fts_errno = 0;
36542273Sbostic 		p->fts_info = FTS_ERR;
36647220Sbostic 	} else
36742273Sbostic 		p->fts_info = FTS_DP;
36852150Sbostic 	return (sp->fts_cur = p);
36939800Sbostic }
37039800Sbostic 
37139800Sbostic /*
37247220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
37339800Sbostic  * implementation; it would be necessary if anyone wanted to add global
37445600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
37545600Sbostic  * reasons.
37639800Sbostic  */
37739800Sbostic /* ARGSUSED */
37845600Sbostic fts_set(sp, p, instr)
37939800Sbostic 	FTS *sp;
38039800Sbostic 	FTSENT *p;
38139800Sbostic 	int instr;
38239800Sbostic {
38342273Sbostic 	p->fts_instr = instr;
38452150Sbostic 	return (0);
38539800Sbostic }
38639800Sbostic 
38739800Sbostic FTSENT *
38845600Sbostic fts_children(sp)
38939800Sbostic 	register FTS *sp;
39039800Sbostic {
39142299Sbostic 	register FTSENT *p;
39242299Sbostic 	int fd;
39342299Sbostic 
39445600Sbostic 	/* Set current node pointer. */
39545600Sbostic 	p = sp->fts_cur;
39645600Sbostic 
39739800Sbostic 	/*
39852160Sbostic 	 * Errno set to 0 so user can distinguish empty directory from
39952160Sbostic 	 * an error.
40039800Sbostic 	 */
40139800Sbostic 	errno = 0;
40252160Sbostic 
40352160Sbostic 	/* Fatal errors stop here. */
40452160Sbostic 	if (ISSET(FTS_STOP))
40552150Sbostic 		return (NULL);
40645600Sbostic 
40752160Sbostic 	/* Return logical hierarchy of user's arguments. */
40852160Sbostic 	if (p->fts_info == FTS_INIT)
40952160Sbostic 		return (p->fts_link);
41052160Sbostic 
41152160Sbostic 	 /* If not a directory being visited in pre-order, stop here. */
41252160Sbostic 	if (p->fts_info != FTS_D && p->fts_info != FTS_DNR)
41352160Sbostic 		return (NULL);
41452160Sbostic 
41545600Sbostic 	/* Free up any previous child list. */
41642273Sbostic 	if (sp->fts_child)
41742273Sbostic 		fts_lfree(sp->fts_child);
41842299Sbostic 
41942299Sbostic 	/*
42045600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
42147194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
42247194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
42347194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
42447194Sbostic 	 * fts_read will work.
42542299Sbostic 	 */
42647212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
42745600Sbostic 	    ISSET(FTS_NOCHDIR))
42852150Sbostic 		return (sp->fts_child = fts_build(sp, BCHILD));
42942299Sbostic 
43042299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
43152150Sbostic 		return (NULL);
43242299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
43342299Sbostic 	if (fchdir(fd))
43452150Sbostic 		return (NULL);
43542299Sbostic 	(void)close(fd);
43652150Sbostic 	return (sp->fts_child);
43739800Sbostic }
43839800Sbostic 
43947194Sbostic /*
44047194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
44147194Sbostic  * idea is to build the linked list of entries that are used by fts_children
44247194Sbostic  * and fts_read.  There are lots of special cases.
44347194Sbostic  *
44447194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
44547194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
44647194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
44747194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
44847194Sbostic  * directories and for any nodes after the directories in the parent node have
44947194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
45047194Sbostic  */
45139800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
45239800Sbostic 
45347155Sbostic static FTSENT *
45442299Sbostic fts_build(sp, type)
45539800Sbostic 	register FTS *sp;
45642299Sbostic 	int type;
45739800Sbostic {
45839800Sbostic 	register struct dirent *dp;
45939800Sbostic 	register FTSENT *p, *head;
46039800Sbostic 	register int nitems;
46147194Sbostic 	FTSENT *cur;
46239800Sbostic 	DIR *dirp;
46347194Sbostic 	int cderr, descend, len, level, maxlen, nlinks, saved_errno;
46439800Sbostic 	char *cp;
46539800Sbostic 
46645600Sbostic 	/* Set current node pointer. */
46747194Sbostic 	cur = sp->fts_cur;
46845600Sbostic 
46947194Sbostic 	/*
47047194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
47147194Sbostic 	 * If being called from fts_read, set the fts_info field.
47247194Sbostic 	 */
47352155Sbostic 	if ((dirp = opendir(cur->fts_accpath)) == NULL) {
474*52206Sbostic 		if (type == BREAD) {
47547194Sbostic 			cur->fts_info = FTS_DNR;
476*52206Sbostic 			cur->fts_errno = errno;
477*52206Sbostic 		}
47852150Sbostic 		return (NULL);
47939800Sbostic 	}
48039800Sbostic 
48139800Sbostic 	/*
48247194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
48347194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
48447194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
48539800Sbostic 	 */
48642273Sbostic 	nlinks =
48745600Sbostic 	    ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
48847194Sbostic 	    cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
48939800Sbostic 
49047194Sbostic 	/*
49147194Sbostic 	 * If we're going to need to stat anything or we want to descend
49247194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
49347194Sbostic 	 * We won't be able to stat anything, but we can still return the
49447194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
49547194Sbostic 	 * chdir into the directory, it will have to return different path
49647194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
49747194Sbostic 	 * has already been visited in pre-order, have to wait until the
49847194Sbostic 	 * post-order visit to return the error.  This is all fairly nasty.
49947194Sbostic 	 * If a program needed sorted entries or stat information, they had
50047194Sbostic 	 * better be checking FTS_NS on the returned nodes.
50147194Sbostic 	 */
50245600Sbostic 	if (nlinks || type == BREAD)
50347194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
50447194Sbostic 			if (type == BREAD)
50552160Sbostic 				cur->fts_errno = errno;
50647194Sbostic 			descend = nlinks = 0;
50747194Sbostic 			cderr = 1;
50847194Sbostic 		} else {
50945600Sbostic 			descend = 1;
51047194Sbostic 			cderr = 0;
51139962Sbostic 		}
51247194Sbostic 	else
51347194Sbostic 		descend = 0;
51439962Sbostic 
51547194Sbostic 	/*
51647194Sbostic 	 * Figure out the max file name length that can be stored in the
51747194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
51847194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
51947194Sbostic 	 * could do them in fts_read before returning the path, but it's a
52047194Sbostic 	 * lot easier here since the length is part of the dirent structure.
52147194Sbostic 	 *
52247194Sbostic 	 * If not changing directories set a pointer so that we can just
52347194Sbostic 	 * append each new name into the path.
52447194Sbostic 	 */
52547194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
52647194Sbostic 	len = NAPPEND(cur);
52747194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
52847194Sbostic 		cp = sp->fts_path + len;
52947194Sbostic 		*cp++ = '/';
53047194Sbostic 	}
53140939Sbostic 
53247194Sbostic 	level = cur->fts_level + 1;
53340939Sbostic 
53447194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
53539800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
53647194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
53739800Sbostic 			continue;
53839800Sbostic 
53952155Sbostic 		if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
54039800Sbostic 			goto mem1;
54139800Sbostic 		if (dp->d_namlen > maxlen) {
54245600Sbostic 			if (!fts_path(sp, (int)dp->d_namlen)) {
54347194Sbostic 				/*
54447194Sbostic 				 * No more memory for path or structures.  Save
54547194Sbostic 				 * errno, free up the current structure and the
54647194Sbostic 				 * structures already allocated.
54747194Sbostic 				 */
54847194Sbostic mem1:				saved_errno = errno;
54947194Sbostic 				if (p)
55047194Sbostic 					free(p);
55147194Sbostic 				fts_lfree(head);
55247194Sbostic 				(void)closedir(dirp);
55339800Sbostic 				errno = saved_errno;
55447194Sbostic 				cur->fts_info = FTS_ERR;
55547194Sbostic 				SET(FTS_STOP);
55652150Sbostic 				return (NULL);
55739800Sbostic 			}
55842273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
55939800Sbostic 		}
56039800Sbostic 
56142273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
56242273Sbostic 		p->fts_parent = sp->fts_cur;
56342273Sbostic 		p->fts_level = level;
56439800Sbostic 
56539800Sbostic 		if (nlinks) {
56647194Sbostic 			/* Build a file name for fts_stat to stat. */
56747194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
56847194Sbostic 				p->fts_accpath = p->fts_path;
56942273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
57047194Sbostic 			} else
57147194Sbostic 				p->fts_accpath = p->fts_name;
57245600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
57347194Sbostic 			if (nlinks > 0 && p->fts_info == FTS_D)
57439800Sbostic 				--nlinks;
57547194Sbostic 		} else if (cderr) {
57647194Sbostic 			p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
57747194Sbostic 			p->fts_accpath = cur->fts_accpath;
57847194Sbostic 		} else {
57947194Sbostic 			p->fts_accpath =
58047194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
58147194Sbostic 			p->fts_info = FTS_NSOK;
58247194Sbostic 		}
58339800Sbostic 
58442273Sbostic 		p->fts_link = head;
58539800Sbostic 		head = p;
58639800Sbostic 		++nitems;
58739800Sbostic 	}
58839800Sbostic 	(void)closedir(dirp);
58939800Sbostic 
59047194Sbostic 	/*
59147194Sbostic 	 * If not changing directories, reset the path back to original
59247194Sbostic 	 * state.
59347194Sbostic 	 */
59447194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
59547194Sbostic 		if (cp - 1 > sp->fts_path)
59647194Sbostic 			--cp;
59747194Sbostic 		*cp = '\0';
59847194Sbostic 	}
59942299Sbostic 
60042299Sbostic 	/*
60147194Sbostic 	 * If descended after called from fts_children or called from
60247194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
60347194Sbostic 	 * back, we're done.
60442299Sbostic 	 */
60542299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
60647194Sbostic 		cur->fts_info = FTS_ERR;
60747194Sbostic 		SET(FTS_STOP);
60852150Sbostic 		return (NULL);
60939962Sbostic 	}
61039962Sbostic 
61147194Sbostic 	/* If we didn't find anything, just do the post-order visit */
61239800Sbostic 	if (!nitems) {
61342299Sbostic 		if (type == BREAD)
61447194Sbostic 			cur->fts_info = FTS_DP;
61552150Sbostic 		return (NULL);
61639800Sbostic 	}
61739800Sbostic 
61847194Sbostic 	/* Sort the entries. */
61942273Sbostic 	if (sp->fts_compar && nitems > 1)
62045600Sbostic 		head = fts_sort(sp, head, nitems);
62152150Sbostic 	return (head);
62239800Sbostic }
62339800Sbostic 
62445600Sbostic static u_short
62545600Sbostic fts_stat(sp, p, follow)
62645600Sbostic 	FTS *sp;
62739800Sbostic 	register FTSENT *p;
62845600Sbostic 	int follow;
62939800Sbostic {
63052160Sbostic 	register FTSENT *t;
63152160Sbostic 	register dev_t dev;
63252160Sbostic 	register ino_t ino;
63347194Sbostic 	int saved_errno;
63447194Sbostic 
63539800Sbostic 	/*
63645600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
63747194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
63852160Sbostic 	 * fail, set the errno from the stat call.
63939800Sbostic 	 */
64045600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
64145600Sbostic 		if (stat(p->fts_accpath, &p->fts_statb)) {
64247194Sbostic 			saved_errno = errno;
64347194Sbostic 			if (!lstat(p->fts_accpath, &p->fts_statb)) {
64447194Sbostic 				errno = 0;
64552150Sbostic 				return (FTS_SLNONE);
64647194Sbostic 			}
64752160Sbostic 			p->fts_errno = saved_errno;
64847194Sbostic 			bzero(&p->fts_statb, sizeof(struct stat));
64952150Sbostic 			return (FTS_NS);
65045600Sbostic 		}
65145600Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb)) {
65252160Sbostic 		p->fts_errno = errno;
65347194Sbostic 		bzero(&p->fts_statb, sizeof(struct stat));
65452150Sbostic 		return (FTS_NS);
65545600Sbostic 	}
65639800Sbostic 
65747194Sbostic 	/*
65847194Sbostic 	 * Cycle detection is done as soon as we find a directory.  Detection
65947194Sbostic 	 * is by brute force; if the tree gets deep enough or the number of
66047194Sbostic 	 * symbolic links to directories high enough something faster might
66147194Sbostic 	 * be worthwhile.
66247194Sbostic 	 */
66347194Sbostic 	if (S_ISDIR(p->fts_statb.st_mode)) {
66447194Sbostic 		dev = p->fts_statb.st_dev;
66547194Sbostic 		ino = p->fts_statb.st_ino;
66652073Sbostic 		for (t = p->fts_parent; t->fts_level >= FTS_ROOTLEVEL;
66747194Sbostic 		    t = t->fts_parent)
66847194Sbostic 			if (ino == t->fts_statb.st_ino &&
66947194Sbostic 			    dev == t->fts_statb.st_dev) {
67052073Sbostic 				p->fts_cycle = t;
67152150Sbostic 				return (FTS_DC);
67247194Sbostic 			}
67352150Sbostic 		return (FTS_D);
67447194Sbostic 	}
67545600Sbostic 	if (S_ISLNK(p->fts_statb.st_mode))
67652150Sbostic 		return (FTS_SL);
67745600Sbostic 	if (S_ISREG(p->fts_statb.st_mode))
67852150Sbostic 		return (FTS_F);
67952150Sbostic 	return (FTS_DEFAULT);
68039800Sbostic }
68139800Sbostic 
68239800Sbostic #define	R(type, nelem, ptr) \
68352150Sbostic 	realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
68439800Sbostic 
68539800Sbostic static FTSENT *
68645600Sbostic fts_sort(sp, head, nitems)
68745600Sbostic 	FTS *sp;
68839800Sbostic 	FTSENT *head;
68939800Sbostic 	register int nitems;
69039800Sbostic {
69139800Sbostic 	register FTSENT **ap, *p;
69239800Sbostic 
69339800Sbostic 	/*
69445600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
69539800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
69639800Sbostic 	 * sort for memory reasons, return the directory entries in their
69739800Sbostic 	 * current order.  Allocate enough space for the current needs plus
69839800Sbostic 	 * 40 so we don't realloc one entry at a time.
69939800Sbostic 	 */
70045600Sbostic 	if (nitems > sp->fts_nitems) {
70145600Sbostic 		sp->fts_nitems = nitems + 40;
70252155Sbostic 		if ((sp->fts_array =
70352155Sbostic 		    R(FTSENT *, sp->fts_nitems, sp->fts_array)) == NULL) {
70445600Sbostic 			sp->fts_nitems = 0;
70552150Sbostic 			return (head);
70639800Sbostic 		}
70739800Sbostic 	}
70845600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
70939800Sbostic 		*ap++ = p;
71045600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
71145600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
71242273Sbostic 		ap[0]->fts_link = ap[1];
71342273Sbostic 	ap[0]->fts_link = NULL;
71452150Sbostic 	return (head);
71539800Sbostic }
71639800Sbostic 
71739800Sbostic static FTSENT *
71845600Sbostic fts_alloc(sp, name, len)
71945600Sbostic 	FTS *sp;
72039800Sbostic 	char *name;
72139800Sbostic 	register int len;
72239800Sbostic {
72339800Sbostic 	register FTSENT *p;
72439800Sbostic 
72539800Sbostic 	/*
72645600Sbostic 	 * Variable sized structures; the name is the last element so
72747194Sbostic 	 * we allocate enough extra space after the structure to store
72847194Sbostic 	 * it.
72939800Sbostic 	 */
73052150Sbostic 	if ((p = malloc((size_t)(sizeof(FTSENT) + len))) == NULL)
73152150Sbostic 		return (NULL);
73242273Sbostic 	bcopy(name, p->fts_name, len + 1);
73342273Sbostic 	p->fts_namelen = len;
73445600Sbostic 	p->fts_path = sp->fts_path;
73547194Sbostic 	p->fts_instr = FTS_NOINSTR;
73652160Sbostic 	p->fts_errno = 0;
73745600Sbostic 	p->fts_number = 0;
73845600Sbostic 	p->fts_pointer = NULL;
73952150Sbostic 	return (p);
74039800Sbostic }
74139800Sbostic 
74245600Sbostic static void
74339800Sbostic fts_lfree(head)
74439800Sbostic 	register FTSENT *head;
74539800Sbostic {
74639800Sbostic 	register FTSENT *p;
74739800Sbostic 
74845600Sbostic 	/* Free a linked list of structures. */
74939800Sbostic 	while (p = head) {
75042273Sbostic 		head = head->fts_link;
75147194Sbostic 		free(p);
75239800Sbostic 	}
75339800Sbostic }
75439800Sbostic 
75539800Sbostic /*
75645600Sbostic  * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
75745600Sbostic  * work on any tree.  Most systems will allow creation of paths much longer
75845600Sbostic  * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
75945600Sbostic  * 128 bytes to the requested size so that we don't realloc the path 2 bytes
76045600Sbostic  * at a time.
76139800Sbostic  */
76242299Sbostic static char *
76345600Sbostic fts_path(sp, size)
76445600Sbostic 	FTS *sp;
76539800Sbostic 	int size;
76639800Sbostic {
76745600Sbostic 	sp->fts_pathlen += size + 128;
76852150Sbostic 	return (sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
76947155Sbostic }
770