]>
git.saurik.com Git - apple/libc.git/blob - gen/nftw.c
5a06b987948e4487b8725d19420d7137eb63de04
1 /* $OpenBSD: nftw.c,v 1.2 2003/07/21 21:15:32 millert Exp $ */
4 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 #if defined(LIBC_SCCS) && !defined(lint)
24 static const char rcsid
[] = "$OpenBSD: nftw.c,v 1.2 2003/07/21 21:15:32 millert Exp $";
25 #endif /* LIBC_SCCS and not lint */
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
41 both_ftw(const char *path
,
42 int (*ofn
)(const char *, const struct stat
*, int),
43 int (*nfn
)(const char *, const struct stat
*, int, struct FTW
*),
44 int nfds
, int ftwflags
)
50 int ftsflags
, fnflag
, error
, postorder
, sverrno
;
51 int cwd_fd
= -1; /* cwd_fd != -1 means call chdir a lot */
54 /* Macro to skip the mount point itself in UNiX03 mode, in legcy
55 mode the mount point is returned, but we don't decend into it */
56 #define SKIP_MOUNT if ((ftwflags & FTW_MOUNT) \
57 && cur->fts_statp->st_dev != path_stat.st_dev) { \
64 /* XXX - nfds is currently unused */
65 if (nfds
< 1 || nfds
> OPEN_MAX
) {
70 ftsflags
= FTS_COMFOLLOW
;
71 if (!(ftwflags
& FTW_CHDIR
))
72 ftsflags
|= FTS_NOCHDIR
;
73 if (ftwflags
& FTW_MOUNT
)
75 if (ftwflags
& FTW_PHYS
) {
76 ftsflags
|= FTS_PHYSICAL
;
78 ftsflags
|= FTS_LOGICAL
;
80 postorder
= (ftwflags
& FTW_DEPTH
) != 0;
82 /* We have been requested to change directories, and fts doesn't
83 always do it (never for FTS_LOGICAL, and sometimes not for
85 if (ftwflags
& FTW_CHDIR
) {
86 cwd_fd
= open(".", O_RDONLY
, 0);
90 /* Prevent problems if fts ever starts using chdir when passed
92 ftsflags
|= FTS_NOCHDIR
;
96 struct stat path_stat
;
98 /* UNIX03 requires us to return -1/errno=ELOOP if path
99 is a looping symlink; fts_open is succesful and fts_read
100 gives us FTS_NS which isn't very useful, in fact we get
101 pretty much the same behaviour for ENAMETOOLONG, ENOENT,
102 ENOTDIR, and EACCES */
104 int rc
= stat(path
, &path_stat
);
107 && (errno
== ELOOP
|| errno
== ENAMETOOLONG
|| errno
== ENOENT
108 || errno
== ENOTDIR
|| errno
== EACCES
)) {
111 if (rc
>= 0 && nfn
) {
112 if (!S_ISDIR(path_stat
.st_mode
)) {
121 ftsp
= fts_open((char * const *)paths
, ftsflags
, NULL
);
126 while ((cur
= fts_read(ftsp
)) != NULL
) {
127 switch (cur
->fts_info
) {
132 /* we will get FTS_DNR next (this is not an issue for
133 FTS_DP, only FTS_D) */
134 if (access(cur
->fts_path
, R_OK
) != 0)
159 fnflag
= nfn
? FTW_SLN
: FTW_SL
;
162 /* The legacy behaviour did not signal an error
163 on symlink loops unless they ended up causing
164 a directory cycle, but the Unix2003 standard
165 requires ELOOP to end ftw and nftw walks with
168 int rc
= stat(cur
->fts_path
, &sb
);
169 if (rc
< 0 && errno
== ELOOP
) {
178 /* Unix03 says nftw should break cycles and not return
179 errors in non-physical mode (which is definitly what it
180 says ftw can't do) */
181 if (nfn
&& !(ftwflags
& FTW_PHYS
)) {
182 /* 4489297 - when FTW_DEPTH is set, skip
198 char *dir
, *free_me
= NULL
;
199 if (fnflag
== FTW_D
) {
202 /* we could alloc just enough for the directory,
203 and use memmove -- but that is a little more
204 error prone, and not noticable in with all the
206 dir
= free_me
= strdup(cur
->fts_path
);
207 dir
[cur
->fts_pathlen
- cur
->fts_namelen
] = '\0';
219 ftw
.base
= cur
->fts_pathlen
- cur
->fts_namelen
;
220 ftw
.level
= cur
->fts_level
;
221 error
= nfn(cur
->fts_path
, cur
->fts_statp
, fnflag
, &ftw
);
223 error
= ofn(cur
->fts_path
, cur
->fts_statp
, fnflag
);
226 if (fchdir(cwd_fd
) < 0) {
237 (void) fts_close(ftsp
);
243 ftw(const char *path
, int (*fn
)(const char *, const struct stat
*, int),
246 /* The legacy implmentation didn't follow symlinks, but Unix03
247 does - this was likely a bug in the legacy implemtation; JKH
248 thinks we ought change the legacy behaviour, and I agree; anyone
249 who doesn't should replace FTW_PHYS with
250 __DARWIN_UNIX03 ? 0 : FTW_PHYS */
251 return both_ftw(path
, fn
, NULL
, nfds
, FTW_PHYS
);
255 nftw(const char *path
, int (*fn
)(const char *, const struct stat
*, int,
256 struct FTW
*), int nfds
, int ftwflags
)
258 return both_ftw(path
, NULL
, fn
, nfds
, ftwflags
);