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