1READDIR(3)                 Linux Programmer's Manual                READDIR(3)
2
3
4

NAME

6       readdir, readdir_r - read a directory
7

SYNOPSIS

9       #include <dirent.h>
10
11       struct dirent *readdir(DIR *dirp);
12
13       int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       readdir_r():
18           _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
19           _SVID_SOURCE || _POSIX_SOURCE
20

DESCRIPTION

22       The readdir() function returns a pointer to a dirent  structure  repre‐
23       senting  the next directory entry in the directory stream pointed to by
24       dirp.  It returns NULL on reaching the end of the directory  stream  or
25       if an error occurred.
26
27       On Linux, the dirent structure is defined as follows:
28
29           struct dirent {
30               ino_t          d_ino;       /* inode number */
31               off_t          d_off;       /* not an offset; see NOTES */
32               unsigned short d_reclen;    /* length of this record */
33               unsigned char  d_type;      /* type of file; not supported
34                                              by all file system types */
35               char           d_name[256]; /* filename */
36           };
37
38       The  only  fields  in the dirent structure that are mandated by POSIX.1
39       are: d_name[], of unspecified size, with at  most  NAME_MAX  characters
40       preceding  the  terminating null byte ('\0'); and (as an XSI extension)
41       d_ino.  The other fields are unstandardized, and  not  present  on  all
42       systems; see NOTES below for some further details.
43
44       The  data  returned by readdir() may be overwritten by subsequent calls
45       to readdir() for the same directory stream.
46
47       The readdir_r() function is a reentrant version of readdir().  It reads
48       the next directory entry from the directory stream dirp, and returns it
49       in the caller-allocated buffer pointed to by  entry.   (See  NOTES  for
50       information on allocating this buffer.)  A pointer to the returned item
51       is placed in *result; if the end of the directory  stream  was  encoun‐
52       tered, then NULL is instead returned in *result.
53

RETURN VALUE

55       On  success,  readdir() returns a pointer to a dirent structure.  (This
56       structure may be statically allocated; do not attempt to  free(3)  it.)
57       If  the  end  of  the directory stream is reached, NULL is returned and
58       errno is not changed.  If an error occurs, NULL is returned  and  errno
59       is set appropriately.
60
61       The  readdir_r() function returns 0 on success.  On error, it returns a
62       positive error number (listed under ERRORS).  If the end of the  direc‐
63       tory  stream  is  reached,  readdir_r()  returns 0, and returns NULL in
64       *result.
65

ERRORS

67       EBADF  Invalid directory stream descriptor dirp.
68

ATTRIBUTES

70   Multithreading (see pthreads(7))
71       The readdir() function is not thread-safe.
72
73       The readdir_r() function is thread-safe.
74

CONFORMING TO

76       SVr4, 4.3BSD, POSIX.1-2001.
77

NOTES

79       Only the fields d_name and d_ino are specified  in  POSIX.1-2001.   The
80       remaining  fields  are  available  on many, but not all systems.  Under
81       glibc, programs can check  for  the  availability  of  the  fields  not
82       defined in POSIX.1 by testing whether the macros _DIRENT_HAVE_D_NAMLEN,
83       _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF, or  _DIRENT_HAVE_D_TYPE  are
84       defined.
85
86       The value returned in d_off is the same as would be returned by calling
87       telldir(3) at the current position in the directory stream.   Be  aware
88       that  despite  its type and name, the d_off field is seldom any kind of
89       directory offset on modern file  systems.   Applications  should  treat
90       this  field  as  an  opaque value, making no assumptions about its con‐
91       tents; see also telldir(3).
92
93       Other than Linux, the d_type field is available mainly only on BSD sys‐
94       tems.   This  field  makes  it possible to avoid the expense of calling
95       lstat(2) if further actions depend on the type of  the  file.   If  the
96       _BSD_SOURCE  feature test macro is defined, then glibc defines the fol‐
97       lowing macro constants for the value returned in d_type:
98
99       DT_BLK      This is a block device.
100
101       DT_CHR      This is a character device.
102
103       DT_DIR      This is a directory.
104
105       DT_FIFO     This is a named pipe (FIFO).
106
107       DT_LNK      This is a symbolic link.
108
109       DT_REG      This is a regular file.
110
111       DT_SOCK     This is a UNIX domain socket.
112
113       DT_UNKNOWN  The file type is unknown.
114
115       If the file type could not  be  determined,  the  value  DT_UNKNOWN  is
116       returned in d_type.
117
118       Currently,  only  some file systems (among them: Btrfs, ext2, ext3, and
119       ext4) have full support for returning the file  type  in  d_type.   All
120       applications must properly handle a return of DT_UNKNOWN.
121
122       Since  POSIX.1 does not specify the size of the d_name field, and other
123       nonstandard fields may precede that field within the dirent  structure,
124       portable  applications  that use readdir_r() should allocate the buffer
125       whose address is passed in entry as follows:
126
127           name_max = pathconf(dirpath, _PC_NAME_MAX);
128           if (name_max == -1)         /* Limit not defined, or error */
129               name_max = 255;         /* Take a guess */
130           len = offsetof(struct dirent, d_name) + name_max + 1;
131           entryp = malloc(len);
132
133       (POSIX.1 requires that d_name is the last field in a struct dirent.)
134

SEE ALSO

136       getdents(2),  read(2),  closedir(3),  dirfd(3),  ftw(3),   offsetof(3),
137       opendir(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)
138

COLOPHON

140       This  page  is  part of release 3.53 of the Linux man-pages project.  A
141       description of the project, and information about reporting  bugs,  can
142       be found at http://www.kernel.org/doc/man-pages/.
143
144
145
146                                  2013-06-21                        READDIR(3)
Impressum