1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2008, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
10 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
11 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
12 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
13 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
14 .\" Rework discussion of nonstandard structure fields.
16 .TH READDIR 3 2021-03-22 "Linux man-pages (unreleased)"
18 readdir \- read a directory
21 .RI ( libc ", " \-lc )
24 .B #include <dirent.h>
26 .BI "struct dirent *readdir(DIR *" dirp );
31 function returns a pointer to a \fIdirent\fP structure
32 representing the next directory entry in the directory stream pointed
34 It returns NULL on reaching the end of the directory stream or if
37 In the glibc implementation, the
39 structure is defined as follows:
44 ino_t d_ino; /* Inode number */
45 off_t d_off; /* Not an offset; see below */
46 unsigned short d_reclen; /* Length of this record */
47 unsigned char d_type; /* Type of file; not supported
48 by all filesystem types */
49 char d_name[256]; /* Null\-terminated filename */
54 The only fields in the
56 structure that are mandated by POSIX.1 are
60 The other fields are unstandardized, and not present on all systems;
61 see NOTES below for some further details.
65 structure are as follows:
68 This is the inode number of the file.
73 is the same as would be returned by calling
75 at the current position in the directory stream.
76 Be aware that despite its type and name, the
78 field is seldom any kind of directory offset on modern filesystems.
79 .\" https://lwn.net/Articles/544298/
80 Applications should treat this field as an opaque value,
81 making no assumptions about its contents; see also
85 This is the size (in bytes) of the returned record.
86 This may not match the size of the structure definition shown above;
90 This field contains a value indicating the file type,
91 making it possible to avoid the expense of calling
93 if further actions depend on the type of the file.
95 When a suitable feature test macro is defined
97 on glibc versions since 2.19, or
99 on glibc versions 2.19 and earlier),
100 glibc defines the following macro constants for the value returned in
105 This is a block device.
108 This is a character device.
114 This is a named pipe (FIFO).
117 This is a symbolic link.
120 This is a regular file.
123 This is a UNIX domain socket.
126 The file type could not be determined.
131 .\" The same sentence is in getdents.2
132 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
133 have full support for returning the file type in
135 All applications must properly handle a return of
139 This field contains the null terminated filename.
144 may be overwritten by subsequent calls to
146 for the same directory stream.
150 returns a pointer to a
153 (This structure may be statically allocated; do not attempt to
157 If the end of the directory stream is reached, NULL is returned and
160 If an error occurs, NULL is returned and
162 is set to indicate the error.
163 To distinguish end of stream from an error, set
165 to zero before calling
167 and then check the value of
173 Invalid directory stream descriptor \fIdirp\fP.
175 For an explanation of the terms used in this section, see
183 Interface Attribute Value
186 T} Thread safety MT-Unsafe race:dirstream
192 In the current POSIX.1 specification (POSIX.1-2008),
194 is not required to be thread-safe.
195 However, in modern implementations (including the glibc implementation),
198 that specify different directory streams are thread-safe.
199 In cases where multiple threads must read from the same directory stream,
202 with external synchronization is still preferable to the use of the deprecated
205 It is expected that a future version of POSIX.1
207 .\" http://www.austingroupbugs.net/view.php?id=696
210 be thread-safe when concurrently employed on different directory streams.
212 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
214 A directory stream is opened using
217 The order in which filenames are read by successive calls to
219 depends on the filesystem implementation;
220 it is unlikely that the names will be sorted in any fashion.
224 and (as an XSI extension)
226 are specified in POSIX.1.
227 .\" POSIX.1-2001, POSIX.1-2008
228 Other than Linux, the
230 field is available mainly only on BSD systems.
231 The remaining fields are available on many, but not all systems.
233 programs can check for the availability of the fields not defined
234 in POSIX.1 by testing whether the macros
235 .BR _DIRENT_HAVE_D_NAMLEN ,
236 .BR _DIRENT_HAVE_D_RECLEN ,
237 .BR _DIRENT_HAVE_D_OFF ,
239 .B _DIRENT_HAVE_D_TYPE
245 structure definition shown above is taken from the glibc headers,
248 field with a fixed size.
251 applications should avoid any dependence on the size of the
255 .IR "char\ d_name[]",
256 a character array of unspecified size, with at most
258 characters preceding the terminating null byte (\(aq\e0\(aq).
260 POSIX.1 explicitly notes that this field should not be used as an lvalue.
261 The standard also notes that the use of
266 (On some systems, this field is defined as
267 .IR char\~d_name[1] !)
268 By implication, the use
269 .I sizeof(struct dirent)
270 to capture the size of the record including the size of
274 Note that while the call
278 fpathconf(fd, _PC_NAME_MAX)
282 returns the value 255 for most filesystems,
283 on some filesystems (e.g., CIFS, Windows SMB servers),
284 the null-terminated filename that is (correctly) returned in
286 can actually exceed this size.
289 field will contain a value that exceeds the size of the glibc
291 structure shown above.