Blame extension/gawkfts.c

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