⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readdir.html

📁 posix标准英文,html格式
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>readdir</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="readdir"></a> <a name="tag_03_595"></a><!-- readdir --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright &copy; 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_595_01"></a>NAME</h4><blockquote>readdir, readdir_r - read a directory</blockquote><h4><a name="tag_03_595_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/dirent.h.html">dirent.h</a>&gt;<br><br> struct dirent *readdir(DIR *</tt><i>dirp</i><tt>);<br></tt></code></p><div class="box"><code><tt><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> int readdir_r(DIR *restrict</tt> <i>dirp</i><tt>, struct dirent *restrict</tt> <i>entry</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct dirent **restrict</tt> <i>result</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_595_03"></a>DESCRIPTION</h4><blockquote><p>The type <b>DIR</b>, which is defined in the <a href="../basedefs/dirent.h.html"><i>&lt;dirent.h&gt;</i></a> header, representsa <i>directory stream</i>, which is an ordered sequence of all the directory entries in a particular directory. Directory entriesrepresent files; files may be removed from a directory or added to a directory asynchronously to the operation of<i>readdir</i>().</p><p>The <i>readdir</i>() function shall return a pointer to a structure representing the directory entry at the current position inthe directory stream specified by the argument <i>dirp</i>, and position the directory stream at the next entry. It shall return anull pointer upon reaching the end of the directory stream. The structure <b>dirent</b> defined in the <a href="../basedefs/dirent.h.html"><i>&lt;dirent.h&gt;</i></a> header describes a directory entry.</p><p>The <i>readdir</i>() function shall not return directory entries containing empty names. If entries for dot or dot-dot exist,one entry shall be returned for dot and one entry shall be returned for dot-dot; otherwise, they shall not be returned.</p><p>The pointer returned by <i>readdir</i>() points to data which may be overwritten by another call to <i>readdir</i>() on the samedirectory stream. This data is not overwritten by another call to <i>readdir</i>() on a different directory stream.</p><p>If a file is removed from or added to the directory after the most recent call to <a href="../functions/opendir.html"><i>opendir</i>()</a> or <a href="../functions/rewinddir.html"><i>rewinddir</i>()</a>, whether asubsequent call to <i>readdir</i>() returns an entry for that file is unspecified.</p><p>The <i>readdir</i>() function may buffer several directory entries per actual read operation; <i>readdir</i>() shall mark forupdate the <i>st_atime</i> field of the directory each time the directory is actually read.</p><p>After a call to <a href="../functions/fork.html"><i>fork</i>()</a>, either the parent or child (but not both) may continueprocessing the directory stream using <i>readdir</i>(), <a href="../functions/rewinddir.html"><i>rewinddir</i>()</a>, <sup>[<ahref="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;or <ahref="../functions/seekdir.html"><i>seekdir</i>()</a>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"> If both theparent and child processes use these functions, the result is undefined.</p><p>If the entry names a symbolic link, the value of the <i>d_ino</i> member is unspecified.</p><p>The <i>readdir</i>() function need not be reentrant. A function that is not required to be reentrant is not required to bethread-safe.</p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>readdir_r</i>() function shall initialize the <b>dirent</b> structure referenced by <i>entry</i> to represent the directoryentry at the current position in the directory stream referred to by <i>dirp</i>, store a pointer to this structure at the locationreferenced by <i>result</i>, and position the directory stream at the next entry.</p><p>The storage pointed to by <i>entry</i> shall be large enough for a <b>dirent</b> with an array of <b>char</b> <i>d_name</i>members containing at least {NAME_MAX}+1 elements.</p><p>Upon successful return, the pointer returned at *<i>result</i> shall have the same value as the argument <i>entry</i>. Uponreaching the end of the directory stream, this pointer shall have the value NULL.</p><p>The <i>readdir_r</i>() function shall not return directory entries containing empty names.</p><p>If a file is removed from or added to the directory after the most recent call to <a href="../functions/opendir.html"><i>opendir</i>()</a> or <a href="../functions/rewinddir.html"><i>rewinddir</i>()</a>, whether asubsequent call to <i>readdir_r</i>() returns an entry for that file is unspecified.</p><p>The <i>readdir_r</i>() function may buffer several directory entries per actual read operation; the <i>readdir_r</i>() functionshall mark for update the <i>st_atime</i> field of the directory each time the directory is actually read. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>Applications wishing to check for error situations should set <i>errno</i> to 0 before calling <i>readdir</i>(). If <i>errno</i>is set to non-zero on return, an error occurred.</p></blockquote><h4><a name="tag_03_595_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>readdir</i>() shall return a pointer to an object of type <b>struct dirent</b>. When an error isencountered, a null pointer shall be returned and <i>errno</i> shall be set to indicate the error. When the end of the directory isencountered, a null pointer shall be returned and <i>errno</i> is not changed.</p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If successful, the <i>readdir_r</i>() function shall return zero; otherwise, an error number shall be returned to indicate theerror. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_595_05"></a>ERRORS</h4><blockquote><p>The <i>readdir</i>() function shall fail if:</p><dl compact><dt>[EOVERFLOW]</dt><dd>One of the values in the structure to be returned cannot be represented correctly.</dd></dl><p>The <i>readdir</i>() function may fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>dirp</i> argument does not refer to an open directory stream.</dd><dt>[ENOENT]</dt><dd>The current position of the directory stream is invalid.</dd></dl><p>The <i>readdir_r</i>() function may fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>dirp</i> argument does not refer to an open directory stream.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_595_06"></a>EXAMPLES</h4><blockquote><p>The following sample program searches the current directory for each of the arguments supplied on the command line.</p><pre><tt>#include &lt;dirent.h&gt;#include &lt;errno.h&gt;#include &lt;stdio.h&gt;#include &lt;string.h&gt;<br>static void lookup(const char *arg){    DIR *dirp;    struct dirent *dp;<br>    if ((dirp = opendir(".")) == NULL) {        perror("couldn't open '.'");        return;    }<br>    do {        errno = 0;        if ((dp = readdir(dirp)) != NULL) {            if (strcmp(dp-&gt;d_name, arg) != 0)                continue;<br>            (void) printf("found %s\n", arg);            (void) closedir(dirp);                return;<br>        }    } while (dp != NULL);<br>    if (errno != 0)        perror("error reading directory");    else        (void) printf("failed to find %s\n", arg);    (void) closedir(dirp);    return;}<br>int main(int argc, char *argv[]){    int i;    for (i = 1; i &lt; argc; i++)        lookup(argv[i]);    return (0);}</tt></pre></blockquote><h4><a name="tag_03_595_07"></a>APPLICATION USAGE</h4><blockquote><p>The <i>readdir</i>() function should be used in conjunction with <a href="../functions/opendir.html"><i>opendir</i>()</a>, <ahref="../functions/closedir.html"><i>closedir</i>()</a>, and <a href="../functions/rewinddir.html"><i>rewinddir</i>()</a> toexamine the contents of the directory.</p><p>The <i>readdir_r</i>() function is thread-safe and shall return values in a user-supplied buffer instead of possibly using astatic data area that may be overwritten by each call.</p></blockquote><h4><a name="tag_03_595_08"></a>RATIONALE</h4><blockquote><p>The returned value of <i>readdir</i>() merely <i>represents</i> a directory entry. No equivalence should be inferred.</p><p>Historical implementations of <i>readdir</i>() obtain multiple directory entries on a single read operation, which permitssubsequent <i>readdir</i>() operations to operate from the buffered information. Any wording that required each successful<i>readdir</i>() operation to mark the directory <i>st_atime</i> field for update would disallow such historicalperformance-oriented implementations.</p><p>Since <i>readdir</i>() returns NULL when it detects an error and when the end of the directory is encountered, an applicationthat needs to tell the difference must set <i>errno</i> to zero before the call and check it if NULL is returned. Since thefunction must not change <i>errno</i> in the second case and must set it to a non-zero value in the first case, a zero <i>errno</i>after a call returning NULL indicates end-of-directory; otherwise, an error.</p><p>Routines to deal with this problem more directly were proposed:</p><pre><tt>int derror (</tt><i>dirp</i><tt>)DIR *</tt><i>dirp</i><tt>;<br>void clearderr (</tt><i>dirp</i><tt>)DIR *</tt><i>dirp</i><tt>;</tt></pre><p>The first would indicate whether an error had occurred, and the second would clear the error indication. The simpler methodinvolving <i>errno</i> was adopted instead by requiring that <i>readdir</i>() not change <i>errno</i> when end-of-directory isencountered.</p><p>An error or signal indicating that a directory has changed while open was considered but rejected.</p><p>The thread-safe version of the directory reading function returns values in a user-supplied buffer instead of possibly using astatic data area that may be overwritten by each call. Either the {NAME_MAX} compile-time constant or the corresponding <a href="../functions/pathconf.html"><i>pathconf</i>()</a> option can be used to determine the maximum sizes of returned pathnames.</p></blockquote><h4><a name="tag_03_595_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_595_10"></a>SEE ALSO</h4><blockquote><p><a href="closedir.html"><i>closedir</i>()</a>, <a href="lstat.html"><i>lstat</i>()</a>, <a href="opendir.html"><i>opendir</i>()</a>, <a href="rewinddir.html"><i>rewinddir</i>()</a>, <a href="symlink.html"><i>symlink</i>()</a>, the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/dirent.h.html"><i>&lt;dirent.h&gt;</i></a>,<a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a></p></blockquote><h4><a name="tag_03_595_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><h4><a name="tag_03_595_12"></a>Issue 5</h4><blockquote><p>Large File Summit extensions are added.</p><p>The <i>readdir_r</i>() function is included for alignment with the POSIX Threads Extension.</p><p>A note indicating that the <i>readdir</i>() function need not be reentrant is added to the DESCRIPTION.</p></blockquote><h4><a name="tag_03_595_13"></a>Issue 6</h4><blockquote><p>The <i>readdir_r</i>() function is marked as part of the Thread-Safe Functions option.</p><p>The Open Group Corrigendum U026/7 is applied, correcting the prototype for <i>readdir_r</i>().</p><p>The Open Group Corrigendum U026/8 is applied, clarifying the wording of the successful return for the <i>readdir_r</i>()function.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The requirement to include <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> has been removed. Although <ahref="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIXspecifications, it was not required for UNIX applications.</p></li><li><p>A statement is added to the DESCRIPTION indicating the disposition of certain fields in <b>struct dirent</b> when an entryrefers to a symbolic link.</p></li><li><p>The [EOVERFLOW] mandatory error condition is added. This change is to support large files.</p></li><li><p>The [ENOENT] optional error condition is added.</p></li></ul><p>The APPLICATION USAGE section is updated to include a note on the thread-safe function and its avoidance of possibly using astatic data area.</p><p>The <b>restrict</b> keyword is added to the <i>readdir_r</i>() prototype for alignment with the ISO/IEC&nbsp;9899:1999standard.</p><p>IEEE&nbsp;Std 1003.1-2001/Cor&nbsp;1-2002, item XSH/TC1/D6/50 is applied, replacing the EXAMPLES section with a new example.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -