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