xref: /minix3/lib/libc/gen/fts.c (revision 6dc59ac7dc1f1d3005e2caf6fd9c1e64bf05e3d7)
10a6a1f1dSLionel Sambuc /*	$NetBSD: fts.c,v 1.48 2015/01/29 15:55:21 manu Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993, 1994
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
332fe8fb19SBen Gras #include "nbtool_config.h"
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras 
362fe8fb19SBen Gras #include <sys/cdefs.h>
372fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
382fe8fb19SBen Gras #if 0
392fe8fb19SBen Gras static char sccsid[] = "@(#)fts.c	8.6 (Berkeley) 8/14/94";
402fe8fb19SBen Gras #else
410a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fts.c,v 1.48 2015/01/29 15:55:21 manu Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #include "namespace.h"
462fe8fb19SBen Gras #include <sys/param.h>
472fe8fb19SBen Gras #include <sys/stat.h>
482fe8fb19SBen Gras 
492fe8fb19SBen Gras #include <assert.h>
502fe8fb19SBen Gras #include <dirent.h>
512fe8fb19SBen Gras #include <errno.h>
522fe8fb19SBen Gras #include <fcntl.h>
532fe8fb19SBen Gras #include <fts.h>
542fe8fb19SBen Gras #include <stdlib.h>
552fe8fb19SBen Gras #include <string.h>
562fe8fb19SBen Gras #include <unistd.h>
572fe8fb19SBen Gras 
582fe8fb19SBen Gras #if ! HAVE_NBTOOL_CONFIG_H
592fe8fb19SBen Gras #define	HAVE_STRUCT_DIRENT_D_NAMLEN
602fe8fb19SBen Gras #endif
612fe8fb19SBen Gras 
622fe8fb19SBen Gras static FTSENT	*fts_alloc(FTS *, const char *, size_t);
632fe8fb19SBen Gras static FTSENT	*fts_build(FTS *, int);
642fe8fb19SBen Gras static void	 fts_free(FTSENT *);
652fe8fb19SBen Gras static void	 fts_lfree(FTSENT *);
662fe8fb19SBen Gras static void	 fts_load(FTS *, FTSENT *);
672fe8fb19SBen Gras static size_t	 fts_maxarglen(char * const *);
682fe8fb19SBen Gras static size_t	 fts_pow2(size_t);
692fe8fb19SBen Gras static int	 fts_palloc(FTS *, size_t);
702fe8fb19SBen Gras static void	 fts_padjust(FTS *, FTSENT *);
712fe8fb19SBen Gras static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
722fe8fb19SBen Gras static unsigned short fts_stat(FTS *, FTSENT *, int);
732fe8fb19SBen Gras static int	 fts_safe_changedir(const FTS *, const FTSENT *, int,
742fe8fb19SBen Gras     const char *);
752fe8fb19SBen Gras 
762fe8fb19SBen Gras #if defined(ALIGNBYTES) && defined(ALIGN)
772fe8fb19SBen Gras #define	FTS_ALLOC_ALIGNED	1
782fe8fb19SBen Gras #else
792fe8fb19SBen Gras #undef	FTS_ALLOC_ALIGNED
802fe8fb19SBen Gras #endif
812fe8fb19SBen Gras 
82f14fb602SLionel Sambuc #ifndef ftsent_namelen_truncate
83f14fb602SLionel Sambuc #define ftsent_namelen_truncate(a)	\
84f14fb602SLionel Sambuc     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
85f14fb602SLionel Sambuc #endif
86f14fb602SLionel Sambuc #ifndef ftsent_pathlen_truncate
87f14fb602SLionel Sambuc #define ftsent_pathlen_truncate(a) \
88f14fb602SLionel Sambuc     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
89f14fb602SLionel Sambuc #endif
90f14fb602SLionel Sambuc #ifndef fts_pathlen_truncate
91f14fb602SLionel Sambuc #define fts_pathlen_truncate(a)	\
92f14fb602SLionel Sambuc     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
93f14fb602SLionel Sambuc #endif
94f14fb602SLionel Sambuc #ifndef fts_nitems_truncate
95f14fb602SLionel Sambuc #define fts_nitems_truncate(a) \
96f14fb602SLionel Sambuc     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
97f14fb602SLionel Sambuc #endif
98f14fb602SLionel Sambuc 
992fe8fb19SBen Gras #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
1002fe8fb19SBen Gras 
1012fe8fb19SBen Gras #define	CLR(opt)	(sp->fts_options &= ~(opt))
1022fe8fb19SBen Gras #define	ISSET(opt)	(sp->fts_options & (opt))
1032fe8fb19SBen Gras #define	SET(opt)	(sp->fts_options |= (opt))
1042fe8fb19SBen Gras 
1052fe8fb19SBen Gras #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
1062fe8fb19SBen Gras #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
1072fe8fb19SBen Gras 
1082fe8fb19SBen Gras /* fts_build flags */
1092fe8fb19SBen Gras #define	BCHILD		1		/* fts_children */
1102fe8fb19SBen Gras #define	BNAMES		2		/* fts_children, names only */
1112fe8fb19SBen Gras #define	BREAD		3		/* fts_read */
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras #ifndef DTF_HIDEW
1142fe8fb19SBen Gras #undef FTS_WHITEOUT
1152fe8fb19SBen Gras #endif
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras FTS *
fts_open(char * const * argv,int options,int (* compar)(const FTSENT **,const FTSENT **))1182fe8fb19SBen Gras fts_open(char * const *argv, int options,
1192fe8fb19SBen Gras     int (*compar)(const FTSENT **, const FTSENT **))
1202fe8fb19SBen Gras {
1212fe8fb19SBen Gras 	FTS *sp;
1222fe8fb19SBen Gras 	FTSENT *p, *root;
1232fe8fb19SBen Gras 	size_t nitems;
1242fe8fb19SBen Gras 	FTSENT *parent, *tmp = NULL;	/* pacify gcc */
1252fe8fb19SBen Gras 	size_t len;
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras 	_DIAGASSERT(argv != NULL);
1282fe8fb19SBen Gras 
1292fe8fb19SBen Gras 	/* Options check. */
1302fe8fb19SBen Gras 	if (options & ~FTS_OPTIONMASK) {
1312fe8fb19SBen Gras 		errno = EINVAL;
1322fe8fb19SBen Gras 		return (NULL);
1332fe8fb19SBen Gras 	}
1342fe8fb19SBen Gras 
1352fe8fb19SBen Gras 	/* Allocate/initialize the stream */
136f14fb602SLionel Sambuc 	if ((sp = malloc(sizeof(FTS))) == NULL)
1372fe8fb19SBen Gras 		return (NULL);
1382fe8fb19SBen Gras 	memset(sp, 0, sizeof(FTS));
1392fe8fb19SBen Gras 	sp->fts_compar = compar;
1402fe8fb19SBen Gras 	sp->fts_options = options;
1412fe8fb19SBen Gras 
1422fe8fb19SBen Gras 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
1432fe8fb19SBen Gras 	if (ISSET(FTS_LOGICAL))
1442fe8fb19SBen Gras 		SET(FTS_NOCHDIR);
1452fe8fb19SBen Gras 
1462fe8fb19SBen Gras 	/*
1472fe8fb19SBen Gras 	 * Start out with 1K of path space, and enough, in any case,
1482fe8fb19SBen Gras 	 * to hold the user's paths.
1492fe8fb19SBen Gras 	 */
1502fe8fb19SBen Gras 	if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
1512fe8fb19SBen Gras 		goto mem1;
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras 	/* Allocate/initialize root's parent. */
1542fe8fb19SBen Gras 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
1552fe8fb19SBen Gras 		goto mem2;
1562fe8fb19SBen Gras 	parent->fts_level = FTS_ROOTPARENTLEVEL;
1572fe8fb19SBen Gras 
1582fe8fb19SBen Gras 	/* Allocate/initialize root(s). */
1592fe8fb19SBen Gras 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
1602fe8fb19SBen Gras 		/* Don't allow zero-length paths. */
1612fe8fb19SBen Gras 		if ((len = strlen(*argv)) == 0) {
1622fe8fb19SBen Gras 			errno = ENOENT;
1632fe8fb19SBen Gras 			goto mem3;
1642fe8fb19SBen Gras 		}
1652fe8fb19SBen Gras 
1662fe8fb19SBen Gras 		if ((p = fts_alloc(sp, *argv, len)) == NULL)
1672fe8fb19SBen Gras 			goto mem3;
1682fe8fb19SBen Gras 		p->fts_level = FTS_ROOTLEVEL;
1692fe8fb19SBen Gras 		p->fts_parent = parent;
1702fe8fb19SBen Gras 		p->fts_accpath = p->fts_name;
1712fe8fb19SBen Gras 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
1722fe8fb19SBen Gras 
1732fe8fb19SBen Gras 		/* Command-line "." and ".." are real directories. */
1742fe8fb19SBen Gras 		if (p->fts_info == FTS_DOT)
1752fe8fb19SBen Gras 			p->fts_info = FTS_D;
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras 		/*
1782fe8fb19SBen Gras 		 * If comparison routine supplied, traverse in sorted
1792fe8fb19SBen Gras 		 * order; otherwise traverse in the order specified.
1802fe8fb19SBen Gras 		 */
1812fe8fb19SBen Gras 		if (compar) {
1822fe8fb19SBen Gras 			p->fts_link = root;
1832fe8fb19SBen Gras 			root = p;
1842fe8fb19SBen Gras 		} else {
1852fe8fb19SBen Gras 			p->fts_link = NULL;
1862fe8fb19SBen Gras 			if (root == NULL)
1872fe8fb19SBen Gras 				tmp = root = p;
1882fe8fb19SBen Gras 			else {
1892fe8fb19SBen Gras 				tmp->fts_link = p;
1902fe8fb19SBen Gras 				tmp = p;
1912fe8fb19SBen Gras 			}
1922fe8fb19SBen Gras 		}
1932fe8fb19SBen Gras 	}
1942fe8fb19SBen Gras 	if (compar && nitems > 1)
1952fe8fb19SBen Gras 		root = fts_sort(sp, root, nitems);
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras 	/*
1982fe8fb19SBen Gras 	 * Allocate a dummy pointer and make fts_read think that we've just
1992fe8fb19SBen Gras 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
2002fe8fb19SBen Gras 	 * so that everything about the "current" node is ignored.
2012fe8fb19SBen Gras 	 */
2022fe8fb19SBen Gras 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
2032fe8fb19SBen Gras 		goto mem3;
2042fe8fb19SBen Gras 	sp->fts_cur->fts_link = root;
2052fe8fb19SBen Gras 	sp->fts_cur->fts_info = FTS_INIT;
2062fe8fb19SBen Gras 
2072fe8fb19SBen Gras 	/*
208f14fb602SLionel Sambuc 	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
2092fe8fb19SBen Gras 	 * that we can get back here; this could be avoided for some paths,
2102fe8fb19SBen Gras 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
2112fe8fb19SBen Gras 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
2122fe8fb19SBen Gras 	 * descriptor we run anyway, just more slowly.
2132fe8fb19SBen Gras 	 */
2142a8a99ccSThomas Veerman #ifndef O_CLOEXEC
2152a8a99ccSThomas Veerman #define O_CLOEXEC 0
2162a8a99ccSThomas Veerman #endif
2172fe8fb19SBen Gras 	if (!ISSET(FTS_NOCHDIR)) {
218f14fb602SLionel Sambuc 		if ((sp->fts_rfd = open(".", O_RDONLY | O_CLOEXEC, 0)) == -1)
2192fe8fb19SBen Gras 			SET(FTS_NOCHDIR);
2202fe8fb19SBen Gras 	}
2212fe8fb19SBen Gras 
2222fe8fb19SBen Gras 	if (nitems == 0)
2232fe8fb19SBen Gras 		fts_free(parent);
2242fe8fb19SBen Gras 
2252fe8fb19SBen Gras 	return (sp);
2262fe8fb19SBen Gras 
2272fe8fb19SBen Gras mem3:	fts_lfree(root);
2282fe8fb19SBen Gras 	fts_free(parent);
2292fe8fb19SBen Gras mem2:	free(sp->fts_path);
2302fe8fb19SBen Gras mem1:	free(sp);
2312fe8fb19SBen Gras 	return (NULL);
2322fe8fb19SBen Gras }
2332fe8fb19SBen Gras 
2342fe8fb19SBen Gras static void
fts_load(FTS * sp,FTSENT * p)2352fe8fb19SBen Gras fts_load(FTS *sp, FTSENT *p)
2362fe8fb19SBen Gras {
2372fe8fb19SBen Gras 	size_t len;
2382fe8fb19SBen Gras 	char *cp;
2392fe8fb19SBen Gras 
2402fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
2412fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
2422fe8fb19SBen Gras 
2432fe8fb19SBen Gras 	/*
2442fe8fb19SBen Gras 	 * Load the stream structure for the next traversal.  Since we don't
2452fe8fb19SBen Gras 	 * actually enter the directory until after the preorder visit, set
2462fe8fb19SBen Gras 	 * the fts_accpath field specially so the chdir gets done to the right
2472fe8fb19SBen Gras 	 * place and the user can access the first node.  From fts_open it's
2482fe8fb19SBen Gras 	 * known that the path will fit.
2492fe8fb19SBen Gras 	 */
2502fe8fb19SBen Gras 	len = p->fts_pathlen = p->fts_namelen;
2512fe8fb19SBen Gras 	memmove(sp->fts_path, p->fts_name, len + 1);
2522fe8fb19SBen Gras 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
2532fe8fb19SBen Gras 		len = strlen(++cp);
2542fe8fb19SBen Gras 		memmove(p->fts_name, cp, len + 1);
255f14fb602SLionel Sambuc 		p->fts_namelen = ftsent_namelen_truncate(len);
2562fe8fb19SBen Gras 	}
2572fe8fb19SBen Gras 	p->fts_accpath = p->fts_path = sp->fts_path;
2582fe8fb19SBen Gras 	sp->fts_dev = p->fts_dev;
2592fe8fb19SBen Gras }
2602fe8fb19SBen Gras 
2612fe8fb19SBen Gras int
fts_close(FTS * sp)2622fe8fb19SBen Gras fts_close(FTS *sp)
2632fe8fb19SBen Gras {
2642fe8fb19SBen Gras 	FTSENT *freep, *p;
2652fe8fb19SBen Gras 	int saved_errno = 0;
2662fe8fb19SBen Gras 
2672fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 	/*
2702fe8fb19SBen Gras 	 * This still works if we haven't read anything -- the dummy structure
2712fe8fb19SBen Gras 	 * points to the root list, so we step through to the end of the root
2722fe8fb19SBen Gras 	 * list which has a valid parent pointer.
2732fe8fb19SBen Gras 	 */
2742fe8fb19SBen Gras 	if (sp->fts_cur) {
2752fe8fb19SBen Gras 		if (sp->fts_cur->fts_flags & FTS_SYMFOLLOW)
2762fe8fb19SBen Gras 			(void)close(sp->fts_cur->fts_symfd);
2772fe8fb19SBen Gras 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
2782fe8fb19SBen Gras 			freep = p;
2792fe8fb19SBen Gras 			p = p->fts_link ? p->fts_link : p->fts_parent;
2802fe8fb19SBen Gras 			fts_free(freep);
2812fe8fb19SBen Gras 		}
2822fe8fb19SBen Gras 		fts_free(p);
2832fe8fb19SBen Gras 	}
2842fe8fb19SBen Gras 
2852fe8fb19SBen Gras 	/* Free up child linked list, sort array, path buffer. */
2862fe8fb19SBen Gras 	if (sp->fts_child)
2872fe8fb19SBen Gras 		fts_lfree(sp->fts_child);
2882fe8fb19SBen Gras 	if (sp->fts_array)
2892fe8fb19SBen Gras 		free(sp->fts_array);
2902fe8fb19SBen Gras 	free(sp->fts_path);
2912fe8fb19SBen Gras 
2922fe8fb19SBen Gras 	/* Return to original directory, save errno if necessary. */
2932fe8fb19SBen Gras 	if (!ISSET(FTS_NOCHDIR)) {
2942fe8fb19SBen Gras 		if (fchdir(sp->fts_rfd) == -1)
2952fe8fb19SBen Gras 			saved_errno = errno;
2962fe8fb19SBen Gras 		(void)close(sp->fts_rfd);
2972fe8fb19SBen Gras 	}
2982fe8fb19SBen Gras 
2992fe8fb19SBen Gras 	/* Free up the stream pointer. */
3002fe8fb19SBen Gras 	free(sp);
3012fe8fb19SBen Gras 	if (saved_errno) {
3022fe8fb19SBen Gras 		errno = saved_errno;
3032fe8fb19SBen Gras 		return -1;
3042fe8fb19SBen Gras 	}
3052fe8fb19SBen Gras 
3062fe8fb19SBen Gras 	return 0;
3072fe8fb19SBen Gras }
3082fe8fb19SBen Gras 
3092fe8fb19SBen Gras #if !defined(__FTS_COMPAT_TAILINGSLASH)
3102fe8fb19SBen Gras 
3112fe8fb19SBen Gras /*
3122fe8fb19SBen Gras  * Special case of "/" at the end of the path so that slashes aren't
3132fe8fb19SBen Gras  * appended which would cause paths to be written as "....//foo".
3142fe8fb19SBen Gras  */
3152fe8fb19SBen Gras #define	NAPPEND(p)							\
3162fe8fb19SBen Gras 	(p->fts_path[p->fts_pathlen - 1] == '/'				\
3172fe8fb19SBen Gras 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
3182fe8fb19SBen Gras 
3192fe8fb19SBen Gras #else /* !defined(__FTS_COMPAT_TAILINGSLASH) */
3202fe8fb19SBen Gras 
3212fe8fb19SBen Gras /*
3222fe8fb19SBen Gras  * compatibility with the old behaviour.
3232fe8fb19SBen Gras  *
3242fe8fb19SBen Gras  * Special case a root of "/" so that slashes aren't appended which would
3252fe8fb19SBen Gras  * cause paths to be written as "//foo".
3262fe8fb19SBen Gras  */
3272fe8fb19SBen Gras 
3282fe8fb19SBen Gras #define	NAPPEND(p)							\
3292fe8fb19SBen Gras 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 &&	\
3302fe8fb19SBen Gras 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
3312fe8fb19SBen Gras 
3322fe8fb19SBen Gras #endif /* !defined(__FTS_COMPAT_TAILINGSLASH) */
3332fe8fb19SBen Gras 
3342fe8fb19SBen Gras FTSENT *
fts_read(FTS * sp)3352fe8fb19SBen Gras fts_read(FTS *sp)
3362fe8fb19SBen Gras {
3372fe8fb19SBen Gras 	FTSENT *p, *tmp;
3382fe8fb19SBen Gras 	int instr;
3392fe8fb19SBen Gras 	char *t;
3402fe8fb19SBen Gras 	int saved_errno;
3412fe8fb19SBen Gras 
3422fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
3432fe8fb19SBen Gras 
3442fe8fb19SBen Gras 	/* If finished or unrecoverable error, return NULL. */
3452fe8fb19SBen Gras 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
3462fe8fb19SBen Gras 		return (NULL);
3472fe8fb19SBen Gras 
3482fe8fb19SBen Gras 	/* Set current node pointer. */
3492fe8fb19SBen Gras 	p = sp->fts_cur;
3502fe8fb19SBen Gras 
3512fe8fb19SBen Gras 	/* Save and zero out user instructions. */
3522fe8fb19SBen Gras 	instr = p->fts_instr;
3532fe8fb19SBen Gras 	p->fts_instr = FTS_NOINSTR;
3542fe8fb19SBen Gras 
3552fe8fb19SBen Gras 	/* Any type of file may be re-visited; re-stat and re-turn. */
3562fe8fb19SBen Gras 	if (instr == FTS_AGAIN) {
3572fe8fb19SBen Gras 		p->fts_info = fts_stat(sp, p, 0);
3582fe8fb19SBen Gras 		return (p);
3592fe8fb19SBen Gras 	}
3602fe8fb19SBen Gras 
3612fe8fb19SBen Gras 	/*
3622fe8fb19SBen Gras 	 * Following a symlink -- SLNONE test allows application to see
3632fe8fb19SBen Gras 	 * SLNONE and recover.  If indirecting through a symlink, have
3642fe8fb19SBen Gras 	 * keep a pointer to current location.  If unable to get that
3652fe8fb19SBen Gras 	 * pointer, follow fails.
3662fe8fb19SBen Gras 	 */
3672fe8fb19SBen Gras 	if (instr == FTS_FOLLOW &&
3682fe8fb19SBen Gras 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
3692fe8fb19SBen Gras 		p->fts_info = fts_stat(sp, p, 1);
3702fe8fb19SBen Gras 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
371f14fb602SLionel Sambuc 			if ((p->fts_symfd = open(".", O_RDONLY | O_CLOEXEC, 0))
372f14fb602SLionel Sambuc 			    == -1) {
3732fe8fb19SBen Gras 				p->fts_errno = errno;
3742fe8fb19SBen Gras 				p->fts_info = FTS_ERR;
3752fe8fb19SBen Gras 			} else
3762fe8fb19SBen Gras 				p->fts_flags |= FTS_SYMFOLLOW;
3772fe8fb19SBen Gras 		}
3782fe8fb19SBen Gras 		return (p);
3792fe8fb19SBen Gras 	}
3802fe8fb19SBen Gras 
3812fe8fb19SBen Gras 	/* Directory in pre-order. */
3822fe8fb19SBen Gras 	if (p->fts_info == FTS_D) {
3832fe8fb19SBen Gras 		/* If skipped or crossed mount point, do post-order visit. */
3842fe8fb19SBen Gras 		if (instr == FTS_SKIP ||
3852fe8fb19SBen Gras 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
3862fe8fb19SBen Gras 			if (p->fts_flags & FTS_SYMFOLLOW)
3872fe8fb19SBen Gras 				(void)close(p->fts_symfd);
3882fe8fb19SBen Gras 			if (sp->fts_child) {
3892fe8fb19SBen Gras 				fts_lfree(sp->fts_child);
3902fe8fb19SBen Gras 				sp->fts_child = NULL;
3912fe8fb19SBen Gras 			}
3922fe8fb19SBen Gras 			p->fts_info = FTS_DP;
3932fe8fb19SBen Gras 			return (p);
3942fe8fb19SBen Gras 		}
3952fe8fb19SBen Gras 
3962fe8fb19SBen Gras 		/* Rebuild if only read the names and now traversing. */
3972fe8fb19SBen Gras 		if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
3982fe8fb19SBen Gras 			CLR(FTS_NAMEONLY);
3992fe8fb19SBen Gras 			fts_lfree(sp->fts_child);
4002fe8fb19SBen Gras 			sp->fts_child = NULL;
4012fe8fb19SBen Gras 		}
4022fe8fb19SBen Gras 
4032fe8fb19SBen Gras 		/*
4042fe8fb19SBen Gras 		 * Cd to the subdirectory.
4052fe8fb19SBen Gras 		 *
4062fe8fb19SBen Gras 		 * If have already read and now fail to chdir, whack the list
4072fe8fb19SBen Gras 		 * to make the names come out right, and set the parent errno
4082fe8fb19SBen Gras 		 * so the application will eventually get an error condition.
4092fe8fb19SBen Gras 		 * Set the FTS_DONTCHDIR flag so that when we logically change
4102fe8fb19SBen Gras 		 * directories back to the parent we don't do a chdir.
4112fe8fb19SBen Gras 		 *
4122fe8fb19SBen Gras 		 * If haven't read do so.  If the read fails, fts_build sets
4132fe8fb19SBen Gras 		 * FTS_STOP or the fts_info field of the node.
4142fe8fb19SBen Gras 		 */
4152fe8fb19SBen Gras 		if (sp->fts_child) {
4162fe8fb19SBen Gras 			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
4172fe8fb19SBen Gras 				p->fts_errno = errno;
4182fe8fb19SBen Gras 				p->fts_flags |= FTS_DONTCHDIR;
4192fe8fb19SBen Gras 				for (p = sp->fts_child; p; p = p->fts_link)
4202fe8fb19SBen Gras 					p->fts_accpath =
4212fe8fb19SBen Gras 					    p->fts_parent->fts_accpath;
4222fe8fb19SBen Gras 			}
4232fe8fb19SBen Gras 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
4242fe8fb19SBen Gras 			if (ISSET(FTS_STOP))
4252fe8fb19SBen Gras 				return (NULL);
4262fe8fb19SBen Gras 			return (p);
4272fe8fb19SBen Gras 		}
4282fe8fb19SBen Gras 		p = sp->fts_child;
4292fe8fb19SBen Gras 		sp->fts_child = NULL;
4302fe8fb19SBen Gras 		goto name;
4312fe8fb19SBen Gras 	}
4322fe8fb19SBen Gras 
4330a6a1f1dSLionel Sambuc next:
4342fe8fb19SBen Gras 	/* Move to the next node on this level. */
4350a6a1f1dSLionel Sambuc 	tmp = p;
4360a6a1f1dSLionel Sambuc 
4370a6a1f1dSLionel Sambuc 	/*
4380a6a1f1dSLionel Sambuc 	 * We are going to free sp->fts_cur, set it to NULL so
4390a6a1f1dSLionel Sambuc 	 * that fts_close() does not attempt to free it again
4400a6a1f1dSLionel Sambuc 	 * if we exit without setting it to a new value because
4410a6a1f1dSLionel Sambuc 	 * FCHDIR() failed below.
4420a6a1f1dSLionel Sambuc 	 */
4430a6a1f1dSLionel Sambuc 	assert(tmp == sp->fts_cur);
4440a6a1f1dSLionel Sambuc 	sp->fts_cur = NULL;
4450a6a1f1dSLionel Sambuc 
4462fe8fb19SBen Gras 	if ((p = p->fts_link) != NULL) {
4472fe8fb19SBen Gras 		fts_free(tmp);
4482fe8fb19SBen Gras 
4492fe8fb19SBen Gras 		/*
4502fe8fb19SBen Gras 		 * If reached the top, return to the original directory, and
4512fe8fb19SBen Gras 		 * load the paths for the next root.
4522fe8fb19SBen Gras 		 */
4532fe8fb19SBen Gras 		if (p->fts_level == FTS_ROOTLEVEL) {
4542fe8fb19SBen Gras 			if (FCHDIR(sp, sp->fts_rfd)) {
4552fe8fb19SBen Gras 				SET(FTS_STOP);
4562fe8fb19SBen Gras 				return (NULL);
4572fe8fb19SBen Gras 			}
4582fe8fb19SBen Gras 			fts_load(sp, p);
4592fe8fb19SBen Gras 			return (sp->fts_cur = p);
4602fe8fb19SBen Gras 		}
4612fe8fb19SBen Gras 
4622fe8fb19SBen Gras 		/*
4632fe8fb19SBen Gras 		 * User may have called fts_set on the node.  If skipped,
4642fe8fb19SBen Gras 		 * ignore.  If followed, get a file descriptor so we can
4652fe8fb19SBen Gras 		 * get back if necessary.
4662fe8fb19SBen Gras 		 */
4672fe8fb19SBen Gras 		if (p->fts_instr == FTS_SKIP)
4682fe8fb19SBen Gras 			goto next;
4692fe8fb19SBen Gras 		if (p->fts_instr == FTS_FOLLOW) {
4702fe8fb19SBen Gras 			p->fts_info = fts_stat(sp, p, 1);
4712fe8fb19SBen Gras 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
4722fe8fb19SBen Gras 				if ((p->fts_symfd =
473f14fb602SLionel Sambuc 				    open(".", O_RDONLY | O_CLOEXEC, 0)) == -1) {
4742fe8fb19SBen Gras 					p->fts_errno = errno;
4752fe8fb19SBen Gras 					p->fts_info = FTS_ERR;
4762fe8fb19SBen Gras 				} else
4772fe8fb19SBen Gras 					p->fts_flags |= FTS_SYMFOLLOW;
4782fe8fb19SBen Gras 			}
4792fe8fb19SBen Gras 			p->fts_instr = FTS_NOINSTR;
4802fe8fb19SBen Gras 		}
4812fe8fb19SBen Gras 
4822fe8fb19SBen Gras name:		t = sp->fts_path + NAPPEND(p->fts_parent);
4832fe8fb19SBen Gras 		*t++ = '/';
4842fe8fb19SBen Gras 		memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1));
4852fe8fb19SBen Gras 		return (sp->fts_cur = p);
4862fe8fb19SBen Gras 	}
4872fe8fb19SBen Gras 
4882fe8fb19SBen Gras 	/* Move up to the parent node. */
4892fe8fb19SBen Gras 	p = tmp->fts_parent;
4902fe8fb19SBen Gras 	fts_free(tmp);
4912fe8fb19SBen Gras 
4922fe8fb19SBen Gras 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
4932fe8fb19SBen Gras 		/*
4942fe8fb19SBen Gras 		 * Done; free everything up and set errno to 0 so the user
4952fe8fb19SBen Gras 		 * can distinguish between error and EOF.
4962fe8fb19SBen Gras 		 */
4972fe8fb19SBen Gras 		fts_free(p);
4982fe8fb19SBen Gras 		errno = 0;
4992fe8fb19SBen Gras 		return (sp->fts_cur = NULL);
5002fe8fb19SBen Gras 	}
5012fe8fb19SBen Gras 
502f14fb602SLionel Sambuc 	/* NUL terminate the pathname. */
5032fe8fb19SBen Gras 	sp->fts_path[p->fts_pathlen] = '\0';
5042fe8fb19SBen Gras 
5052fe8fb19SBen Gras 	/*
5062fe8fb19SBen Gras 	 * Return to the parent directory.  If at a root node or came through
5072fe8fb19SBen Gras 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
5082fe8fb19SBen Gras 	 * one directory.
5092fe8fb19SBen Gras 	 */
5102fe8fb19SBen Gras 	if (p->fts_level == FTS_ROOTLEVEL) {
5112fe8fb19SBen Gras 		if (FCHDIR(sp, sp->fts_rfd)) {
5122fe8fb19SBen Gras 			SET(FTS_STOP);
5132fe8fb19SBen Gras 			return (NULL);
5142fe8fb19SBen Gras 		}
5152fe8fb19SBen Gras 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
5162fe8fb19SBen Gras 		if (FCHDIR(sp, p->fts_symfd)) {
5172fe8fb19SBen Gras 			saved_errno = errno;
5182fe8fb19SBen Gras 			(void)close(p->fts_symfd);
5192fe8fb19SBen Gras 			errno = saved_errno;
5202fe8fb19SBen Gras 			SET(FTS_STOP);
5212fe8fb19SBen Gras 			return (NULL);
5222fe8fb19SBen Gras 		}
5232fe8fb19SBen Gras 		(void)close(p->fts_symfd);
5242fe8fb19SBen Gras 	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
5252fe8fb19SBen Gras 	    fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
5262fe8fb19SBen Gras 		SET(FTS_STOP);
5272fe8fb19SBen Gras 		return (NULL);
5282fe8fb19SBen Gras 	}
5292fe8fb19SBen Gras 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
5302fe8fb19SBen Gras 	return (sp->fts_cur = p);
5312fe8fb19SBen Gras }
5322fe8fb19SBen Gras 
5332fe8fb19SBen Gras /*
5342fe8fb19SBen Gras  * Fts_set takes the stream as an argument although it's not used in this
5352fe8fb19SBen Gras  * implementation; it would be necessary if anyone wanted to add global
5362fe8fb19SBen Gras  * semantics to fts using fts_set.  An error return is allowed for similar
5372fe8fb19SBen Gras  * reasons.
5382fe8fb19SBen Gras  */
5392fe8fb19SBen Gras /* ARGSUSED */
5402fe8fb19SBen Gras int
fts_set(FTS * sp,FTSENT * p,int instr)5412fe8fb19SBen Gras fts_set(FTS *sp, FTSENT *p, int instr)
5422fe8fb19SBen Gras {
5432fe8fb19SBen Gras 
5442fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
5452fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
5462fe8fb19SBen Gras 
5472fe8fb19SBen Gras 	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
5482fe8fb19SBen Gras 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
5492fe8fb19SBen Gras 		errno = EINVAL;
5502fe8fb19SBen Gras 		return (1);
5512fe8fb19SBen Gras 	}
5522fe8fb19SBen Gras 	p->fts_instr = instr;
5532fe8fb19SBen Gras 	return (0);
5542fe8fb19SBen Gras }
5552fe8fb19SBen Gras 
5562fe8fb19SBen Gras FTSENT *
fts_children(FTS * sp,int instr)5572fe8fb19SBen Gras fts_children(FTS *sp, int instr)
5582fe8fb19SBen Gras {
5592fe8fb19SBen Gras 	FTSENT *p;
5602fe8fb19SBen Gras 	int fd;
5612fe8fb19SBen Gras 
5622fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
5632fe8fb19SBen Gras 
5642fe8fb19SBen Gras 	if (instr && instr != FTS_NAMEONLY) {
5652fe8fb19SBen Gras 		errno = EINVAL;
5662fe8fb19SBen Gras 		return (NULL);
5672fe8fb19SBen Gras 	}
5682fe8fb19SBen Gras 
5692fe8fb19SBen Gras 	/* Set current node pointer. */
5702fe8fb19SBen Gras 	p = sp->fts_cur;
5712fe8fb19SBen Gras 
5722fe8fb19SBen Gras 	/*
5732fe8fb19SBen Gras 	 * Errno set to 0 so user can distinguish empty directory from
5742fe8fb19SBen Gras 	 * an error.
5752fe8fb19SBen Gras 	 */
5762fe8fb19SBen Gras 	errno = 0;
5772fe8fb19SBen Gras 
5782fe8fb19SBen Gras 	/* Fatal errors stop here. */
5792fe8fb19SBen Gras 	if (ISSET(FTS_STOP))
5802fe8fb19SBen Gras 		return (NULL);
5812fe8fb19SBen Gras 
5822fe8fb19SBen Gras 	/* Return logical hierarchy of user's arguments. */
5832fe8fb19SBen Gras 	if (p->fts_info == FTS_INIT)
5842fe8fb19SBen Gras 		return (p->fts_link);
5852fe8fb19SBen Gras 
5862fe8fb19SBen Gras 	/*
5872fe8fb19SBen Gras 	 * If not a directory being visited in pre-order, stop here.  Could
5882fe8fb19SBen Gras 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
5892fe8fb19SBen Gras 	 * same effect is available with FTS_AGAIN.
5902fe8fb19SBen Gras 	 */
5912fe8fb19SBen Gras 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
5922fe8fb19SBen Gras 		return (NULL);
5932fe8fb19SBen Gras 
5942fe8fb19SBen Gras 	/* Free up any previous child list. */
5952fe8fb19SBen Gras 	if (sp->fts_child)
5962fe8fb19SBen Gras 		fts_lfree(sp->fts_child);
5972fe8fb19SBen Gras 
5982fe8fb19SBen Gras 	if (instr == FTS_NAMEONLY) {
5992fe8fb19SBen Gras 		SET(FTS_NAMEONLY);
6002fe8fb19SBen Gras 		instr = BNAMES;
6012fe8fb19SBen Gras 	} else
6022fe8fb19SBen Gras 		instr = BCHILD;
6032fe8fb19SBen Gras 
6042fe8fb19SBen Gras 	/*
6052fe8fb19SBen Gras 	 * If using chdir on a relative path and called BEFORE fts_read does
6062fe8fb19SBen Gras 	 * its chdir to the root of a traversal, we can lose -- we need to
6072fe8fb19SBen Gras 	 * chdir into the subdirectory, and we don't know where the current
6082fe8fb19SBen Gras 	 * directory is, so we can't get back so that the upcoming chdir by
6092fe8fb19SBen Gras 	 * fts_read will work.
6102fe8fb19SBen Gras 	 */
6112fe8fb19SBen Gras 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
6122fe8fb19SBen Gras 	    ISSET(FTS_NOCHDIR))
6132fe8fb19SBen Gras 		return (sp->fts_child = fts_build(sp, instr));
6142fe8fb19SBen Gras 
6150a6a1f1dSLionel Sambuc 	if ((fd = open(".", O_RDONLY | O_CLOEXEC, 0)) == -1)
6162fe8fb19SBen Gras 		return (sp->fts_child = NULL);
6172fe8fb19SBen Gras 	sp->fts_child = fts_build(sp, instr);
6182fe8fb19SBen Gras 	if (fchdir(fd)) {
6192fe8fb19SBen Gras 		(void)close(fd);
6202fe8fb19SBen Gras 		return (NULL);
6212fe8fb19SBen Gras 	}
6222fe8fb19SBen Gras 	(void)close(fd);
6232fe8fb19SBen Gras 	return (sp->fts_child);
6242fe8fb19SBen Gras }
6252fe8fb19SBen Gras 
6262fe8fb19SBen Gras /*
6272fe8fb19SBen Gras  * This is the tricky part -- do not casually change *anything* in here.  The
6282fe8fb19SBen Gras  * idea is to build the linked list of entries that are used by fts_children
6292fe8fb19SBen Gras  * and fts_read.  There are lots of special cases.
6302fe8fb19SBen Gras  *
6312fe8fb19SBen Gras  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
6322fe8fb19SBen Gras  * set and it's a physical walk (so that symbolic links can't be directories),
6332fe8fb19SBen Gras  * we can do things quickly.  First, if it's a 4.4BSD file system, the type
6342fe8fb19SBen Gras  * of the file is in the directory entry.  Otherwise, we assume that the number
6352fe8fb19SBen Gras  * of subdirectories in a node is equal to the number of links to the parent.
6362fe8fb19SBen Gras  * The former skips all stat calls.  The latter skips stat calls in any leaf
6372fe8fb19SBen Gras  * directories and for any files after the subdirectories in the directory have
6382fe8fb19SBen Gras  * been found, cutting the stat calls by about 2/3.
6392fe8fb19SBen Gras  */
6402fe8fb19SBen Gras static FTSENT *
fts_build(FTS * sp,int type)6412fe8fb19SBen Gras fts_build(FTS *sp, int type)
6422fe8fb19SBen Gras {
6432fe8fb19SBen Gras 	struct dirent *dp;
6442fe8fb19SBen Gras 	FTSENT *p, *head;
6452fe8fb19SBen Gras 	size_t nitems;
6462fe8fb19SBen Gras 	FTSENT *cur, *tail;
6472fe8fb19SBen Gras 	DIR *dirp;
6482fe8fb19SBen Gras 	void *oldaddr;
6492fe8fb19SBen Gras 	size_t dnamlen;
6502fe8fb19SBen Gras 	int cderrno, descend, level, nlinks, saved_errno, nostat, doadjust;
6512fe8fb19SBen Gras 	size_t len, maxlen;
6522fe8fb19SBen Gras #ifdef FTS_WHITEOUT
6532fe8fb19SBen Gras 	int oflag;
6542fe8fb19SBen Gras #endif
6552fe8fb19SBen Gras 	char *cp = NULL;	/* pacify gcc */
6562fe8fb19SBen Gras 
6572fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
6582fe8fb19SBen Gras 
6592fe8fb19SBen Gras 	/* Set current node pointer. */
6602fe8fb19SBen Gras 	cur = sp->fts_cur;
6612fe8fb19SBen Gras 
6622fe8fb19SBen Gras 	/*
6632fe8fb19SBen Gras 	 * Open the directory for reading.  If this fails, we're done.
6642fe8fb19SBen Gras 	 * If being called from fts_read, set the fts_info field.
6652fe8fb19SBen Gras 	 */
6662fe8fb19SBen Gras #ifdef FTS_WHITEOUT
6672fe8fb19SBen Gras 	if (ISSET(FTS_WHITEOUT))
6682fe8fb19SBen Gras 		oflag = DTF_NODUP|DTF_REWIND;
6692fe8fb19SBen Gras 	else
6702fe8fb19SBen Gras 		oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
6712fe8fb19SBen Gras #else
6722fe8fb19SBen Gras #define	__opendir2(path, flag) opendir(path)
6732fe8fb19SBen Gras #endif
6742fe8fb19SBen Gras 	if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
6752fe8fb19SBen Gras 		if (type == BREAD) {
6762fe8fb19SBen Gras 			cur->fts_info = FTS_DNR;
6772fe8fb19SBen Gras 			cur->fts_errno = errno;
6782fe8fb19SBen Gras 		}
6792fe8fb19SBen Gras 		return (NULL);
6802fe8fb19SBen Gras 	}
6812fe8fb19SBen Gras 
6822fe8fb19SBen Gras 	/*
6832fe8fb19SBen Gras 	 * Nlinks is the number of possible entries of type directory in the
6842fe8fb19SBen Gras 	 * directory if we're cheating on stat calls, 0 if we're not doing
6852fe8fb19SBen Gras 	 * any stat calls at all, -1 if we're doing stats on everything.
6862fe8fb19SBen Gras 	 */
6872fe8fb19SBen Gras 	if (type == BNAMES) {
6882fe8fb19SBen Gras 		nlinks = 0;
6892fe8fb19SBen Gras 		nostat = 1;
6902fe8fb19SBen Gras 	} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
6912fe8fb19SBen Gras 		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
6922fe8fb19SBen Gras 		nostat = 1;
6932fe8fb19SBen Gras 	} else {
6942fe8fb19SBen Gras 		nlinks = -1;
6952fe8fb19SBen Gras 		nostat = 0;
6962fe8fb19SBen Gras 	}
6972fe8fb19SBen Gras 
6982fe8fb19SBen Gras #ifdef notdef
6992fe8fb19SBen Gras 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
7002fe8fb19SBen Gras 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
7012fe8fb19SBen Gras 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
7022fe8fb19SBen Gras #endif
7032fe8fb19SBen Gras 	/*
7042fe8fb19SBen Gras 	 * If we're going to need to stat anything or we want to descend
7052fe8fb19SBen Gras 	 * and stay in the directory, chdir.  If this fails we keep going,
7062fe8fb19SBen Gras 	 * but set a flag so we don't chdir after the post-order visit.
7072fe8fb19SBen Gras 	 * We won't be able to stat anything, but we can still return the
7082fe8fb19SBen Gras 	 * names themselves.  Note, that since fts_read won't be able to
7092fe8fb19SBen Gras 	 * chdir into the directory, it will have to return different path
7102fe8fb19SBen Gras 	 * names than before, i.e. "a/b" instead of "b".  Since the node
7112fe8fb19SBen Gras 	 * has already been visited in pre-order, have to wait until the
7122fe8fb19SBen Gras 	 * post-order visit to return the error.  There is a special case
7132fe8fb19SBen Gras 	 * here, if there was nothing to stat then it's not an error to
7142fe8fb19SBen Gras 	 * not be able to stat.  This is all fairly nasty.  If a program
7152fe8fb19SBen Gras 	 * needed sorted entries or stat information, they had better be
7162fe8fb19SBen Gras 	 * checking FTS_NS on the returned nodes.
7172fe8fb19SBen Gras 	 */
7182fe8fb19SBen Gras 	cderrno = 0;
7192fe8fb19SBen Gras 	if (nlinks || type == BREAD) {
7202fe8fb19SBen Gras 		if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
7212fe8fb19SBen Gras 			if (nlinks && type == BREAD)
7222fe8fb19SBen Gras 				cur->fts_errno = errno;
7232fe8fb19SBen Gras 			cur->fts_flags |= FTS_DONTCHDIR;
7242fe8fb19SBen Gras 			descend = 0;
7252fe8fb19SBen Gras 			cderrno = errno;
7262fe8fb19SBen Gras 		} else
7272fe8fb19SBen Gras 			descend = 1;
7282fe8fb19SBen Gras 	} else
7292fe8fb19SBen Gras 		descend = 0;
7302fe8fb19SBen Gras 
7312fe8fb19SBen Gras 	/*
7322fe8fb19SBen Gras 	 * Figure out the max file name length that can be stored in the
7332fe8fb19SBen Gras 	 * current path -- the inner loop allocates more path as necessary.
7342fe8fb19SBen Gras 	 * We really wouldn't have to do the maxlen calculations here, we
7352fe8fb19SBen Gras 	 * could do them in fts_read before returning the path, but it's a
7362fe8fb19SBen Gras 	 * lot easier here since the length is part of the dirent structure.
7372fe8fb19SBen Gras 	 *
7382fe8fb19SBen Gras 	 * If not changing directories set a pointer so that can just append
7392fe8fb19SBen Gras 	 * each new name into the path.
7402fe8fb19SBen Gras 	 */
7412fe8fb19SBen Gras 	len = NAPPEND(cur);
7422fe8fb19SBen Gras 	if (ISSET(FTS_NOCHDIR)) {
7432fe8fb19SBen Gras 		cp = sp->fts_path + len;
7442fe8fb19SBen Gras 		*cp++ = '/';
7452fe8fb19SBen Gras 	}
7462fe8fb19SBen Gras 	len++;
7472fe8fb19SBen Gras 	maxlen = sp->fts_pathlen - len;
7482fe8fb19SBen Gras 
7492fe8fb19SBen Gras #if defined(__FTS_COMPAT_LEVEL)
7502fe8fb19SBen Gras 	if (cur->fts_level == SHRT_MAX) {
7512fe8fb19SBen Gras 		(void)closedir(dirp);
7522fe8fb19SBen Gras 		cur->fts_info = FTS_ERR;
7532fe8fb19SBen Gras 		SET(FTS_STOP);
7542fe8fb19SBen Gras 		errno = ENAMETOOLONG;
7552fe8fb19SBen Gras 		return (NULL);
7562fe8fb19SBen Gras 	}
7572fe8fb19SBen Gras #endif
7582fe8fb19SBen Gras 
7592fe8fb19SBen Gras 	level = cur->fts_level + 1;
7602fe8fb19SBen Gras 
7612fe8fb19SBen Gras 	/* Read the directory, attaching each entry to the `link' pointer. */
7622fe8fb19SBen Gras 	doadjust = 0;
7632fe8fb19SBen Gras 	for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) {
7642fe8fb19SBen Gras 
7652fe8fb19SBen Gras 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
7662fe8fb19SBen Gras 			continue;
7672fe8fb19SBen Gras 
7682fe8fb19SBen Gras #if defined(HAVE_STRUCT_DIRENT_D_NAMLEN)
7692fe8fb19SBen Gras 		dnamlen = dp->d_namlen;
7702fe8fb19SBen Gras #else
7712fe8fb19SBen Gras 		dnamlen = strlen(dp->d_name);
7722fe8fb19SBen Gras #endif
7732fe8fb19SBen Gras 		if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL)
7742fe8fb19SBen Gras 			goto mem1;
7752fe8fb19SBen Gras 		if (dnamlen >= maxlen) {	/* include space for NUL */
7762fe8fb19SBen Gras 			oldaddr = sp->fts_path;
7772fe8fb19SBen Gras 			if (fts_palloc(sp, dnamlen + len + 1)) {
7782fe8fb19SBen Gras 				/*
7792fe8fb19SBen Gras 				 * No more memory for path or structures.  Save
7802fe8fb19SBen Gras 				 * errno, free up the current structure and the
7812fe8fb19SBen Gras 				 * structures already allocated.
7822fe8fb19SBen Gras 				 */
7832fe8fb19SBen Gras mem1:				saved_errno = errno;
7842fe8fb19SBen Gras 				if (p)
7852fe8fb19SBen Gras 					fts_free(p);
7862fe8fb19SBen Gras 				fts_lfree(head);
7872fe8fb19SBen Gras 				(void)closedir(dirp);
7882fe8fb19SBen Gras 				errno = saved_errno;
7892fe8fb19SBen Gras 				cur->fts_info = FTS_ERR;
7902fe8fb19SBen Gras 				SET(FTS_STOP);
7912fe8fb19SBen Gras 				return (NULL);
7922fe8fb19SBen Gras 			}
7932fe8fb19SBen Gras 			/* Did realloc() change the pointer? */
7942fe8fb19SBen Gras 			if (oldaddr != sp->fts_path) {
7952fe8fb19SBen Gras 				doadjust = 1;
7962fe8fb19SBen Gras 				if (ISSET(FTS_NOCHDIR))
7972fe8fb19SBen Gras 					cp = sp->fts_path + len;
7982fe8fb19SBen Gras 			}
7992fe8fb19SBen Gras 			maxlen = sp->fts_pathlen - len;
8002fe8fb19SBen Gras 		}
8012fe8fb19SBen Gras 
8022fe8fb19SBen Gras #if defined(__FTS_COMPAT_LENGTH)
8032fe8fb19SBen Gras 		if (len + dnamlen >= USHRT_MAX) {
8042fe8fb19SBen Gras 			/*
8052fe8fb19SBen Gras 			 * In an FTSENT, fts_pathlen is an unsigned short
8062fe8fb19SBen Gras 			 * so it is possible to wraparound here.
8072fe8fb19SBen Gras 			 * If we do, free up the current structure and the
8082fe8fb19SBen Gras 			 * structures already allocated, then error out
8092fe8fb19SBen Gras 			 * with ENAMETOOLONG.
8102fe8fb19SBen Gras 			 */
8112fe8fb19SBen Gras 			fts_free(p);
8122fe8fb19SBen Gras 			fts_lfree(head);
8132fe8fb19SBen Gras 			(void)closedir(dirp);
8142fe8fb19SBen Gras 			cur->fts_info = FTS_ERR;
8152fe8fb19SBen Gras 			SET(FTS_STOP);
8162fe8fb19SBen Gras 			errno = ENAMETOOLONG;
8172fe8fb19SBen Gras 			return (NULL);
8182fe8fb19SBen Gras 		}
8192fe8fb19SBen Gras #endif
8202fe8fb19SBen Gras 		p->fts_level = level;
821f14fb602SLionel Sambuc 		p->fts_pathlen = ftsent_pathlen_truncate(len + dnamlen);
8222fe8fb19SBen Gras 		p->fts_parent = sp->fts_cur;
8232fe8fb19SBen Gras 
8242fe8fb19SBen Gras #ifdef FTS_WHITEOUT
8252fe8fb19SBen Gras 		if (dp->d_type == DT_WHT)
8262fe8fb19SBen Gras 			p->fts_flags |= FTS_ISW;
8272fe8fb19SBen Gras #endif
8282fe8fb19SBen Gras 
8292fe8fb19SBen Gras 		if (cderrno) {
8302fe8fb19SBen Gras 			if (nlinks) {
8312fe8fb19SBen Gras 				p->fts_info = FTS_NS;
8322fe8fb19SBen Gras 				p->fts_errno = cderrno;
8332fe8fb19SBen Gras 			} else
8342fe8fb19SBen Gras 				p->fts_info = FTS_NSOK;
8352fe8fb19SBen Gras 			p->fts_accpath = cur->fts_accpath;
8362fe8fb19SBen Gras 		} else if (nlinks == 0
8372fe8fb19SBen Gras #ifdef DT_DIR
8382fe8fb19SBen Gras 		    || (nostat &&
8392fe8fb19SBen Gras 		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
8402fe8fb19SBen Gras #endif
8412fe8fb19SBen Gras 		    ) {
8422fe8fb19SBen Gras 			p->fts_accpath =
8432fe8fb19SBen Gras 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
8442fe8fb19SBen Gras 			p->fts_info = FTS_NSOK;
8452fe8fb19SBen Gras 		} else {
8462fe8fb19SBen Gras 			/* Build a file name for fts_stat to stat. */
8472fe8fb19SBen Gras 			if (ISSET(FTS_NOCHDIR)) {
8482fe8fb19SBen Gras 				p->fts_accpath = p->fts_path;
8492fe8fb19SBen Gras 				memmove(cp, p->fts_name,
8502fe8fb19SBen Gras 				        (size_t)(p->fts_namelen + 1));
8512fe8fb19SBen Gras 			} else
8522fe8fb19SBen Gras 				p->fts_accpath = p->fts_name;
8532fe8fb19SBen Gras 			/* Stat it. */
8542fe8fb19SBen Gras 			p->fts_info = fts_stat(sp, p, 0);
8552fe8fb19SBen Gras 
8562fe8fb19SBen Gras 			/* Decrement link count if applicable. */
8572fe8fb19SBen Gras 			if (nlinks > 0 && (p->fts_info == FTS_D ||
8582fe8fb19SBen Gras 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
8592fe8fb19SBen Gras 				--nlinks;
8602fe8fb19SBen Gras 		}
8612fe8fb19SBen Gras 
8622fe8fb19SBen Gras 		/* We walk in directory order so "ls -f" doesn't get upset. */
8632fe8fb19SBen Gras 		p->fts_link = NULL;
8642fe8fb19SBen Gras 		if (head == NULL)
8652fe8fb19SBen Gras 			head = tail = p;
8662fe8fb19SBen Gras 		else {
8672fe8fb19SBen Gras 			tail->fts_link = p;
8682fe8fb19SBen Gras 			tail = p;
8692fe8fb19SBen Gras 		}
8702fe8fb19SBen Gras 		++nitems;
8712fe8fb19SBen Gras 	}
8722fe8fb19SBen Gras 	(void)closedir(dirp);
8732fe8fb19SBen Gras 
8742fe8fb19SBen Gras 	/*
8752fe8fb19SBen Gras 	 * If had to realloc the path, adjust the addresses for the rest
8762fe8fb19SBen Gras 	 * of the tree.
8772fe8fb19SBen Gras 	 */
8782fe8fb19SBen Gras 	if (doadjust)
8792fe8fb19SBen Gras 		fts_padjust(sp, head);
8802fe8fb19SBen Gras 
8812fe8fb19SBen Gras 	/*
8822fe8fb19SBen Gras 	 * If not changing directories, reset the path back to original
8832fe8fb19SBen Gras 	 * state.
8842fe8fb19SBen Gras 	 */
8852fe8fb19SBen Gras 	if (ISSET(FTS_NOCHDIR)) {
8862fe8fb19SBen Gras 		if (len == sp->fts_pathlen || nitems == 0)
8872fe8fb19SBen Gras 			--cp;
8882fe8fb19SBen Gras 		*cp = '\0';
8892fe8fb19SBen Gras 	}
8902fe8fb19SBen Gras 
8912fe8fb19SBen Gras 	/*
8922fe8fb19SBen Gras 	 * If descended after called from fts_children or after called from
8932fe8fb19SBen Gras 	 * fts_read and nothing found, get back.  At the root level we use
8942fe8fb19SBen Gras 	 * the saved fd; if one of fts_open()'s arguments is a relative path
8952fe8fb19SBen Gras 	 * to an empty directory, we wind up here with no other way back.  If
8962fe8fb19SBen Gras 	 * can't get back, we're done.
8972fe8fb19SBen Gras 	 */
8982fe8fb19SBen Gras 	if (descend && (type == BCHILD || !nitems) &&
8992fe8fb19SBen Gras 	    (cur->fts_level == FTS_ROOTLEVEL ?
9002fe8fb19SBen Gras 	    FCHDIR(sp, sp->fts_rfd) :
9012fe8fb19SBen Gras 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
9022fe8fb19SBen Gras 		cur->fts_info = FTS_ERR;
9032fe8fb19SBen Gras 		SET(FTS_STOP);
9042fe8fb19SBen Gras 		return (NULL);
9052fe8fb19SBen Gras 	}
9062fe8fb19SBen Gras 
9072fe8fb19SBen Gras 	/* If didn't find anything, return NULL. */
9082fe8fb19SBen Gras 	if (!nitems) {
9092fe8fb19SBen Gras 		if (type == BREAD)
9102fe8fb19SBen Gras 			cur->fts_info = FTS_DP;
9112fe8fb19SBen Gras 		return (NULL);
9122fe8fb19SBen Gras 	}
9132fe8fb19SBen Gras 
9142fe8fb19SBen Gras 	/* Sort the entries. */
9152fe8fb19SBen Gras 	if (sp->fts_compar && nitems > 1)
9162fe8fb19SBen Gras 		head = fts_sort(sp, head, nitems);
9172fe8fb19SBen Gras 	return (head);
9182fe8fb19SBen Gras }
9192fe8fb19SBen Gras 
9202fe8fb19SBen Gras static unsigned short
fts_stat(FTS * sp,FTSENT * p,int follow)9212fe8fb19SBen Gras fts_stat(FTS *sp, FTSENT *p, int follow)
9222fe8fb19SBen Gras {
9232fe8fb19SBen Gras 	FTSENT *t;
9242fe8fb19SBen Gras 	dev_t dev;
9252fe8fb19SBen Gras 	__fts_ino_t ino;
9262fe8fb19SBen Gras 	__fts_stat_t *sbp, sb;
9272fe8fb19SBen Gras 	int saved_errno;
9282fe8fb19SBen Gras 
9292fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
9302fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
9312fe8fb19SBen Gras 
9322fe8fb19SBen Gras 	/* If user needs stat info, stat buffer already allocated. */
9332fe8fb19SBen Gras 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
9342fe8fb19SBen Gras 
9352fe8fb19SBen Gras #ifdef FTS_WHITEOUT
9362fe8fb19SBen Gras 	/* check for whiteout */
9372fe8fb19SBen Gras 	if (p->fts_flags & FTS_ISW) {
9382fe8fb19SBen Gras 		if (sbp != &sb) {
9392fe8fb19SBen Gras 			memset(sbp, '\0', sizeof (*sbp));
9402fe8fb19SBen Gras 			sbp->st_mode = S_IFWHT;
9412fe8fb19SBen Gras 		}
9422fe8fb19SBen Gras 		return (FTS_W);
9432fe8fb19SBen Gras 	}
9442fe8fb19SBen Gras #endif
9452fe8fb19SBen Gras 
9462fe8fb19SBen Gras 	/*
9472fe8fb19SBen Gras 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
9482fe8fb19SBen Gras 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
9492fe8fb19SBen Gras 	 * fail, set the errno from the stat call.
9502fe8fb19SBen Gras 	 */
9512fe8fb19SBen Gras 	if (ISSET(FTS_LOGICAL) || follow) {
9522fe8fb19SBen Gras 		if (stat(p->fts_accpath, sbp)) {
9532fe8fb19SBen Gras 			saved_errno = errno;
9542fe8fb19SBen Gras 			if (!lstat(p->fts_accpath, sbp)) {
9552fe8fb19SBen Gras 				errno = 0;
9562fe8fb19SBen Gras 				return (FTS_SLNONE);
9572fe8fb19SBen Gras 			}
9582fe8fb19SBen Gras 			p->fts_errno = saved_errno;
9592fe8fb19SBen Gras 			goto err;
9602fe8fb19SBen Gras 		}
9612fe8fb19SBen Gras 	} else if (lstat(p->fts_accpath, sbp)) {
9622fe8fb19SBen Gras 		p->fts_errno = errno;
9632fe8fb19SBen Gras err:		memset(sbp, 0, sizeof(*sbp));
9642fe8fb19SBen Gras 		return (FTS_NS);
9652fe8fb19SBen Gras 	}
9662fe8fb19SBen Gras 
9672fe8fb19SBen Gras 	if (S_ISDIR(sbp->st_mode)) {
9682fe8fb19SBen Gras 		/*
9692fe8fb19SBen Gras 		 * Set the device/inode.  Used to find cycles and check for
9702fe8fb19SBen Gras 		 * crossing mount points.  Also remember the link count, used
9712fe8fb19SBen Gras 		 * in fts_build to limit the number of stat calls.  It is
9722fe8fb19SBen Gras 		 * understood that these fields are only referenced if fts_info
9732fe8fb19SBen Gras 		 * is set to FTS_D.
9742fe8fb19SBen Gras 		 */
9752fe8fb19SBen Gras 		dev = p->fts_dev = sbp->st_dev;
9762fe8fb19SBen Gras 		ino = p->fts_ino = sbp->st_ino;
9772fe8fb19SBen Gras 		p->fts_nlink = sbp->st_nlink;
9782fe8fb19SBen Gras 
9792fe8fb19SBen Gras 		if (ISDOT(p->fts_name))
9802fe8fb19SBen Gras 			return (FTS_DOT);
9812fe8fb19SBen Gras 
9822fe8fb19SBen Gras 		/*
9832fe8fb19SBen Gras 		 * Cycle detection is done by brute force when the directory
9842fe8fb19SBen Gras 		 * is first encountered.  If the tree gets deep enough or the
9852fe8fb19SBen Gras 		 * number of symbolic links to directories is high enough,
9862fe8fb19SBen Gras 		 * something faster might be worthwhile.
9872fe8fb19SBen Gras 		 */
9882fe8fb19SBen Gras 		for (t = p->fts_parent;
9892fe8fb19SBen Gras 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
9902fe8fb19SBen Gras 			if (ino == t->fts_ino && dev == t->fts_dev) {
9912fe8fb19SBen Gras 				p->fts_cycle = t;
9922fe8fb19SBen Gras 				return (FTS_DC);
9932fe8fb19SBen Gras 			}
9942fe8fb19SBen Gras 		return (FTS_D);
9952fe8fb19SBen Gras 	}
9962fe8fb19SBen Gras 	if (S_ISLNK(sbp->st_mode))
9972fe8fb19SBen Gras 		return (FTS_SL);
9982fe8fb19SBen Gras 	if (S_ISREG(sbp->st_mode))
9992fe8fb19SBen Gras 		return (FTS_F);
10002fe8fb19SBen Gras 	return (FTS_DEFAULT);
10012fe8fb19SBen Gras }
10022fe8fb19SBen Gras 
10032fe8fb19SBen Gras static FTSENT *
fts_sort(FTS * sp,FTSENT * head,size_t nitems)10042fe8fb19SBen Gras fts_sort(FTS *sp, FTSENT *head, size_t nitems)
10052fe8fb19SBen Gras {
10062fe8fb19SBen Gras 	FTSENT **ap, *p;
10072fe8fb19SBen Gras 
10082fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
10092fe8fb19SBen Gras 	_DIAGASSERT(head != NULL);
10102fe8fb19SBen Gras 
10112fe8fb19SBen Gras 	/*
10122fe8fb19SBen Gras 	 * Construct an array of pointers to the structures and call qsort(3).
10132fe8fb19SBen Gras 	 * Reassemble the array in the order returned by qsort.  If unable to
10142fe8fb19SBen Gras 	 * sort for memory reasons, return the directory entries in their
10152fe8fb19SBen Gras 	 * current order.  Allocate enough space for the current needs plus
10162fe8fb19SBen Gras 	 * 40 so don't realloc one entry at a time.
10172fe8fb19SBen Gras 	 */
10182fe8fb19SBen Gras 	if (nitems > sp->fts_nitems) {
10192fe8fb19SBen Gras 		FTSENT **new;
10202fe8fb19SBen Gras 
10212fe8fb19SBen Gras 		new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40));
10222fe8fb19SBen Gras 		if (new == 0)
10232fe8fb19SBen Gras 			return (head);
10242fe8fb19SBen Gras 		sp->fts_array = new;
1025f14fb602SLionel Sambuc 		sp->fts_nitems = fts_nitems_truncate(nitems + 40);
10262fe8fb19SBen Gras 	}
10272fe8fb19SBen Gras 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
10282fe8fb19SBen Gras 		*ap++ = p;
10292fe8fb19SBen Gras 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *),
10302fe8fb19SBen Gras 		(int (*)(const void *, const void *))sp->fts_compar);
10312fe8fb19SBen Gras 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
10322fe8fb19SBen Gras 		ap[0]->fts_link = ap[1];
10332fe8fb19SBen Gras 	ap[0]->fts_link = NULL;
10342fe8fb19SBen Gras 	return (head);
10352fe8fb19SBen Gras }
10362fe8fb19SBen Gras 
10372fe8fb19SBen Gras static FTSENT *
fts_alloc(FTS * sp,const char * name,size_t namelen)10382fe8fb19SBen Gras fts_alloc(FTS *sp, const char *name, size_t namelen)
10392fe8fb19SBen Gras {
10402fe8fb19SBen Gras 	FTSENT *p;
10412fe8fb19SBen Gras #if defined(FTS_ALLOC_ALIGNED)
10422fe8fb19SBen Gras 	size_t len;
10432fe8fb19SBen Gras #endif
10442fe8fb19SBen Gras 
10452fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
10462fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
10472fe8fb19SBen Gras 
10482fe8fb19SBen Gras #if defined(FTS_ALLOC_ALIGNED)
10492fe8fb19SBen Gras 	/*
10502fe8fb19SBen Gras 	 * The file name is a variable length array and no stat structure is
10512fe8fb19SBen Gras 	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
10522fe8fb19SBen Gras 	 * structure, the file name and the stat structure in one chunk, but
10532fe8fb19SBen Gras 	 * be careful that the stat structure is reasonably aligned.  Since the
10542fe8fb19SBen Gras 	 * fts_name field is declared to be of size 1, the fts_name pointer is
10552fe8fb19SBen Gras 	 * namelen + 2 before the first possible address of the stat structure.
10562fe8fb19SBen Gras 	 */
10572fe8fb19SBen Gras 	len = sizeof(FTSENT) + namelen;
10582fe8fb19SBen Gras 	if (!ISSET(FTS_NOSTAT))
10592fe8fb19SBen Gras 		len += sizeof(*(p->fts_statp)) + ALIGNBYTES;
10602fe8fb19SBen Gras 	if ((p = malloc(len)) == NULL)
10612fe8fb19SBen Gras 		return (NULL);
10622fe8fb19SBen Gras 
10632fe8fb19SBen Gras 	if (!ISSET(FTS_NOSTAT))
10642fe8fb19SBen Gras 		p->fts_statp = (__fts_stat_t *)ALIGN(
10652fe8fb19SBen Gras 		    (unsigned long)(p->fts_name + namelen + 2));
10662fe8fb19SBen Gras #else
10672fe8fb19SBen Gras 	if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
10682fe8fb19SBen Gras 		return (NULL);
10692fe8fb19SBen Gras 
10702fe8fb19SBen Gras 	if (!ISSET(FTS_NOSTAT))
10712fe8fb19SBen Gras 		if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) {
10722fe8fb19SBen Gras 			free(p);
10732fe8fb19SBen Gras 			return (NULL);
10742fe8fb19SBen Gras 		}
10752fe8fb19SBen Gras #endif
10762fe8fb19SBen Gras 
10772fe8fb19SBen Gras         if (ISSET(FTS_NOSTAT))
10782fe8fb19SBen Gras                 p->fts_statp = NULL;
10792fe8fb19SBen Gras 
10802fe8fb19SBen Gras 	/* Copy the name plus the trailing NULL. */
10812fe8fb19SBen Gras 	memmove(p->fts_name, name, namelen + 1);
10822fe8fb19SBen Gras 
1083f14fb602SLionel Sambuc 	p->fts_namelen = ftsent_namelen_truncate(namelen);
10842fe8fb19SBen Gras 	p->fts_path = sp->fts_path;
10852fe8fb19SBen Gras 	p->fts_errno = 0;
10862fe8fb19SBen Gras 	p->fts_flags = 0;
10872fe8fb19SBen Gras 	p->fts_instr = FTS_NOINSTR;
10882fe8fb19SBen Gras 	p->fts_number = 0;
10892fe8fb19SBen Gras 	p->fts_pointer = NULL;
10902fe8fb19SBen Gras 	return (p);
10912fe8fb19SBen Gras }
10922fe8fb19SBen Gras 
10932fe8fb19SBen Gras static void
fts_free(FTSENT * p)10942fe8fb19SBen Gras fts_free(FTSENT *p)
10952fe8fb19SBen Gras {
10962fe8fb19SBen Gras #if !defined(FTS_ALLOC_ALIGNED)
10972fe8fb19SBen Gras 	if (p->fts_statp)
10982fe8fb19SBen Gras 		free(p->fts_statp);
10992fe8fb19SBen Gras #endif
11002fe8fb19SBen Gras 	free(p);
11012fe8fb19SBen Gras }
11022fe8fb19SBen Gras 
11032fe8fb19SBen Gras static void
fts_lfree(FTSENT * head)11042fe8fb19SBen Gras fts_lfree(FTSENT *head)
11052fe8fb19SBen Gras {
11062fe8fb19SBen Gras 	FTSENT *p;
11072fe8fb19SBen Gras 
11082fe8fb19SBen Gras 	/* XXX: head may be NULL ? */
11092fe8fb19SBen Gras 
11102fe8fb19SBen Gras 	/* Free a linked list of structures. */
11112fe8fb19SBen Gras 	while ((p = head) != NULL) {
11122fe8fb19SBen Gras 		head = head->fts_link;
11132fe8fb19SBen Gras 		fts_free(p);
11142fe8fb19SBen Gras 	}
11152fe8fb19SBen Gras }
11162fe8fb19SBen Gras 
11172fe8fb19SBen Gras static size_t
fts_pow2(size_t x)11182fe8fb19SBen Gras fts_pow2(size_t x)
11192fe8fb19SBen Gras {
11202fe8fb19SBen Gras 
11212fe8fb19SBen Gras 	x--;
11222fe8fb19SBen Gras 	x |= x>>1;
11232fe8fb19SBen Gras 	x |= x>>2;
11242fe8fb19SBen Gras 	x |= x>>4;
11252fe8fb19SBen Gras 	x |= x>>8;
11262fe8fb19SBen Gras 	x |= x>>16;
11272fe8fb19SBen Gras #if LONG_BIT > 32
11282fe8fb19SBen Gras 	x |= x>>32;
11292fe8fb19SBen Gras #endif
11302fe8fb19SBen Gras #if LONG_BIT > 64
11312fe8fb19SBen Gras 	x |= x>>64;
11322fe8fb19SBen Gras #endif
11332fe8fb19SBen Gras 	x++;
11342fe8fb19SBen Gras 	return (x);
11352fe8fb19SBen Gras }
11362fe8fb19SBen Gras 
11372fe8fb19SBen Gras /*
11382fe8fb19SBen Gras  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
11392fe8fb19SBen Gras  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
11402fe8fb19SBen Gras  * though the kernel won't resolve them.  Round up the new size to a power of 2,
11412fe8fb19SBen Gras  * so we don't realloc the path 2 bytes at a time.
11422fe8fb19SBen Gras  */
11432fe8fb19SBen Gras static int
fts_palloc(FTS * sp,size_t size)11442fe8fb19SBen Gras fts_palloc(FTS *sp, size_t size)
11452fe8fb19SBen Gras {
11462fe8fb19SBen Gras 	char *new;
11472fe8fb19SBen Gras 
11482fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
11492fe8fb19SBen Gras 
11502fe8fb19SBen Gras #ifdef __FTS_COMPAT_LENGTH
11512fe8fb19SBen Gras 	/* Protect against fts_pathlen overflow. */
11522fe8fb19SBen Gras 	if (size > USHRT_MAX + 1) {
11532fe8fb19SBen Gras 		errno = ENAMETOOLONG;
11542fe8fb19SBen Gras 		return (1);
11552fe8fb19SBen Gras 	}
11562fe8fb19SBen Gras #endif
11572fe8fb19SBen Gras 	size = fts_pow2(size);
11582fe8fb19SBen Gras 	new = realloc(sp->fts_path, size);
11592fe8fb19SBen Gras 	if (new == 0)
11602fe8fb19SBen Gras 		return (1);
11612fe8fb19SBen Gras 	sp->fts_path = new;
1162f14fb602SLionel Sambuc 	sp->fts_pathlen = fts_pathlen_truncate(size);
11632fe8fb19SBen Gras 	return (0);
11642fe8fb19SBen Gras }
11652fe8fb19SBen Gras 
11662fe8fb19SBen Gras /*
11672fe8fb19SBen Gras  * When the path is realloc'd, have to fix all of the pointers in structures
11682fe8fb19SBen Gras  * already returned.
11692fe8fb19SBen Gras  */
11702fe8fb19SBen Gras static void
fts_padjust(FTS * sp,FTSENT * head)11712fe8fb19SBen Gras fts_padjust(FTS *sp, FTSENT *head)
11722fe8fb19SBen Gras {
11732fe8fb19SBen Gras 	FTSENT *p;
11742fe8fb19SBen Gras 	char *addr;
11752fe8fb19SBen Gras 
11762fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
11772fe8fb19SBen Gras 
11782fe8fb19SBen Gras #define	ADJUST(p) do {							\
11792fe8fb19SBen Gras 	if ((p)->fts_accpath != (p)->fts_name)				\
11802fe8fb19SBen Gras 		(p)->fts_accpath =					\
11812fe8fb19SBen Gras 		    addr + ((p)->fts_accpath - (p)->fts_path);		\
11822fe8fb19SBen Gras 	(p)->fts_path = addr;						\
11832fe8fb19SBen Gras } while (/*CONSTCOND*/0)
11842fe8fb19SBen Gras 
11852fe8fb19SBen Gras 	addr = sp->fts_path;
11862fe8fb19SBen Gras 
11872fe8fb19SBen Gras 	/* Adjust the current set of children. */
11882fe8fb19SBen Gras 	for (p = sp->fts_child; p; p = p->fts_link)
11892fe8fb19SBen Gras 		ADJUST(p);
11902fe8fb19SBen Gras 
11912fe8fb19SBen Gras 	/* Adjust the rest of the tree, including the current level. */
11922fe8fb19SBen Gras 	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
11932fe8fb19SBen Gras 		ADJUST(p);
11942fe8fb19SBen Gras 		p = p->fts_link ? p->fts_link : p->fts_parent;
11952fe8fb19SBen Gras 	}
11962fe8fb19SBen Gras }
11972fe8fb19SBen Gras 
11982fe8fb19SBen Gras static size_t
fts_maxarglen(char * const * argv)11992fe8fb19SBen Gras fts_maxarglen(char * const *argv)
12002fe8fb19SBen Gras {
12012fe8fb19SBen Gras 	size_t len, max;
12022fe8fb19SBen Gras 
12032fe8fb19SBen Gras 	_DIAGASSERT(argv != NULL);
12042fe8fb19SBen Gras 
12052fe8fb19SBen Gras 	for (max = 0; *argv; ++argv)
12062fe8fb19SBen Gras 		if ((len = strlen(*argv)) > max)
12072fe8fb19SBen Gras 			max = len;
12082fe8fb19SBen Gras 	return (max + 1);
12092fe8fb19SBen Gras }
12102fe8fb19SBen Gras 
12110a6a1f1dSLionel Sambuc #if defined(__minix)
1212*6dc59ac7SLionel Sambuc #if ! HAVE_NBTOOL_CONFIG_H
1213b329f2c7SBen Gras #include <minix/dmap.h>
1214*6dc59ac7SLionel Sambuc #else
1215*6dc59ac7SLionel Sambuc #define NONE_MAJOR 0
1216*6dc59ac7SLionel Sambuc #endif
12170a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
1218b329f2c7SBen Gras 
12192fe8fb19SBen Gras /*
12202fe8fb19SBen Gras  * Change to dir specified by fd or p->fts_accpath without getting
12212fe8fb19SBen Gras  * tricked by someone changing the world out from underneath us.
12222fe8fb19SBen Gras  * Assumes p->fts_dev and p->fts_ino are filled in.
12232fe8fb19SBen Gras  */
12242fe8fb19SBen Gras static int
fts_safe_changedir(const FTS * sp,const FTSENT * p,int fd,const char * path)12252fe8fb19SBen Gras fts_safe_changedir(const FTS *sp, const FTSENT *p, int fd, const char *path)
12262fe8fb19SBen Gras {
12272fe8fb19SBen Gras 	int oldfd = fd, ret = -1;
12282fe8fb19SBen Gras 	__fts_stat_t sb;
12292fe8fb19SBen Gras 
12302fe8fb19SBen Gras 	if (ISSET(FTS_NOCHDIR))
12312fe8fb19SBen Gras 		return 0;
12322fe8fb19SBen Gras 
12330a6a1f1dSLionel Sambuc 	if (oldfd < 0 && (fd = open(path, O_RDONLY | O_CLOEXEC)) == -1)
12342fe8fb19SBen Gras 		return -1;
12352fe8fb19SBen Gras 
12362fe8fb19SBen Gras 	if (fstat(fd, &sb) == -1)
12372fe8fb19SBen Gras 		goto bail;
12382fe8fb19SBen Gras 
12390a6a1f1dSLionel Sambuc #if defined(__minix)
1240b329f2c7SBen Gras 	/*
1241b329f2c7SBen Gras 	 * Skip the safety check on file systems where inodes are not static.
1242b329f2c7SBen Gras 	 * On such file systems, a file may legitimately be assigned a new
1243b329f2c7SBen Gras 	 * inode number due to being deleted and regenerated while we are
1244b329f2c7SBen Gras 	 * running.  This behavior is not POSIX compliant, but necessary for
1245b329f2c7SBen Gras 	 * certain types of file systems.  Currently, we assume that this
1246b329f2c7SBen Gras 	 * applies to all (and only) file systems that are not device backed.
1247b329f2c7SBen Gras 	 * In the future, we may have to obtain an appropriate flag through
1248b329f2c7SBen Gras 	 * statvfs(3) instead.
1249b329f2c7SBen Gras 	 */
1250b329f2c7SBen Gras 	if ((sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev)
1251b329f2c7SBen Gras 		&& major(sb.st_dev) != NONE_MAJOR) {
1252b329f2c7SBen Gras #else
12532fe8fb19SBen Gras 	if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
12540a6a1f1dSLionel Sambuc #endif /*  defined(__minix) */
12552fe8fb19SBen Gras 		errno = ENOENT;
12562fe8fb19SBen Gras 		goto bail;
12572fe8fb19SBen Gras 	}
12582fe8fb19SBen Gras 
12592fe8fb19SBen Gras 	ret = fchdir(fd);
12602fe8fb19SBen Gras 
12612fe8fb19SBen Gras bail:
12622fe8fb19SBen Gras 	if (oldfd < 0) {
12632fe8fb19SBen Gras 		int save_errno = errno;
12642fe8fb19SBen Gras 		(void)close(fd);
12652fe8fb19SBen Gras 		errno = save_errno;
12662fe8fb19SBen Gras 	}
12672fe8fb19SBen Gras 	return ret;
12682fe8fb19SBen Gras }
1269