The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
A newer edition of this document exists here

NAME

nftw - walk a file tree

SYNOPSIS

[XSI] [Option Start] #include <ftw.h>

int nftw(const char *
path, int (*fn)(const char *,
       const struct stat *, int, struct FTW *), int
fd_limit, int flags);
[Option End]

DESCRIPTION

The nftw() function shall recursively descend 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() shall change the current working directory to each directory as it reports files in that directory. If clear, nftw() shall not change the current working directory.
FTW_DEPTH
If set, nftw() shall report all files in a directory before reporting the directory itself. If clear, nftw() shall report any directory before reporting the files in that directory.
FTW_MOUNT
If set, nftw() shall only report files in the same file system as path. If clear, nftw() shall report all files encountered during the walk.
FTW_PHYS
If set, nftw() shall perform a physical walk and shall not follow symbolic links.

If FTW_PHYS is clear and FTW_DEPTH is set, nftw() 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, nftw() shall follow links instead of reporting them, but shall not report the contents of any directory that would be a descendant of itself.

At each file it encounters, nftw() shall call the user-supplied function fn with four arguments:

The results are unspecified if the application-supplied fn function does not preserve the current working directory.

The argument fd_limit sets the maximum number of file descriptors that shall be used by nftw() while traversing the file tree. At most one file descriptor shall be used for each directory level.

The nftw() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

RETURN VALUE

The nftw() function shall continue until the first of the following conditions occurs:

ERRORS

The nftw() function shall 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.
[ELOOP]
A loop exists in symbolic links encountered during resolution of the path argument.
[ENAMETOOLONG]
The length of the path argument 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.
[EOVERFLOW]
A field in the stat structure cannot be represented correctly in the current programming environment for one or more files found in the file hierarchy.

The nftw() function may fail if:

[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
[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 to by fn causes errno to be set.


The following sections are informative.

EXAMPLES

The following example walks the /tmp directory and its subdirectories, calling the nftw() function for every directory entry, using a maximum of 5 file descriptors.

#include <ftw.h>
...
int nftwfunc(const char *, const struct stat *, int, struct FTW *);

int nftwfunc(const char *filename, const struct stat *statptr, int fileflags, struct FTW *pfwt) { return 0; } ... char *startpath = "/tmp"; int fd_limit = 5; int flags = FTW_CHDIR | FTW_DEPTH | FTW_MOUNT; int ret;
ret = nftw(startpath, nftwfunc, fd_limit, flags);

APPLICATION USAGE

None.

RATIONALE

None.

FUTURE DIRECTIONS

None.

SEE ALSO

lstat(), opendir(), readdir(), stat(), the Base Definitions volume of IEEE Std 1003.1-2001, <ftw.h>

CHANGE HISTORY

First released in Issue 4, Version 2.

Issue 5

Moved from X/OPEN UNIX extension to BASE.

In the DESCRIPTION, the definition of the depth argument is clarified.

Issue 6

The Open Group Base Resolution bwg97-003 is applied.

The ERRORS section is updated as follows:

Text is added to the DESCRIPTION to say that the nftw() function need not be reentrant and that the results are unspecified if the application-supplied fn function does not preserve the current working directory.

IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/64 is applied, changing the argument depth to fd_limit throughout and changing ``to a maximum of 5 levels deep'' to ``using a maximum of 5 file descriptors'' in the EXAMPLES section.

End of informative text.

UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
[ Main Index | XBD | XCU | XSH | XRAT ]