The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright © 2001-2024 The IEEE and The Open Group

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 argument flags 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 that have the same device ID (st_dev) as path and shall not descend below directories that have a different device ID than path. If clear, nftw() shall report all files encountered during the walk, unless FTW_XDEV is set.
FTW_PHYS
If set, nftw() shall perform a physical walk and shall not follow symbolic links.
FTW_XDEV
If set, nftw() shall not descend below directories that have a different device ID (st_dev) than path; that is, when a directory with a different device ID is encountered, nftw() shall report the directory itself (unless FTW_MOUNT is set) but shall not report any files below the directory. If clear, nftw() shall report all files encountered during the walk, unless FTW_MOUNT is set.
Note:
If both FTW_MOUNT and FTW_XDEV are set, nftw() obeys both flags but the end result is the same as if FTW_XDEV were clear.

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 FD_CLOEXEC flag shall be set on any file descriptor opened by nftw() (see <fcntl.h> ) not including those opened by the user-supplied fn function. Every file descriptor opened by nftw() not including those opened by the user-supplied fn function shall be closed before nftw() returns.

The nftw() function need not 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 a component of a pathname 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 names an existing file that is neither a directory nor a symbolic link to 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]
All file descriptors available to the process are currently open.
[ENAMETOOLONG]
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}.
[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 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 flags argument when calling nftw().

#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

static int display_info(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) { printf("%-3s %2d %7jd %-40s %d %s\n", (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) ((tflag == FTW_NS) ? -1 : sb->st_size), fpath, ftwbuf->base, fpath + ftwbuf->base); return 0; /* To tell nftw() to continue */ }
int main(int argc, char *argv[]) { int flags = 0;
if (argc > 2 && strchr(argv[2], 'd') != NULL) flags |= FTW_DEPTH; if (argc > 2 && strchr(argv[2], 'p') != NULL) flags |= FTW_PHYS;
if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1) { perror("nftw"); exit(EXIT_FAILURE); }
exit(EXIT_SUCCESS); }

APPLICATION USAGE

The nftw() function may allocate dynamic storage during its operation. If nftw() is forcibly terminated, such as by longjmp() or siglongjmp() being executed by the function pointed to by fn or an interrupt routine, nftw() 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 fn return a non-zero value at its next invocation.

When restricting the walk to files on one file system, it can sometimes be desirable for the crossing points themselves to be reported and sometimes for them not to be reported. (Crossing points are mount points and, if FTW_PHYS is clear, symbolic links to directories on other file systems.) With FTW_XDEV nftw() reports them and with FTW_MOUNT it does not. However, with FTW_MOUNT it also does not report symbolic links to non-directory files on other file systems (if FTW_PHYS is clear). If there is a need for an application to exclude crossing points but include symbolic links to non-directory files on other file systems, this can be achieved by using FTW_XDEV and performing a check such as the following in the function pointed to by fn:

    if (tflag == FTW_D && sb->st_dev != saved_dev)
        return 0;

(where saved_dev is the st_dev value for path).

RATIONALE

Earlier versions of this standard did not make clear that, as well as not reporting them, FTW_MOUNT prevents descent below directories that have a different device ID than path if they are encountered by following a symbolic link (rather than by being a mount point). This meant that if such a directory contained any symbolic links to files with the same device ID as path, nftw() with FTW_PHYS clear was required to report them. However, this was not how nftw() implementations behaved and the standard has been amended to match existing practice.

FUTURE DIRECTIONS

None.

SEE ALSO

fdopendir , fstatat , readdir

XBD <fcntl.h> , <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.

Issue 7

Austin Group Interpretations 1003.1-2001 #143 and #156 are applied.

SD5-XBD-ERN-4 is applied, changing the definition of the [EMFILE] error.

SD5-XBD-ERN-61 is applied.

APPLICATION USAGE is added and the EXAMPLES section is replaced with a new example.

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0409 [403], XSH/TC1-2008/0410 [324], and XSH/TC1-2008/0411 [403] are applied.

Issue 8

Austin Group Defect 368 is applied, adding a requirement for the FD_CLOEXEC flag to be set.

Austin Group Defect 1121 is applied, changing the description of FTW_SLN and the handling of FTW_NS in the EXAMPLES section.

Austin Group Defect 1133 is applied, adding FTW_XDEV.

Austin Group Defect 1210 is applied, changing the description of FTW_MOUNT and the RATIONALE section.

Austin Group Defect 1330 is applied, removing obsolescent interfaces.

End of informative text.

 

return to top of page

UNIX® is a registered Trademark of The Open Group.
POSIX™ is a Trademark of The IEEE.
Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]