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