1readdir(3)                 Library Functions Manual                 readdir(3)
2
3
4

NAME

6       readdir - read a directory
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <dirent.h>
13
14       struct dirent *readdir(DIR *dirp);
15

DESCRIPTION

17       The  readdir()  function returns a pointer to a dirent structure repre‐
18       senting the next directory entry in the directory stream pointed to  by
19       dirp.   It  returns NULL on reaching the end of the directory stream or
20       if an error occurred.
21
22       In the glibc implementation, the dirent structure is  defined  as  fol‐
23       lows:
24
25           struct dirent {
26               ino_t          d_ino;       /* Inode number */
27               off_t          d_off;       /* Not an offset; see below */
28               unsigned short d_reclen;    /* Length of this record */
29               unsigned char  d_type;      /* Type of file; not supported
30                                              by all filesystem types */
31               char           d_name[256]; /* Null-terminated filename */
32           };
33
34       The  only  fields  in the dirent structure that are mandated by POSIX.1
35       are d_name and d_ino.  The other fields  are  unstandardized,  and  not
36       present on all systems; see NOTES below for some further details.
37
38       The fields of the dirent structure are as follows:
39
40       d_ino  This is the inode number of the file.
41
42       d_off  The  value returned in d_off is the same as would be returned by
43              calling telldir(3) at the  current  position  in  the  directory
44              stream.   Be  aware  that  despite  its type and name, the d_off
45              field is seldom any kind of directory offset on modern  filesys‐
46              tems.   Applications should treat this field as an opaque value,
47              making no assumptions about its contents; see also telldir(3).
48
49       d_reclen
50              This is the size (in bytes) of the returned  record.   This  may
51              not  match the size of the structure definition shown above; see
52              NOTES.
53
54       d_type This field contains a value indicating the file type, making  it
55              possible to avoid the expense of calling lstat(2) if further ac‐
56              tions depend on the type of the file.
57
58              When a suitable feature test macro is  defined  (_DEFAULT_SOURCE
59              since  glibc  2.19,  or  _BSD_SOURCE on glibc 2.19 and earlier),
60              glibc defines the following macro constants for  the  value  re‐
61              turned in d_type:
62
63              DT_BLK      This is a block device.
64
65              DT_CHR      This is a character device.
66
67              DT_DIR      This is a directory.
68
69              DT_FIFO     This is a named pipe (FIFO).
70
71              DT_LNK      This is a symbolic link.
72
73              DT_REG      This is a regular file.
74
75              DT_SOCK     This is a UNIX domain socket.
76
77              DT_UNKNOWN  The file type could not be determined.
78
79              Currently, only some filesystems (among them: Btrfs, ext2, ext3,
80              and ext4) have full support  for  returning  the  file  type  in
81              d_type.   All  applications  must  properly  handle  a return of
82              DT_UNKNOWN.
83
84       d_name This field contains the null terminated filename.  See NOTES.
85
86       The data returned by readdir() may be overwritten by  subsequent  calls
87       to readdir() for the same directory stream.
88

RETURN VALUE

90       On  success,  readdir() returns a pointer to a dirent structure.  (This
91       structure may be statically allocated; do not attempt to free(3) it.)
92
93       If the end of the directory stream is reached, NULL is returned and er‐
94       rno  is not changed.  If an error occurs, NULL is returned and errno is
95       set to indicate the error.  To distinguish end of stream from an error,
96       set  errno to zero before calling readdir() and then check the value of
97       errno if NULL is returned.
98

ERRORS

100       EBADF  Invalid directory stream descriptor dirp.
101

ATTRIBUTES

103       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
104       tributes(7).
105
106       ┌───────────────────────────┬───────────────┬──────────────────────────┐
107Interface                  Attribute     Value                    
108       ├───────────────────────────┼───────────────┼──────────────────────────┤
109readdir()                  │ Thread safety │ MT-Unsafe race:dirstream │
110       └───────────────────────────┴───────────────┴──────────────────────────┘
111
112       In  the  current POSIX.1 specification (POSIX.1-2008), readdir() is not
113       required to be thread-safe.  However, in  modern  implementations  (in‐
114       cluding  the  glibc implementation), concurrent calls to readdir() that
115       specify different directory streams are thread-safe.   In  cases  where
116       multiple  threads must read from the same directory stream, using read‐
117       dir() with external synchronization is still preferable to the  use  of
118       the  deprecated  readdir_r(3)  function.   It is expected that a future
119       version of POSIX.1 will require that readdir() be thread-safe when con‐
120       currently employed on different directory streams.
121

VERSIONS

123       Only the fields d_name and (as an XSI extension) d_ino are specified in
124       POSIX.1.  Other than Linux, the d_type field is available  mainly  only
125       on  BSD  systems.   The remaining fields are available on many, but not
126       all systems.  Under glibc, programs can check for the  availability  of
127       the  fields  not  defined in POSIX.1 by testing whether the macros _DI‐
128       RENT_HAVE_D_NAMLEN, _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF, or  _DI‐
129       RENT_HAVE_D_TYPE are defined.
130
131   The d_name field
132       The  dirent  structure  definition  shown above is taken from the glibc
133       headers, and shows the d_name field with a fixed size.
134
135       Warning: applications should avoid any dependence on the  size  of  the
136       d_name  field.  POSIX defines it as char d_name[], a character array of
137       unspecified size, with at most NAME_MAX characters preceding the termi‐
138       nating null byte ('\0').
139
140       POSIX.1 explicitly notes that this field should not be used as an lval‐
141       ue.  The standard also notes that the use of sizeof(d_name)  is  incor‐
142       rect;  use strlen(d_name) instead.  (On some systems, this field is de‐
143       fined as char d_name[1]!)  By implication, the  use  sizeof(struct  di‐
144       rent) to capture the size of the record including the size of d_name is
145       also incorrect.
146
147       Note that while the call
148
149           fpathconf(fd, _PC_NAME_MAX)
150
151       returns the value 255 for most filesystems, on some filesystems  (e.g.,
152       CIFS,  Windows SMB servers), the null-terminated filename that is (cor‐
153       rectly) returned in d_name can actually  exceed  this  size.   In  such
154       cases, the d_reclen field will contain a value that exceeds the size of
155       the glibc dirent structure shown above.
156

STANDARDS

158       POSIX.1-2008.
159

HISTORY

161       POSIX.1-2001, SVr4, 4.3BSD.
162

NOTES

164       A directory stream is opened using opendir(3).
165
166       The order in which filenames are read by successive calls to  readdir()
167       depends on the filesystem implementation; it is unlikely that the names
168       will be sorted in any fashion.
169

SEE ALSO

171       getdents(2),  read(2),  closedir(3),  dirfd(3),  ftw(3),   offsetof(3),
172       opendir(3),   readdir_r(3),   rewinddir(3),   scandir(3),   seekdir(3),
173       telldir(3)
174
175
176
177Linux man-pages 6.04              2023-03-30                        readdir(3)
Impressum