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