xref: /freebsd/lib/libc/gen/nftw-compat11.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <fts.h>
28 #include <ftw.h>
29 
30 #include "fts-compat11.h"
31 
32 int
33 freebsd11_nftw(const char *path,
34     int (*fn)(const char *, const struct freebsd11_stat *, int, struct FTW *),
35     int nfds, int ftwflags)
36 {
37 	char * const paths[2] = { (char *)path, NULL };
38 	struct FTW ftw;
39 	FTSENT11 *cur;
40 	FTS11 *ftsp;
41 	int error = 0, ftsflags, fnflag, postorder, sverrno;
42 
43 	/* XXX - nfds is currently unused */
44 	if (nfds < 1) {
45 		errno = EINVAL;
46 		return (-1);
47 	}
48 
49 	ftsflags = FTS_COMFOLLOW;
50 	if (!(ftwflags & FTW_CHDIR))
51 		ftsflags |= FTS_NOCHDIR;
52 	if (ftwflags & FTW_MOUNT)
53 		ftsflags |= FTS_XDEV;
54 	if (ftwflags & FTW_PHYS)
55 		ftsflags |= FTS_PHYSICAL;
56 	else
57 		ftsflags |= FTS_LOGICAL;
58 	postorder = (ftwflags & FTW_DEPTH) != 0;
59 	ftsp = freebsd11_fts_open(paths, ftsflags, NULL);
60 	if (ftsp == NULL)
61 		return (-1);
62 	while ((cur = freebsd11_fts_read(ftsp)) != NULL) {
63 		switch (cur->fts_info) {
64 		case FTS_D:
65 			if (postorder)
66 				continue;
67 			fnflag = FTW_D;
68 			break;
69 		case FTS_DC:
70 			continue;
71 		case FTS_DNR:
72 			fnflag = FTW_DNR;
73 			break;
74 		case FTS_DP:
75 			if (!postorder)
76 				continue;
77 			fnflag = FTW_DP;
78 			break;
79 		case FTS_F:
80 		case FTS_DEFAULT:
81 			fnflag = FTW_F;
82 			break;
83 		case FTS_NS:
84 		case FTS_NSOK:
85 			fnflag = FTW_NS;
86 			break;
87 		case FTS_SL:
88 			fnflag = FTW_SL;
89 			break;
90 		case FTS_SLNONE:
91 			fnflag = FTW_SLN;
92 			break;
93 		default:
94 			error = -1;
95 			goto done;
96 		}
97 		ftw.base = cur->fts_pathlen - cur->fts_namelen;
98 		ftw.level = cur->fts_level;
99 		error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
100 		if (error != 0)
101 			break;
102 	}
103 done:
104 	sverrno = errno;
105 	if (freebsd11_fts_close(ftsp) != 0 && error == 0)
106 		error = -1;
107 	else
108 		errno = sverrno;
109 	return (error);
110 }
111 
112 __sym_compat(nftw, freebsd11_nftw, FBSD_1.0);
113