2 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" and Copyright (C) 2008, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References consulted:
8 .\" Linux libc source code
9 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
11 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
12 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
13 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
14 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
15 .\" Rework discussion of nonstandard structure fields.
17 .TH readdir 3 (date) "Linux man-pages (unreleased)"
19 readdir \- read a directory
22 .RI ( libc ", " \-lc )
25 .B #include <dirent.h>
27 .BI "struct dirent *readdir(DIR *" dirp );
32 function returns a pointer to a \fIdirent\fP structure
33 representing the next directory entry in the directory stream pointed
35 It returns NULL on reaching the end of the directory stream or if
38 In the glibc implementation, the
40 structure is defined as follows:
45 ino_t d_ino; /* Inode number */
46 off_t d_off; /* Not an offset; see below */
47 unsigned short d_reclen; /* Length of this record */
48 unsigned char d_type; /* Type of file; not supported
49 by all filesystem types */
50 char d_name[256]; /* Null\-terminated filename */
55 The only fields in the
57 structure that are mandated by POSIX.1 are
61 The other fields are unstandardized, and not present on all systems;
62 see NOTES below for some further details.
66 structure are as follows:
69 This is the inode number of the file.
74 is the same as would be returned by calling
76 at the current position in the directory stream.
77 Be aware that despite its type and name, the
79 field is seldom any kind of directory offset on modern filesystems.
80 .\" https://lwn.net/Articles/544298/
81 Applications should treat this field as an opaque value,
82 making no assumptions about its contents; see also
86 This is the size (in bytes) of the returned record.
87 This may not match the size of the structure definition shown above;
91 This field contains a value indicating the file type,
92 making it possible to avoid the expense of calling
94 if further actions depend on the type of the file.
96 When a suitable feature test macro is defined
100 on glibc 2.19 and earlier),
101 glibc defines the following macro constants for the value returned in
106 This is a block device.
109 This is a character device.
115 This is a named pipe (FIFO).
118 This is a symbolic link.
121 This is a regular file.
124 This is a UNIX domain socket.
127 The file type could not be determined.
132 .\" The same sentence is in getdents.2
133 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
134 have full support for returning the file type in
136 All applications must properly handle a return of
140 This field contains the null terminated filename.
145 may be overwritten by subsequent calls to
147 for the same directory stream.
151 returns a pointer to a
154 (This structure may be statically allocated; do not attempt to
158 If the end of the directory stream is reached, NULL is returned and
161 If an error occurs, NULL is returned and
163 is set to indicate the error.
164 To distinguish end of stream from an error, set
166 to zero before calling
168 and then check the value of
174 Invalid directory stream descriptor \fIdirp\fP.
176 For an explanation of the terms used in this section, see
182 Interface Attribute Value
187 T} Thread safety MT-Unsafe race:dirstream
191 In the current POSIX.1 specification (POSIX.1-2008),
193 is not required to be thread-safe.
194 However, in modern implementations (including the glibc implementation),
197 that specify different directory streams are thread-safe.
198 In cases where multiple threads must read from the same directory stream,
201 with external synchronization is still preferable to the use of the deprecated
204 It is expected that a future version of POSIX.1
206 .\" http://www.austingroupbugs.net/view.php?id=696
209 be thread-safe when concurrently employed on different directory streams.
213 and (as an XSI extension)
215 are specified in POSIX.1.
216 .\" POSIX.1-2001, POSIX.1-2008
217 Other than Linux, the
219 field is available mainly only on BSD systems.
220 The remaining fields are available on many, but not all systems.
222 programs can check for the availability of the fields not defined
223 in POSIX.1 by testing whether the macros
224 .BR _DIRENT_HAVE_D_NAMLEN ,
225 .BR _DIRENT_HAVE_D_RECLEN ,
226 .BR _DIRENT_HAVE_D_OFF ,
228 .B _DIRENT_HAVE_D_TYPE
234 structure definition shown above is taken from the glibc headers,
237 field with a fixed size.
240 applications should avoid any dependence on the size of the
244 .IR "char\ d_name[]",
245 a character array of unspecified size, with at most
247 characters preceding the terminating null byte (\[aq]\e0\[aq]).
249 POSIX.1 explicitly notes that this field should not be used as an lvalue.
250 The standard also notes that the use of
255 (On some systems, this field is defined as
256 .IR char\~d_name[1] !)
257 By implication, the use
258 .I sizeof(struct dirent)
259 to capture the size of the record including the size of
263 Note that while the call
267 fpathconf(fd, _PC_NAME_MAX)
271 returns the value 255 for most filesystems,
272 on some filesystems (e.g., CIFS, Windows SMB servers),
273 the null-terminated filename that is (correctly) returned in
275 can actually exceed this size.
278 field will contain a value that exceeds the size of the glibc
280 structure shown above.
284 POSIX.1-2001, SVr4, 4.3BSD.
286 A directory stream is opened using
289 The order in which filenames are read by successive calls to
291 depends on the filesystem implementation;
292 it is unlikely that the names will be sorted in any fashion.