362 Chapter 18
The program in Listing 18-3 displays an indented hierarchy of the filenames in a
directory tree, one file per line, as well as the file type and i-node number. Com-
mand-line options can be used to specify settings for the flags argument used to call
nftw(). The following shell session shows examples of what we see when we run this
program. We first create a new empty subdirectory, which we populate with vari-
ous types of files:
$ mkdir dir
$ touch dir/a dir/b Create some plain files
$ ln -s a dir/sl and a symbolic link
$ ln -s x dir/dsl and a dangling symbolic link
$ mkdir dir/sub and a subdirectory
$ touch dir/sub/x with a file of its own
$ mkdir dir/sub2 and another subdirectory
$ chmod 0 dir/sub2 that is not readable
We then use our program to invoke nftw() with a flags argument of 0:
$ ./nftw_dir_tree dir
d D 2327983 dir
- F 2327984 a
- F 2327985 b
- F 2327984 sl The symbolic link sl was resolved to a
l SLN 2327987 dsl
d D 2327988 sub - F 2327989 x
d DNR 2327994 sub2
In the above output, we can see that the symbolic link s1 was resolved.
We then use our program to invoke nftw() with a flags argument containing
FTW_PHYS and FTW_DEPTH:
$ ./nftw_dir_tree -p -d dir
- F 2327984 a
- F 2327985 b
l SL 2327986 sl The symbolic link sl was not resolved
l SL 2327987 dsl - F 2327989 x
d DP 2327988 sub
d DNR 2327994 sub2
d DP 2327983 dir
From the above output, we can see that the symbolic link s1 was not resolved.
The nftw() FTW_ACTIONRETVAL flag
Starting with version 2.3.3, glibc permits an additional, nonstandard flag to be spec-
ified in flags. This flag, FTW_ACTIONRETVAL, changes the way that nftw() interprets the
return value from calls to func(). When this flag is specified, func() should return
one of the following values:
FTW_CONTINUE
Continue processing entries in the directory tree, as with the traditional 0
return from func().