xref: /csrg-svn/lib/libc/gen/fts.c (revision 42301)
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*42301Sbostic static char sccsid[] = "@(#)fts.c	5.8 (Berkeley) 05/24/90";
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>
1739800Sbostic #include <fts.h>
1842273Sbostic #include <string.h>
1942281Sbostic #include <stdlib.h>
2039800Sbostic 
2139800Sbostic FTSENT *fts_alloc(), *fts_build(), *fts_cycle(), *fts_sort(), *fts_root();
2239800Sbostic short fts_stat();
2339800Sbostic 
2439800Sbostic /*
2539800Sbostic  * Special case a root of "/" so that slashes aren't appended causing
2639800Sbostic  * paths to be written as "//foo".
2739800Sbostic  */
2839800Sbostic #define	NAPPEND(p) \
2942273Sbostic 	(p->fts_level == ROOTLEVEL && p->fts_pathlen == 1 && \
3042273Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
3139800Sbostic 
3242273Sbostic #define	CHDIR(sp, path)	(!(sp->fts_options & FTS_NOCHDIR) && chdir(path))
3342273Sbostic #define	FCHDIR(sp, fd)	(!(sp->fts_options & FTS_NOCHDIR) && fchdir(fd))
3439800Sbostic 
3539800Sbostic #define	ROOTLEVEL	0
3639800Sbostic #define	ROOTPARENTLEVEL	-1
3739800Sbostic 
3842299Sbostic /* fts_build flags */
3942299Sbostic #define	BCHILD		1		/* from ftschildren */
4042299Sbostic #define	BREAD		2		/* from ftsread */
4142299Sbostic 
4239800Sbostic static FTS *stream;			/* current stream pointer */
4339800Sbostic 
4439800Sbostic FTS *
4539800Sbostic ftsopen(argv, options, compar)
4639800Sbostic 	char *argv[];
4739800Sbostic 	register int options;
4839800Sbostic 	int (*compar)();
4939800Sbostic {
5039800Sbostic 	register FTS *sp;
5139800Sbostic 	register FTSENT *p, *root;
5239800Sbostic 	register int nitems, maxlen;
5339800Sbostic 	FTSENT *parent, *tmp;
5442299Sbostic 	char *fts_path();
5539800Sbostic 
5639800Sbostic 	/* allocate/initialize the stream */
5739800Sbostic 	if (!(stream = sp = (FTS *)malloc((u_int)sizeof(FTS))))
5839800Sbostic 		return(NULL);
5939800Sbostic 	bzero(sp, sizeof(FTS));
6042273Sbostic 	sp->fts_compar = compar;
6142299Sbostic 
6242299Sbostic 	/*
6342299Sbostic 	 * logical walks turn on NOCHDIR; symbolic links are just too
6442299Sbostic 	 * hard to deal with.
6542299Sbostic 	 */
6642273Sbostic 	sp->fts_options = options;
6742299Sbostic 	if (options & FTS_LOGICAL)
6842299Sbostic 		sp->fts_options |= FTS_NOCHDIR;
6939800Sbostic 
7039800Sbostic 	/* allocate/initialize root's parent */
7139800Sbostic 	if (!(parent = fts_alloc("", 0)))
7239800Sbostic 		goto mem1;
7342273Sbostic 	parent->fts_level = ROOTPARENTLEVEL;
7439800Sbostic 
7539800Sbostic 	/* allocate/initialize root(s) */
7639800Sbostic 	if (options & FTS_MULTIPLE) {
7739800Sbostic 		maxlen = -1;
7839800Sbostic 		for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
7939800Sbostic 			if (!(p = fts_root(*argv)))
8039800Sbostic 				goto mem2;
8142273Sbostic 			if (maxlen < p->fts_namelen)
8242273Sbostic 				maxlen = p->fts_namelen;
8339800Sbostic 			/*
8439800Sbostic 			 * if comparison routine supplied, traverse in sorted
8539800Sbostic 			 * order; otherwise traverse in the order specified.
8639800Sbostic 			 */
8739800Sbostic 			if (compar) {
8842273Sbostic 				p->fts_link = root;
8939800Sbostic 				root = p;
9042273Sbostic 				p->fts_accpath = p->fts_name;
9142273Sbostic 				p->fts_info = fts_stat(p, 0);
9239800Sbostic 			} else {
9342273Sbostic 				p->fts_link = NULL;
9439800Sbostic 				if (!root)
9539800Sbostic 					tmp = root = p;
9639800Sbostic 				else {
9742273Sbostic 					tmp->fts_link = p;
9839800Sbostic 					tmp = p;
9939800Sbostic 				}
10039800Sbostic 			}
10142273Sbostic 			p->fts_level = ROOTLEVEL;
10242273Sbostic 			p->fts_parent = parent;
10339800Sbostic 		}
10439800Sbostic 		if (compar && nitems > 1)
10539800Sbostic 			root = fts_sort(root, nitems);
10639800Sbostic 	} else {
10739800Sbostic 		if (!(root = fts_root((char *)argv)))
10839800Sbostic 			goto mem2;
10942273Sbostic 		maxlen = root->fts_namelen;
11042273Sbostic 		root->fts_link = NULL;
11142273Sbostic 		root->fts_level = ROOTLEVEL;
11242273Sbostic 		root->fts_parent = parent;
11339800Sbostic 	}
11439800Sbostic 
11542281Sbostic 	/*
11642281Sbostic 	 * allocate a dummy pointer and make ftsread think that we've just
11742281Sbostic 	 * finished the node before the root(s); set p->fts_info to FTS_NS
11842281Sbostic 	 * so that everything about the "current" node is ignored.
11942281Sbostic 	 */
12042281Sbostic 	if (!(sp->fts_cur = fts_alloc("", 0)))
12142281Sbostic 		goto mem2;
12242281Sbostic 	sp->fts_cur->fts_link = root;
12342281Sbostic 	sp->fts_cur->fts_info = FTS_NS;
12442281Sbostic 
12539800Sbostic 	/* start out with at least 1K+ of path space */
12639800Sbostic 	if (!fts_path(MAX(maxlen, MAXPATHLEN)))
12742281Sbostic 		goto mem3;
12839800Sbostic 
12939800Sbostic 	/*
13042299Sbostic 	 * if using chdir(2), grab a file descriptor pointing to dot to insure
13142299Sbostic 	 * that we can get back here; this could be avoided for some paths,
13242299Sbostic 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
13342299Sbostic 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
13442299Sbostic 	 * descriptor we run anyway, just more slowly.
13539800Sbostic 	 */
13642273Sbostic 	if (!(options & FTS_NOCHDIR) &&
13742299Sbostic 	    (sp->fts_sd = open(".", O_RDONLY, 0)) < 0)
13842273Sbostic 		sp->fts_options |= FTS_NOCHDIR;
13939800Sbostic 
14039800Sbostic 	return(sp);
14139800Sbostic 
14242299Sbostic mem3:	(void)free((void *)sp->fts_cur);
14339800Sbostic mem2:	fts_lfree(root);
14442299Sbostic 	(void)free((void *)parent);
14542299Sbostic mem1:	(void)free((void *)sp);
14639800Sbostic 	return(NULL);
14739800Sbostic }
14839800Sbostic 
14939800Sbostic static
15039800Sbostic fts_load(p)
15139800Sbostic 	register FTSENT *p;
15239800Sbostic {
15339800Sbostic 	register int len;
15439800Sbostic 	register char *cp;
15539800Sbostic 
15639800Sbostic 	/*
15739800Sbostic 	 * load the stream structure for the next traversal; set the
15839800Sbostic 	 * accpath field specially so the chdir gets done to the right
15939800Sbostic 	 * place and the user can access the first node.
16039800Sbostic 	 */
16142273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
16242273Sbostic 	bcopy(p->fts_name, stream->fts_path, len + 1);
16342273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
16439800Sbostic 		len = strlen(++cp);
16542273Sbostic 		bcopy(cp, p->fts_name, len + 1);
16642273Sbostic 		p->fts_namelen = len;
16739800Sbostic 	}
16842273Sbostic 	p->fts_accpath = p->fts_path = stream->fts_path;
16939800Sbostic }
17039800Sbostic 
17139800Sbostic ftsclose(sp)
17239800Sbostic 	FTS *sp;
17339800Sbostic {
17439800Sbostic 	register FTSENT *freep, *p;
17539800Sbostic 	int saved_errno;
17639800Sbostic 
17742281Sbostic 	if (sp->fts_cur) {
17842281Sbostic 		/*
17942281Sbostic 		 * this still works if we haven't read anything -- the dummy
18042281Sbostic 		 * structure points to the root list, so we step through to
18142281Sbostic 		 * the end of the root list which has a valid parent pointer.
18242281Sbostic 		 */
18342281Sbostic 		for (p = sp->fts_cur; p->fts_level > ROOTPARENTLEVEL;) {
18442281Sbostic 			freep = p;
18542281Sbostic 			p = p->fts_link ? p->fts_link : p->fts_parent;
18642299Sbostic 			(void)free((void *)freep);
18739800Sbostic 		}
18842299Sbostic 		(void)free((void *)p);
18942281Sbostic 	}
19039800Sbostic 
19139800Sbostic 	/* free up child linked list, sort array, path buffer */
19242273Sbostic 	if (sp->fts_child)
19342273Sbostic 		fts_lfree(sp->fts_child);
19442273Sbostic 	if (sp->fts_array)
19542299Sbostic 		(void)free((void *)sp->fts_array);
19642273Sbostic 	(void)free((char *)sp->fts_path);
19739800Sbostic 
19839800Sbostic 	/*
19939800Sbostic 	 * return to original directory, save errno if necessary;
20039800Sbostic 	 * free up the directory buffer
20139800Sbostic 	 */
20242273Sbostic 	if (!(sp->fts_options & FTS_NOCHDIR)) {
20342299Sbostic 		saved_errno = fchdir(sp->fts_sd) ? errno : 0;
20442299Sbostic 		(void)close(sp->fts_sd);
20539800Sbostic 	}
20639800Sbostic 
20739800Sbostic 	/* free up the stream pointer */
20842299Sbostic 	(void)free((void *)sp);
20939800Sbostic 
21039800Sbostic 	/* set errno and return */
21142273Sbostic 	if (!(sp->fts_options & FTS_NOCHDIR) && saved_errno) {
21239800Sbostic 		errno = saved_errno;
21339800Sbostic 		return(-1);
21439800Sbostic 	}
21539800Sbostic 	return(0);
21639800Sbostic }
21739800Sbostic 
21839800Sbostic FTSENT *
21939800Sbostic ftsread(sp)
22039800Sbostic 	register FTS *sp;
22139800Sbostic {
22242299Sbostic 	register FTSENT *p, *tmp;
22339800Sbostic 	register int instr;
22442299Sbostic 	register char *cp;
22539800Sbostic 	static int cd;
22639800Sbostic 
22739800Sbostic 	/* if finished or unrecoverable error, return NULL */
22842273Sbostic 	if (!sp->fts_cur || sp->fts_options & FTS__STOP)
22939800Sbostic 		return(NULL);
23039800Sbostic 
23139800Sbostic 	/* set global stream pointer, and current node pointer */
23239800Sbostic 	stream = sp;
23342273Sbostic 	p = sp->fts_cur;
23439800Sbostic 
23539800Sbostic 	/* save and zero out user instructions */
23642273Sbostic 	instr = p->fts_instr;
23742273Sbostic 	p->fts_instr = 0;
23839800Sbostic 
23939800Sbostic 	/* if used link pointer for cycle detection, restore it */
24042273Sbostic 	if (sp->fts_savelink) {
24142273Sbostic 		p->fts_link = sp->fts_savelink;
24242273Sbostic 		sp->fts_savelink = NULL;
24339800Sbostic 	}
24439800Sbostic 
24539800Sbostic 	/* any type of file may be re-visited; re-stat and return */
24639800Sbostic 	if (instr == FTS_AGAIN) {
24742273Sbostic 		p->fts_info = fts_stat(p, 0);
24839800Sbostic 		return(p);
24939800Sbostic 	}
25039800Sbostic 
25142299Sbostic 	/* following a symbolic link */
25242299Sbostic 	if (p->fts_info == FTS_SL && instr == FTS_FOLLOW) {
25342299Sbostic 		p->fts_info = fts_stat(p, 1);
25442299Sbostic 		return(p);
25542299Sbostic 	}
25642299Sbostic 
25742299Sbostic 	/* directory in pre-order */
25842299Sbostic 	if (p->fts_info == FTS_D) {
259*42301Sbostic 		/* if skipped or crossed mount point, do post-order visit */
26042280Sbostic 		if (instr == FTS_SKIP || sp->fts_options & FTS_XDEV &&
26142280Sbostic 		    p->fts_statb.st_dev != sp->sdev) {
26242273Sbostic 			if (sp->fts_child) {
26342273Sbostic 				fts_lfree(sp->fts_child);
26442273Sbostic 				sp->fts_child = NULL;
26539800Sbostic 			}
266*42301Sbostic 			p->fts_info = FTS_DP;
267*42301Sbostic 			return(p);
26842299Sbostic 		}
26942299Sbostic 
27042299Sbostic 		/* read the directory if necessary, and return first entry */
27142299Sbostic 		if (sp->fts_child)
27242299Sbostic 			if (CHDIR(sp, p->fts_accpath)) {
27342299Sbostic 				fts_lfree(sp->fts_child);
27442299Sbostic 				p->fts_info = FTS_DNX;
27542299Sbostic 			} else {
27642299Sbostic 				p = sp->fts_child;
27742299Sbostic 				cp = sp->fts_path + NAPPEND(p->fts_parent);
27842299Sbostic 				*cp++ = '/';
27942299Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
28042299Sbostic 			}
28142299Sbostic 		else {
28242299Sbostic 			if (!(sp->fts_child = fts_build(sp, BREAD)))
28339800Sbostic 				return(p);
28442273Sbostic 			p = sp->fts_child;
28539800Sbostic 		}
28642299Sbostic 		sp->fts_child = NULL;
28742299Sbostic 		return(sp->fts_cur = p);
28839800Sbostic 	}
28939800Sbostic 
29042299Sbostic 	/* move to next node on this level */
29142299Sbostic next:	tmp = p;
29242299Sbostic 	if (p = p->fts_link) {
29339800Sbostic 		/*
29439800Sbostic 		 * if root level node, set up paths and return.  If not the
29539800Sbostic 		 * first time, and it's not an absolute pathname, get back
29642299Sbostic 		 * to starting directory.
29739800Sbostic 		 */
29842273Sbostic 		if (p->fts_level == ROOTLEVEL) {
29939800Sbostic 			fts_load(p);
30042299Sbostic 			(void)free((void *)tmp);
30142281Sbostic 			if (cd &&
30242299Sbostic 			    p->fts_path[0] != '/' && FCHDIR(sp, sp->fts_sd)) {
30342299Sbostic 				/* should never happen... */
30442299Sbostic 				p->fts_path = "starting directory";
30542281Sbostic 				p->fts_info = FTS_ERR;
30642281Sbostic 				sp->fts_options |= FTS__STOP;
30742281Sbostic 				return(sp->fts_cur = p);
30842299Sbostic 			}
30942281Sbostic 			cd = 1;
31042273Sbostic 			p->fts_info = fts_stat(p, 0);
31142280Sbostic 			sp->sdev = p->fts_statb.st_dev;
31239800Sbostic 		} else {
31342299Sbostic 			(void)free((void *)tmp);
31442299Sbostic 
31542299Sbostic 			/* user may have called ftsset on node */
31642299Sbostic 			if (p->fts_instr == FTS_SKIP)
31742299Sbostic 				goto next;
31842299Sbostic 			if (p->fts_instr == FTS_FOLLOW) {
31942299Sbostic 				p->fts_info = fts_stat(p, 1);
32042299Sbostic 				p->fts_instr = 0;
32142299Sbostic 			}
32242299Sbostic 
32342299Sbostic 			/* fill in the paths */
32442273Sbostic 			cp = sp->fts_path + NAPPEND(p->fts_parent);
32539800Sbostic 			*cp++ = '/';
32642273Sbostic 			bcopy(p->fts_name, cp, p->fts_namelen + 1);
32742299Sbostic 
32842299Sbostic 			/* check for directory cycles */
32942273Sbostic 			if (p->fts_info == FTS_D && (tmp = fts_cycle(p))) {
33042273Sbostic 				sp->fts_savelink = p->fts_link;
33142273Sbostic 				p->fts_link = tmp;
33242299Sbostic 				p->fts_info = FTS_DC;
33339800Sbostic 			}
33439800Sbostic 		}
33542273Sbostic 		return(sp->fts_cur = p);
33639800Sbostic 	}
33739800Sbostic 
33842299Sbostic 	/* move to parent */
33942299Sbostic 	p = tmp->fts_parent;
34042299Sbostic 	(void)free((void *)tmp);
34142299Sbostic 
34242273Sbostic 	if (p->fts_level == ROOTPARENTLEVEL) {
34339800Sbostic 		/*
34439800Sbostic 		 * done; free everything up and set errno to 0 so the user
34539800Sbostic 		 * can distinguish between error and EOF.
34639800Sbostic 		 */
34742299Sbostic 		(void)free((void *)p);
34839800Sbostic 		errno = 0;
34942273Sbostic 		return(sp->fts_cur = NULL);
35039800Sbostic 	}
35139800Sbostic 
35242273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
35339800Sbostic 	if (CHDIR(sp, "..")) {
35442273Sbostic 		sp->fts_options |= FTS__STOP;
35542273Sbostic 		p->fts_info = FTS_ERR;
35639800Sbostic 	} else
35742273Sbostic 		p->fts_info = FTS_DP;
35842273Sbostic 	return(sp->fts_cur = p);
35939800Sbostic }
36039800Sbostic 
36139800Sbostic /*
36239800Sbostic  * ftsset takes the stream as an argument although it's not used in this
36339800Sbostic  * implementation; it would be necessary if anyone wanted to add global
36439800Sbostic  * semantics to fts using ftsset.  A possible error return is allowed for
36539800Sbostic  * similar reasons.
36639800Sbostic  */
36739800Sbostic /* ARGSUSED */
36839800Sbostic ftsset(sp, p, instr)
36939800Sbostic 	FTS *sp;
37039800Sbostic 	FTSENT *p;
37139800Sbostic 	int instr;
37239800Sbostic {
37342273Sbostic 	p->fts_instr = instr;
37439800Sbostic 	return(0);
37539800Sbostic }
37639800Sbostic 
37739800Sbostic FTSENT *
37839800Sbostic ftschildren(sp)
37939800Sbostic 	register FTS *sp;
38039800Sbostic {
38142299Sbostic 	register FTSENT *p;
38242299Sbostic 	int fd;
38342299Sbostic 
38439800Sbostic 	/*
38539800Sbostic 	 * set errno to 0 so that user can tell the difference between an
38639800Sbostic 	 * error and a directory without entries.
38739800Sbostic 	 */
38839800Sbostic 	errno = 0;
38942299Sbostic 	p = sp->fts_cur;
39042299Sbostic 	if (p->fts_info != FTS_D || sp->fts_options & FTS__STOP)
39139800Sbostic 		return(NULL);
39242273Sbostic 	if (sp->fts_child)
39342273Sbostic 		fts_lfree(sp->fts_child);
39442299Sbostic 
39542299Sbostic 	/*
39642299Sbostic 	 * if using chdir on a relative path and called BEFORE ftsread on the
39742299Sbostic 	 * root of a traversal, we can lose because we need to chdir into the
39842299Sbostic 	 * subdirectory, and we don't know where the current directory is to
39942299Sbostic 	 * get back so that the upcoming chdir by ftsread will work.
40042299Sbostic 	 */
40142299Sbostic 	if (p->fts_level != ROOTLEVEL || p->fts_accpath[0] == '/' ||
40242299Sbostic 	    sp->fts_options & FTS_NOCHDIR)
40342299Sbostic 		return(sp->fts_child = fts_build(sp, BCHILD));
40442299Sbostic 
40542299Sbostic 	if ((fd = open(".", O_RDONLY, 0)) < 0)
40642299Sbostic 		return(NULL);
40742299Sbostic 	sp->fts_child = fts_build(sp, BCHILD);
40842299Sbostic 	if (fchdir(fd))
40942299Sbostic 		return(NULL);
41042299Sbostic 	(void)close(fd);
41142299Sbostic 	return(sp->fts_child);
41239800Sbostic }
41339800Sbostic 
41439800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
41539800Sbostic 
41642299Sbostic FTSENT *
41742299Sbostic fts_build(sp, type)
41839800Sbostic 	register FTS *sp;
41942299Sbostic 	int type;
42039800Sbostic {
42139800Sbostic 	register struct dirent *dp;
42239800Sbostic 	register FTSENT *p, *head;
42339800Sbostic 	register int nitems;
42439800Sbostic 	DIR *dirp;
42539962Sbostic 	int descend, len, level, maxlen, nlinks, saved_errno;
42639800Sbostic 	char *cp;
42739800Sbostic 
42842273Sbostic 	p = sp->fts_cur;
42942273Sbostic 	if (!(dirp = opendir(p->fts_accpath))) {
43042299Sbostic 		if (type == BREAD) {
43139800Sbostic 			errno = 0;
43242273Sbostic 			p->fts_info = FTS_DNR;
43339800Sbostic 		}
43439800Sbostic 		return(NULL);
43539800Sbostic 	}
43639800Sbostic 
43739800Sbostic 	/*
43839800Sbostic 	 * the real slowdown in walking the tree is the stat calls.  If
43939800Sbostic 	 * FTS_NOSTAT is set and it's a physical walk (so that symbolic
44039800Sbostic 	 * links can't be directories), fts assumes that the number of
44139800Sbostic 	 * subdirectories in a node is equal to the number of links to
44239800Sbostic 	 * the parent.  This allows stat calls to be skipped in any leaf
44339800Sbostic 	 * directories and for any nodes after the directories in the
44439800Sbostic 	 * parent node have been found.  This empirically cuts the stat
44539800Sbostic 	 * calls by about 2/3.
44639800Sbostic 	 */
44742273Sbostic 	nlinks =
44842273Sbostic 	    sp->fts_options & FTS_NOSTAT && sp->fts_options & FTS_PHYSICAL ?
44942273Sbostic 	    p->fts_statb.st_nlink - (sp->fts_options & FTS_SEEDOT ? 0 : 2) : -1;
45039800Sbostic 
45142299Sbostic 	/* if told to descend or found links and not told not to descend. */
45242299Sbostic 	if (nlinks || type == BREAD) {
45339962Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
45442299Sbostic 			if (type == BREAD) {
45539962Sbostic 				errno = 0;
45642273Sbostic 				p->fts_info = FTS_DNX;
45739962Sbostic 			}
45842299Sbostic 			(void)closedir(dirp);
45939962Sbostic 			return(NULL);
46039962Sbostic 		}
46139962Sbostic 		descend = 1;
46239962Sbostic 	} else
46339962Sbostic 		descend = 0;
46439962Sbostic 
46540939Sbostic 	/* get max file name length that can be stored in current path */
46642273Sbostic 	maxlen = sp->fts_pathlen - p->fts_pathlen - 1;
46740939Sbostic 
46842273Sbostic 	cp = sp->fts_path + (len = NAPPEND(p));
46940939Sbostic 	*cp++ = '/';
47040939Sbostic 
47142273Sbostic 	level = p->fts_level + 1;
47240939Sbostic 
47339800Sbostic 	/* read the directory, attching each new entry to the `link' pointer */
47439800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
47542273Sbostic 		if (ISDOT(dp->d_name) && !(sp->fts_options & FTS_SEEDOT))
47639800Sbostic 			continue;
47739800Sbostic 
47839800Sbostic 		if (!(p = fts_alloc(dp->d_name, dp->d_namlen))) {
47939800Sbostic 			saved_errno = errno;
48039800Sbostic 			goto mem1;
48139800Sbostic 		}
48239800Sbostic 		if (dp->d_namlen > maxlen) {
48339800Sbostic 			if (!fts_path((int)dp->d_namlen)) {
48439800Sbostic 				/* quit: this stream no longer has a path */
48542273Sbostic 				sp->fts_options |= FTS__STOP;
48639800Sbostic 				saved_errno = errno;
48742299Sbostic 				(void)free((void *)p);
48839800Sbostic mem1:				fts_lfree(head);
48942299Sbostic 				if (type == BREAD)
49042273Sbostic 					p->fts_info = FTS_ERR;
49139962Sbostic 				if (descend && CHDIR(sp, "..")) {
49239800Sbostic 					saved_errno = errno;
49342273Sbostic 					sp->fts_options |= FTS__STOP;
49439800Sbostic 				}
49539800Sbostic 				errno = saved_errno;
49642299Sbostic 				(void)closedir(dirp);
49739800Sbostic 				return(NULL);
49839800Sbostic 			}
49942273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
50039800Sbostic 		}
50139800Sbostic 
50242273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
50342273Sbostic 		p->fts_accpath =
50442273Sbostic 		    sp->fts_options & FTS_NOCHDIR ? p->fts_path : p->fts_name;
50539800Sbostic 
50642273Sbostic 		p->fts_parent = sp->fts_cur;
50742273Sbostic 		p->fts_level = level;
50839800Sbostic 
50939800Sbostic 		if (nlinks) {
51039800Sbostic 			/* make sure fts_stat has a filename to stat */
51142273Sbostic 			if (sp->fts_options & FTS_NOCHDIR)
51242273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
51342273Sbostic 			p->fts_info = fts_stat(p, 0);
51442273Sbostic 			if (nlinks > 0 && (p->fts_info == FTS_D ||
51542273Sbostic 			    p->fts_info == FTS_DNR || p->fts_info == FTS_DNX))
51639800Sbostic 				--nlinks;
51739800Sbostic 		} else
51842273Sbostic 			p->fts_info = FTS_NS;
51939800Sbostic 
52042273Sbostic 		p->fts_link = head;
52139800Sbostic 		head = p;
52239800Sbostic 		++nitems;
52339800Sbostic 	}
52439800Sbostic 	(void)closedir(dirp);
52539800Sbostic 
52642299Sbostic 	/* reset the path */
52742299Sbostic 	if (cp - 1 > sp->fts_path)
52842299Sbostic 		--cp;
52942299Sbostic 	*cp = '\0';
53042299Sbostic 
53142299Sbostic 	/*
53242299Sbostic 	 * if descended: if were called from ftsread and didn't find anything,
53342299Sbostic 	 * or were called from ftschildren, get back.
53442299Sbostic 	 */
53542299Sbostic 	if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
53642273Sbostic 		sp->fts_options |= FTS__STOP;
53742273Sbostic 		p->fts_info = FTS_ERR;
53839962Sbostic 		return(NULL);
53939962Sbostic 	}
54039962Sbostic 
54139800Sbostic 	if (!nitems) {
54242299Sbostic 		if (type == BREAD)
54342273Sbostic 			p->fts_info = FTS_DP;
54439800Sbostic 		return(NULL);
54539800Sbostic 	}
54639800Sbostic 
54742273Sbostic 	if (sp->fts_compar && nitems > 1)
54839800Sbostic 		head = fts_sort(head, nitems);
54939800Sbostic 
55042299Sbostic 	if (type == BREAD) {
55142299Sbostic 		*cp = '/';
55242299Sbostic 		bcopy(head->fts_name, cp + 1, head->fts_namelen + 1);
55342299Sbostic 	}
55439800Sbostic 	return(head);
55539800Sbostic }
55639800Sbostic 
55739800Sbostic static short
55839800Sbostic fts_stat(p, symflag)
55939800Sbostic 	register FTSENT *p;
56039800Sbostic 	int symflag;
56139800Sbostic {
56239800Sbostic 	/*
56339800Sbostic 	 * detection of symbolic links w/o targets.  If FTS_FOLLOW is set,
56439800Sbostic 	 * the symlink structure is overwritten with the stat structure of
56539800Sbostic 	 * the target.
56639800Sbostic 	 */
56742273Sbostic 	if (stream->fts_options & FTS_LOGICAL || symflag) {
56842273Sbostic 		if (stat(p->fts_accpath, &p->fts_statb))
56942273Sbostic 			return(symflag || !lstat(p->fts_accpath,
57042273Sbostic 			    &p->fts_statb) ? FTS_SLNONE : FTS_ERR);
57142273Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb))
57239800Sbostic 		return(FTS_ERR);
57339800Sbostic 
57442273Sbostic 	switch(p->fts_statb.st_mode&S_IFMT) {
57539800Sbostic 	case S_IFDIR:
57639800Sbostic 		return(FTS_D);
57739800Sbostic 	case S_IFLNK:
57839800Sbostic 		return(FTS_SL);
57939800Sbostic 	case S_IFREG:
58039800Sbostic 		return(FTS_F);
58139800Sbostic 	}
58240939Sbostic 	return(FTS_DEFAULT);
58339800Sbostic }
58439800Sbostic 
58539800Sbostic static FTSENT *
58639800Sbostic fts_cycle(p)
58739800Sbostic 	register FTSENT *p;
58839800Sbostic {
58939800Sbostic 	register dev_t dev;
59039800Sbostic 	register ino_t ino;
59139800Sbostic 
59239800Sbostic 	/*
59339800Sbostic 	 * cycle detection is brute force; if the tree gets deep enough or
59439800Sbostic 	 * the number of symbolic links to directories is really high
59539800Sbostic 	 * something faster might be worthwhile.
59639800Sbostic 	 */
59742273Sbostic 	dev = p->fts_statb.st_dev;
59842273Sbostic 	ino = p->fts_statb.st_ino;
59942273Sbostic 	for (p = p->fts_parent; p->fts_level > ROOTLEVEL; p = p->fts_parent)
60042273Sbostic 		if (ino == p->fts_statb.st_ino && dev == p->fts_statb.st_dev)
60139800Sbostic 			return(p);
60239800Sbostic 	return(NULL);
60339800Sbostic }
60439800Sbostic 
60539800Sbostic #define	R(type, nelem, ptr) \
60642299Sbostic 	(type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
60739800Sbostic 
60839800Sbostic static FTSENT *
60939800Sbostic fts_sort(head, nitems)
61039800Sbostic 	FTSENT *head;
61139800Sbostic 	register int nitems;
61239800Sbostic {
61339800Sbostic 	register FTSENT **ap, *p;
61439800Sbostic 
61539800Sbostic 	/*
61639800Sbostic 	 * construct an array of pointers to the structures and call qsort(3).
61739800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
61839800Sbostic 	 * sort for memory reasons, return the directory entries in their
61939800Sbostic 	 * current order.  Allocate enough space for the current needs plus
62039800Sbostic 	 * 40 so we don't realloc one entry at a time.
62139800Sbostic 	 */
62242273Sbostic 	if (nitems > stream->fts_nitems) {
62342273Sbostic 		stream->fts_nitems = nitems + 40;
62442273Sbostic 		if (!(stream->fts_array =
62542273Sbostic 		    R(FTSENT *, stream->fts_nitems, stream->fts_array))) {
62642273Sbostic 			stream->fts_nitems = 0;
62739800Sbostic 			return(head);
62839800Sbostic 		}
62939800Sbostic 	}
63042273Sbostic 	for (ap = stream->fts_array, p = head; p; p = p->fts_link)
63139800Sbostic 		*ap++ = p;
63242299Sbostic 	qsort((void *)stream->fts_array, nitems, sizeof(FTSENT *),
63342273Sbostic 	    stream->fts_compar);
63442273Sbostic 	for (head = *(ap = stream->fts_array); --nitems; ++ap)
63542273Sbostic 		ap[0]->fts_link = ap[1];
63642273Sbostic 	ap[0]->fts_link = NULL;
63739800Sbostic 	return(head);
63839800Sbostic }
63939800Sbostic 
64039800Sbostic static FTSENT *
64139800Sbostic fts_alloc(name, len)
64239800Sbostic 	char *name;
64339800Sbostic 	register int len;
64439800Sbostic {
64539800Sbostic 	register FTSENT *p;
64639800Sbostic 
64739800Sbostic 	/*
64839800Sbostic 	 * variable sized structures; the name is the last element so
64939800Sbostic 	 * allocate enough extra space after the structure to hold it.
65039800Sbostic 	 */
65142299Sbostic 	if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
65239800Sbostic 		return(NULL);
65342273Sbostic 	bcopy(name, p->fts_name, len + 1);
65442273Sbostic 	p->fts_namelen = len;
65542273Sbostic 	p->fts_path = stream->fts_path;
65642273Sbostic 	p->fts_instr = 0;
65742273Sbostic 	p->fts_local.number = 0;
65842273Sbostic 	p->fts_local.pointer = NULL;
65939800Sbostic 	return(p);
66039800Sbostic }
66139800Sbostic 
66239800Sbostic static
66339800Sbostic fts_lfree(head)
66439800Sbostic 	register FTSENT *head;
66539800Sbostic {
66639800Sbostic 	register FTSENT *p;
66739800Sbostic 
66839800Sbostic 	while (p = head) {
66942273Sbostic 		head = head->fts_link;
67042299Sbostic 		(void)free((void *)p);
67139800Sbostic 	}
67239800Sbostic }
67339800Sbostic 
67439800Sbostic /*
67539800Sbostic  * allow essentially unlimited paths; certain programs (find, remove, ls)
67639800Sbostic  * need to work on any tree.  Most systems will allow creation of paths
67739800Sbostic  * much longer than MAXPATHLEN, even though the kernel won't resolve them.
67839800Sbostic  * Add an extra 128 bytes to the requested size so that we don't realloc
67939800Sbostic  * the path 2 bytes at a time.
68039800Sbostic  */
68142299Sbostic static char *
68239800Sbostic fts_path(size)
68339800Sbostic 	int size;
68439800Sbostic {
68542273Sbostic 	stream->fts_pathlen += size + 128;
68642299Sbostic 	return(stream->fts_path =
68742299Sbostic 	    R(char, stream->fts_pathlen, stream->fts_path));
68839800Sbostic }
68939800Sbostic 
69039800Sbostic static FTSENT *
69139800Sbostic fts_root(name)
69239800Sbostic 	register char *name;
69339800Sbostic {
69439800Sbostic 	register char *cp;
69539800Sbostic 
69639800Sbostic 	/*
69739800Sbostic 	 * rip trailing slashes; it's somewhat unclear in POSIX 1003.1 what
69839800Sbostic 	 * /a/b/ really is, they don't talk about what a null path component
69939800Sbostic 	 * resolves to.  This hopefully does what the user intended.  Don't
70039800Sbostic 	 * allow null pathnames.
70139800Sbostic 	 */
70239800Sbostic 	for (cp = name; *cp; ++cp);
70339800Sbostic 	if (cp == name) {
70439800Sbostic 		errno = ENOENT;
70539800Sbostic 		return(NULL);
70639800Sbostic 	}
70739800Sbostic 	while (--cp > name && *cp == '/');
70839800Sbostic 	*++cp = '\0';
70939800Sbostic 	return(fts_alloc(name, cp - name));
71039800Sbostic }
711