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 2012-07-07 "" "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; /* offset to the next dirent */
79 unsigned short d_reclen; /* length of this record */
80 unsigned char d_type; /* type of file; not supported
81 by all file system 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;
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 SVr4, 4.3BSD, POSIX.1-2001.
154 are specified in POSIX.1-2001.
155 The remaining fields are available on many, but not all systems.
157 programs can check for the availability of the fields not defined
158 in POSIX.1 by testing whether the macros
159 .BR _DIRENT_HAVE_D_NAMLEN ,
160 .BR _DIRENT_HAVE_D_RECLEN ,
161 .BR _DIRENT_HAVE_D_OFF ,
163 .B _DIRENT_HAVE_D_TYPE
166 Other than Linux, the
168 field is available mainly only on BSD systems.
169 This field makes it possible to avoid the expense of calling
171 if further actions depend on the type of the file.
174 feature test macro is defined,
175 then glibc defines the following macro constants
176 for the value returned in
180 This is a block device.
183 This is a character device.
189 This is a named pipe (FIFO).
192 This is a symbolic link.
195 This is a regular file.
198 This is a UNIX domain socket.
201 The file type is unknown.
202 .\" The glibc manual says that on some systems this is the only
205 If the file type could not be determined, the value
212 .\" The same sentence is in getdents.2
213 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
214 have full support for returning the file type in
216 All applications must properly handle a return of
219 Since POSIX.1 does not specify the size of the
221 field, and other nonstandard fields may precede that field within the
223 structure, portable applications that use
225 should allocate the buffer whose address is passed in
231 name_max = pathconf(dirpath, _PC_NAME_MAX);
232 if (name_max == \-1) /* Limit not defined, or error */
233 name_max = 255; /* Take a guess */
234 len = offsetof(struct dirent, d_name) + name_max + 1;
235 entryp = malloc(len);
239 (POSIX.1 requires that
241 is the last field in a
242 .IR "struct dirent" .)