Directories and Links 359
The flags argument to nftw() is created by ORing (|) zero or more of the follow-
ing constants, which modify the operation of the function:
FTW_CHDIR
Do a chdir() into each directory before processing its contents. This is use-
ful if func is designed to do some work in the directory in which the file
specified by its pathname argument resides.
FTW_DEPTH
Do a postorder traversal of the directory tree. This means that nftw() calls
func on all of the files (and subdirectories) within a directory before execut-
ing func on the directory itself. (The name of this flag is somewhat mislead-
ing—nftw() always does a depth-first, rather than a breadth-first, traversal of
the directory tree. All that this flag does is convert the traversal from preorder
to postorder.)
FTW_MOUNT
Don’t cross over into another file system. Thus, if one of the subdirectories
of the tree is a mount point, it is not traversed.
FTW_PHYS
By default, nftw() dereferences symbolic links. This flag tells it not to do so.
Instead, a symbolic link is passed to func with a typeflag value of FTW_SL, as
described below.
For each file, nftw() passes four arguments when calling func. The first of these
arguments, pathname, is the pathname of the file. This pathname may be absolute,
if dirpath was specified as an absolute pathname, or relative to the current working
directory of the calling process at the time of the call to ntfw(), if dirpath was
expressed as a relative pathname. The second argument, statbuf, is a pointer to a
stat structure (Section 15.1) containing information about this file. The third argu-
ment, typeflag, provides further information about the file, and has one of the fol-
lowing symbolic values:
FTW_D
This is a directory.
FTW_DNR
This is a directory that can’t be read (and so nftw() doesn’t traverse any of
its descendants).
FTW_DP
We are doing a postorder traversal (FTW_DEPTH) of a directory, and the cur-
rent item is a directory whose files and subdirectories have already been
processed.
FTW_F
This is a file of any type other than a directory or symbolic link.
FTW_NS
Calling stat() on this file failed, probably because of permission restric-
tions. The value in statbuf is undefined.