ftw() — Traverse a file tree

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <ftw.h>

int ftw(const char *path,
        int (*fn)(const char *, const struct stat *, int),
        int ndirs);

General description

The ftw() function recursively descends the directory hierarchy rooted in path. For each object in the hierarchy, ftw() calls the function pointed to by fn, passing it a pointer to a NULL-terminated string containing the name of the object, a pointer to a stat structure containing information about the object, and an integer. Possible values of the integer, defined in the <ftw.h> header, are:
FTW_D
for a directory
FTW_DNR
for a directory that cannot be read
FTW_F
for a file
FTW_SL
for a symbolic link
FTW_NS
for an object other than a symbolic link on which stat() could not be successfully executed. If the object is a symbolic link, and stat() failed, it is unspecified whether ftw() passes FTW_SL or FTW_NS to the user-supplied function.

If the integer is FTW_DNR, descendants of that directory will not be processed. If the integer is FTW_NS, the stat structure will contain undefined values. An example of an object that would cause FTW_NS to be passed to the function pointed to by fn would be a file in a directory with read but without execute (search) permission.

The ftw() function visits a directory before visiting any of its descendants.

The ftw() function uses at most one file descriptor for each level in the tree.

The argument ndirs should be in the range of 1 to OPEN_MAX.

The tree traversal continues until the tree is exhausted, an invocation of fn returns a nonzero value, or some other error, other than [EACCES], is detected within ftw().

The ndirs argument specifies the maximum number of directory streams or file descriptors or both available for use by ftw() while traversing the tree. When ftw() returns it closes any directory streams and file descriptors it uses not counting any opened by the application-supplied fn function.

Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.

Returned value

If the tree is exhausted, ftw() returns 0. If the function pointed to by fn returns a nonzero value, ftw() stops its tree traversal and returns whatever value was returned by the function pointed to by fn().

If ftw() detects an error, it returns -1 and sets errno to one of the following values. All other errnos returned by ftw() are unchanged.
Error Code
Description
EACCES
Search permission is denied for any component of path or read permission is denied for path.
EINVAL
The value of the ndirs argument is not valid.
ELOOP
Too many symbolic links were encountered.
ENAMETOOLONG
One of the following error conditions exists:
  • Path name resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX.
  • The length of path exceeds PATH_MAX, or a path name component is longer than PATH_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.

Related information