1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
'\" et
.TH NFTW "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
nftw
\(em walk a file tree
.SH SYNOPSIS
.LP
.nf
#include <ftw.h>
.P
int nftw(const char *\fIpath\fP, int (*\fIfn\fP)(const char *,
const struct stat *, int, struct FTW *), int \fIfd_limit\fP, int \fIflags\fP);
.fi
.SH DESCRIPTION
The
\fInftw\fR()
function shall recursively descend the directory hierarchy rooted in
.IR path .
The
\fInftw\fR()
function has a similar effect to
\fIftw\fR()
except that it takes an additional argument
.IR flags ,
which is a bitwise-inclusive OR of zero or more of the following flags:
.IP FTW_CHDIR 12
If set,
\fInftw\fR()
shall change the current working directory to each directory as it
reports files in that directory. If clear,
\fInftw\fR()
shall not change the current working directory.
.IP FTW_DEPTH 12
If set,
\fInftw\fR()
shall report all files in a directory before reporting the directory
itself. If clear,
\fInftw\fR()
shall report any directory before reporting the files in that directory.
.IP FTW_MOUNT 12
If set,
\fInftw\fR()
shall only report files in the same file system as
.IR path .
If clear,
\fInftw\fR()
shall report all files encountered during the walk.
.IP FTW_PHYS 12
If set,
\fInftw\fR()
shall perform a physical walk and shall not follow symbolic links.
.P
If FTW_PHYS is clear and FTW_DEPTH is set,
\fInftw\fR()
shall follow links instead of reporting them, but shall not report any
directory that would be a descendant of itself. If FTW_PHYS is clear
and FTW_DEPTH is clear,
\fInftw\fR()
shall follow links instead of reporting them, but shall not report the
contents of any directory that would be a descendant of itself.
.P
At each file it encounters,
\fInftw\fR()
shall call the user-supplied function
.IR fn
with four arguments:
.IP " *" 4
The first argument is the pathname of the object.
.IP " *" 4
The second argument is a pointer to the
.BR stat
buffer containing information on the object, filled in as if
\fIfstatat\fR(),
\fIstat\fR(),
or
\fIlstat\fR()
had been called to retrieve the information.
.IP " *" 4
The third argument is an integer giving additional information. Its
value is one of the following:
.RS 4
.IP FTW_D 10
The object is a directory.
.IP FTW_DNR 10
The object is a directory that cannot be read. The
.IR fn
function shall not be called for any of its descendants.
.IP FTW_DP 10
The object is a directory and subdirectories have been visited. (This
condition shall only occur if the FTW_DEPTH flag is included in
.IR flags .)
.IP FTW_F 10
The object is a non-directory file.
.IP FTW_NS 10
The
\fIstat\fR()
function failed on the object because of lack of appropriate
permission. The
.BR stat
buffer passed to
.IR fn
is undefined. Failure of
\fIstat\fR()
for any other reason is considered an error and
\fInftw\fR()
shall return \(mi1.
.IP FTW_SL 10
The object is a symbolic link. (This condition shall only occur if the
FTW_PHYS flag is included in
.IR flags .)
.IP FTW_SLN 10
The object is a symbolic link that does not name an existing file.
(This condition shall only occur if the FTW_PHYS flag is not included in
.IR flags .)
.RE
.IP " *" 4
The fourth argument is a pointer to an
.BR FTW
structure.
The value of
.BR base
is the offset of the object's filename in the pathname passed as the
first argument to
.IR fn .
The value of
.BR level
indicates depth relative to the root of the walk, where the root level
is 0.
.P
The results are unspecified if the application-supplied
.IR fn
function does not preserve the current working directory.
.P
The argument
.IR fd_limit
sets the maximum number of file descriptors that shall be used by
\fInftw\fR()
while traversing the file tree. At most one file descriptor shall be
used for each directory level.
.P
The
\fInftw\fR()
function need not be thread-safe.
.SH "RETURN VALUE"
The
\fInftw\fR()
function shall continue until the first of the following conditions
occurs:
.IP " *" 4
An invocation of
.IR fn
shall return a non-zero value, in which case
\fInftw\fR()
shall return that value.
.IP " *" 4
The
\fInftw\fR()
function detects an error other than
.BR [EACCES]
(see FTW_DNR and FTW_NS above),
in which case
\fInftw\fR()
shall return \(mi1 and set
.IR errno
to indicate the error.
.IP " *" 4
The tree is exhausted, in which case
\fInftw\fR()
shall return 0.
.SH ERRORS
The
\fInftw\fR()
function shall fail if:
.TP
.BR EACCES
Search permission is denied for any component of
.IR path
or read permission is denied for
.IR path ,
or
.IR fn
returns \(mi1 and does not reset
.IR errno .
.TP
.BR ELOOP
A loop exists in symbolic links encountered during resolution of the
.IR path
argument.
.TP
.BR ENAMETOOLONG
.br
The length of a component of a pathname is longer than
{NAME_MAX}.
.TP
.BR ENOENT
A component of
.IR path
does not name an existing file or
.IR path
is an empty string.
.TP
.BR ENOTDIR
A component of
.IR path
names an existing file that is neither a directory nor a symbolic link
to a directory.
.TP
.BR EOVERFLOW
A field in the
.BR stat
structure cannot be represented correctly in the current programming
environment for one or more files found in the file hierarchy.
.P
The
\fInftw\fR()
function may fail if:
.TP
.BR ELOOP
More than
{SYMLOOP_MAX}
symbolic links were encountered during resolution of the
.IR path
argument.
.TP
.BR EMFILE
All file descriptors available to the process are currently open.
.TP
.BR ENAMETOOLONG
.br
The length of a pathname exceeds
{PATH_MAX},
or pathname resolution of a symbolic link produced an intermediate
result with a length that exceeds
{PATH_MAX}.
.TP
.BR ENFILE
Too many files are currently open in the system.
.P
In addition,
.IR errno
may be set if the function pointed to by
.IR fn
causes
.IR errno
to be set.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
The following program traverses the directory tree under the path named
in its first command-line argument, or under the current directory if
no argument is supplied. It displays various information about each
file. The second command-line argument can be used to specify characters
that control the value assigned to the
.IR flags
argument when calling
\fInftw\fR().
.sp
.RS 4
.nf
\fB
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
.P
static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
printf("%-3s %2d %7jd %-40s %d %s\en",
(tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
(tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ?
(S_ISBLK(sb->st_mode) ? "f b" :
S_ISCHR(sb->st_mode) ? "f c" :
S_ISFIFO(sb->st_mode) ? "f p" :
S_ISREG(sb->st_mode) ? "f r" :
S_ISSOCK(sb->st_mode) ? "f s" : "f ?") :
(tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
(tflag == FTW_SLN) ? "sln" : "?",
ftwbuf->level, (intmax_t) sb->st_size,
fpath, ftwbuf->base, fpath + ftwbuf->base);
return 0; /* To tell nftw() to continue */
}
.P
int
main(int argc, char *argv[])
{
int flags = 0;
.P
if (argc > 2 && strchr(argv[2], 'd') != NULL)
flags |= FTW_DEPTH;
if (argc > 2 && strchr(argv[2], 'p') != NULL)
flags |= FTW_PHYS;
.P
if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
{
perror("nftw");
exit(EXIT_FAILURE);
}
.P
exit(EXIT_SUCCESS);
}
.fi \fR
.P
.RE
.SH "APPLICATION USAGE"
The
\fInftw\fR()
function may allocate dynamic storage during its operation. If
\fInftw\fR()
is forcibly terminated, such as by
\fIlongjmp\fR()
or
\fIsiglongjmp\fR()
being executed by the function pointed to by
.IR fn
or an interrupt routine,
\fInftw\fR()
does not have a chance to free that storage, so it remains permanently
allocated. A safe way to handle interrupts is to store the fact that an
interrupt has occurred, and arrange to have the function pointed to by
.IR fn
return a non-zero value at its next invocation.
.SH RATIONALE
None.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIfdopendir\fR\^(\|)",
.IR "\fIfstatat\fR\^(\|)",
.IR "\fIreaddir\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2008,
.IR "\fB<ftw.h>\fP"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.unix.org/online.html .
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|