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