OSDN Git Service

(split) LDP: Update original to LDP v3.51.
[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-03-24 "" "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 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 The value returned in
167 .I d_off
168 is the same as would be returned by calling
169 .BR telldir (3)
170 at the current position in the directory stream.
171 Be aware that despite its type and name, the
172 .I d_off
173 field is seldom any kind of directory offset on modern file systems.
174 .\" https://lwn.net/Articles/544298/
175 Applications should treat this field as an opaque value,
176 making no assumptions about its contents; see also
177 .BR telldir (3).
178
179 Other than Linux, the
180 .I d_type
181 field is available mainly only on BSD systems.
182 This field makes it possible to avoid the expense of calling
183 .BR lstat (2)
184 if further actions depend on the type of the file.
185 If the
186 .B _BSD_SOURCE
187 feature test macro is defined,
188 then glibc defines the following macro constants
189 for the value returned in
190 .IR d_type :
191 .TP 12
192 .B DT_BLK
193 This is a block device.
194 .TP
195 .B DT_CHR
196 This is a character device.
197 .TP
198 .B DT_DIR
199 This is a directory.
200 .TP
201 .B DT_FIFO
202 This is a named pipe (FIFO).
203 .TP
204 .B DT_LNK
205 This is a symbolic link.
206 .TP
207 .B DT_REG
208 This is a regular file.
209 .TP
210 .B DT_SOCK
211 This is a UNIX domain socket.
212 .TP
213 .B DT_UNKNOWN
214 The file type is unknown.
215 .\" The glibc manual says that on some systems this is the only
216 .\" value returned
217 .PP
218 If the file type could not be determined, the value
219 .B DT_UNKNOWN
220 is returned in
221 .IR d_type .
222
223 Currently,
224 .\" kernel 2.6.27
225 .\" The same sentence is in getdents.2
226 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
227 have full support for returning the file type in
228 .IR d_type .
229 All applications must properly handle a return of
230 .BR DT_UNKNOWN .
231
232 Since POSIX.1 does not specify the size of the
233 .I d_name
234 field, and other nonstandard fields may precede that field within the
235 .I dirent
236 structure, portable applications that use
237 .BR readdir_r ()
238 should allocate the buffer whose address is passed in
239 .IR entry
240 as follows:
241 .in +4n
242 .nf
243
244 name_max = pathconf(dirpath, _PC_NAME_MAX);
245 if (name_max == \-1)         /* Limit not defined, or error */
246     name_max = 255;         /* Take a guess */
247 len = offsetof(struct dirent, d_name) + name_max + 1;
248 entryp = malloc(len);
249
250 .fi
251 .in
252 (POSIX.1 requires that
253 .I d_name
254 is the last field in a
255 .IR "struct dirent" .)
256 .SH SEE ALSO
257 .BR getdents (2),
258 .BR read (2),
259 .BR closedir (3),
260 .BR dirfd (3),
261 .BR ftw (3),
262 .BR offsetof (3),
263 .BR opendir (3),
264 .BR rewinddir (3),
265 .BR scandir (3),
266 .BR seekdir (3),
267 .BR telldir (3)