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


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

 

遍历目录 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...

win7 X64或x86安装mySQL,服务无法启动问题的解决

首先我安装的是解压版的mysql-5.7.16-win64,具体的下载网址是https://downloads.mysql.com/archives/community/找到对应的32位或64位下载即...

IOS成长之路-DES+Base64 加密、解密

DES是一种分组数据加密技术(先将数据分成固定长度的小数据块,之后进行加密),速度较快,适用于大量数据加密 3DES是一种基于DES的加密算法,使用3个不同密匙对同一个分组数据块进行3次加密,如此以...

iOS学习笔记64-Apple Pay编程指南

本文由CocoaChina译者xiaoying、tr培子翻译自苹果官方文档:Apple Pay Programming Guide 目录 关于Apple Pay 配置支付环境 创建支付请求 授权支付...

Python 和 VBScript 对文件编码-解码的方式 (Hex, Base64) 的疑问

本人目前的项目需要在Windows上用VBScript将一文件转换为二进制码存入数据库,再在Linux上用Python从数据库中读取并解码成图片。反复调试,发现微软和Python的转码标准不统一,导致...

编译x86_64老是不过,iphone模拟器debug不了解决方法

其实没什么好解决的, 忍了2年了 所以今天重新把所有代码,所有编译过程,重新看一遍 编译curl很多遍了,老是提示architecture x86_64 一开始以为是编译问题,还想用gcc重新...
  • avi9111
  • avi9111
  • 2016年10月16日 17:36
  • 661

Qt:Windows10 64bit VMware虚拟机环境,基于Qt 5.6 MinGW,静态编译版本和Android环境

很多人觉得编译静态环境麻烦,配置安卓环境麻烦,所以我干脆做了一个虚拟机,系统是Windows10 64bit 专业版,Qt环境是 5.6.0 MinGW版本...

WIN7(64位)系统发现数据源ODBC里面没有配置access数据源连接&System, System32, Syswow64 概述

本机是win7 64位操作系统,做数据库实例时需要创建Access数据库的ODBC数据源,但是打开ODBC数据源后发现添加里面没有配置access的连接,如下图: 还会弹出以下问题框:  ...

为什么802.3MAC帧最小是64字节?

 看下图: | Pre | SFD | DA | SA | Length Type | Data unit + pad | FCS | |__7__|__1__|__6_|__6_|...
  • qijitao
  • qijitao
  • 2016年02月22日 10:37
  • 1349
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:nftw(), nftw64()
举报原因:
原因补充:

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