xref: /freebsd/lib/libc/gen/fts.c (revision 8a507b98ab8d7f882e4c691ebfa4e84d6651f9d1)
19a91f1ccSWarner Losh /*	$OpenBSD: fts.c,v 1.9 1997/08/02 00:13:49 millert Exp $	*/
29a91f1ccSWarner Losh 
358f0484fSRodney W. Grimes /*-
458f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1558f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1658f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1758f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1858f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1958f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes  *    without specific prior written permission.
2258f0484fSRodney W. Grimes  *
2358f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes  * SUCH DAMAGE.
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
37adf6ad9eSPeter Wemm static char sccsid[] = "@(#)fts.c	8.6 (Berkeley) 8/14/94";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <sys/param.h>
4158f0484fSRodney W. Grimes #include <sys/stat.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <dirent.h>
4458f0484fSRodney W. Grimes #include <errno.h>
4558f0484fSRodney W. Grimes #include <fcntl.h>
4658f0484fSRodney W. Grimes #include <fts.h>
4758f0484fSRodney W. Grimes #include <stdlib.h>
4858f0484fSRodney W. Grimes #include <string.h>
4958f0484fSRodney W. Grimes #include <unistd.h>
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes static FTSENT	*fts_alloc __P((FTS *, char *, int));
5258f0484fSRodney W. Grimes static FTSENT	*fts_build __P((FTS *, int));
5358f0484fSRodney W. Grimes static void	 fts_lfree __P((FTSENT *));
5458f0484fSRodney W. Grimes static void	 fts_load __P((FTS *, FTSENT *));
5558f0484fSRodney W. Grimes static size_t	 fts_maxarglen __P((char * const *));
5658f0484fSRodney W. Grimes static void	 fts_padjust __P((FTS *, void *));
5758f0484fSRodney W. Grimes static int	 fts_palloc __P((FTS *, size_t));
5858f0484fSRodney W. Grimes static FTSENT	*fts_sort __P((FTS *, FTSENT *, int));
5958f0484fSRodney W. Grimes static u_short	 fts_stat __P((FTS *, FTSENT *, int));
609a91f1ccSWarner Losh static int	 fts_safe_changedir __P((FTS *, FTSENT *, int));
6158f0484fSRodney W. Grimes 
6251295a4dSJordan K. Hubbard #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
6358f0484fSRodney W. Grimes 
649a91f1ccSWarner Losh #define	CLR(opt)	(sp->fts_options &= ~(opt))
659a91f1ccSWarner Losh #define	ISSET(opt)	(sp->fts_options & (opt))
669a91f1ccSWarner Losh #define	SET(opt)	(sp->fts_options |= (opt))
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
6958f0484fSRodney W. Grimes #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
7058f0484fSRodney W. Grimes 
7158f0484fSRodney W. Grimes /* fts_build flags */
7258f0484fSRodney W. Grimes #define	BCHILD		1		/* fts_children */
7358f0484fSRodney W. Grimes #define	BNAMES		2		/* fts_children, names only */
7458f0484fSRodney W. Grimes #define	BREAD		3		/* fts_read */
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes FTS *
7758f0484fSRodney W. Grimes fts_open(argv, options, compar)
7858f0484fSRodney W. Grimes 	char * const *argv;
7958f0484fSRodney W. Grimes 	register int options;
809a91f1ccSWarner Losh 	int (*compar) __P((const FTSENT **, const FTSENT **));
8158f0484fSRodney W. Grimes {
8258f0484fSRodney W. Grimes 	register FTS *sp;
8358f0484fSRodney W. Grimes 	register FTSENT *p, *root;
8458f0484fSRodney W. Grimes 	register int nitems;
8558f0484fSRodney W. Grimes 	FTSENT *parent, *tmp;
8658f0484fSRodney W. Grimes 	int len;
8758f0484fSRodney W. Grimes 
8858f0484fSRodney W. Grimes 	/* Options check. */
8958f0484fSRodney W. Grimes 	if (options & ~FTS_OPTIONMASK) {
9058f0484fSRodney W. Grimes 		errno = EINVAL;
9158f0484fSRodney W. Grimes 		return (NULL);
9258f0484fSRodney W. Grimes 	}
9358f0484fSRodney W. Grimes 
9458f0484fSRodney W. Grimes 	/* Allocate/initialize the stream */
9558f0484fSRodney W. Grimes 	if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
9658f0484fSRodney W. Grimes 		return (NULL);
9758f0484fSRodney W. Grimes 	memset(sp, 0, sizeof(FTS));
9858f0484fSRodney W. Grimes 	sp->fts_compar = compar;
9958f0484fSRodney W. Grimes 	sp->fts_options = options;
10058f0484fSRodney W. Grimes 
10158f0484fSRodney W. Grimes 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
10258f0484fSRodney W. Grimes 	if (ISSET(FTS_LOGICAL))
10358f0484fSRodney W. Grimes 		SET(FTS_NOCHDIR);
10458f0484fSRodney W. Grimes 
10558f0484fSRodney W. Grimes 	/*
10658f0484fSRodney W. Grimes 	 * Start out with 1K of path space, and enough, in any case,
10758f0484fSRodney W. Grimes 	 * to hold the user's paths.
10858f0484fSRodney W. Grimes 	 */
10958f0484fSRodney W. Grimes 	if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
11058f0484fSRodney W. Grimes 		goto mem1;
11158f0484fSRodney W. Grimes 
11258f0484fSRodney W. Grimes 	/* Allocate/initialize root's parent. */
11358f0484fSRodney W. Grimes 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
11458f0484fSRodney W. Grimes 		goto mem2;
11558f0484fSRodney W. Grimes 	parent->fts_level = FTS_ROOTPARENTLEVEL;
11658f0484fSRodney W. Grimes 
11758f0484fSRodney W. Grimes 	/* Allocate/initialize root(s). */
11858f0484fSRodney W. Grimes 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
11958f0484fSRodney W. Grimes 		/* Don't allow zero-length paths. */
12058f0484fSRodney W. Grimes 		if ((len = strlen(*argv)) == 0) {
12158f0484fSRodney W. Grimes 			errno = ENOENT;
12258f0484fSRodney W. Grimes 			goto mem3;
12358f0484fSRodney W. Grimes 		}
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 		p = fts_alloc(sp, *argv, len);
12658f0484fSRodney W. Grimes 		p->fts_level = FTS_ROOTLEVEL;
12758f0484fSRodney W. Grimes 		p->fts_parent = parent;
12858f0484fSRodney W. Grimes 		p->fts_accpath = p->fts_name;
12958f0484fSRodney W. Grimes 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
13058f0484fSRodney W. Grimes 
13158f0484fSRodney W. Grimes 		/* Command-line "." and ".." are real directories. */
13258f0484fSRodney W. Grimes 		if (p->fts_info == FTS_DOT)
13358f0484fSRodney W. Grimes 			p->fts_info = FTS_D;
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes 		/*
13658f0484fSRodney W. Grimes 		 * If comparison routine supplied, traverse in sorted
13758f0484fSRodney W. Grimes 		 * order; otherwise traverse in the order specified.
13858f0484fSRodney W. Grimes 		 */
13958f0484fSRodney W. Grimes 		if (compar) {
14058f0484fSRodney W. Grimes 			p->fts_link = root;
14158f0484fSRodney W. Grimes 			root = p;
14258f0484fSRodney W. Grimes 		} else {
14358f0484fSRodney W. Grimes 			p->fts_link = NULL;
14458f0484fSRodney W. Grimes 			if (root == NULL)
14558f0484fSRodney W. Grimes 				tmp = root = p;
14658f0484fSRodney W. Grimes 			else {
14758f0484fSRodney W. Grimes 				tmp->fts_link = p;
14858f0484fSRodney W. Grimes 				tmp = p;
14958f0484fSRodney W. Grimes 			}
15058f0484fSRodney W. Grimes 		}
15158f0484fSRodney W. Grimes 	}
15258f0484fSRodney W. Grimes 	if (compar && nitems > 1)
15358f0484fSRodney W. Grimes 		root = fts_sort(sp, root, nitems);
15458f0484fSRodney W. Grimes 
15558f0484fSRodney W. Grimes 	/*
15658f0484fSRodney W. Grimes 	 * Allocate a dummy pointer and make fts_read think that we've just
15758f0484fSRodney W. Grimes 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
15858f0484fSRodney W. Grimes 	 * so that everything about the "current" node is ignored.
15958f0484fSRodney W. Grimes 	 */
16058f0484fSRodney W. Grimes 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
16158f0484fSRodney W. Grimes 		goto mem3;
16258f0484fSRodney W. Grimes 	sp->fts_cur->fts_link = root;
16358f0484fSRodney W. Grimes 	sp->fts_cur->fts_info = FTS_INIT;
16458f0484fSRodney W. Grimes 
16558f0484fSRodney W. Grimes 	/*
16658f0484fSRodney W. Grimes 	 * If using chdir(2), grab a file descriptor pointing to dot to insure
16758f0484fSRodney W. Grimes 	 * that we can get back here; this could be avoided for some paths,
16858f0484fSRodney W. Grimes 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
16958f0484fSRodney W. Grimes 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
17058f0484fSRodney W. Grimes 	 * descriptor we run anyway, just more slowly.
17158f0484fSRodney W. Grimes 	 */
17258f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
17358f0484fSRodney W. Grimes 		SET(FTS_NOCHDIR);
17458f0484fSRodney W. Grimes 
17558f0484fSRodney W. Grimes 	return (sp);
17658f0484fSRodney W. Grimes 
17758f0484fSRodney W. Grimes mem3:	fts_lfree(root);
17858f0484fSRodney W. Grimes 	free(parent);
17958f0484fSRodney W. Grimes mem2:	free(sp->fts_path);
18058f0484fSRodney W. Grimes mem1:	free(sp);
18158f0484fSRodney W. Grimes 	return (NULL);
18258f0484fSRodney W. Grimes }
18358f0484fSRodney W. Grimes 
18458f0484fSRodney W. Grimes static void
18558f0484fSRodney W. Grimes fts_load(sp, p)
18658f0484fSRodney W. Grimes 	FTS *sp;
18758f0484fSRodney W. Grimes 	register FTSENT *p;
18858f0484fSRodney W. Grimes {
18958f0484fSRodney W. Grimes 	register int len;
19058f0484fSRodney W. Grimes 	register char *cp;
19158f0484fSRodney W. Grimes 
19258f0484fSRodney W. Grimes 	/*
19358f0484fSRodney W. Grimes 	 * Load the stream structure for the next traversal.  Since we don't
19458f0484fSRodney W. Grimes 	 * actually enter the directory until after the preorder visit, set
19558f0484fSRodney W. Grimes 	 * the fts_accpath field specially so the chdir gets done to the right
19658f0484fSRodney W. Grimes 	 * place and the user can access the first node.  From fts_open it's
19758f0484fSRodney W. Grimes 	 * known that the path will fit.
19858f0484fSRodney W. Grimes 	 */
19958f0484fSRodney W. Grimes 	len = p->fts_pathlen = p->fts_namelen;
20058f0484fSRodney W. Grimes 	memmove(sp->fts_path, p->fts_name, len + 1);
20158f0484fSRodney W. Grimes 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
20258f0484fSRodney W. Grimes 		len = strlen(++cp);
20358f0484fSRodney W. Grimes 		memmove(p->fts_name, cp, len + 1);
20458f0484fSRodney W. Grimes 		p->fts_namelen = len;
20558f0484fSRodney W. Grimes 	}
20658f0484fSRodney W. Grimes 	p->fts_accpath = p->fts_path = sp->fts_path;
20758f0484fSRodney W. Grimes 	sp->fts_dev = p->fts_dev;
20858f0484fSRodney W. Grimes }
20958f0484fSRodney W. Grimes 
21058f0484fSRodney W. Grimes int
21158f0484fSRodney W. Grimes fts_close(sp)
21258f0484fSRodney W. Grimes 	FTS *sp;
21358f0484fSRodney W. Grimes {
21458f0484fSRodney W. Grimes 	register FTSENT *freep, *p;
21558f0484fSRodney W. Grimes 	int saved_errno;
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes 	/*
21858f0484fSRodney W. Grimes 	 * This still works if we haven't read anything -- the dummy structure
21958f0484fSRodney W. Grimes 	 * points to the root list, so we step through to the end of the root
22058f0484fSRodney W. Grimes 	 * list which has a valid parent pointer.
22158f0484fSRodney W. Grimes 	 */
22258f0484fSRodney W. Grimes 	if (sp->fts_cur) {
22358f0484fSRodney W. Grimes 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
22458f0484fSRodney W. Grimes 			freep = p;
22558f0484fSRodney W. Grimes 			p = p->fts_link ? p->fts_link : p->fts_parent;
22658f0484fSRodney W. Grimes 			free(freep);
22758f0484fSRodney W. Grimes 		}
22858f0484fSRodney W. Grimes 		free(p);
22958f0484fSRodney W. Grimes 	}
23058f0484fSRodney W. Grimes 
23158f0484fSRodney W. Grimes 	/* Free up child linked list, sort array, path buffer. */
23258f0484fSRodney W. Grimes 	if (sp->fts_child)
23358f0484fSRodney W. Grimes 		fts_lfree(sp->fts_child);
23458f0484fSRodney W. Grimes 	if (sp->fts_array)
23558f0484fSRodney W. Grimes 		free(sp->fts_array);
23658f0484fSRodney W. Grimes 	free(sp->fts_path);
23758f0484fSRodney W. Grimes 
23858f0484fSRodney W. Grimes 	/* Return to original directory, save errno if necessary. */
23958f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOCHDIR)) {
24058f0484fSRodney W. Grimes 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
24158f0484fSRodney W. Grimes 		(void)close(sp->fts_rfd);
24258f0484fSRodney W. Grimes 	}
24358f0484fSRodney W. Grimes 
24458f0484fSRodney W. Grimes 	/* Set errno and return. */
24558f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOCHDIR) && saved_errno) {
2468a507b98SPoul-Henning Kamp 		/* Free up the stream pointer. */
2478a507b98SPoul-Henning Kamp 		free(sp);
24858f0484fSRodney W. Grimes 		errno = saved_errno;
24958f0484fSRodney W. Grimes 		return (-1);
25058f0484fSRodney W. Grimes 	}
2518a507b98SPoul-Henning Kamp 	/* Free up the stream pointer. */
2528a507b98SPoul-Henning Kamp 	free(sp);
25358f0484fSRodney W. Grimes 	return (0);
25458f0484fSRodney W. Grimes }
25558f0484fSRodney W. Grimes 
25658f0484fSRodney W. Grimes /*
2579a91f1ccSWarner Losh  * Special case of "/" at the end of the path so that slashes aren't
2589a91f1ccSWarner Losh  * appended which would cause paths to be written as "....//foo".
25958f0484fSRodney W. Grimes  */
26058f0484fSRodney W. Grimes #define	NAPPEND(p)							\
2619a91f1ccSWarner Losh 	(p->fts_path[p->fts_pathlen - 1] == '/'				\
2629a91f1ccSWarner Losh 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
26358f0484fSRodney W. Grimes 
26458f0484fSRodney W. Grimes FTSENT *
26558f0484fSRodney W. Grimes fts_read(sp)
26658f0484fSRodney W. Grimes 	register FTS *sp;
26758f0484fSRodney W. Grimes {
26858f0484fSRodney W. Grimes 	register FTSENT *p, *tmp;
26958f0484fSRodney W. Grimes 	register int instr;
27058f0484fSRodney W. Grimes 	register char *t;
27158f0484fSRodney W. Grimes 	int saved_errno;
27258f0484fSRodney W. Grimes 
27358f0484fSRodney W. Grimes 	/* If finished or unrecoverable error, return NULL. */
27458f0484fSRodney W. Grimes 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
27558f0484fSRodney W. Grimes 		return (NULL);
27658f0484fSRodney W. Grimes 
27758f0484fSRodney W. Grimes 	/* Set current node pointer. */
27858f0484fSRodney W. Grimes 	p = sp->fts_cur;
27958f0484fSRodney W. Grimes 
28058f0484fSRodney W. Grimes 	/* Save and zero out user instructions. */
28158f0484fSRodney W. Grimes 	instr = p->fts_instr;
28258f0484fSRodney W. Grimes 	p->fts_instr = FTS_NOINSTR;
28358f0484fSRodney W. Grimes 
28458f0484fSRodney W. Grimes 	/* Any type of file may be re-visited; re-stat and re-turn. */
28558f0484fSRodney W. Grimes 	if (instr == FTS_AGAIN) {
28658f0484fSRodney W. Grimes 		p->fts_info = fts_stat(sp, p, 0);
28758f0484fSRodney W. Grimes 		return (p);
28858f0484fSRodney W. Grimes 	}
28958f0484fSRodney W. Grimes 
29058f0484fSRodney W. Grimes 	/*
29158f0484fSRodney W. Grimes 	 * Following a symlink -- SLNONE test allows application to see
29258f0484fSRodney W. Grimes 	 * SLNONE and recover.  If indirecting through a symlink, have
29358f0484fSRodney W. Grimes 	 * keep a pointer to current location.  If unable to get that
29458f0484fSRodney W. Grimes 	 * pointer, follow fails.
29558f0484fSRodney W. Grimes 	 */
29658f0484fSRodney W. Grimes 	if (instr == FTS_FOLLOW &&
29758f0484fSRodney W. Grimes 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
29858f0484fSRodney W. Grimes 		p->fts_info = fts_stat(sp, p, 1);
29958f0484fSRodney W. Grimes 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
30058f0484fSRodney W. Grimes 			if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
30158f0484fSRodney W. Grimes 				p->fts_errno = errno;
30258f0484fSRodney W. Grimes 				p->fts_info = FTS_ERR;
30358f0484fSRodney W. Grimes 			} else
30458f0484fSRodney W. Grimes 				p->fts_flags |= FTS_SYMFOLLOW;
30558f0484fSRodney W. Grimes 		return (p);
30658f0484fSRodney W. Grimes 	}
30758f0484fSRodney W. Grimes 
30858f0484fSRodney W. Grimes 	/* Directory in pre-order. */
30958f0484fSRodney W. Grimes 	if (p->fts_info == FTS_D) {
31058f0484fSRodney W. Grimes 		/* If skipped or crossed mount point, do post-order visit. */
31158f0484fSRodney W. Grimes 		if (instr == FTS_SKIP ||
31251295a4dSJordan K. Hubbard 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
31358f0484fSRodney W. Grimes 			if (p->fts_flags & FTS_SYMFOLLOW)
31458f0484fSRodney W. Grimes 				(void)close(p->fts_symfd);
31558f0484fSRodney W. Grimes 			if (sp->fts_child) {
31658f0484fSRodney W. Grimes 				fts_lfree(sp->fts_child);
31758f0484fSRodney W. Grimes 				sp->fts_child = NULL;
31858f0484fSRodney W. Grimes 			}
31958f0484fSRodney W. Grimes 			p->fts_info = FTS_DP;
32058f0484fSRodney W. Grimes 			return (p);
32158f0484fSRodney W. Grimes 		}
32258f0484fSRodney W. Grimes 
32358f0484fSRodney W. Grimes 		/* Rebuild if only read the names and now traversing. */
3249a91f1ccSWarner Losh 		if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
3259a91f1ccSWarner Losh 			CLR(FTS_NAMEONLY);
32658f0484fSRodney W. Grimes 			fts_lfree(sp->fts_child);
32758f0484fSRodney W. Grimes 			sp->fts_child = NULL;
32858f0484fSRodney W. Grimes 		}
32958f0484fSRodney W. Grimes 
33058f0484fSRodney W. Grimes 		/*
33158f0484fSRodney W. Grimes 		 * Cd to the subdirectory.
33258f0484fSRodney W. Grimes 		 *
33358f0484fSRodney W. Grimes 		 * If have already read and now fail to chdir, whack the list
33458f0484fSRodney W. Grimes 		 * to make the names come out right, and set the parent errno
33558f0484fSRodney W. Grimes 		 * so the application will eventually get an error condition.
33658f0484fSRodney W. Grimes 		 * Set the FTS_DONTCHDIR flag so that when we logically change
33758f0484fSRodney W. Grimes 		 * directories back to the parent we don't do a chdir.
33858f0484fSRodney W. Grimes 		 *
33958f0484fSRodney W. Grimes 		 * If haven't read do so.  If the read fails, fts_build sets
34058f0484fSRodney W. Grimes 		 * FTS_STOP or the fts_info field of the node.
34158f0484fSRodney W. Grimes 		 */
34258f0484fSRodney W. Grimes 		if (sp->fts_child) {
3439a91f1ccSWarner Losh 			if (fts_safe_changedir(sp, p, -1)) {
34458f0484fSRodney W. Grimes 				p->fts_errno = errno;
34558f0484fSRodney W. Grimes 				p->fts_flags |= FTS_DONTCHDIR;
34658f0484fSRodney W. Grimes 				for (p = sp->fts_child; p; p = p->fts_link)
34758f0484fSRodney W. Grimes 					p->fts_accpath =
34858f0484fSRodney W. Grimes 					    p->fts_parent->fts_accpath;
34958f0484fSRodney W. Grimes 			}
35058f0484fSRodney W. Grimes 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
35158f0484fSRodney W. Grimes 			if (ISSET(FTS_STOP))
35258f0484fSRodney W. Grimes 				return (NULL);
35358f0484fSRodney W. Grimes 			return (p);
35458f0484fSRodney W. Grimes 		}
35558f0484fSRodney W. Grimes 		p = sp->fts_child;
35658f0484fSRodney W. Grimes 		sp->fts_child = NULL;
35758f0484fSRodney W. Grimes 		goto name;
35858f0484fSRodney W. Grimes 	}
35958f0484fSRodney W. Grimes 
36058f0484fSRodney W. Grimes 	/* Move to the next node on this level. */
36158f0484fSRodney W. Grimes next:	tmp = p;
36251295a4dSJordan K. Hubbard 	if ((p = p->fts_link)) {
36358f0484fSRodney W. Grimes 		free(tmp);
36458f0484fSRodney W. Grimes 
36558f0484fSRodney W. Grimes 		/*
36658f0484fSRodney W. Grimes 		 * If reached the top, return to the original directory, and
36758f0484fSRodney W. Grimes 		 * load the paths for the next root.
36858f0484fSRodney W. Grimes 		 */
36958f0484fSRodney W. Grimes 		if (p->fts_level == FTS_ROOTLEVEL) {
3709a91f1ccSWarner Losh 			if (FCHDIR(sp, sp->fts_rfd)) {
37158f0484fSRodney W. Grimes 				SET(FTS_STOP);
37258f0484fSRodney W. Grimes 				return (NULL);
37358f0484fSRodney W. Grimes 			}
37458f0484fSRodney W. Grimes 			fts_load(sp, p);
37558f0484fSRodney W. Grimes 			return (sp->fts_cur = p);
37658f0484fSRodney W. Grimes 		}
37758f0484fSRodney W. Grimes 
37858f0484fSRodney W. Grimes 		/*
37958f0484fSRodney W. Grimes 		 * User may have called fts_set on the node.  If skipped,
38058f0484fSRodney W. Grimes 		 * ignore.  If followed, get a file descriptor so we can
38158f0484fSRodney W. Grimes 		 * get back if necessary.
38258f0484fSRodney W. Grimes 		 */
38358f0484fSRodney W. Grimes 		if (p->fts_instr == FTS_SKIP)
38458f0484fSRodney W. Grimes 			goto next;
38558f0484fSRodney W. Grimes 		if (p->fts_instr == FTS_FOLLOW) {
38658f0484fSRodney W. Grimes 			p->fts_info = fts_stat(sp, p, 1);
38758f0484fSRodney W. Grimes 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
38858f0484fSRodney W. Grimes 				if ((p->fts_symfd =
38958f0484fSRodney W. Grimes 				    open(".", O_RDONLY, 0)) < 0) {
39058f0484fSRodney W. Grimes 					p->fts_errno = errno;
39158f0484fSRodney W. Grimes 					p->fts_info = FTS_ERR;
39258f0484fSRodney W. Grimes 				} else
39358f0484fSRodney W. Grimes 					p->fts_flags |= FTS_SYMFOLLOW;
39458f0484fSRodney W. Grimes 			p->fts_instr = FTS_NOINSTR;
39558f0484fSRodney W. Grimes 		}
39658f0484fSRodney W. Grimes 
39758f0484fSRodney W. Grimes name:		t = sp->fts_path + NAPPEND(p->fts_parent);
39858f0484fSRodney W. Grimes 		*t++ = '/';
39958f0484fSRodney W. Grimes 		memmove(t, p->fts_name, p->fts_namelen + 1);
40058f0484fSRodney W. Grimes 		return (sp->fts_cur = p);
40158f0484fSRodney W. Grimes 	}
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 	/* Move up to the parent node. */
40458f0484fSRodney W. Grimes 	p = tmp->fts_parent;
40558f0484fSRodney W. Grimes 	free(tmp);
40658f0484fSRodney W. Grimes 
40758f0484fSRodney W. Grimes 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
40858f0484fSRodney W. Grimes 		/*
40958f0484fSRodney W. Grimes 		 * Done; free everything up and set errno to 0 so the user
41058f0484fSRodney W. Grimes 		 * can distinguish between error and EOF.
41158f0484fSRodney W. Grimes 		 */
41258f0484fSRodney W. Grimes 		free(p);
41358f0484fSRodney W. Grimes 		errno = 0;
41458f0484fSRodney W. Grimes 		return (sp->fts_cur = NULL);
41558f0484fSRodney W. Grimes 	}
41658f0484fSRodney W. Grimes 
41758f0484fSRodney W. Grimes 	/* Nul terminate the pathname. */
41858f0484fSRodney W. Grimes 	sp->fts_path[p->fts_pathlen] = '\0';
41958f0484fSRodney W. Grimes 
42058f0484fSRodney W. Grimes 	/*
42158f0484fSRodney W. Grimes 	 * Return to the parent directory.  If at a root node or came through
42258f0484fSRodney W. Grimes 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
42358f0484fSRodney W. Grimes 	 * one directory.
42458f0484fSRodney W. Grimes 	 */
42558f0484fSRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL) {
4269a91f1ccSWarner Losh 		if (FCHDIR(sp, sp->fts_rfd)) {
42758f0484fSRodney W. Grimes 			SET(FTS_STOP);
42858f0484fSRodney W. Grimes 			return (NULL);
42958f0484fSRodney W. Grimes 		}
43058f0484fSRodney W. Grimes 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
43158f0484fSRodney W. Grimes 		if (FCHDIR(sp, p->fts_symfd)) {
43258f0484fSRodney W. Grimes 			saved_errno = errno;
43358f0484fSRodney W. Grimes 			(void)close(p->fts_symfd);
43458f0484fSRodney W. Grimes 			errno = saved_errno;
43558f0484fSRodney W. Grimes 			SET(FTS_STOP);
43658f0484fSRodney W. Grimes 			return (NULL);
43758f0484fSRodney W. Grimes 		}
43858f0484fSRodney W. Grimes 		(void)close(p->fts_symfd);
43958f0484fSRodney W. Grimes 	} else if (!(p->fts_flags & FTS_DONTCHDIR)) {
44058f0484fSRodney W. Grimes 		if (CHDIR(sp, "..")) {
44158f0484fSRodney W. Grimes 			SET(FTS_STOP);
44258f0484fSRodney W. Grimes 			return (NULL);
44358f0484fSRodney W. Grimes 		}
44458f0484fSRodney W. Grimes 	}
44558f0484fSRodney W. Grimes 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
44658f0484fSRodney W. Grimes 	return (sp->fts_cur = p);
44758f0484fSRodney W. Grimes }
44858f0484fSRodney W. Grimes 
44958f0484fSRodney W. Grimes /*
45058f0484fSRodney W. Grimes  * Fts_set takes the stream as an argument although it's not used in this
45158f0484fSRodney W. Grimes  * implementation; it would be necessary if anyone wanted to add global
45258f0484fSRodney W. Grimes  * semantics to fts using fts_set.  An error return is allowed for similar
45358f0484fSRodney W. Grimes  * reasons.
45458f0484fSRodney W. Grimes  */
45558f0484fSRodney W. Grimes /* ARGSUSED */
45658f0484fSRodney W. Grimes int
45758f0484fSRodney W. Grimes fts_set(sp, p, instr)
45858f0484fSRodney W. Grimes 	FTS *sp;
45958f0484fSRodney W. Grimes 	FTSENT *p;
46058f0484fSRodney W. Grimes 	int instr;
46158f0484fSRodney W. Grimes {
46258f0484fSRodney W. Grimes 	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
46358f0484fSRodney W. Grimes 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
46458f0484fSRodney W. Grimes 		errno = EINVAL;
46558f0484fSRodney W. Grimes 		return (1);
46658f0484fSRodney W. Grimes 	}
46758f0484fSRodney W. Grimes 	p->fts_instr = instr;
46858f0484fSRodney W. Grimes 	return (0);
46958f0484fSRodney W. Grimes }
47058f0484fSRodney W. Grimes 
47158f0484fSRodney W. Grimes FTSENT *
47258f0484fSRodney W. Grimes fts_children(sp, instr)
47358f0484fSRodney W. Grimes 	register FTS *sp;
47458f0484fSRodney W. Grimes 	int instr;
47558f0484fSRodney W. Grimes {
47658f0484fSRodney W. Grimes 	register FTSENT *p;
47758f0484fSRodney W. Grimes 	int fd;
47858f0484fSRodney W. Grimes 
47958f0484fSRodney W. Grimes 	if (instr && instr != FTS_NAMEONLY) {
48058f0484fSRodney W. Grimes 		errno = EINVAL;
48158f0484fSRodney W. Grimes 		return (NULL);
48258f0484fSRodney W. Grimes 	}
48358f0484fSRodney W. Grimes 
48458f0484fSRodney W. Grimes 	/* Set current node pointer. */
48558f0484fSRodney W. Grimes 	p = sp->fts_cur;
48658f0484fSRodney W. Grimes 
48758f0484fSRodney W. Grimes 	/*
48858f0484fSRodney W. Grimes 	 * Errno set to 0 so user can distinguish empty directory from
48958f0484fSRodney W. Grimes 	 * an error.
49058f0484fSRodney W. Grimes 	 */
49158f0484fSRodney W. Grimes 	errno = 0;
49258f0484fSRodney W. Grimes 
49358f0484fSRodney W. Grimes 	/* Fatal errors stop here. */
49458f0484fSRodney W. Grimes 	if (ISSET(FTS_STOP))
49558f0484fSRodney W. Grimes 		return (NULL);
49658f0484fSRodney W. Grimes 
49758f0484fSRodney W. Grimes 	/* Return logical hierarchy of user's arguments. */
49858f0484fSRodney W. Grimes 	if (p->fts_info == FTS_INIT)
49958f0484fSRodney W. Grimes 		return (p->fts_link);
50058f0484fSRodney W. Grimes 
50158f0484fSRodney W. Grimes 	/*
50258f0484fSRodney W. Grimes 	 * If not a directory being visited in pre-order, stop here.  Could
50358f0484fSRodney W. Grimes 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
50458f0484fSRodney W. Grimes 	 * same effect is available with FTS_AGAIN.
50558f0484fSRodney W. Grimes 	 */
50658f0484fSRodney W. Grimes 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
50758f0484fSRodney W. Grimes 		return (NULL);
50858f0484fSRodney W. Grimes 
50958f0484fSRodney W. Grimes 	/* Free up any previous child list. */
51058f0484fSRodney W. Grimes 	if (sp->fts_child)
51158f0484fSRodney W. Grimes 		fts_lfree(sp->fts_child);
51258f0484fSRodney W. Grimes 
51358f0484fSRodney W. Grimes 	if (instr == FTS_NAMEONLY) {
5149a91f1ccSWarner Losh 		SET(FTS_NAMEONLY);
51558f0484fSRodney W. Grimes 		instr = BNAMES;
51658f0484fSRodney W. Grimes 	} else
51758f0484fSRodney W. Grimes 		instr = BCHILD;
51858f0484fSRodney W. Grimes 
51958f0484fSRodney W. Grimes 	/*
52058f0484fSRodney W. Grimes 	 * If using chdir on a relative path and called BEFORE fts_read does
52158f0484fSRodney W. Grimes 	 * its chdir to the root of a traversal, we can lose -- we need to
52258f0484fSRodney W. Grimes 	 * chdir into the subdirectory, and we don't know where the current
52358f0484fSRodney W. Grimes 	 * directory is, so we can't get back so that the upcoming chdir by
52458f0484fSRodney W. Grimes 	 * fts_read will work.
52558f0484fSRodney W. Grimes 	 */
52658f0484fSRodney W. Grimes 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
52758f0484fSRodney W. Grimes 	    ISSET(FTS_NOCHDIR))
52858f0484fSRodney W. Grimes 		return (sp->fts_child = fts_build(sp, instr));
52958f0484fSRodney W. Grimes 
53058f0484fSRodney W. Grimes 	if ((fd = open(".", O_RDONLY, 0)) < 0)
53158f0484fSRodney W. Grimes 		return (NULL);
53258f0484fSRodney W. Grimes 	sp->fts_child = fts_build(sp, instr);
53358f0484fSRodney W. Grimes 	if (fchdir(fd))
53458f0484fSRodney W. Grimes 		return (NULL);
53558f0484fSRodney W. Grimes 	(void)close(fd);
53658f0484fSRodney W. Grimes 	return (sp->fts_child);
53758f0484fSRodney W. Grimes }
53858f0484fSRodney W. Grimes 
53958f0484fSRodney W. Grimes /*
54058f0484fSRodney W. Grimes  * This is the tricky part -- do not casually change *anything* in here.  The
54158f0484fSRodney W. Grimes  * idea is to build the linked list of entries that are used by fts_children
54258f0484fSRodney W. Grimes  * and fts_read.  There are lots of special cases.
54358f0484fSRodney W. Grimes  *
54458f0484fSRodney W. Grimes  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
54558f0484fSRodney W. Grimes  * set and it's a physical walk (so that symbolic links can't be directories),
54658f0484fSRodney W. Grimes  * we can do things quickly.  First, if it's a 4.4BSD file system, the type
54758f0484fSRodney W. Grimes  * of the file is in the directory entry.  Otherwise, we assume that the number
54858f0484fSRodney W. Grimes  * of subdirectories in a node is equal to the number of links to the parent.
54958f0484fSRodney W. Grimes  * The former skips all stat calls.  The latter skips stat calls in any leaf
55058f0484fSRodney W. Grimes  * directories and for any files after the subdirectories in the directory have
55158f0484fSRodney W. Grimes  * been found, cutting the stat calls by about 2/3.
55258f0484fSRodney W. Grimes  */
55358f0484fSRodney W. Grimes static FTSENT *
55458f0484fSRodney W. Grimes fts_build(sp, type)
55558f0484fSRodney W. Grimes 	register FTS *sp;
55658f0484fSRodney W. Grimes 	int type;
55758f0484fSRodney W. Grimes {
55858f0484fSRodney W. Grimes 	register struct dirent *dp;
55958f0484fSRodney W. Grimes 	register FTSENT *p, *head;
56058f0484fSRodney W. Grimes 	register int nitems;
56158f0484fSRodney W. Grimes 	FTSENT *cur, *tail;
56258f0484fSRodney W. Grimes 	DIR *dirp;
56358f0484fSRodney W. Grimes 	void *adjaddr;
564adf6ad9eSPeter Wemm 	int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno;
56558f0484fSRodney W. Grimes 	char *cp;
56658f0484fSRodney W. Grimes 
56758f0484fSRodney W. Grimes 	/* Set current node pointer. */
56858f0484fSRodney W. Grimes 	cur = sp->fts_cur;
56958f0484fSRodney W. Grimes 
57058f0484fSRodney W. Grimes 	/*
57158f0484fSRodney W. Grimes 	 * Open the directory for reading.  If this fails, we're done.
57258f0484fSRodney W. Grimes 	 * If being called from fts_read, set the fts_info field.
57358f0484fSRodney W. Grimes 	 */
574adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
575adf6ad9eSPeter Wemm 	if (ISSET(FTS_WHITEOUT))
576adf6ad9eSPeter Wemm 		oflag = DTF_NODUP|DTF_REWIND;
577adf6ad9eSPeter Wemm 	else
578adf6ad9eSPeter Wemm 		oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
579adf6ad9eSPeter Wemm #else
580adf6ad9eSPeter Wemm #define __opendir2(path, flag) opendir(path)
581adf6ad9eSPeter Wemm #endif
582adf6ad9eSPeter Wemm 	if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
58358f0484fSRodney W. Grimes 		if (type == BREAD) {
58458f0484fSRodney W. Grimes 			cur->fts_info = FTS_DNR;
58558f0484fSRodney W. Grimes 			cur->fts_errno = errno;
58658f0484fSRodney W. Grimes 		}
58758f0484fSRodney W. Grimes 		return (NULL);
58858f0484fSRodney W. Grimes 	}
58958f0484fSRodney W. Grimes 
59058f0484fSRodney W. Grimes 	/*
59158f0484fSRodney W. Grimes 	 * Nlinks is the number of possible entries of type directory in the
59258f0484fSRodney W. Grimes 	 * directory if we're cheating on stat calls, 0 if we're not doing
59358f0484fSRodney W. Grimes 	 * any stat calls at all, -1 if we're doing stats on everything.
59458f0484fSRodney W. Grimes 	 */
59558f0484fSRodney W. Grimes 	if (type == BNAMES)
59658f0484fSRodney W. Grimes 		nlinks = 0;
59758f0484fSRodney W. Grimes 	else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
59858f0484fSRodney W. Grimes 		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
59958f0484fSRodney W. Grimes 	else
60058f0484fSRodney W. Grimes 		nlinks = -1;
60158f0484fSRodney W. Grimes 
60258f0484fSRodney W. Grimes #ifdef notdef
60358f0484fSRodney W. Grimes 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
60458f0484fSRodney W. Grimes 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
60558f0484fSRodney W. Grimes 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
60658f0484fSRodney W. Grimes #endif
60758f0484fSRodney W. Grimes 	/*
60858f0484fSRodney W. Grimes 	 * If we're going to need to stat anything or we want to descend
60958f0484fSRodney W. Grimes 	 * and stay in the directory, chdir.  If this fails we keep going,
61058f0484fSRodney W. Grimes 	 * but set a flag so we don't chdir after the post-order visit.
61158f0484fSRodney W. Grimes 	 * We won't be able to stat anything, but we can still return the
61258f0484fSRodney W. Grimes 	 * names themselves.  Note, that since fts_read won't be able to
61358f0484fSRodney W. Grimes 	 * chdir into the directory, it will have to return different path
61458f0484fSRodney W. Grimes 	 * names than before, i.e. "a/b" instead of "b".  Since the node
61558f0484fSRodney W. Grimes 	 * has already been visited in pre-order, have to wait until the
61658f0484fSRodney W. Grimes 	 * post-order visit to return the error.  There is a special case
61758f0484fSRodney W. Grimes 	 * here, if there was nothing to stat then it's not an error to
61858f0484fSRodney W. Grimes 	 * not be able to stat.  This is all fairly nasty.  If a program
61958f0484fSRodney W. Grimes 	 * needed sorted entries or stat information, they had better be
62058f0484fSRodney W. Grimes 	 * checking FTS_NS on the returned nodes.
62158f0484fSRodney W. Grimes 	 */
62258f0484fSRodney W. Grimes 	cderrno = 0;
62358f0484fSRodney W. Grimes 	if (nlinks || type == BREAD)
6249a91f1ccSWarner Losh 		if (fts_safe_changedir(sp, cur, dirfd(dirp))) {
62558f0484fSRodney W. Grimes 			if (nlinks && type == BREAD)
62658f0484fSRodney W. Grimes 				cur->fts_errno = errno;
62758f0484fSRodney W. Grimes 			cur->fts_flags |= FTS_DONTCHDIR;
62858f0484fSRodney W. Grimes 			descend = 0;
62958f0484fSRodney W. Grimes 			cderrno = errno;
6309a91f1ccSWarner Losh 			(void)closedir(dirp);
6319a91f1ccSWarner Losh 			dirp = NULL;
63258f0484fSRodney W. Grimes 		} else
63358f0484fSRodney W. Grimes 			descend = 1;
63458f0484fSRodney W. Grimes 	else
63558f0484fSRodney W. Grimes 		descend = 0;
63658f0484fSRodney W. Grimes 
63758f0484fSRodney W. Grimes 	/*
63858f0484fSRodney W. Grimes 	 * Figure out the max file name length that can be stored in the
63958f0484fSRodney W. Grimes 	 * current path -- the inner loop allocates more path as necessary.
64058f0484fSRodney W. Grimes 	 * We really wouldn't have to do the maxlen calculations here, we
64158f0484fSRodney W. Grimes 	 * could do them in fts_read before returning the path, but it's a
64258f0484fSRodney W. Grimes 	 * lot easier here since the length is part of the dirent structure.
64358f0484fSRodney W. Grimes 	 *
64458f0484fSRodney W. Grimes 	 * If not changing directories set a pointer so that can just append
64558f0484fSRodney W. Grimes 	 * each new name into the path.
64658f0484fSRodney W. Grimes 	 */
64758f0484fSRodney W. Grimes 	maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
64858f0484fSRodney W. Grimes 	len = NAPPEND(cur);
64958f0484fSRodney W. Grimes 	if (ISSET(FTS_NOCHDIR)) {
65058f0484fSRodney W. Grimes 		cp = sp->fts_path + len;
65158f0484fSRodney W. Grimes 		*cp++ = '/';
65258f0484fSRodney W. Grimes 	}
65358f0484fSRodney W. Grimes 
65458f0484fSRodney W. Grimes 	level = cur->fts_level + 1;
65558f0484fSRodney W. Grimes 
65658f0484fSRodney W. Grimes 	/* Read the directory, attaching each entry to the `link' pointer. */
65758f0484fSRodney W. Grimes 	adjaddr = NULL;
6589a91f1ccSWarner Losh 	for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
65958f0484fSRodney W. Grimes 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
66058f0484fSRodney W. Grimes 			continue;
66158f0484fSRodney W. Grimes 
66258f0484fSRodney W. Grimes 		if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
66358f0484fSRodney W. Grimes 			goto mem1;
66458f0484fSRodney W. Grimes 		if (dp->d_namlen > maxlen) {
66558f0484fSRodney W. Grimes 			if (fts_palloc(sp, (size_t)dp->d_namlen)) {
66658f0484fSRodney W. Grimes 				/*
66758f0484fSRodney W. Grimes 				 * No more memory for path or structures.  Save
66858f0484fSRodney W. Grimes 				 * errno, free up the current structure and the
66958f0484fSRodney W. Grimes 				 * structures already allocated.
67058f0484fSRodney W. Grimes 				 */
67158f0484fSRodney W. Grimes mem1:				saved_errno = errno;
67258f0484fSRodney W. Grimes 				if (p)
67358f0484fSRodney W. Grimes 					free(p);
67458f0484fSRodney W. Grimes 				fts_lfree(head);
67558f0484fSRodney W. Grimes 				(void)closedir(dirp);
67658f0484fSRodney W. Grimes 				errno = saved_errno;
67758f0484fSRodney W. Grimes 				cur->fts_info = FTS_ERR;
67858f0484fSRodney W. Grimes 				SET(FTS_STOP);
67958f0484fSRodney W. Grimes 				return (NULL);
68058f0484fSRodney W. Grimes 			}
68158f0484fSRodney W. Grimes 			adjaddr = sp->fts_path;
68258f0484fSRodney W. Grimes 			maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
68358f0484fSRodney W. Grimes 		}
68458f0484fSRodney W. Grimes 
68558f0484fSRodney W. Grimes 		p->fts_pathlen = len + dp->d_namlen + 1;
68658f0484fSRodney W. Grimes 		p->fts_parent = sp->fts_cur;
68758f0484fSRodney W. Grimes 		p->fts_level = level;
68858f0484fSRodney W. Grimes 
689adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
690adf6ad9eSPeter Wemm 		if (dp->d_type == DT_WHT)
691adf6ad9eSPeter Wemm 			p->fts_flags |= FTS_ISW;
692adf6ad9eSPeter Wemm #endif
693adf6ad9eSPeter Wemm 
69458f0484fSRodney W. Grimes 		if (cderrno) {
69558f0484fSRodney W. Grimes 			if (nlinks) {
69658f0484fSRodney W. Grimes 				p->fts_info = FTS_NS;
69758f0484fSRodney W. Grimes 				p->fts_errno = cderrno;
69858f0484fSRodney W. Grimes 			} else
69958f0484fSRodney W. Grimes 				p->fts_info = FTS_NSOK;
70058f0484fSRodney W. Grimes 			p->fts_accpath = cur->fts_accpath;
70158f0484fSRodney W. Grimes 		} else if (nlinks == 0
70258f0484fSRodney W. Grimes #ifdef DT_DIR
70351295a4dSJordan K. Hubbard 		    || (nlinks > 0 &&
70451295a4dSJordan K. Hubbard 		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
70558f0484fSRodney W. Grimes #endif
70658f0484fSRodney W. Grimes 		    ) {
70758f0484fSRodney W. Grimes 			p->fts_accpath =
70858f0484fSRodney W. Grimes 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
70958f0484fSRodney W. Grimes 			p->fts_info = FTS_NSOK;
71058f0484fSRodney W. Grimes 		} else {
71158f0484fSRodney W. Grimes 			/* Build a file name for fts_stat to stat. */
71258f0484fSRodney W. Grimes 			if (ISSET(FTS_NOCHDIR)) {
71358f0484fSRodney W. Grimes 				p->fts_accpath = p->fts_path;
71458f0484fSRodney W. Grimes 				memmove(cp, p->fts_name, p->fts_namelen + 1);
71558f0484fSRodney W. Grimes 			} else
71658f0484fSRodney W. Grimes 				p->fts_accpath = p->fts_name;
71758f0484fSRodney W. Grimes 			/* Stat it. */
71858f0484fSRodney W. Grimes 			p->fts_info = fts_stat(sp, p, 0);
71958f0484fSRodney W. Grimes 
72058f0484fSRodney W. Grimes 			/* Decrement link count if applicable. */
72158f0484fSRodney W. Grimes 			if (nlinks > 0 && (p->fts_info == FTS_D ||
72258f0484fSRodney W. Grimes 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
72358f0484fSRodney W. Grimes 				--nlinks;
72458f0484fSRodney W. Grimes 		}
72558f0484fSRodney W. Grimes 
72658f0484fSRodney W. Grimes 		/* We walk in directory order so "ls -f" doesn't get upset. */
72758f0484fSRodney W. Grimes 		p->fts_link = NULL;
72858f0484fSRodney W. Grimes 		if (head == NULL)
72958f0484fSRodney W. Grimes 			head = tail = p;
73058f0484fSRodney W. Grimes 		else {
73158f0484fSRodney W. Grimes 			tail->fts_link = p;
73258f0484fSRodney W. Grimes 			tail = p;
73358f0484fSRodney W. Grimes 		}
73458f0484fSRodney W. Grimes 		++nitems;
73558f0484fSRodney W. Grimes 	}
7369a91f1ccSWarner Losh 	if (dirp)
73758f0484fSRodney W. Grimes 		(void)closedir(dirp);
73858f0484fSRodney W. Grimes 
73958f0484fSRodney W. Grimes 	/*
74058f0484fSRodney W. Grimes 	 * If had to realloc the path, adjust the addresses for the rest
74158f0484fSRodney W. Grimes 	 * of the tree.
74258f0484fSRodney W. Grimes 	 */
74358f0484fSRodney W. Grimes 	if (adjaddr)
74458f0484fSRodney W. Grimes 		fts_padjust(sp, adjaddr);
74558f0484fSRodney W. Grimes 
74658f0484fSRodney W. Grimes 	/*
74758f0484fSRodney W. Grimes 	 * If not changing directories, reset the path back to original
74858f0484fSRodney W. Grimes 	 * state.
74958f0484fSRodney W. Grimes 	 */
75058f0484fSRodney W. Grimes 	if (ISSET(FTS_NOCHDIR)) {
75142396e05SPeter Wemm 		if (len == sp->fts_pathlen)
75258f0484fSRodney W. Grimes 			--cp;
75358f0484fSRodney W. Grimes 		*cp = '\0';
75458f0484fSRodney W. Grimes 	}
75558f0484fSRodney W. Grimes 
75658f0484fSRodney W. Grimes 	/*
75758f0484fSRodney W. Grimes 	 * If descended after called from fts_children or after called from
75858f0484fSRodney W. Grimes 	 * fts_read and nothing found, get back.  At the root level we use
75958f0484fSRodney W. Grimes 	 * the saved fd; if one of fts_open()'s arguments is a relative path
76058f0484fSRodney W. Grimes 	 * to an empty directory, we wind up here with no other way back.  If
76158f0484fSRodney W. Grimes 	 * can't get back, we're done.
76258f0484fSRodney W. Grimes 	 */
76358f0484fSRodney W. Grimes 	if (descend && (type == BCHILD || !nitems) &&
76458f0484fSRodney W. Grimes 	    (cur->fts_level == FTS_ROOTLEVEL ?
76558f0484fSRodney W. Grimes 	    FCHDIR(sp, sp->fts_rfd) : CHDIR(sp, ".."))) {
76658f0484fSRodney W. Grimes 		cur->fts_info = FTS_ERR;
76758f0484fSRodney W. Grimes 		SET(FTS_STOP);
76858f0484fSRodney W. Grimes 		return (NULL);
76958f0484fSRodney W. Grimes 	}
77058f0484fSRodney W. Grimes 
77158f0484fSRodney W. Grimes 	/* If didn't find anything, return NULL. */
77258f0484fSRodney W. Grimes 	if (!nitems) {
77358f0484fSRodney W. Grimes 		if (type == BREAD)
77458f0484fSRodney W. Grimes 			cur->fts_info = FTS_DP;
77558f0484fSRodney W. Grimes 		return (NULL);
77658f0484fSRodney W. Grimes 	}
77758f0484fSRodney W. Grimes 
77858f0484fSRodney W. Grimes 	/* Sort the entries. */
77958f0484fSRodney W. Grimes 	if (sp->fts_compar && nitems > 1)
78058f0484fSRodney W. Grimes 		head = fts_sort(sp, head, nitems);
78158f0484fSRodney W. Grimes 	return (head);
78258f0484fSRodney W. Grimes }
78358f0484fSRodney W. Grimes 
78458f0484fSRodney W. Grimes static u_short
78558f0484fSRodney W. Grimes fts_stat(sp, p, follow)
78658f0484fSRodney W. Grimes 	FTS *sp;
78758f0484fSRodney W. Grimes 	register FTSENT *p;
78858f0484fSRodney W. Grimes 	int follow;
78958f0484fSRodney W. Grimes {
79058f0484fSRodney W. Grimes 	register FTSENT *t;
79158f0484fSRodney W. Grimes 	register dev_t dev;
79258f0484fSRodney W. Grimes 	register ino_t ino;
79358f0484fSRodney W. Grimes 	struct stat *sbp, sb;
79458f0484fSRodney W. Grimes 	int saved_errno;
79558f0484fSRodney W. Grimes 
79658f0484fSRodney W. Grimes 	/* If user needs stat info, stat buffer already allocated. */
79758f0484fSRodney W. Grimes 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
79858f0484fSRodney W. Grimes 
799adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
800adf6ad9eSPeter Wemm 	/* check for whiteout */
801adf6ad9eSPeter Wemm 	if (p->fts_flags & FTS_ISW) {
802adf6ad9eSPeter Wemm 		if (sbp != &sb) {
803adf6ad9eSPeter Wemm 			memset(sbp, '\0', sizeof (*sbp));
804adf6ad9eSPeter Wemm 			sbp->st_mode = S_IFWHT;
805adf6ad9eSPeter Wemm 		}
806adf6ad9eSPeter Wemm 		return (FTS_W);
807adf6ad9eSPeter Wemm 	}
808adf6ad9eSPeter Wemm #endif
809adf6ad9eSPeter Wemm 
81058f0484fSRodney W. Grimes 	/*
81158f0484fSRodney W. Grimes 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
81258f0484fSRodney W. Grimes 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
81358f0484fSRodney W. Grimes 	 * fail, set the errno from the stat call.
81458f0484fSRodney W. Grimes 	 */
81558f0484fSRodney W. Grimes 	if (ISSET(FTS_LOGICAL) || follow) {
81658f0484fSRodney W. Grimes 		if (stat(p->fts_accpath, sbp)) {
81758f0484fSRodney W. Grimes 			saved_errno = errno;
81858f0484fSRodney W. Grimes 			if (!lstat(p->fts_accpath, sbp)) {
81958f0484fSRodney W. Grimes 				errno = 0;
82058f0484fSRodney W. Grimes 				return (FTS_SLNONE);
82158f0484fSRodney W. Grimes 			}
82258f0484fSRodney W. Grimes 			p->fts_errno = saved_errno;
82358f0484fSRodney W. Grimes 			goto err;
82458f0484fSRodney W. Grimes 		}
82558f0484fSRodney W. Grimes 	} else if (lstat(p->fts_accpath, sbp)) {
82658f0484fSRodney W. Grimes 		p->fts_errno = errno;
82758f0484fSRodney W. Grimes err:		memset(sbp, 0, sizeof(struct stat));
82858f0484fSRodney W. Grimes 		return (FTS_NS);
82958f0484fSRodney W. Grimes 	}
83058f0484fSRodney W. Grimes 
83158f0484fSRodney W. Grimes 	if (S_ISDIR(sbp->st_mode)) {
83258f0484fSRodney W. Grimes 		/*
83358f0484fSRodney W. Grimes 		 * Set the device/inode.  Used to find cycles and check for
83458f0484fSRodney W. Grimes 		 * crossing mount points.  Also remember the link count, used
83558f0484fSRodney W. Grimes 		 * in fts_build to limit the number of stat calls.  It is
83658f0484fSRodney W. Grimes 		 * understood that these fields are only referenced if fts_info
83758f0484fSRodney W. Grimes 		 * is set to FTS_D.
83858f0484fSRodney W. Grimes 		 */
83958f0484fSRodney W. Grimes 		dev = p->fts_dev = sbp->st_dev;
84058f0484fSRodney W. Grimes 		ino = p->fts_ino = sbp->st_ino;
84158f0484fSRodney W. Grimes 		p->fts_nlink = sbp->st_nlink;
84258f0484fSRodney W. Grimes 
84358f0484fSRodney W. Grimes 		if (ISDOT(p->fts_name))
84458f0484fSRodney W. Grimes 			return (FTS_DOT);
84558f0484fSRodney W. Grimes 
84658f0484fSRodney W. Grimes 		/*
84758f0484fSRodney W. Grimes 		 * Cycle detection is done by brute force when the directory
84858f0484fSRodney W. Grimes 		 * is first encountered.  If the tree gets deep enough or the
84958f0484fSRodney W. Grimes 		 * number of symbolic links to directories is high enough,
85058f0484fSRodney W. Grimes 		 * something faster might be worthwhile.
85158f0484fSRodney W. Grimes 		 */
85258f0484fSRodney W. Grimes 		for (t = p->fts_parent;
85358f0484fSRodney W. Grimes 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
85458f0484fSRodney W. Grimes 			if (ino == t->fts_ino && dev == t->fts_dev) {
85558f0484fSRodney W. Grimes 				p->fts_cycle = t;
85658f0484fSRodney W. Grimes 				return (FTS_DC);
85758f0484fSRodney W. Grimes 			}
85858f0484fSRodney W. Grimes 		return (FTS_D);
85958f0484fSRodney W. Grimes 	}
86058f0484fSRodney W. Grimes 	if (S_ISLNK(sbp->st_mode))
86158f0484fSRodney W. Grimes 		return (FTS_SL);
86258f0484fSRodney W. Grimes 	if (S_ISREG(sbp->st_mode))
86358f0484fSRodney W. Grimes 		return (FTS_F);
86458f0484fSRodney W. Grimes 	return (FTS_DEFAULT);
86558f0484fSRodney W. Grimes }
86658f0484fSRodney W. Grimes 
86758f0484fSRodney W. Grimes static FTSENT *
86858f0484fSRodney W. Grimes fts_sort(sp, head, nitems)
86958f0484fSRodney W. Grimes 	FTS *sp;
87058f0484fSRodney W. Grimes 	FTSENT *head;
87158f0484fSRodney W. Grimes 	register int nitems;
87258f0484fSRodney W. Grimes {
87358f0484fSRodney W. Grimes 	register FTSENT **ap, *p;
87458f0484fSRodney W. Grimes 
87558f0484fSRodney W. Grimes 	/*
87658f0484fSRodney W. Grimes 	 * Construct an array of pointers to the structures and call qsort(3).
87758f0484fSRodney W. Grimes 	 * Reassemble the array in the order returned by qsort.  If unable to
87858f0484fSRodney W. Grimes 	 * sort for memory reasons, return the directory entries in their
87958f0484fSRodney W. Grimes 	 * current order.  Allocate enough space for the current needs plus
88058f0484fSRodney W. Grimes 	 * 40 so don't realloc one entry at a time.
88158f0484fSRodney W. Grimes 	 */
88258f0484fSRodney W. Grimes 	if (nitems > sp->fts_nitems) {
88358f0484fSRodney W. Grimes 		sp->fts_nitems = nitems + 40;
88458f0484fSRodney W. Grimes 		if ((sp->fts_array = realloc(sp->fts_array,
88558f0484fSRodney W. Grimes 		    (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
88658f0484fSRodney W. Grimes 			sp->fts_nitems = 0;
88758f0484fSRodney W. Grimes 			return (head);
88858f0484fSRodney W. Grimes 		}
88958f0484fSRodney W. Grimes 	}
89058f0484fSRodney W. Grimes 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
89158f0484fSRodney W. Grimes 		*ap++ = p;
89258f0484fSRodney W. Grimes 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
89358f0484fSRodney W. Grimes 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
89458f0484fSRodney W. Grimes 		ap[0]->fts_link = ap[1];
89558f0484fSRodney W. Grimes 	ap[0]->fts_link = NULL;
89658f0484fSRodney W. Grimes 	return (head);
89758f0484fSRodney W. Grimes }
89858f0484fSRodney W. Grimes 
89958f0484fSRodney W. Grimes static FTSENT *
90058f0484fSRodney W. Grimes fts_alloc(sp, name, namelen)
90158f0484fSRodney W. Grimes 	FTS *sp;
90258f0484fSRodney W. Grimes 	char *name;
90358f0484fSRodney W. Grimes 	register int namelen;
90458f0484fSRodney W. Grimes {
90558f0484fSRodney W. Grimes 	register FTSENT *p;
90658f0484fSRodney W. Grimes 	size_t len;
90758f0484fSRodney W. Grimes 
90858f0484fSRodney W. Grimes 	/*
90958f0484fSRodney W. Grimes 	 * The file name is a variable length array and no stat structure is
91058f0484fSRodney W. Grimes 	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
91158f0484fSRodney W. Grimes 	 * structure, the file name and the stat structure in one chunk, but
91258f0484fSRodney W. Grimes 	 * be careful that the stat structure is reasonably aligned.  Since the
91358f0484fSRodney W. Grimes 	 * fts_name field is declared to be of size 1, the fts_name pointer is
91458f0484fSRodney W. Grimes 	 * namelen + 2 before the first possible address of the stat structure.
91558f0484fSRodney W. Grimes 	 */
91658f0484fSRodney W. Grimes 	len = sizeof(FTSENT) + namelen;
91758f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOSTAT))
91858f0484fSRodney W. Grimes 		len += sizeof(struct stat) + ALIGNBYTES;
91958f0484fSRodney W. Grimes 	if ((p = malloc(len)) == NULL)
92058f0484fSRodney W. Grimes 		return (NULL);
92158f0484fSRodney W. Grimes 
92258f0484fSRodney W. Grimes 	/* Copy the name plus the trailing NULL. */
92358f0484fSRodney W. Grimes 	memmove(p->fts_name, name, namelen + 1);
92458f0484fSRodney W. Grimes 
92558f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOSTAT))
92658f0484fSRodney W. Grimes 		p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
92758f0484fSRodney W. Grimes 	p->fts_namelen = namelen;
92858f0484fSRodney W. Grimes 	p->fts_path = sp->fts_path;
92958f0484fSRodney W. Grimes 	p->fts_errno = 0;
93058f0484fSRodney W. Grimes 	p->fts_flags = 0;
93158f0484fSRodney W. Grimes 	p->fts_instr = FTS_NOINSTR;
93258f0484fSRodney W. Grimes 	p->fts_number = 0;
93358f0484fSRodney W. Grimes 	p->fts_pointer = NULL;
93458f0484fSRodney W. Grimes 	return (p);
93558f0484fSRodney W. Grimes }
93658f0484fSRodney W. Grimes 
93758f0484fSRodney W. Grimes static void
93858f0484fSRodney W. Grimes fts_lfree(head)
93958f0484fSRodney W. Grimes 	register FTSENT *head;
94058f0484fSRodney W. Grimes {
94158f0484fSRodney W. Grimes 	register FTSENT *p;
94258f0484fSRodney W. Grimes 
94358f0484fSRodney W. Grimes 	/* Free a linked list of structures. */
94451295a4dSJordan K. Hubbard 	while ((p = head)) {
94558f0484fSRodney W. Grimes 		head = head->fts_link;
94658f0484fSRodney W. Grimes 		free(p);
94758f0484fSRodney W. Grimes 	}
94858f0484fSRodney W. Grimes }
94958f0484fSRodney W. Grimes 
95058f0484fSRodney W. Grimes /*
95158f0484fSRodney W. Grimes  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
95258f0484fSRodney W. Grimes  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
95358f0484fSRodney W. Grimes  * though the kernel won't resolve them.  Add the size (not just what's needed)
95458f0484fSRodney W. Grimes  * plus 256 bytes so don't realloc the path 2 bytes at a time.
95558f0484fSRodney W. Grimes  */
95658f0484fSRodney W. Grimes static int
95758f0484fSRodney W. Grimes fts_palloc(sp, more)
95858f0484fSRodney W. Grimes 	FTS *sp;
95958f0484fSRodney W. Grimes 	size_t more;
96058f0484fSRodney W. Grimes {
96158f0484fSRodney W. Grimes 	sp->fts_pathlen += more + 256;
96258f0484fSRodney W. Grimes 	sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
96358f0484fSRodney W. Grimes 	return (sp->fts_path == NULL);
96458f0484fSRodney W. Grimes }
96558f0484fSRodney W. Grimes 
96658f0484fSRodney W. Grimes /*
96758f0484fSRodney W. Grimes  * When the path is realloc'd, have to fix all of the pointers in structures
96858f0484fSRodney W. Grimes  * already returned.
96958f0484fSRodney W. Grimes  */
97058f0484fSRodney W. Grimes static void
97158f0484fSRodney W. Grimes fts_padjust(sp, addr)
97258f0484fSRodney W. Grimes 	FTS *sp;
97358f0484fSRodney W. Grimes 	void *addr;
97458f0484fSRodney W. Grimes {
97558f0484fSRodney W. Grimes 	FTSENT *p;
97658f0484fSRodney W. Grimes 
97758f0484fSRodney W. Grimes #define	ADJUST(p) {							\
97858f0484fSRodney W. Grimes 	(p)->fts_accpath =						\
97958f0484fSRodney W. Grimes 	    (char *)addr + ((p)->fts_accpath - (p)->fts_path);		\
98058f0484fSRodney W. Grimes 	(p)->fts_path = addr;						\
98158f0484fSRodney W. Grimes }
98258f0484fSRodney W. Grimes 	/* Adjust the current set of children. */
98358f0484fSRodney W. Grimes 	for (p = sp->fts_child; p; p = p->fts_link)
98458f0484fSRodney W. Grimes 		ADJUST(p);
98558f0484fSRodney W. Grimes 
98658f0484fSRodney W. Grimes 	/* Adjust the rest of the tree. */
98758f0484fSRodney W. Grimes 	for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
98858f0484fSRodney W. Grimes 		ADJUST(p);
98958f0484fSRodney W. Grimes 		p = p->fts_link ? p->fts_link : p->fts_parent;
99058f0484fSRodney W. Grimes 	}
99158f0484fSRodney W. Grimes }
99258f0484fSRodney W. Grimes 
99358f0484fSRodney W. Grimes static size_t
99458f0484fSRodney W. Grimes fts_maxarglen(argv)
99558f0484fSRodney W. Grimes 	char * const *argv;
99658f0484fSRodney W. Grimes {
99758f0484fSRodney W. Grimes 	size_t len, max;
99858f0484fSRodney W. Grimes 
99958f0484fSRodney W. Grimes 	for (max = 0; *argv; ++argv)
100058f0484fSRodney W. Grimes 		if ((len = strlen(*argv)) > max)
100158f0484fSRodney W. Grimes 			max = len;
100258f0484fSRodney W. Grimes 	return (max);
100358f0484fSRodney W. Grimes }
10049a91f1ccSWarner Losh 
10059a91f1ccSWarner Losh /*
10069a91f1ccSWarner Losh  * Change to dir specified by fd or p->fts_accpath without getting
10079a91f1ccSWarner Losh  * tricked by someone changing the world out from underneath us.
10089a91f1ccSWarner Losh  * Assumes p->fts_dev and p->fts_ino are filled in.
10099a91f1ccSWarner Losh  */
10109a91f1ccSWarner Losh static int
10119a91f1ccSWarner Losh fts_safe_changedir(sp, p, fd)
10129a91f1ccSWarner Losh 	FTS *sp;
10139a91f1ccSWarner Losh 	FTSENT *p;
10149a91f1ccSWarner Losh 	int fd;
10159a91f1ccSWarner Losh {
10169a91f1ccSWarner Losh 	int ret, oerrno, newfd;
10179a91f1ccSWarner Losh 	struct stat sb;
10189a91f1ccSWarner Losh 
10199a91f1ccSWarner Losh 	newfd = fd;
10209a91f1ccSWarner Losh 	if (ISSET(FTS_NOCHDIR))
10219a91f1ccSWarner Losh 		return (0);
10229a91f1ccSWarner Losh 	if (fd < 0 && (newfd = open(p->fts_accpath, O_RDONLY, 0)) < 0)
10239a91f1ccSWarner Losh 		return (-1);
10249a91f1ccSWarner Losh 	if (fstat(newfd, &sb)) {
10259a91f1ccSWarner Losh 		ret = -1;
10269a91f1ccSWarner Losh 		goto bail;
10279a91f1ccSWarner Losh 	}
10289a91f1ccSWarner Losh 	if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
10299a91f1ccSWarner Losh 		errno = ENOENT;		/* disinformation */
10309a91f1ccSWarner Losh 		ret = -1;
10319a91f1ccSWarner Losh 		goto bail;
10329a91f1ccSWarner Losh 	}
10339a91f1ccSWarner Losh 	ret = fchdir(newfd);
10349a91f1ccSWarner Losh bail:
10359a91f1ccSWarner Losh 	oerrno = errno;
10369a91f1ccSWarner Losh 	if (fd < 0)
10379a91f1ccSWarner Losh 		(void)close(newfd);
10389a91f1ccSWarner Losh 	errno = oerrno;
10399a91f1ccSWarner Losh 	return (ret);
10409a91f1ccSWarner Losh }
1041