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