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