Node:Working with Directory Trees, Next:Hard Links, Previous:Accessing Directories, Up:File System Interface
The functions described so far for handling the files in a directory
have allowed you to either retrieve the information bit by bit, or to
process all the files as a group (see scandir
). Sometimes it is
useful to process whole hierarchies of directories and their contained
files. The X/Open specification defines two functions to do this. The
simpler form is derived from an early definition in System V systems
and therefore this function is available on SVID-derived systems. The
prototypes and required definitions can be found in the ftw.h
header.
There are four functions in this family: ftw
, nftw
and
their 64-bit counterparts ftw64
and nftw64
. These
functions take as one of their arguments a pointer to a callback
function of the appropriate type.
__ftw_func_t | Data Type |
int (*) (const char *, const struct stat *, int) The type of callback functions given to the The last parameter is a flag giving more information about the current file. It can have the following values:
If the sources are compiled with |
For the LFS interface and for use in the function ftw64
, the
header ftw.h
defines another function type.
__ftw64_func_t | Data Type |
int (*) (const char *, const struct stat64 *, int) This type is used just like |
__nftw_func_t | Data Type |
int (*) (const char *, const struct stat *, int, struct FTW *) The first three arguments are the same as for the
The last parameter of the callback function is a pointer to a structure with some extra information as described below. If the sources are compiled with |
For the LFS interface there is also a variant of this data type
available which has to be used with the nftw64
function.
__nftw64_func_t | Data Type |
int (*) (const char *, const struct stat64 *, int, struct FTW *) This type is used just like |
struct FTW | Data Type |
The information contained in this structure helps in interpreting the
name parameter and gives some information about the current state of the
traversal of the directory hierarchy.
|
int ftw (const char *filename, __ftw_func_t func, int descriptors) | Function |
The ftw function calls the callback function given in the
parameter func for every item which is found in the directory
specified by filename and all directories below. The function
follows symbolic links if necessary but does not process an item twice.
If filename is not a directory then it itself is the only object
returned to the callback function.
The file name passed to the callback function is constructed by taking
the filename parameter and appending the names of all passed
directories and then the local file name. So the callback function can
use this parameter to access the file. The callback function is expected to return 0 to indicate that no
error occurred and that processing should continue. If an error
occurred in the callback function or it wants The descriptors parameter to The return value of the When the sources are compiled with |
int ftw64 (const char *filename, __ftw64_func_t func, int descriptors) | Function |
This function is similar to ftw but it can work on filesystems
with large files. File information is reported using a variable of type
struct stat64 which is passed by reference to the callback
function.
When the sources are compiled with |
int nftw (const char *filename, __nftw_func_t func, int descriptors, int flag) | Function |
The nftw function works like the ftw functions. They call
the callback function func for all items found in the directory
filename and below. At most descriptors file descriptors
are consumed during the nftw call.
One difference is that the callback function is of a different type. It
is of type A second difference is that
The return value is computed in the same way as for When the sources are compiled with |
int nftw64 (const char *filename, __nftw64_func_t func, int descriptors, int flag) | Function |
This function is similar to nftw but it can work on filesystems
with large files. File information is reported using a variable of type
struct stat64 which is passed by reference to the callback
function.
When the sources are compiled with |