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