Syntax | #include <ftw.h> int ftw(const char *path, int ( *fn) (const char *, const struct stat *ptr, int flag), int ndirs); int ftw64(const char *path, int (*fn) (const char *, const struct stat64 *ptr, int flag), int ndirs); CRTE111A30 int ftwx(const char *path, int ( *fn) (const char *, const struct statx *ptr, int flag), int ndirs); int ftw64x(const char *path, int (*fn) (const char *, const struct stat64x *ptr, int flag), int ndirs); (End) |
Description | ftw() 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 character string containing the name of the object, a pointer to a stat structure (see also sys/stat.h ) containing information about the object (obtained by calling the function stat() ), and an integer. The possible values of the integer are defined in the ftw.h header. These are:
|
FTW_F
FTW_D
FTW_DNR
FTW_NS
| for a file for a directory for a directory that cannot be read for an object on which stat() could not successfully be executed |
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. ftw() visits a directory before visiting any of its descendants.
The tree traversal continues until the tree is exhausted, an invocation of fn returns a nonzero value, or some error is detected within ftw() . ndirs specifies the maximum number of directory streams and/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 fn function of the user. There is no difference in functionality between ftw() and ftw64() except that ftw64() calls stat64() instead of stat() and passes a pointer to a stat64 structure to fn. The functions with the suffix x behave like the functions of the same name without a suffix, except that they reference the corresponding stat() function and stat structure also with suffix x and therefore will work correctly after 01/19/2038 03:14:07 UTC. |
Return val. | 0 | if successful, i.e. when the file tree is exhausted. ftw() returns the result of the function pointed to by fn. |
| -1 | if an error occurs; errno is set to indicate the error. If the function pointed to by fn returns a non-zero 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 (see above). If the function pointed to by fn detects a system error, errno can be set to that error value. |
Errors | ftw() , ftw64() , ftwx() and ftw64x() will fail if:
|
| EACCES
| Search permission is denied for any component of path or read permission is denied for path. |
| Extension |
| EBADF
| An attempt was made to access a BS2000 file. (End) |
| ENAMETOOLONG
|
|
| The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX} . |
| ENOENT
| path points to the name of a file that does not exist or to an empty string. |
| ENOTDIR
| A component of path is not a directory. |
| errno can also be set if stat() or the function pointed to by fn() sets errno .
|
Notes | Since ftw() is recursive, it is possible for it to terminate with a memory error when applied to very deep file structures. ftw() uses malloc() to allocate dynamic storage during its operation. If ftw() is forcibly terminated, such as by longjmp() or siglongjmp() being executed by the function pointed to by fn or a signal-handling routine, ftw() will not have a chance to free that storage, so it will remain permanently allocated. A safe way to handle interrupts is to store the fact that an interrupt has occurred, and arrange to have the function pointed to by fn return a non-zero value at its next invocation.
ftw() is executed only for POSIX files.
|
See also | longjmp() , malloc() , siglongjmp() , stat() , ftw.h .
|