1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2008, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
32 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
33 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
34 .\" Rework discussion of nonstandard structure fields.
36 .TH READDIR 3 2021-03-22 "" "Linux Programmer's Manual"
38 readdir \- read a directory
41 .B #include <dirent.h>
43 .BI "struct dirent *readdir(DIR *" dirp );
48 function returns a pointer to a \fIdirent\fP structure
49 representing the next directory entry in the directory stream pointed
51 It returns NULL on reaching the end of the directory stream or if
54 In the glibc implementation, the
56 structure is defined as follows:
61 ino_t d_ino; /* Inode number */
62 off_t d_off; /* Not an offset; see below */
63 unsigned short d_reclen; /* Length of this record */
64 unsigned char d_type; /* Type of file; not supported
65 by all filesystem types */
66 char d_name[256]; /* Null\-terminated filename */
71 The only fields in the
73 structure that are mandated by POSIX.1 are
77 The other fields are unstandardized, and not present on all systems;
78 see NOTES below for some further details.
82 structure are as follows:
85 This is the inode number of the file.
90 is the same as would be returned by calling
92 at the current position in the directory stream.
93 Be aware that despite its type and name, the
95 field is seldom any kind of directory offset on modern filesystems.
96 .\" https://lwn.net/Articles/544298/
97 Applications should treat this field as an opaque value,
98 making no assumptions about its contents; see also
102 This is the size (in bytes) of the returned record.
103 This may not match the size of the structure definition shown above;
107 This field contains a value indicating the file type,
108 making it possible to avoid the expense of calling
110 if further actions depend on the type of the file.
112 When a suitable feature test macro is defined
113 .RB ( _DEFAULT_SOURCE
114 on glibc versions since 2.19, or
116 on glibc versions 2.19 and earlier),
117 glibc defines the following macro constants for the value returned in
122 This is a block device.
125 This is a character device.
131 This is a named pipe (FIFO).
134 This is a symbolic link.
137 This is a regular file.
140 This is a UNIX domain socket.
143 The file type could not be determined.
148 .\" The same sentence is in getdents.2
149 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
150 have full support for returning the file type in
152 All applications must properly handle a return of
156 This field contains the null terminated filename.
161 may be overwritten by subsequent calls to
163 for the same directory stream.
167 returns a pointer to a
170 (This structure may be statically allocated; do not attempt to
174 If the end of the directory stream is reached, NULL is returned and
177 If an error occurs, NULL is returned and
179 is set to indicate the error.
180 To distinguish end of stream from an error, set
182 to zero before calling
184 and then check the value of
190 Invalid directory stream descriptor \fIdirp\fP.
192 For an explanation of the terms used in this section, see
200 Interface Attribute Value
203 T} Thread safety MT-Unsafe race:dirstream
209 In the current POSIX.1 specification (POSIX.1-2008),
211 is not required to be thread-safe.
212 However, in modern implementations (including the glibc implementation),
215 that specify different directory streams are thread-safe.
216 In cases where multiple threads must read from the same directory stream,
219 with external synchronization is still preferable to the use of the deprecated
222 It is expected that a future version of POSIX.1
224 .\" http://www.austingroupbugs.net/view.php?id=696
227 be thread-safe when concurrently employed on different directory streams.
229 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
231 A directory stream is opened using
234 The order in which filenames are read by successive calls to
236 depends on the filesystem implementation;
237 it is unlikely that the names will be sorted in any fashion.
241 and (as an XSI extension)
243 are specified in POSIX.1.
244 .\" POSIX.1-2001, POSIX.1-2008
245 Other than Linux, the
247 field is available mainly only on BSD systems.
248 The remaining fields are available on many, but not all systems.
250 programs can check for the availability of the fields not defined
251 in POSIX.1 by testing whether the macros
252 .BR _DIRENT_HAVE_D_NAMLEN ,
253 .BR _DIRENT_HAVE_D_RECLEN ,
254 .BR _DIRENT_HAVE_D_OFF ,
256 .B _DIRENT_HAVE_D_TYPE
262 structure definition shown above is taken from the glibc headers,
265 field with a fixed size.
268 applications should avoid any dependence on the size of the
272 .IR "char\ d_name[]",
273 a character array of unspecified size, with at most
275 characters preceding the terminating null byte (\(aq\e0\(aq).
277 POSIX.1 explicitly notes that this field should not be used as an lvalue.
278 The standard also notes that the use of
283 (On some systems, this field is defined as
284 .IR char\ d_name[1] !)
285 By implication, the use
286 .IR "sizeof(struct dirent)"
287 to capture the size of the record including the size of
291 Note that while the call
293 fpathconf(fd, _PC_NAME_MAX)
295 returns the value 255 for most filesystems,
296 on some filesystems (e.g., CIFS, Windows SMB servers),
297 the null-terminated filename that is (correctly) returned in
299 can actually exceed this size.
302 field will contain a value that exceeds the size of the glibc
304 structure shown above.