nftw - walk a file tree
#include <ftw.h> int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int depth, int flags);
The nftw() function recursively descends the directory hierarchy rooted in path. The nftw() function has a similar effect to ftw() except that it takes an additional argument flags, which is a bitwise inclusive-OR of zero or more of the following flags:
- FTW_CHDIR
- If set, nftw() will change the current working directory to each directory as it reports files in that directory. If clear, nftw() will not change the current working directory.
- FTW_DEPTH
- If set, nftw() will report all files in a directory before reporting the directory itself. If clear, nftw() will report any directory before reporting the files in that directory.
- FTW_MOUNT
- If set, nftw() will only report files in the same file system as path. If clear, nftw() will report all files encountered during the walk.
- FTW_PHYS
- If set, nftw() performs a physical walk and does not follow symbolic links. If clear, nftw() will follow links instead of reporting them, and will not report the same file twice.
At each file it encounters, nftw() calls the user-supplied function fn() with four arguments:
- The first argument is the pathname of the object.
- The second argument is a pointer to the stat buffer containing information on the object.
- The third argument is an integer giving additional information. Its value is one of the following:
- FTW_F
- The object is a file.
- FTW_D
- The object is a directory.
- FTW_DP
- The object is a directory and subdirectories have been visited. (This condition will only occur if the FTW_DEPTH flag is included in flags.)
- FTW_SL
- The object is a symbolic link. (This condition will only occur if the FTW_PHYS flag is included in flags.)
- FTW_SLN
- The object is a symbolic link that does not name an existing file. (This condition will only occur if the FTW_PHYS flag is not included in flags.)
- FTW_DNR
- The object is a directory that cannot be read. The fn() function will not be called for any of its descendants.
- FTW_NS
- The stat() function failed on the object because of lack of appropriate permission. The stat buffer passed to fn() is undefined. Failure of stat() for any other reason is considered an error and nftw() returns -1.
- The fourth argument is a pointer to an FTW structure. The value of base is the offset of the object's filename in the pathname passed as the first argument to fn(). The value of level indicates depth relative to the root of of the walk, where the root level is 0.
The argument depth sets the maximum number of file descriptors that will be used by nftw() while traversing the file tree. At most one file descriptor will be used for each directory level.
The nftw() function continues until the first of the following conditions occurs:
- An invocation of fn() returns a non-zero value, in which case nftw() returns that value.
- The nftw() function detects an error other than [EACCES] (see FTW_DNR and FTW_NS above), in which case nftw() returns -1 and sets errno to indicate the error.
- The tree is exhausted, in which case nftw() returns 0.
The nftw() function will fail if:
- [EACCES]
- Search permission is denied for any component of path or read permission is denied for path, or fn() returns -1 and does not reset errno.
- [ENAMETOOLONG]
- The length of the path string exceeds {PATH_MAX}, or a pathname component is longer than {NAME_MAX}.
- [ENOENT]
- A component of path does not name an existing file or path is an empty string.
- [ENOTDIR]
- A component of path is not a directory.
The nftw() function may fail if:
- [ELOOP]
- Too many symbolic links were encountered in resolving path.
- [EMFILE]
- {OPEN_MAX} file descriptors are currently open in the calling process.
- [ENAMETOOLONG]
- Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.
- [ENFILE]
- Too many files are currently open in the system.
In addition, errno may be set if the function pointed by fn() causes errno to be set.
None.
None.
None.
lstat(), opendir(), readdir(), stat(), <ftw.h>.