Обойти файловое дерево
#include <ftw.h>int nftw( const char *path,int (*fn)( const char *fname,const struct stat *sbuf,int flags,struct FTW *ftw ),int depth,int flags );
1
. The depth must not be greater than the number of file descriptors currently available for use. The nftw() function is faster if depth is at least as large as the number of levels in the tree.<ftw.h>
header file. libc
The nftw() function recursively descends the directory hierarchy identified by path. For each object in the hierarchy, nftw() calls the user-defined function fn(), passing to it:
NULL
-terminated character string containing the name of the object <nftw.h>
header, are: FTW_DEPTH
flag above. FTW_PHYS
flag above.
The tree traversal continues until the tree is exhausted, an invocation of fn() returns a nonzero value, or some error is detected within nftw() (such as an I/O error). If the tree is exhausted, nftw() returns zero. If fn() returns a nonzero value, nftw() stops its tree traversal and returns whatever value was returned by fn().
When nftw() returns, it closes any file descriptors it opened; it doesn't close any file descriptors that may have been opened by fn().
EACCESS
). Код ошибки записан в errno.nftw() — POSIX 1003.1 XSI; nftw64() — Поддержка больших файлов
Because nftw() is recursive, it might terminate with a memory fault when applied to very deep file structures.
This function uses malloc() to allocate dynamic storage during its operation. If nftw() is forcibly terminated, for example if longjmp() longjmp() is executed by fn() or an interrupt routine, nftw() doesn't have a chance to free that storage, so it remains permanently allocated. A safe way to handle interrupts is to store the fact that an interrupt has occurred, and arrange to have fn() return a nonzero value at its next invocation.
struct stat, struct FTW, ftw(), longjmp(), malloc(), stat()
Предыдущий раздел: Описание API системной библиотеки