xref: /freebsd/lib/libc/gen/fts.c (revision 5abef29833d32d257a20b61732993987dd2a6056)
158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
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.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
301a9b5f47SBrian Feldman  *
311a9b5f47SBrian Feldman  * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
34d201fe46SDaniel Eischen #include "namespace.h"
3558f0484fSRodney W. Grimes #include <sys/param.h>
3699ca5b88SPeter Edwards #include <sys/mount.h>
3707dee1a7SPeter Edwards #include <sys/stat.h>
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <dirent.h>
4058f0484fSRodney W. Grimes #include <errno.h>
4158f0484fSRodney W. Grimes #include <fcntl.h>
4258f0484fSRodney W. Grimes #include <fts.h>
4379679a18SDag-Erling Smørgrav #include <stdalign.h>
4458f0484fSRodney W. Grimes #include <stdlib.h>
4558f0484fSRodney W. Grimes #include <string.h>
4658f0484fSRodney W. Grimes #include <unistd.h>
47d201fe46SDaniel Eischen #include "un-namespace.h"
4858f0484fSRodney W. Grimes 
490bb2aabfSGleb Kurtsou #include "gen-private.h"
500bb2aabfSGleb Kurtsou 
5148aaad5fSYaroslav Tykhiy static FTSENT	*fts_alloc(FTS *, char *, size_t);
52b231cb39SDavid E. O'Brien static FTSENT	*fts_build(FTS *, int);
53b231cb39SDavid E. O'Brien static void	 fts_lfree(FTSENT *);
54b231cb39SDavid E. O'Brien static void	 fts_load(FTS *, FTSENT *);
55b231cb39SDavid E. O'Brien static size_t	 fts_maxarglen(char * const *);
56b231cb39SDavid E. O'Brien static void	 fts_padjust(FTS *, FTSENT *);
57b231cb39SDavid E. O'Brien static int	 fts_palloc(FTS *, size_t);
5848aaad5fSYaroslav Tykhiy static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
5954cc1f88SJilles Tjoelker static int	 fts_stat(FTS *, FTSENT *, int, int);
60b231cb39SDavid E. O'Brien static int	 fts_safe_changedir(FTS *, FTSENT *, int, char *);
6107dee1a7SPeter Edwards static int	 fts_ufslinks(FTS *, const FTSENT *);
6258f0484fSRodney W. Grimes 
6351295a4dSJordan K. Hubbard #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
6458f0484fSRodney W. Grimes 
659a91f1ccSWarner Losh #define	CLR(opt)	(sp->fts_options &= ~(opt))
669a91f1ccSWarner Losh #define	ISSET(opt)	(sp->fts_options & (opt))
679a91f1ccSWarner Losh #define	SET(opt)	(sp->fts_options |= (opt))
6858f0484fSRodney W. Grimes 
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 
7699ca5b88SPeter Edwards /*
7707dee1a7SPeter Edwards  * Internal representation of an FTS, including extra implementation
7807dee1a7SPeter Edwards  * details.  The FTS returned from fts_open points to this structure's
7907dee1a7SPeter Edwards  * ftsp_fts member (and can be cast to an _fts_private as required)
8099ca5b88SPeter Edwards  */
8199ca5b88SPeter Edwards struct _fts_private {
8299ca5b88SPeter Edwards 	FTS		ftsp_fts;
8399ca5b88SPeter Edwards 	struct statfs	ftsp_statfs;
8499ca5b88SPeter Edwards 	dev_t		ftsp_dev;
8599ca5b88SPeter Edwards 	int		ftsp_linksreliable;
8699ca5b88SPeter Edwards };
8799ca5b88SPeter Edwards 
8899ca5b88SPeter Edwards /*
8907dee1a7SPeter Edwards  * The "FTS_NOSTAT" option can avoid a lot of calls to stat(2) if it
9007dee1a7SPeter Edwards  * knows that a directory could not possibly have subdirectories.  This
9107dee1a7SPeter Edwards  * is decided by looking at the link count: a subdirectory would
9207dee1a7SPeter Edwards  * increment its parent's link count by virtue of its own ".." entry.
9307dee1a7SPeter Edwards  * This assumption only holds for UFS-like filesystems that implement
9407dee1a7SPeter Edwards  * links and directories this way, so we must punt for others.
9599ca5b88SPeter Edwards  */
9699ca5b88SPeter Edwards 
9799ca5b88SPeter Edwards static const char *ufslike_filesystems[] = {
9899ca5b88SPeter Edwards 	"ufs",
99681ff708SPawel Jakub Dawidek 	"zfs",
10099ca5b88SPeter Edwards 	"nfs",
10199ca5b88SPeter Edwards 	"ext2fs",
10299ca5b88SPeter Edwards 	0
10399ca5b88SPeter Edwards };
10499ca5b88SPeter Edwards 
10558f0484fSRodney W. Grimes FTS *
fts_open(char * const * argv,int options,int (* compar)(const FTSENT * const *,const FTSENT * const *))10655b6b759SCraig Rodrigues fts_open(char * const *argv, int options,
10755b6b759SCraig Rodrigues     int (*compar)(const FTSENT * const *, const FTSENT * const *))
10858f0484fSRodney W. Grimes {
10999ca5b88SPeter Edwards 	struct _fts_private *priv;
110b231cb39SDavid E. O'Brien 	FTS *sp;
111b231cb39SDavid E. O'Brien 	FTSENT *p, *root;
11258f0484fSRodney W. Grimes 	FTSENT *parent, *tmp;
11348aaad5fSYaroslav Tykhiy 	size_t len, nitems;
11458f0484fSRodney W. Grimes 
11558f0484fSRodney W. Grimes 	/* Options check. */
11658f0484fSRodney W. Grimes 	if (options & ~FTS_OPTIONMASK) {
11758f0484fSRodney W. Grimes 		errno = EINVAL;
11858f0484fSRodney W. Grimes 		return (NULL);
11958f0484fSRodney W. Grimes 	}
12058f0484fSRodney W. Grimes 
1218b8a820dSXin LI 	/* fts_open() requires at least one path */
1228b8a820dSXin LI 	if (*argv == NULL) {
1238b8a820dSXin LI 		errno = EINVAL;
1248b8a820dSXin LI 		return (NULL);
1258b8a820dSXin LI 	}
1268b8a820dSXin LI 
127a1acdbf0SBruce Evans 	/* Allocate/initialize the stream. */
1280dfbbb33SXin LI 	if ((priv = calloc(1, sizeof(*priv))) == NULL)
12958f0484fSRodney W. Grimes 		return (NULL);
13099ca5b88SPeter Edwards 	sp = &priv->ftsp_fts;
13158f0484fSRodney W. Grimes 	sp->fts_compar = compar;
13258f0484fSRodney W. Grimes 	sp->fts_options = options;
13358f0484fSRodney W. Grimes 
13458f0484fSRodney W. Grimes 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
13558f0484fSRodney W. Grimes 	if (ISSET(FTS_LOGICAL))
13658f0484fSRodney W. Grimes 		SET(FTS_NOCHDIR);
13758f0484fSRodney W. Grimes 
13858f0484fSRodney W. Grimes 	/*
13958f0484fSRodney W. Grimes 	 * Start out with 1K of path space, and enough, in any case,
14058f0484fSRodney W. Grimes 	 * to hold the user's paths.
14158f0484fSRodney W. Grimes 	 */
14258f0484fSRodney W. Grimes 	if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
14358f0484fSRodney W. Grimes 		goto mem1;
14458f0484fSRodney W. Grimes 
14558f0484fSRodney W. Grimes 	/* Allocate/initialize root's parent. */
14658f0484fSRodney W. Grimes 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
14758f0484fSRodney W. Grimes 		goto mem2;
14858f0484fSRodney W. Grimes 	parent->fts_level = FTS_ROOTPARENTLEVEL;
14958f0484fSRodney W. Grimes 
15005231117SPedro F. Giffuni 	/* Shush, GCC. */
15105231117SPedro F. Giffuni 	tmp = NULL;
15205231117SPedro F. Giffuni 
15358f0484fSRodney W. Grimes 	/* Allocate/initialize root(s). */
1548ca72379SBrian Feldman 	for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
155c8a17392SJilles Tjoelker 		len = strlen(*argv);
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes 		p = fts_alloc(sp, *argv, len);
15858f0484fSRodney W. Grimes 		p->fts_level = FTS_ROOTLEVEL;
15958f0484fSRodney W. Grimes 		p->fts_parent = parent;
16058f0484fSRodney W. Grimes 		p->fts_accpath = p->fts_name;
16154cc1f88SJilles Tjoelker 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW), -1);
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes 		/* Command-line "." and ".." are real directories. */
16458f0484fSRodney W. Grimes 		if (p->fts_info == FTS_DOT)
16558f0484fSRodney W. Grimes 			p->fts_info = FTS_D;
16658f0484fSRodney W. Grimes 
16758f0484fSRodney W. Grimes 		/*
16858f0484fSRodney W. Grimes 		 * If comparison routine supplied, traverse in sorted
16958f0484fSRodney W. Grimes 		 * order; otherwise traverse in the order specified.
17058f0484fSRodney W. Grimes 		 */
17158f0484fSRodney W. Grimes 		if (compar) {
17258f0484fSRodney W. Grimes 			p->fts_link = root;
17358f0484fSRodney W. Grimes 			root = p;
17458f0484fSRodney W. Grimes 		} else {
17558f0484fSRodney W. Grimes 			p->fts_link = NULL;
17658f0484fSRodney W. Grimes 			if (root == NULL)
17758f0484fSRodney W. Grimes 				tmp = root = p;
17858f0484fSRodney W. Grimes 			else {
17958f0484fSRodney W. Grimes 				tmp->fts_link = p;
18058f0484fSRodney W. Grimes 				tmp = p;
18158f0484fSRodney W. Grimes 			}
18258f0484fSRodney W. Grimes 		}
18358f0484fSRodney W. Grimes 	}
18458f0484fSRodney W. Grimes 	if (compar && nitems > 1)
18558f0484fSRodney W. Grimes 		root = fts_sort(sp, root, nitems);
18658f0484fSRodney W. Grimes 
18758f0484fSRodney W. Grimes 	/*
18858f0484fSRodney W. Grimes 	 * Allocate a dummy pointer and make fts_read think that we've just
18958f0484fSRodney W. Grimes 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
19058f0484fSRodney W. Grimes 	 * so that everything about the "current" node is ignored.
19158f0484fSRodney W. Grimes 	 */
19258f0484fSRodney W. Grimes 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
19358f0484fSRodney W. Grimes 		goto mem3;
19458f0484fSRodney W. Grimes 	sp->fts_cur->fts_link = root;
19558f0484fSRodney W. Grimes 	sp->fts_cur->fts_info = FTS_INIT;
19658f0484fSRodney W. Grimes 
19758f0484fSRodney W. Grimes 	/*
1981a9b5f47SBrian Feldman 	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
19958f0484fSRodney W. Grimes 	 * that we can get back here; this could be avoided for some paths,
20058f0484fSRodney W. Grimes 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
20158f0484fSRodney W. Grimes 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
20258f0484fSRodney W. Grimes 	 * descriptor we run anyway, just more slowly.
20358f0484fSRodney W. Grimes 	 */
2046b422721SJilles Tjoelker 	if (!ISSET(FTS_NOCHDIR) &&
2056b422721SJilles Tjoelker 	    (sp->fts_rfd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
20658f0484fSRodney W. Grimes 		SET(FTS_NOCHDIR);
20758f0484fSRodney W. Grimes 
20858f0484fSRodney W. Grimes 	return (sp);
20958f0484fSRodney W. Grimes 
21058f0484fSRodney W. Grimes mem3:	fts_lfree(root);
21158f0484fSRodney W. Grimes 	free(parent);
21258f0484fSRodney W. Grimes mem2:	free(sp->fts_path);
21358f0484fSRodney W. Grimes mem1:	free(sp);
21458f0484fSRodney W. Grimes 	return (NULL);
21558f0484fSRodney W. Grimes }
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes static void
fts_load(FTS * sp,FTSENT * p)2183a12c046SXin LI fts_load(FTS *sp, FTSENT *p)
21958f0484fSRodney W. Grimes {
22048aaad5fSYaroslav Tykhiy 	size_t len;
221b231cb39SDavid E. O'Brien 	char *cp;
22258f0484fSRodney W. Grimes 
22358f0484fSRodney W. Grimes 	/*
22458f0484fSRodney W. Grimes 	 * Load the stream structure for the next traversal.  Since we don't
22558f0484fSRodney W. Grimes 	 * actually enter the directory until after the preorder visit, set
22658f0484fSRodney W. Grimes 	 * the fts_accpath field specially so the chdir gets done to the right
22758f0484fSRodney W. Grimes 	 * place and the user can access the first node.  From fts_open it's
22858f0484fSRodney W. Grimes 	 * known that the path will fit.
22958f0484fSRodney W. Grimes 	 */
23058f0484fSRodney W. Grimes 	len = p->fts_pathlen = p->fts_namelen;
23158f0484fSRodney W. Grimes 	memmove(sp->fts_path, p->fts_name, len + 1);
23258f0484fSRodney W. Grimes 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
23358f0484fSRodney W. Grimes 		len = strlen(++cp);
23458f0484fSRodney W. Grimes 		memmove(p->fts_name, cp, len + 1);
23558f0484fSRodney W. Grimes 		p->fts_namelen = len;
23658f0484fSRodney W. Grimes 	}
23758f0484fSRodney W. Grimes 	p->fts_accpath = p->fts_path = sp->fts_path;
23858f0484fSRodney W. Grimes 	sp->fts_dev = p->fts_dev;
23958f0484fSRodney W. Grimes }
24058f0484fSRodney W. Grimes 
24158f0484fSRodney W. Grimes int
fts_close(FTS * sp)2423a12c046SXin LI fts_close(FTS *sp)
24358f0484fSRodney W. Grimes {
244b231cb39SDavid E. O'Brien 	FTSENT *freep, *p;
24558f0484fSRodney W. Grimes 	int saved_errno;
24658f0484fSRodney W. Grimes 
24758f0484fSRodney W. Grimes 	/*
24858f0484fSRodney W. Grimes 	 * This still works if we haven't read anything -- the dummy structure
24958f0484fSRodney W. Grimes 	 * points to the root list, so we step through to the end of the root
25058f0484fSRodney W. Grimes 	 * list which has a valid parent pointer.
25158f0484fSRodney W. Grimes 	 */
25258f0484fSRodney W. Grimes 	if (sp->fts_cur) {
25358f0484fSRodney W. Grimes 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
25458f0484fSRodney W. Grimes 			freep = p;
2558ca72379SBrian Feldman 			p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
25658f0484fSRodney W. Grimes 			free(freep);
25758f0484fSRodney W. Grimes 		}
25858f0484fSRodney W. Grimes 		free(p);
25958f0484fSRodney W. Grimes 	}
26058f0484fSRodney W. Grimes 
26158f0484fSRodney W. Grimes 	/* Free up child linked list, sort array, path buffer. */
26258f0484fSRodney W. Grimes 	if (sp->fts_child)
26358f0484fSRodney W. Grimes 		fts_lfree(sp->fts_child);
26458f0484fSRodney W. Grimes 	if (sp->fts_array)
26558f0484fSRodney W. Grimes 		free(sp->fts_array);
26658f0484fSRodney W. Grimes 	free(sp->fts_path);
26758f0484fSRodney W. Grimes 
26858f0484fSRodney W. Grimes 	/* Return to original directory, save errno if necessary. */
26958f0484fSRodney W. Grimes 	if (!ISSET(FTS_NOCHDIR)) {
27058f0484fSRodney W. Grimes 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
2719233c4d9SJason Evans 		(void)_close(sp->fts_rfd);
27258f0484fSRodney W. Grimes 
27358f0484fSRodney W. Grimes 		/* Set errno and return. */
2741a9b5f47SBrian Feldman 		if (saved_errno != 0) {
2758a507b98SPoul-Henning Kamp 			/* Free up the stream pointer. */
2768a507b98SPoul-Henning Kamp 			free(sp);
27758f0484fSRodney W. Grimes 			errno = saved_errno;
27858f0484fSRodney W. Grimes 			return (-1);
27958f0484fSRodney W. Grimes 		}
2801a9b5f47SBrian Feldman 	}
2811a9b5f47SBrian Feldman 
2828a507b98SPoul-Henning Kamp 	/* Free up the stream pointer. */
2838a507b98SPoul-Henning Kamp 	free(sp);
28458f0484fSRodney W. Grimes 	return (0);
28558f0484fSRodney W. Grimes }
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes /*
2889a91f1ccSWarner Losh  * Special case of "/" at the end of the path so that slashes aren't
2899a91f1ccSWarner Losh  * appended which would cause paths to be written as "....//foo".
29058f0484fSRodney W. Grimes  */
29158f0484fSRodney W. Grimes #define	NAPPEND(p)							\
2929a91f1ccSWarner Losh 	(p->fts_path[p->fts_pathlen - 1] == '/'				\
2939a91f1ccSWarner Losh 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
29458f0484fSRodney W. Grimes 
29558f0484fSRodney W. Grimes FTSENT *
fts_read(FTS * sp)2963a12c046SXin LI fts_read(FTS *sp)
29758f0484fSRodney W. Grimes {
298b231cb39SDavid E. O'Brien 	FTSENT *p, *tmp;
299b231cb39SDavid E. O'Brien 	int instr;
300b231cb39SDavid E. O'Brien 	char *t;
30158f0484fSRodney W. Grimes 	int saved_errno;
30258f0484fSRodney W. Grimes 
30358f0484fSRodney W. Grimes 	/* If finished or unrecoverable error, return NULL. */
30458f0484fSRodney W. Grimes 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
30558f0484fSRodney W. Grimes 		return (NULL);
30658f0484fSRodney W. Grimes 
30758f0484fSRodney W. Grimes 	/* Set current node pointer. */
30858f0484fSRodney W. Grimes 	p = sp->fts_cur;
30958f0484fSRodney W. Grimes 
31058f0484fSRodney W. Grimes 	/* Save and zero out user instructions. */
31158f0484fSRodney W. Grimes 	instr = p->fts_instr;
31258f0484fSRodney W. Grimes 	p->fts_instr = FTS_NOINSTR;
31358f0484fSRodney W. Grimes 
31458f0484fSRodney W. Grimes 	/* Any type of file may be re-visited; re-stat and re-turn. */
31558f0484fSRodney W. Grimes 	if (instr == FTS_AGAIN) {
31654cc1f88SJilles Tjoelker 		p->fts_info = fts_stat(sp, p, 0, -1);
31758f0484fSRodney W. Grimes 		return (p);
31858f0484fSRodney W. Grimes 	}
31958f0484fSRodney W. Grimes 
32058f0484fSRodney W. Grimes 	/*
32158f0484fSRodney W. Grimes 	 * Following a symlink -- SLNONE test allows application to see
32258f0484fSRodney W. Grimes 	 * SLNONE and recover.  If indirecting through a symlink, have
32358f0484fSRodney W. Grimes 	 * keep a pointer to current location.  If unable to get that
32458f0484fSRodney W. Grimes 	 * pointer, follow fails.
32558f0484fSRodney W. Grimes 	 */
32658f0484fSRodney W. Grimes 	if (instr == FTS_FOLLOW &&
32758f0484fSRodney W. Grimes 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
32854cc1f88SJilles Tjoelker 		p->fts_info = fts_stat(sp, p, 1, -1);
3291a9b5f47SBrian Feldman 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
3306b422721SJilles Tjoelker 			if ((p->fts_symfd = _open(".", O_RDONLY | O_CLOEXEC,
3316b422721SJilles Tjoelker 			    0)) < 0) {
33258f0484fSRodney W. Grimes 				p->fts_errno = errno;
33358f0484fSRodney W. Grimes 				p->fts_info = FTS_ERR;
33458f0484fSRodney W. Grimes 			} else
33558f0484fSRodney W. Grimes 				p->fts_flags |= FTS_SYMFOLLOW;
3361a9b5f47SBrian Feldman 		}
33758f0484fSRodney W. Grimes 		return (p);
33858f0484fSRodney W. Grimes 	}
33958f0484fSRodney W. Grimes 
34058f0484fSRodney W. Grimes 	/* Directory in pre-order. */
34158f0484fSRodney W. Grimes 	if (p->fts_info == FTS_D) {
34258f0484fSRodney W. Grimes 		/* If skipped or crossed mount point, do post-order visit. */
34358f0484fSRodney W. Grimes 		if (instr == FTS_SKIP ||
34451295a4dSJordan K. Hubbard 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
34558f0484fSRodney W. Grimes 			if (p->fts_flags & FTS_SYMFOLLOW)
3469233c4d9SJason Evans 				(void)_close(p->fts_symfd);
34758f0484fSRodney W. Grimes 			if (sp->fts_child) {
34858f0484fSRodney W. Grimes 				fts_lfree(sp->fts_child);
34958f0484fSRodney W. Grimes 				sp->fts_child = NULL;
35058f0484fSRodney W. Grimes 			}
35158f0484fSRodney W. Grimes 			p->fts_info = FTS_DP;
35258f0484fSRodney W. Grimes 			return (p);
35358f0484fSRodney W. Grimes 		}
35458f0484fSRodney W. Grimes 
35558f0484fSRodney W. Grimes 		/* Rebuild if only read the names and now traversing. */
3568ca72379SBrian Feldman 		if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
3579a91f1ccSWarner Losh 			CLR(FTS_NAMEONLY);
35858f0484fSRodney W. Grimes 			fts_lfree(sp->fts_child);
35958f0484fSRodney W. Grimes 			sp->fts_child = NULL;
36058f0484fSRodney W. Grimes 		}
36158f0484fSRodney W. Grimes 
36258f0484fSRodney W. Grimes 		/*
36358f0484fSRodney W. Grimes 		 * Cd to the subdirectory.
36458f0484fSRodney W. Grimes 		 *
36558f0484fSRodney W. Grimes 		 * If have already read and now fail to chdir, whack the list
36658f0484fSRodney W. Grimes 		 * to make the names come out right, and set the parent errno
36758f0484fSRodney W. Grimes 		 * so the application will eventually get an error condition.
36858f0484fSRodney W. Grimes 		 * Set the FTS_DONTCHDIR flag so that when we logically change
36958f0484fSRodney W. Grimes 		 * directories back to the parent we don't do a chdir.
37058f0484fSRodney W. Grimes 		 *
37158f0484fSRodney W. Grimes 		 * If haven't read do so.  If the read fails, fts_build sets
37258f0484fSRodney W. Grimes 		 * FTS_STOP or the fts_info field of the node.
37358f0484fSRodney W. Grimes 		 */
3748ca72379SBrian Feldman 		if (sp->fts_child != NULL) {
37593a85518SKris Kennaway 			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
37658f0484fSRodney W. Grimes 				p->fts_errno = errno;
37758f0484fSRodney W. Grimes 				p->fts_flags |= FTS_DONTCHDIR;
3788ca72379SBrian Feldman 				for (p = sp->fts_child; p != NULL;
3798ca72379SBrian Feldman 				    p = p->fts_link)
38058f0484fSRodney W. Grimes 					p->fts_accpath =
38158f0484fSRodney W. Grimes 					    p->fts_parent->fts_accpath;
38258f0484fSRodney W. Grimes 			}
38358f0484fSRodney W. Grimes 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
38458f0484fSRodney W. Grimes 			if (ISSET(FTS_STOP))
38558f0484fSRodney W. Grimes 				return (NULL);
38658f0484fSRodney W. Grimes 			return (p);
38758f0484fSRodney W. Grimes 		}
38858f0484fSRodney W. Grimes 		p = sp->fts_child;
38958f0484fSRodney W. Grimes 		sp->fts_child = NULL;
39058f0484fSRodney W. Grimes 		goto name;
39158f0484fSRodney W. Grimes 	}
39258f0484fSRodney W. Grimes 
39358f0484fSRodney W. Grimes 	/* Move to the next node on this level. */
39458f0484fSRodney W. Grimes next:	tmp = p;
3958ca72379SBrian Feldman 	if ((p = p->fts_link) != NULL) {
39658f0484fSRodney W. Grimes 		/*
3971a9b5f47SBrian Feldman 		 * If reached the top, return to the original directory (or
3981a9b5f47SBrian Feldman 		 * the root of the tree), and load the paths for the next root.
39958f0484fSRodney W. Grimes 		 */
40058f0484fSRodney W. Grimes 		if (p->fts_level == FTS_ROOTLEVEL) {
4019a91f1ccSWarner Losh 			if (FCHDIR(sp, sp->fts_rfd)) {
40258f0484fSRodney W. Grimes 				SET(FTS_STOP);
40358f0484fSRodney W. Grimes 				return (NULL);
40458f0484fSRodney W. Grimes 			}
4055de8a0fdSJilles Tjoelker 			free(tmp);
40658f0484fSRodney W. Grimes 			fts_load(sp, p);
40758f0484fSRodney W. Grimes 			return (sp->fts_cur = p);
40858f0484fSRodney W. Grimes 		}
40958f0484fSRodney W. Grimes 
41058f0484fSRodney W. Grimes 		/*
41158f0484fSRodney W. Grimes 		 * User may have called fts_set on the node.  If skipped,
41258f0484fSRodney W. Grimes 		 * ignore.  If followed, get a file descriptor so we can
41358f0484fSRodney W. Grimes 		 * get back if necessary.
41458f0484fSRodney W. Grimes 		 */
4155de8a0fdSJilles Tjoelker 		if (p->fts_instr == FTS_SKIP) {
4165de8a0fdSJilles Tjoelker 			free(tmp);
41758f0484fSRodney W. Grimes 			goto next;
4185de8a0fdSJilles Tjoelker 		}
41958f0484fSRodney W. Grimes 		if (p->fts_instr == FTS_FOLLOW) {
42054cc1f88SJilles Tjoelker 			p->fts_info = fts_stat(sp, p, 1, -1);
4211a9b5f47SBrian Feldman 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
42258f0484fSRodney W. Grimes 				if ((p->fts_symfd =
4236b422721SJilles Tjoelker 				    _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) {
42458f0484fSRodney W. Grimes 					p->fts_errno = errno;
42558f0484fSRodney W. Grimes 					p->fts_info = FTS_ERR;
42658f0484fSRodney W. Grimes 				} else
42758f0484fSRodney W. Grimes 					p->fts_flags |= FTS_SYMFOLLOW;
4281a9b5f47SBrian Feldman 			}
42958f0484fSRodney W. Grimes 			p->fts_instr = FTS_NOINSTR;
43058f0484fSRodney W. Grimes 		}
43158f0484fSRodney W. Grimes 
4325de8a0fdSJilles Tjoelker 		free(tmp);
4335de8a0fdSJilles Tjoelker 
43458f0484fSRodney W. Grimes name:		t = sp->fts_path + NAPPEND(p->fts_parent);
43558f0484fSRodney W. Grimes 		*t++ = '/';
43658f0484fSRodney W. Grimes 		memmove(t, p->fts_name, p->fts_namelen + 1);
43758f0484fSRodney W. Grimes 		return (sp->fts_cur = p);
43858f0484fSRodney W. Grimes 	}
43958f0484fSRodney W. Grimes 
44058f0484fSRodney W. Grimes 	/* Move up to the parent node. */
44158f0484fSRodney W. Grimes 	p = tmp->fts_parent;
44258f0484fSRodney W. Grimes 
44358f0484fSRodney W. Grimes 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
44458f0484fSRodney W. Grimes 		/*
44558f0484fSRodney W. Grimes 		 * Done; free everything up and set errno to 0 so the user
44658f0484fSRodney W. Grimes 		 * can distinguish between error and EOF.
44758f0484fSRodney W. Grimes 		 */
4485de8a0fdSJilles Tjoelker 		free(tmp);
44958f0484fSRodney W. Grimes 		free(p);
45058f0484fSRodney W. Grimes 		errno = 0;
45158f0484fSRodney W. Grimes 		return (sp->fts_cur = NULL);
45258f0484fSRodney W. Grimes 	}
45358f0484fSRodney W. Grimes 
4541a9b5f47SBrian Feldman 	/* NUL terminate the pathname. */
45558f0484fSRodney W. Grimes 	sp->fts_path[p->fts_pathlen] = '\0';
45658f0484fSRodney W. Grimes 
45758f0484fSRodney W. Grimes 	/*
45858f0484fSRodney W. Grimes 	 * Return to the parent directory.  If at a root node or came through
45958f0484fSRodney W. Grimes 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
46058f0484fSRodney W. Grimes 	 * one directory.
46158f0484fSRodney W. Grimes 	 */
46258f0484fSRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL) {
4639a91f1ccSWarner Losh 		if (FCHDIR(sp, sp->fts_rfd)) {
46458f0484fSRodney W. Grimes 			SET(FTS_STOP);
46558f0484fSRodney W. Grimes 			return (NULL);
46658f0484fSRodney W. Grimes 		}
46758f0484fSRodney W. Grimes 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
46858f0484fSRodney W. Grimes 		if (FCHDIR(sp, p->fts_symfd)) {
46958f0484fSRodney W. Grimes 			saved_errno = errno;
4709233c4d9SJason Evans 			(void)_close(p->fts_symfd);
47158f0484fSRodney W. Grimes 			errno = saved_errno;
47258f0484fSRodney W. Grimes 			SET(FTS_STOP);
47358f0484fSRodney W. Grimes 			return (NULL);
47458f0484fSRodney W. Grimes 		}
4759233c4d9SJason Evans 		(void)_close(p->fts_symfd);
476fdeb0156SKris Kennaway 	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
47793a85518SKris Kennaway 	    fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
47858f0484fSRodney W. Grimes 		SET(FTS_STOP);
47958f0484fSRodney W. Grimes 		return (NULL);
48058f0484fSRodney W. Grimes 	}
4815de8a0fdSJilles Tjoelker 	free(tmp);
48258f0484fSRodney W. Grimes 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
48358f0484fSRodney W. Grimes 	return (sp->fts_cur = p);
48458f0484fSRodney W. Grimes }
48558f0484fSRodney W. Grimes 
48658f0484fSRodney W. Grimes /*
48758f0484fSRodney W. Grimes  * Fts_set takes the stream as an argument although it's not used in this
48858f0484fSRodney W. Grimes  * implementation; it would be necessary if anyone wanted to add global
48958f0484fSRodney W. Grimes  * semantics to fts using fts_set.  An error return is allowed for similar
49058f0484fSRodney W. Grimes  * reasons.
49158f0484fSRodney W. Grimes  */
49258f0484fSRodney W. Grimes /* ARGSUSED */
49358f0484fSRodney W. Grimes int
fts_set(FTS * sp,FTSENT * p,int instr)4943a12c046SXin LI fts_set(FTS *sp, FTSENT *p, int instr)
49558f0484fSRodney W. Grimes {
4968ca72379SBrian Feldman 	if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
49758f0484fSRodney W. Grimes 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
49858f0484fSRodney W. Grimes 		errno = EINVAL;
49958f0484fSRodney W. Grimes 		return (1);
50058f0484fSRodney W. Grimes 	}
50158f0484fSRodney W. Grimes 	p->fts_instr = instr;
50258f0484fSRodney W. Grimes 	return (0);
50358f0484fSRodney W. Grimes }
50458f0484fSRodney W. Grimes 
50558f0484fSRodney W. Grimes FTSENT *
fts_children(FTS * sp,int instr)5063a12c046SXin LI fts_children(FTS *sp, int instr)
50758f0484fSRodney W. Grimes {
508b231cb39SDavid E. O'Brien 	FTSENT *p;
509764a9bbeSJilles Tjoelker 	int fd, rc, serrno;
51058f0484fSRodney W. Grimes 
5118ca72379SBrian Feldman 	if (instr != 0 && instr != FTS_NAMEONLY) {
51258f0484fSRodney W. Grimes 		errno = EINVAL;
51358f0484fSRodney W. Grimes 		return (NULL);
51458f0484fSRodney W. Grimes 	}
51558f0484fSRodney W. Grimes 
51658f0484fSRodney W. Grimes 	/* Set current node pointer. */
51758f0484fSRodney W. Grimes 	p = sp->fts_cur;
51858f0484fSRodney W. Grimes 
51958f0484fSRodney W. Grimes 	/*
52058f0484fSRodney W. Grimes 	 * Errno set to 0 so user can distinguish empty directory from
52158f0484fSRodney W. Grimes 	 * an error.
52258f0484fSRodney W. Grimes 	 */
52358f0484fSRodney W. Grimes 	errno = 0;
52458f0484fSRodney W. Grimes 
52558f0484fSRodney W. Grimes 	/* Fatal errors stop here. */
52658f0484fSRodney W. Grimes 	if (ISSET(FTS_STOP))
52758f0484fSRodney W. Grimes 		return (NULL);
52858f0484fSRodney W. Grimes 
52958f0484fSRodney W. Grimes 	/* Return logical hierarchy of user's arguments. */
53058f0484fSRodney W. Grimes 	if (p->fts_info == FTS_INIT)
53158f0484fSRodney W. Grimes 		return (p->fts_link);
53258f0484fSRodney W. Grimes 
53358f0484fSRodney W. Grimes 	/*
53458f0484fSRodney W. Grimes 	 * If not a directory being visited in pre-order, stop here.  Could
53558f0484fSRodney W. Grimes 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
53658f0484fSRodney W. Grimes 	 * same effect is available with FTS_AGAIN.
53758f0484fSRodney W. Grimes 	 */
53858f0484fSRodney W. Grimes 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
53958f0484fSRodney W. Grimes 		return (NULL);
54058f0484fSRodney W. Grimes 
54158f0484fSRodney W. Grimes 	/* Free up any previous child list. */
5428ca72379SBrian Feldman 	if (sp->fts_child != NULL)
54358f0484fSRodney W. Grimes 		fts_lfree(sp->fts_child);
54458f0484fSRodney W. Grimes 
54558f0484fSRodney W. Grimes 	if (instr == FTS_NAMEONLY) {
5469a91f1ccSWarner Losh 		SET(FTS_NAMEONLY);
54758f0484fSRodney W. Grimes 		instr = BNAMES;
54858f0484fSRodney W. Grimes 	} else
54958f0484fSRodney W. Grimes 		instr = BCHILD;
55058f0484fSRodney W. Grimes 
55158f0484fSRodney W. Grimes 	/*
55258f0484fSRodney W. Grimes 	 * If using chdir on a relative path and called BEFORE fts_read does
55358f0484fSRodney W. Grimes 	 * its chdir to the root of a traversal, we can lose -- we need to
55458f0484fSRodney W. Grimes 	 * chdir into the subdirectory, and we don't know where the current
55558f0484fSRodney W. Grimes 	 * directory is, so we can't get back so that the upcoming chdir by
55658f0484fSRodney W. Grimes 	 * fts_read will work.
55758f0484fSRodney W. Grimes 	 */
55858f0484fSRodney W. Grimes 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
55958f0484fSRodney W. Grimes 	    ISSET(FTS_NOCHDIR))
56058f0484fSRodney W. Grimes 		return (sp->fts_child = fts_build(sp, instr));
56158f0484fSRodney W. Grimes 
5626b422721SJilles Tjoelker 	if ((fd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
56358f0484fSRodney W. Grimes 		return (NULL);
56458f0484fSRodney W. Grimes 	sp->fts_child = fts_build(sp, instr);
565764a9bbeSJilles Tjoelker 	serrno = (sp->fts_child == NULL) ? errno : 0;
566764a9bbeSJilles Tjoelker 	rc = fchdir(fd);
567764a9bbeSJilles Tjoelker 	if (rc < 0 && serrno == 0)
568764a9bbeSJilles Tjoelker 		serrno = errno;
569ce5c3df1SDavid Schultz 	(void)_close(fd);
570764a9bbeSJilles Tjoelker 	errno = serrno;
571764a9bbeSJilles Tjoelker 	if (rc < 0)
57258f0484fSRodney W. Grimes 		return (NULL);
57358f0484fSRodney W. Grimes 	return (sp->fts_child);
57458f0484fSRodney W. Grimes }
57558f0484fSRodney W. Grimes 
5760d3bcc2eSGarrett Wollman #ifndef fts_get_clientptr
5770d3bcc2eSGarrett Wollman #error "fts_get_clientptr not defined"
5780d3bcc2eSGarrett Wollman #endif
5790d3bcc2eSGarrett Wollman 
5800d3bcc2eSGarrett Wollman void *
5810d3bcc2eSGarrett Wollman (fts_get_clientptr)(FTS *sp)
5820d3bcc2eSGarrett Wollman {
5830d3bcc2eSGarrett Wollman 
5840d3bcc2eSGarrett Wollman 	return (fts_get_clientptr(sp));
5850d3bcc2eSGarrett Wollman }
5860d3bcc2eSGarrett Wollman 
5870d3bcc2eSGarrett Wollman #ifndef fts_get_stream
5880d3bcc2eSGarrett Wollman #error "fts_get_stream not defined"
5890d3bcc2eSGarrett Wollman #endif
5900d3bcc2eSGarrett Wollman 
5910d3bcc2eSGarrett Wollman FTS *
5920d3bcc2eSGarrett Wollman (fts_get_stream)(FTSENT *p)
5930d3bcc2eSGarrett Wollman {
5940d3bcc2eSGarrett Wollman 	return (fts_get_stream(p));
5950d3bcc2eSGarrett Wollman }
5960d3bcc2eSGarrett Wollman 
5970d3bcc2eSGarrett Wollman void
fts_set_clientptr(FTS * sp,void * clientptr)5980d3bcc2eSGarrett Wollman fts_set_clientptr(FTS *sp, void *clientptr)
5990d3bcc2eSGarrett Wollman {
6000d3bcc2eSGarrett Wollman 
6010d3bcc2eSGarrett Wollman 	sp->fts_clientptr = clientptr;
6020d3bcc2eSGarrett Wollman }
6030d3bcc2eSGarrett Wollman 
6040cff70caSGanael LAPLANCHE static struct dirent *
fts_safe_readdir(DIR * dirp,int * readdir_errno)6050cff70caSGanael LAPLANCHE fts_safe_readdir(DIR *dirp, int *readdir_errno)
6060cff70caSGanael LAPLANCHE {
6070cff70caSGanael LAPLANCHE 	struct dirent *ret;
6080cff70caSGanael LAPLANCHE 
6090cff70caSGanael LAPLANCHE 	errno = 0;
6100cff70caSGanael LAPLANCHE 	if (!dirp)
6110cff70caSGanael LAPLANCHE 		return (NULL);
6120cff70caSGanael LAPLANCHE 	ret = readdir(dirp);
6130cff70caSGanael LAPLANCHE 	*readdir_errno = errno;
6140cff70caSGanael LAPLANCHE 	return (ret);
6150cff70caSGanael LAPLANCHE }
6160cff70caSGanael LAPLANCHE 
61758f0484fSRodney W. Grimes /*
61858f0484fSRodney W. Grimes  * This is the tricky part -- do not casually change *anything* in here.  The
61958f0484fSRodney W. Grimes  * idea is to build the linked list of entries that are used by fts_children
62058f0484fSRodney W. Grimes  * and fts_read.  There are lots of special cases.
62158f0484fSRodney W. Grimes  *
62258f0484fSRodney W. Grimes  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
62358f0484fSRodney W. Grimes  * set and it's a physical walk (so that symbolic links can't be directories),
62458f0484fSRodney W. Grimes  * we can do things quickly.  First, if it's a 4.4BSD file system, the type
62558f0484fSRodney W. Grimes  * of the file is in the directory entry.  Otherwise, we assume that the number
62658f0484fSRodney W. Grimes  * of subdirectories in a node is equal to the number of links to the parent.
62758f0484fSRodney W. Grimes  * The former skips all stat calls.  The latter skips stat calls in any leaf
62858f0484fSRodney W. Grimes  * directories and for any files after the subdirectories in the directory have
62958f0484fSRodney W. Grimes  * been found, cutting the stat calls by about 2/3.
63058f0484fSRodney W. Grimes  */
63158f0484fSRodney W. Grimes static FTSENT *
fts_build(FTS * sp,int type)6323a12c046SXin LI fts_build(FTS *sp, int type)
63358f0484fSRodney W. Grimes {
634b231cb39SDavid E. O'Brien 	struct dirent *dp;
635b231cb39SDavid E. O'Brien 	FTSENT *p, *head;
63658f0484fSRodney W. Grimes 	FTSENT *cur, *tail;
63758f0484fSRodney W. Grimes 	DIR *dirp;
6381a9b5f47SBrian Feldman 	void *oldaddr;
63958f0484fSRodney W. Grimes 	char *cp;
6400cff70caSGanael LAPLANCHE 	int cderrno, descend, oflag, saved_errno, nostat, doadjust,
6410cff70caSGanael LAPLANCHE 	    readdir_errno;
64248aaad5fSYaroslav Tykhiy 	long level;
64348aaad5fSYaroslav Tykhiy 	long nlinks;	/* has to be signed because -1 is a magic value */
64448aaad5fSYaroslav Tykhiy 	size_t dnamlen, len, maxlen, nitems;
64558f0484fSRodney W. Grimes 
64658f0484fSRodney W. Grimes 	/* Set current node pointer. */
64758f0484fSRodney W. Grimes 	cur = sp->fts_cur;
64858f0484fSRodney W. Grimes 
64958f0484fSRodney W. Grimes 	/*
65058f0484fSRodney W. Grimes 	 * Open the directory for reading.  If this fails, we're done.
65158f0484fSRodney W. Grimes 	 * If being called from fts_read, set the fts_info field.
65258f0484fSRodney W. Grimes 	 */
653adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
654adf6ad9eSPeter Wemm 	if (ISSET(FTS_WHITEOUT))
655ed5e102aSEd Maste 		oflag = DTF_NODUP;
656adf6ad9eSPeter Wemm 	else
657ed5e102aSEd Maste 		oflag = DTF_HIDEW | DTF_NODUP;
658adf6ad9eSPeter Wemm #else
659adf6ad9eSPeter Wemm #define __opendir2(path, flag) opendir(path)
660adf6ad9eSPeter Wemm #endif
661adf6ad9eSPeter Wemm 	if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
66258f0484fSRodney W. Grimes 		if (type == BREAD) {
66358f0484fSRodney W. Grimes 			cur->fts_info = FTS_DNR;
66458f0484fSRodney W. Grimes 			cur->fts_errno = errno;
66558f0484fSRodney W. Grimes 		}
66658f0484fSRodney W. Grimes 		return (NULL);
66758f0484fSRodney W. Grimes 	}
66858f0484fSRodney W. Grimes 
66958f0484fSRodney W. Grimes 	/*
67058f0484fSRodney W. Grimes 	 * Nlinks is the number of possible entries of type directory in the
67158f0484fSRodney W. Grimes 	 * directory if we're cheating on stat calls, 0 if we're not doing
67258f0484fSRodney W. Grimes 	 * any stat calls at all, -1 if we're doing stats on everything.
67358f0484fSRodney W. Grimes 	 */
6741a9b5f47SBrian Feldman 	if (type == BNAMES) {
67558f0484fSRodney W. Grimes 		nlinks = 0;
6761a9b5f47SBrian Feldman 		/* Be quiet about nostat, GCC. */
6771a9b5f47SBrian Feldman 		nostat = 0;
6781a9b5f47SBrian Feldman 	} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
67999ca5b88SPeter Edwards 		if (fts_ufslinks(sp, cur))
68058f0484fSRodney W. Grimes 			nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
68199ca5b88SPeter Edwards 		else
68299ca5b88SPeter Edwards 			nlinks = -1;
6831a9b5f47SBrian Feldman 		nostat = 1;
6841a9b5f47SBrian Feldman 	} else {
68558f0484fSRodney W. Grimes 		nlinks = -1;
6861a9b5f47SBrian Feldman 		nostat = 0;
6871a9b5f47SBrian Feldman 	}
68858f0484fSRodney W. Grimes 
68958f0484fSRodney W. Grimes #ifdef notdef
69058f0484fSRodney W. Grimes 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
69158f0484fSRodney W. Grimes 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
69258f0484fSRodney W. Grimes 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
69358f0484fSRodney W. Grimes #endif
69458f0484fSRodney W. Grimes 	/*
69558f0484fSRodney W. Grimes 	 * If we're going to need to stat anything or we want to descend
69658f0484fSRodney W. Grimes 	 * and stay in the directory, chdir.  If this fails we keep going,
69758f0484fSRodney W. Grimes 	 * but set a flag so we don't chdir after the post-order visit.
69858f0484fSRodney W. Grimes 	 * We won't be able to stat anything, but we can still return the
69958f0484fSRodney W. Grimes 	 * names themselves.  Note, that since fts_read won't be able to
70058f0484fSRodney W. Grimes 	 * chdir into the directory, it will have to return different path
70158f0484fSRodney W. Grimes 	 * names than before, i.e. "a/b" instead of "b".  Since the node
70258f0484fSRodney W. Grimes 	 * has already been visited in pre-order, have to wait until the
70358f0484fSRodney W. Grimes 	 * post-order visit to return the error.  There is a special case
70458f0484fSRodney W. Grimes 	 * here, if there was nothing to stat then it's not an error to
70558f0484fSRodney W. Grimes 	 * not be able to stat.  This is all fairly nasty.  If a program
70658f0484fSRodney W. Grimes 	 * needed sorted entries or stat information, they had better be
70758f0484fSRodney W. Grimes 	 * checking FTS_NS on the returned nodes.
70858f0484fSRodney W. Grimes 	 */
70958f0484fSRodney W. Grimes 	cderrno = 0;
7101a9b5f47SBrian Feldman 	if (nlinks || type == BREAD) {
7110bb2aabfSGleb Kurtsou 		if (fts_safe_changedir(sp, cur, _dirfd(dirp), NULL)) {
71258f0484fSRodney W. Grimes 			if (nlinks && type == BREAD)
71358f0484fSRodney W. Grimes 				cur->fts_errno = errno;
71458f0484fSRodney W. Grimes 			cur->fts_flags |= FTS_DONTCHDIR;
71558f0484fSRodney W. Grimes 			descend = 0;
71658f0484fSRodney W. Grimes 			cderrno = errno;
71758f0484fSRodney W. Grimes 		} else
71858f0484fSRodney W. Grimes 			descend = 1;
7191a9b5f47SBrian Feldman 	} else
72058f0484fSRodney W. Grimes 		descend = 0;
72158f0484fSRodney W. Grimes 
72258f0484fSRodney W. Grimes 	/*
72358f0484fSRodney W. Grimes 	 * Figure out the max file name length that can be stored in the
72458f0484fSRodney W. Grimes 	 * current path -- the inner loop allocates more path as necessary.
72558f0484fSRodney W. Grimes 	 * We really wouldn't have to do the maxlen calculations here, we
72658f0484fSRodney W. Grimes 	 * could do them in fts_read before returning the path, but it's a
72758f0484fSRodney W. Grimes 	 * lot easier here since the length is part of the dirent structure.
72858f0484fSRodney W. Grimes 	 *
72958f0484fSRodney W. Grimes 	 * If not changing directories set a pointer so that can just append
73058f0484fSRodney W. Grimes 	 * each new name into the path.
73158f0484fSRodney W. Grimes 	 */
73258f0484fSRodney W. Grimes 	len = NAPPEND(cur);
73358f0484fSRodney W. Grimes 	if (ISSET(FTS_NOCHDIR)) {
73458f0484fSRodney W. Grimes 		cp = sp->fts_path + len;
73558f0484fSRodney W. Grimes 		*cp++ = '/';
7361a9b5f47SBrian Feldman 	} else {
7371a9b5f47SBrian Feldman 		/* GCC, you're too verbose. */
7381a9b5f47SBrian Feldman 		cp = NULL;
73958f0484fSRodney W. Grimes 	}
7401a9b5f47SBrian Feldman 	len++;
7411a9b5f47SBrian Feldman 	maxlen = sp->fts_pathlen - len;
74258f0484fSRodney W. Grimes 
74358f0484fSRodney W. Grimes 	level = cur->fts_level + 1;
74458f0484fSRodney W. Grimes 
74558f0484fSRodney W. Grimes 	/* Read the directory, attaching each entry to the `link' pointer. */
7461a9b5f47SBrian Feldman 	doadjust = 0;
7470cff70caSGanael LAPLANCHE 	readdir_errno = 0;
7480cff70caSGanael LAPLANCHE 	for (head = tail = NULL, nitems = 0;
7490cff70caSGanael LAPLANCHE 	    (dp = fts_safe_readdir(dirp, &readdir_errno));) {
750542fc104STim Kientzle 		dnamlen = dp->d_namlen;
75158f0484fSRodney W. Grimes 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
75258f0484fSRodney W. Grimes 			continue;
75358f0484fSRodney W. Grimes 
75448aaad5fSYaroslav Tykhiy 		if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL)
75558f0484fSRodney W. Grimes 			goto mem1;
756542fc104STim Kientzle 		if (dnamlen >= maxlen) {	/* include space for NUL */
7571a9b5f47SBrian Feldman 			oldaddr = sp->fts_path;
758542fc104STim Kientzle 			if (fts_palloc(sp, dnamlen + len + 1)) {
75958f0484fSRodney W. Grimes 				/*
76058f0484fSRodney W. Grimes 				 * No more memory for path or structures.  Save
76158f0484fSRodney W. Grimes 				 * errno, free up the current structure and the
76258f0484fSRodney W. Grimes 				 * structures already allocated.
76358f0484fSRodney W. Grimes 				 */
76458f0484fSRodney W. Grimes mem1:				saved_errno = errno;
76558f0484fSRodney W. Grimes 				if (p)
76658f0484fSRodney W. Grimes 					free(p);
76758f0484fSRodney W. Grimes 				fts_lfree(head);
76858f0484fSRodney W. Grimes 				(void)closedir(dirp);
76958f0484fSRodney W. Grimes 				cur->fts_info = FTS_ERR;
77058f0484fSRodney W. Grimes 				SET(FTS_STOP);
7711a9b5f47SBrian Feldman 				errno = saved_errno;
77258f0484fSRodney W. Grimes 				return (NULL);
77358f0484fSRodney W. Grimes 			}
7741a9b5f47SBrian Feldman 			/* Did realloc() change the pointer? */
7751a9b5f47SBrian Feldman 			if (oldaddr != sp->fts_path) {
7761a9b5f47SBrian Feldman 				doadjust = 1;
7771a9b5f47SBrian Feldman 				if (ISSET(FTS_NOCHDIR))
7781a9b5f47SBrian Feldman 					cp = sp->fts_path + len;
7791a9b5f47SBrian Feldman 			}
7801a9b5f47SBrian Feldman 			maxlen = sp->fts_pathlen - len;
78158f0484fSRodney W. Grimes 		}
78258f0484fSRodney W. Grimes 
78358f0484fSRodney W. Grimes 		p->fts_level = level;
7841a9b5f47SBrian Feldman 		p->fts_parent = sp->fts_cur;
785542fc104STim Kientzle 		p->fts_pathlen = len + dnamlen;
78658f0484fSRodney W. Grimes 
787adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
788adf6ad9eSPeter Wemm 		if (dp->d_type == DT_WHT)
789adf6ad9eSPeter Wemm 			p->fts_flags |= FTS_ISW;
790adf6ad9eSPeter Wemm #endif
791adf6ad9eSPeter Wemm 
79258f0484fSRodney W. Grimes 		if (cderrno) {
79358f0484fSRodney W. Grimes 			if (nlinks) {
79458f0484fSRodney W. Grimes 				p->fts_info = FTS_NS;
79558f0484fSRodney W. Grimes 				p->fts_errno = cderrno;
79658f0484fSRodney W. Grimes 			} else
79758f0484fSRodney W. Grimes 				p->fts_info = FTS_NSOK;
79858f0484fSRodney W. Grimes 			p->fts_accpath = cur->fts_accpath;
79958f0484fSRodney W. Grimes 		} else if (nlinks == 0
80058f0484fSRodney W. Grimes #ifdef DT_DIR
8011a9b5f47SBrian Feldman 		    || (nostat &&
80251295a4dSJordan K. Hubbard 		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
80358f0484fSRodney W. Grimes #endif
80458f0484fSRodney W. Grimes 		    ) {
80558f0484fSRodney W. Grimes 			p->fts_accpath =
80658f0484fSRodney W. Grimes 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
80758f0484fSRodney W. Grimes 			p->fts_info = FTS_NSOK;
80858f0484fSRodney W. Grimes 		} else {
80958f0484fSRodney W. Grimes 			/* Build a file name for fts_stat to stat. */
81058f0484fSRodney W. Grimes 			if (ISSET(FTS_NOCHDIR)) {
81158f0484fSRodney W. Grimes 				p->fts_accpath = p->fts_path;
81258f0484fSRodney W. Grimes 				memmove(cp, p->fts_name, p->fts_namelen + 1);
81354cc1f88SJilles Tjoelker 				p->fts_info = fts_stat(sp, p, 0, _dirfd(dirp));
81454cc1f88SJilles Tjoelker 			} else {
81558f0484fSRodney W. Grimes 				p->fts_accpath = p->fts_name;
81654cc1f88SJilles Tjoelker 				p->fts_info = fts_stat(sp, p, 0, -1);
81754cc1f88SJilles Tjoelker 			}
81858f0484fSRodney W. Grimes 
81958f0484fSRodney W. Grimes 			/* Decrement link count if applicable. */
82058f0484fSRodney W. Grimes 			if (nlinks > 0 && (p->fts_info == FTS_D ||
82158f0484fSRodney W. Grimes 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
82258f0484fSRodney W. Grimes 				--nlinks;
82358f0484fSRodney W. Grimes 		}
82458f0484fSRodney W. Grimes 
82558f0484fSRodney W. Grimes 		/* We walk in directory order so "ls -f" doesn't get upset. */
82658f0484fSRodney W. Grimes 		p->fts_link = NULL;
82758f0484fSRodney W. Grimes 		if (head == NULL)
82858f0484fSRodney W. Grimes 			head = tail = p;
82958f0484fSRodney W. Grimes 		else {
83058f0484fSRodney W. Grimes 			tail->fts_link = p;
83158f0484fSRodney W. Grimes 			tail = p;
83258f0484fSRodney W. Grimes 		}
83358f0484fSRodney W. Grimes 		++nitems;
83458f0484fSRodney W. Grimes 	}
8350cff70caSGanael LAPLANCHE 
8360cff70caSGanael LAPLANCHE 	if (readdir_errno) {
8370cff70caSGanael LAPLANCHE 		cur->fts_errno = readdir_errno;
8380cff70caSGanael LAPLANCHE 		/*
8390cff70caSGanael LAPLANCHE 		 * If we've not read any items yet, treat
8400cff70caSGanael LAPLANCHE 		 * the error as if we can't access the dir.
8410cff70caSGanael LAPLANCHE 		 */
8420cff70caSGanael LAPLANCHE 		cur->fts_info = nitems ? FTS_ERR : FTS_DNR;
8430cff70caSGanael LAPLANCHE 	}
8440cff70caSGanael LAPLANCHE 
8459a91f1ccSWarner Losh 	if (dirp)
84658f0484fSRodney W. Grimes 		(void)closedir(dirp);
84758f0484fSRodney W. Grimes 
84858f0484fSRodney W. Grimes 	/*
8491a9b5f47SBrian Feldman 	 * If realloc() changed the address of the path, adjust the
8501a9b5f47SBrian Feldman 	 * addresses for the rest of the tree and the dir list.
85158f0484fSRodney W. Grimes 	 */
8521a9b5f47SBrian Feldman 	if (doadjust)
8531a9b5f47SBrian Feldman 		fts_padjust(sp, head);
85458f0484fSRodney W. Grimes 
85558f0484fSRodney W. Grimes 	/*
85658f0484fSRodney W. Grimes 	 * If not changing directories, reset the path back to original
85758f0484fSRodney W. Grimes 	 * state.
85858f0484fSRodney W. Grimes 	 */
859909e2601SJaakko Heinonen 	if (ISSET(FTS_NOCHDIR))
860909e2601SJaakko Heinonen 		sp->fts_path[cur->fts_pathlen] = '\0';
86158f0484fSRodney W. Grimes 
86258f0484fSRodney W. Grimes 	/*
86358f0484fSRodney W. Grimes 	 * If descended after called from fts_children or after called from
86458f0484fSRodney W. Grimes 	 * fts_read and nothing found, get back.  At the root level we use
86558f0484fSRodney W. Grimes 	 * the saved fd; if one of fts_open()'s arguments is a relative path
86658f0484fSRodney W. Grimes 	 * to an empty directory, we wind up here with no other way back.  If
86758f0484fSRodney W. Grimes 	 * can't get back, we're done.
86858f0484fSRodney W. Grimes 	 */
86958f0484fSRodney W. Grimes 	if (descend && (type == BCHILD || !nitems) &&
87058f0484fSRodney W. Grimes 	    (cur->fts_level == FTS_ROOTLEVEL ?
87193a85518SKris Kennaway 	    FCHDIR(sp, sp->fts_rfd) :
87293a85518SKris Kennaway 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
8738844cec8SConrad Meyer 		fts_lfree(head);
87458f0484fSRodney W. Grimes 		cur->fts_info = FTS_ERR;
87558f0484fSRodney W. Grimes 		SET(FTS_STOP);
87658f0484fSRodney W. Grimes 		return (NULL);
87758f0484fSRodney W. Grimes 	}
87858f0484fSRodney W. Grimes 
87958f0484fSRodney W. Grimes 	/* If didn't find anything, return NULL. */
88058f0484fSRodney W. Grimes 	if (!nitems) {
8810cff70caSGanael LAPLANCHE 		if (type == BREAD &&
8820cff70caSGanael LAPLANCHE 		    cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR)
88358f0484fSRodney W. Grimes 			cur->fts_info = FTS_DP;
88458f0484fSRodney W. Grimes 		return (NULL);
88558f0484fSRodney W. Grimes 	}
88658f0484fSRodney W. Grimes 
88758f0484fSRodney W. Grimes 	/* Sort the entries. */
88858f0484fSRodney W. Grimes 	if (sp->fts_compar && nitems > 1)
88958f0484fSRodney W. Grimes 		head = fts_sort(sp, head, nitems);
89058f0484fSRodney W. Grimes 	return (head);
89158f0484fSRodney W. Grimes }
89258f0484fSRodney W. Grimes 
89348aaad5fSYaroslav Tykhiy static int
fts_stat(FTS * sp,FTSENT * p,int follow,int dfd)89454cc1f88SJilles Tjoelker fts_stat(FTS *sp, FTSENT *p, int follow, int dfd)
89558f0484fSRodney W. Grimes {
896b231cb39SDavid E. O'Brien 	FTSENT *t;
897b231cb39SDavid E. O'Brien 	dev_t dev;
898b231cb39SDavid E. O'Brien 	ino_t ino;
89958f0484fSRodney W. Grimes 	struct stat *sbp, sb;
90058f0484fSRodney W. Grimes 	int saved_errno;
90154cc1f88SJilles Tjoelker 	const char *path;
90254cc1f88SJilles Tjoelker 
903*5abef298SDag-Erling Smørgrav 	if (dfd == -1) {
904*5abef298SDag-Erling Smørgrav 		path = p->fts_accpath;
905*5abef298SDag-Erling Smørgrav 		dfd = AT_FDCWD;
906*5abef298SDag-Erling Smørgrav 	} else {
90754cc1f88SJilles Tjoelker 		path = p->fts_name;
908*5abef298SDag-Erling Smørgrav 	}
90958f0484fSRodney W. Grimes 
91058f0484fSRodney W. Grimes 	/* If user needs stat info, stat buffer already allocated. */
91158f0484fSRodney W. Grimes 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
91258f0484fSRodney W. Grimes 
913adf6ad9eSPeter Wemm #ifdef FTS_WHITEOUT
914a1acdbf0SBruce Evans 	/* Check for whiteout. */
915adf6ad9eSPeter Wemm 	if (p->fts_flags & FTS_ISW) {
916adf6ad9eSPeter Wemm 		if (sbp != &sb) {
917adf6ad9eSPeter Wemm 			memset(sbp, '\0', sizeof(*sbp));
918adf6ad9eSPeter Wemm 			sbp->st_mode = S_IFWHT;
919adf6ad9eSPeter Wemm 		}
920adf6ad9eSPeter Wemm 		return (FTS_W);
921adf6ad9eSPeter Wemm 	}
922adf6ad9eSPeter Wemm #endif
923adf6ad9eSPeter Wemm 
92458f0484fSRodney W. Grimes 	/*
92558f0484fSRodney W. Grimes 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
92658f0484fSRodney W. Grimes 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
92758f0484fSRodney W. Grimes 	 * fail, set the errno from the stat call.
92858f0484fSRodney W. Grimes 	 */
92958f0484fSRodney W. Grimes 	if (ISSET(FTS_LOGICAL) || follow) {
93054cc1f88SJilles Tjoelker 		if (fstatat(dfd, path, sbp, 0)) {
93158f0484fSRodney W. Grimes 			saved_errno = errno;
932d30c6f8eSJilles Tjoelker 			if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
93358f0484fSRodney W. Grimes 				p->fts_errno = saved_errno;
93458f0484fSRodney W. Grimes 				goto err;
93558f0484fSRodney W. Grimes 			}
936d30c6f8eSJilles Tjoelker 			errno = 0;
937d30c6f8eSJilles Tjoelker 			if (S_ISLNK(sbp->st_mode))
938d30c6f8eSJilles Tjoelker 				return (FTS_SLNONE);
939d30c6f8eSJilles Tjoelker 		}
94054cc1f88SJilles Tjoelker 	} else if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
94158f0484fSRodney W. Grimes 		p->fts_errno = errno;
94258f0484fSRodney W. Grimes err:		memset(sbp, 0, sizeof(struct stat));
94358f0484fSRodney W. Grimes 		return (FTS_NS);
94458f0484fSRodney W. Grimes 	}
94558f0484fSRodney W. Grimes 
94658f0484fSRodney W. Grimes 	if (S_ISDIR(sbp->st_mode)) {
94758f0484fSRodney W. Grimes 		/*
94858f0484fSRodney W. Grimes 		 * Set the device/inode.  Used to find cycles and check for
94958f0484fSRodney W. Grimes 		 * crossing mount points.  Also remember the link count, used
95058f0484fSRodney W. Grimes 		 * in fts_build to limit the number of stat calls.  It is
95158f0484fSRodney W. Grimes 		 * understood that these fields are only referenced if fts_info
95258f0484fSRodney W. Grimes 		 * is set to FTS_D.
95358f0484fSRodney W. Grimes 		 */
95458f0484fSRodney W. Grimes 		dev = p->fts_dev = sbp->st_dev;
95558f0484fSRodney W. Grimes 		ino = p->fts_ino = sbp->st_ino;
95658f0484fSRodney W. Grimes 		p->fts_nlink = sbp->st_nlink;
95758f0484fSRodney W. Grimes 
95858f0484fSRodney W. Grimes 		if (ISDOT(p->fts_name))
95958f0484fSRodney W. Grimes 			return (FTS_DOT);
96058f0484fSRodney W. Grimes 
96158f0484fSRodney W. Grimes 		/*
96258f0484fSRodney W. Grimes 		 * Cycle detection is done by brute force when the directory
96358f0484fSRodney W. Grimes 		 * is first encountered.  If the tree gets deep enough or the
96458f0484fSRodney W. Grimes 		 * number of symbolic links to directories is high enough,
96558f0484fSRodney W. Grimes 		 * something faster might be worthwhile.
96658f0484fSRodney W. Grimes 		 */
96758f0484fSRodney W. Grimes 		for (t = p->fts_parent;
96858f0484fSRodney W. Grimes 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
96958f0484fSRodney W. Grimes 			if (ino == t->fts_ino && dev == t->fts_dev) {
97058f0484fSRodney W. Grimes 				p->fts_cycle = t;
97158f0484fSRodney W. Grimes 				return (FTS_DC);
97258f0484fSRodney W. Grimes 			}
97358f0484fSRodney W. Grimes 		return (FTS_D);
97458f0484fSRodney W. Grimes 	}
97558f0484fSRodney W. Grimes 	if (S_ISLNK(sbp->st_mode))
97658f0484fSRodney W. Grimes 		return (FTS_SL);
97758f0484fSRodney W. Grimes 	if (S_ISREG(sbp->st_mode))
97858f0484fSRodney W. Grimes 		return (FTS_F);
97958f0484fSRodney W. Grimes 	return (FTS_DEFAULT);
98058f0484fSRodney W. Grimes }
98158f0484fSRodney W. Grimes 
9820d3bcc2eSGarrett Wollman /*
9830d3bcc2eSGarrett Wollman  * The comparison function takes pointers to pointers to FTSENT structures.
9840d3bcc2eSGarrett Wollman  * Qsort wants a comparison function that takes pointers to void.
9850d3bcc2eSGarrett Wollman  * (Both with appropriate levels of const-poisoning, of course!)
9860d3bcc2eSGarrett Wollman  * Use a trampoline function to deal with the difference.
9870d3bcc2eSGarrett Wollman  */
9880d3bcc2eSGarrett Wollman static int
fts_compar(const void * a,const void * b)9890d3bcc2eSGarrett Wollman fts_compar(const void *a, const void *b)
9900d3bcc2eSGarrett Wollman {
9910d3bcc2eSGarrett Wollman 	FTS *parent;
9920d3bcc2eSGarrett Wollman 
9930d3bcc2eSGarrett Wollman 	parent = (*(const FTSENT * const *)a)->fts_fts;
9940d3bcc2eSGarrett Wollman 	return (*parent->fts_compar)(a, b);
9950d3bcc2eSGarrett Wollman }
9960d3bcc2eSGarrett Wollman 
99758f0484fSRodney W. Grimes static FTSENT *
fts_sort(FTS * sp,FTSENT * head,size_t nitems)9983a12c046SXin LI fts_sort(FTS *sp, FTSENT *head, size_t nitems)
99958f0484fSRodney W. Grimes {
1000b231cb39SDavid E. O'Brien 	FTSENT **ap, *p;
100158f0484fSRodney W. Grimes 
100258f0484fSRodney W. Grimes 	/*
100358f0484fSRodney W. Grimes 	 * Construct an array of pointers to the structures and call qsort(3).
100458f0484fSRodney W. Grimes 	 * Reassemble the array in the order returned by qsort.  If unable to
100558f0484fSRodney W. Grimes 	 * sort for memory reasons, return the directory entries in their
100658f0484fSRodney W. Grimes 	 * current order.  Allocate enough space for the current needs plus
100758f0484fSRodney W. Grimes 	 * 40 so don't realloc one entry at a time.
100858f0484fSRodney W. Grimes 	 */
100958f0484fSRodney W. Grimes 	if (nitems > sp->fts_nitems) {
101058f0484fSRodney W. Grimes 		sp->fts_nitems = nitems + 40;
10118ca72379SBrian Feldman 		if ((sp->fts_array = reallocf(sp->fts_array,
10121a9b5f47SBrian Feldman 		    sp->fts_nitems * sizeof(FTSENT *))) == NULL) {
101358f0484fSRodney W. Grimes 			sp->fts_nitems = 0;
101458f0484fSRodney W. Grimes 			return (head);
101558f0484fSRodney W. Grimes 		}
101658f0484fSRodney W. Grimes 	}
101758f0484fSRodney W. Grimes 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
101858f0484fSRodney W. Grimes 		*ap++ = p;
10190d3bcc2eSGarrett Wollman 	qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar);
102058f0484fSRodney W. Grimes 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
102158f0484fSRodney W. Grimes 		ap[0]->fts_link = ap[1];
102258f0484fSRodney W. Grimes 	ap[0]->fts_link = NULL;
102358f0484fSRodney W. Grimes 	return (head);
102458f0484fSRodney W. Grimes }
102558f0484fSRodney W. Grimes 
102658f0484fSRodney W. Grimes static FTSENT *
fts_alloc(FTS * sp,char * name,size_t namelen)10273a12c046SXin LI fts_alloc(FTS *sp, char *name, size_t namelen)
102858f0484fSRodney W. Grimes {
1029b231cb39SDavid E. O'Brien 	FTSENT *p;
103058f0484fSRodney W. Grimes 	size_t len;
103158f0484fSRodney W. Grimes 
103258f0484fSRodney W. Grimes 	/*
103358f0484fSRodney W. Grimes 	 * The file name is a variable length array and no stat structure is
103458f0484fSRodney W. Grimes 	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
103558f0484fSRodney W. Grimes 	 * structure, the file name and the stat structure in one chunk, but
10360d3bcc2eSGarrett Wollman 	 * be careful that the stat structure is reasonably aligned.
103758f0484fSRodney W. Grimes 	 */
10380d3bcc2eSGarrett Wollman 	len = sizeof(FTSENT) + namelen + 1;
103979679a18SDag-Erling Smørgrav 	if (!ISSET(FTS_NOSTAT)) {
104079679a18SDag-Erling Smørgrav 		len = roundup(len, alignof(struct stat));
104179679a18SDag-Erling Smørgrav 		p = calloc(1, len + sizeof(struct stat));
104279679a18SDag-Erling Smørgrav 	} else {
104379679a18SDag-Erling Smørgrav 		p = calloc(1, len);
104479679a18SDag-Erling Smørgrav 	}
104579679a18SDag-Erling Smørgrav 	if (p == NULL)
104658f0484fSRodney W. Grimes 		return (NULL);
104758f0484fSRodney W. Grimes 
104879679a18SDag-Erling Smørgrav 	p->fts_symfd = -1;
104958f0484fSRodney W. Grimes 	p->fts_path = sp->fts_path;
105079679a18SDag-Erling Smørgrav 	p->fts_name = (char *)(p + 1);
105179679a18SDag-Erling Smørgrav 	p->fts_namelen = namelen;
105258f0484fSRodney W. Grimes 	p->fts_instr = FTS_NOINSTR;
105379679a18SDag-Erling Smørgrav 	if (!ISSET(FTS_NOSTAT))
105479679a18SDag-Erling Smørgrav 		p->fts_statp = (struct stat *)((char *)p + len);
10550d3bcc2eSGarrett Wollman 	p->fts_fts = sp;
105679679a18SDag-Erling Smørgrav 	memcpy(p->fts_name, name, namelen);
105779679a18SDag-Erling Smørgrav 
105858f0484fSRodney W. Grimes 	return (p);
105958f0484fSRodney W. Grimes }
106058f0484fSRodney W. Grimes 
106158f0484fSRodney W. Grimes static void
fts_lfree(FTSENT * head)10623a12c046SXin LI fts_lfree(FTSENT *head)
106358f0484fSRodney W. Grimes {
1064b231cb39SDavid E. O'Brien 	FTSENT *p;
106558f0484fSRodney W. Grimes 
106658f0484fSRodney W. Grimes 	/* Free a linked list of structures. */
106751295a4dSJordan K. Hubbard 	while ((p = head)) {
106858f0484fSRodney W. Grimes 		head = head->fts_link;
106958f0484fSRodney W. Grimes 		free(p);
107058f0484fSRodney W. Grimes 	}
107158f0484fSRodney W. Grimes }
107258f0484fSRodney W. Grimes 
107358f0484fSRodney W. Grimes /*
107458f0484fSRodney W. Grimes  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
107558f0484fSRodney W. Grimes  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
107658f0484fSRodney W. Grimes  * though the kernel won't resolve them.  Add the size (not just what's needed)
107758f0484fSRodney W. Grimes  * plus 256 bytes so don't realloc the path 2 bytes at a time.
107858f0484fSRodney W. Grimes  */
107958f0484fSRodney W. Grimes static int
fts_palloc(FTS * sp,size_t more)10803a12c046SXin LI fts_palloc(FTS *sp, size_t more)
108158f0484fSRodney W. Grimes {
108258f0484fSRodney W. Grimes 
10831a9b5f47SBrian Feldman 	sp->fts_pathlen += more + 256;
10848ca72379SBrian Feldman 	sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen);
10858ca72379SBrian Feldman 	return (sp->fts_path == NULL);
1086f183d53eSWarner Losh }
1087f183d53eSWarner Losh 
108858f0484fSRodney W. Grimes /*
108958f0484fSRodney W. Grimes  * When the path is realloc'd, have to fix all of the pointers in structures
109058f0484fSRodney W. Grimes  * already returned.
109158f0484fSRodney W. Grimes  */
109258f0484fSRodney W. Grimes static void
fts_padjust(FTS * sp,FTSENT * head)10933a12c046SXin LI fts_padjust(FTS *sp, FTSENT *head)
109458f0484fSRodney W. Grimes {
109558f0484fSRodney W. Grimes 	FTSENT *p;
10961a9b5f47SBrian Feldman 	char *addr = sp->fts_path;
109758f0484fSRodney W. Grimes 
10988ca72379SBrian Feldman #define	ADJUST(p) do {							\
10991a9b5f47SBrian Feldman 	if ((p)->fts_accpath != (p)->fts_name) {			\
11001a9b5f47SBrian Feldman 		(p)->fts_accpath =					\
11011a9b5f47SBrian Feldman 		    (char *)addr + ((p)->fts_accpath - (p)->fts_path);	\
11021a9b5f47SBrian Feldman 	}								\
110358f0484fSRodney W. Grimes 	(p)->fts_path = addr;						\
11048ca72379SBrian Feldman } while (0)
110558f0484fSRodney W. Grimes 	/* Adjust the current set of children. */
110658f0484fSRodney W. Grimes 	for (p = sp->fts_child; p; p = p->fts_link)
11071a9b5f47SBrian Feldman 		ADJUST(p);
110858f0484fSRodney W. Grimes 
11091a9b5f47SBrian Feldman 	/* Adjust the rest of the tree, including the current level. */
11101a9b5f47SBrian Feldman 	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
11111a9b5f47SBrian Feldman 		ADJUST(p);
111258f0484fSRodney W. Grimes 		p = p->fts_link ? p->fts_link : p->fts_parent;
111358f0484fSRodney W. Grimes 	}
111458f0484fSRodney W. Grimes }
111558f0484fSRodney W. Grimes 
111658f0484fSRodney W. Grimes static size_t
fts_maxarglen(char * const * argv)111755b6b759SCraig Rodrigues fts_maxarglen(char * const *argv)
111858f0484fSRodney W. Grimes {
111958f0484fSRodney W. Grimes 	size_t len, max;
112058f0484fSRodney W. Grimes 
112158f0484fSRodney W. Grimes 	for (max = 0; *argv; ++argv)
112258f0484fSRodney W. Grimes 		if ((len = strlen(*argv)) > max)
112358f0484fSRodney W. Grimes 			max = len;
11241a9b5f47SBrian Feldman 	return (max + 1);
112558f0484fSRodney W. Grimes }
11269a91f1ccSWarner Losh 
11279a91f1ccSWarner Losh /*
11289a91f1ccSWarner Losh  * Change to dir specified by fd or p->fts_accpath without getting
11299a91f1ccSWarner Losh  * tricked by someone changing the world out from underneath us.
11309a91f1ccSWarner Losh  * Assumes p->fts_dev and p->fts_ino are filled in.
11319a91f1ccSWarner Losh  */
11329a91f1ccSWarner Losh static int
fts_safe_changedir(FTS * sp,FTSENT * p,int fd,char * path)11333a12c046SXin LI fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path)
11349a91f1ccSWarner Losh {
11359a91f1ccSWarner Losh 	int ret, oerrno, newfd;
11369a91f1ccSWarner Losh 	struct stat sb;
1137e5999120SKonstantin Belousov 	struct statfs sf;
11389a91f1ccSWarner Losh 
11399a91f1ccSWarner Losh 	newfd = fd;
11409a91f1ccSWarner Losh 	if (ISSET(FTS_NOCHDIR))
11419a91f1ccSWarner Losh 		return (0);
1142ad7d20c9SJilles Tjoelker 	if (fd < 0 && (newfd = _open(path, O_RDONLY | O_DIRECTORY |
1143ad7d20c9SJilles Tjoelker 	    O_CLOEXEC, 0)) < 0)
11449a91f1ccSWarner Losh 		return (-1);
1145d201fe46SDaniel Eischen 	if (_fstat(newfd, &sb)) {
11469a91f1ccSWarner Losh 		ret = -1;
11479a91f1ccSWarner Losh 		goto bail;
11489a91f1ccSWarner Losh 	}
11499a91f1ccSWarner Losh 	if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1150e5999120SKonstantin Belousov 		if (_fstatfs(newfd, &sf) != 0 ||
1151e5999120SKonstantin Belousov 		    (sf.f_flags & MNT_AUTOMOUNTED) == 0) {
11529a91f1ccSWarner Losh 			errno = ENOENT;		/* disinformation */
11539a91f1ccSWarner Losh 			ret = -1;
11549a91f1ccSWarner Losh 			goto bail;
11559a91f1ccSWarner Losh 		}
1156e5999120SKonstantin Belousov 		/* autofs might did the mount under us, accept. */
1157e5999120SKonstantin Belousov 		p->fts_dev = sb.st_dev;
1158ab6a311cSStefan Eßer 		p->fts_ino = sb.st_ino;
1159e5999120SKonstantin Belousov 	}
11609a91f1ccSWarner Losh 	ret = fchdir(newfd);
11619a91f1ccSWarner Losh bail:
11629a91f1ccSWarner Losh 	oerrno = errno;
11639a91f1ccSWarner Losh 	if (fd < 0)
11649233c4d9SJason Evans 		(void)_close(newfd);
11659a91f1ccSWarner Losh 	errno = oerrno;
11669a91f1ccSWarner Losh 	return (ret);
11679a91f1ccSWarner Losh }
116899ca5b88SPeter Edwards 
116999ca5b88SPeter Edwards /*
117099ca5b88SPeter Edwards  * Check if the filesystem for "ent" has UFS-style links.
117199ca5b88SPeter Edwards  */
117299ca5b88SPeter Edwards static int
fts_ufslinks(FTS * sp,const FTSENT * ent)117399ca5b88SPeter Edwards fts_ufslinks(FTS *sp, const FTSENT *ent)
117499ca5b88SPeter Edwards {
117599ca5b88SPeter Edwards 	struct _fts_private *priv;
117699ca5b88SPeter Edwards 	const char **cpp;
117799ca5b88SPeter Edwards 
117807dee1a7SPeter Edwards 	priv = (struct _fts_private *)sp;
117999ca5b88SPeter Edwards 	/*
118099ca5b88SPeter Edwards 	 * If this node's device is different from the previous, grab
118199ca5b88SPeter Edwards 	 * the filesystem information, and decide on the reliability
118299ca5b88SPeter Edwards 	 * of the link information from this filesystem for stat(2)
118399ca5b88SPeter Edwards 	 * avoidance.
118499ca5b88SPeter Edwards 	 */
118599ca5b88SPeter Edwards 	if (priv->ftsp_dev != ent->fts_dev) {
118699ca5b88SPeter Edwards 		if (statfs(ent->fts_path, &priv->ftsp_statfs) != -1) {
118799ca5b88SPeter Edwards 			priv->ftsp_dev = ent->fts_dev;
118899ca5b88SPeter Edwards 			priv->ftsp_linksreliable = 0;
118999ca5b88SPeter Edwards 			for (cpp = ufslike_filesystems; *cpp; cpp++) {
119099ca5b88SPeter Edwards 				if (strcmp(priv->ftsp_statfs.f_fstypename,
119199ca5b88SPeter Edwards 				    *cpp) == 0) {
119299ca5b88SPeter Edwards 					priv->ftsp_linksreliable = 1;
119399ca5b88SPeter Edwards 					break;
119499ca5b88SPeter Edwards 				}
119599ca5b88SPeter Edwards 			}
119699ca5b88SPeter Edwards 		} else {
119799ca5b88SPeter Edwards 			priv->ftsp_linksreliable = 0;
119899ca5b88SPeter Edwards 		}
119999ca5b88SPeter Edwards 	}
120007dee1a7SPeter Edwards 	return (priv->ftsp_linksreliable);
120199ca5b88SPeter Edwards }
1202