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