xref: /csrg-svn/lib/libc/gen/fts.c (revision 52770)
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*52770Sbostic static char sccsid[] = "@(#)fts.c	5.32 (Berkeley) 03/01/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 */
40*52770Sbostic #define	BCHILD		1		/* fts_children */
41*52770Sbostic #define	BNAMES		2		/* fts_children, names only */
42*52770Sbostic #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 
56*52770Sbostic 	/* Options check. */
57*52770Sbostic 	if (options & ~FTS_OPTIONMASK) {
58*52770Sbostic 		errno = EINVAL;
59*52770Sbostic 		return (NULL);
60*52770Sbostic 	}
61*52770Sbostic 
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;
24039800Sbostic 
24145600Sbostic 	/* If finished or unrecoverable error, return NULL. */
24252155Sbostic 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
24352150Sbostic 		return (NULL);
24439800Sbostic 
24545600Sbostic 	/* Set current node pointer. */
24642273Sbostic 	p = sp->fts_cur;
24739800Sbostic 
24845600Sbostic 	/* Save and zero out user instructions. */
24942273Sbostic 	instr = p->fts_instr;
25047194Sbostic 	p->fts_instr = FTS_NOINSTR;
25139800Sbostic 
25247194Sbostic 	/* Any type of file may be re-visited; re-stat and re-turn. */
25339800Sbostic 	if (instr == FTS_AGAIN) {
25445600Sbostic 		p->fts_info = fts_stat(sp, p, 0);
25552150Sbostic 		return (p);
25639800Sbostic 	}
25739800Sbostic 
25847194Sbostic 	/*
25947194Sbostic 	 * Following a symlink -- SLNONE test allows application to see
26052290Sbostic 	 * SLNONE and recover.  If indirecting through a symlink, have
26152290Sbostic 	 * keep a pointer to current location.  If unable to get that
26252290Sbostic 	 * pointer, follow fails.
26347194Sbostic 	 */
26447194Sbostic 	if (instr == FTS_FOLLOW &&
26547194Sbostic 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
26645600Sbostic 		p->fts_info = fts_stat(sp, p, 1);
26752290Sbostic 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
26852290Sbostic 			if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
26952290Sbostic 				p->fts_errno = errno;
27052290Sbostic 				p->fts_info = FTS_ERR;
27152290Sbostic 			} else
27252290Sbostic 				p->fts_flags |= FTS_SYMFOLLOW;
27352150Sbostic 		return (p);
27442299Sbostic 	}
27542299Sbostic 
27645600Sbostic 	/* Directory in pre-order. */
27742299Sbostic 	if (p->fts_info == FTS_D) {
27845600Sbostic 		/* If skipped or crossed mount point, do post-order visit. */
27947194Sbostic 		if (instr == FTS_SKIP ||
28052209Sbostic 		    ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
28152290Sbostic 			if (p->fts_flags & FTS_SYMFOLLOW)
28252290Sbostic 				(void)close(p->fts_symfd);
28342273Sbostic 			if (sp->fts_child) {
28442273Sbostic 				fts_lfree(sp->fts_child);
28542273Sbostic 				sp->fts_child = NULL;
28639800Sbostic 			}
28742301Sbostic 			p->fts_info = FTS_DP;
28852150Sbostic 			return (p);
28942299Sbostic 		}
29042299Sbostic 
291*52770Sbostic 		/* Rebuild if only got the names and now traversing. */
292*52770Sbostic 		if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
293*52770Sbostic 			sp->fts_options &= ~FTS_NAMEONLY;
294*52770Sbostic 			fts_lfree(sp->fts_child);
295*52770Sbostic 			sp->fts_child = NULL;
296*52770Sbostic 		}
297*52770Sbostic 
29847194Sbostic 		/*
29947194Sbostic 		 * Cd to the subdirectory, reading it if haven't already.  If
30047194Sbostic 		 * the read fails for any reason, or the directory is empty,
30147194Sbostic 		 * the fts_info field of the current node is set by fts_build.
30247220Sbostic 		 * If have already read and now fail to chdir, whack the list
30347220Sbostic 		 * to make the names come out right, and set the parent state
30447220Sbostic 		 * so the application will eventually get an error condition.
30547220Sbostic 		 * If haven't read and fail to chdir, check to see if we're
30647220Sbostic 		 * at the root node -- if so, we have to get back or the root
30747220Sbostic 		 * node may be inaccessible.
30847194Sbostic 		 */
30947194Sbostic 		if (sp->fts_child) {
31042299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
31152278Sbostic 				p->fts_errno = errno;
31252290Sbostic 				p->fts_flags |= FTS_DONTCHDIR;
31347194Sbostic 				for (p = sp->fts_child; p; p = p->fts_link)
31447194Sbostic 					p->fts_accpath =
31547194Sbostic 					    p->fts_parent->fts_accpath;
31642299Sbostic 			}
31752155Sbostic 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
31852290Sbostic 			p->fts_flags |= FTS_DONTCHDIR;
31952290Sbostic 			if (ISSET(FTS_STOP))
32052150Sbostic 				return (NULL);
32147220Sbostic 			if (p->fts_level == FTS_ROOTLEVEL &&
32247220Sbostic 			    FCHDIR(sp, sp->fts_rfd)) {
32352290Sbostic 				(void)close(sp->fts_rfd);
32447220Sbostic 				SET(FTS_STOP);
32552150Sbostic 				return (NULL);
32647220Sbostic 			}
32752290Sbostic 			(void)close(sp->fts_rfd);
32852150Sbostic 			return (p);
32947220Sbostic 		}
33047194Sbostic 		p = sp->fts_child;
33142299Sbostic 		sp->fts_child = NULL;
33247194Sbostic 		goto name;
33339800Sbostic 	}
33439800Sbostic 
33552278Sbostic 	/* Move to the next node on this level. */
33642299Sbostic next:	tmp = p;
33742299Sbostic 	if (p = p->fts_link) {
33847194Sbostic 		free(tmp);
33947194Sbostic 
34047194Sbostic 		/* If reached the top, load the paths for the next root. */
34147212Sbostic 		if (p->fts_level == FTS_ROOTLEVEL) {
34247404Sbostic 			fts_load(sp, p);
34352150Sbostic 			return (sp->fts_cur = p);
34447194Sbostic 		}
34542299Sbostic 
34652290Sbostic 		/*
34752290Sbostic 		 * User may have called fts_set on the node.  If skipped,
34852290Sbostic 		 * ignore.  If followed, get a file descriptor so we can
34952290Sbostic 		 * get back if necessary.
35052290Sbostic 		 */
35147194Sbostic 		if (p->fts_instr == FTS_SKIP)
35247194Sbostic 			goto next;
35347194Sbostic 		if (p->fts_instr == FTS_FOLLOW) {
35447194Sbostic 			p->fts_info = fts_stat(sp, p, 1);
35552290Sbostic 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
35652290Sbostic 				if ((p->fts_symfd =
35752290Sbostic 				    open(".", O_RDONLY, 0)) < 0) {
35852290Sbostic 					p->fts_errno = errno;
35952290Sbostic 					p->fts_info = FTS_ERR;
36052290Sbostic 				} else
36152290Sbostic 					p->fts_flags |= FTS_SYMFOLLOW;
36247194Sbostic 			p->fts_instr = FTS_NOINSTR;
36347194Sbostic 		}
36442299Sbostic 
36547194Sbostic name:		t = sp->fts_path + NAPPEND(p->fts_parent);
36647194Sbostic 		*t++ = '/';
36747194Sbostic 		bcopy(p->fts_name, t, p->fts_namelen + 1);
36852150Sbostic 		return (sp->fts_cur = p);
36939800Sbostic 	}
37039800Sbostic 
37147220Sbostic 	/* Move up to the parent node. */
37242299Sbostic 	p = tmp->fts_parent;
37347194Sbostic 	free(tmp);
37442299Sbostic 
37547212Sbostic 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
37639800Sbostic 		/*
37745600Sbostic 		 * Done; free everything up and set errno to 0 so the user
37839800Sbostic 		 * can distinguish between error and EOF.
37939800Sbostic 		 */
38047194Sbostic 		free(p);
38139800Sbostic 		errno = 0;
38252150Sbostic 		return (sp->fts_cur = NULL);
38339800Sbostic 	}
38439800Sbostic 
38542273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
38647194Sbostic 
38747194Sbostic 	/*
38852290Sbostic 	 * Change to starting directory.  If at a root node or came through a
38952290Sbostic 	 * symlink, go back through the file descriptor.  Otherwise, just cd
39052290Sbostic 	 * up one directory.
39147194Sbostic 	 */
39247220Sbostic 	if (p->fts_level == FTS_ROOTLEVEL) {
39347220Sbostic 		if (FCHDIR(sp, sp->fts_rfd)) {
39452290Sbostic 			(void)close(sp->fts_rfd);
39547220Sbostic 			SET(FTS_STOP);
39652150Sbostic 			return (NULL);
39747220Sbostic 		}
39852290Sbostic 		(void)close(sp->fts_rfd);
39952290Sbostic 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
40052290Sbostic 		if (FCHDIR(sp, p->fts_symfd)) {
40152290Sbostic 			(void)close(p->fts_symfd);
40252290Sbostic 			SET(FTS_STOP);
40352290Sbostic 			return (NULL);
40452290Sbostic 		}
40552290Sbostic 		(void)close(p->fts_symfd);
40652290Sbostic 	} else if (!(p->fts_flags & FTS_DONTCHDIR)) {
40752278Sbostic 		if (CHDIR(sp, "..")) {
40852278Sbostic 			SET(FTS_STOP);
40952278Sbostic 			return (NULL);
41052278Sbostic 		}
41152278Sbostic 	}
41252290Sbostic 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
41352150Sbostic 	return (sp->fts_cur = p);
41439800Sbostic }
41539800Sbostic 
41639800Sbostic /*
41747220Sbostic  * Fts_set takes the stream as an argument although it's not used in this
41839800Sbostic  * implementation; it would be necessary if anyone wanted to add global
41945600Sbostic  * semantics to fts using fts_set.  An error return is allowed for similar
42045600Sbostic  * reasons.
42139800Sbostic  */
42239800Sbostic /* ARGSUSED */
42352209Sbostic int
42445600Sbostic fts_set(sp, p, instr)
42539800Sbostic 	FTS *sp;
42639800Sbostic 	FTSENT *p;
42739800Sbostic 	int instr;
42839800Sbostic {
429*52770Sbostic 	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
430*52770Sbostic 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
431*52770Sbostic 		errno = EINVAL;
432*52770Sbostic 		return (1);
433*52770Sbostic 	}
43442273Sbostic 	p->fts_instr = instr;
43552150Sbostic 	return (0);
43639800Sbostic }
43739800Sbostic 
43839800Sbostic FTSENT *
439*52770Sbostic fts_children(sp, instr)
44039800Sbostic 	register FTS *sp;
441*52770Sbostic 	int instr;
44239800Sbostic {
44342299Sbostic 	register FTSENT *p;
44442299Sbostic 	int fd;
44542299Sbostic 
446*52770Sbostic 	if (instr && instr != FTS_NAMEONLY) {
447*52770Sbostic 		errno = EINVAL;
448*52770Sbostic 		return (NULL);
449*52770Sbostic 	}
450*52770Sbostic 
45145600Sbostic 	/* Set current node pointer. */
45245600Sbostic 	p = sp->fts_cur;
45345600Sbostic 
45439800Sbostic 	/*
45552160Sbostic 	 * Errno set to 0 so user can distinguish empty directory from
45652160Sbostic 	 * an error.
45739800Sbostic 	 */
45839800Sbostic 	errno = 0;
45952160Sbostic 
46052160Sbostic 	/* Fatal errors stop here. */
46152160Sbostic 	if (ISSET(FTS_STOP))
46252150Sbostic 		return (NULL);
46345600Sbostic 
46452160Sbostic 	/* Return logical hierarchy of user's arguments. */
46552160Sbostic 	if (p->fts_info == FTS_INIT)
46652160Sbostic 		return (p->fts_link);
46752160Sbostic 
46852290Sbostic 	/*
46952290Sbostic 	 * If not a directory being visited in pre-order, stop here.  Could
47052290Sbostic 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
47152290Sbostic 	 * same effect is available with FTS_AGAIN.
47252290Sbostic 	 */
47352290Sbostic 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
47452160Sbostic 		return (NULL);
47552160Sbostic 
47645600Sbostic 	/* Free up any previous child list. */
47742273Sbostic 	if (sp->fts_child)
47842273Sbostic 		fts_lfree(sp->fts_child);
47942299Sbostic 
480*52770Sbostic 	if (instr == FTS_NAMEONLY) {
481*52770Sbostic 		sp->fts_options |= FTS_NAMEONLY;
482*52770Sbostic 		instr = BNAMES;
483*52770Sbostic 	} else
484*52770Sbostic 		instr = BCHILD;
485*52770Sbostic 
48642299Sbostic 	/*
48745600Sbostic 	 * If using chdir on a relative path and called BEFORE fts_read does
48847194Sbostic 	 * its chdir to the root of a traversal, we can lose -- we need to
48947194Sbostic 	 * chdir into the subdirectory, and we don't know where the current
49047194Sbostic 	 * directory is, so we can't get back so that the upcoming chdir by
49147194Sbostic 	 * fts_read will work.
49242299Sbostic 	 */
49347212Sbostic 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
49445600Sbostic 	    ISSET(FTS_NOCHDIR))
495*52770Sbostic 		return (sp->fts_child = fts_build(sp, instr));
49642299Sbostic 
49742299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
49852150Sbostic 		return (NULL);
499*52770Sbostic 	sp->fts_child = fts_build(sp, instr);
50042299Sbostic 	if (fchdir(fd))
50152150Sbostic 		return (NULL);
50242299Sbostic 	(void)close(fd);
50352150Sbostic 	return (sp->fts_child);
50439800Sbostic }
50539800Sbostic 
50647194Sbostic /*
50747194Sbostic  * This is the tricky part -- do not casually change *anything* in here.  The
50847194Sbostic  * idea is to build the linked list of entries that are used by fts_children
50947194Sbostic  * and fts_read.  There are lots of special cases.
51047194Sbostic  *
51147194Sbostic  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
51247194Sbostic  * set and it's a physical walk (so that symbolic links can't be directories),
51347194Sbostic  * we assume that the number of subdirectories in a node is equal to the number
51447194Sbostic  * of links to the parent.  This allows stat calls to be skipped in any leaf
51547194Sbostic  * directories and for any nodes after the directories in the parent node have
51647194Sbostic  * been found.  This empirically cuts the stat calls by about 2/3.
51747194Sbostic  */
51847155Sbostic static FTSENT *
51942299Sbostic fts_build(sp, type)
52039800Sbostic 	register FTS *sp;
52142299Sbostic 	int type;
52239800Sbostic {
52339800Sbostic 	register struct dirent *dp;
52439800Sbostic 	register FTSENT *p, *head;
52539800Sbostic 	register int nitems;
52652290Sbostic 	FTSENT *cur, *tail;
52739800Sbostic 	DIR *dirp;
52852209Sbostic 	void *adjaddr;
52952278Sbostic 	int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
53039800Sbostic 	char *cp;
53139800Sbostic 
53245600Sbostic 	/* Set current node pointer. */
53347194Sbostic 	cur = sp->fts_cur;
53445600Sbostic 
53547194Sbostic 	/*
53647194Sbostic 	 * Open the directory for reading.  If this fails, we're done.
53747194Sbostic 	 * If being called from fts_read, set the fts_info field.
53847194Sbostic 	 */
53952155Sbostic 	if ((dirp = opendir(cur->fts_accpath)) == NULL) {
54052206Sbostic 		if (type == BREAD) {
54147194Sbostic 			cur->fts_info = FTS_DNR;
54252206Sbostic 			cur->fts_errno = errno;
54352206Sbostic 		}
54452150Sbostic 		return (NULL);
54539800Sbostic 	}
54639800Sbostic 
54739800Sbostic 	/*
54847194Sbostic 	 * Nlinks is the number of possible entries of type directory in the
54947194Sbostic 	 * directory if we're cheating on stat calls, 0 if we're not doing
55047194Sbostic 	 * any stat calls at all, -1 if we're doing stats on everything.
55139800Sbostic 	 */
552*52770Sbostic 	if (type == BNAMES)
553*52770Sbostic 		nlinks = 0;
554*52770Sbostic 	else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
555*52770Sbostic 		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
556*52770Sbostic 	else
557*52770Sbostic 		nlinks = -1;
55839800Sbostic 
55952290Sbostic #ifdef notdef
56052290Sbostic 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
56152290Sbostic 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
56252290Sbostic 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
56352290Sbostic #endif
56447194Sbostic 	/*
56547194Sbostic 	 * If we're going to need to stat anything or we want to descend
56647194Sbostic 	 * and stay in the directory, chdir.  If this fails we keep going.
56747194Sbostic 	 * We won't be able to stat anything, but we can still return the
56847194Sbostic 	 * names themselves.  Note, that since fts_read won't be able to
56947194Sbostic 	 * chdir into the directory, it will have to return different path
57047194Sbostic 	 * names than before, i.e. "a/b" instead of "b".  Since the node
57147194Sbostic 	 * has already been visited in pre-order, have to wait until the
57252290Sbostic 	 * post-order visit to return the error.  There is a special case
57352290Sbostic 	 * here, if there was nothing to stat then it's not an error to
57452290Sbostic 	 * not be able to stat.  This is all fairly nasty.  If a program
57552290Sbostic 	 * needed sorted entries or stat information, they had better be
57652290Sbostic 	 * checking FTS_NS on the returned nodes.
57747194Sbostic 	 */
57845600Sbostic 	if (nlinks || type == BREAD)
57947194Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
58052290Sbostic 			if (nlinks && type == BREAD)
58152160Sbostic 				cur->fts_errno = errno;
58252290Sbostic 			descend = 0;
58352278Sbostic 			cderrno = errno;
58447194Sbostic 		} else {
58545600Sbostic 			descend = 1;
58652278Sbostic 			cderrno = 0;
58739962Sbostic 		}
58847194Sbostic 	else
58947194Sbostic 		descend = 0;
59039962Sbostic 
59147194Sbostic 	/*
59247194Sbostic 	 * Figure out the max file name length that can be stored in the
59347194Sbostic 	 * current path -- the inner loop allocates more path as necessary.
59447194Sbostic 	 * We really wouldn't have to do the maxlen calculations here, we
59547194Sbostic 	 * could do them in fts_read before returning the path, but it's a
59647194Sbostic 	 * lot easier here since the length is part of the dirent structure.
59747194Sbostic 	 *
59852209Sbostic 	 * If not changing directories set a pointer so that can just append
59952209Sbostic 	 * each new name into the path.
60047194Sbostic 	 */
60147194Sbostic 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
60247194Sbostic 	len = NAPPEND(cur);
60347194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
60447194Sbostic 		cp = sp->fts_path + len;
60547194Sbostic 		*cp++ = '/';
60647194Sbostic 	}
60740939Sbostic 
60847194Sbostic 	level = cur->fts_level + 1;
60940939Sbostic 
61047194Sbostic 	/* Read the directory, attaching each entry to the `link' pointer. */
61152209Sbostic 	adjaddr = NULL;
61252290Sbostic 	for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
61347194Sbostic 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
61439800Sbostic 			continue;
61539800Sbostic 
61652155Sbostic 		if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
61739800Sbostic 			goto mem1;
61839800Sbostic 		if (dp->d_namlen > maxlen) {
61952209Sbostic 			if (fts_palloc(sp, (int)dp->d_namlen)) {
62047194Sbostic 				/*
62147194Sbostic 				 * No more memory for path or structures.  Save
62247194Sbostic 				 * errno, free up the current structure and the
62347194Sbostic 				 * structures already allocated.
62447194Sbostic 				 */
62547194Sbostic mem1:				saved_errno = errno;
62647194Sbostic 				if (p)
62747194Sbostic 					free(p);
62847194Sbostic 				fts_lfree(head);
62947194Sbostic 				(void)closedir(dirp);
63039800Sbostic 				errno = saved_errno;
63147194Sbostic 				cur->fts_info = FTS_ERR;
63247194Sbostic 				SET(FTS_STOP);
63352150Sbostic 				return (NULL);
63439800Sbostic 			}
63552209Sbostic 			adjaddr = sp->fts_path;
63642273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
63739800Sbostic 		}
63839800Sbostic 
63942273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
64042273Sbostic 		p->fts_parent = sp->fts_cur;
64142273Sbostic 		p->fts_level = level;
64239800Sbostic 
64352290Sbostic 		if (cderrno) {
64452290Sbostic 			if (nlinks) {
64552290Sbostic 				p->fts_info = FTS_NS;
64652290Sbostic 				p->fts_errno = cderrno;
64752290Sbostic 			} else
64852290Sbostic 				p->fts_info = FTS_NSOK;
64952290Sbostic 			p->fts_accpath = cur->fts_accpath;
65052290Sbostic 		} else if (nlinks) {
65147194Sbostic 			/* Build a file name for fts_stat to stat. */
65247194Sbostic 			if (ISSET(FTS_NOCHDIR)) {
65347194Sbostic 				p->fts_accpath = p->fts_path;
65442273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
65547194Sbostic 			} else
65647194Sbostic 				p->fts_accpath = p->fts_name;
65745600Sbostic 			p->fts_info = fts_stat(sp, p, 0);
65852209Sbostic 			if (nlinks > 0 && (p->fts_info == FTS_D ||
65952209Sbostic 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
66039800Sbostic 				--nlinks;
66147194Sbostic 		} else {
66247194Sbostic 			p->fts_accpath =
66347194Sbostic 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
66447194Sbostic 			p->fts_info = FTS_NSOK;
66547194Sbostic 		}
66639800Sbostic 
66752290Sbostic 		/* We walk in directory order so "ls -f" doesn't get upset. */
66852290Sbostic 		p->fts_link = NULL;
66952290Sbostic 		if (head == NULL)
67052290Sbostic 			head = tail = p;
67152290Sbostic 		else {
67252290Sbostic 			tail->fts_link = p;
67352290Sbostic 			tail = p;
67452290Sbostic 		}
67539800Sbostic 		++nitems;
67639800Sbostic 	}
67739800Sbostic 	(void)closedir(dirp);
67839800Sbostic 
67947194Sbostic 	/*
68052209Sbostic 	 * If had to realloc the path, adjust the addresses for the rest
68152209Sbostic 	 * of the tree.
68252209Sbostic 	 */
68352209Sbostic 	if (adjaddr)
68452209Sbostic 		fts_padjust(sp, adjaddr);
68552209Sbostic 
68652209Sbostic 	/*
68747194Sbostic 	 * If not changing directories, reset the path back to original
68847194Sbostic 	 * state.
68947194Sbostic 	 */
69047194Sbostic 	if (ISSET(FTS_NOCHDIR)) {
69147194Sbostic 		if (cp - 1 > sp->fts_path)
69247194Sbostic 			--cp;
69347194Sbostic 		*cp = '\0';
69447194Sbostic 	}
69542299Sbostic 
69642299Sbostic 	/*
69747194Sbostic 	 * If descended after called from fts_children or called from
69847194Sbostic 	 * fts_read and didn't find anything, get back.  If can't get
69952209Sbostic 	 * back, done.
70042299Sbostic 	 */
70142299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
70247194Sbostic 		cur->fts_info = FTS_ERR;
70347194Sbostic 		SET(FTS_STOP);
70452150Sbostic 		return (NULL);
70539962Sbostic 	}
70639962Sbostic 
70752209Sbostic 	/* If didn't find anything, just do the post-order visit */
70839800Sbostic 	if (!nitems) {
70942299Sbostic 		if (type == BREAD)
71047194Sbostic 			cur->fts_info = FTS_DP;
71152150Sbostic 		return (NULL);
71239800Sbostic 	}
71339800Sbostic 
71447194Sbostic 	/* Sort the entries. */
71542273Sbostic 	if (sp->fts_compar && nitems > 1)
71645600Sbostic 		head = fts_sort(sp, head, nitems);
71752150Sbostic 	return (head);
71839800Sbostic }
71939800Sbostic 
72045600Sbostic static u_short
72145600Sbostic fts_stat(sp, p, follow)
72245600Sbostic 	FTS *sp;
72339800Sbostic 	register FTSENT *p;
72445600Sbostic 	int follow;
72539800Sbostic {
72652160Sbostic 	register FTSENT *t;
72752160Sbostic 	register dev_t dev;
72852160Sbostic 	register ino_t ino;
72952209Sbostic 	struct stat *sbp, sb;
73047194Sbostic 	int saved_errno;
73147194Sbostic 
73252209Sbostic 	/* If user needs stat info, stat buffer already allocated. */
73352209Sbostic 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
73452209Sbostic 
73539800Sbostic 	/*
73645600Sbostic 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
73747194Sbostic 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
73852160Sbostic 	 * fail, set the errno from the stat call.
73939800Sbostic 	 */
74045600Sbostic 	if (ISSET(FTS_LOGICAL) || follow) {
74152209Sbostic 		if (stat(p->fts_accpath, sbp)) {
74247194Sbostic 			saved_errno = errno;
74352209Sbostic 			if (!lstat(p->fts_accpath, sbp)) {
74447194Sbostic 				errno = 0;
74552150Sbostic 				return (FTS_SLNONE);
74647194Sbostic 			}
74752160Sbostic 			p->fts_errno = saved_errno;
74852209Sbostic 			goto err;
74945600Sbostic 		}
75052209Sbostic 	} else if (lstat(p->fts_accpath, sbp)) {
75152160Sbostic 		p->fts_errno = errno;
75252209Sbostic err:		bzero(sbp, sizeof(struct stat));
75352150Sbostic 		return (FTS_NS);
75445600Sbostic 	}
75539800Sbostic 
75652209Sbostic 	if (S_ISDIR(sbp->st_mode)) {
75752209Sbostic 		/*
75852209Sbostic 		 * Set the device/inode.  Used to find cycles and check for
75952209Sbostic 		 * crossing mount points.  Also remember the link count, used
76052209Sbostic 		 * in fts_build to limit the number of stat calls.  It is
76152209Sbostic 		 * understood that these fields are only referenced if fts_info
76252209Sbostic 		 * is set to FTS_D.
76352209Sbostic 		 */
76452209Sbostic 		dev = p->fts_dev = sbp->st_dev;
76552209Sbostic 		ino = p->fts_ino = sbp->st_ino;
76652209Sbostic 		p->fts_nlink = sbp->st_nlink;
76752209Sbostic 
76852290Sbostic 		if (ISDOT(p->fts_name))
76952290Sbostic 			return (FTS_DOT);
77052290Sbostic 
77152209Sbostic 		/*
77252209Sbostic 		 * Cycle detection is done by brute force when the directory
77352209Sbostic 		 * is first encountered.  If the tree gets deep enough or the
77452209Sbostic 		 * number of symbolic links to directories is high enough,
77552209Sbostic 		 * something faster might be worthwhile.
77652209Sbostic 		 */
77752209Sbostic 		for (t = p->fts_parent;
77852209Sbostic 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
77952209Sbostic 			if (ino == t->fts_ino && dev == t->fts_dev) {
78052073Sbostic 				p->fts_cycle = t;
78152150Sbostic 				return (FTS_DC);
78247194Sbostic 			}
78352150Sbostic 		return (FTS_D);
78447194Sbostic 	}
78552209Sbostic 	if (S_ISLNK(sbp->st_mode))
78652150Sbostic 		return (FTS_SL);
78752209Sbostic 	if (S_ISREG(sbp->st_mode))
78852150Sbostic 		return (FTS_F);
78952150Sbostic 	return (FTS_DEFAULT);
79039800Sbostic }
79139800Sbostic 
79239800Sbostic static FTSENT *
79345600Sbostic fts_sort(sp, head, nitems)
79445600Sbostic 	FTS *sp;
79539800Sbostic 	FTSENT *head;
79639800Sbostic 	register int nitems;
79739800Sbostic {
79839800Sbostic 	register FTSENT **ap, *p;
79939800Sbostic 
80039800Sbostic 	/*
80145600Sbostic 	 * Construct an array of pointers to the structures and call qsort(3).
80239800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
80339800Sbostic 	 * sort for memory reasons, return the directory entries in their
80439800Sbostic 	 * current order.  Allocate enough space for the current needs plus
80552209Sbostic 	 * 40 so don't realloc one entry at a time.
80639800Sbostic 	 */
80745600Sbostic 	if (nitems > sp->fts_nitems) {
80845600Sbostic 		sp->fts_nitems = nitems + 40;
80952209Sbostic 		if ((sp->fts_array = realloc(sp->fts_array,
81052209Sbostic 		    (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
81145600Sbostic 			sp->fts_nitems = 0;
81252150Sbostic 			return (head);
81339800Sbostic 		}
81439800Sbostic 	}
81545600Sbostic 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
81639800Sbostic 		*ap++ = p;
81745600Sbostic 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
81845600Sbostic 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
81942273Sbostic 		ap[0]->fts_link = ap[1];
82042273Sbostic 	ap[0]->fts_link = NULL;
82152150Sbostic 	return (head);
82239800Sbostic }
82339800Sbostic 
82439800Sbostic static FTSENT *
82545600Sbostic fts_alloc(sp, name, len)
82645600Sbostic 	FTS *sp;
82739800Sbostic 	char *name;
82839800Sbostic 	register int len;
82939800Sbostic {
83039800Sbostic 	register FTSENT *p;
83152209Sbostic 	int needstat;
83239800Sbostic 
83339800Sbostic 	/*
83452209Sbostic 	 * Variable sized structures.  The stat structure isn't necessary
83552209Sbostic 	 * if the user doesn't need it, and the name is variable length.
83652209Sbostic 	 * Allocate enough extra space after the structure to store them.
83739800Sbostic 	 */
83852579Sbostic 	needstat = ISSET(FTS_NOSTAT) ? 0 : ALIGN(sizeof(struct stat));
83952209Sbostic 	if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL)
84052150Sbostic 		return (NULL);
84142273Sbostic 	bcopy(name, p->fts_name, len + 1);
84252209Sbostic 	if (needstat)
84352579Sbostic 		p->fts_statp = (struct stat *)ALIGN(p->fts_name + len + 1);
84442273Sbostic 	p->fts_namelen = len;
84545600Sbostic 	p->fts_path = sp->fts_path;
84652290Sbostic 	p->fts_errno = 0;
84752290Sbostic 	p->fts_flags = 0;
84847194Sbostic 	p->fts_instr = FTS_NOINSTR;
84945600Sbostic 	p->fts_number = 0;
85052209Sbostic #ifdef NOT_NECESSARY
85145600Sbostic 	p->fts_pointer = NULL;
85252209Sbostic #endif
85352150Sbostic 	return (p);
85439800Sbostic }
85539800Sbostic 
85645600Sbostic static void
85739800Sbostic fts_lfree(head)
85839800Sbostic 	register FTSENT *head;
85939800Sbostic {
86039800Sbostic 	register FTSENT *p;
86139800Sbostic 
86245600Sbostic 	/* Free a linked list of structures. */
86339800Sbostic 	while (p = head) {
86442273Sbostic 		head = head->fts_link;
86547194Sbostic 		free(p);
86639800Sbostic 	}
86739800Sbostic }
86839800Sbostic 
86939800Sbostic /*
87052209Sbostic  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
87152209Sbostic  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
87252209Sbostic  * though the kernel won't resolve them.  Add the size (not just what's needed)
87352209Sbostic  * plus 256 bytes so don't realloc the path 2 bytes at a time.
87439800Sbostic  */
87552209Sbostic static int
87652209Sbostic fts_palloc(sp, more)
87745600Sbostic 	FTS *sp;
87852209Sbostic 	int more;
87939800Sbostic {
88052209Sbostic 
88152209Sbostic 	sp->fts_pathlen += more + 256;
88252209Sbostic 	sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
88352209Sbostic 	return (sp->fts_path == NULL);
88447155Sbostic }
88552209Sbostic 
88652209Sbostic /*
88752209Sbostic  * When the path is realloc'd, have to fix all of the pointers in structures
88852209Sbostic  * already returned.
88952209Sbostic  */
89052209Sbostic static void
89152209Sbostic fts_padjust(sp, addr)
89252209Sbostic 	FTS *sp;
89352209Sbostic 	void *addr;
89452209Sbostic {
89552209Sbostic 	FTSENT *p;
89652209Sbostic 
89752209Sbostic #define	ADJUST(p) { \
89852209Sbostic 	(p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \
89952209Sbostic 	(p)->fts_path = addr; \
90052209Sbostic }
90152209Sbostic 	/* Adjust the current set of children. */
90252209Sbostic 	for (p = sp->fts_child; p; p = p->fts_link)
90352209Sbostic 		ADJUST(p);
90452209Sbostic 
90552209Sbostic 	/* Adjust the rest of the tree. */
90652209Sbostic 	for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
90752209Sbostic 		ADJUST(p);
90852209Sbostic 		p = p->fts_link ? p->fts_link : p->fts_parent;
90952209Sbostic 	}
91052209Sbostic }
911