OSDN Git Service

f7870fcfa7c148bdfd9b4696f5417aa60a50fd80
[linuxjm/LDP_man-pages.git] / original / man3 / readdir.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
31 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
32 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
33 .\"     Rework discussion of nonstandard structure fields.
34 .\" 2008-09-11, mtk, Document readdir_r().
35 .\"
36 .TH READDIR 3  2012-07-07 "" "Linux Programmer's Manual"
37 .SH NAME
38 readdir, readdir_r \- read a directory
39 .SH SYNOPSIS
40 .nf
41 .B #include <dirent.h>
42 .sp
43 .BI "struct dirent *readdir(DIR *" dirp );
44 .sp
45 .BI "int readdir_r(DIR *" dirp ", struct dirent *" entry \
46 ", struct dirent **" result );
47 .fi
48 .sp
49 .in -4n
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
52 .ad l
53 .in
54 .sp
55 .BR readdir_r ():
56 .RS 4
57 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
58 _SVID_SOURCE || _POSIX_SOURCE
59 .RE
60 .ad b
61 .SH DESCRIPTION
62 The
63 .BR readdir ()
64 function returns a pointer to a \fIdirent\fP structure
65 representing the next directory entry in the directory stream pointed
66 to by \fIdirp\fP.
67 It returns NULL on reaching the end of the directory stream or if
68 an error occurred.
69 .PP
70 On Linux, the
71 .I dirent
72 structure is defined as follows:
73 .PP
74 .in +4n
75 .nf
76 struct dirent {
77     ino_t          d_ino;       /* inode number */
78     off_t          d_off;       /* offset to the next dirent */
79     unsigned short d_reclen;    /* length of this record */
80     unsigned char  d_type;      /* type of file; not supported
81                                    by all file system types */
82     char           d_name[256]; /* filename */
83 };
84 .fi
85 .in
86 .PP
87 The only fields in the
88 .I dirent
89 structure that are mandated by POSIX.1 are:
90 .IR d_name [],
91 of unspecified size, with at most
92 .B NAME_MAX
93 characters preceding the terminating null byte;
94 and (as an XSI extension)
95 .IR d_ino .
96 The other fields are unstandardized, and not present on all systems;
97 see NOTES below for some further details.
98 .PP
99 The data returned by
100 .BR readdir ()
101 may be overwritten by subsequent calls to
102 .BR readdir ()
103 for the same directory stream.
104
105 The
106 .BR readdir_r ()
107 function is a reentrant version of
108 .BR readdir ().
109 It reads the next directory entry from the directory stream
110 .IR dirp ,
111 and returns it in the caller-allocated buffer pointed to by
112 .IR entry .
113 (See NOTES for information on allocating this buffer.)
114 A pointer to the returned item is placed in
115 .IR *result ;
116 if the end of the directory stream was encountered,
117 then NULL is instead returned in
118 .IR *result .
119 .SH RETURN VALUE
120 On success,
121 .BR readdir ()
122 returns a pointer to a
123 .I dirent
124 structure.
125 (This structure may be statically allocated; do not attempt to
126 .BR free (3)
127 it.)
128 If the end of the directory stream is reached, NULL is returned and
129 .I errno
130 is not changed.
131 If an error occurs, NULL is returned and
132 .I errno
133 is set appropriately.
134
135 The
136 .BR readdir_r ()
137 function returns 0 on success.
138 On error, it returns a positive error number (listed under ERRORS).
139 If the end of the directory stream is reached,
140 .BR readdir_r ()
141 returns 0, and returns NULL in
142 .IR *result .
143 .SH ERRORS
144 .TP
145 .B EBADF
146 Invalid directory stream descriptor \fIdirp\fP.
147 .SH CONFORMING TO
148 SVr4, 4.3BSD, POSIX.1-2001.
149 .SH NOTES
150 Only the fields
151 .I d_name
152 and
153 .I d_ino
154 are specified in POSIX.1-2001.
155 The remaining fields are available on many, but not all systems.
156 Under glibc,
157 programs can check for the availability of the fields not defined
158 in POSIX.1 by testing whether the macros
159 .BR _DIRENT_HAVE_D_NAMLEN ,
160 .BR _DIRENT_HAVE_D_RECLEN ,
161 .BR _DIRENT_HAVE_D_OFF ,
162 or
163 .B _DIRENT_HAVE_D_TYPE
164 are defined.
165
166 Other than Linux, the
167 .I d_type
168 field is available mainly only on BSD systems.
169 This field makes it possible to avoid the expense of calling
170 .BR lstat (2)
171 if further actions depend on the type of the file.
172 If the
173 .B _BSD_SOURCE
174 feature test macro is defined,
175 then glibc defines the following macro constants
176 for the value returned in
177 .IR d_type :
178 .TP 12
179 .B DT_BLK
180 This is a block device.
181 .TP
182 .B DT_CHR
183 This is a character device.
184 .TP
185 .B DT_DIR
186 This is a directory.
187 .TP
188 .B DT_FIFO
189 This is a named pipe (FIFO).
190 .TP
191 .B DT_LNK
192 This is a symbolic link.
193 .TP
194 .B DT_REG
195 This is a regular file.
196 .TP
197 .B DT_SOCK
198 This is a UNIX domain socket.
199 .TP
200 .B DT_UNKNOWN
201 The file type is unknown.
202 .\" The glibc manual says that on some systems this is the only
203 .\" value returned
204 .PP
205 If the file type could not be determined, the value
206 .B DT_UNKNOWN
207 is returned in
208 .IR d_type .
209
210 Currently,
211 .\" kernel 2.6.27
212 .\" The same sentence is in getdents.2
213 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
214 have full support for returning the file type in
215 .IR d_type .
216 All applications must properly handle a return of
217 .BR DT_UNKNOWN .
218
219 Since POSIX.1 does not specify the size of the
220 .I d_name
221 field, and other nonstandard fields may precede that field within the
222 .I dirent
223 structure, portable applications that use
224 .BR readdir_r ()
225 should allocate the buffer whose address is passed in
226 .IR entry
227 as follows:
228 .in +4n
229 .nf
230
231 name_max = pathconf(dirpath, _PC_NAME_MAX);
232 if (name_max == \-1)         /* Limit not defined, or error */
233     name_max = 255;         /* Take a guess */
234 len = offsetof(struct dirent, d_name) + name_max + 1;
235 entryp = malloc(len);
236
237 .fi
238 .in
239 (POSIX.1 requires that
240 .I d_name
241 is the last field in a
242 .IR "struct dirent" .)
243 .SH SEE ALSO
244 .BR getdents (2),
245 .BR read (2),
246 .BR closedir (3),
247 .BR dirfd (3),
248 .BR ftw (3),
249 .BR offsetof (3),
250 .BR opendir (3),
251 .BR rewinddir (3),
252 .BR scandir (3),
253 .BR seekdir (3),
254 .BR telldir (3)