OSDN Git Service

0c8cfded67767582bda0a1af2a6ffdc801f2efde
[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  2013-06-21 "" "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;       /* not an offset; see NOTES */
79     unsigned short d_reclen;    /* length of this record */
80     unsigned char  d_type;      /* type of file; not supported
81                                    by all filesystem 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 (\(aq\\0\(aq);
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 ATTRIBUTES
148 .SS Multithreading (see pthreads(7))
149 The
150 .BR readdir ()
151 function is not thread-safe.
152 .LP
153 The
154 .BR readdir_r ()
155 function is thread-safe.
156 .SH CONFORMING TO
157 SVr4, 4.3BSD, POSIX.1-2001.
158 .SH NOTES
159 Only the fields
160 .I d_name
161 and
162 .I d_ino
163 are specified in POSIX.1-2001.
164 The remaining fields are available on many, but not all systems.
165 Under glibc,
166 programs can check for the availability of the fields not defined
167 in POSIX.1 by testing whether the macros
168 .BR _DIRENT_HAVE_D_NAMLEN ,
169 .BR _DIRENT_HAVE_D_RECLEN ,
170 .BR _DIRENT_HAVE_D_OFF ,
171 or
172 .B _DIRENT_HAVE_D_TYPE
173 are defined.
174
175 The value returned in
176 .I d_off
177 is the same as would be returned by calling
178 .BR telldir (3)
179 at the current position in the directory stream.
180 Be aware that despite its type and name, the
181 .I d_off
182 field is seldom any kind of directory offset on modern filesystems.
183 .\" https://lwn.net/Articles/544298/
184 Applications should treat this field as an opaque value,
185 making no assumptions about its contents; see also
186 .BR telldir (3).
187
188 Other than Linux, the
189 .I d_type
190 field is available mainly only on BSD systems.
191 This field makes it possible to avoid the expense of calling
192 .BR lstat (2)
193 if further actions depend on the type of the file.
194 If the
195 .B _BSD_SOURCE
196 feature test macro is defined,
197 then glibc defines the following macro constants
198 for the value returned in
199 .IR d_type :
200 .TP 12
201 .B DT_BLK
202 This is a block device.
203 .TP
204 .B DT_CHR
205 This is a character device.
206 .TP
207 .B DT_DIR
208 This is a directory.
209 .TP
210 .B DT_FIFO
211 This is a named pipe (FIFO).
212 .TP
213 .B DT_LNK
214 This is a symbolic link.
215 .TP
216 .B DT_REG
217 This is a regular file.
218 .TP
219 .B DT_SOCK
220 This is a UNIX domain socket.
221 .TP
222 .B DT_UNKNOWN
223 The file type is unknown.
224 .\" The glibc manual says that on some systems this is the only
225 .\" value returned
226 .PP
227 If the file type could not be determined, the value
228 .B DT_UNKNOWN
229 is returned in
230 .IR d_type .
231
232 Currently,
233 .\" kernel 2.6.27
234 .\" The same sentence is in getdents.2
235 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
236 have full support for returning the file type in
237 .IR d_type .
238 All applications must properly handle a return of
239 .BR DT_UNKNOWN .
240
241 Since POSIX.1 does not specify the size of the
242 .I d_name
243 field, and other nonstandard fields may precede that field within the
244 .I dirent
245 structure, portable applications that use
246 .BR readdir_r ()
247 should allocate the buffer whose address is passed in
248 .IR entry
249 as follows:
250 .in +4n
251 .nf
252
253 name_max = pathconf(dirpath, _PC_NAME_MAX);
254 if (name_max == \-1)         /* Limit not defined, or error */
255     name_max = 255;         /* Take a guess */
256 len = offsetof(struct dirent, d_name) + name_max + 1;
257 entryp = malloc(len);
258
259 .fi
260 .in
261 (POSIX.1 requires that
262 .I d_name
263 is the last field in a
264 .IR "struct dirent" .)
265 .SH SEE ALSO
266 .BR getdents (2),
267 .BR read (2),
268 .BR closedir (3),
269 .BR dirfd (3),
270 .BR ftw (3),
271 .BR offsetof (3),
272 .BR opendir (3),
273 .BR rewinddir (3),
274 .BR scandir (3),
275 .BR seekdir (3),
276 .BR telldir (3)
277 .SH COLOPHON
278 This page is part of release 3.67 of the Linux
279 .I man-pages
280 project.
281 A description of the project,
282 information about reporting bugs,
283 and the latest version of this page,
284 can be found at
285 \%http://www.kernel.org/doc/man\-pages/.