nftw.3p (9321B)
- '\" et
- .TH NFTW "3P" 2017 "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 \-1.
- .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 \-1 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 \-1 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
- #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], \(aqd\(aq) != NULL)
- flags |= FTW_DEPTH;
- if (argc > 2 && strchr(argv[2], \(aqp\(aq) != 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
- .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\(hy2017,
- .IR "\fB<ftw.h>\fP"
- .\"
- .SH COPYRIGHT
- Portions of this text are reprinted and reproduced in electronic form
- from IEEE Std 1003.1-2017, Standard for Information Technology
- -- Portable Operating System Interface (POSIX), The Open Group Base
- Specifications Issue 7, 2018 Edition,
- Copyright (C) 2018 by the Institute of
- Electrical and Electronics Engineers, Inc and The Open Group.
- 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.opengroup.org/unix/online.html .
- .PP
- 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 .