blob: 9092896ff947ec08ba029188e9086e4049508ab5 [file] [log] [blame]
<html lang="en">
<head>
<title>Reading/Closing Directory - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Accessing-Directories.html#Accessing-Directories" title="Accessing Directories">
<link rel="prev" href="Opening-a-Directory.html#Opening-a-Directory" title="Opening a Directory">
<link rel="next" href="Simple-Directory-Lister.html#Simple-Directory-Lister" title="Simple Directory Lister">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Reading%2fClosing-Directory"></a>
<a name="Reading_002fClosing-Directory"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Simple-Directory-Lister.html#Simple-Directory-Lister">Simple Directory Lister</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Opening-a-Directory.html#Opening-a-Directory">Opening a Directory</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Accessing-Directories.html#Accessing-Directories">Accessing Directories</a>
<hr>
</div>
<h4 class="subsection">14.2.3 Reading and Closing a Directory Stream</h4>
<p><a name="index-dirent_002eh-1416"></a>This section describes how to read directory entries from a directory
stream, and how to close the stream when you are done with it. All the
symbols are declared in the header file <samp><span class="file">dirent.h</span></samp>.
<!-- dirent.h -->
<!-- POSIX.1 -->
<div class="defun">
&mdash; Function: struct dirent * <b>readdir</b> (<var>DIR *dirstream</var>)<var><a name="index-readdir-1417"></a></var><br>
<blockquote><p>This function reads the next entry from the directory. It normally
returns a pointer to a structure containing information about the file.
This structure is statically allocated and can be rewritten by a
subsequent call.
<p><strong>Portability Note:</strong> On some systems <code>readdir</code> may not
return entries for <samp><span class="file">.</span></samp> and <samp><span class="file">..</span></samp>, even though these are always
valid file names in any directory. See <a href="File-Name-Resolution.html#File-Name-Resolution">File Name Resolution</a>.
<p>If there are no more entries in the directory or an error is detected,
<code>readdir</code> returns a null pointer. The following <code>errno</code> error
conditions are defined for this function:
<dl>
<dt><code>EBADF</code><dd>The <var>dirstream</var> argument is not valid.
</dl>
<p><code>readdir</code> is not thread safe. Multiple threads using
<code>readdir</code> on the same <var>dirstream</var> may overwrite the return
value. Use <code>readdir_r</code> when this is critical.
</p></blockquote></div>
<!-- dirent.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>readdir_r</b> (<var>DIR *dirstream, struct dirent *entry, struct dirent **result</var>)<var><a name="index-readdir_005fr-1418"></a></var><br>
<blockquote><p>This function is the reentrant version of <code>readdir</code>. Like
<code>readdir</code> it returns the next entry from the directory. But to
prevent conflicts between simultaneously running threads the result is
not stored in statically allocated memory. Instead the argument
<var>entry</var> points to a place to store the result.
<p>Normally <code>readdir_r</code> returns zero and sets <code>*</code><var>result</var>
to <var>entry</var>. If there are no more entries in the directory or an
error is detected, <code>readdir_r</code> sets <code>*</code><var>result</var> to a
null pointer and returns a nonzero error code, also stored in
<code>errno</code>, as described for <code>readdir</code>.
<p><strong>Portability Note:</strong> On some systems <code>readdir_r</code> may not
return a NUL terminated string for the file name, even when there is no
<code>d_reclen</code> field in <code>struct dirent</code> and the file
name is the maximum allowed size. Modern systems all have the
<code>d_reclen</code> field, and on old systems multi-threading is not
critical. In any case there is no such problem with the <code>readdir</code>
function, so that even on systems without the <code>d_reclen</code> member one
could use multiple threads by using external locking.
<p>It is also important to look at the definition of the <code>struct
dirent</code> type. Simply passing a pointer to an object of this type for
the second parameter of <code>readdir_r</code> might not be enough. Some
systems don't define the <code>d_name</code> element sufficiently long. In
this case the user has to provide additional space. There must be room
for at least <code>NAME_MAX + 1</code> characters in the <code>d_name</code> array.
Code to call <code>readdir_r</code> could look like this:
<pre class="smallexample"> union
{
struct dirent d;
char b[offsetof (struct dirent, d_name) + NAME_MAX + 1];
} u;
if (readdir_r (dir, &amp;u.d, &amp;res) == 0)
...
</pre>
</blockquote></div>
<p>To support large filesystems on 32-bit machines there are LFS variants
of the last two functions.
<!-- dirent.h -->
<!-- LFS -->
<div class="defun">
&mdash; Function: struct dirent64 * <b>readdir64</b> (<var>DIR *dirstream</var>)<var><a name="index-readdir64-1419"></a></var><br>
<blockquote><p>The <code>readdir64</code> function is just like the <code>readdir</code> function
except that it returns a pointer to a record of type <code>struct
dirent64</code>. Some of the members of this data type (notably <code>d_ino</code>)
might have a different size to allow large filesystems.
<p>In all other aspects this function is equivalent to <code>readdir</code>.
</p></blockquote></div>
<!-- dirent.h -->
<!-- LFS -->
<div class="defun">
&mdash; Function: int <b>readdir64_r</b> (<var>DIR *dirstream, struct dirent64 *entry, struct dirent64 **result</var>)<var><a name="index-readdir64_005fr-1420"></a></var><br>
<blockquote><p>The <code>readdir64_r</code> function is equivalent to the <code>readdir_r</code>
function except that it takes parameters of base type <code>struct
dirent64</code> instead of <code>struct dirent</code> in the second and third
position. The same precautions mentioned in the documentation of
<code>readdir_r</code> also apply here.
</p></blockquote></div>
<!-- dirent.h -->
<!-- POSIX.1 -->
<div class="defun">
&mdash; Function: int <b>closedir</b> (<var>DIR *dirstream</var>)<var><a name="index-closedir-1421"></a></var><br>
<blockquote><p>This function closes the directory stream <var>dirstream</var>. It returns
<code>0</code> on success and <code>-1</code> on failure.
<p>The following <code>errno</code> error conditions are defined for this
function:
<dl>
<dt><code>EBADF</code><dd>The <var>dirstream</var> argument is not valid.
</dl>
</p></blockquote></div>
</body></html>