xref: /freebsd/lib/libc/gen/fts.c (revision 92f340d137ba5d6db7610ba1dae35842e2c9c8ea)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $
32  */
33 
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 
39 #include <dirent.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <fts.h>
43 #include <stdalign.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include "un-namespace.h"
48 
49 #include "gen-private.h"
50 
51 #ifdef __BLOCKS__
52 #include <Block.h>
53 #else
54 #include "block_abi.h"
55 typedef DECLARE_BLOCK(int, fts_block,
56     const FTSENT * const *, const FTSENT * const *);
57 void qsort_b(void *, size_t, size_t, fts_block);
58 #endif /* __BLOCKS__ */
59 /* only present if linked with blocks runtime */
60 void *_Block_copy(const void *) __weak_symbol;
61 void _Block_release(const void *) __weak_symbol;
62 extern void *_NSConcreteGlobalBlock[] __weak_symbol;
63 
64 static FTSENT	*fts_alloc(FTS *, char *, size_t);
65 static FTSENT	*fts_build(FTS *, int);
66 static void	 fts_lfree(FTSENT *);
67 static void	 fts_load(FTS *, FTSENT *);
68 static size_t	 fts_maxarglen(char * const *);
69 static void	 fts_padjust(FTS *, FTSENT *);
70 static int	 fts_palloc(FTS *, size_t);
71 static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
72 static int	 fts_stat(FTS *, FTSENT *, int, int);
73 static int	 fts_safe_changedir(FTS *, FTSENT *, int, char *);
74 static int	 fts_ufslinks(FTS *, const FTSENT *);
75 
76 #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
77 
78 #define	CLR(opt)	(sp->fts_options &= ~(opt))
79 #define	ISSET(opt)	(sp->fts_options & (opt))
80 #define	SET(opt)	(sp->fts_options |= (opt))
81 
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 /*
90  * Internal representation of an FTS, including extra implementation
91  * details.  The FTS returned from fts_open points to this structure's
92  * ftsp_fts member (and can be cast to an _fts_private as required)
93  */
94 struct _fts_private {
95 	FTS		ftsp_fts;
96 	struct statfs	ftsp_statfs;
97 	dev_t		ftsp_dev;
98 	int		ftsp_linksreliable;
99 };
100 
101 /*
102  * The "FTS_NOSTAT" option can avoid a lot of calls to stat(2) if it
103  * knows that a directory could not possibly have subdirectories.  This
104  * is decided by looking at the link count: a subdirectory would
105  * increment its parent's link count by virtue of its own ".." entry.
106  * This assumption only holds for UFS-like filesystems that implement
107  * links and directories this way, so we must punt for others.
108  */
109 
110 static const char *ufslike_filesystems[] = {
111 	"ufs",
112 	"zfs",
113 	"nfs",
114 	"ext2fs",
115 	0
116 };
117 
118 static FTS *
119 __fts_open(FTS *sp, char * const *argv)
120 {
121 	FTSENT *p, *root;
122 	FTSENT *parent, *tmp;
123 	size_t len, nitems;
124 
125 	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
126 	if (ISSET(FTS_LOGICAL))
127 		SET(FTS_NOCHDIR);
128 
129 	/*
130 	 * Start out with 1K of path space, and enough, in any case,
131 	 * to hold the user's paths.
132 	 */
133 	if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
134 		goto mem1;
135 
136 	/* Allocate/initialize root's parent. */
137 	if ((parent = fts_alloc(sp, "", 0)) == NULL)
138 		goto mem2;
139 	parent->fts_level = FTS_ROOTPARENTLEVEL;
140 
141 	/* Shush, GCC. */
142 	tmp = NULL;
143 
144 	/* Allocate/initialize root(s). */
145 	for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
146 		len = strlen(*argv);
147 
148 		p = fts_alloc(sp, *argv, len);
149 		p->fts_level = FTS_ROOTLEVEL;
150 		p->fts_parent = parent;
151 		p->fts_accpath = p->fts_name;
152 		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW), -1);
153 
154 		/* Command-line "." and ".." are real directories. */
155 		if (p->fts_info == FTS_DOT)
156 			p->fts_info = FTS_D;
157 
158 		/*
159 		 * If comparison routine supplied, traverse in sorted
160 		 * order; otherwise traverse in the order specified.
161 		 */
162 		if (sp->fts_compar) {
163 			p->fts_link = root;
164 			root = p;
165 		} else {
166 			p->fts_link = NULL;
167 			if (root == NULL)
168 				tmp = root = p;
169 			else {
170 				tmp->fts_link = p;
171 				tmp = p;
172 			}
173 		}
174 	}
175 	if (sp->fts_compar && nitems > 1)
176 		root = fts_sort(sp, root, nitems);
177 
178 	/*
179 	 * Allocate a dummy pointer and make fts_read think that we've just
180 	 * finished the node before the root(s); set p->fts_info to FTS_INIT
181 	 * so that everything about the "current" node is ignored.
182 	 */
183 	if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
184 		goto mem3;
185 	sp->fts_cur->fts_link = root;
186 	sp->fts_cur->fts_info = FTS_INIT;
187 
188 	/*
189 	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
190 	 * that we can get back here; this could be avoided for some paths,
191 	 * but almost certainly not worth the effort.  Slashes, symbolic links,
192 	 * and ".." are all fairly nasty problems.  Note, if we can't get the
193 	 * descriptor we run anyway, just more slowly.
194 	 */
195 	if (!ISSET(FTS_NOCHDIR) &&
196 	    (sp->fts_rfd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
197 		SET(FTS_NOCHDIR);
198 
199 	return (sp);
200 
201 mem3:	fts_lfree(root);
202 	free(parent);
203 mem2:	free(sp->fts_path);
204 mem1:	free(sp);
205 	return (NULL);
206 }
207 
208 FTS *
209 fts_open(char * const *argv, int options,
210     int (*compar)(const FTSENT * const *, const FTSENT * const *))
211 {
212 	struct _fts_private *priv;
213 	FTS *sp;
214 
215 	/* Options check. */
216 	if (options & ~FTS_OPTIONMASK) {
217 		errno = EINVAL;
218 		return (NULL);
219 	}
220 
221 	/* fts_open() requires at least one path */
222 	if (*argv == NULL) {
223 		errno = EINVAL;
224 		return (NULL);
225 	}
226 
227 	/* Allocate/initialize the stream. */
228 	if ((priv = calloc(1, sizeof(*priv))) == NULL)
229 		return (NULL);
230 	sp = &priv->ftsp_fts;
231 	sp->fts_compar = compar;
232 	sp->fts_options = options;
233 
234 	return (__fts_open(sp, argv));
235 }
236 
237 #ifdef __BLOCKS__
238 FTS *
239 fts_open_b(char * const *argv, int options,
240     int (^compar)(const FTSENT * const *, const FTSENT * const *))
241 #else
242 FTS *
243 fts_open_b(char * const *argv, int options, fts_block compar)
244 #endif /* __BLOCKS__ */
245 {
246 	struct _fts_private *priv;
247 	FTS *sp;
248 
249 	/* No blocks, no problems. */
250 	if (compar == NULL)
251 		return (fts_open(argv, options, NULL));
252 
253 	/* Avoid segfault if blocks runtime is missing. */
254 	if (_Block_copy == NULL) {
255 		errno = ENOSYS;
256 		return (NULL);
257 	}
258 
259 	/* Options check. */
260 	if (options & ~FTS_OPTIONMASK) {
261 		errno = EINVAL;
262 		return (NULL);
263 	}
264 
265 	/* fts_open() requires at least one path */
266 	if (*argv == NULL) {
267 		errno = EINVAL;
268 		return (NULL);
269 	}
270 
271 	/* Allocate/initialize the stream. */
272 	if ((priv = calloc(1, sizeof(*priv))) == NULL)
273 		return (NULL);
274 	sp = &priv->ftsp_fts;
275 #ifdef __BLOCKS__
276 	compar = Block_copy(compar);
277 #else
278 	if (compar->isa != &_NSConcreteGlobalBlock)
279 		compar = _Block_copy(compar);
280 #endif /* __BLOCKS__ */
281 	if (compar == NULL) {
282 		free(priv);
283 		return (NULL);
284 	}
285 	sp->fts_compar_b = compar;
286 	sp->fts_options = options | FTS_COMPAR_B;
287 
288 	if ((sp = __fts_open(sp, argv)) == NULL) {
289 #ifdef __BLOCKS__
290 		Block_release(compar);
291 #else
292 		if (compar->isa != &_NSConcreteGlobalBlock)
293 			_Block_release(compar);
294 #endif /* __BLOCKS__ */
295 	}
296 	return (sp);
297 }
298 
299 static void
300 fts_load(FTS *sp, FTSENT *p)
301 {
302 	size_t len;
303 	char *cp;
304 
305 	/*
306 	 * Load the stream structure for the next traversal.  Since we don't
307 	 * actually enter the directory until after the preorder visit, set
308 	 * the fts_accpath field specially so the chdir gets done to the right
309 	 * place and the user can access the first node.  From fts_open it's
310 	 * known that the path will fit.
311 	 */
312 	len = p->fts_pathlen = p->fts_namelen;
313 	memmove(sp->fts_path, p->fts_name, len + 1);
314 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
315 		len = strlen(++cp);
316 		memmove(p->fts_name, cp, len + 1);
317 		p->fts_namelen = len;
318 	}
319 	p->fts_accpath = p->fts_path = sp->fts_path;
320 	sp->fts_dev = p->fts_dev;
321 }
322 
323 int
324 fts_close(FTS *sp)
325 {
326 	FTSENT *freep, *p;
327 	int saved_errno;
328 
329 	/*
330 	 * This still works if we haven't read anything -- the dummy structure
331 	 * points to the root list, so we step through to the end of the root
332 	 * list which has a valid parent pointer.
333 	 */
334 	if (sp->fts_cur) {
335 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
336 			freep = p;
337 			p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
338 			free(freep);
339 		}
340 		free(p);
341 	}
342 
343 	/* Free up child linked list, sort array, path buffer. */
344 	if (sp->fts_child)
345 		fts_lfree(sp->fts_child);
346 	if (sp->fts_array)
347 		free(sp->fts_array);
348 	free(sp->fts_path);
349 
350 	/* Free up any block pointer. */
351 	if (ISSET(FTS_COMPAR_B) && sp->fts_compar_b != NULL) {
352 #ifdef __BLOCKS__
353 		Block_release(sp->fts_compar_b);
354 #else
355 		if (sp->fts_compar_b->isa != &_NSConcreteGlobalBlock)
356 			_Block_release(sp->fts_compar_b);
357 #endif /* __BLOCKS__ */
358 	}
359 
360 	/* Return to original directory, save errno if necessary. */
361 	if (!ISSET(FTS_NOCHDIR)) {
362 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
363 		(void)_close(sp->fts_rfd);
364 
365 		/* Set errno and return. */
366 		if (saved_errno != 0) {
367 			/* Free up the stream pointer. */
368 			free(sp);
369 			errno = saved_errno;
370 			return (-1);
371 		}
372 	}
373 
374 	/* Free up the stream pointer. */
375 	free(sp);
376 	return (0);
377 }
378 
379 /*
380  * Special case of "/" at the end of the path so that slashes aren't
381  * appended which would cause paths to be written as "....//foo".
382  */
383 #define	NAPPEND(p)							\
384 	(p->fts_path[p->fts_pathlen - 1] == '/'				\
385 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
386 
387 FTSENT *
388 fts_read(FTS *sp)
389 {
390 	FTSENT *p, *tmp;
391 	int instr;
392 	char *t;
393 	int saved_errno;
394 
395 	/* If finished or unrecoverable error, return NULL. */
396 	if (sp->fts_cur == NULL || ISSET(FTS_STOP))
397 		return (NULL);
398 
399 	/* Set current node pointer. */
400 	p = sp->fts_cur;
401 
402 	/* Save and zero out user instructions. */
403 	instr = p->fts_instr;
404 	p->fts_instr = FTS_NOINSTR;
405 
406 	/* Any type of file may be re-visited; re-stat and re-turn. */
407 	if (instr == FTS_AGAIN) {
408 		p->fts_info = fts_stat(sp, p, 0, -1);
409 		return (p);
410 	}
411 
412 	/*
413 	 * Following a symlink -- SLNONE test allows application to see
414 	 * SLNONE and recover.  If indirecting through a symlink, have
415 	 * keep a pointer to current location.  If unable to get that
416 	 * pointer, follow fails.
417 	 */
418 	if (instr == FTS_FOLLOW &&
419 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
420 		p->fts_info = fts_stat(sp, p, 1, -1);
421 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
422 			if ((p->fts_symfd = _open(".", O_RDONLY | O_CLOEXEC,
423 			    0)) < 0) {
424 				p->fts_errno = errno;
425 				p->fts_info = FTS_ERR;
426 			} else
427 				p->fts_flags |= FTS_SYMFOLLOW;
428 		}
429 		return (p);
430 	}
431 
432 	/* Directory in pre-order. */
433 	if (p->fts_info == FTS_D) {
434 		/* If skipped or crossed mount point, do post-order visit. */
435 		if (instr == FTS_SKIP ||
436 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
437 			if (p->fts_flags & FTS_SYMFOLLOW)
438 				(void)_close(p->fts_symfd);
439 			if (sp->fts_child) {
440 				fts_lfree(sp->fts_child);
441 				sp->fts_child = NULL;
442 			}
443 			p->fts_info = FTS_DP;
444 			return (p);
445 		}
446 
447 		/* Rebuild if only read the names and now traversing. */
448 		if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
449 			CLR(FTS_NAMEONLY);
450 			fts_lfree(sp->fts_child);
451 			sp->fts_child = NULL;
452 		}
453 
454 		/*
455 		 * Cd to the subdirectory.
456 		 *
457 		 * If have already read and now fail to chdir, whack the list
458 		 * to make the names come out right, and set the parent errno
459 		 * so the application will eventually get an error condition.
460 		 * Set the FTS_DONTCHDIR flag so that when we logically change
461 		 * directories back to the parent we don't do a chdir.
462 		 *
463 		 * If haven't read do so.  If the read fails, fts_build sets
464 		 * FTS_STOP or the fts_info field of the node.
465 		 */
466 		if (sp->fts_child != NULL) {
467 			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
468 				p->fts_errno = errno;
469 				p->fts_flags |= FTS_DONTCHDIR;
470 				for (p = sp->fts_child; p != NULL;
471 				    p = p->fts_link)
472 					p->fts_accpath =
473 					    p->fts_parent->fts_accpath;
474 			}
475 		} else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
476 			if (ISSET(FTS_STOP))
477 				return (NULL);
478 			return (p);
479 		}
480 		p = sp->fts_child;
481 		sp->fts_child = NULL;
482 		goto name;
483 	}
484 
485 	/* Move to the next node on this level. */
486 next:	tmp = p;
487 	if ((p = p->fts_link) != NULL) {
488 		/*
489 		 * If reached the top, return to the original directory (or
490 		 * the root of the tree), and load the paths for the next root.
491 		 */
492 		if (p->fts_level == FTS_ROOTLEVEL) {
493 			if (FCHDIR(sp, sp->fts_rfd)) {
494 				SET(FTS_STOP);
495 				return (NULL);
496 			}
497 			free(tmp);
498 			fts_load(sp, p);
499 			return (sp->fts_cur = p);
500 		}
501 
502 		/*
503 		 * User may have called fts_set on the node.  If skipped,
504 		 * ignore.  If followed, get a file descriptor so we can
505 		 * get back if necessary.
506 		 */
507 		if (p->fts_instr == FTS_SKIP) {
508 			free(tmp);
509 			goto next;
510 		}
511 		if (p->fts_instr == FTS_FOLLOW) {
512 			p->fts_info = fts_stat(sp, p, 1, -1);
513 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
514 				if ((p->fts_symfd =
515 				    _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) {
516 					p->fts_errno = errno;
517 					p->fts_info = FTS_ERR;
518 				} else
519 					p->fts_flags |= FTS_SYMFOLLOW;
520 			}
521 			p->fts_instr = FTS_NOINSTR;
522 		}
523 
524 		free(tmp);
525 
526 name:		t = sp->fts_path + NAPPEND(p->fts_parent);
527 		*t++ = '/';
528 		memmove(t, p->fts_name, p->fts_namelen + 1);
529 		return (sp->fts_cur = p);
530 	}
531 
532 	/* Move up to the parent node. */
533 	p = tmp->fts_parent;
534 
535 	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
536 		/*
537 		 * Done; free everything up and set errno to 0 so the user
538 		 * can distinguish between error and EOF.
539 		 */
540 		free(tmp);
541 		free(p);
542 		errno = 0;
543 		return (sp->fts_cur = NULL);
544 	}
545 
546 	/* NUL terminate the pathname. */
547 	sp->fts_path[p->fts_pathlen] = '\0';
548 
549 	/*
550 	 * Return to the parent directory.  If at a root node or came through
551 	 * a symlink, go back through the file descriptor.  Otherwise, cd up
552 	 * one directory.
553 	 */
554 	if (p->fts_level == FTS_ROOTLEVEL) {
555 		if (FCHDIR(sp, sp->fts_rfd)) {
556 			SET(FTS_STOP);
557 			return (NULL);
558 		}
559 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
560 		if (FCHDIR(sp, p->fts_symfd)) {
561 			saved_errno = errno;
562 			(void)_close(p->fts_symfd);
563 			errno = saved_errno;
564 			SET(FTS_STOP);
565 			return (NULL);
566 		}
567 		(void)_close(p->fts_symfd);
568 	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
569 	    fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
570 		SET(FTS_STOP);
571 		return (NULL);
572 	}
573 	free(tmp);
574 	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
575 	return (sp->fts_cur = p);
576 }
577 
578 /*
579  * Fts_set takes the stream as an argument although it's not used in this
580  * implementation; it would be necessary if anyone wanted to add global
581  * semantics to fts using fts_set.  An error return is allowed for similar
582  * reasons.
583  */
584 /* ARGSUSED */
585 int
586 fts_set(FTS *sp, FTSENT *p, int instr)
587 {
588 	if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
589 	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
590 		errno = EINVAL;
591 		return (1);
592 	}
593 	p->fts_instr = instr;
594 	return (0);
595 }
596 
597 FTSENT *
598 fts_children(FTS *sp, int instr)
599 {
600 	FTSENT *p;
601 	int fd, rc, serrno;
602 
603 	if (instr != 0 && instr != FTS_NAMEONLY) {
604 		errno = EINVAL;
605 		return (NULL);
606 	}
607 
608 	/* Set current node pointer. */
609 	p = sp->fts_cur;
610 
611 	/*
612 	 * Errno set to 0 so user can distinguish empty directory from
613 	 * an error.
614 	 */
615 	errno = 0;
616 
617 	/* Fatal errors stop here. */
618 	if (ISSET(FTS_STOP))
619 		return (NULL);
620 
621 	/* Return logical hierarchy of user's arguments. */
622 	if (p->fts_info == FTS_INIT)
623 		return (p->fts_link);
624 
625 	/*
626 	 * If not a directory being visited in pre-order, stop here.  Could
627 	 * allow FTS_DNR, assuming the user has fixed the problem, but the
628 	 * same effect is available with FTS_AGAIN.
629 	 */
630 	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
631 		return (NULL);
632 
633 	/* Free up any previous child list. */
634 	if (sp->fts_child != NULL)
635 		fts_lfree(sp->fts_child);
636 
637 	if (instr == FTS_NAMEONLY) {
638 		SET(FTS_NAMEONLY);
639 		instr = BNAMES;
640 	} else
641 		instr = BCHILD;
642 
643 	/*
644 	 * If using chdir on a relative path and called BEFORE fts_read does
645 	 * its chdir to the root of a traversal, we can lose -- we need to
646 	 * chdir into the subdirectory, and we don't know where the current
647 	 * directory is, so we can't get back so that the upcoming chdir by
648 	 * fts_read will work.
649 	 */
650 	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
651 	    ISSET(FTS_NOCHDIR))
652 		return (sp->fts_child = fts_build(sp, instr));
653 
654 	if ((fd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
655 		return (NULL);
656 	sp->fts_child = fts_build(sp, instr);
657 	serrno = (sp->fts_child == NULL) ? errno : 0;
658 	rc = fchdir(fd);
659 	if (rc < 0 && serrno == 0)
660 		serrno = errno;
661 	(void)_close(fd);
662 	errno = serrno;
663 	if (rc < 0)
664 		return (NULL);
665 	return (sp->fts_child);
666 }
667 
668 #ifndef fts_get_clientptr
669 #error "fts_get_clientptr not defined"
670 #endif
671 
672 void *
673 (fts_get_clientptr)(FTS *sp)
674 {
675 
676 	return (fts_get_clientptr(sp));
677 }
678 
679 #ifndef fts_get_stream
680 #error "fts_get_stream not defined"
681 #endif
682 
683 FTS *
684 (fts_get_stream)(FTSENT *p)
685 {
686 	return (fts_get_stream(p));
687 }
688 
689 void
690 fts_set_clientptr(FTS *sp, void *clientptr)
691 {
692 
693 	sp->fts_clientptr = clientptr;
694 }
695 
696 static struct dirent *
697 fts_safe_readdir(DIR *dirp, int *readdir_errno)
698 {
699 	struct dirent *ret;
700 
701 	errno = 0;
702 	if (!dirp)
703 		return (NULL);
704 	ret = readdir(dirp);
705 	*readdir_errno = errno;
706 	return (ret);
707 }
708 
709 /*
710  * This is the tricky part -- do not casually change *anything* in here.  The
711  * idea is to build the linked list of entries that are used by fts_children
712  * and fts_read.  There are lots of special cases.
713  *
714  * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
715  * set and it's a physical walk (so that symbolic links can't be directories),
716  * we can do things quickly.  First, if it's a 4.4BSD file system, the type
717  * of the file is in the directory entry.  Otherwise, we assume that the number
718  * of subdirectories in a node is equal to the number of links to the parent.
719  * The former skips all stat calls.  The latter skips stat calls in any leaf
720  * directories and for any files after the subdirectories in the directory have
721  * been found, cutting the stat calls by about 2/3.
722  */
723 static FTSENT *
724 fts_build(FTS *sp, int type)
725 {
726 	struct dirent *dp;
727 	FTSENT *p, *head;
728 	FTSENT *cur, *tail;
729 	DIR *dirp;
730 	void *oldaddr;
731 	char *cp;
732 	int cderrno, descend, oflag, saved_errno, nostat, doadjust,
733 	    readdir_errno;
734 	long level;
735 	long nlinks;	/* has to be signed because -1 is a magic value */
736 	size_t dnamlen, len, maxlen, nitems;
737 
738 	/* Set current node pointer. */
739 	cur = sp->fts_cur;
740 
741 	/*
742 	 * Open the directory for reading.  If this fails, we're done.
743 	 * If being called from fts_read, set the fts_info field.
744 	 */
745 #ifdef FTS_WHITEOUT
746 	if (ISSET(FTS_WHITEOUT))
747 		oflag = DTF_NODUP;
748 	else
749 		oflag = DTF_HIDEW | DTF_NODUP;
750 #else
751 #define __opendir2(path, flag) opendir(path)
752 #endif
753 	if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
754 		if (type == BREAD) {
755 			cur->fts_info = FTS_DNR;
756 			cur->fts_errno = errno;
757 		}
758 		return (NULL);
759 	}
760 
761 	/*
762 	 * Nlinks is the number of possible entries of type directory in the
763 	 * directory if we're cheating on stat calls, 0 if we're not doing
764 	 * any stat calls at all, -1 if we're doing stats on everything.
765 	 */
766 	if (type == BNAMES) {
767 		nlinks = 0;
768 		/* Be quiet about nostat, GCC. */
769 		nostat = 0;
770 	} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
771 		if (fts_ufslinks(sp, cur))
772 			nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
773 		else
774 			nlinks = -1;
775 		nostat = 1;
776 	} else {
777 		nlinks = -1;
778 		nostat = 0;
779 	}
780 
781 #ifdef notdef
782 	(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
783 	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
784 	    ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
785 #endif
786 	/*
787 	 * If we're going to need to stat anything or we want to descend
788 	 * and stay in the directory, chdir.  If this fails we keep going,
789 	 * but set a flag so we don't chdir after the post-order visit.
790 	 * We won't be able to stat anything, but we can still return the
791 	 * names themselves.  Note, that since fts_read won't be able to
792 	 * chdir into the directory, it will have to return different path
793 	 * names than before, i.e. "a/b" instead of "b".  Since the node
794 	 * has already been visited in pre-order, have to wait until the
795 	 * post-order visit to return the error.  There is a special case
796 	 * here, if there was nothing to stat then it's not an error to
797 	 * not be able to stat.  This is all fairly nasty.  If a program
798 	 * needed sorted entries or stat information, they had better be
799 	 * checking FTS_NS on the returned nodes.
800 	 */
801 	cderrno = 0;
802 	if (nlinks || type == BREAD) {
803 		if (fts_safe_changedir(sp, cur, _dirfd(dirp), NULL)) {
804 			if (nlinks && type == BREAD)
805 				cur->fts_errno = errno;
806 			cur->fts_flags |= FTS_DONTCHDIR;
807 			descend = 0;
808 			cderrno = errno;
809 		} else
810 			descend = 1;
811 	} else
812 		descend = 0;
813 
814 	/*
815 	 * Figure out the max file name length that can be stored in the
816 	 * current path -- the inner loop allocates more path as necessary.
817 	 * We really wouldn't have to do the maxlen calculations here, we
818 	 * could do them in fts_read before returning the path, but it's a
819 	 * lot easier here since the length is part of the dirent structure.
820 	 *
821 	 * If not changing directories set a pointer so that can just append
822 	 * each new name into the path.
823 	 */
824 	len = NAPPEND(cur);
825 	if (ISSET(FTS_NOCHDIR)) {
826 		cp = sp->fts_path + len;
827 		*cp++ = '/';
828 	} else {
829 		/* GCC, you're too verbose. */
830 		cp = NULL;
831 	}
832 	len++;
833 	maxlen = sp->fts_pathlen - len;
834 
835 	level = cur->fts_level + 1;
836 
837 	/* Read the directory, attaching each entry to the `link' pointer. */
838 	doadjust = 0;
839 	readdir_errno = 0;
840 	for (head = tail = NULL, nitems = 0;
841 	    (dp = fts_safe_readdir(dirp, &readdir_errno));) {
842 		dnamlen = dp->d_namlen;
843 		if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
844 			continue;
845 
846 		if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL)
847 			goto mem1;
848 		if (dnamlen >= maxlen) {	/* include space for NUL */
849 			oldaddr = sp->fts_path;
850 			if (fts_palloc(sp, dnamlen + len + 1)) {
851 				/*
852 				 * No more memory for path or structures.  Save
853 				 * errno, free up the current structure and the
854 				 * structures already allocated.
855 				 */
856 mem1:				saved_errno = errno;
857 				if (p)
858 					free(p);
859 				fts_lfree(head);
860 				(void)closedir(dirp);
861 				cur->fts_info = FTS_ERR;
862 				SET(FTS_STOP);
863 				errno = saved_errno;
864 				return (NULL);
865 			}
866 			/* Did realloc() change the pointer? */
867 			if (oldaddr != sp->fts_path) {
868 				doadjust = 1;
869 				if (ISSET(FTS_NOCHDIR))
870 					cp = sp->fts_path + len;
871 			}
872 			maxlen = sp->fts_pathlen - len;
873 		}
874 
875 		p->fts_level = level;
876 		p->fts_parent = sp->fts_cur;
877 		p->fts_pathlen = len + dnamlen;
878 
879 #ifdef FTS_WHITEOUT
880 		if (dp->d_type == DT_WHT)
881 			p->fts_flags |= FTS_ISW;
882 #endif
883 
884 		if (cderrno) {
885 			if (nlinks) {
886 				p->fts_info = FTS_NS;
887 				p->fts_errno = cderrno;
888 			} else
889 				p->fts_info = FTS_NSOK;
890 			p->fts_accpath = cur->fts_accpath;
891 		} else if (nlinks == 0
892 #ifdef DT_DIR
893 		    || (nostat &&
894 		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
895 #endif
896 		    ) {
897 			p->fts_accpath =
898 			    ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
899 			p->fts_info = FTS_NSOK;
900 		} else {
901 			/* Build a file name for fts_stat to stat. */
902 			if (ISSET(FTS_NOCHDIR)) {
903 				p->fts_accpath = p->fts_path;
904 				memmove(cp, p->fts_name, p->fts_namelen + 1);
905 				p->fts_info = fts_stat(sp, p, 0, _dirfd(dirp));
906 			} else {
907 				p->fts_accpath = p->fts_name;
908 				p->fts_info = fts_stat(sp, p, 0, -1);
909 			}
910 
911 			/* Decrement link count if applicable. */
912 			if (nlinks > 0 && (p->fts_info == FTS_D ||
913 			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
914 				--nlinks;
915 		}
916 
917 		/* We walk in directory order so "ls -f" doesn't get upset. */
918 		p->fts_link = NULL;
919 		if (head == NULL)
920 			head = tail = p;
921 		else {
922 			tail->fts_link = p;
923 			tail = p;
924 		}
925 		++nitems;
926 	}
927 
928 	if (readdir_errno) {
929 		cur->fts_errno = readdir_errno;
930 		/*
931 		 * If we've not read any items yet, treat
932 		 * the error as if we can't access the dir.
933 		 */
934 		cur->fts_info = nitems ? FTS_ERR : FTS_DNR;
935 	}
936 
937 	if (dirp)
938 		(void)closedir(dirp);
939 
940 	/*
941 	 * If realloc() changed the address of the path, adjust the
942 	 * addresses for the rest of the tree and the dir list.
943 	 */
944 	if (doadjust)
945 		fts_padjust(sp, head);
946 
947 	/*
948 	 * If not changing directories, reset the path back to original
949 	 * state.
950 	 */
951 	if (ISSET(FTS_NOCHDIR))
952 		sp->fts_path[cur->fts_pathlen] = '\0';
953 
954 	/*
955 	 * If descended after called from fts_children or after called from
956 	 * fts_read and nothing found, get back.  At the root level we use
957 	 * the saved fd; if one of fts_open()'s arguments is a relative path
958 	 * to an empty directory, we wind up here with no other way back.  If
959 	 * can't get back, we're done.
960 	 */
961 	if (descend && (type == BCHILD || !nitems) &&
962 	    (cur->fts_level == FTS_ROOTLEVEL ?
963 	    FCHDIR(sp, sp->fts_rfd) :
964 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
965 		fts_lfree(head);
966 		cur->fts_info = FTS_ERR;
967 		SET(FTS_STOP);
968 		return (NULL);
969 	}
970 
971 	/* If didn't find anything, return NULL. */
972 	if (!nitems) {
973 		if (type == BREAD &&
974 		    cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR)
975 			cur->fts_info = FTS_DP;
976 		return (NULL);
977 	}
978 
979 	/* Sort the entries. */
980 	if (sp->fts_compar && nitems > 1)
981 		head = fts_sort(sp, head, nitems);
982 	return (head);
983 }
984 
985 static int
986 fts_stat(FTS *sp, FTSENT *p, int follow, int dfd)
987 {
988 	FTSENT *t;
989 	dev_t dev;
990 	ino_t ino;
991 	struct stat *sbp, sb;
992 	int saved_errno;
993 	const char *path;
994 
995 	if (dfd == -1) {
996 		path = p->fts_accpath;
997 		dfd = AT_FDCWD;
998 	} else {
999 		path = p->fts_name;
1000 	}
1001 
1002 	/* If user needs stat info, stat buffer already allocated. */
1003 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
1004 
1005 #ifdef FTS_WHITEOUT
1006 	/* Check for whiteout. */
1007 	if (p->fts_flags & FTS_ISW) {
1008 		if (sbp != &sb) {
1009 			memset(sbp, '\0', sizeof(*sbp));
1010 			sbp->st_mode = S_IFWHT;
1011 		}
1012 		return (FTS_W);
1013 	}
1014 #endif
1015 
1016 	/*
1017 	 * If doing a logical walk, or application requested FTS_FOLLOW, do
1018 	 * a stat(2).  If that fails, check for a non-existent symlink.  If
1019 	 * fail, set the errno from the stat call.
1020 	 */
1021 	if (ISSET(FTS_LOGICAL) || follow) {
1022 		if (fstatat(dfd, path, sbp, 0)) {
1023 			saved_errno = errno;
1024 			if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
1025 				p->fts_errno = saved_errno;
1026 				goto err;
1027 			}
1028 			errno = 0;
1029 			if (S_ISLNK(sbp->st_mode))
1030 				return (FTS_SLNONE);
1031 		}
1032 	} else if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
1033 		p->fts_errno = errno;
1034 err:		memset(sbp, 0, sizeof(struct stat));
1035 		return (FTS_NS);
1036 	}
1037 
1038 	if (S_ISDIR(sbp->st_mode)) {
1039 		/*
1040 		 * Set the device/inode.  Used to find cycles and check for
1041 		 * crossing mount points.  Also remember the link count, used
1042 		 * in fts_build to limit the number of stat calls.  It is
1043 		 * understood that these fields are only referenced if fts_info
1044 		 * is set to FTS_D.
1045 		 */
1046 		dev = p->fts_dev = sbp->st_dev;
1047 		ino = p->fts_ino = sbp->st_ino;
1048 		p->fts_nlink = sbp->st_nlink;
1049 
1050 		if (ISDOT(p->fts_name))
1051 			return (FTS_DOT);
1052 
1053 		/*
1054 		 * Cycle detection is done by brute force when the directory
1055 		 * is first encountered.  If the tree gets deep enough or the
1056 		 * number of symbolic links to directories is high enough,
1057 		 * something faster might be worthwhile.
1058 		 */
1059 		for (t = p->fts_parent;
1060 		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
1061 			if (ino == t->fts_ino && dev == t->fts_dev) {
1062 				p->fts_cycle = t;
1063 				return (FTS_DC);
1064 			}
1065 		return (FTS_D);
1066 	}
1067 	if (S_ISLNK(sbp->st_mode))
1068 		return (FTS_SL);
1069 	if (S_ISREG(sbp->st_mode))
1070 		return (FTS_F);
1071 	return (FTS_DEFAULT);
1072 }
1073 
1074 static FTSENT *
1075 fts_sort(FTS *sp, FTSENT *head, size_t nitems)
1076 {
1077 	FTSENT **ap, *p;
1078 
1079 	/*
1080 	 * Construct an array of pointers to the structures and call qsort(3).
1081 	 * Reassemble the array in the order returned by qsort.  If unable to
1082 	 * sort for memory reasons, return the directory entries in their
1083 	 * current order.  Allocate enough space for the current needs plus
1084 	 * 40 so don't realloc one entry at a time.
1085 	 */
1086 	if (nitems > sp->fts_nitems) {
1087 		sp->fts_nitems = nitems + 40;
1088 		if ((sp->fts_array = reallocf(sp->fts_array,
1089 		    sp->fts_nitems * sizeof(FTSENT *))) == NULL) {
1090 			sp->fts_nitems = 0;
1091 			return (head);
1092 		}
1093 	}
1094 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
1095 		*ap++ = p;
1096 	if (ISSET(FTS_COMPAR_B)) {
1097 #ifdef __BLOCKS__
1098 		qsort_b(sp->fts_array, nitems, sizeof(FTSENT *),
1099 		    (int (^)(const void *, const void *))sp->fts_compar_b);
1100 #else
1101 		qsort_b(sp->fts_array, nitems, sizeof(FTSENT *),
1102 		    sp->fts_compar_b);
1103 #endif /* __BLOCKS__ */
1104 	} else {
1105 		qsort(sp->fts_array, nitems, sizeof(FTSENT *),
1106 		    (int (*)(const void *, const void *))sp->fts_compar);
1107 	}
1108 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
1109 		ap[0]->fts_link = ap[1];
1110 	ap[0]->fts_link = NULL;
1111 	return (head);
1112 }
1113 
1114 static FTSENT *
1115 fts_alloc(FTS *sp, char *name, size_t namelen)
1116 {
1117 	FTSENT *p;
1118 	size_t len;
1119 
1120 	/*
1121 	 * The file name is a variable length array and no stat structure is
1122 	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
1123 	 * structure, the file name and the stat structure in one chunk, but
1124 	 * be careful that the stat structure is reasonably aligned.
1125 	 */
1126 	len = sizeof(FTSENT) + namelen + 1;
1127 	if (!ISSET(FTS_NOSTAT)) {
1128 		len = roundup(len, alignof(struct stat));
1129 		p = calloc(1, len + sizeof(struct stat));
1130 	} else {
1131 		p = calloc(1, len);
1132 	}
1133 	if (p == NULL)
1134 		return (NULL);
1135 
1136 	p->fts_symfd = -1;
1137 	p->fts_path = sp->fts_path;
1138 	p->fts_name = (char *)(p + 1);
1139 	p->fts_namelen = namelen;
1140 	p->fts_instr = FTS_NOINSTR;
1141 	if (!ISSET(FTS_NOSTAT))
1142 		p->fts_statp = (struct stat *)((char *)p + len);
1143 	p->fts_fts = sp;
1144 	memcpy(p->fts_name, name, namelen);
1145 
1146 	return (p);
1147 }
1148 
1149 static void
1150 fts_lfree(FTSENT *head)
1151 {
1152 	FTSENT *p;
1153 
1154 	/* Free a linked list of structures. */
1155 	while ((p = head)) {
1156 		head = head->fts_link;
1157 		free(p);
1158 	}
1159 }
1160 
1161 /*
1162  * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1163  * Most systems will allow creation of paths much longer than MAXPATHLEN, even
1164  * though the kernel won't resolve them.  Add the size (not just what's needed)
1165  * plus 256 bytes so don't realloc the path 2 bytes at a time.
1166  */
1167 static int
1168 fts_palloc(FTS *sp, size_t more)
1169 {
1170 
1171 	sp->fts_pathlen += more + 256;
1172 	sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen);
1173 	return (sp->fts_path == NULL);
1174 }
1175 
1176 /*
1177  * When the path is realloc'd, have to fix all of the pointers in structures
1178  * already returned.
1179  */
1180 static void
1181 fts_padjust(FTS *sp, FTSENT *head)
1182 {
1183 	FTSENT *p;
1184 	char *addr = sp->fts_path;
1185 
1186 #define	ADJUST(p) do {							\
1187 	if ((p)->fts_accpath != (p)->fts_name) {			\
1188 		(p)->fts_accpath =					\
1189 		    (char *)addr + ((p)->fts_accpath - (p)->fts_path);	\
1190 	}								\
1191 	(p)->fts_path = addr;						\
1192 } while (0)
1193 	/* Adjust the current set of children. */
1194 	for (p = sp->fts_child; p; p = p->fts_link)
1195 		ADJUST(p);
1196 
1197 	/* Adjust the rest of the tree, including the current level. */
1198 	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
1199 		ADJUST(p);
1200 		p = p->fts_link ? p->fts_link : p->fts_parent;
1201 	}
1202 }
1203 
1204 static size_t
1205 fts_maxarglen(char * const *argv)
1206 {
1207 	size_t len, max;
1208 
1209 	for (max = 0; *argv; ++argv)
1210 		if ((len = strlen(*argv)) > max)
1211 			max = len;
1212 	return (max + 1);
1213 }
1214 
1215 /*
1216  * Change to dir specified by fd or p->fts_accpath without getting
1217  * tricked by someone changing the world out from underneath us.
1218  * Assumes p->fts_dev and p->fts_ino are filled in.
1219  */
1220 static int
1221 fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path)
1222 {
1223 	int ret, oerrno, newfd;
1224 	struct stat sb;
1225 	struct statfs sf;
1226 
1227 	newfd = fd;
1228 	if (ISSET(FTS_NOCHDIR))
1229 		return (0);
1230 	if (fd < 0 && (newfd = _open(path, O_RDONLY | O_DIRECTORY |
1231 	    O_CLOEXEC, 0)) < 0)
1232 		return (-1);
1233 	if (_fstat(newfd, &sb)) {
1234 		ret = -1;
1235 		goto bail;
1236 	}
1237 	if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1238 		if (_fstatfs(newfd, &sf) != 0 ||
1239 		    (sf.f_flags & MNT_AUTOMOUNTED) == 0) {
1240 			errno = ENOENT;		/* disinformation */
1241 			ret = -1;
1242 			goto bail;
1243 		}
1244 		/* autofs might did the mount under us, accept. */
1245 		p->fts_dev = sb.st_dev;
1246 		p->fts_ino = sb.st_ino;
1247 	}
1248 	ret = fchdir(newfd);
1249 bail:
1250 	oerrno = errno;
1251 	if (fd < 0)
1252 		(void)_close(newfd);
1253 	errno = oerrno;
1254 	return (ret);
1255 }
1256 
1257 /*
1258  * Check if the filesystem for "ent" has UFS-style links.
1259  */
1260 static int
1261 fts_ufslinks(FTS *sp, const FTSENT *ent)
1262 {
1263 	struct _fts_private *priv;
1264 	const char **cpp;
1265 
1266 	priv = (struct _fts_private *)sp;
1267 	/*
1268 	 * If this node's device is different from the previous, grab
1269 	 * the filesystem information, and decide on the reliability
1270 	 * of the link information from this filesystem for stat(2)
1271 	 * avoidance.
1272 	 */
1273 	if (priv->ftsp_dev != ent->fts_dev) {
1274 		if (statfs(ent->fts_path, &priv->ftsp_statfs) != -1) {
1275 			priv->ftsp_dev = ent->fts_dev;
1276 			priv->ftsp_linksreliable = 0;
1277 			for (cpp = ufslike_filesystems; *cpp; cpp++) {
1278 				if (strcmp(priv->ftsp_statfs.f_fstypename,
1279 				    *cpp) == 0) {
1280 					priv->ftsp_linksreliable = 1;
1281 					break;
1282 				}
1283 			}
1284 		} else {
1285 			priv->ftsp_linksreliable = 0;
1286 		}
1287 	}
1288 	return (priv->ftsp_linksreliable);
1289 }
1290