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