1READDIR(3) Linux Programmer's Manual READDIR(3)
2
3
4
6 readdir - read a directory
7
9 #include <dirent.h>
10
11 struct dirent *readdir(DIR *dirp);
12
14 The readdir() function returns a pointer to a dirent structure repre‐
15 senting the next directory entry in the directory stream pointed to by
16 dirp. It returns NULL on reaching the end of the directory stream or
17 if an error occurred.
18
19 In the glibc implementation, the dirent structure is defined as fol‐
20 lows:
21
22 struct dirent {
23 ino_t d_ino; /* Inode number */
24 off_t d_off; /* Not an offset; see below */
25 unsigned short d_reclen; /* Length of this record */
26 unsigned char d_type; /* Type of file; not supported
27 by all filesystem types */
28 char d_name[256]; /* Null-terminated filename */
29 };
30
31 The only fields in the dirent structure that are mandated by POSIX.1
32 are d_name and d_ino. The other fields are unstandardized, and not
33 present on all systems; see NOTES below for some further details.
34
35 The fields of the dirent structure are as follows:
36
37 d_ino This is the inode number of the file.
38
39 d_off The value returned in d_off is the same as would be returned by
40 calling telldir(3) at the current position in the directory
41 stream. Be aware that despite its type and name, the d_off
42 field is seldom any kind of directory offset on modern filesys‐
43 tems. Applications should treat this field as an opaque value,
44 making no assumptions about its contents; see also telldir(3).
45
46 d_reclen
47 This is the size (in bytes) of the returned record. This may
48 not match the size of the structure definition shown above; see
49 NOTES.
50
51 d_type This field contains a value indicating the file type, making it
52 possible to avoid the expense of calling lstat(2) if further
53 actions depend on the type of the file.
54
55 When a suitable feature test macro is defined (_DEFAULT_SOURCE
56 on glibc versions since 2.19, or _BSD_SOURCE on glibc versions
57 2.19 and earlier), glibc defines the following macro constants
58 for the value returned in d_type:
59
60 DT_BLK This is a block device.
61
62 DT_CHR This is a character device.
63
64 DT_DIR This is a directory.
65
66 DT_FIFO This is a named pipe (FIFO).
67
68 DT_LNK This is a symbolic link.
69
70 DT_REG This is a regular file.
71
72 DT_SOCK This is a UNIX domain socket.
73
74 DT_UNKNOWN The file type could not be determined.
75
76 Currently, only some filesystems (among them: Btrfs, ext2, ext3,
77 and ext4) have full support for returning the file type in
78 d_type. All applications must properly handle a return of
79 DT_UNKNOWN.
80
81 d_name This field contains the null terminated filename. See NOTES.
82
83 The data returned by readdir() may be overwritten by subsequent calls
84 to readdir() for the same directory stream.
85
87 On success, readdir() returns a pointer to a dirent structure. (This
88 structure may be statically allocated; do not attempt to free(3) it.)
89
90 If the end of the directory stream is reached, NULL is returned and
91 errno is not changed. If an error occurs, NULL is returned and errno
92 is set appropriately. To distinguish end of stream and from an error,
93 set errno to zero before calling readdir() and then check the value of
94 errno if NULL is returned.
95
97 EBADF Invalid directory stream descriptor dirp.
98
100 For an explanation of the terms used in this section, see
101 attributes(7).
102
103 ┌──────────┬───────────────┬──────────────────────────┐
104 │Interface │ Attribute │ Value │
105 ├──────────┼───────────────┼──────────────────────────┤
106 │readdir() │ Thread safety │ MT-Unsafe race:dirstream │
107 └──────────┴───────────────┴──────────────────────────┘
108
109 In the current POSIX.1 specification (POSIX.1-2008), readdir() is not
110 required to be thread-safe. However, in modern implementations
111 (including the glibc implementation), concurrent calls to readdir()
112 that specify different directory streams are thread-safe. In cases
113 where multiple threads must read from the same directory stream, using
114 readdir() with external synchronization is still preferable to the use
115 of the deprecated readdir_r(3) function. It is expected that a future
116 version of POSIX.1 will require that readdir() be thread-safe when con‐
117 currently employed on different directory streams.
118
120 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
121
123 A directory stream is opened using opendir(3).
124
125 The order in which filenames are read by successive calls to readdir()
126 depends on the filesystem implementation; it is unlikely that the names
127 will be sorted in any fashion.
128
129 Only the fields d_name and (as an XSI extension) d_ino are specified in
130 POSIX.1. Other than Linux, the d_type field is available mainly only
131 on BSD systems. The remaining fields are available on many, but not
132 all systems. Under glibc, programs can check for the availability of
133 the fields not defined in POSIX.1 by testing whether the macros
134 _DIRENT_HAVE_D_NAMLEN, _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF, or
135 _DIRENT_HAVE_D_TYPE are defined.
136
137 The d_name field
138 The dirent structure definition shown above is taken from the glibc
139 headers, and shows the d_name field with a fixed size.
140
141 Warning: applications should avoid any dependence on the size of the
142 d_name field. POSIX defines it as char d_name[], a character array of
143 unspecified size, with at most NAME_MAX characters preceding the termi‐
144 nating null byte ('\0').
145
146 POSIX.1 explicitly notes that this field should not be used as an
147 lvalue. The standard also notes that the use of sizeof(d_name) is
148 incorrect; use strlen(d_name) instead. (On some systems, this field is
149 defined as char d_name[1]!) By implication, the use sizeof(struct
150 dirent) to capture the size of the record including the size of d_name
151 is also incorrect.
152
153 Note that while the call
154
155 fpathconf(fd, _PC_NAME_MAX)
156
157 returns the value 255 for most filesystems, on some filesystems (e.g.,
158 CIFS, Windows SMB servers), the null-terminated filename that is (cor‐
159 rectly) returned in d_name can actually exceed this size. In such
160 cases, the d_reclen field will contain a value that exceeds the size of
161 the glibc dirent structure shown above.
162
164 getdents(2), read(2), closedir(3), dirfd(3), ftw(3), offsetof(3),
165 opendir(3), readdir_r(3), rewinddir(3), scandir(3), seekdir(3),
166 telldir(3)
167
169 This page is part of release 5.04 of the Linux man-pages project. A
170 description of the project, information about reporting bugs, and the
171 latest version of this page, can be found at
172 https://www.kernel.org/doc/man-pages/.
173
174
175
176 2019-03-06 READDIR(3)