xref: /csrg-svn/lib/libc/gen/fts.c (revision 42280)
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*42280Sbostic static char sccsid[] = "@(#)fts.c	5.5 (Berkeley) 05/22/90";
1039800Sbostic #endif /* LIBC_SCCS and not lint */
1139800Sbostic 
1239800Sbostic #include <sys/param.h>
1339800Sbostic #include <sys/stat.h>
1439800Sbostic #include <dirent.h>
1539800Sbostic #include <errno.h>
1639800Sbostic #include <fts.h>
1742273Sbostic #include <string.h>
1839800Sbostic 
1939800Sbostic extern int errno;
2039800Sbostic 
2139800Sbostic FTSENT *fts_alloc(), *fts_build(), *fts_cycle(), *fts_sort(), *fts_root();
2239800Sbostic short fts_stat();
2339800Sbostic char *malloc(), *realloc();
2439800Sbostic 
2539800Sbostic /*
2639800Sbostic  * Special case a root of "/" so that slashes aren't appended causing
2739800Sbostic  * paths to be written as "//foo".
2839800Sbostic  */
2939800Sbostic #define	NAPPEND(p) \
3042273Sbostic 	(p->fts_level == ROOTLEVEL && p->fts_pathlen == 1 && \
3142273Sbostic 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
3239800Sbostic 
3342273Sbostic #define	CHDIR(sp, path)	(!(sp->fts_options & FTS_NOCHDIR) && chdir(path))
3442273Sbostic #define	FCHDIR(sp, fd)	(!(sp->fts_options & FTS_NOCHDIR) && fchdir(fd))
3539800Sbostic 
3639800Sbostic #define	ROOTLEVEL	0
3739800Sbostic #define	ROOTPARENTLEVEL	-1
3839800Sbostic 
3939800Sbostic static FTS *stream;			/* current stream pointer */
4039800Sbostic 
4139800Sbostic FTS *
4239800Sbostic ftsopen(argv, options, compar)
4339800Sbostic 	char *argv[];
4439800Sbostic 	register int options;
4539800Sbostic 	int (*compar)();
4639800Sbostic {
4739800Sbostic 	register FTS *sp;
4839800Sbostic 	register FTSENT *p, *root;
4939800Sbostic 	register int nitems, maxlen;
5039800Sbostic 	FTSENT *parent, *tmp;
5139800Sbostic 	char wd[MAXPATHLEN], *getwd(), *strdup();
5239800Sbostic 
5339800Sbostic 	/* allocate/initialize the stream */
5439800Sbostic 	if (!(stream = sp = (FTS *)malloc((u_int)sizeof(FTS))))
5539800Sbostic 		return(NULL);
5639800Sbostic 	bzero(sp, sizeof(FTS));
5742273Sbostic 	sp->fts_compar = compar;
5842273Sbostic 	sp->fts_options = options;
5939800Sbostic 
6039800Sbostic 	/* allocate/initialize root's parent */
6139800Sbostic 	if (!(parent = fts_alloc("", 0)))
6239800Sbostic 		goto mem1;
6342273Sbostic 	parent->fts_level = ROOTPARENTLEVEL;
6439800Sbostic 
6539800Sbostic 	/* allocate/initialize root(s) */
6639800Sbostic 	if (options & FTS_MULTIPLE) {
6739800Sbostic 		maxlen = -1;
6839800Sbostic 		for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
6939800Sbostic 			if (!(p = fts_root(*argv)))
7039800Sbostic 				goto mem2;
7142273Sbostic 			if (maxlen < p->fts_namelen)
7242273Sbostic 				maxlen = p->fts_namelen;
7339800Sbostic 			/*
7439800Sbostic 			 * if comparison routine supplied, traverse in sorted
7539800Sbostic 			 * order; otherwise traverse in the order specified.
7639800Sbostic 			 */
7739800Sbostic 			if (compar) {
7842273Sbostic 				p->fts_link = root;
7939800Sbostic 				root = p;
8042273Sbostic 				p->fts_accpath = p->fts_name;
8142273Sbostic 				p->fts_info = fts_stat(p, 0);
8239800Sbostic 			} else {
8342273Sbostic 				p->fts_link = NULL;
8439800Sbostic 				if (!root)
8539800Sbostic 					tmp = root = p;
8639800Sbostic 				else {
8742273Sbostic 					tmp->fts_link = p;
8839800Sbostic 					tmp = p;
8939800Sbostic 				}
9039800Sbostic 			}
9142273Sbostic 			p->fts_level = ROOTLEVEL;
9242273Sbostic 			p->fts_parent = parent;
9339800Sbostic 		}
9439800Sbostic 		if (compar && nitems > 1)
9539800Sbostic 			root = fts_sort(root, nitems);
9639800Sbostic 	} else {
9739800Sbostic 		if (!(root = fts_root((char *)argv)))
9839800Sbostic 			goto mem2;
9942273Sbostic 		maxlen = root->fts_namelen;
10042273Sbostic 		root->fts_link = NULL;
10142273Sbostic 		root->fts_level = ROOTLEVEL;
10242273Sbostic 		root->fts_parent = parent;
10339800Sbostic 	}
10439800Sbostic 
10539800Sbostic 	/* start out with at least 1K+ of path space */
10639800Sbostic 	if (!fts_path(MAX(maxlen, MAXPATHLEN)))
10739800Sbostic 		goto mem2;
10839800Sbostic 
10939800Sbostic 	/*
11039800Sbostic 	 * some minor trickiness.  Set the pointers so that ftsread thinks
11142273Sbostic 	 * we've just finished the node before the root(s); set p->fts_info
11242273Sbostic 	 * to FTS_NS so that everything about the "current" node is ignored.
11339800Sbostic 	 * Rather than allocate a dummy node use the root's parent's link
11439800Sbostic 	 * pointer.  This is handled specially in ftsclose() as well.
11539800Sbostic 	 */
11642273Sbostic 	sp->fts_cur = parent;
11742273Sbostic 	parent->fts_link = root;
11842273Sbostic 	parent->fts_info = FTS_NS;
11939800Sbostic 
12039800Sbostic 	/*
12139800Sbostic 	 * if using chdir(2), do a getwd() to insure that we can get back
12239800Sbostic 	 * here; this could be avoided for some paths, but probably not
12339800Sbostic 	 * worth the effort.  Slashes, symbolic links, and ".." are all
12439800Sbostic 	 * fairly nasty problems.  Note, if we can't get the current
12539800Sbostic 	 * working directory we run anyway, just more slowly.
12639800Sbostic 	 */
12742273Sbostic 	if (!(options & FTS_NOCHDIR) &&
12842273Sbostic 	    (!getwd(wd) || !(sp->fts_wd = strdup(wd))))
12942273Sbostic 		sp->fts_options |= FTS_NOCHDIR;
13039800Sbostic 
13139800Sbostic 	return(sp);
13239800Sbostic 
13339800Sbostic mem2:	fts_lfree(root);
13439800Sbostic 	(void)free((char *)parent);
13539800Sbostic mem1:	(void)free((char *)sp);
13639800Sbostic 	return(NULL);
13739800Sbostic }
13839800Sbostic 
13939800Sbostic static
14039800Sbostic fts_load(p)
14139800Sbostic 	register FTSENT *p;
14239800Sbostic {
14339800Sbostic 	register int len;
14439800Sbostic 	register char *cp;
14539800Sbostic 
14639800Sbostic 	/*
14739800Sbostic 	 * load the stream structure for the next traversal; set the
14839800Sbostic 	 * accpath field specially so the chdir gets done to the right
14939800Sbostic 	 * place and the user can access the first node.
15039800Sbostic 	 */
15142273Sbostic 	len = p->fts_pathlen = p->fts_namelen;
15242273Sbostic 	bcopy(p->fts_name, stream->fts_path, len + 1);
15342273Sbostic 	if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
15439800Sbostic 		len = strlen(++cp);
15542273Sbostic 		bcopy(cp, p->fts_name, len + 1);
15642273Sbostic 		p->fts_namelen = len;
15739800Sbostic 	}
15842273Sbostic 	p->fts_accpath = p->fts_path = stream->fts_path;
15939800Sbostic }
16039800Sbostic 
16139800Sbostic ftsclose(sp)
16239800Sbostic 	FTS *sp;
16339800Sbostic {
16439800Sbostic 	register FTSENT *freep, *p;
16539800Sbostic 	int saved_errno;
16639800Sbostic 
16742273Sbostic 	if (sp->fts_cur)
16839800Sbostic 		/* check for never having read anything */
16942273Sbostic 		if (sp->fts_cur->fts_level == ROOTPARENTLEVEL)
17042273Sbostic 			fts_lfree(sp->fts_cur);
17139800Sbostic 		else {
17242273Sbostic 			for (p = sp->fts_cur; p->fts_level > ROOTPARENTLEVEL;) {
17339800Sbostic 				freep = p;
17442273Sbostic 				p = p->fts_link ? p->fts_link : p->fts_parent;
17539800Sbostic 				(void)free((char *)freep);
17639800Sbostic 			}
17739800Sbostic 			(void)free((char *)p);
17839800Sbostic 		}
17939800Sbostic 
18039800Sbostic 	/* free up child linked list, sort array, path buffer */
18142273Sbostic 	if (sp->fts_child)
18242273Sbostic 		fts_lfree(sp->fts_child);
18342273Sbostic 	if (sp->fts_array)
18442273Sbostic 		(void)free((char *)sp->fts_array);
18542273Sbostic 	(void)free((char *)sp->fts_path);
18639800Sbostic 
18739800Sbostic 	/*
18839800Sbostic 	 * return to original directory, save errno if necessary;
18939800Sbostic 	 * free up the directory buffer
19039800Sbostic 	 */
19142273Sbostic 	if (!(sp->fts_options & FTS_NOCHDIR)) {
19242273Sbostic 		saved_errno = chdir(sp->fts_wd) ? errno : 0;
19342273Sbostic 		(void)free(sp->fts_wd);
19439800Sbostic 	}
19539800Sbostic 
19639800Sbostic 	/* free up the stream pointer */
19739800Sbostic 	(void)free((char *)sp);
19839800Sbostic 
19939800Sbostic 	/* set errno and return */
20042273Sbostic 	if (!(sp->fts_options & FTS_NOCHDIR) && saved_errno) {
20139800Sbostic 		errno = saved_errno;
20239800Sbostic 		return(-1);
20339800Sbostic 	}
20439800Sbostic 	return(0);
20539800Sbostic }
20639800Sbostic 
20739800Sbostic FTSENT *
20839800Sbostic ftsread(sp)
20939800Sbostic 	register FTS *sp;
21039800Sbostic {
21139800Sbostic 	register FTSENT *p;
21239800Sbostic 	register int instr;
21339800Sbostic 	static int cd;
21439800Sbostic 	FTSENT *tmp;
21539800Sbostic 	char *cp;
21639800Sbostic 
21739800Sbostic 	/* if finished or unrecoverable error, return NULL */
21842273Sbostic 	if (!sp->fts_cur || sp->fts_options & FTS__STOP)
21939800Sbostic 		return(NULL);
22039800Sbostic 
22139800Sbostic 	/* set global stream pointer, and current node pointer */
22239800Sbostic 	stream = sp;
22342273Sbostic 	p = sp->fts_cur;
22439800Sbostic 
22539800Sbostic 	/* save and zero out user instructions */
22642273Sbostic 	instr = p->fts_instr;
22742273Sbostic 	p->fts_instr = 0;
22839800Sbostic 
22939800Sbostic 	/* if used link pointer for cycle detection, restore it */
23042273Sbostic 	if (sp->fts_savelink) {
23142273Sbostic 		p->fts_link = sp->fts_savelink;
23242273Sbostic 		sp->fts_savelink = NULL;
23339800Sbostic 	}
23439800Sbostic 
23539800Sbostic 	/* any type of file may be re-visited; re-stat and return */
23639800Sbostic 	if (instr == FTS_AGAIN) {
23742273Sbostic 		p->fts_info = fts_stat(p, 0);
23839800Sbostic 		return(p);
23939800Sbostic 	}
24039800Sbostic 
24142273Sbostic 	if (p->fts_info == FTS_D)
242*42280Sbostic 		if (instr == FTS_SKIP || sp->fts_options & FTS_XDEV &&
243*42280Sbostic 		    p->fts_statb.st_dev != sp->sdev) {
24442273Sbostic 			if (sp->fts_child) {
24542273Sbostic 				fts_lfree(sp->fts_child);
24642273Sbostic 				sp->fts_child = NULL;
24739800Sbostic 			}
24839800Sbostic 		} else {
24942273Sbostic 			if (!sp->fts_child &&
25042273Sbostic 			    (!(sp->fts_child = fts_build(sp, 1))))
25139800Sbostic 				return(p);
25242273Sbostic 			p = sp->fts_child;
25342273Sbostic 			sp->fts_child = NULL;
25442273Sbostic 			return(sp->fts_cur = p);
25539800Sbostic 		}
25642273Sbostic 	else if (p->fts_info == FTS_SL && instr == FTS_FOLLOW) {
25742273Sbostic 		p->fts_info = fts_stat(p, 1);
25839800Sbostic 		return(p);
25939800Sbostic 	}
26039800Sbostic 
26139800Sbostic 	/*
26239800Sbostic 	 * user may have called ftsset on pointer returned by
26339800Sbostic 	 * ftschildren; handle it here.
26439800Sbostic 	 */
26542273Sbostic 	for (p = p->fts_link; p;) {
26642273Sbostic 		instr = p->fts_instr;
26739800Sbostic 		if (instr == FTS_FOLLOW) {
26842273Sbostic 			p->fts_info = fts_stat(p, 1);
26942273Sbostic 			p->fts_instr = 0;
27039800Sbostic 			break;
27139800Sbostic 		}
27239800Sbostic 		if (instr == FTS_SKIP) {
27339800Sbostic 			tmp = p;
27442273Sbostic 			p = p->fts_link;
27539800Sbostic 			(void)free((char *)tmp);
27639800Sbostic 			continue;
27739800Sbostic 		}
27842273Sbostic 		p->fts_instr = 0;
27939800Sbostic 		break;
28039800Sbostic 	}
28139800Sbostic 
28239800Sbostic 	/* go to next node on this level */
28339800Sbostic 	if (p) {
28439800Sbostic 		/*
28539800Sbostic 		 * if root level node, set up paths and return.  If not the
28639800Sbostic 		 * first time, and it's not an absolute pathname, get back
28739800Sbostic 		 * to wherever we started from.
28839800Sbostic 		 */
28942273Sbostic 		if (p->fts_level == ROOTLEVEL) {
29039800Sbostic 			fts_load(p);
29139800Sbostic 			if (cd) {
29242273Sbostic 				(void)free((char *)sp->fts_cur);
29342273Sbostic 				if (p->fts_path[0] != '/' &&
29442273Sbostic 				    CHDIR(sp, sp->fts_wd)) {
29539800Sbostic 					/* return target path for error msg */
29642273Sbostic 					p->fts_path = sp->fts_wd;
29742273Sbostic 					p->fts_info = FTS_ERR;
29842273Sbostic 					sp->fts_options |= FTS__STOP;
29942273Sbostic 					return(sp->fts_cur = p);
30039800Sbostic 				}
30139800Sbostic 			} else
30239800Sbostic 				cd = 1;
30342273Sbostic 			p->fts_info = fts_stat(p, 0);
304*42280Sbostic 			sp->sdev = p->fts_statb.st_dev;
30539800Sbostic 		} else {
30642273Sbostic 			(void)free((char *)sp->fts_cur);
30742273Sbostic 			cp = sp->fts_path + NAPPEND(p->fts_parent);
30839800Sbostic 			*cp++ = '/';
30942273Sbostic 			bcopy(p->fts_name, cp, p->fts_namelen + 1);
31042273Sbostic 			if (p->fts_info == FTS_D && (tmp = fts_cycle(p))) {
31142273Sbostic 				sp->fts_savelink = p->fts_link;
31242273Sbostic 				p->fts_link = tmp;
31339800Sbostic 			}
31439800Sbostic 		}
31542273Sbostic 		return(sp->fts_cur = p);
31639800Sbostic 	}
31739800Sbostic 
31839800Sbostic 	/* go to parent */
31942273Sbostic 	p = sp->fts_cur->fts_parent;
32042273Sbostic 	(void)free((char *)sp->fts_cur);
32142273Sbostic 	if (p->fts_level == ROOTPARENTLEVEL) {
32239800Sbostic 		/*
32339800Sbostic 		 * done; free everything up and set errno to 0 so the user
32439800Sbostic 		 * can distinguish between error and EOF.
32539800Sbostic 		 */
32639800Sbostic 		(void)free((char *)p);
32739800Sbostic 		errno = 0;
32842273Sbostic 		return(sp->fts_cur = NULL);
32939800Sbostic 	}
33039800Sbostic 
33142273Sbostic 	sp->fts_path[p->fts_pathlen] = '\0';
33239800Sbostic 	if (CHDIR(sp, "..")) {
33342273Sbostic 		sp->fts_options |= FTS__STOP;
33442273Sbostic 		p->fts_info = FTS_ERR;
33539800Sbostic 	} else
33642273Sbostic 		p->fts_info = FTS_DP;
33742273Sbostic 	return(sp->fts_cur = p);
33839800Sbostic }
33939800Sbostic 
34039800Sbostic /*
34139800Sbostic  * ftsset takes the stream as an argument although it's not used in this
34239800Sbostic  * implementation; it would be necessary if anyone wanted to add global
34339800Sbostic  * semantics to fts using ftsset.  A possible error return is allowed for
34439800Sbostic  * similar reasons.
34539800Sbostic  */
34639800Sbostic /* ARGSUSED */
34739800Sbostic ftsset(sp, p, instr)
34839800Sbostic 	FTS *sp;
34939800Sbostic 	FTSENT *p;
35039800Sbostic 	int instr;
35139800Sbostic {
35242273Sbostic 	p->fts_instr = instr;
35339800Sbostic 	return(0);
35439800Sbostic }
35539800Sbostic 
35639800Sbostic FTSENT *
35739800Sbostic ftschildren(sp)
35839800Sbostic 	register FTS *sp;
35939800Sbostic {
36039800Sbostic 	/*
36139800Sbostic 	 * set errno to 0 so that user can tell the difference between an
36239800Sbostic 	 * error and a directory without entries.
36339800Sbostic 	 */
36439800Sbostic 	errno = 0;
36542273Sbostic 	if (sp->fts_cur->fts_info != FTS_D || sp->fts_options & FTS__STOP)
36639800Sbostic 		return(NULL);
36742273Sbostic 	if (sp->fts_child)
36842273Sbostic 		fts_lfree(sp->fts_child);
36942273Sbostic 	return(sp->fts_child = fts_build(sp, 0));
37039800Sbostic }
37139800Sbostic 
37239800Sbostic #define	ISDOT(a)	(a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
37339800Sbostic 
37439800Sbostic static FTSENT *
37539962Sbostic fts_build(sp, set_node)
37639800Sbostic 	register FTS *sp;
37739962Sbostic 	int set_node;
37839800Sbostic {
37939800Sbostic 	register struct dirent *dp;
38039800Sbostic 	register FTSENT *p, *head;
38139800Sbostic 	register int nitems;
38239800Sbostic 	DIR *dirp;
38339962Sbostic 	int descend, len, level, maxlen, nlinks, saved_errno;
38439800Sbostic 	char *cp;
38539800Sbostic 
38642273Sbostic 	p = sp->fts_cur;
38742273Sbostic 	if (!(dirp = opendir(p->fts_accpath))) {
38839962Sbostic 		if (set_node) {
38939800Sbostic 			errno = 0;
39042273Sbostic 			p->fts_info = FTS_DNR;
39139800Sbostic 		}
39239800Sbostic 		return(NULL);
39339800Sbostic 	}
39439800Sbostic 
39539800Sbostic 	/*
39639800Sbostic 	 * the real slowdown in walking the tree is the stat calls.  If
39739800Sbostic 	 * FTS_NOSTAT is set and it's a physical walk (so that symbolic
39839800Sbostic 	 * links can't be directories), fts assumes that the number of
39939800Sbostic 	 * subdirectories in a node is equal to the number of links to
40039800Sbostic 	 * the parent.  This allows stat calls to be skipped in any leaf
40139800Sbostic 	 * directories and for any nodes after the directories in the
40239800Sbostic 	 * parent node have been found.  This empirically cuts the stat
40339800Sbostic 	 * calls by about 2/3.
40439800Sbostic 	 */
40542273Sbostic 	nlinks =
40642273Sbostic 	    sp->fts_options & FTS_NOSTAT && sp->fts_options & FTS_PHYSICAL ?
40742273Sbostic 	    p->fts_statb.st_nlink - (sp->fts_options & FTS_SEEDOT ? 0 : 2) : -1;
40839800Sbostic 
40939962Sbostic 	if (nlinks || set_node) {
41039962Sbostic 		if (FCHDIR(sp, dirfd(dirp))) {
41139962Sbostic 			if (set_node) {
41239962Sbostic 				errno = 0;
41342273Sbostic 				p->fts_info = FTS_DNX;
41439962Sbostic 			}
41539962Sbostic 			return(NULL);
41639962Sbostic 		}
41739962Sbostic 		descend = 1;
41839962Sbostic 	} else
41939962Sbostic 		descend = 0;
42039962Sbostic 
42140939Sbostic 	/* get max file name length that can be stored in current path */
42242273Sbostic 	maxlen = sp->fts_pathlen - p->fts_pathlen - 1;
42340939Sbostic 
42442273Sbostic 	cp = sp->fts_path + (len = NAPPEND(p));
42540939Sbostic 	*cp++ = '/';
42640939Sbostic 
42742273Sbostic 	level = p->fts_level + 1;
42840939Sbostic 
42939800Sbostic 	/* read the directory, attching each new entry to the `link' pointer */
43039800Sbostic 	for (head = NULL, nitems = 0; dp = readdir(dirp);) {
43142273Sbostic 		if (ISDOT(dp->d_name) && !(sp->fts_options & FTS_SEEDOT))
43239800Sbostic 			continue;
43339800Sbostic 
43439800Sbostic 		if (!(p = fts_alloc(dp->d_name, dp->d_namlen))) {
43539800Sbostic 			saved_errno = errno;
43639800Sbostic 			goto mem1;
43739800Sbostic 		}
43839800Sbostic 		if (dp->d_namlen > maxlen) {
43939800Sbostic 			if (!fts_path((int)dp->d_namlen)) {
44039800Sbostic 				/* quit: this stream no longer has a path */
44142273Sbostic 				sp->fts_options |= FTS__STOP;
44239800Sbostic 				saved_errno = errno;
44339800Sbostic 				(void)free((char *)p);
44439800Sbostic mem1:				fts_lfree(head);
44539962Sbostic 				if (set_node)
44642273Sbostic 					p->fts_info = FTS_ERR;
44739962Sbostic 				if (descend && CHDIR(sp, "..")) {
44839800Sbostic 					saved_errno = errno;
44942273Sbostic 					sp->fts_options |= FTS__STOP;
45039800Sbostic 				}
45139800Sbostic 				errno = saved_errno;
45239800Sbostic 				return(NULL);
45339800Sbostic 			}
45442273Sbostic 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
45539800Sbostic 		}
45639800Sbostic 
45742273Sbostic 		p->fts_pathlen = len + dp->d_namlen + 1;
45842273Sbostic 		p->fts_accpath =
45942273Sbostic 		    sp->fts_options & FTS_NOCHDIR ? p->fts_path : p->fts_name;
46039800Sbostic 
46142273Sbostic 		p->fts_parent = sp->fts_cur;
46242273Sbostic 		p->fts_level = level;
46339800Sbostic 
46439800Sbostic 		if (nlinks) {
46539800Sbostic 			/* make sure fts_stat has a filename to stat */
46642273Sbostic 			if (sp->fts_options & FTS_NOCHDIR)
46742273Sbostic 				bcopy(p->fts_name, cp, p->fts_namelen + 1);
46842273Sbostic 			p->fts_info = fts_stat(p, 0);
46942273Sbostic 			if (nlinks > 0 && (p->fts_info == FTS_D ||
47042273Sbostic 			    p->fts_info == FTS_DNR || p->fts_info == FTS_DNX))
47139800Sbostic 				--nlinks;
47239800Sbostic 		} else
47342273Sbostic 			p->fts_info = FTS_NS;
47439800Sbostic 
47542273Sbostic 		p->fts_link = head;
47639800Sbostic 		head = p;
47739800Sbostic 		++nitems;
47839800Sbostic 	}
47939800Sbostic 	(void)closedir(dirp);
48039800Sbostic 
48139962Sbostic 	if (descend && (!nitems || !set_node) && CHDIR(sp, "..")) {
48242273Sbostic 		sp->fts_options |= FTS__STOP;
48342273Sbostic 		p->fts_info = FTS_ERR;
48439962Sbostic 		*--cp = '\0';
48539962Sbostic 		return(NULL);
48639962Sbostic 	}
48739962Sbostic 
48839800Sbostic 	if (!nitems) {
48939962Sbostic 		if (set_node)
49042273Sbostic 			p->fts_info = FTS_DP;
49139800Sbostic 		*--cp = '\0';
49239800Sbostic 		return(NULL);
49339800Sbostic 	}
49439800Sbostic 
49542273Sbostic 	if (sp->fts_compar && nitems > 1)
49639800Sbostic 		head = fts_sort(head, nitems);
49739800Sbostic 
49839962Sbostic 	if (set_node)
49942273Sbostic 		bcopy(head->fts_name, cp, head->fts_namelen + 1);
50039800Sbostic 	else
50139800Sbostic 		*--cp = '\0';
50239800Sbostic 	return(head);
50339800Sbostic }
50439800Sbostic 
50539800Sbostic static short
50639800Sbostic fts_stat(p, symflag)
50739800Sbostic 	register FTSENT *p;
50839800Sbostic 	int symflag;
50939800Sbostic {
51039800Sbostic 	/*
51139800Sbostic 	 * detection of symbolic links w/o targets.  If FTS_FOLLOW is set,
51239800Sbostic 	 * the symlink structure is overwritten with the stat structure of
51339800Sbostic 	 * the target.
51439800Sbostic 	 */
51542273Sbostic 	if (stream->fts_options & FTS_LOGICAL || symflag) {
51642273Sbostic 		if (stat(p->fts_accpath, &p->fts_statb))
51742273Sbostic 			return(symflag || !lstat(p->fts_accpath,
51842273Sbostic 			    &p->fts_statb) ? FTS_SLNONE : FTS_ERR);
51942273Sbostic 	} else if (lstat(p->fts_accpath, &p->fts_statb))
52039800Sbostic 		return(FTS_ERR);
52139800Sbostic 
52242273Sbostic 	switch(p->fts_statb.st_mode&S_IFMT) {
52339800Sbostic 	case S_IFDIR:
52439800Sbostic 		return(FTS_D);
52539800Sbostic 	case S_IFLNK:
52639800Sbostic 		return(FTS_SL);
52739800Sbostic 	case S_IFREG:
52839800Sbostic 		return(FTS_F);
52939800Sbostic 	}
53040939Sbostic 	return(FTS_DEFAULT);
53139800Sbostic }
53239800Sbostic 
53339800Sbostic static FTSENT *
53439800Sbostic fts_cycle(p)
53539800Sbostic 	register FTSENT *p;
53639800Sbostic {
53739800Sbostic 	register dev_t dev;
53839800Sbostic 	register ino_t ino;
53939800Sbostic 
54039800Sbostic 	/*
54139800Sbostic 	 * cycle detection is brute force; if the tree gets deep enough or
54239800Sbostic 	 * the number of symbolic links to directories is really high
54339800Sbostic 	 * something faster might be worthwhile.
54439800Sbostic 	 */
54542273Sbostic 	dev = p->fts_statb.st_dev;
54642273Sbostic 	ino = p->fts_statb.st_ino;
54742273Sbostic 	for (p = p->fts_parent; p->fts_level > ROOTLEVEL; p = p->fts_parent)
54842273Sbostic 		if (ino == p->fts_statb.st_ino && dev == p->fts_statb.st_dev)
54939800Sbostic 			return(p);
55039800Sbostic 	return(NULL);
55139800Sbostic }
55239800Sbostic 
55339800Sbostic #define	R(type, nelem, ptr) \
55439800Sbostic 	(type *)realloc((char *)ptr, (u_int)((nelem) * sizeof(type)))
55539800Sbostic 
55639800Sbostic static FTSENT *
55739800Sbostic fts_sort(head, nitems)
55839800Sbostic 	FTSENT *head;
55939800Sbostic 	register int nitems;
56039800Sbostic {
56139800Sbostic 	register FTSENT **ap, *p;
56239800Sbostic 
56339800Sbostic 	/*
56439800Sbostic 	 * construct an array of pointers to the structures and call qsort(3).
56539800Sbostic 	 * Reassemble the array in the order returned by qsort.  If unable to
56639800Sbostic 	 * sort for memory reasons, return the directory entries in their
56739800Sbostic 	 * current order.  Allocate enough space for the current needs plus
56839800Sbostic 	 * 40 so we don't realloc one entry at a time.
56939800Sbostic 	 */
57042273Sbostic 	if (nitems > stream->fts_nitems) {
57142273Sbostic 		stream->fts_nitems = nitems + 40;
57242273Sbostic 		if (!(stream->fts_array =
57342273Sbostic 		    R(FTSENT *, stream->fts_nitems, stream->fts_array))) {
57442273Sbostic 			stream->fts_nitems = 0;
57539800Sbostic 			return(head);
57639800Sbostic 		}
57739800Sbostic 	}
57842273Sbostic 	for (ap = stream->fts_array, p = head; p; p = p->fts_link)
57939800Sbostic 		*ap++ = p;
58042273Sbostic 	qsort((char *)stream->fts_array, nitems, sizeof(FTSENT *),
58142273Sbostic 	    stream->fts_compar);
58242273Sbostic 	for (head = *(ap = stream->fts_array); --nitems; ++ap)
58342273Sbostic 		ap[0]->fts_link = ap[1];
58442273Sbostic 	ap[0]->fts_link = NULL;
58539800Sbostic 	return(head);
58639800Sbostic }
58739800Sbostic 
58839800Sbostic static FTSENT *
58939800Sbostic fts_alloc(name, len)
59039800Sbostic 	char *name;
59139800Sbostic 	register int len;
59239800Sbostic {
59339800Sbostic 	register FTSENT *p;
59439800Sbostic 
59539800Sbostic 	/*
59639800Sbostic 	 * variable sized structures; the name is the last element so
59739800Sbostic 	 * allocate enough extra space after the structure to hold it.
59839800Sbostic 	 */
59939800Sbostic 	if (!(p = (FTSENT *)malloc((u_int)(sizeof(FTSENT) + len))))
60039800Sbostic 		return(NULL);
60142273Sbostic 	bcopy(name, p->fts_name, len + 1);
60242273Sbostic 	p->fts_namelen = len;
60342273Sbostic 	p->fts_path = stream->fts_path;
60442273Sbostic 	p->fts_instr = 0;
60542273Sbostic 	p->fts_local.number = 0;
60642273Sbostic 	p->fts_local.pointer = NULL;
60739800Sbostic 	return(p);
60839800Sbostic }
60939800Sbostic 
61039800Sbostic static
61139800Sbostic fts_lfree(head)
61239800Sbostic 	register FTSENT *head;
61339800Sbostic {
61439800Sbostic 	register FTSENT *p;
61539800Sbostic 
61639800Sbostic 	while (p = head) {
61742273Sbostic 		head = head->fts_link;
61839800Sbostic 		(void)free((char *)p);
61939800Sbostic 	}
62039800Sbostic }
62139800Sbostic 
62239800Sbostic /*
62339800Sbostic  * allow essentially unlimited paths; certain programs (find, remove, ls)
62439800Sbostic  * need to work on any tree.  Most systems will allow creation of paths
62539800Sbostic  * much longer than MAXPATHLEN, even though the kernel won't resolve them.
62639800Sbostic  * Add an extra 128 bytes to the requested size so that we don't realloc
62739800Sbostic  * the path 2 bytes at a time.
62839800Sbostic  */
62939800Sbostic static
63039800Sbostic fts_path(size)
63139800Sbostic 	int size;
63239800Sbostic {
63342273Sbostic 	stream->fts_pathlen += size + 128;
63442273Sbostic 	return((int)(stream->fts_path =
63542273Sbostic 	    R(char, stream->fts_pathlen, stream->fts_path)));
63639800Sbostic }
63739800Sbostic 
63839800Sbostic static FTSENT *
63939800Sbostic fts_root(name)
64039800Sbostic 	register char *name;
64139800Sbostic {
64239800Sbostic 	register char *cp;
64339800Sbostic 
64439800Sbostic 	/*
64539800Sbostic 	 * rip trailing slashes; it's somewhat unclear in POSIX 1003.1 what
64639800Sbostic 	 * /a/b/ really is, they don't talk about what a null path component
64739800Sbostic 	 * resolves to.  This hopefully does what the user intended.  Don't
64839800Sbostic 	 * allow null pathnames.
64939800Sbostic 	 */
65039800Sbostic 	for (cp = name; *cp; ++cp);
65139800Sbostic 	if (cp == name) {
65239800Sbostic 		errno = ENOENT;
65339800Sbostic 		return(NULL);
65439800Sbostic 	}
65539800Sbostic 	while (--cp > name && *cp == '/');
65639800Sbostic 	*++cp = '\0';
65739800Sbostic 	return(fts_alloc(name, cp - name));
65839800Sbostic }
659