Blame io/fts.c

Packit 6c4009
/* File tree traversal functions.
Packit 6c4009
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/*-
Packit 6c4009
 * Copyright (c) 1990, 1993, 1994
Packit 6c4009
 *	The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBC_SCCS) && !defined(lint)
Packit 6c4009
static char sccsid[] = "@(#)fts.c	8.6 (Berkeley) 8/14/94";
Packit 6c4009
#endif /* LIBC_SCCS and not lint */
Packit 6c4009
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <include/sys/stat.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fts.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Largest alignment size needed, minus one.
Packit 6c4009
   Usually long double is the worst case.  */
Packit 6c4009
#ifndef ALIGNBYTES
Packit 6c4009
#define ALIGNBYTES	(__alignof__ (long double) - 1)
Packit 6c4009
#endif
Packit 6c4009
/* Align P to that size.  */
Packit 6c4009
#ifndef ALIGN
Packit 6c4009
#define	ALIGN(p)	(((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Support for the LFS API version.  */
Packit 6c4009
#ifndef FTS_OPEN
Packit 6c4009
#define FTS_OPEN fts_open
Packit 6c4009
#define FTS_CLOSE fts_close
Packit 6c4009
#define FTS_READ fts_read
Packit 6c4009
#define FTS_SET fts_set
Packit 6c4009
#define FTS_CHILDREN fts_children
Packit 6c4009
# define FTSOBJ FTS
Packit 6c4009
# define FTSENTRY FTSENT
Packit 6c4009
# define INO_T ino_t
Packit 6c4009
# define STAT stat
Packit 6c4009
# define LSTAT lstat
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static FTSENTRY	*fts_alloc (FTSOBJ *, const char *, size_t);
Packit 6c4009
static FTSENTRY	*fts_build (FTSOBJ *, int);
Packit 6c4009
static void	 fts_lfree (FTSENTRY *);
Packit 6c4009
static void	 fts_load (FTSOBJ *, FTSENTRY *);
Packit 6c4009
static size_t	 fts_maxarglen (char * const *);
Packit 6c4009
static void	 fts_padjust (FTSOBJ *, FTSENTRY *);
Packit 6c4009
static int	 fts_palloc (FTSOBJ *, size_t);
Packit 6c4009
static FTSENTRY	*fts_sort (FTSOBJ *, FTSENTRY *, int);
Packit 6c4009
static u_short	 fts_stat (FTSOBJ *, FTSENTRY *, int);
Packit 6c4009
static int      fts_safe_changedir (FTSOBJ *, FTSENTRY *, int, const char *);
Packit 6c4009
Packit 6c4009
#ifndef MAX
Packit 6c4009
#define MAX(a, b)	({ __typeof__ (a) _a = (a); \
Packit 6c4009
			   __typeof__ (b) _b = (b); \
Packit 6c4009
			   _a > _b ? _a : _b; })
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
Packit 6c4009
Packit 6c4009
#define CLR(opt)	(sp->fts_options &= ~(opt))
Packit 6c4009
#define	ISSET(opt)	(sp->fts_options & (opt))
Packit 6c4009
#define	SET(opt)	(sp->fts_options |= (opt))
Packit 6c4009
Packit 6c4009
#define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && __fchdir(fd))
Packit 6c4009
Packit 6c4009
/* fts_build flags */
Packit 6c4009
#define	BCHILD		1		/* fts_children */
Packit 6c4009
#define	BNAMES		2		/* fts_children, names only */
Packit 6c4009
#define	BREAD		3		/* fts_read */
Packit 6c4009
Packit 6c4009
FTSOBJ *
Packit 6c4009
FTS_OPEN (char * const *argv, int options,
Packit 6c4009
	  int (*compar) (const FTSENTRY **, const FTSENTRY **))
Packit 6c4009
{
Packit 6c4009
	FTSOBJ *sp;
Packit 6c4009
	FTSENTRY *p, *root;
Packit 6c4009
	int nitems;
Packit 6c4009
	FTSENTRY *parent = NULL;
Packit 6c4009
	FTSENTRY *tmp;
Packit 6c4009
Packit 6c4009
	/* Options check. */
Packit 6c4009
	if (options & ~FTS_OPTIONMASK) {
Packit 6c4009
		__set_errno (EINVAL);
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Allocate/initialize the stream */
Packit 6c4009
	if ((sp = malloc((u_int)sizeof(FTSOBJ))) == NULL)
Packit 6c4009
		return (NULL);
Packit 6c4009
	memset(sp, 0, sizeof(FTSOBJ));
Packit 6c4009
	sp->fts_compar = (int (*) (const void *, const void *)) compar;
Packit 6c4009
	sp->fts_options = options;
Packit 6c4009
Packit 6c4009
	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
Packit 6c4009
	if (ISSET(FTS_LOGICAL))
Packit 6c4009
		SET(FTS_NOCHDIR);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Start out with 1K of path space, and enough, in any case,
Packit 6c4009
	 * to hold the user's paths.
Packit 6c4009
	 */
Packit 6c4009
#ifndef MAXPATHLEN
Packit 6c4009
#define MAXPATHLEN 1024
Packit 6c4009
#endif
Packit 6c4009
	size_t maxarglen = fts_maxarglen(argv);
Packit 6c4009
	if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
Packit 6c4009
		goto mem1;
Packit 6c4009
Packit 6c4009
	/* Allocate/initialize root's parent. */
Packit 6c4009
	if (*argv != NULL) {
Packit 6c4009
		if ((parent = fts_alloc(sp, "", 0)) == NULL)
Packit 6c4009
			goto mem2;
Packit 6c4009
		parent->fts_level = FTS_ROOTPARENTLEVEL;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* Allocate/initialize root(s). */
Packit 6c4009
	for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
Packit 6c4009
		/* Don't allow zero-length paths. */
Packit 6c4009
		size_t len = strlen(*argv);
Packit 6c4009
		if (len == 0) {
Packit 6c4009
			__set_errno (ENOENT);
Packit 6c4009
			goto mem3;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		p = fts_alloc(sp, *argv, len);
Packit 6c4009
		p->fts_level = FTS_ROOTLEVEL;
Packit 6c4009
		p->fts_parent = parent;
Packit 6c4009
		p->fts_accpath = p->fts_name;
Packit 6c4009
		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
Packit 6c4009
Packit 6c4009
		/* Command-line "." and ".." are real directories. */
Packit 6c4009
		if (p->fts_info == FTS_DOT)
Packit 6c4009
			p->fts_info = FTS_D;
Packit 6c4009
Packit 6c4009
		/*
Packit 6c4009
		 * If comparison routine supplied, traverse in sorted
Packit 6c4009
		 * order; otherwise traverse in the order specified.
Packit 6c4009
		 */
Packit 6c4009
		if (compar) {
Packit 6c4009
			p->fts_link = root;
Packit 6c4009
			root = p;
Packit 6c4009
		} else {
Packit 6c4009
			p->fts_link = NULL;
Packit 6c4009
			if (root == NULL)
Packit 6c4009
				tmp = root = p;
Packit 6c4009
			else {
Packit 6c4009
				tmp->fts_link = p;
Packit 6c4009
				tmp = p;
Packit 6c4009
			}
Packit 6c4009
		}
Packit 6c4009
	}
Packit 6c4009
	if (compar && nitems > 1)
Packit 6c4009
		root = fts_sort(sp, root, nitems);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Allocate a dummy pointer and make fts_read think that we've just
Packit 6c4009
	 * finished the node before the root(s); set p->fts_info to FTS_INIT
Packit 6c4009
	 * so that everything about the "current" node is ignored.
Packit 6c4009
	 */
Packit 6c4009
	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
Packit 6c4009
		goto mem3;
Packit 6c4009
	sp->fts_cur->fts_link = root;
Packit 6c4009
	sp->fts_cur->fts_info = FTS_INIT;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
Packit 6c4009
	 * that we can get back here; this could be avoided for some paths,
Packit 6c4009
	 * but almost certainly not worth the effort.  Slashes, symbolic links,
Packit 6c4009
	 * and ".." are all fairly nasty problems.  Note, if we can't get the
Packit 6c4009
	 * descriptor we run anyway, just more slowly.
Packit 6c4009
	 */
Packit 6c4009
	if (!ISSET(FTS_NOCHDIR)
Packit 6c4009
	    && (sp->fts_rfd = __open(".", O_RDONLY, 0)) < 0)
Packit 6c4009
		SET(FTS_NOCHDIR);
Packit 6c4009
Packit 6c4009
	return (sp);
Packit 6c4009
Packit 6c4009
mem3:	fts_lfree(root);
Packit 6c4009
	free(parent);
Packit 6c4009
mem2:	free(sp->fts_path);
Packit 6c4009
mem1:	free(sp);
Packit 6c4009
	return (NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fts_load (FTSOBJ *sp, FTSENTRY *p)
Packit 6c4009
{
Packit 6c4009
	int len;
Packit 6c4009
	char *cp;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Load the stream structure for the next traversal.  Since we don't
Packit 6c4009
	 * actually enter the directory until after the preorder visit, set
Packit 6c4009
	 * the fts_accpath field specially so the chdir gets done to the right
Packit 6c4009
	 * place and the user can access the first node.  From fts_open it's
Packit 6c4009
	 * known that the path will fit.
Packit 6c4009
	 */
Packit 6c4009
	len = p->fts_pathlen = p->fts_namelen;
Packit 6c4009
	memmove(sp->fts_path, p->fts_name, len + 1);
Packit 6c4009
	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
Packit 6c4009
		len = strlen(++cp);
Packit 6c4009
		memmove(p->fts_name, cp, len + 1);
Packit 6c4009
		p->fts_namelen = len;
Packit 6c4009
	}
Packit 6c4009
	p->fts_accpath = p->fts_path = sp->fts_path;
Packit 6c4009
	sp->fts_dev = p->fts_dev;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
FTS_CLOSE (FTSOBJ *sp)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *freep, *p;
Packit 6c4009
	int saved_errno;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * This still works if we haven't read anything -- the dummy structure
Packit 6c4009
	 * points to the root list, so we step through to the end of the root
Packit 6c4009
	 * list which has a valid parent pointer.
Packit 6c4009
	 */
Packit 6c4009
	if (sp->fts_cur) {
Packit 6c4009
		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
Packit 6c4009
			freep = p;
Packit 6c4009
			p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
Packit 6c4009
			free(freep);
Packit 6c4009
		}
Packit 6c4009
		free(p);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Free up child linked list, sort array, path buffer. */
Packit 6c4009
	if (sp->fts_child)
Packit 6c4009
		fts_lfree(sp->fts_child);
Packit 6c4009
	free(sp->fts_array);
Packit 6c4009
	free(sp->fts_path);
Packit 6c4009
Packit 6c4009
	/* Return to original directory, save errno if necessary. */
Packit 6c4009
	if (!ISSET(FTS_NOCHDIR)) {
Packit 6c4009
		saved_errno = __fchdir(sp->fts_rfd) ? errno : 0;
Packit 6c4009
		(void)__close(sp->fts_rfd);
Packit 6c4009
Packit 6c4009
		/* Set errno and return. */
Packit 6c4009
		if (saved_errno != 0) {
Packit 6c4009
			/* Free up the stream pointer. */
Packit 6c4009
			free(sp);
Packit 6c4009
			__set_errno (saved_errno);
Packit 6c4009
			return (-1);
Packit 6c4009
		}
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Free up the stream pointer. */
Packit 6c4009
	free(sp);
Packit 6c4009
	return (0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Special case of "/" at the end of the path so that slashes aren't
Packit 6c4009
 * appended which would cause paths to be written as "....//foo".
Packit 6c4009
 */
Packit 6c4009
#define	NAPPEND(p)							\
Packit 6c4009
	(p->fts_path[p->fts_pathlen - 1] == '/'				\
Packit 6c4009
	    ? p->fts_pathlen - 1 : p->fts_pathlen)
Packit 6c4009
Packit 6c4009
FTSENTRY *
Packit 6c4009
FTS_READ (FTSOBJ *sp)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *p, *tmp;
Packit 6c4009
	int instr;
Packit 6c4009
	char *t;
Packit 6c4009
	int saved_errno;
Packit 6c4009
Packit 6c4009
	/* If finished or unrecoverable error, return NULL. */
Packit 6c4009
	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
Packit 6c4009
		return (NULL);
Packit 6c4009
Packit 6c4009
	/* Set current node pointer. */
Packit 6c4009
	p = sp->fts_cur;
Packit 6c4009
Packit 6c4009
	/* Save and zero out user instructions. */
Packit 6c4009
	instr = p->fts_instr;
Packit 6c4009
	p->fts_instr = FTS_NOINSTR;
Packit 6c4009
Packit 6c4009
	/* Any type of file may be re-visited; re-stat and re-turn. */
Packit 6c4009
	if (instr == FTS_AGAIN) {
Packit 6c4009
		p->fts_info = fts_stat(sp, p, 0);
Packit 6c4009
		return (p);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Following a symlink -- SLNONE test allows application to see
Packit 6c4009
	 * SLNONE and recover.  If indirecting through a symlink, have
Packit 6c4009
	 * keep a pointer to current location.  If unable to get that
Packit 6c4009
	 * pointer, follow fails.
Packit 6c4009
	 */
Packit 6c4009
	if (instr == FTS_FOLLOW &&
Packit 6c4009
	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
Packit 6c4009
		p->fts_info = fts_stat(sp, p, 1);
Packit 6c4009
		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
Packit 6c4009
			if ((p->fts_symfd = __open(".", O_RDONLY, 0)) < 0) {
Packit 6c4009
				p->fts_errno = errno;
Packit 6c4009
				p->fts_info = FTS_ERR;
Packit 6c4009
			} else
Packit 6c4009
				p->fts_flags |= FTS_SYMFOLLOW;
Packit 6c4009
		}
Packit 6c4009
		return (p);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Directory in pre-order. */
Packit 6c4009
	if (p->fts_info == FTS_D) {
Packit 6c4009
		/* If skipped or crossed mount point, do post-order visit. */
Packit 6c4009
		if (instr == FTS_SKIP ||
Packit 6c4009
		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
Packit 6c4009
			if (p->fts_flags & FTS_SYMFOLLOW)
Packit 6c4009
				(void)__close(p->fts_symfd);
Packit 6c4009
			if (sp->fts_child) {
Packit 6c4009
				fts_lfree(sp->fts_child);
Packit 6c4009
				sp->fts_child = NULL;
Packit 6c4009
			}
Packit 6c4009
			p->fts_info = FTS_DP;
Packit 6c4009
			return (p);
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		/* Rebuild if only read the names and now traversing. */
Packit 6c4009
		if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
Packit 6c4009
			CLR(FTS_NAMEONLY);
Packit 6c4009
			fts_lfree(sp->fts_child);
Packit 6c4009
			sp->fts_child = NULL;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		/*
Packit 6c4009
		 * Cd to the subdirectory.
Packit 6c4009
		 *
Packit 6c4009
		 * If have already read and now fail to chdir, whack the list
Packit 6c4009
		 * to make the names come out right, and set the parent errno
Packit 6c4009
		 * so the application will eventually get an error condition.
Packit 6c4009
		 * Set the FTS_DONTCHDIR flag so that when we logically change
Packit 6c4009
		 * directories back to the parent we don't do a chdir.
Packit 6c4009
		 *
Packit 6c4009
		 * If haven't read do so.  If the read fails, fts_build sets
Packit 6c4009
		 * FTS_STOP or the fts_info field of the node.
Packit 6c4009
		 */
Packit 6c4009
		if (sp->fts_child != NULL) {
Packit 6c4009
			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
Packit 6c4009
				p->fts_errno = errno;
Packit 6c4009
				p->fts_flags |= FTS_DONTCHDIR;
Packit 6c4009
				for (p = sp->fts_child; p != NULL;
Packit 6c4009
				     p = p->fts_link)
Packit 6c4009
					p->fts_accpath =
Packit 6c4009
					    p->fts_parent->fts_accpath;
Packit 6c4009
			}
Packit 6c4009
		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
Packit 6c4009
			if (ISSET(FTS_STOP))
Packit 6c4009
				return (NULL);
Packit 6c4009
			return (p);
Packit 6c4009
		}
Packit 6c4009
		p = sp->fts_child;
Packit 6c4009
		sp->fts_child = NULL;
Packit 6c4009
		sp->fts_cur = p;
Packit 6c4009
		goto name;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Move to the next node on this level. */
Packit 6c4009
next:	tmp = p;
Packit 6c4009
	if ((p = p->fts_link) != NULL) {
Packit 6c4009
		sp->fts_cur = p;
Packit 6c4009
		free(tmp);
Packit 6c4009
Packit 6c4009
		/*
Packit 6c4009
		 * If reached the top, return to the original directory (or
Packit 6c4009
		 * the root of the tree), and load the paths for the next root.
Packit 6c4009
		 */
Packit 6c4009
		if (p->fts_level == FTS_ROOTLEVEL) {
Packit 6c4009
			if (FCHDIR(sp, sp->fts_rfd)) {
Packit 6c4009
				SET(FTS_STOP);
Packit 6c4009
				return (NULL);
Packit 6c4009
			}
Packit 6c4009
			fts_load(sp, p);
Packit 6c4009
			return p;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		/*
Packit 6c4009
		 * User may have called fts_set on the node.  If skipped,
Packit 6c4009
		 * ignore.  If followed, get a file descriptor so we can
Packit 6c4009
		 * get back if necessary.
Packit 6c4009
		 */
Packit 6c4009
		if (p->fts_instr == FTS_SKIP)
Packit 6c4009
			goto next;
Packit 6c4009
		if (p->fts_instr == FTS_FOLLOW) {
Packit 6c4009
			p->fts_info = fts_stat(sp, p, 1);
Packit 6c4009
			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
Packit 6c4009
				if ((p->fts_symfd =
Packit 6c4009
				    __open(".", O_RDONLY, 0)) < 0) {
Packit 6c4009
					p->fts_errno = errno;
Packit 6c4009
					p->fts_info = FTS_ERR;
Packit 6c4009
				} else
Packit 6c4009
					p->fts_flags |= FTS_SYMFOLLOW;
Packit 6c4009
			}
Packit 6c4009
			p->fts_instr = FTS_NOINSTR;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
name:		t = sp->fts_path + NAPPEND(p->fts_parent);
Packit 6c4009
		*t++ = '/';
Packit 6c4009
		memmove(t, p->fts_name, p->fts_namelen + 1);
Packit 6c4009
		return p;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Move up to the parent node. */
Packit 6c4009
	p = tmp->fts_parent;
Packit 6c4009
	sp->fts_cur = p;
Packit 6c4009
	free(tmp);
Packit 6c4009
Packit 6c4009
	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
Packit 6c4009
		/*
Packit 6c4009
		 * Done; free everything up and set errno to 0 so the user
Packit 6c4009
		 * can distinguish between error and EOF.
Packit 6c4009
		 */
Packit 6c4009
		free(p);
Packit 6c4009
		__set_errno (0);
Packit 6c4009
		return (sp->fts_cur = NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* NUL terminate the pathname. */
Packit 6c4009
	sp->fts_path[p->fts_pathlen] = '\0';
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Return to the parent directory.  If at a root node or came through
Packit 6c4009
	 * a symlink, go back through the file descriptor.  Otherwise, cd up
Packit 6c4009
	 * one directory.
Packit 6c4009
	 */
Packit 6c4009
	if (p->fts_level == FTS_ROOTLEVEL) {
Packit 6c4009
		if (FCHDIR(sp, sp->fts_rfd)) {
Packit 6c4009
			SET(FTS_STOP);
Packit 6c4009
			return (NULL);
Packit 6c4009
		}
Packit 6c4009
	} else if (p->fts_flags & FTS_SYMFOLLOW) {
Packit 6c4009
		if (FCHDIR(sp, p->fts_symfd)) {
Packit 6c4009
			saved_errno = errno;
Packit 6c4009
			(void)__close(p->fts_symfd);
Packit 6c4009
			__set_errno (saved_errno);
Packit 6c4009
			SET(FTS_STOP);
Packit 6c4009
			return (NULL);
Packit 6c4009
		}
Packit 6c4009
		(void)__close(p->fts_symfd);
Packit 6c4009
	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
Packit 6c4009
		   fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
Packit 6c4009
		SET(FTS_STOP);
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
Packit 6c4009
	return p;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Fts_set takes the stream as an argument although it's not used in this
Packit 6c4009
 * implementation; it would be necessary if anyone wanted to add global
Packit 6c4009
 * semantics to fts using fts_set.  An error return is allowed for similar
Packit 6c4009
 * reasons.
Packit 6c4009
 */
Packit 6c4009
/* ARGSUSED */
Packit 6c4009
int
Packit 6c4009
FTS_SET (FTSOBJ *sp, FTSENTRY *p, int instr)
Packit 6c4009
{
Packit 6c4009
	if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
Packit 6c4009
	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
Packit 6c4009
		__set_errno (EINVAL);
Packit 6c4009
		return (1);
Packit 6c4009
	}
Packit 6c4009
	p->fts_instr = instr;
Packit 6c4009
	return (0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
FTSENTRY *
Packit 6c4009
FTS_CHILDREN(FTSOBJ *sp, int instr)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *p;
Packit 6c4009
	int fd;
Packit 6c4009
Packit 6c4009
	if (instr != 0 && instr != FTS_NAMEONLY) {
Packit 6c4009
		__set_errno (EINVAL);
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Set current node pointer. */
Packit 6c4009
	p = sp->fts_cur;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Errno set to 0 so user can distinguish empty directory from
Packit 6c4009
	 * an error.
Packit 6c4009
	 */
Packit 6c4009
	__set_errno (0);
Packit 6c4009
Packit 6c4009
	/* Fatal errors stop here. */
Packit 6c4009
	if (ISSET(FTS_STOP))
Packit 6c4009
		return (NULL);
Packit 6c4009
Packit 6c4009
	/* Return logical hierarchy of user's arguments. */
Packit 6c4009
	if (p->fts_info == FTS_INIT)
Packit 6c4009
		return (p->fts_link);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If not a directory being visited in pre-order, stop here.  Could
Packit 6c4009
	 * allow FTS_DNR, assuming the user has fixed the problem, but the
Packit 6c4009
	 * same effect is available with FTS_AGAIN.
Packit 6c4009
	 */
Packit 6c4009
	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
Packit 6c4009
		return (NULL);
Packit 6c4009
Packit 6c4009
	/* Free up any previous child list. */
Packit 6c4009
	if (sp->fts_child != NULL)
Packit 6c4009
		fts_lfree(sp->fts_child);
Packit 6c4009
Packit 6c4009
	if (instr == FTS_NAMEONLY) {
Packit 6c4009
		SET(FTS_NAMEONLY);
Packit 6c4009
		instr = BNAMES;
Packit 6c4009
	} else
Packit 6c4009
		instr = BCHILD;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If using chdir on a relative path and called BEFORE fts_read does
Packit 6c4009
	 * its chdir to the root of a traversal, we can lose -- we need to
Packit 6c4009
	 * chdir into the subdirectory, and we don't know where the current
Packit 6c4009
	 * directory is, so we can't get back so that the upcoming chdir by
Packit 6c4009
	 * fts_read will work.
Packit 6c4009
	 */
Packit 6c4009
	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
Packit 6c4009
	    ISSET(FTS_NOCHDIR))
Packit 6c4009
		return (sp->fts_child = fts_build(sp, instr));
Packit 6c4009
Packit 6c4009
	if ((fd = __open(".", O_RDONLY, 0)) < 0)
Packit 6c4009
		return (NULL);
Packit 6c4009
	sp->fts_child = fts_build(sp, instr);
Packit 6c4009
	if (__fchdir(fd))
Packit 6c4009
		return (NULL);
Packit 6c4009
	(void)__close(fd);
Packit 6c4009
	return (sp->fts_child);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
dirent_not_directory(const struct dirent *dp)
Packit 6c4009
{
Packit 6c4009
#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
Packit 6c4009
        return dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN;
Packit 6c4009
#else
Packit 6c4009
        return 0;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * This is the tricky part -- do not casually change *anything* in here.  The
Packit 6c4009
 * idea is to build the linked list of entries that are used by fts_children
Packit 6c4009
 * and fts_read.  There are lots of special cases.
Packit 6c4009
 *
Packit 6c4009
 * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
Packit 6c4009
 * set and it's a physical walk (so that symbolic links can't be directories),
Packit 6c4009
 * we can do things quickly.  First, if it's a 4.4BSD file system, the type
Packit 6c4009
 * of the file is in the directory entry.  Otherwise, we assume that the number
Packit 6c4009
 * of subdirectories in a node is equal to the number of links to the parent.
Packit 6c4009
 * The former skips all stat calls.  The latter skips stat calls in any leaf
Packit 6c4009
 * directories and for any files after the subdirectories in the directory have
Packit 6c4009
 * been found, cutting the stat calls by about 2/3.
Packit 6c4009
 */
Packit 6c4009
static FTSENTRY *
Packit 6c4009
fts_build (FTSOBJ *sp, int type)
Packit 6c4009
{
Packit 6c4009
	struct dirent *dp;
Packit 6c4009
	FTSENTRY *p, *head;
Packit 6c4009
	int nitems;
Packit 6c4009
	FTSENTRY *cur, *tail;
Packit 6c4009
	DIR *dirp;
Packit 6c4009
	void *oldaddr;
Packit 6c4009
	int cderrno, descend, len, level, nlinks, saved_errno,
Packit 6c4009
	    nostat, doadjust;
Packit 6c4009
	size_t maxlen;
Packit 6c4009
	char *cp;
Packit 6c4009
Packit 6c4009
	/* Set current node pointer. */
Packit 6c4009
	cur = sp->fts_cur;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Open the directory for reading.  If this fails, we're done.
Packit 6c4009
	 * If being called from fts_read, set the fts_info field.
Packit 6c4009
	 */
Packit 6c4009
#if defined FTS_WHITEOUT && 0
Packit 6c4009
	if (ISSET(FTS_WHITEOUT))
Packit 6c4009
		oflag = DTF_NODUP|DTF_REWIND;
Packit 6c4009
	else
Packit 6c4009
		oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
Packit 6c4009
#else
Packit 6c4009
# define __opendir2(path, flag) __opendir(path)
Packit 6c4009
#endif
Packit 6c4009
       if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
Packit 6c4009
		if (type == BREAD) {
Packit 6c4009
			cur->fts_info = FTS_DNR;
Packit 6c4009
			cur->fts_errno = errno;
Packit 6c4009
		}
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Nlinks is the number of possible entries of type directory in the
Packit 6c4009
	 * directory if we're cheating on stat calls, 0 if we're not doing
Packit 6c4009
	 * any stat calls at all, -1 if we're doing stats on everything.
Packit 6c4009
	 */
Packit 6c4009
	if (type == BNAMES) {
Packit 6c4009
		nlinks = 0;
Packit 6c4009
		/* Be quiet about nostat, GCC. */
Packit 6c4009
		nostat = 0;
Packit 6c4009
	} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
Packit 6c4009
		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
Packit 6c4009
		nostat = 1;
Packit 6c4009
	} else {
Packit 6c4009
		nlinks = -1;
Packit 6c4009
		nostat = 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#ifdef notdef
Packit 6c4009
	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
Packit 6c4009
	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
Packit 6c4009
	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
Packit 6c4009
#endif
Packit 6c4009
	/*
Packit 6c4009
	 * If we're going to need to stat anything or we want to descend
Packit 6c4009
	 * and stay in the directory, chdir.  If this fails we keep going,
Packit 6c4009
	 * but set a flag so we don't chdir after the post-order visit.
Packit 6c4009
	 * We won't be able to stat anything, but we can still return the
Packit 6c4009
	 * names themselves.  Note, that since fts_read won't be able to
Packit 6c4009
	 * chdir into the directory, it will have to return different path
Packit 6c4009
	 * names than before, i.e. "a/b" instead of "b".  Since the node
Packit 6c4009
	 * has already been visited in pre-order, have to wait until the
Packit 6c4009
	 * post-order visit to return the error.  There is a special case
Packit 6c4009
	 * here, if there was nothing to stat then it's not an error to
Packit 6c4009
	 * not be able to stat.  This is all fairly nasty.  If a program
Packit 6c4009
	 * needed sorted entries or stat information, they had better be
Packit 6c4009
	 * checking FTS_NS on the returned nodes.
Packit 6c4009
	 */
Packit 6c4009
	cderrno = 0;
Packit 6c4009
	if (nlinks || type == BREAD) {
Packit 6c4009
		if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
Packit 6c4009
			if (nlinks && type == BREAD)
Packit 6c4009
				cur->fts_errno = errno;
Packit 6c4009
			cur->fts_flags |= FTS_DONTCHDIR;
Packit 6c4009
			descend = 0;
Packit 6c4009
			cderrno = errno;
Packit 6c4009
			(void)__closedir(dirp);
Packit 6c4009
			dirp = NULL;
Packit 6c4009
		} else
Packit 6c4009
			descend = 1;
Packit 6c4009
	} else
Packit 6c4009
		descend = 0;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Figure out the max file name length that can be stored in the
Packit 6c4009
	 * current path -- the inner loop allocates more path as necessary.
Packit 6c4009
	 * We really wouldn't have to do the maxlen calculations here, we
Packit 6c4009
	 * could do them in fts_read before returning the path, but it's a
Packit 6c4009
	 * lot easier here since the length is part of the dirent structure.
Packit 6c4009
	 *
Packit 6c4009
	 * If not changing directories set a pointer so that can just append
Packit 6c4009
	 * each new name into the path.
Packit 6c4009
	 */
Packit 6c4009
	len = NAPPEND(cur);
Packit 6c4009
	if (ISSET(FTS_NOCHDIR)) {
Packit 6c4009
		cp = sp->fts_path + len;
Packit 6c4009
		*cp++ = '/';
Packit 6c4009
	} else {
Packit 6c4009
		/* GCC, you're too verbose. */
Packit 6c4009
		cp = NULL;
Packit 6c4009
	}
Packit 6c4009
	len++;
Packit 6c4009
	maxlen = sp->fts_pathlen - len;
Packit 6c4009
Packit 6c4009
	level = cur->fts_level + 1;
Packit 6c4009
Packit 6c4009
	/* Read the directory, attaching each entry to the `link' pointer. */
Packit 6c4009
	doadjust = 0;
Packit 6c4009
	for (head = tail = NULL, nitems = 0; dirp && (dp = __readdir(dirp));) {
Packit 6c4009
		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
Packit 6c4009
			continue;
Packit 6c4009
Packit 6c4009
		if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
Packit 6c4009
			goto mem1;
Packit 6c4009
		if (_D_EXACT_NAMLEN (dp) >= maxlen) {/* include space for NUL */
Packit 6c4009
			oldaddr = sp->fts_path;
Packit 6c4009
			if (fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) {
Packit 6c4009
				/*
Packit 6c4009
				 * No more memory for path or structures.  Save
Packit 6c4009
				 * errno, free up the current structure and the
Packit 6c4009
				 * structures already allocated.
Packit 6c4009
				 */
Packit 6c4009
mem1:				saved_errno = errno;
Packit 6c4009
				free(p);
Packit 6c4009
				fts_lfree(head);
Packit 6c4009
				(void)__closedir(dirp);
Packit 6c4009
				cur->fts_info = FTS_ERR;
Packit 6c4009
				SET(FTS_STOP);
Packit 6c4009
				__set_errno (saved_errno);
Packit 6c4009
				return (NULL);
Packit 6c4009
			}
Packit 6c4009
			/* Did realloc() change the pointer? */
Packit 6c4009
			if (oldaddr != sp->fts_path) {
Packit 6c4009
				doadjust = 1;
Packit 6c4009
				if (ISSET(FTS_NOCHDIR))
Packit 6c4009
					cp = sp->fts_path + len;
Packit 6c4009
			}
Packit 6c4009
			maxlen = sp->fts_pathlen - len;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
Packit 6c4009
			/*
Packit 6c4009
			 * In an FTSENT, fts_pathlen is a u_short so it is
Packit 6c4009
			 * possible to wraparound here.  If we do, free up
Packit 6c4009
			 * the current structure and the structures already
Packit 6c4009
			 * allocated, then error out with ENAMETOOLONG.
Packit 6c4009
			 */
Packit 6c4009
			free(p);
Packit 6c4009
			fts_lfree(head);
Packit 6c4009
			(void)__closedir(dirp);
Packit 6c4009
			cur->fts_info = FTS_ERR;
Packit 6c4009
			SET(FTS_STOP);
Packit 6c4009
			__set_errno (ENAMETOOLONG);
Packit 6c4009
			return (NULL);
Packit 6c4009
		}
Packit 6c4009
		p->fts_level = level;
Packit 6c4009
		p->fts_parent = sp->fts_cur;
Packit 6c4009
		p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
Packit 6c4009
Packit 6c4009
#if defined FTS_WHITEOUT && 0
Packit 6c4009
		if (dp->d_type == DT_WHT)
Packit 6c4009
			p->fts_flags |= FTS_ISW;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
		/* Unreachable code.  cderrno is only ever set to a nonnull
Packit 6c4009
		   value if dirp is closed at the same time.  But then we
Packit 6c4009
		   cannot enter this loop.  */
Packit 6c4009
		if (0 && cderrno) {
Packit 6c4009
			if (nlinks) {
Packit 6c4009
				p->fts_info = FTS_NS;
Packit 6c4009
				p->fts_errno = cderrno;
Packit 6c4009
			} else
Packit 6c4009
				p->fts_info = FTS_NSOK;
Packit 6c4009
			p->fts_accpath = cur->fts_accpath;
Packit 6c4009
		} else if (nlinks == 0
Packit 6c4009
                           || (nostat && dirent_not_directory(dp))) {
Packit 6c4009
			p->fts_accpath =
Packit 6c4009
			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
Packit 6c4009
			p->fts_info = FTS_NSOK;
Packit 6c4009
		} else {
Packit 6c4009
			/* Build a file name for fts_stat to stat. */
Packit 6c4009
			if (ISSET(FTS_NOCHDIR)) {
Packit 6c4009
				p->fts_accpath = p->fts_path;
Packit 6c4009
				memmove(cp, p->fts_name, p->fts_namelen + 1);
Packit 6c4009
			} else
Packit 6c4009
				p->fts_accpath = p->fts_name;
Packit 6c4009
			/* Stat it. */
Packit 6c4009
			p->fts_info = fts_stat(sp, p, 0);
Packit 6c4009
Packit 6c4009
			/* Decrement link count if applicable. */
Packit 6c4009
			if (nlinks > 0 && (p->fts_info == FTS_D ||
Packit 6c4009
			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
Packit 6c4009
				--nlinks;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
		/* We walk in directory order so "ls -f" doesn't get upset. */
Packit 6c4009
		p->fts_link = NULL;
Packit 6c4009
		if (head == NULL)
Packit 6c4009
			head = tail = p;
Packit 6c4009
		else {
Packit 6c4009
			tail->fts_link = p;
Packit 6c4009
			tail = p;
Packit 6c4009
		}
Packit 6c4009
		++nitems;
Packit 6c4009
	}
Packit 6c4009
	if (dirp)
Packit 6c4009
		(void)__closedir(dirp);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If realloc() changed the address of the path, adjust the
Packit 6c4009
	 * addresses for the rest of the tree and the dir list.
Packit 6c4009
	 */
Packit 6c4009
	if (doadjust)
Packit 6c4009
		fts_padjust(sp, head);
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If not changing directories, reset the path back to original
Packit 6c4009
	 * state.
Packit 6c4009
	 */
Packit 6c4009
	if (ISSET(FTS_NOCHDIR)) {
Packit 6c4009
		if (len == sp->fts_pathlen || nitems == 0)
Packit 6c4009
			--cp;
Packit 6c4009
		*cp = '\0';
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If descended after called from fts_children or after called from
Packit 6c4009
	 * fts_read and nothing found, get back.  At the root level we use
Packit 6c4009
	 * the saved fd; if one of fts_open()'s arguments is a relative path
Packit 6c4009
	 * to an empty directory, we wind up here with no other way back.  If
Packit 6c4009
	 * can't get back, we're done.
Packit 6c4009
	 */
Packit 6c4009
	if (descend && (type == BCHILD || !nitems) &&
Packit 6c4009
	    (cur->fts_level == FTS_ROOTLEVEL ?
Packit 6c4009
	     FCHDIR(sp, sp->fts_rfd) :
Packit 6c4009
	     fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
Packit 6c4009
		cur->fts_info = FTS_ERR;
Packit 6c4009
		SET(FTS_STOP);
Packit 6c4009
		fts_lfree(head);
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* If didn't find anything, return NULL. */
Packit 6c4009
	if (!nitems) {
Packit 6c4009
		if (type == BREAD)
Packit 6c4009
			cur->fts_info = FTS_DP;
Packit 6c4009
		fts_lfree(head);
Packit 6c4009
		return (NULL);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Sort the entries. */
Packit 6c4009
	if (sp->fts_compar && nitems > 1)
Packit 6c4009
		head = fts_sort(sp, head, nitems);
Packit 6c4009
	return (head);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static u_short
Packit 6c4009
fts_stat (FTSOBJ *sp, FTSENTRY *p, int follow)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *t;
Packit 6c4009
	dev_t dev;
Packit 6c4009
	INO_T ino;
Packit 6c4009
	struct STAT *sbp, sb;
Packit 6c4009
	int saved_errno;
Packit 6c4009
Packit 6c4009
	/* If user needs stat info, stat buffer already allocated. */
Packit 6c4009
	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
Packit 6c4009
Packit 6c4009
#if defined FTS_WHITEOUT && 0
Packit 6c4009
	/* check for whiteout */
Packit 6c4009
	if (p->fts_flags & FTS_ISW) {
Packit 6c4009
		if (sbp != &sb) {
Packit 6c4009
			memset(sbp, '\0', sizeof (*sbp));
Packit 6c4009
			sbp->st_mode = S_IFWHT;
Packit 6c4009
		}
Packit 6c4009
		return (FTS_W);
Packit 6c4009
       }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * If doing a logical walk, or application requested FTS_FOLLOW, do
Packit 6c4009
	 * a stat(2).  If that fails, check for a non-existent symlink.  If
Packit 6c4009
	 * fail, set the errno from the stat call.
Packit 6c4009
	 */
Packit 6c4009
	if (ISSET(FTS_LOGICAL) || follow) {
Packit 6c4009
		if (STAT(p->fts_accpath, sbp)) {
Packit 6c4009
			saved_errno = errno;
Packit 6c4009
			if (!LSTAT(p->fts_accpath, sbp)) {
Packit 6c4009
				__set_errno (0);
Packit 6c4009
				return (FTS_SLNONE);
Packit 6c4009
			}
Packit 6c4009
			p->fts_errno = saved_errno;
Packit 6c4009
			goto err;
Packit 6c4009
		}
Packit 6c4009
	} else if (LSTAT(p->fts_accpath, sbp)) {
Packit 6c4009
		p->fts_errno = errno;
Packit 6c4009
err:		memset(sbp, 0, sizeof(struct STAT));
Packit 6c4009
		return (FTS_NS);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	if (S_ISDIR(sbp->st_mode)) {
Packit 6c4009
		/*
Packit 6c4009
		 * Set the device/inode.  Used to find cycles and check for
Packit 6c4009
		 * crossing mount points.  Also remember the link count, used
Packit 6c4009
		 * in fts_build to limit the number of stat calls.  It is
Packit 6c4009
		 * understood that these fields are only referenced if fts_info
Packit 6c4009
		 * is set to FTS_D.
Packit 6c4009
		 */
Packit 6c4009
		dev = p->fts_dev = sbp->st_dev;
Packit 6c4009
		ino = p->fts_ino = sbp->st_ino;
Packit 6c4009
		p->fts_nlink = sbp->st_nlink;
Packit 6c4009
Packit 6c4009
		if (ISDOT(p->fts_name))
Packit 6c4009
			return (FTS_DOT);
Packit 6c4009
Packit 6c4009
		/*
Packit 6c4009
		 * Cycle detection is done by brute force when the directory
Packit 6c4009
		 * is first encountered.  If the tree gets deep enough or the
Packit 6c4009
		 * number of symbolic links to directories is high enough,
Packit 6c4009
		 * something faster might be worthwhile.
Packit 6c4009
		 */
Packit 6c4009
		for (t = p->fts_parent;
Packit 6c4009
		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
Packit 6c4009
			if (ino == t->fts_ino && dev == t->fts_dev) {
Packit 6c4009
				p->fts_cycle = t;
Packit 6c4009
				return (FTS_DC);
Packit 6c4009
			}
Packit 6c4009
		return (FTS_D);
Packit 6c4009
	}
Packit 6c4009
	if (S_ISLNK(sbp->st_mode))
Packit 6c4009
		return (FTS_SL);
Packit 6c4009
	if (S_ISREG(sbp->st_mode))
Packit 6c4009
		return (FTS_F);
Packit 6c4009
	return (FTS_DEFAULT);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static FTSENTRY *
Packit 6c4009
fts_sort (FTSOBJ *sp, FTSENTRY *head, int nitems)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY **ap, *p;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Construct an array of pointers to the structures and call qsort(3).
Packit 6c4009
	 * Reassemble the array in the order returned by qsort.  If unable to
Packit 6c4009
	 * sort for memory reasons, return the directory entries in their
Packit 6c4009
	 * current order.  Allocate enough space for the current needs plus
Packit 6c4009
	 * 40 so don't realloc one entry at a time.
Packit 6c4009
	 */
Packit 6c4009
	if (nitems > sp->fts_nitems) {
Packit 6c4009
		FTSENTRY **a;
Packit 6c4009
Packit 6c4009
		sp->fts_nitems = nitems + 40;
Packit 6c4009
		if ((a = realloc(sp->fts_array,
Packit 6c4009
		    (size_t)(sp->fts_nitems * sizeof(FTSENTRY *)))) == NULL) {
Packit 6c4009
			free(sp->fts_array);
Packit 6c4009
			sp->fts_array = NULL;
Packit 6c4009
			sp->fts_nitems = 0;
Packit 6c4009
			return (head);
Packit 6c4009
		}
Packit 6c4009
		sp->fts_array = a;
Packit 6c4009
	}
Packit 6c4009
	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
Packit 6c4009
		*ap++ = p;
Packit 6c4009
	qsort((void *)sp->fts_array, nitems, sizeof(FTSENTRY *), sp->fts_compar);
Packit 6c4009
	for (head = *(ap = sp->fts_array); --nitems; ++ap)
Packit 6c4009
		ap[0]->fts_link = ap[1];
Packit 6c4009
	ap[0]->fts_link = NULL;
Packit 6c4009
	return (head);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static FTSENTRY *
Packit 6c4009
fts_alloc (FTSOBJ *sp, const char *name, size_t namelen)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *p;
Packit 6c4009
	size_t len;
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * The file name is a variable length array and no stat structure is
Packit 6c4009
	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
Packit 6c4009
	 * structure, the file name and the stat structure in one chunk, but
Packit 6c4009
	 * be careful that the stat structure is reasonably aligned.  Since the
Packit 6c4009
	 * fts_name field is declared to be of size 1, the fts_name pointer is
Packit 6c4009
	 * namelen + 2 before the first possible address of the stat structure.
Packit 6c4009
	 */
Packit 6c4009
	len = sizeof(FTSENTRY) + namelen;
Packit 6c4009
	if (!ISSET(FTS_NOSTAT))
Packit 6c4009
		len += sizeof(struct STAT) + ALIGNBYTES;
Packit 6c4009
	if ((p = malloc(len)) == NULL)
Packit 6c4009
		return (NULL);
Packit 6c4009
Packit 6c4009
	/* Copy the name and guarantee NUL termination. */
Packit 6c4009
	memmove(p->fts_name, name, namelen);
Packit 6c4009
	p->fts_name[namelen] = '\0';
Packit 6c4009
Packit 6c4009
	if (!ISSET(FTS_NOSTAT))
Packit 6c4009
		p->fts_statp = (struct STAT *)ALIGN(p->fts_name + namelen + 2);
Packit 6c4009
	p->fts_namelen = namelen;
Packit 6c4009
	p->fts_path = sp->fts_path;
Packit 6c4009
	p->fts_errno = 0;
Packit 6c4009
	p->fts_flags = 0;
Packit 6c4009
	p->fts_instr = FTS_NOINSTR;
Packit 6c4009
	p->fts_number = 0;
Packit 6c4009
	p->fts_pointer = NULL;
Packit 6c4009
	return (p);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fts_lfree (FTSENTRY *head)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *p;
Packit 6c4009
Packit 6c4009
	/* Free a linked list of structures. */
Packit 6c4009
	while ((p = head)) {
Packit 6c4009
		head = head->fts_link;
Packit 6c4009
		free(p);
Packit 6c4009
	}
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
Packit 6c4009
 * Most systems will allow creation of paths much longer than MAXPATHLEN, even
Packit 6c4009
 * though the kernel won't resolve them.  Add the size (not just what's needed)
Packit 6c4009
 * plus 256 bytes so don't realloc the path 2 bytes at a time.
Packit 6c4009
 */
Packit 6c4009
static int
Packit 6c4009
fts_palloc (FTSOBJ *sp, size_t more)
Packit 6c4009
{
Packit 6c4009
	char *p;
Packit 6c4009
Packit 6c4009
	sp->fts_pathlen += more + 256;
Packit 6c4009
	/*
Packit 6c4009
	 * Check for possible wraparound.  In an FTS, fts_pathlen is
Packit 6c4009
	 * a signed int but in an FTSENT it is an unsigned short.
Packit 6c4009
	 * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
Packit 6c4009
	 */
Packit 6c4009
	if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
Packit 6c4009
		free(sp->fts_path);
Packit 6c4009
		sp->fts_path = NULL;
Packit 6c4009
		__set_errno (ENAMETOOLONG);
Packit 6c4009
		return (1);
Packit 6c4009
	}
Packit 6c4009
	p = realloc(sp->fts_path, sp->fts_pathlen);
Packit 6c4009
	if (p == NULL) {
Packit 6c4009
		free(sp->fts_path);
Packit 6c4009
		sp->fts_path = NULL;
Packit 6c4009
		return 1;
Packit 6c4009
	}
Packit 6c4009
	sp->fts_path = p;
Packit 6c4009
	return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * When the path is realloc'd, have to fix all of the pointers in structures
Packit 6c4009
 * already returned.
Packit 6c4009
 */
Packit 6c4009
static void
Packit 6c4009
fts_padjust (FTSOBJ *sp, FTSENTRY *head)
Packit 6c4009
{
Packit 6c4009
	FTSENTRY *p;
Packit 6c4009
	char *addr = sp->fts_path;
Packit 6c4009
Packit 6c4009
#define	ADJUST(p) do {							\
Packit 6c4009
	if ((p)->fts_accpath != (p)->fts_name) {			\
Packit 6c4009
		(p)->fts_accpath =					\
Packit 6c4009
		    (char *)addr + ((p)->fts_accpath - (p)->fts_path);	\
Packit 6c4009
	}								\
Packit 6c4009
	(p)->fts_path = addr;						\
Packit 6c4009
} while (0)
Packit 6c4009
	/* Adjust the current set of children. */
Packit 6c4009
	for (p = sp->fts_child; p; p = p->fts_link)
Packit 6c4009
		ADJUST(p);
Packit 6c4009
Packit 6c4009
	/* Adjust the rest of the tree, including the current level. */
Packit 6c4009
	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
Packit 6c4009
		ADJUST(p);
Packit 6c4009
		p = p->fts_link ? p->fts_link : p->fts_parent;
Packit 6c4009
	}
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static size_t
Packit 6c4009
fts_maxarglen (char * const *argv)
Packit 6c4009
{
Packit 6c4009
	size_t len, max;
Packit 6c4009
Packit 6c4009
	for (max = 0; *argv; ++argv)
Packit 6c4009
		if ((len = strlen(*argv)) > max)
Packit 6c4009
			max = len;
Packit 6c4009
	return (max + 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Change to dir specified by fd or p->fts_accpath without getting
Packit 6c4009
 * tricked by someone changing the world out from underneath us.
Packit 6c4009
 * Assumes p->fts_dev and p->fts_ino are filled in.
Packit 6c4009
 */
Packit 6c4009
static int
Packit 6c4009
fts_safe_changedir (FTSOBJ *sp, FTSENTRY *p, int fd, const char *path)
Packit 6c4009
{
Packit 6c4009
	int ret, oerrno, newfd;
Packit 6c4009
	struct stat64 sb;
Packit 6c4009
Packit 6c4009
	newfd = fd;
Packit 6c4009
	if (ISSET(FTS_NOCHDIR))
Packit 6c4009
		return (0);
Packit 6c4009
	if (fd < 0 && (newfd = __open(path, O_RDONLY, 0)) < 0)
Packit 6c4009
		return (-1);
Packit 6c4009
	if (__fxstat64(_STAT_VER, newfd, &sb)) {
Packit 6c4009
		ret = -1;
Packit 6c4009
		goto bail;
Packit 6c4009
	}
Packit 6c4009
	if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
Packit 6c4009
		__set_errno (ENOENT);		/* disinformation */
Packit 6c4009
		ret = -1;
Packit 6c4009
		goto bail;
Packit 6c4009
	}
Packit 6c4009
	ret = __fchdir(newfd);
Packit 6c4009
bail:
Packit 6c4009
	oerrno = errno;
Packit 6c4009
	if (fd < 0)
Packit 6c4009
		(void)__close(newfd);
Packit 6c4009
	__set_errno (oerrno);
Packit 6c4009
	return (ret);
Packit 6c4009
}