xref: /csrg-svn/lib/libc/gen/fts.c (revision 53096)
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*53096Sbostic static char sccsid[] = "@(#)fts.c	5.35 (Berkeley) 03/30/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 */
4052770Sbostic #define	BCHILD		1		/* fts_children */
4152770Sbostic #define	BNAMES		2		/* fts_children, names only */
4252770Sbostic #define	BREAD		3		/* fts_read */
4342299Sbostic 
4439800Sbostic FTS *
4545600Sbostic fts_open(argv, options, compar)
4647155Sbostic 	char * const *argv;
4739800Sbostic 	register int options;
4847194Sbostic 	int (*compar)();
4939800Sbostic {
5039800Sbostic 	register FTS *sp;
5139800Sbostic 	register FTSENT *p, *root;
5239800Sbostic 	register int nitems, maxlen;
5339800Sbostic 	FTSENT *parent, *tmp;
5447194Sbostic 	int len;
5539800Sbostic 
5652770Sbostic 	/* Options check. */
5752770Sbostic 	if (options & ~FTS_OPTIONMASK) {
5852770Sbostic 		errno = EINVAL;
5952770Sbostic 		return (NULL);
6052770Sbostic 	}
6152770Sbostic 
6245600Sbostic 	/* Allocate/initialize the stream */
6352150Sbostic 	if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
6452150Sbostic 		return (NULL);
6539800Sbostic 	bzero(sp, sizeof(FTS));
6642273Sbostic 	sp->fts_compar = compar;
6742273Sbostic 	sp->fts_options = options;
6839800Sbostic 
6945600Sbostic 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
7045600Sbostic 	if (ISSET(FTS_LOGICAL))
7145600Sbostic 		SET(FTS_NOCHDIR);
7245600Sbostic 
7345600Sbostic 	/* Allocate/initialize root's parent. */
7452155Sbostic 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
7539800Sbostic 		goto mem1;
7647212Sbostic 	parent->fts_level = FTS_ROOTPARENTLEVEL;
7739800Sbostic 
7845600Sbostic 	/* Allocate/initialize root(s). */
7952160Sbostic 	for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) {
8052160Sbostic 		/* Don't allow zero-length paths. */
8152209Sbostic 		if ((len = strlen(*argv)) == 0) {
8247194Sbostic 			errno = ENOENT;
8343070Sbostic 			goto mem2;
8447194Sbostic 		}
8547194Sbostic 		if (maxlen < len)
8647194Sbostic 			maxlen = len;
8752209Sbostic 
8847194Sbostic 		p = fts_alloc(sp, *argv, len);
8949519Sbostic 		p->fts_level = FTS_ROOTLEVEL;
9049519Sbostic 		p->fts_parent = parent;
9152218Sbostic 		p->fts_accpath = p->fts_name;
9252344Sbostic 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
9352209Sbostic 
9452209Sbostic 		/* Command-line "." and ".." are real directories. */
9552209Sbostic 		if (p->fts_info == FTS_DOT)
9652209Sbostic 			p->fts_info = FTS_D;
9752209Sbostic 
9843070Sbostic 		/*
9945600Sbostic 		 * If comparison routine supplied, traverse in sorted
10043070Sbostic 		 * order; otherwise traverse in the order specified.
10143070Sbostic 		 */
10243070Sbostic 		if (compar) {
10343070Sbostic 			p->fts_link = root;
10443070Sbostic 			root = p;
10543070Sbostic 		} else {
10643070Sbostic 			p->fts_link = NULL;
10752155Sbostic 			if (root == NULL)
10843070Sbostic 				tmp = root = p;
10943070Sbostic 			else {
11043070Sbostic 				tmp->fts_link = p;
11143070Sbostic 				tmp = p;
11239800Sbostic 			}
11339800Sbostic 		}
11439800Sbostic 	}
11543070Sbostic 	if (compar && nitems > 1)
11645600Sbostic 		root = fts_sort(sp, root, nitems);
11739800Sbostic 
11842281Sbostic 	/*
11945600Sbostic 	 * Allocate a dummy pointer and make fts_read think that we've just
12052160Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
12142281Sbostic 	 * so that everything about the "current" node is ignored.
12242281Sbostic 	 */
12352155Sbostic 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
12442281Sbostic 		goto mem2;
12542281Sbostic 	sp->fts_cur->fts_link = root;
12652160Sbostic 	sp->fts_cur->fts_info = FTS_INIT;
12742281Sbostic 
12852160Sbostic 	/*
12952209Sbostic 	 * Start out with more than 1K of path space, and enough, in any
13052160Sbostic 	 * case, to hold the user's paths.
13152160Sbostic 	 */
13252209Sbostic 	if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN)))
13342281Sbostic 		goto mem3;
13439800Sbostic 
13539800Sbostic 	/*
13645600Sbostic 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
13742299Sbostic 	 * that we can get back here; this could be avoided for some paths,
13842299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
13942299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
14042299Sbostic 	 * descriptor we run anyway, just more slowly.
14139800Sbostic 	 */
14247194Sbostic 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
14345600Sbostic 		SET(FTS_NOCHDIR);
14439800Sbostic 
14552150Sbostic 	return (sp);
14639800Sbostic 
14747194Sbostic mem3:	free(sp->fts_cur);
14839800Sbostic mem2:	fts_lfree(root);
14947194Sbostic 	free(parent);
15047194Sbostic mem1:	free(sp);
15152150Sbostic 	return (NULL);
15239800Sbostic }
15339800Sbostic 
15447404Sbostic static void
15545600Sbostic fts_load(sp, p)
15645600Sbostic 	FTS *sp;
15739800Sbostic 	register FTSENT *p;
15839800Sbostic {
15939800Sbostic 	register int len;
16039800Sbostic 	register char *cp;
16139800Sbostic 
16239800Sbostic 	/*
16347220Sbostic 	 * Load the stream structure for the next traversal.  Since we don't
16447220Sbostic 	 * actually enter the directory until after the preorder visit, set
16547220Sbostic 	 * the fts_accpath field specially so the chdir gets done to the right
16652209Sbostic 	 * place and the user can access the first node.  From fts_open it's
16752209Sbostic 	 * known that the path will fit.
16839800Sbostic 	 */
16942273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
17045600Sbostic 	bcopy(p->fts_name, sp->fts_path, len + 1);
17142273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
17239800Sbostic 		len = strlen(++cp);
17342273Sbostic 		bcopy(cp, p->fts_name, len + 1);
17442273Sbostic 		p->fts_namelen = len;
17539800Sbostic 	}
17645600Sbostic 	p->fts_accpath = p->fts_path = sp->fts_path;
17752209Sbostic 	sp->fts_dev = p->fts_dev;
17839800Sbostic }
17939800Sbostic 
18052209Sbostic int
18145600Sbostic fts_close(sp)
18239800Sbostic 	FTS *sp;
18339800Sbostic {
18439800Sbostic 	register FTSENT *freep, *p;
18539800Sbostic 	int saved_errno;
18639800Sbostic 
18752209Sbostic 	/*
18852209Sbostic 	 * This still works if we haven't read anything -- the dummy structure
18952209Sbostic 	 * points to the root list, so we step through to the end of the root
19052209Sbostic 	 * list which has a valid parent pointer.
19152209Sbostic 	 */
19242281Sbostic 	if (sp->fts_cur) {
19352209Sbostic 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
19442281Sbostic 			freep = p;
19542281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
19647194Sbostic 			free(freep);
19739800Sbostic 		}
19847194Sbostic 		free(p);
19942281Sbostic 	}
20039800Sbostic 
20145600Sbostic 	/* Free up child linked list, sort array, path buffer. */
20242273Sbostic 	if (sp->fts_child)
20342273Sbostic 		fts_lfree(sp->fts_child);
20442273Sbostic 	if (sp->fts_array)
20547194Sbostic 		free(sp->fts_array);
20647194Sbostic 	free(sp->fts_path);
20739800Sbostic 
20847194Sbostic 	/* Return to original directory, save errno if necessary. */
20945600Sbostic 	if (!ISSET(FTS_NOCHDIR)) {
21047194Sbostic 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
21147194Sbostic 		(void)close(sp->fts_rfd);
21239800Sbostic 	}
21339800Sbostic 
21445600Sbostic 	/* Free up the stream pointer. */
21547194Sbostic 	free(sp);
21639800Sbostic 
21745600Sbostic 	/* Set errno and return. */
21845600Sbostic 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
21939800Sbostic 		errno = saved_errno;
22052150Sbostic 		return (-1);
22139800Sbostic 	}
22252150Sbostic 	return (0);
22339800Sbostic }
22439800Sbostic 
22547194Sbostic /*
22652160Sbostic  * Special case a root of "/" so that slashes aren't appended which would
22752160Sbostic  * cause paths to be written as "//foo".
22847194Sbostic  */
22947194Sbostic #define	NAPPEND(p) \
23047212Sbostic 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
23147194Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
23247194Sbostic 
23339800Sbostic FTSENT *
23445600Sbostic fts_read(sp)
23539800Sbostic 	register FTS *sp;
23639800Sbostic {
23742299Sbostic 	register FTSENT *p, *tmp;
23839800Sbostic 	register int instr;
23947194Sbostic 	register char *t;
24053032Sbostic 	int saved_errno;
24139800Sbostic 
24245600Sbostic 	/* If finished or unrecoverable error, return NULL. */
24352155Sbostic 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
24452150Sbostic 		return (NULL);
24539800Sbostic 
24645600Sbostic 	/* Set current node pointer. */
24742273Sbostic 	p = sp->fts_cur;
24839800Sbostic 
24945600Sbostic 	/* Save and zero out user instructions. */
25042273Sbostic 	instr = p->fts_instr;
25147194Sbostic 	p->fts_instr = FTS_NOINSTR;
25239800Sbostic 
25347194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
25439800Sbostic 	if (instr == FTS_AGAIN) {
25545600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
25652150Sbostic 		return (p);
25739800Sbostic 	}
25839800Sbostic 
25947194Sbostic 	/*
26047194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
26152290Sbostic 	 * SLNONE and recover.  If indirecting through a symlink, have
26252290Sbostic 	 * keep a pointer to current location.  If unable to get that
26352290Sbostic 	 * pointer, follow fails.
26447194Sbostic 	 */
26547194Sbostic 	if (instr == FTS_FOLLOW &&
26647194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
26745600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
26852290Sbostic 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
26952290Sbostic 			if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
27052290Sbostic 				p->fts_errno = errno;
27152290Sbostic 				p->fts_info = FTS_ERR;
27252290Sbostic 			} else
27352290Sbostic 				p->fts_flags |= FTS_SYMFOLLOW;
27452150Sbostic 		return (p);
27542299Sbostic 	}
27642299Sbostic 
27745600Sbostic 	/* Directory in pre-order. */
27842299Sbostic 	if (p->fts_info == FTS_D) {
27945600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
28047194Sbostic 		if (instr == FTS_SKIP ||
28152209Sbostic 		    ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
28252290Sbostic 			if (p->fts_flags & FTS_SYMFOLLOW)
28352290Sbostic 				(void)close(p->fts_symfd);
28442273Sbostic 			if (sp->fts_child) {
28542273Sbostic 				fts_lfree(sp->fts_child);
28642273Sbostic 				sp->fts_child = NULL;
28739800Sbostic 			}
28842301Sbostic 			p->fts_info = FTS_DP;
28952150Sbostic 			return (p);
29042299Sbostic 		}
29142299Sbostic 
292*53096Sbostic 		/* Rebuild if only read the names and now traversing. */
29352770Sbostic 		if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
29452770Sbostic 			sp->fts_options &= ~FTS_NAMEONLY;
29552770Sbostic 			fts_lfree(sp->fts_child);
29652770Sbostic 			sp->fts_child = NULL;
29752770Sbostic 		}
29852770Sbostic 
29947194Sbostic 		/*
300*53096Sbostic 		 * Cd to the subdirectory.
301*53096Sbostic 		 *
30247220Sbostic 		 * If have already read and now fail to chdir, whack the list
303*53096Sbostic 		 * to make the names come out right, and set the parent errno
30447220Sbostic 		 * so the application will eventually get an error condition.
305*53096Sbostic 		 * Set the FTS_DONTCHDIR flag so that when we logically change
306*53096Sbostic 		 * directories back to the parent we don't do a chdir.
307*53096Sbostic 		 *
308*53096Sbostic 		 * If haven't read do so.  If the read fails, fts_build sets
309*53096Sbostic 		 * FTS_STOP or the fts_info field of the node.
31047194Sbostic 		 */
31147194Sbostic 		if (sp->fts_child) {
31242299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
31352278Sbostic 				p->fts_errno = errno;
31452290Sbostic 				p->fts_flags |= FTS_DONTCHDIR;
31547194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
31647194Sbostic 					p->fts_accpath =
31747194Sbostic 					    p->fts_parent->fts_accpath;
31842299Sbostic 			}
31952155Sbostic 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
32052290Sbostic 			if (ISSET(FTS_STOP))
32152150Sbostic 				return (NULL);
32252150Sbostic 			return (p);
32347220Sbostic 		}
32447194Sbostic 		p = sp->fts_child;
32542299Sbostic 		sp->fts_child = NULL;
32647194Sbostic 		goto name;
32739800Sbostic 	}
32839800Sbostic 
32952278Sbostic 	/* Move to the next node on this level. */
33042299Sbostic next:	tmp = p;
33142299Sbostic 	if (p = p->fts_link) {
33247194Sbostic 		free(tmp);
33347194Sbostic 
33447194Sbostic 		/* If reached the top, load the paths for the next root. */
33547212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
33647404Sbostic 			fts_load(sp, p);
33752150Sbostic 			return (sp->fts_cur = p);
33847194Sbostic 		}
33942299Sbostic 
34052290Sbostic 		/*
34152290Sbostic 		 * User may have called fts_set on the node.  If skipped,
34252290Sbostic 		 * ignore.  If followed, get a file descriptor so we can
34352290Sbostic 		 * get back if necessary.
34452290Sbostic 		 */
34547194Sbostic 		if (p->fts_instr == FTS_SKIP)
34647194Sbostic 			goto next;
34747194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
34847194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
34952290Sbostic 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
35052290Sbostic 				if ((p->fts_symfd =
35152290Sbostic 				    open(".", O_RDONLY, 0)) < 0) {
35252290Sbostic 					p->fts_errno = errno;
35352290Sbostic 					p->fts_info = FTS_ERR;
35452290Sbostic 				} else
35552290Sbostic 					p->fts_flags |= FTS_SYMFOLLOW;
35647194Sbostic 			p->fts_instr = FTS_NOINSTR;
35747194Sbostic 		}
35842299Sbostic 
35947194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
36047194Sbostic 		*t++ = '/';
36147194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
36252150Sbostic 		return (sp->fts_cur = p);
36339800Sbostic 	}
36439800Sbostic 
36547220Sbostic 	/* Move up to the parent node. */
36642299Sbostic 	p = tmp->fts_parent;
36747194Sbostic 	free(tmp);
36842299Sbostic 
36947212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
37039800Sbostic 		/*
37145600Sbostic 		 * Done; free everything up and set errno to 0 so the user
37239800Sbostic 		 * can distinguish between error and EOF.
37339800Sbostic 		 */
37447194Sbostic 		free(p);
37539800Sbostic 		errno = 0;
37652150Sbostic 		return (sp->fts_cur = NULL);
37739800Sbostic 	}
37839800Sbostic 
379*53096Sbostic 	/* Nul terminate the pathname. */
38042273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
38147194Sbostic 
38247194Sbostic 	/*
383*53096Sbostic 	 * Return to the parent directory.  If at a root node or came through
384*53096Sbostic 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
385*53096Sbostic 	 * one directory.
38647194Sbostic 	 */
38747220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
388*53096Sbostic 		if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
38953032Sbostic 			saved_errno = errno;
39052290Sbostic 			(void)close(sp->fts_rfd);
39153032Sbostic 			errno = saved_errno;
39247220Sbostic 			SET(FTS_STOP);
39352150Sbostic 			return (NULL);
39447220Sbostic 		}
39552290Sbostic 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
39652290Sbostic 		if (FCHDIR(sp, p->fts_symfd)) {
39753032Sbostic 			saved_errno = errno;
39852290Sbostic 			(void)close(p->fts_symfd);
39953032Sbostic 			errno = saved_errno;
40052290Sbostic 			SET(FTS_STOP);
40152290Sbostic 			return (NULL);
40252290Sbostic 		}
40352290Sbostic 		(void)close(p->fts_symfd);
40452290Sbostic 	} else if (!(p->fts_flags & FTS_DONTCHDIR)) {
40552278Sbostic 		if (CHDIR(sp, "..")) {
40652278Sbostic 			SET(FTS_STOP);
40752278Sbostic 			return (NULL);
40852278Sbostic 		}
40952278Sbostic 	}
41052290Sbostic 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
41152150Sbostic 	return (sp->fts_cur = p);
41239800Sbostic }
41339800Sbostic 
41439800Sbostic /*
41547220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
41639800Sbostic  * implementation; it would be necessary if anyone wanted to add global
41745600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
41845600Sbostic  * reasons.
41939800Sbostic  */
42039800Sbostic /* ARGSUSED */
42152209Sbostic int
42245600Sbostic fts_set(sp, p, instr)
42339800Sbostic 	FTS *sp;
42439800Sbostic 	FTSENT *p;
42539800Sbostic 	int instr;
42639800Sbostic {
42752770Sbostic 	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
42852770Sbostic 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
42952770Sbostic 		errno = EINVAL;
43052770Sbostic 		return (1);
43152770Sbostic 	}
43242273Sbostic 	p->fts_instr = instr;
43352150Sbostic 	return (0);
43439800Sbostic }
43539800Sbostic 
43639800Sbostic FTSENT *
43752770Sbostic fts_children(sp, instr)
43839800Sbostic 	register FTS *sp;
43952770Sbostic 	int instr;
44039800Sbostic {
44142299Sbostic 	register FTSENT *p;
44242299Sbostic 	int fd;
44342299Sbostic 
44452770Sbostic 	if (instr && instr != FTS_NAMEONLY) {
44552770Sbostic 		errno = EINVAL;
44652770Sbostic 		return (NULL);
44752770Sbostic 	}
44852770Sbostic 
44945600Sbostic 	/* Set current node pointer. */
45045600Sbostic 	p = sp->fts_cur;
45145600Sbostic 
45239800Sbostic 	/*
45352160Sbostic 	 * Errno set to 0 so user can distinguish empty directory from
45452160Sbostic 	 * an error.
45539800Sbostic 	 */
45639800Sbostic 	errno = 0;
45752160Sbostic 
45852160Sbostic 	/* Fatal errors stop here. */
45952160Sbostic 	if (ISSET(FTS_STOP))
46052150Sbostic 		return (NULL);
46145600Sbostic 
46252160Sbostic 	/* Return logical hierarchy of user's arguments. */
46352160Sbostic 	if (p->fts_info == FTS_INIT)
46452160Sbostic 		return (p->fts_link);
46552160Sbostic 
46652290Sbostic 	/*
46752290Sbostic 	 * If not a directory being visited in pre-order, stop here.  Could
46852290Sbostic 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
46952290Sbostic 	 * same effect is available with FTS_AGAIN.
47052290Sbostic 	 */
47152290Sbostic 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
47252160Sbostic 		return (NULL);
47352160Sbostic 
47445600Sbostic 	/* Free up any previous child list. */
47542273Sbostic 	if (sp->fts_child)
47642273Sbostic 		fts_lfree(sp->fts_child);
47742299Sbostic 
47852770Sbostic 	if (instr == FTS_NAMEONLY) {
47952770Sbostic 		sp->fts_options |= FTS_NAMEONLY;
48052770Sbostic 		instr = BNAMES;
48152770Sbostic 	} else
48252770Sbostic 		instr = BCHILD;
48352770Sbostic 
48442299Sbostic 	/*
48545600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
48647194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
48747194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
48847194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
48947194Sbostic 	 * fts_read will work.
49042299Sbostic 	 */
49147212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
49245600Sbostic 	    ISSET(FTS_NOCHDIR))
49352770Sbostic 		return (sp->fts_child = fts_build(sp, instr));
49442299Sbostic 
49542299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
49652150Sbostic 		return (NULL);
49752770Sbostic 	sp->fts_child = fts_build(sp, instr);
49842299Sbostic 	if (fchdir(fd))
49952150Sbostic 		return (NULL);
50042299Sbostic 	(void)close(fd);
50152150Sbostic 	return (sp->fts_child);
50239800Sbostic }
50339800Sbostic 
50447194Sbostic /*
50547194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
50647194Sbostic  * idea is to build the linked list of entries that are used by fts_children
50747194Sbostic  * and fts_read.  There are lots of special cases.
50847194Sbostic  *
50947194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
51047194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
51147194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
51247194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
51347194Sbostic  * directories and for any nodes after the directories in the parent node have
51447194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
51547194Sbostic  */
51647155Sbostic static FTSENT *
51742299Sbostic fts_build(sp, type)
51839800Sbostic 	register FTS *sp;
51942299Sbostic 	int type;
52039800Sbostic {
52139800Sbostic 	register struct dirent *dp;
52239800Sbostic 	register FTSENT *p, *head;
52339800Sbostic 	register int nitems;
52452290Sbostic 	FTSENT *cur, *tail;
52539800Sbostic 	DIR *dirp;
52652209Sbostic 	void *adjaddr;
52752278Sbostic 	int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
52839800Sbostic 	char *cp;
52939800Sbostic 
53045600Sbostic 	/* Set current node pointer. */
53147194Sbostic 	cur = sp->fts_cur;
53245600Sbostic 
53347194Sbostic 	/*
53447194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
53547194Sbostic 	 * If being called from fts_read, set the fts_info field.
53647194Sbostic 	 */
53752155Sbostic 	if ((dirp = opendir(cur->fts_accpath)) == NULL) {
53852206Sbostic 		if (type == BREAD) {
53947194Sbostic 			cur->fts_info = FTS_DNR;
54052206Sbostic 			cur->fts_errno = errno;
54152206Sbostic 		}
54252150Sbostic 		return (NULL);
54339800Sbostic 	}
54439800Sbostic 
54539800Sbostic 	/*
54647194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
54747194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
54847194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
54939800Sbostic 	 */
55052770Sbostic 	if (type == BNAMES)
55152770Sbostic 		nlinks = 0;
55252770Sbostic 	else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
55352770Sbostic 		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
55452770Sbostic 	else
55552770Sbostic 		nlinks = -1;
55639800Sbostic 
55752290Sbostic #ifdef notdef
55852290Sbostic 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
55952290Sbostic 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
56052290Sbostic 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
56152290Sbostic #endif
56247194Sbostic 	/*
56347194Sbostic 	 * If we're going to need to stat anything or we want to descend
56447194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
56547194Sbostic 	 * We won't be able to stat anything, but we can still return the
56647194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
56747194Sbostic 	 * chdir into the directory, it will have to return different path
56847194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
56947194Sbostic 	 * has already been visited in pre-order, have to wait until the
57052290Sbostic 	 * post-order visit to return the error.  There is a special case
57152290Sbostic 	 * here, if there was nothing to stat then it's not an error to
57252290Sbostic 	 * not be able to stat.  This is all fairly nasty.  If a program
57352290Sbostic 	 * needed sorted entries or stat information, they had better be
57452290Sbostic 	 * checking FTS_NS on the returned nodes.
57547194Sbostic 	 */
57645600Sbostic 	if (nlinks || type == BREAD)
57747194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
57852290Sbostic 			if (nlinks && type == BREAD)
57952160Sbostic 				cur->fts_errno = errno;
58052290Sbostic 			descend = 0;
58152278Sbostic 			cderrno = errno;
58247194Sbostic 		} else {
58345600Sbostic 			descend = 1;
58452278Sbostic 			cderrno = 0;
58539962Sbostic 		}
58647194Sbostic 	else
58747194Sbostic 		descend = 0;
58839962Sbostic 
58947194Sbostic 	/*
59047194Sbostic 	 * Figure out the max file name length that can be stored in the
59147194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
59247194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
59347194Sbostic 	 * could do them in fts_read before returning the path, but it's a
59447194Sbostic 	 * lot easier here since the length is part of the dirent structure.
59547194Sbostic 	 *
59652209Sbostic 	 * If not changing directories set a pointer so that can just append
59752209Sbostic 	 * each new name into the path.
59847194Sbostic 	 */
59947194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
60047194Sbostic 	len = NAPPEND(cur);
60147194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
60247194Sbostic 		cp = sp->fts_path + len;
60347194Sbostic 		*cp++ = '/';
60447194Sbostic 	}
60540939Sbostic 
60647194Sbostic 	level = cur->fts_level + 1;
60740939Sbostic 
60847194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
60952209Sbostic 	adjaddr = NULL;
61052290Sbostic 	for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
61147194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
61239800Sbostic 			continue;
61339800Sbostic 
61452155Sbostic 		if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
61539800Sbostic 			goto mem1;
61639800Sbostic 		if (dp->d_namlen > maxlen) {
61752209Sbostic 			if (fts_palloc(sp, (int)dp->d_namlen)) {
61847194Sbostic 				/*
61947194Sbostic 				 * No more memory for path or structures.  Save
62047194Sbostic 				 * errno, free up the current structure and the
62147194Sbostic 				 * structures already allocated.
62247194Sbostic 				 */
62347194Sbostic mem1:				saved_errno = errno;
62447194Sbostic 				if (p)
62547194Sbostic 					free(p);
62647194Sbostic 				fts_lfree(head);
62747194Sbostic 				(void)closedir(dirp);
62839800Sbostic 				errno = saved_errno;
62947194Sbostic 				cur->fts_info = FTS_ERR;
63047194Sbostic 				SET(FTS_STOP);
63152150Sbostic 				return (NULL);
63239800Sbostic 			}
63352209Sbostic 			adjaddr = sp->fts_path;
63442273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
63539800Sbostic 		}
63639800Sbostic 
63742273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
63842273Sbostic 		p->fts_parent = sp->fts_cur;
63942273Sbostic 		p->fts_level = level;
64039800Sbostic 
64152290Sbostic 		if (cderrno) {
64252290Sbostic 			if (nlinks) {
64352290Sbostic 				p->fts_info = FTS_NS;
64452290Sbostic 				p->fts_errno = cderrno;
64552290Sbostic 			} else
64652290Sbostic 				p->fts_info = FTS_NSOK;
64752290Sbostic 			p->fts_accpath = cur->fts_accpath;
64852290Sbostic 		} else if (nlinks) {
64947194Sbostic 			/* Build a file name for fts_stat to stat. */
65047194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
65147194Sbostic 				p->fts_accpath = p->fts_path;
65242273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
65347194Sbostic 			} else
65447194Sbostic 				p->fts_accpath = p->fts_name;
65545600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
65652209Sbostic 			if (nlinks > 0 && (p->fts_info == FTS_D ||
65752209Sbostic 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
65839800Sbostic 				--nlinks;
65947194Sbostic 		} else {
66047194Sbostic 			p->fts_accpath =
66147194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
66247194Sbostic 			p->fts_info = FTS_NSOK;
66347194Sbostic 		}
66439800Sbostic 
66552290Sbostic 		/* We walk in directory order so "ls -f" doesn't get upset. */
66652290Sbostic 		p->fts_link = NULL;
66752290Sbostic 		if (head == NULL)
66852290Sbostic 			head = tail = p;
66952290Sbostic 		else {
67052290Sbostic 			tail->fts_link = p;
67152290Sbostic 			tail = p;
67252290Sbostic 		}
67339800Sbostic 		++nitems;
67439800Sbostic 	}
67539800Sbostic 	(void)closedir(dirp);
67639800Sbostic 
67747194Sbostic 	/*
67852209Sbostic 	 * If had to realloc the path, adjust the addresses for the rest
67952209Sbostic 	 * of the tree.
68052209Sbostic 	 */
68152209Sbostic 	if (adjaddr)
68252209Sbostic 		fts_padjust(sp, adjaddr);
68352209Sbostic 
68452209Sbostic 	/*
68547194Sbostic 	 * If not changing directories, reset the path back to original
68647194Sbostic 	 * state.
68747194Sbostic 	 */
68847194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
68947194Sbostic 		if (cp - 1 > sp->fts_path)
69047194Sbostic 			--cp;
69147194Sbostic 		*cp = '\0';
69247194Sbostic 	}
69342299Sbostic 
69442299Sbostic 	/*
69547194Sbostic 	 * If descended after called from fts_children or called from
69647194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
69752209Sbostic 	 * back, done.
69842299Sbostic 	 */
69942299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
70047194Sbostic 		cur->fts_info = FTS_ERR;
70147194Sbostic 		SET(FTS_STOP);
70252150Sbostic 		return (NULL);
70339962Sbostic 	}
70439962Sbostic 
70553032Sbostic 	/* If didn't find anything, return NULL. */
706*53096Sbostic 	if (!nitems) {
707*53096Sbostic 		if (type == BREAD)
708*53096Sbostic 			cur->fts_info = FTS_DP;
70952150Sbostic 		return (NULL);
710*53096Sbostic 	}
71139800Sbostic 
71247194Sbostic 	/* Sort the entries. */
71342273Sbostic 	if (sp->fts_compar && nitems > 1)
71445600Sbostic 		head = fts_sort(sp, head, nitems);
71552150Sbostic 	return (head);
71639800Sbostic }
71739800Sbostic 
71845600Sbostic static u_short
71945600Sbostic fts_stat(sp, p, follow)
72045600Sbostic 	FTS *sp;
72139800Sbostic 	register FTSENT *p;
72245600Sbostic 	int follow;
72339800Sbostic {
72452160Sbostic 	register FTSENT *t;
72552160Sbostic 	register dev_t dev;
72652160Sbostic 	register ino_t ino;
72752209Sbostic 	struct stat *sbp, sb;
72847194Sbostic 	int saved_errno;
72947194Sbostic 
73052209Sbostic 	/* If user needs stat info, stat buffer already allocated. */
73152209Sbostic 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
73252209Sbostic 
73339800Sbostic 	/*
73445600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
73547194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
73652160Sbostic 	 * fail, set the errno from the stat call.
73739800Sbostic 	 */
73845600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
73952209Sbostic 		if (stat(p->fts_accpath, sbp)) {
74047194Sbostic 			saved_errno = errno;
74152209Sbostic 			if (!lstat(p->fts_accpath, sbp)) {
74247194Sbostic 				errno = 0;
74352150Sbostic 				return (FTS_SLNONE);
74447194Sbostic 			}
74552160Sbostic 			p->fts_errno = saved_errno;
74652209Sbostic 			goto err;
74745600Sbostic 		}
74852209Sbostic 	} else if (lstat(p->fts_accpath, sbp)) {
74952160Sbostic 		p->fts_errno = errno;
75052209Sbostic err:		bzero(sbp, sizeof(struct stat));
75152150Sbostic 		return (FTS_NS);
75245600Sbostic 	}
75339800Sbostic 
75452209Sbostic 	if (S_ISDIR(sbp->st_mode)) {
75552209Sbostic 		/*
75652209Sbostic 		 * Set the device/inode.  Used to find cycles and check for
75752209Sbostic 		 * crossing mount points.  Also remember the link count, used
75852209Sbostic 		 * in fts_build to limit the number of stat calls.  It is
75952209Sbostic 		 * understood that these fields are only referenced if fts_info
76052209Sbostic 		 * is set to FTS_D.
76152209Sbostic 		 */
76252209Sbostic 		dev = p->fts_dev = sbp->st_dev;
76352209Sbostic 		ino = p->fts_ino = sbp->st_ino;
76452209Sbostic 		p->fts_nlink = sbp->st_nlink;
76552209Sbostic 
76652290Sbostic 		if (ISDOT(p->fts_name))
76752290Sbostic 			return (FTS_DOT);
76852290Sbostic 
76952209Sbostic 		/*
77052209Sbostic 		 * Cycle detection is done by brute force when the directory
77152209Sbostic 		 * is first encountered.  If the tree gets deep enough or the
77252209Sbostic 		 * number of symbolic links to directories is high enough,
77352209Sbostic 		 * something faster might be worthwhile.
77452209Sbostic 		 */
77552209Sbostic 		for (t = p->fts_parent;
77652209Sbostic 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
77752209Sbostic 			if (ino == t->fts_ino && dev == t->fts_dev) {
77852073Sbostic 				p->fts_cycle = t;
77952150Sbostic 				return (FTS_DC);
78047194Sbostic 			}
78152150Sbostic 		return (FTS_D);
78247194Sbostic 	}
78352209Sbostic 	if (S_ISLNK(sbp->st_mode))
78452150Sbostic 		return (FTS_SL);
78552209Sbostic 	if (S_ISREG(sbp->st_mode))
78652150Sbostic 		return (FTS_F);
78752150Sbostic 	return (FTS_DEFAULT);
78839800Sbostic }
78939800Sbostic 
79039800Sbostic static FTSENT *
79145600Sbostic fts_sort(sp, head, nitems)
79245600Sbostic 	FTS *sp;
79339800Sbostic 	FTSENT *head;
79439800Sbostic 	register int nitems;
79539800Sbostic {
79639800Sbostic 	register FTSENT **ap, *p;
79739800Sbostic 
79839800Sbostic 	/*
79945600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
80039800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
80139800Sbostic 	 * sort for memory reasons, return the directory entries in their
80239800Sbostic 	 * current order.  Allocate enough space for the current needs plus
80352209Sbostic 	 * 40 so don't realloc one entry at a time.
80439800Sbostic 	 */
80545600Sbostic 	if (nitems > sp->fts_nitems) {
80645600Sbostic 		sp->fts_nitems = nitems + 40;
80752209Sbostic 		if ((sp->fts_array = realloc(sp->fts_array,
80852209Sbostic 		    (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
80945600Sbostic 			sp->fts_nitems = 0;
81052150Sbostic 			return (head);
81139800Sbostic 		}
81239800Sbostic 	}
81345600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
81439800Sbostic 		*ap++ = p;
81545600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
81645600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
81742273Sbostic 		ap[0]->fts_link = ap[1];
81842273Sbostic 	ap[0]->fts_link = NULL;
81952150Sbostic 	return (head);
82039800Sbostic }
82139800Sbostic 
82239800Sbostic static FTSENT *
82345600Sbostic fts_alloc(sp, name, len)
82445600Sbostic 	FTS *sp;
82539800Sbostic 	char *name;
82639800Sbostic 	register int len;
82739800Sbostic {
82839800Sbostic 	register FTSENT *p;
82952209Sbostic 	int needstat;
83039800Sbostic 
83139800Sbostic 	/*
83252209Sbostic 	 * Variable sized structures.  The stat structure isn't necessary
83352209Sbostic 	 * if the user doesn't need it, and the name is variable length.
83452209Sbostic 	 * Allocate enough extra space after the structure to store them.
83539800Sbostic 	 */
83652579Sbostic 	needstat = ISSET(FTS_NOSTAT) ? 0 : ALIGN(sizeof(struct stat));
83752209Sbostic 	if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL)
83852150Sbostic 		return (NULL);
83942273Sbostic 	bcopy(name, p->fts_name, len + 1);
84052209Sbostic 	if (needstat)
84152579Sbostic 		p->fts_statp = (struct stat *)ALIGN(p->fts_name + len + 1);
84242273Sbostic 	p->fts_namelen = len;
84345600Sbostic 	p->fts_path = sp->fts_path;
84452290Sbostic 	p->fts_errno = 0;
84552290Sbostic 	p->fts_flags = 0;
84647194Sbostic 	p->fts_instr = FTS_NOINSTR;
84745600Sbostic 	p->fts_number = 0;
84845600Sbostic 	p->fts_pointer = NULL;
84952150Sbostic 	return (p);
85039800Sbostic }
85139800Sbostic 
85245600Sbostic static void
85339800Sbostic fts_lfree(head)
85439800Sbostic 	register FTSENT *head;
85539800Sbostic {
85639800Sbostic 	register FTSENT *p;
85739800Sbostic 
85845600Sbostic 	/* Free a linked list of structures. */
85939800Sbostic 	while (p = head) {
86042273Sbostic 		head = head->fts_link;
86147194Sbostic 		free(p);
86239800Sbostic 	}
86339800Sbostic }
86439800Sbostic 
86539800Sbostic /*
86652209Sbostic  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
86752209Sbostic  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
86852209Sbostic  * though the kernel won't resolve them.  Add the size (not just what's needed)
86952209Sbostic  * plus 256 bytes so don't realloc the path 2 bytes at a time.
87039800Sbostic  */
87152209Sbostic static int
87252209Sbostic fts_palloc(sp, more)
87345600Sbostic 	FTS *sp;
87452209Sbostic 	int more;
87539800Sbostic {
87652209Sbostic 
87752209Sbostic 	sp->fts_pathlen += more + 256;
87852209Sbostic 	sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
87952209Sbostic 	return (sp->fts_path == NULL);
88047155Sbostic }
88152209Sbostic 
88252209Sbostic /*
88352209Sbostic  * When the path is realloc'd, have to fix all of the pointers in structures
88452209Sbostic  * already returned.
88552209Sbostic  */
88652209Sbostic static void
88752209Sbostic fts_padjust(sp, addr)
88852209Sbostic 	FTS *sp;
88952209Sbostic 	void *addr;
89052209Sbostic {
89152209Sbostic 	FTSENT *p;
89252209Sbostic 
89352209Sbostic #define	ADJUST(p) { \
89452209Sbostic 	(p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \
89552209Sbostic 	(p)->fts_path = addr; \
89652209Sbostic }
89752209Sbostic 	/* Adjust the current set of children. */
89852209Sbostic 	for (p = sp->fts_child; p; p = p->fts_link)
89952209Sbostic 		ADJUST(p);
90052209Sbostic 
90152209Sbostic 	/* Adjust the rest of the tree. */
90252209Sbostic 	for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
90352209Sbostic 		ADJUST(p);
90452209Sbostic 		p = p->fts_link ? p->fts_link : p->fts_parent;
90552209Sbostic 	}
90652209Sbostic }
907