nftw(), nftw64()

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

nftw(), nftw64()
Walk a file tree


Synopsis:
#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:
path
The path of the directory whose file tree you want to walk.
fn
A pointer to a function that you want to call for each file; see below.
depth
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.

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

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 libc.so (in order to save space).

--------------------------------------------------------------------------------

Description:
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:
FTW_F
The object is a file.
FTW_D
The object is a directory.
FTW_DNR
The object is a directory that can't be read. Descendents of the directory aren't processed.
FTW_DP
The object is a directory, and its contents have been reported. See the FTW_DEPTH flag above.
FTW_NS
The stat() failed on the object because the permissions weren't appropriate. The stat buffer passed to fn() is undefined.
FTW_SL
The object is a symbolic link. See the FTW_PHYS flag above.
FTW_SLN
The object is a symbolic link that does not name an existing file.
a pointer to a FTW structure, which contains the following fields:
base
The offset of the objects filename in the pathname passed as the first argument to fn().
level
The depth relative to the root of the walk (where the root is level 0).
quit
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:
FTW_SKR
Skip the remainder of this directory
FTW_SKD
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().

Returns:
0
Success.
-1
An error (other than EACCESS) occurred (errno is set).
Classification:
nftw() is POSIX 1003.1 XSI; nftw64() is Large-file support

Safety: 
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

Caveats:
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下遍历目录树方法总结

前几天需要实现对整个目录树的遍历,查阅了相关的一些资料。开始找到的原始的方法是使用readdir()与lstat()函数实现递归遍历,后来发现linux对于目录遍历这种最常用的操作已经提供了很完善的接...

遍历目录 nftw and ftw

·       ftw() 相关函数:opendir 表头文件:#include 定义函数:int  ftw(const char *dir, int (*fn) (const *file, ...

ftw nftw遍历目录

#include        int ftw(const char *dir, int (*fn)(const char *file, const struct stat *sb, int fl...

image转换为base64编码

在前端优化方案中,减少HTTP请求,最常见的方法是css sprite。今天来说另一种方式,就是将小图片转换为base64码,直接在img的src中引用,这种方式不向服务器端发送HTTP请求。 但是...

在x64平台上调试依赖于x86的WCF服务

 根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架。它使得开发者能够建立一个跨平台的安全、可信赖...

【教程】Windows 7 SP1 x64 旗舰版 微软官方安装U盘的制作

[本主题由中山草泥羊于2013-08-20 23:14:33设为精华1,原因:不错~] 最后由 风中枯萎 于 2013-08-20 23:43:28 修改   ...

cocos2dx 2.2.3在Windows 7 64bit上搭建开发环境

终于弄完了cocos2dx 2.2.3在windows 7 64bit上的环境搭建,you

Android之配置方法数超过64K的应用

Android中配置方法数超过64K的或者APP报64K的错误可以看看、

win7安装vs2015出错安装包损坏或丢失 windows6.1-kb2999226-x64.msu

win7安装vs2015出错安装包损坏或丢失 windows6.1-kb2999226-x64.msu

Go语言笔记:base64/sha1的使用

1.base64编码 package main //这个语法引入了 encoding/base64 包并使用名称 b64代替默认的 base64。这样可以节省点空间。 import b64 "...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)