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