1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
29 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
30 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
31 .\" Rework discussion of nonstandard structure fields.
32 .\" 2008-09-11, mtk, Document readdir_r().
34 .TH READDIR 3 2010-09-10 "" "Linux Programmer's Manual"
36 readdir, readdir_r \- read a directory
39 .B #include <dirent.h>
41 .BI "struct dirent *readdir(DIR *" dirp );
43 .BI "int readdir_r(DIR *" dirp ", struct dirent *" entry \
44 ", struct dirent **" result );
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
55 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
56 _SVID_SOURCE || _POSIX_SOURCE
62 function returns a pointer to a \fIdirent\fP structure
63 representing the next directory entry in the directory stream pointed
65 It returns NULL on reaching the end of the directory stream or if
70 structure is defined as follows:
75 ino_t d_ino; /* inode number */
76 off_t d_off; /* offset to the next dirent */
77 unsigned short d_reclen; /* length of this record */
78 unsigned char d_type; /* type of file; not supported
79 by all file system types */
80 char d_name[256]; /* filename */
85 The only fields in the
87 structure that are mandated by POSIX.1 are:
89 of unspecified size, with at most
91 characters preceding the terminating null byte;
92 and (as an XSI extension)
94 The other fields are unstandardized, and not present on all systems;
95 see NOTES below for some further details.
99 may be overwritten by subsequent calls to
101 for the same directory stream.
105 function is a reentrant version of
107 It reads the next directory entry from the directory stream
109 and returns it in the caller-allocated buffer pointed to by
111 (See NOTES for information on allocating this buffer.)
112 A pointer to the returned item is placed in
114 if the end of the directory stream was encountered,
115 then NULL is instead returned in
120 returns a pointer to a
123 (This structure may be statically allocated; do not attempt to
126 If the end of the directory stream is reached, NULL is returned and
129 If an error occurs, NULL is returned and
131 is set appropriately.
135 function returns 0 on success.
136 On error, it returns a positive error number (listed under ERRORS).
137 If the end of the directory stream is reached,
139 returns 0, and returns NULL in
144 Invalid directory stream descriptor \fIdirp\fP.
146 SVr4, 4.3BSD, POSIX.1-2001.
152 are specified in POSIX.1-2001.
153 The remaining fields are available on many, but not all systems.
155 programs can check for the availability of the fields not defined
156 in POSIX.1 by testing whether the macros
157 .BR _DIRENT_HAVE_D_NAMLEN ,
158 .BR _DIRENT_HAVE_D_RECLEN ,
159 .BR _DIRENT_HAVE_D_OFF ,
161 .B _DIRENT_HAVE_D_TYPE
164 Other than Linux, the
166 field is available mainly only on BSD systems.
167 This field makes it possible to avoid the expense of calling
169 if further actions depend on the type of the file.
172 feature test macro is defined,
173 then glibc defines the following macro constants
174 for the value returned in
178 This is a block device.
181 This is a character device.
187 This is a named pipe (FIFO).
190 This is a symbolic link.
193 This is a regular file.
196 This is a UNIX domain socket.
199 The file type is unknown.
200 .\" The glibc manual says that on some systems this is the only
203 If the file type could not be determined, the value
210 .\" The same sentence is in getdents.2
211 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
212 have full support for returning the file type in
214 All applications must properly handle a return of
217 Since POSIX.1 does not specify the size of the
219 field, and other nonstandard fields may precede that field within the
221 structure, portable applications that use
223 should allocate the buffer whose address is passed in
229 len = offsetof(struct dirent, d_name) +
230 pathconf(dirpath, _PC_NAME_MAX) + 1
231 entryp = malloc(len);
235 (POSIX.1 requires that
237 is the last field in a
238 .IR "struct dirent" .)