xref: /freebsd/lib/libc/gen/ftw.c (revision 8e27b6e60f1d378a4e7fb15f0a564ca3c298a6bb)
1*8e27b6e6SXin LI /*	$OpenBSD: ftw.c,v 1.5 2005/08/08 08:05:34 espie Exp $	*/
2e1bcce4fSTim J. Robbins 
3b03b864aSDavid Schultz /*
4e1bcce4fSTim J. Robbins  * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
5b03b864aSDavid Schultz  *
6e1bcce4fSTim J. Robbins  * Permission to use, copy, modify, and distribute this software for any
7e1bcce4fSTim J. Robbins  * purpose with or without fee is hereby granted, provided that the above
8e1bcce4fSTim J. Robbins  * copyright notice and this permission notice appear in all copies.
9b03b864aSDavid Schultz  *
10e1bcce4fSTim J. Robbins  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11e1bcce4fSTim J. Robbins  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12e1bcce4fSTim J. Robbins  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13e1bcce4fSTim J. Robbins  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14e1bcce4fSTim J. Robbins  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15e1bcce4fSTim J. Robbins  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16e1bcce4fSTim J. Robbins  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17b03b864aSDavid Schultz  *
18e1bcce4fSTim J. Robbins  * Sponsored in part by the Defense Advanced Research Projects
19e1bcce4fSTim J. Robbins  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20e1bcce4fSTim J. Robbins  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21b03b864aSDavid Schultz  */
22b03b864aSDavid Schultz 
23e1bcce4fSTim J. Robbins #include <sys/cdefs.h>
24e1bcce4fSTim J. Robbins __FBSDID("$FreeBSD$");
25e1bcce4fSTim J. Robbins 
26e1bcce4fSTim J. Robbins #include <sys/types.h>
27b03b864aSDavid Schultz #include <sys/stat.h>
28e1bcce4fSTim J. Robbins #include <errno.h>
29b03b864aSDavid Schultz #include <fts.h>
30e1bcce4fSTim J. Robbins #include <ftw.h>
31e1bcce4fSTim J. Robbins #include <limits.h>
32b03b864aSDavid Schultz 
33e1bcce4fSTim J. Robbins int
34e1bcce4fSTim J. Robbins ftw(const char *path, int (*fn)(const char *, const struct stat *, int),
35e1bcce4fSTim J. Robbins     int nfds)
36e1bcce4fSTim J. Robbins {
37e1bcce4fSTim J. Robbins 	char * const paths[2] = { (char *)path, NULL };
38e1bcce4fSTim J. Robbins 	FTSENT *cur;
39e1bcce4fSTim J. Robbins 	FTS *ftsp;
40e1bcce4fSTim J. Robbins 	int error = 0, fnflag, sverrno;
41b03b864aSDavid Schultz 
42e1bcce4fSTim J. Robbins 	/* XXX - nfds is currently unused */
43e1bcce4fSTim J. Robbins 	if (nfds < 1 || nfds > OPEN_MAX) {
44b03b864aSDavid Schultz 		errno = EINVAL;
45e1bcce4fSTim J. Robbins 		return (-1);
46b03b864aSDavid Schultz 	}
47b03b864aSDavid Schultz 
48e1bcce4fSTim J. Robbins 	ftsp = fts_open(paths, FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR, NULL);
49e1bcce4fSTim J. Robbins 	if (ftsp == NULL)
50e1bcce4fSTim J. Robbins 		return (-1);
51e1bcce4fSTim J. Robbins 	while ((cur = fts_read(ftsp)) != NULL) {
52e1bcce4fSTim J. Robbins 		switch (cur->fts_info) {
53b03b864aSDavid Schultz 		case FTS_D:
54e1bcce4fSTim J. Robbins 			fnflag = FTW_D;
55b03b864aSDavid Schultz 			break;
56b03b864aSDavid Schultz 		case FTS_DNR:
57e1bcce4fSTim J. Robbins 			fnflag = FTW_DNR;
58b03b864aSDavid Schultz 			break;
59b03b864aSDavid Schultz 		case FTS_DP:
60e1bcce4fSTim J. Robbins 			/* we only visit in preorder */
61e1bcce4fSTim J. Robbins 			continue;
62e1bcce4fSTim J. Robbins 		case FTS_F:
63e1bcce4fSTim J. Robbins 		case FTS_DEFAULT:
64e1bcce4fSTim J. Robbins 			fnflag = FTW_F;
65b03b864aSDavid Schultz 			break;
66e1bcce4fSTim J. Robbins 		case FTS_NS:
67e1bcce4fSTim J. Robbins 		case FTS_NSOK:
68e1bcce4fSTim J. Robbins 		case FTS_SLNONE:
69e1bcce4fSTim J. Robbins 			fnflag = FTW_NS;
70e1bcce4fSTim J. Robbins 			break;
71e1bcce4fSTim J. Robbins 		case FTS_SL:
72e1bcce4fSTim J. Robbins 			fnflag = FTW_SL;
73e1bcce4fSTim J. Robbins 			break;
74e1bcce4fSTim J. Robbins 		case FTS_DC:
75e1bcce4fSTim J. Robbins 			errno = ELOOP;
76e1bcce4fSTim J. Robbins 			/* FALLTHROUGH */
77b03b864aSDavid Schultz 		default:
78e1bcce4fSTim J. Robbins 			error = -1;
79e1bcce4fSTim J. Robbins 			goto done;
80e1bcce4fSTim J. Robbins 		}
81e1bcce4fSTim J. Robbins 		error = fn(cur->fts_path, cur->fts_statp, fnflag);
82e1bcce4fSTim J. Robbins 		if (error != 0)
839b5f0052SDavid Schultz 			break;
84b03b864aSDavid Schultz 	}
85e1bcce4fSTim J. Robbins done:
86e1bcce4fSTim J. Robbins 	sverrno = errno;
87e1bcce4fSTim J. Robbins 	if (fts_close(ftsp) != 0 && error == 0)
88e1bcce4fSTim J. Robbins 		error = -1;
89e1bcce4fSTim J. Robbins 	else
90e1bcce4fSTim J. Robbins 		errno = sverrno;
91e1bcce4fSTim J. Robbins 	return (error);
92b03b864aSDavid Schultz }
93