OSDN Git Service

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