📄 ftw.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>ftw</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_005_383"> </a>NAME</h4><blockquote>ftw - traverse (walk) a file tree</blockquote><h4><a name = "tag_000_005_384"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="ftw.h.html">ftw.h</a>>int ftw(const char *<i>path</i>, int (*<i>fn</i>)(const char *, const struct stat *<i>ptr</i>, int <i>flag</i>), int <i>ndirs</i>);</code></pre></blockquote><h4><a name = "tag_000_005_385"> </a>DESCRIPTION</h4><blockquote>The<i>ftw()</i>function recursively descends the directory hierarchy rooted in<i>path</i>.For each object in the hierarchy,<i>ftw()</i>calls the function pointed to by<i>fn</i>,passing it a pointer to a null-terminated character string containing the nameof the object, a pointer to a<b>stat</b>structure containing information about the object, and an integer.Possible values of the integer, defined in the<i><a href="ftw.h.html"><ftw.h></a></i>header, are:<dl compact><dt>FTW_D<dd>For a directory.<dt>FTW_DNR<dd>For a directory that cannot be read.<dt>FTW_F<dd>For a file.<dt>FTW_SL<dd>For a symbolic link (but see also FTW_NS below).<dt>FTW_NS<dd>For an objectother than a symbolic linkon which<i><a href="stat.html">stat()</a></i>could not successfully be executed.If the object is a symbolic link and<i><a href="stat.html">stat()</a></i>failed, it is unspecified whether<i>ftw()</i>passes FTW_SL or FTW_NS to the user-supplied function.</dl><p>If the integer is FTW_DNR, descendants of that directory will not beprocessed.If the integer is FTW_NS, the<b>stat</b>structure will contain undefined values.An example of an object that would cause FTW_NSto be passed to the function pointed to by<i>fn</i>would be a file in a directory with read butwithout execute (search) permission.<p>The<i>ftw()</i>function visits a directory before visiting any of its descendants.<p>The<i>ftw()</i>function uses at most one file descriptor for each level in the tree.<p>The argument <i>ndirs</i> should be in the range of 1 to {OPEN_MAX}.<p>The tree traversal continues until the tree is exhausted, an invocation of<i>fn</i>returns a non-zero value, or some error, other than [EACCES], is detectedwithin<i>ftw()</i>.<p>The<i>ndirs</i>argument specifies the maximum number of directory streams orfile descriptors or both available for use by<i>ftw()</i>while traversing the tree. When<i>ftw()</i>returns it closes any directory streams and file descriptors ituses not counting any opened by the application-supplied<i>fn()</i>function.</blockquote><h4><a name = "tag_000_005_386"> </a>RETURN VALUE</h4><blockquote>If the tree is exhausted,<i>ftw()</i>returns 0. If the function pointed to by<i>fn</i>returns a non-zero value,<i>ftw()</i>stops its tree traversal and returns whatevervalue was returned by the function pointed to by<i>fn()</i>.If<i>ftw()</i>detects an error, it returns -1 and sets<i>errno</i>to indicate the error.<p>If<i>ftw()</i>encounters an error other than [EACCES] (see FTW_DNR and FTW_NS above), itreturns -1 and <i>errno</i> is set to indicate the error. The externalvariable <i>errno</i> may contain any error value that is possible when adirectory is opened or when one of the<i>stat</i>functions is executed on a directory or file.<br></blockquote><h4><a name = "tag_000_005_387"> </a>ERRORS</h4><blockquote>The<i>ftw()</i>function will fail if:<dl compact><dt>[EACCES]<dd>Search permission is denied for any component of<i>path</i>or read permission is denied for<i>path.</i><dt>[ELOOP]<dd>Too many symbolic links were encountered.<dt>[ENAMETOOLONG]<dd>The length of the<i>path</i>exceeds {PATH_MAX}, or a pathname component is longer than {NAME_MAX}.<dt>[ENOENT]<dd>A component of <i>path</i> does not name an existing fileor <i>path</i> is an empty string.<dt>[ENOTDIR]<dd>A component of<i>path</i>is not a directory.</dl><p>The<i>ftw()</i>function may fail if:<dl compact><dt>[EINVAL]<dd>The value of the<i>ndirs</i>argument is invalid.<dt>[ENAMETOOLONG]<dd>Pathname resolution of a symbolic link produced an intermediate result whoselength exceeds {PATH_MAX}.</dl><p>In addition, if the function pointed to by<i>fn</i>encounters system errors,<i>errno</i>may be set accordingly.<br></blockquote><h4><a name = "tag_000_005_388"> </a>EXAMPLES</h4><blockquote>None.</blockquote><h4><a name = "tag_000_005_389"> </a>APPLICATION USAGE</h4><blockquote>The<i>ftw()</i>may allocate dynamic storage during its operation. If<i>ftw()</i>is forcibly terminated, such as by<i><a href="longjmp.html">longjmp()</a></i>or<i><a href="siglongjmp.html">siglongjmp()</a></i>being executed by the function pointed to by<i>fn</i>or an interrupt routine,<i>ftw()</i>will not have a chance to free that storage,so it will remain permanently allocated.A safe way to handle interrupts is to storethe fact that an interrupt has occurred,and arrange to have the function pointed to by<i>fn</i>return a non-zero value at its next invocation.</blockquote><h4><a name = "tag_000_005_390"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_005_391"> </a>SEE ALSO</h4><blockquote><i><a href="longjmp.html">longjmp()</a></i>,<i><a href="lstat.html">lstat()</a></i>,<i><a href="malloc.html">malloc()</a></i>,<i><a href="nftw.html">nftw()</a></i>,<i><a href="opendir.html">opendir()</a></i>,<i><a href="siglongjmp.html">siglongjmp()</a></i>,<i><a href="stat.html">stat()</a></i>,<i><a href="ftw.h.html"><ftw.h></a></i>,<i><a href="sysstat.h.html"><sys/stat.h></a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -