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