nftw(), nftw64()

原创 2012年03月28日 18:01:40

nftw(), nftw64()
Walk a file tree

#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);Arguments:
The path of the directory whose file tree you want to walk.
A pointer to a function that you want to call for each file; see below.
The maximum number of file descriptors that nftw() can use. The nftw() function uses one file descriptor for each level in the tree.
If depth is zero or negative, the effect is the same as if it were 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.

The value of flags is constructed by the bitwise ORing of values from the following list, defined in the <ftw.h> header file.
If set, nftw() changes the current working directory to each directory as it reports files in that directory.
If set, nftw() reports all files in a directory before reporting the directory itself (otherwise the directory is reported before any file it contains).
If set, nftw() only reports files on the same filesystem as path.
If set, nftw() performs a physical walk and doesn't follow any symbolic link.

Use the -l c option to qcc to link against this library. This library is usually included automatically.

These functions are in libc.a, but not in (in order to save space).


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:

a pointer to a NULL-terminated character string containing the name of the object
a pointer to a stat structure (see stat()) containing information about the object
an integer. Possible values of the integer, defined in the <nftw.h> header, are:
The object is a file.
The object is a directory.
The object is a directory that can't be read. Descendents of the directory aren't processed.
The object is a directory, and its contents have been reported. See the FTW_DEPTH flag above.
The stat() failed on the object because the permissions weren't appropriate. The stat buffer passed to fn() is undefined.
The object is a symbolic link. See the FTW_PHYS flag above.
The object is a symbolic link that does not name an existing file.
a pointer to a FTW structure, which contains the following fields:
The offset of the objects filename in the pathname passed as the first argument to fn().
The depth relative to the root of the walk (where the root is level 0).
A flag that can be set to control the behaviour of nftw() within the current directory. If assigned, it may be given the following values:
Skip the remainder of this directory
If the object is FTW_D, then do not enter into this directory.
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().

An error (other than EACCESS) occurred (errno is set).
nftw() is POSIX 1003.1 XSI; nftw64() is Large-file support

Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

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() 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.

See also:
ftw(), longjmp(), malloc(), stat()



Linux C函数之文件及目录函数(二)

转载: link: 建立文件硬连接 头文件: unistd.h 函数定义: int...
  • wangwenxc
  • wangwenxc
  • 2013年12月18日 13:40
  • 737

更新了Xcode, 报错:'system' is unavailable: not available on iOS

:'system' is unavailable: not available on iOS
  • wangwenfei1990
  • wangwenfei1990
  • 2017年09月28日 11:56
  • 2471

nftw(), nftw64()

nftw(), nftw64() Walk a file tree Synopsis: #include int nftw( const char *path,          i...
  • wang6077160
  • wang6077160
  • 2012年03月28日 18:01
  • 9086

unavailable function 'system': not available on iOS

升级到xcode9后,会出现这个编译错误 解决方法: 1、用nftw替换system
  • holdsky
  • holdsky
  • 2017年09月27日 10:12
  • 2026

linux遍历文件夹(目录树)nftw #define _XOPEN_SOURCE 500 #include #include #inc...
  • 2018年01月04日 00:05
  • 84

linux下遍历目录树方法总结(下) 2、使用ftw调用遍历目录 2.1ftw函数族     使用readdir函数等...
  • bytxl
  • bytxl
  • 2013年04月23日 15:36
  • 732


  • TQW4321
  • TQW4321
  • 2016年02月29日 09:02
  • 403

nftw遍历目录树 nftw遍历目录树 表头文件:#include   ...
  • chenyijun
  • chenyijun
  • 2015年03月02日 09:37
  • 510

ftw nftw遍历目录

#include        int ftw(const char *dir, int (*fn)(const char *file, const struct stat *sb, int fl...
  • whhit_436
  • whhit_436
  • 2013年01月04日 16:38
  • 2355

遍历目录 nftw and ftw

·       ftw() 相关函数:opendir 表头文件:#include 定义函数:int  ftw(const char *dir, int (*fn) (const *file, ...
  • wang6077160
  • wang6077160
  • 2012年07月31日 14:41
  • 464
您举报文章:nftw(), nftw64()