Home | History | Annotate | Line # | Download | only in gen
      1  1.52    rillig /*	$NetBSD: fts.c,v 1.52 2022/04/19 20:32:15 rillig Exp $	*/
      2  1.12       cgd 
      3  1.25  christos /*-
      4  1.25  christos  * Copyright (c) 1990, 1993, 1994
      5  1.25  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.25  christos  *
      7  1.25  christos  * Redistribution and use in source and binary forms, with or without
      8  1.25  christos  * modification, are permitted provided that the following conditions
      9  1.25  christos  * are met:
     10  1.25  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.25  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.25  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.25  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.25  christos  *    documentation and/or other materials provided with the distribution.
     15  1.25  christos  * 3. Neither the name of the University nor the names of its contributors
     16  1.25  christos  *    may be used to endorse or promote products derived from this software
     17  1.25  christos  *    without specific prior written permission.
     18  1.25  christos  *
     19  1.25  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.25  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.25  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.25  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.25  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.25  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.25  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.25  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.25  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.25  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.25  christos  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.25  christos #if HAVE_NBTOOL_CONFIG_H
     33  1.25  christos #include "nbtool_config.h"
     34  1.25  christos #endif
     35  1.25  christos 
     36  1.25  christos #include <sys/cdefs.h>
     37  1.25  christos #if defined(LIBC_SCCS) && !defined(lint)
     38  1.25  christos #if 0
     39  1.25  christos static char sccsid[] = "@(#)fts.c	8.6 (Berkeley) 8/14/94";
     40  1.25  christos #else
     41  1.52    rillig __RCSID("$NetBSD: fts.c,v 1.52 2022/04/19 20:32:15 rillig Exp $");
     42  1.25  christos #endif
     43  1.25  christos #endif /* LIBC_SCCS and not lint */
     44  1.25  christos 
     45  1.24  christos #include "namespace.h"
     46  1.25  christos #include <sys/param.h>
     47  1.25  christos #include <sys/stat.h>
     48  1.25  christos 
     49  1.25  christos #include <assert.h>
     50  1.24  christos #include <dirent.h>
     51  1.25  christos #include <errno.h>
     52  1.25  christos #include <fcntl.h>
     53  1.25  christos #include <fts.h>
     54  1.25  christos #include <stdlib.h>
     55  1.25  christos #include <string.h>
     56  1.25  christos #include <unistd.h>
     57  1.25  christos 
     58  1.25  christos #if ! HAVE_NBTOOL_CONFIG_H
     59  1.32     lukem #define	HAVE_STRUCT_DIRENT_D_NAMLEN
     60  1.25  christos #endif
     61  1.25  christos 
     62  1.28  christos static FTSENT	*fts_alloc(FTS *, const char *, size_t);
     63  1.28  christos static FTSENT	*fts_build(FTS *, int);
     64  1.33     lukem static void	 fts_free(FTSENT *);
     65  1.28  christos static void	 fts_lfree(FTSENT *);
     66  1.28  christos static void	 fts_load(FTS *, FTSENT *);
     67  1.28  christos static size_t	 fts_maxarglen(char * const *);
     68  1.28  christos static size_t	 fts_pow2(size_t);
     69  1.28  christos static int	 fts_palloc(FTS *, size_t);
     70  1.28  christos static void	 fts_padjust(FTS *, FTSENT *);
     71  1.28  christos static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
     72  1.32     lukem static unsigned short fts_stat(FTS *, FTSENT *, int);
     73  1.28  christos static int	 fts_safe_changedir(const FTS *, const FTSENT *, int,
     74  1.28  christos     const char *);
     75  1.25  christos 
     76  1.33     lukem #if defined(ALIGNBYTES) && defined(ALIGN)
     77  1.33     lukem #define	FTS_ALLOC_ALIGNED	1
     78  1.33     lukem #else
     79  1.33     lukem #undef	FTS_ALLOC_ALIGNED
     80  1.33     lukem #endif
     81  1.33     lukem 
     82  1.44  christos #ifndef ftsent_namelen_truncate
     83  1.44  christos #define ftsent_namelen_truncate(a)	\
     84  1.44  christos     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
     85  1.44  christos #endif
     86  1.44  christos #ifndef ftsent_pathlen_truncate
     87  1.44  christos #define ftsent_pathlen_truncate(a) \
     88  1.43  christos     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
     89  1.43  christos #endif
     90  1.43  christos #ifndef fts_pathlen_truncate
     91  1.43  christos #define fts_pathlen_truncate(a)	\
     92  1.43  christos     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
     93  1.43  christos #endif
     94  1.44  christos #ifndef fts_nitems_truncate
     95  1.44  christos #define fts_nitems_truncate(a) \
     96  1.43  christos     ((a) > UINT_MAX ? UINT_MAX : (unsigned int)(a))
     97  1.43  christos #endif
     98  1.43  christos 
     99  1.25  christos #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
    100  1.25  christos 
    101  1.25  christos #define	CLR(opt)	(sp->fts_options &= ~(opt))
    102  1.25  christos #define	ISSET(opt)	(sp->fts_options & (opt))
    103  1.25  christos #define	SET(opt)	(sp->fts_options |= (opt))
    104  1.25  christos 
    105  1.25  christos #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
    106  1.25  christos #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
    107  1.25  christos 
    108  1.25  christos /* fts_build flags */
    109  1.25  christos #define	BCHILD		1		/* fts_children */
    110  1.25  christos #define	BNAMES		2		/* fts_children, names only */
    111  1.25  christos #define	BREAD		3		/* fts_read */
    112  1.25  christos 
    113  1.25  christos #ifndef DTF_HIDEW
    114  1.25  christos #undef FTS_WHITEOUT
    115  1.25  christos #endif
    116  1.25  christos 
    117  1.25  christos FTS *
    118  1.28  christos fts_open(char * const *argv, int options,
    119  1.28  christos     int (*compar)(const FTSENT **, const FTSENT **))
    120  1.25  christos {
    121  1.25  christos 	FTS *sp;
    122  1.25  christos 	FTSENT *p, *root;
    123  1.25  christos 	size_t nitems;
    124  1.25  christos 	FTSENT *parent, *tmp = NULL;	/* pacify gcc */
    125  1.25  christos 	size_t len;
    126  1.25  christos 
    127  1.25  christos 	_DIAGASSERT(argv != NULL);
    128  1.25  christos 
    129  1.25  christos 	/* Options check. */
    130  1.25  christos 	if (options & ~FTS_OPTIONMASK) {
    131  1.25  christos 		errno = EINVAL;
    132  1.25  christos 		return (NULL);
    133  1.25  christos 	}
    134  1.25  christos 
    135  1.25  christos 	/* Allocate/initialize the stream */
    136  1.49  pgoyette 	if ((sp = calloc(1, sizeof(FTS))) == NULL)
    137  1.25  christos 		return (NULL);
    138  1.25  christos 	sp->fts_compar = compar;
    139  1.25  christos 	sp->fts_options = options;
    140  1.25  christos 
    141  1.25  christos 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
    142  1.25  christos 	if (ISSET(FTS_LOGICAL))
    143  1.25  christos 		SET(FTS_NOCHDIR);
    144  1.25  christos 
    145  1.25  christos 	/*
    146  1.25  christos 	 * Start out with 1K of path space, and enough, in any case,
    147  1.25  christos 	 * to hold the user's paths.
    148  1.25  christos 	 */
    149  1.25  christos 	if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
    150  1.25  christos 		goto mem1;
    151  1.25  christos 
    152  1.25  christos 	/* Allocate/initialize root's parent. */
    153  1.25  christos 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
    154  1.25  christos 		goto mem2;
    155  1.25  christos 	parent->fts_level = FTS_ROOTPARENTLEVEL;
    156  1.25  christos 
    157  1.25  christos 	/* Allocate/initialize root(s). */
    158  1.25  christos 	for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
    159  1.25  christos 		/* Don't allow zero-length paths. */
    160  1.25  christos 		if ((len = strlen(*argv)) == 0) {
    161  1.25  christos 			errno = ENOENT;
    162  1.25  christos 			goto mem3;
    163  1.25  christos 		}
    164  1.25  christos 
    165  1.25  christos 		if ((p = fts_alloc(sp, *argv, len)) == NULL)
    166  1.25  christos 			goto mem3;
    167  1.25  christos 		p->fts_level = FTS_ROOTLEVEL;
    168  1.25  christos 		p->fts_parent = parent;
    169  1.25  christos 		p->fts_accpath = p->fts_name;
    170  1.25  christos 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
    171  1.25  christos 
    172  1.25  christos 		/* Command-line "." and ".." are real directories. */
    173  1.25  christos 		if (p->fts_info == FTS_DOT)
    174  1.25  christos 			p->fts_info = FTS_D;
    175  1.25  christos 
    176  1.25  christos 		/*
    177  1.25  christos 		 * If comparison routine supplied, traverse in sorted
    178  1.25  christos 		 * order; otherwise traverse in the order specified.
    179  1.25  christos 		 */
    180  1.25  christos 		if (compar) {
    181  1.25  christos 			p->fts_link = root;
    182  1.25  christos 			root = p;
    183  1.25  christos 		} else {
    184  1.25  christos 			p->fts_link = NULL;
    185  1.25  christos 			if (root == NULL)
    186  1.25  christos 				tmp = root = p;
    187  1.25  christos 			else {
    188  1.25  christos 				tmp->fts_link = p;
    189  1.25  christos 				tmp = p;
    190  1.25  christos 			}
    191  1.25  christos 		}
    192  1.25  christos 	}
    193  1.25  christos 	if (compar && nitems > 1)
    194  1.25  christos 		root = fts_sort(sp, root, nitems);
    195  1.25  christos 
    196  1.25  christos 	/*
    197  1.25  christos 	 * Allocate a dummy pointer and make fts_read think that we've just
    198  1.25  christos 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
    199  1.25  christos 	 * so that everything about the "current" node is ignored.
    200  1.25  christos 	 */
    201  1.25  christos 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
    202  1.25  christos 		goto mem3;
    203  1.25  christos 	sp->fts_cur->fts_link = root;
    204  1.25  christos 	sp->fts_cur->fts_info = FTS_INIT;
    205  1.25  christos 
    206  1.25  christos 	/*
    207  1.46   msaitoh 	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
    208  1.25  christos 	 * that we can get back here; this could be avoided for some paths,
    209  1.25  christos 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
    210  1.25  christos 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
    211  1.25  christos 	 * descriptor we run anyway, just more slowly.
    212  1.25  christos 	 */
    213  1.42       mrg #ifndef O_CLOEXEC
    214  1.42       mrg #define O_CLOEXEC 0
    215  1.42       mrg #endif
    216  1.25  christos 	if (!ISSET(FTS_NOCHDIR)) {
    217  1.41  christos 		if ((sp->fts_rfd = open(".", O_RDONLY | O_CLOEXEC, 0)) == -1)
    218  1.25  christos 			SET(FTS_NOCHDIR);
    219  1.25  christos 	}
    220  1.25  christos 
    221  1.30  christos 	if (nitems == 0)
    222  1.33     lukem 		fts_free(parent);
    223  1.30  christos 
    224  1.25  christos 	return (sp);
    225  1.25  christos 
    226  1.25  christos mem3:	fts_lfree(root);
    227  1.33     lukem 	fts_free(parent);
    228  1.25  christos mem2:	free(sp->fts_path);
    229  1.25  christos mem1:	free(sp);
    230  1.25  christos 	return (NULL);
    231  1.25  christos }
    232  1.25  christos 
    233  1.25  christos static void
    234  1.28  christos fts_load(FTS *sp, FTSENT *p)
    235  1.25  christos {
    236  1.25  christos 	size_t len;
    237  1.25  christos 	char *cp;
    238  1.25  christos 
    239  1.25  christos 	_DIAGASSERT(sp != NULL);
    240  1.25  christos 	_DIAGASSERT(p != NULL);
    241  1.25  christos 
    242  1.25  christos 	/*
    243  1.25  christos 	 * Load the stream structure for the next traversal.  Since we don't
    244  1.25  christos 	 * actually enter the directory until after the preorder visit, set
    245  1.25  christos 	 * the fts_accpath field specially so the chdir gets done to the right
    246  1.25  christos 	 * place and the user can access the first node.  From fts_open it's
    247  1.25  christos 	 * known that the path will fit.
    248  1.25  christos 	 */
    249  1.25  christos 	len = p->fts_pathlen = p->fts_namelen;
    250  1.25  christos 	memmove(sp->fts_path, p->fts_name, len + 1);
    251  1.25  christos 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
    252  1.25  christos 		len = strlen(++cp);
    253  1.25  christos 		memmove(p->fts_name, cp, len + 1);
    254  1.44  christos 		p->fts_namelen = ftsent_namelen_truncate(len);
    255  1.25  christos 	}
    256  1.25  christos 	p->fts_accpath = p->fts_path = sp->fts_path;
    257  1.25  christos 	sp->fts_dev = p->fts_dev;
    258  1.25  christos }
    259  1.25  christos 
    260  1.25  christos int
    261  1.28  christos fts_close(FTS *sp)
    262  1.25  christos {
    263  1.25  christos 	FTSENT *freep, *p;
    264  1.29  christos 	int saved_errno = 0;
    265  1.25  christos 
    266  1.25  christos 	_DIAGASSERT(sp != NULL);
    267  1.25  christos 
    268  1.25  christos 	/*
    269  1.25  christos 	 * This still works if we haven't read anything -- the dummy structure
    270  1.25  christos 	 * points to the root list, so we step through to the end of the root
    271  1.25  christos 	 * list which has a valid parent pointer.
    272  1.25  christos 	 */
    273  1.25  christos 	if (sp->fts_cur) {
    274  1.35     lukem 		if (sp->fts_cur->fts_flags & FTS_SYMFOLLOW)
    275  1.25  christos 			(void)close(sp->fts_cur->fts_symfd);
    276  1.25  christos 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
    277  1.25  christos 			freep = p;
    278  1.25  christos 			p = p->fts_link ? p->fts_link : p->fts_parent;
    279  1.33     lukem 			fts_free(freep);
    280  1.25  christos 		}
    281  1.33     lukem 		fts_free(p);
    282  1.25  christos 	}
    283  1.25  christos 
    284  1.25  christos 	/* Free up child linked list, sort array, path buffer. */
    285  1.25  christos 	if (sp->fts_child)
    286  1.25  christos 		fts_lfree(sp->fts_child);
    287  1.25  christos 	if (sp->fts_array)
    288  1.25  christos 		free(sp->fts_array);
    289  1.25  christos 	free(sp->fts_path);
    290  1.25  christos 
    291  1.25  christos 	/* Return to original directory, save errno if necessary. */
    292  1.25  christos 	if (!ISSET(FTS_NOCHDIR)) {
    293  1.29  christos 		if (fchdir(sp->fts_rfd) == -1)
    294  1.29  christos 			saved_errno = errno;
    295  1.25  christos 		(void)close(sp->fts_rfd);
    296  1.25  christos 	}
    297  1.25  christos 
    298  1.25  christos 	/* Free up the stream pointer. */
    299  1.25  christos 	free(sp);
    300  1.29  christos 	if (saved_errno) {
    301  1.29  christos 		errno = saved_errno;
    302  1.29  christos 		return -1;
    303  1.29  christos 	}
    304  1.25  christos 
    305  1.29  christos 	return 0;
    306  1.25  christos }
    307  1.25  christos 
    308  1.29  christos #if !defined(__FTS_COMPAT_TAILINGSLASH)
    309  1.29  christos 
    310  1.25  christos /*
    311  1.26  christos  * Special case of "/" at the end of the path so that slashes aren't
    312  1.26  christos  * appended which would cause paths to be written as "....//foo".
    313  1.25  christos  */
    314  1.25  christos #define	NAPPEND(p)							\
    315  1.26  christos 	(p->fts_path[p->fts_pathlen - 1] == '/'				\
    316  1.26  christos 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
    317  1.25  christos 
    318  1.29  christos #else /* !defined(__FTS_COMPAT_TAILINGSLASH) */
    319  1.29  christos 
    320  1.29  christos /*
    321  1.29  christos  * compatibility with the old behaviour.
    322  1.29  christos  *
    323  1.29  christos  * Special case a root of "/" so that slashes aren't appended which would
    324  1.29  christos  * cause paths to be written as "//foo".
    325  1.29  christos  */
    326  1.29  christos 
    327  1.29  christos #define	NAPPEND(p)							\
    328  1.29  christos 	(p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 &&	\
    329  1.29  christos 	    p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
    330  1.29  christos 
    331  1.29  christos #endif /* !defined(__FTS_COMPAT_TAILINGSLASH) */
    332  1.29  christos 
    333  1.25  christos FTSENT *
    334  1.28  christos fts_read(FTS *sp)
    335  1.25  christos {
    336  1.25  christos 	FTSENT *p, *tmp;
    337  1.25  christos 	int instr;
    338  1.25  christos 	char *t;
    339  1.25  christos 	int saved_errno;
    340  1.25  christos 
    341  1.25  christos 	_DIAGASSERT(sp != NULL);
    342  1.25  christos 
    343  1.25  christos 	/* If finished or unrecoverable error, return NULL. */
    344  1.25  christos 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
    345  1.25  christos 		return (NULL);
    346  1.25  christos 
    347  1.25  christos 	/* Set current node pointer. */
    348  1.25  christos 	p = sp->fts_cur;
    349  1.25  christos 
    350  1.25  christos 	/* Save and zero out user instructions. */
    351  1.25  christos 	instr = p->fts_instr;
    352  1.25  christos 	p->fts_instr = FTS_NOINSTR;
    353  1.25  christos 
    354  1.25  christos 	/* Any type of file may be re-visited; re-stat and re-turn. */
    355  1.25  christos 	if (instr == FTS_AGAIN) {
    356  1.25  christos 		p->fts_info = fts_stat(sp, p, 0);
    357  1.25  christos 		return (p);
    358  1.25  christos 	}
    359  1.25  christos 
    360  1.25  christos 	/*
    361  1.25  christos 	 * Following a symlink -- SLNONE test allows application to see
    362  1.25  christos 	 * SLNONE and recover.  If indirecting through a symlink, have
    363  1.25  christos 	 * keep a pointer to current location.  If unable to get that
    364  1.25  christos 	 * pointer, follow fails.
    365  1.25  christos 	 */
    366  1.25  christos 	if (instr == FTS_FOLLOW &&
    367  1.25  christos 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
    368  1.25  christos 		p->fts_info = fts_stat(sp, p, 1);
    369  1.25  christos 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
    370  1.41  christos 			if ((p->fts_symfd = open(".", O_RDONLY | O_CLOEXEC, 0))
    371  1.41  christos 			    == -1) {
    372  1.25  christos 				p->fts_errno = errno;
    373  1.25  christos 				p->fts_info = FTS_ERR;
    374  1.25  christos 			} else
    375  1.25  christos 				p->fts_flags |= FTS_SYMFOLLOW;
    376  1.25  christos 		}
    377  1.25  christos 		return (p);
    378  1.25  christos 	}
    379  1.25  christos 
    380  1.25  christos 	/* Directory in pre-order. */
    381  1.25  christos 	if (p->fts_info == FTS_D) {
    382  1.25  christos 		/* If skipped or crossed mount point, do post-order visit. */
    383  1.25  christos 		if (instr == FTS_SKIP ||
    384  1.25  christos 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
    385  1.25  christos 			if (p->fts_flags & FTS_SYMFOLLOW)
    386  1.25  christos 				(void)close(p->fts_symfd);
    387  1.25  christos 			if (sp->fts_child) {
    388  1.25  christos 				fts_lfree(sp->fts_child);
    389  1.25  christos 				sp->fts_child = NULL;
    390  1.25  christos 			}
    391  1.25  christos 			p->fts_info = FTS_DP;
    392  1.25  christos 			return (p);
    393  1.32     lukem 		}
    394  1.25  christos 
    395  1.25  christos 		/* Rebuild if only read the names and now traversing. */
    396  1.25  christos 		if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
    397  1.25  christos 			CLR(FTS_NAMEONLY);
    398  1.25  christos 			fts_lfree(sp->fts_child);
    399  1.25  christos 			sp->fts_child = NULL;
    400  1.25  christos 		}
    401  1.25  christos 
    402  1.25  christos 		/*
    403  1.25  christos 		 * Cd to the subdirectory.
    404  1.25  christos 		 *
    405  1.25  christos 		 * If have already read and now fail to chdir, whack the list
    406  1.25  christos 		 * to make the names come out right, and set the parent errno
    407  1.25  christos 		 * so the application will eventually get an error condition.
    408  1.25  christos 		 * Set the FTS_DONTCHDIR flag so that when we logically change
    409  1.25  christos 		 * directories back to the parent we don't do a chdir.
    410  1.25  christos 		 *
    411  1.25  christos 		 * If haven't read do so.  If the read fails, fts_build sets
    412  1.25  christos 		 * FTS_STOP or the fts_info field of the node.
    413  1.25  christos 		 */
    414  1.25  christos 		if (sp->fts_child) {
    415  1.25  christos 			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
    416  1.25  christos 				p->fts_errno = errno;
    417  1.25  christos 				p->fts_flags |= FTS_DONTCHDIR;
    418  1.25  christos 				for (p = sp->fts_child; p; p = p->fts_link)
    419  1.25  christos 					p->fts_accpath =
    420  1.25  christos 					    p->fts_parent->fts_accpath;
    421  1.25  christos 			}
    422  1.25  christos 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
    423  1.25  christos 			if (ISSET(FTS_STOP))
    424  1.25  christos 				return (NULL);
    425  1.25  christos 			return (p);
    426  1.25  christos 		}
    427  1.25  christos 		p = sp->fts_child;
    428  1.25  christos 		sp->fts_child = NULL;
    429  1.25  christos 		goto name;
    430  1.25  christos 	}
    431  1.25  christos 
    432  1.48      manu next:
    433  1.25  christos 	/* Move to the next node on this level. */
    434  1.48      manu 	tmp = p;
    435  1.48      manu 
    436  1.48      manu 	/*
    437  1.48      manu 	 * We are going to free sp->fts_cur, set it to NULL so
    438  1.48      manu 	 * that fts_close() does not attempt to free it again
    439  1.48      manu 	 * if we exit without setting it to a new value because
    440  1.48      manu 	 * FCHDIR() failed below.
    441  1.48      manu 	 */
    442  1.48      manu 	assert(tmp == sp->fts_cur);
    443  1.48      manu 	sp->fts_cur = NULL;
    444  1.48      manu 
    445  1.25  christos 	if ((p = p->fts_link) != NULL) {
    446  1.33     lukem 		fts_free(tmp);
    447  1.25  christos 
    448  1.25  christos 		/*
    449  1.25  christos 		 * If reached the top, return to the original directory, and
    450  1.25  christos 		 * load the paths for the next root.
    451  1.25  christos 		 */
    452  1.25  christos 		if (p->fts_level == FTS_ROOTLEVEL) {
    453  1.25  christos 			if (FCHDIR(sp, sp->fts_rfd)) {
    454  1.25  christos 				SET(FTS_STOP);
    455  1.25  christos 				return (NULL);
    456  1.25  christos 			}
    457  1.25  christos 			fts_load(sp, p);
    458  1.25  christos 			return (sp->fts_cur = p);
    459  1.25  christos 		}
    460  1.25  christos 
    461  1.25  christos 		/*
    462  1.25  christos 		 * User may have called fts_set on the node.  If skipped,
    463  1.25  christos 		 * ignore.  If followed, get a file descriptor so we can
    464  1.25  christos 		 * get back if necessary.
    465  1.25  christos 		 */
    466  1.25  christos 		if (p->fts_instr == FTS_SKIP)
    467  1.25  christos 			goto next;
    468  1.25  christos 		if (p->fts_instr == FTS_FOLLOW) {
    469  1.25  christos 			p->fts_info = fts_stat(sp, p, 1);
    470  1.25  christos 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
    471  1.25  christos 				if ((p->fts_symfd =
    472  1.41  christos 				    open(".", O_RDONLY | O_CLOEXEC, 0)) == -1) {
    473  1.25  christos 					p->fts_errno = errno;
    474  1.25  christos 					p->fts_info = FTS_ERR;
    475  1.25  christos 				} else
    476  1.25  christos 					p->fts_flags |= FTS_SYMFOLLOW;
    477  1.25  christos 			}
    478  1.25  christos 			p->fts_instr = FTS_NOINSTR;
    479  1.25  christos 		}
    480  1.25  christos 
    481  1.25  christos name:		t = sp->fts_path + NAPPEND(p->fts_parent);
    482  1.25  christos 		*t++ = '/';
    483  1.25  christos 		memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1));
    484  1.25  christos 		return (sp->fts_cur = p);
    485  1.25  christos 	}
    486  1.25  christos 
    487  1.25  christos 	/* Move up to the parent node. */
    488  1.25  christos 	p = tmp->fts_parent;
    489  1.33     lukem 	fts_free(tmp);
    490  1.25  christos 
    491  1.25  christos 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
    492  1.25  christos 		/*
    493  1.25  christos 		 * Done; free everything up and set errno to 0 so the user
    494  1.25  christos 		 * can distinguish between error and EOF.
    495  1.25  christos 		 */
    496  1.33     lukem 		fts_free(p);
    497  1.25  christos 		errno = 0;
    498  1.25  christos 		return (sp->fts_cur = NULL);
    499  1.25  christos 	}
    500  1.25  christos 
    501  1.46   msaitoh 	/* NUL terminate the pathname. */
    502  1.25  christos 	sp->fts_path[p->fts_pathlen] = '\0';
    503  1.25  christos 
    504  1.25  christos 	/*
    505  1.25  christos 	 * Return to the parent directory.  If at a root node or came through
    506  1.25  christos 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
    507  1.25  christos 	 * one directory.
    508  1.25  christos 	 */
    509  1.25  christos 	if (p->fts_level == FTS_ROOTLEVEL) {
    510  1.25  christos 		if (FCHDIR(sp, sp->fts_rfd)) {
    511  1.25  christos 			SET(FTS_STOP);
    512  1.25  christos 			return (NULL);
    513  1.25  christos 		}
    514  1.25  christos 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
    515  1.25  christos 		if (FCHDIR(sp, p->fts_symfd)) {
    516  1.25  christos 			saved_errno = errno;
    517  1.25  christos 			(void)close(p->fts_symfd);
    518  1.25  christos 			errno = saved_errno;
    519  1.25  christos 			SET(FTS_STOP);
    520  1.25  christos 			return (NULL);
    521  1.25  christos 		}
    522  1.25  christos 		(void)close(p->fts_symfd);
    523  1.25  christos 	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
    524  1.25  christos 	    fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
    525  1.25  christos 		SET(FTS_STOP);
    526  1.25  christos 		return (NULL);
    527  1.25  christos 	}
    528  1.25  christos 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
    529  1.25  christos 	return (sp->fts_cur = p);
    530  1.25  christos }
    531  1.25  christos 
    532  1.25  christos /*
    533  1.25  christos  * Fts_set takes the stream as an argument although it's not used in this
    534  1.25  christos  * implementation; it would be necessary if anyone wanted to add global
    535  1.25  christos  * semantics to fts using fts_set.  An error return is allowed for similar
    536  1.25  christos  * reasons.
    537  1.25  christos  */
    538  1.25  christos /* ARGSUSED */
    539  1.25  christos int
    540  1.28  christos fts_set(FTS *sp, FTSENT *p, int instr)
    541  1.25  christos {
    542  1.25  christos 
    543  1.25  christos 	_DIAGASSERT(sp != NULL);
    544  1.25  christos 	_DIAGASSERT(p != NULL);
    545  1.25  christos 
    546  1.25  christos 	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
    547  1.25  christos 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
    548  1.25  christos 		errno = EINVAL;
    549  1.25  christos 		return (1);
    550  1.25  christos 	}
    551  1.25  christos 	p->fts_instr = instr;
    552  1.25  christos 	return (0);
    553  1.25  christos }
    554  1.25  christos 
    555  1.25  christos FTSENT *
    556  1.28  christos fts_children(FTS *sp, int instr)
    557  1.25  christos {
    558  1.25  christos 	FTSENT *p;
    559  1.25  christos 	int fd;
    560  1.25  christos 
    561  1.25  christos 	_DIAGASSERT(sp != NULL);
    562  1.25  christos 
    563  1.25  christos 	if (instr && instr != FTS_NAMEONLY) {
    564  1.25  christos 		errno = EINVAL;
    565  1.25  christos 		return (NULL);
    566  1.25  christos 	}
    567  1.25  christos 
    568  1.25  christos 	/* Set current node pointer. */
    569  1.25  christos 	p = sp->fts_cur;
    570  1.25  christos 
    571  1.25  christos 	/*
    572  1.25  christos 	 * Errno set to 0 so user can distinguish empty directory from
    573  1.25  christos 	 * an error.
    574  1.25  christos 	 */
    575  1.25  christos 	errno = 0;
    576  1.25  christos 
    577  1.25  christos 	/* Fatal errors stop here. */
    578  1.25  christos 	if (ISSET(FTS_STOP))
    579  1.25  christos 		return (NULL);
    580  1.25  christos 
    581  1.25  christos 	/* Return logical hierarchy of user's arguments. */
    582  1.25  christos 	if (p->fts_info == FTS_INIT)
    583  1.25  christos 		return (p->fts_link);
    584  1.25  christos 
    585  1.25  christos 	/*
    586  1.25  christos 	 * If not a directory being visited in pre-order, stop here.  Could
    587  1.25  christos 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
    588  1.25  christos 	 * same effect is available with FTS_AGAIN.
    589  1.25  christos 	 */
    590  1.25  christos 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
    591  1.25  christos 		return (NULL);
    592  1.25  christos 
    593  1.25  christos 	/* Free up any previous child list. */
    594  1.25  christos 	if (sp->fts_child)
    595  1.25  christos 		fts_lfree(sp->fts_child);
    596  1.25  christos 
    597  1.25  christos 	if (instr == FTS_NAMEONLY) {
    598  1.25  christos 		SET(FTS_NAMEONLY);
    599  1.25  christos 		instr = BNAMES;
    600  1.32     lukem 	} else
    601  1.25  christos 		instr = BCHILD;
    602  1.25  christos 
    603  1.25  christos 	/*
    604  1.25  christos 	 * If using chdir on a relative path and called BEFORE fts_read does
    605  1.25  christos 	 * its chdir to the root of a traversal, we can lose -- we need to
    606  1.25  christos 	 * chdir into the subdirectory, and we don't know where the current
    607  1.25  christos 	 * directory is, so we can't get back so that the upcoming chdir by
    608  1.25  christos 	 * fts_read will work.
    609  1.25  christos 	 */
    610  1.25  christos 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
    611  1.25  christos 	    ISSET(FTS_NOCHDIR))
    612  1.25  christos 		return (sp->fts_child = fts_build(sp, instr));
    613  1.25  christos 
    614  1.47  christos 	if ((fd = open(".", O_RDONLY | O_CLOEXEC, 0)) == -1)
    615  1.25  christos 		return (sp->fts_child = NULL);
    616  1.25  christos 	sp->fts_child = fts_build(sp, instr);
    617  1.25  christos 	if (fchdir(fd)) {
    618  1.25  christos 		(void)close(fd);
    619  1.25  christos 		return (NULL);
    620  1.25  christos 	}
    621  1.25  christos 	(void)close(fd);
    622  1.25  christos 	return (sp->fts_child);
    623  1.25  christos }
    624  1.25  christos 
    625  1.25  christos /*
    626  1.25  christos  * This is the tricky part -- do not casually change *anything* in here.  The
    627  1.25  christos  * idea is to build the linked list of entries that are used by fts_children
    628  1.25  christos  * and fts_read.  There are lots of special cases.
    629  1.25  christos  *
    630  1.25  christos  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
    631  1.25  christos  * set and it's a physical walk (so that symbolic links can't be directories),
    632  1.25  christos  * we can do things quickly.  First, if it's a 4.4BSD file system, the type
    633  1.25  christos  * of the file is in the directory entry.  Otherwise, we assume that the number
    634  1.25  christos  * of subdirectories in a node is equal to the number of links to the parent.
    635  1.25  christos  * The former skips all stat calls.  The latter skips stat calls in any leaf
    636  1.25  christos  * directories and for any files after the subdirectories in the directory have
    637  1.25  christos  * been found, cutting the stat calls by about 2/3.
    638  1.25  christos  */
    639  1.25  christos static FTSENT *
    640  1.28  christos fts_build(FTS *sp, int type)
    641  1.25  christos {
    642  1.25  christos 	struct dirent *dp;
    643  1.25  christos 	FTSENT *p, *head;
    644  1.25  christos 	size_t nitems;
    645  1.25  christos 	FTSENT *cur, *tail;
    646  1.25  christos 	DIR *dirp;
    647  1.27  christos 	void *oldaddr;
    648  1.27  christos 	size_t dnamlen;
    649  1.37     lukem 	int cderrno, descend, level, nlinks, saved_errno, nostat, doadjust;
    650  1.37     lukem 	size_t len, maxlen;
    651  1.25  christos #ifdef FTS_WHITEOUT
    652  1.25  christos 	int oflag;
    653  1.25  christos #endif
    654  1.25  christos 	char *cp = NULL;	/* pacify gcc */
    655  1.25  christos 
    656  1.25  christos 	_DIAGASSERT(sp != NULL);
    657  1.25  christos 
    658  1.25  christos 	/* Set current node pointer. */
    659  1.25  christos 	cur = sp->fts_cur;
    660  1.25  christos 
    661  1.25  christos 	/*
    662  1.25  christos 	 * Open the directory for reading.  If this fails, we're done.
    663  1.25  christos 	 * If being called from fts_read, set the fts_info field.
    664  1.25  christos 	 */
    665  1.25  christos #ifdef FTS_WHITEOUT
    666  1.25  christos 	if (ISSET(FTS_WHITEOUT))
    667  1.25  christos 		oflag = DTF_NODUP|DTF_REWIND;
    668  1.25  christos 	else
    669  1.25  christos 		oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
    670  1.25  christos #else
    671  1.32     lukem #define	__opendir2(path, flag) opendir(path)
    672  1.25  christos #endif
    673  1.25  christos 	if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
    674  1.25  christos 		if (type == BREAD) {
    675  1.25  christos 			cur->fts_info = FTS_DNR;
    676  1.25  christos 			cur->fts_errno = errno;
    677  1.25  christos 		}
    678  1.25  christos 		return (NULL);
    679  1.25  christos 	}
    680  1.25  christos 
    681  1.25  christos 	/*
    682  1.25  christos 	 * Nlinks is the number of possible entries of type directory in the
    683  1.25  christos 	 * directory if we're cheating on stat calls, 0 if we're not doing
    684  1.25  christos 	 * any stat calls at all, -1 if we're doing stats on everything.
    685  1.25  christos 	 */
    686  1.25  christos 	if (type == BNAMES) {
    687  1.25  christos 		nlinks = 0;
    688  1.25  christos 		nostat = 1;
    689  1.25  christos 	} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
    690  1.25  christos 		nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
    691  1.25  christos 		nostat = 1;
    692  1.25  christos 	} else {
    693  1.25  christos 		nlinks = -1;
    694  1.25  christos 		nostat = 0;
    695  1.25  christos 	}
    696  1.25  christos 
    697  1.25  christos #ifdef notdef
    698  1.25  christos 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
    699  1.25  christos 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
    700  1.25  christos 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
    701  1.25  christos #endif
    702  1.25  christos 	/*
    703  1.25  christos 	 * If we're going to need to stat anything or we want to descend
    704  1.25  christos 	 * and stay in the directory, chdir.  If this fails we keep going,
    705  1.25  christos 	 * but set a flag so we don't chdir after the post-order visit.
    706  1.25  christos 	 * We won't be able to stat anything, but we can still return the
    707  1.25  christos 	 * names themselves.  Note, that since fts_read won't be able to
    708  1.25  christos 	 * chdir into the directory, it will have to return different path
    709  1.25  christos 	 * names than before, i.e. "a/b" instead of "b".  Since the node
    710  1.25  christos 	 * has already been visited in pre-order, have to wait until the
    711  1.25  christos 	 * post-order visit to return the error.  There is a special case
    712  1.25  christos 	 * here, if there was nothing to stat then it's not an error to
    713  1.25  christos 	 * not be able to stat.  This is all fairly nasty.  If a program
    714  1.25  christos 	 * needed sorted entries or stat information, they had better be
    715  1.25  christos 	 * checking FTS_NS on the returned nodes.
    716  1.25  christos 	 */
    717  1.25  christos 	cderrno = 0;
    718  1.25  christos 	if (nlinks || type == BREAD) {
    719  1.25  christos 		if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
    720  1.25  christos 			if (nlinks && type == BREAD)
    721  1.25  christos 				cur->fts_errno = errno;
    722  1.25  christos 			cur->fts_flags |= FTS_DONTCHDIR;
    723  1.25  christos 			descend = 0;
    724  1.25  christos 			cderrno = errno;
    725  1.25  christos 		} else
    726  1.25  christos 			descend = 1;
    727  1.25  christos 	} else
    728  1.25  christos 		descend = 0;
    729  1.25  christos 
    730  1.25  christos 	/*
    731  1.25  christos 	 * Figure out the max file name length that can be stored in the
    732  1.25  christos 	 * current path -- the inner loop allocates more path as necessary.
    733  1.25  christos 	 * We really wouldn't have to do the maxlen calculations here, we
    734  1.25  christos 	 * could do them in fts_read before returning the path, but it's a
    735  1.25  christos 	 * lot easier here since the length is part of the dirent structure.
    736  1.25  christos 	 *
    737  1.25  christos 	 * If not changing directories set a pointer so that can just append
    738  1.25  christos 	 * each new name into the path.
    739  1.25  christos 	 */
    740  1.25  christos 	len = NAPPEND(cur);
    741  1.25  christos 	if (ISSET(FTS_NOCHDIR)) {
    742  1.25  christos 		cp = sp->fts_path + len;
    743  1.25  christos 		*cp++ = '/';
    744  1.25  christos 	}
    745  1.25  christos 	len++;
    746  1.25  christos 	maxlen = sp->fts_pathlen - len;
    747  1.25  christos 
    748  1.39  christos #if defined(__FTS_COMPAT_LEVEL)
    749  1.38  pgoyette 	if (cur->fts_level == SHRT_MAX) {
    750  1.38  pgoyette 		(void)closedir(dirp);
    751  1.38  pgoyette 		cur->fts_info = FTS_ERR;
    752  1.38  pgoyette 		SET(FTS_STOP);
    753  1.38  pgoyette 		errno = ENAMETOOLONG;
    754  1.38  pgoyette 		return (NULL);
    755  1.38  pgoyette 	}
    756  1.39  christos #endif
    757  1.38  pgoyette 
    758  1.25  christos 	level = cur->fts_level + 1;
    759  1.25  christos 
    760  1.25  christos 	/* Read the directory, attaching each entry to the `link' pointer. */
    761  1.27  christos 	doadjust = 0;
    762  1.25  christos 	for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) {
    763  1.25  christos 
    764  1.25  christos 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
    765  1.25  christos 			continue;
    766  1.25  christos 
    767  1.32     lukem #if defined(HAVE_STRUCT_DIRENT_D_NAMLEN)
    768  1.27  christos 		dnamlen = dp->d_namlen;
    769  1.25  christos #else
    770  1.27  christos 		dnamlen = strlen(dp->d_name);
    771  1.25  christos #endif
    772  1.27  christos 		if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL)
    773  1.25  christos 			goto mem1;
    774  1.27  christos 		if (dnamlen >= maxlen) {	/* include space for NUL */
    775  1.27  christos 			oldaddr = sp->fts_path;
    776  1.27  christos 			if (fts_palloc(sp, dnamlen + len + 1)) {
    777  1.25  christos 				/*
    778  1.25  christos 				 * No more memory for path or structures.  Save
    779  1.25  christos 				 * errno, free up the current structure and the
    780  1.25  christos 				 * structures already allocated.
    781  1.25  christos 				 */
    782  1.25  christos mem1:				saved_errno = errno;
    783  1.25  christos 				if (p)
    784  1.33     lukem 					fts_free(p);
    785  1.25  christos 				fts_lfree(head);
    786  1.25  christos 				(void)closedir(dirp);
    787  1.25  christos 				errno = saved_errno;
    788  1.25  christos 				cur->fts_info = FTS_ERR;
    789  1.25  christos 				SET(FTS_STOP);
    790  1.25  christos 				return (NULL);
    791  1.25  christos 			}
    792  1.27  christos 			/* Did realloc() change the pointer? */
    793  1.27  christos 			if (oldaddr != sp->fts_path) {
    794  1.27  christos 				doadjust = 1;
    795  1.27  christos 				if (ISSET(FTS_NOCHDIR))
    796  1.27  christos 					cp = sp->fts_path + len;
    797  1.27  christos 			}
    798  1.25  christos 			maxlen = sp->fts_pathlen - len;
    799  1.25  christos 		}
    800  1.25  christos 
    801  1.31  christos #if defined(__FTS_COMPAT_LENGTH)
    802  1.27  christos 		if (len + dnamlen >= USHRT_MAX) {
    803  1.27  christos 			/*
    804  1.32     lukem 			 * In an FTSENT, fts_pathlen is an unsigned short
    805  1.32     lukem 			 * so it is possible to wraparound here.
    806  1.32     lukem 			 * If we do, free up the current structure and the
    807  1.32     lukem 			 * structures already allocated, then error out
    808  1.32     lukem 			 * with ENAMETOOLONG.
    809  1.27  christos 			 */
    810  1.33     lukem 			fts_free(p);
    811  1.27  christos 			fts_lfree(head);
    812  1.27  christos 			(void)closedir(dirp);
    813  1.27  christos 			cur->fts_info = FTS_ERR;
    814  1.27  christos 			SET(FTS_STOP);
    815  1.27  christos 			errno = ENAMETOOLONG;
    816  1.27  christos 			return (NULL);
    817  1.27  christos 		}
    818  1.31  christos #endif
    819  1.27  christos 		p->fts_level = level;
    820  1.43  christos 		p->fts_pathlen = ftsent_pathlen_truncate(len + dnamlen);
    821  1.25  christos 		p->fts_parent = sp->fts_cur;
    822  1.25  christos 
    823  1.25  christos #ifdef FTS_WHITEOUT
    824  1.25  christos 		if (dp->d_type == DT_WHT)
    825  1.25  christos 			p->fts_flags |= FTS_ISW;
    826  1.25  christos #endif
    827  1.25  christos 
    828  1.25  christos 		if (cderrno) {
    829  1.25  christos 			if (nlinks) {
    830  1.25  christos 				p->fts_info = FTS_NS;
    831  1.25  christos 				p->fts_errno = cderrno;
    832  1.25  christos 			} else
    833  1.25  christos 				p->fts_info = FTS_NSOK;
    834  1.25  christos 			p->fts_accpath = cur->fts_accpath;
    835  1.25  christos 		} else if (nlinks == 0
    836  1.25  christos #ifdef DT_DIR
    837  1.32     lukem 		    || (nostat &&
    838  1.25  christos 		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
    839  1.25  christos #endif
    840  1.25  christos 		    ) {
    841  1.25  christos 			p->fts_accpath =
    842  1.25  christos 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
    843  1.25  christos 			p->fts_info = FTS_NSOK;
    844  1.25  christos 		} else {
    845  1.25  christos 			/* Build a file name for fts_stat to stat. */
    846  1.25  christos 			if (ISSET(FTS_NOCHDIR)) {
    847  1.25  christos 				p->fts_accpath = p->fts_path;
    848  1.25  christos 				memmove(cp, p->fts_name,
    849  1.25  christos 				        (size_t)(p->fts_namelen + 1));
    850  1.25  christos 			} else
    851  1.25  christos 				p->fts_accpath = p->fts_name;
    852  1.25  christos 			/* Stat it. */
    853  1.25  christos 			p->fts_info = fts_stat(sp, p, 0);
    854  1.25  christos 
    855  1.25  christos 			/* Decrement link count if applicable. */
    856  1.25  christos 			if (nlinks > 0 && (p->fts_info == FTS_D ||
    857  1.25  christos 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
    858  1.25  christos 				--nlinks;
    859  1.25  christos 		}
    860  1.25  christos 
    861  1.25  christos 		/* We walk in directory order so "ls -f" doesn't get upset. */
    862  1.25  christos 		p->fts_link = NULL;
    863  1.25  christos 		if (head == NULL)
    864  1.25  christos 			head = tail = p;
    865  1.25  christos 		else {
    866  1.25  christos 			tail->fts_link = p;
    867  1.25  christos 			tail = p;
    868  1.25  christos 		}
    869  1.25  christos 		++nitems;
    870  1.25  christos 	}
    871  1.25  christos 	(void)closedir(dirp);
    872  1.25  christos 
    873  1.25  christos 	/*
    874  1.25  christos 	 * If had to realloc the path, adjust the addresses for the rest
    875  1.25  christos 	 * of the tree.
    876  1.25  christos 	 */
    877  1.27  christos 	if (doadjust)
    878  1.25  christos 		fts_padjust(sp, head);
    879  1.25  christos 
    880  1.25  christos 	/*
    881  1.25  christos 	 * If not changing directories, reset the path back to original
    882  1.25  christos 	 * state.
    883  1.25  christos 	 */
    884  1.25  christos 	if (ISSET(FTS_NOCHDIR)) {
    885  1.27  christos 		if (len == sp->fts_pathlen || nitems == 0)
    886  1.25  christos 			--cp;
    887  1.25  christos 		*cp = '\0';
    888  1.25  christos 	}
    889  1.25  christos 
    890  1.25  christos 	/*
    891  1.25  christos 	 * If descended after called from fts_children or after called from
    892  1.25  christos 	 * fts_read and nothing found, get back.  At the root level we use
    893  1.25  christos 	 * the saved fd; if one of fts_open()'s arguments is a relative path
    894  1.25  christos 	 * to an empty directory, we wind up here with no other way back.  If
    895  1.25  christos 	 * can't get back, we're done.
    896  1.25  christos 	 */
    897  1.25  christos 	if (descend && (type == BCHILD || !nitems) &&
    898  1.25  christos 	    (cur->fts_level == FTS_ROOTLEVEL ?
    899  1.25  christos 	    FCHDIR(sp, sp->fts_rfd) :
    900  1.25  christos 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
    901  1.25  christos 		cur->fts_info = FTS_ERR;
    902  1.25  christos 		SET(FTS_STOP);
    903  1.25  christos 		return (NULL);
    904  1.25  christos 	}
    905  1.25  christos 
    906  1.25  christos 	/* If didn't find anything, return NULL. */
    907  1.25  christos 	if (!nitems) {
    908  1.25  christos 		if (type == BREAD)
    909  1.25  christos 			cur->fts_info = FTS_DP;
    910  1.25  christos 		return (NULL);
    911  1.25  christos 	}
    912  1.25  christos 
    913  1.25  christos 	/* Sort the entries. */
    914  1.25  christos 	if (sp->fts_compar && nitems > 1)
    915  1.25  christos 		head = fts_sort(sp, head, nitems);
    916  1.25  christos 	return (head);
    917  1.25  christos }
    918  1.25  christos 
    919  1.32     lukem static unsigned short
    920  1.28  christos fts_stat(FTS *sp, FTSENT *p, int follow)
    921  1.25  christos {
    922  1.25  christos 	FTSENT *t;
    923  1.25  christos 	dev_t dev;
    924  1.25  christos 	__fts_ino_t ino;
    925  1.25  christos 	__fts_stat_t *sbp, sb;
    926  1.25  christos 	int saved_errno;
    927  1.25  christos 
    928  1.25  christos 	_DIAGASSERT(sp != NULL);
    929  1.25  christos 	_DIAGASSERT(p != NULL);
    930  1.25  christos 
    931  1.25  christos 	/* If user needs stat info, stat buffer already allocated. */
    932  1.25  christos 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
    933  1.25  christos 
    934  1.25  christos #ifdef FTS_WHITEOUT
    935  1.25  christos 	/* check for whiteout */
    936  1.25  christos 	if (p->fts_flags & FTS_ISW) {
    937  1.25  christos 		if (sbp != &sb) {
    938  1.25  christos 			memset(sbp, '\0', sizeof (*sbp));
    939  1.25  christos 			sbp->st_mode = S_IFWHT;
    940  1.25  christos 		}
    941  1.25  christos 		return (FTS_W);
    942  1.25  christos 	}
    943  1.25  christos #endif
    944  1.25  christos 
    945  1.25  christos 	/*
    946  1.25  christos 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
    947  1.25  christos 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
    948  1.25  christos 	 * fail, set the errno from the stat call.
    949  1.25  christos 	 */
    950  1.25  christos 	if (ISSET(FTS_LOGICAL) || follow) {
    951  1.25  christos 		if (stat(p->fts_accpath, sbp)) {
    952  1.25  christos 			saved_errno = errno;
    953  1.25  christos 			if (!lstat(p->fts_accpath, sbp)) {
    954  1.25  christos 				errno = 0;
    955  1.25  christos 				return (FTS_SLNONE);
    956  1.32     lukem 			}
    957  1.25  christos 			p->fts_errno = saved_errno;
    958  1.25  christos 			goto err;
    959  1.25  christos 		}
    960  1.25  christos 	} else if (lstat(p->fts_accpath, sbp)) {
    961  1.25  christos 		p->fts_errno = errno;
    962  1.25  christos err:		memset(sbp, 0, sizeof(*sbp));
    963  1.25  christos 		return (FTS_NS);
    964  1.25  christos 	}
    965  1.25  christos 
    966  1.25  christos 	if (S_ISDIR(sbp->st_mode)) {
    967  1.25  christos 		/*
    968  1.25  christos 		 * Set the device/inode.  Used to find cycles and check for
    969  1.25  christos 		 * crossing mount points.  Also remember the link count, used
    970  1.25  christos 		 * in fts_build to limit the number of stat calls.  It is
    971  1.25  christos 		 * understood that these fields are only referenced if fts_info
    972  1.25  christos 		 * is set to FTS_D.
    973  1.25  christos 		 */
    974  1.25  christos 		dev = p->fts_dev = sbp->st_dev;
    975  1.25  christos 		ino = p->fts_ino = sbp->st_ino;
    976  1.25  christos 		p->fts_nlink = sbp->st_nlink;
    977  1.25  christos 
    978  1.25  christos 		if (ISDOT(p->fts_name))
    979  1.25  christos 			return (FTS_DOT);
    980  1.25  christos 
    981  1.25  christos 		/*
    982  1.25  christos 		 * Cycle detection is done by brute force when the directory
    983  1.25  christos 		 * is first encountered.  If the tree gets deep enough or the
    984  1.25  christos 		 * number of symbolic links to directories is high enough,
    985  1.25  christos 		 * something faster might be worthwhile.
    986  1.25  christos 		 */
    987  1.25  christos 		for (t = p->fts_parent;
    988  1.25  christos 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
    989  1.25  christos 			if (ino == t->fts_ino && dev == t->fts_dev) {
    990  1.25  christos 				p->fts_cycle = t;
    991  1.25  christos 				return (FTS_DC);
    992  1.25  christos 			}
    993  1.25  christos 		return (FTS_D);
    994  1.25  christos 	}
    995  1.25  christos 	if (S_ISLNK(sbp->st_mode))
    996  1.25  christos 		return (FTS_SL);
    997  1.25  christos 	if (S_ISREG(sbp->st_mode))
    998  1.25  christos 		return (FTS_F);
    999  1.25  christos 	return (FTS_DEFAULT);
   1000  1.25  christos }
   1001  1.25  christos 
   1002  1.25  christos static FTSENT *
   1003  1.28  christos fts_sort(FTS *sp, FTSENT *head, size_t nitems)
   1004  1.25  christos {
   1005  1.25  christos 	FTSENT **ap, *p;
   1006  1.25  christos 
   1007  1.25  christos 	_DIAGASSERT(sp != NULL);
   1008  1.25  christos 	_DIAGASSERT(head != NULL);
   1009  1.25  christos 
   1010  1.25  christos 	/*
   1011  1.25  christos 	 * Construct an array of pointers to the structures and call qsort(3).
   1012  1.25  christos 	 * Reassemble the array in the order returned by qsort.  If unable to
   1013  1.25  christos 	 * sort for memory reasons, return the directory entries in their
   1014  1.25  christos 	 * current order.  Allocate enough space for the current needs plus
   1015  1.25  christos 	 * 40 so don't realloc one entry at a time.
   1016  1.25  christos 	 */
   1017  1.25  christos 	if (nitems > sp->fts_nitems) {
   1018  1.51  christos 		errno = reallocarr(&sp->fts_array,
   1019  1.51  christos 		    nitems + 40, sizeof(*sp->fts_array));
   1020  1.51  christos 		if (errno)
   1021  1.51  christos 			return head;
   1022  1.44  christos 		sp->fts_nitems = fts_nitems_truncate(nitems + 40);
   1023  1.25  christos 	}
   1024  1.25  christos 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
   1025  1.25  christos 		*ap++ = p;
   1026  1.32     lukem 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *),
   1027  1.28  christos 		(int (*)(const void *, const void *))sp->fts_compar);
   1028  1.25  christos 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
   1029  1.25  christos 		ap[0]->fts_link = ap[1];
   1030  1.25  christos 	ap[0]->fts_link = NULL;
   1031  1.25  christos 	return (head);
   1032  1.25  christos }
   1033  1.25  christos 
   1034  1.25  christos static FTSENT *
   1035  1.28  christos fts_alloc(FTS *sp, const char *name, size_t namelen)
   1036  1.25  christos {
   1037  1.25  christos 	FTSENT *p;
   1038  1.34     lukem #if defined(FTS_ALLOC_ALIGNED)
   1039  1.25  christos 	size_t len;
   1040  1.34     lukem #endif
   1041  1.25  christos 
   1042  1.25  christos 	_DIAGASSERT(sp != NULL);
   1043  1.25  christos 	_DIAGASSERT(name != NULL);
   1044  1.25  christos 
   1045  1.33     lukem #if defined(FTS_ALLOC_ALIGNED)
   1046  1.25  christos 	/*
   1047  1.25  christos 	 * The file name is a variable length array and no stat structure is
   1048  1.25  christos 	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
   1049  1.25  christos 	 * structure, the file name and the stat structure in one chunk, but
   1050  1.25  christos 	 * be careful that the stat structure is reasonably aligned.  Since the
   1051  1.25  christos 	 * fts_name field is declared to be of size 1, the fts_name pointer is
   1052  1.25  christos 	 * namelen + 2 before the first possible address of the stat structure.
   1053  1.25  christos 	 */
   1054  1.25  christos 	len = sizeof(FTSENT) + namelen;
   1055  1.25  christos 	if (!ISSET(FTS_NOSTAT))
   1056  1.25  christos 		len += sizeof(*(p->fts_statp)) + ALIGNBYTES;
   1057  1.25  christos 	if ((p = malloc(len)) == NULL)
   1058  1.25  christos 		return (NULL);
   1059  1.25  christos 
   1060  1.25  christos 	if (!ISSET(FTS_NOSTAT))
   1061  1.32     lukem 		p->fts_statp = (__fts_stat_t *)ALIGN(
   1062  1.32     lukem 		    (unsigned long)(p->fts_name + namelen + 2));
   1063  1.25  christos #else
   1064  1.25  christos 	if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
   1065  1.25  christos 		return (NULL);
   1066  1.25  christos 
   1067  1.25  christos 	if (!ISSET(FTS_NOSTAT))
   1068  1.25  christos 		if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) {
   1069  1.25  christos 			free(p);
   1070  1.25  christos 			return (NULL);
   1071  1.25  christos 		}
   1072  1.25  christos #endif
   1073  1.25  christos 
   1074  1.40  stacktic         if (ISSET(FTS_NOSTAT))
   1075  1.40  stacktic                 p->fts_statp = NULL;
   1076  1.40  stacktic 
   1077  1.25  christos 	/* Copy the name plus the trailing NULL. */
   1078  1.25  christos 	memmove(p->fts_name, name, namelen + 1);
   1079  1.25  christos 
   1080  1.44  christos 	p->fts_namelen = ftsent_namelen_truncate(namelen);
   1081  1.25  christos 	p->fts_path = sp->fts_path;
   1082  1.25  christos 	p->fts_errno = 0;
   1083  1.25  christos 	p->fts_flags = 0;
   1084  1.25  christos 	p->fts_instr = FTS_NOINSTR;
   1085  1.25  christos 	p->fts_number = 0;
   1086  1.25  christos 	p->fts_pointer = NULL;
   1087  1.25  christos 	return (p);
   1088  1.25  christos }
   1089  1.25  christos 
   1090  1.25  christos static void
   1091  1.33     lukem fts_free(FTSENT *p)
   1092  1.33     lukem {
   1093  1.33     lukem #if !defined(FTS_ALLOC_ALIGNED)
   1094  1.33     lukem 	if (p->fts_statp)
   1095  1.33     lukem 		free(p->fts_statp);
   1096  1.33     lukem #endif
   1097  1.33     lukem 	free(p);
   1098  1.33     lukem }
   1099  1.33     lukem 
   1100  1.33     lukem static void
   1101  1.28  christos fts_lfree(FTSENT *head)
   1102  1.25  christos {
   1103  1.25  christos 	FTSENT *p;
   1104  1.25  christos 
   1105  1.25  christos 	/* XXX: head may be NULL ? */
   1106  1.25  christos 
   1107  1.25  christos 	/* Free a linked list of structures. */
   1108  1.25  christos 	while ((p = head) != NULL) {
   1109  1.25  christos 		head = head->fts_link;
   1110  1.33     lukem 		fts_free(p);
   1111  1.25  christos 	}
   1112  1.25  christos }
   1113  1.25  christos 
   1114  1.25  christos static size_t
   1115  1.28  christos fts_pow2(size_t x)
   1116  1.25  christos {
   1117  1.25  christos 
   1118  1.25  christos 	x--;
   1119  1.25  christos 	x |= x>>1;
   1120  1.25  christos 	x |= x>>2;
   1121  1.25  christos 	x |= x>>4;
   1122  1.25  christos 	x |= x>>8;
   1123  1.25  christos 	x |= x>>16;
   1124  1.25  christos #if LONG_BIT > 32
   1125  1.25  christos 	x |= x>>32;
   1126  1.25  christos #endif
   1127  1.25  christos #if LONG_BIT > 64
   1128  1.25  christos 	x |= x>>64;
   1129  1.25  christos #endif
   1130  1.25  christos 	x++;
   1131  1.25  christos 	return (x);
   1132  1.25  christos }
   1133  1.25  christos 
   1134  1.25  christos /*
   1135  1.25  christos  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
   1136  1.25  christos  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
   1137  1.25  christos  * though the kernel won't resolve them.  Round up the new size to a power of 2,
   1138  1.32     lukem  * so we don't realloc the path 2 bytes at a time.
   1139  1.25  christos  */
   1140  1.25  christos static int
   1141  1.28  christos fts_palloc(FTS *sp, size_t size)
   1142  1.25  christos {
   1143  1.25  christos 	char *new;
   1144  1.25  christos 
   1145  1.25  christos 	_DIAGASSERT(sp != NULL);
   1146  1.25  christos 
   1147  1.31  christos #ifdef __FTS_COMPAT_LENGTH
   1148  1.25  christos 	/* Protect against fts_pathlen overflow. */
   1149  1.25  christos 	if (size > USHRT_MAX + 1) {
   1150  1.27  christos 		errno = ENAMETOOLONG;
   1151  1.25  christos 		return (1);
   1152  1.25  christos 	}
   1153  1.25  christos #endif
   1154  1.25  christos 	size = fts_pow2(size);
   1155  1.25  christos 	new = realloc(sp->fts_path, size);
   1156  1.25  christos 	if (new == 0)
   1157  1.25  christos 		return (1);
   1158  1.25  christos 	sp->fts_path = new;
   1159  1.43  christos 	sp->fts_pathlen = fts_pathlen_truncate(size);
   1160  1.25  christos 	return (0);
   1161  1.25  christos }
   1162  1.25  christos 
   1163  1.25  christos /*
   1164  1.25  christos  * When the path is realloc'd, have to fix all of the pointers in structures
   1165  1.25  christos  * already returned.
   1166  1.25  christos  */
   1167  1.25  christos static void
   1168  1.28  christos fts_padjust(FTS *sp, FTSENT *head)
   1169  1.25  christos {
   1170  1.25  christos 	FTSENT *p;
   1171  1.25  christos 	char *addr;
   1172  1.25  christos 
   1173  1.25  christos 	_DIAGASSERT(sp != NULL);
   1174  1.25  christos 
   1175  1.25  christos #define	ADJUST(p) do {							\
   1176  1.25  christos 	if ((p)->fts_accpath != (p)->fts_name)				\
   1177  1.25  christos 		(p)->fts_accpath =					\
   1178  1.25  christos 		    addr + ((p)->fts_accpath - (p)->fts_path);		\
   1179  1.25  christos 	(p)->fts_path = addr;						\
   1180  1.52    rillig } while (0)
   1181  1.25  christos 
   1182  1.25  christos 	addr = sp->fts_path;
   1183  1.25  christos 
   1184  1.25  christos 	/* Adjust the current set of children. */
   1185  1.25  christos 	for (p = sp->fts_child; p; p = p->fts_link)
   1186  1.25  christos 		ADJUST(p);
   1187  1.25  christos 
   1188  1.25  christos 	/* Adjust the rest of the tree, including the current level. */
   1189  1.25  christos 	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
   1190  1.25  christos 		ADJUST(p);
   1191  1.25  christos 		p = p->fts_link ? p->fts_link : p->fts_parent;
   1192  1.25  christos 	}
   1193  1.25  christos }
   1194  1.25  christos 
   1195  1.25  christos static size_t
   1196  1.28  christos fts_maxarglen(char * const *argv)
   1197  1.25  christos {
   1198  1.25  christos 	size_t len, max;
   1199  1.25  christos 
   1200  1.25  christos 	_DIAGASSERT(argv != NULL);
   1201  1.25  christos 
   1202  1.25  christos 	for (max = 0; *argv; ++argv)
   1203  1.25  christos 		if ((len = strlen(*argv)) > max)
   1204  1.25  christos 			max = len;
   1205  1.25  christos 	return (max + 1);
   1206  1.25  christos }
   1207  1.25  christos 
   1208  1.25  christos /*
   1209  1.25  christos  * Change to dir specified by fd or p->fts_accpath without getting
   1210  1.25  christos  * tricked by someone changing the world out from underneath us.
   1211  1.25  christos  * Assumes p->fts_dev and p->fts_ino are filled in.
   1212  1.25  christos  */
   1213  1.25  christos static int
   1214  1.28  christos fts_safe_changedir(const FTS *sp, const FTSENT *p, int fd, const char *path)
   1215  1.25  christos {
   1216  1.25  christos 	int oldfd = fd, ret = -1;
   1217  1.25  christos 	__fts_stat_t sb;
   1218  1.25  christos 
   1219  1.25  christos 	if (ISSET(FTS_NOCHDIR))
   1220  1.25  christos 		return 0;
   1221  1.25  christos 
   1222  1.47  christos 	if (oldfd < 0 && (fd = open(path, O_RDONLY | O_CLOEXEC)) == -1)
   1223  1.25  christos 		return -1;
   1224  1.25  christos 
   1225  1.25  christos 	if (fstat(fd, &sb) == -1)
   1226  1.25  christos 		goto bail;
   1227  1.24  christos 
   1228  1.25  christos 	if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
   1229  1.25  christos 		errno = ENOENT;
   1230  1.25  christos 		goto bail;
   1231  1.25  christos 	}
   1232  1.21      fvdl 
   1233  1.25  christos 	ret = fchdir(fd);
   1234  1.24  christos 
   1235  1.25  christos bail:
   1236  1.25  christos 	if (oldfd < 0) {
   1237  1.25  christos 		int save_errno = errno;
   1238  1.25  christos 		(void)close(fd);
   1239  1.25  christos 		errno = save_errno;
   1240  1.25  christos 	}
   1241  1.25  christos 	return ret;
   1242  1.25  christos }
   1243