Computer Science
FTW(3) Linux Programmer's Manual FTW(3)
NAME
ftw - file tree walk
SYNOPSIS
#include <ftw.h>
int ftw(const char *directory, int (*funcptr )(const char
*file, struct stat *sb, int flag), int depth);
DESCRIPTION
ftw() walks through the directory tree starting from the
indicated directory. For each found entry in the tree, it
calls funcptr with the full pathname of the entry relative
to directory, a pointer to a the second argument is a
pointer to the stat(2) structure for the entry and an int,
which value will be one of the following:
FTW_F Item is a normal file
FTW_D Item is a directory
FTW_NS The stat failed on the item
FTW_DNR Item is a directory which can't be read
Warning: Anything other than directories, like symbolic
links, gets the FTW_F tag.
ftw() recursively calls itself for traversing found direc-
tories. To avoid using up all a program's file descrip-
tors, the depth specifies the number of simultaneous open
directories. When the depth is exceeded, ftw() will
become slower because directories have to be closed and
reopened.
To stop the tree walk, funcptr returns a non-zero value;
this value will become the return value of ftw(). Other-
wise, ftw() will continue until it has traversed the
entire tree, in which case it will return zero, or until
it hits an error such as a malloc(3) failure, in which
case it will return -1.
Because ftw() uses dynamic data structures, the only safe
way to exit out of a tree walk is to return a non-zero
value. To handle interrupts, for example, mark that the
interrupt occurred and return a non-zero value--don't use
longjmp(3) unless the program is going to terminate.
CONFORMING TO
AES, SVID2, SVID3, XPG2, XPG3, XPG4
SEE ALSO
stat(2)
Linux July 18, 1993 1
Back to the index