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