1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
31 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
32 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
33 .\" Rework discussion of nonstandard structure fields.
34 .\" 2008-09-11, mtk, Document readdir_r().
36 .TH READDIR 3 2013-06-21 "" "Linux Programmer's Manual"
38 readdir, readdir_r \- read a directory
41 .B #include <dirent.h>
43 .BI "struct dirent *readdir(DIR *" dirp );
45 .BI "int readdir_r(DIR *" dirp ", struct dirent *" entry \
46 ", struct dirent **" result );
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
57 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
58 _SVID_SOURCE || _POSIX_SOURCE
64 function returns a pointer to a \fIdirent\fP structure
65 representing the next directory entry in the directory stream pointed
67 It returns NULL on reaching the end of the directory stream or if
72 structure is defined as follows:
77 ino_t d_ino; /* inode number */
78 off_t d_off; /* not an offset; see NOTES */
79 unsigned short d_reclen; /* length of this record */
80 unsigned char d_type; /* type of file; not supported
81 by all filesystem types */
82 char d_name[256]; /* filename */
87 The only fields in the
89 structure that are mandated by POSIX.1 are:
91 of unspecified size, with at most
93 characters preceding the terminating null byte (\(aq\\0\(aq);
94 and (as an XSI extension)
96 The other fields are unstandardized, and not present on all systems;
97 see NOTES below for some further details.
101 may be overwritten by subsequent calls to
103 for the same directory stream.
107 function is a reentrant version of
109 It reads the next directory entry from the directory stream
111 and returns it in the caller-allocated buffer pointed to by
113 (See NOTES for information on allocating this buffer.)
114 A pointer to the returned item is placed in
116 if the end of the directory stream was encountered,
117 then NULL is instead returned in
122 returns a pointer to a
125 (This structure may be statically allocated; do not attempt to
128 If the end of the directory stream is reached, NULL is returned and
131 If an error occurs, NULL is returned and
133 is set appropriately.
137 function returns 0 on success.
138 On error, it returns a positive error number (listed under ERRORS).
139 If the end of the directory stream is reached,
141 returns 0, and returns NULL in
146 Invalid directory stream descriptor \fIdirp\fP.
148 .SS Multithreading (see pthreads(7))
151 function is not thread-safe.
155 function is thread-safe.
157 SVr4, 4.3BSD, POSIX.1-2001.
163 are specified in POSIX.1-2001.
164 The remaining fields are available on many, but not all systems.
166 programs can check for the availability of the fields not defined
167 in POSIX.1 by testing whether the macros
168 .BR _DIRENT_HAVE_D_NAMLEN ,
169 .BR _DIRENT_HAVE_D_RECLEN ,
170 .BR _DIRENT_HAVE_D_OFF ,
172 .B _DIRENT_HAVE_D_TYPE
175 The value returned in
177 is the same as would be returned by calling
179 at the current position in the directory stream.
180 Be aware that despite its type and name, the
182 field is seldom any kind of directory offset on modern filesystems.
183 .\" https://lwn.net/Articles/544298/
184 Applications should treat this field as an opaque value,
185 making no assumptions about its contents; see also
188 Other than Linux, the
190 field is available mainly only on BSD systems.
191 This field makes it possible to avoid the expense of calling
193 if further actions depend on the type of the file.
196 feature test macro is defined,
197 then glibc defines the following macro constants
198 for the value returned in
202 This is a block device.
205 This is a character device.
211 This is a named pipe (FIFO).
214 This is a symbolic link.
217 This is a regular file.
220 This is a UNIX domain socket.
223 The file type is unknown.
224 .\" The glibc manual says that on some systems this is the only
227 If the file type could not be determined, the value
234 .\" The same sentence is in getdents.2
235 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
236 have full support for returning the file type in
238 All applications must properly handle a return of
241 Since POSIX.1 does not specify the size of the
243 field, and other nonstandard fields may precede that field within the
245 structure, portable applications that use
247 should allocate the buffer whose address is passed in
253 name_max = pathconf(dirpath, _PC_NAME_MAX);
254 if (name_max == \-1) /* Limit not defined, or error */
255 name_max = 255; /* Take a guess */
256 len = offsetof(struct dirent, d_name) + name_max + 1;
257 entryp = malloc(len);
261 (POSIX.1 requires that
263 is the last field in a
264 .IR "struct dirent" .)
278 This page is part of release 3.67 of the Linux
281 A description of the project,
282 information about reporting bugs,
283 and the latest version of this page,
285 \%http://www.kernel.org/doc/man\-pages/.