1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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().
34 .\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka
35 .\" all rights reserved.
36 .\" Translated 1997-12-24, HIROFUMI Nishizuka <nishi@rpts.cl.nec.co.jp>
37 .\" Updated & Modified 2002-03-24, Yuichi SATO <ysato@h4.dion.ne.jp>
38 .\" Updated & Modified 2005-01-16, Yuichi SATO <ysato444@yahoo.co.jp>
39 .\" Updated & Modified 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
40 .\" Updated 2008-08-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
42 .TH READDIR 3 2010-09-10 "" "Linux Programmer's Manual"
45 .\"O readdir, readdir_r \- read a directory
46 readdir, readdir_r \- ¥Ç¥£¥ì¥¯¥È¥ê¤òÆɤ߹þ¤à
50 .B #include <dirent.h>
52 .BI "struct dirent *readdir(DIR *" dirp );
54 .BI "int readdir_r(DIR *" dirp ", struct dirent *" entry \
55 ", struct dirent **" result );
59 .\"O Feature Test Macro Requirements for glibc (see
60 .\"O .BR feature_test_macros (7)):
61 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
62 .RB ( feature_test_macros (7)
69 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
70 _SVID_SOURCE || _POSIX_SOURCE
77 .\"O function returns a pointer to a \fIdirent\fP structure
78 .\"O representing the next directory entry in the directory stream pointed
79 .\"O to by \fIdirp\fP.
80 .\"O It returns NULL on reaching the end of the directory stream or if
81 .\"O an error occurred.
83 ´Ø¿ô¤Ï¡¢\fIdirp\fP ¤¬»Ø¤¹¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¤ÎÃæ¤Ç¡¢
84 ¼¡¤Î¥Ç¥£¥ì¥¯¥È¥ê¥¨¥ó¥È¥ê¤òɽ¤¹ \fIdirent\fP ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹¡£
85 ¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤Ë㤷¤¿¾ì¹ç¤ä¡¢
86 ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï¡¢ NULL ¤òÊÖ¤¹¡£
90 .\"O structure is defined as follows:
93 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
98 .\"O ino_t d_ino; /* inode number */
99 .\"O off_t d_off; /* offset to the next dirent */
100 .\"O unsigned short d_reclen; /* length of this record */
101 .\"O unsigned char d_type; /* type of file; not supported
102 .\"O by all file system types */
103 .\"O char d_name[256]; /* filename */
104 ino_t d_ino; /* inode ÈÖ¹æ */
105 off_t d_off; /* ¼¡¤Î dirent ¤Ø¤Î¥ª¥Õ¥»¥Ã¥È */
106 unsigned short d_reclen; /* ¤³¤Î¥ì¥³¡¼¥É¤ÎŤµ */
107 unsigned char d_type; /* ¥Õ¥¡¥¤¥ë¼ïÊÌ¡£Á´¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à */
108 ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤ */
109 char d_name[256]; /* ¥Õ¥¡¥¤¥ë̾ */
114 .\"O The only fields in the
116 .\"O structure that are mandated by POSIX.1 are:
118 .\"O of unspecified size, with at most
120 .\"O characters preceding the terminating null byte;
121 .\"O and (as an XSI extension)
124 ¹½Â¤ÂΤΥե£¡¼¥ë¥É¤Ç POSIX.1 ¤ÇÍ׵ᤵ¤ì¤Æ¤¤¤ë¤Î¤Ï¡¢
130 ¤Ï¤½¤ÎÂ礤µ¤âµ¬Äꤵ¤ì¤Æ¤ª¤é¤º¡¢
131 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë¤ÏºÇÂç¤Ç
133 ¸Ä¤Îʸ»ú¤È¡¢¤½¤ì¤Ë³¤¯½ªÃ¼¤Î NULL ¥Ð¥¤¥È¤¬³ÊǼ¤µ¤ì¤ë¡£
134 .\"O The other fields are unstandardized, and not present on all systems;
135 .\"O see NOTES below for some further details.
136 ¾¤Î¥Õ¥£¡¼¥ë¥É¤ÏÈóɸ½à¤Ç¤¢¤ê¡¢Á´¤Æ¤Î¥·¥¹¥Æ¥à¤Ë¸ºß¤¹¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤¡£
137 ¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï¡¢²¼µ¤Î¡ÖÃí°Õ¡×¤ò»²¾È¤Î¤³¤È¡£
139 .\"O The data returned by
141 .\"O may be overwritten by subsequent calls to
143 .\"O for the same directory stream.
145 ¤Ë¤è¤Ã¤ÆÊÖ¤µ¤ì¤ë¥Ç¡¼¥¿¤Ï¡¢¤½¤ì°Ê¹ß¤ÎƱ¤¸¥¹¥È¥ê¡¼¥à¤ËÂФ¹¤ë
147 ¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤Ã¤Æ¾å½ñ¤¤µ¤ì¤ë²ÄǽÀ¤¬¤¢¤ë¡£
150 .\"O .BR readdir_r ()
151 .\"O function is a reentrant version of
153 .\"O It reads the next directory entry from the directory stream
155 .\"O and returns it in the caller-allocated buffer pointed to by
157 .\"O (See NOTES for information on allocating this buffer.)
158 .\"O A pointer to the returned item is placed in
160 .\"O if the end of the directory stream was encountered,
161 .\"O then NULL is instead returned in
166 ¤Î¥ê¥¨¥ó¥È¥é¥ó¥ÈÈǤǤ¢¤ë¡£
167 ¤³¤Î´Ø¿ô¤Ï¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à
169 ¤«¤é¼¡¤Î¥Ç¥£¥ì¥¯¥È¥ê¥¨¥ó¥È¥ê¤òÆɤ߹þ¤ß¡¢
171 ¤¬»Ø¤¹¸Æ¤Ó½Ð¤·¸µ¤¬³ä¤êÅö¤Æ¤¿¥Ð¥Ã¥Õ¥¡¤Ë¤½¤Î¥¨¥ó¥È¥ê¤ò³ÊǼ¤·¤ÆÊÖ¤¹
172 (¤³¤Î¥Ð¥Ã¥Õ¥¡¤Î³ä¤êÅö¤Æ¤Ë¤Ä¤¤¤Æ¤Ï¡ÖÃí°Õ¡×¤ÎÀá¤ò»²¾È¤Î¤³¤È)¡£
173 ÊÖ¤µ¤ì¤ë¥¨¥ó¥È¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬
175 ¤Ë³ÊǼ¤µ¤ì¤ë¡£¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤Ë㤷¤¿¾ì¹ç¤Ï¡¢
179 .\"O .SH "RETURN VALUE"
183 .\"O returns a pointer to a
186 .\"O (This structure may be statically allocated; do not attempt to
189 .\"O If the end of the directory stream is reached, NULL is returned and
196 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹¡£
197 (¤³¤Î¹½Â¤ÂΤÏÀÅŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
200 ¤·¤è¤¦¤È¤·¤Ê¤¤¤³¤È¡£)
201 ¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤Ë㤷¤¿¾ì¹ç¤Ë¤Ï¡¢NULL ¤¬ÊÖ¤µ¤ì¡¢
204 .\"O If an error occurs, NULL is returned and
206 .\"O is set appropriately.
207 ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¡¢NULL ¤¬ÊÖ¤µ¤ì¡¢
212 .\"O .BR readdir_r ()
213 .\"O function returns 0 on success.
214 .\"O On error, it returns a positive error number (listed under ERRORS).
215 .\"O If the end of the directory stream is reached,
216 .\"O .BR readdir_r ()
217 .\"O returns 0, and returns NULL in
222 ¥¨¥é¡¼¤Î¾ì¹ç¡¢(¡Ö¥¨¥é¡¼¡×¤ÎÀá¤Î¥ê¥¹¥È¤ËºÜ¤Ã¤Æ¤¤¤ë) Àµ¤Î¥¨¥é¡¼ÈÖ¹æ¤òÊÖ¤¹¡£
223 ¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤Ë㤷¤¿¾ì¹ç¡¢
225 ¤ÏÊÖ¤êÃͤȤ·¤Æ 0 ¤òÊÖ¤·¡¢
232 .\"O Invalid directory stream descriptor \fIdirp\fP.
233 ¥Ç¥£¥ì¥¯¥È¥ê¥¹¥È¥ê¡¼¥à¥Ç¥£¥¹¥¯¥ê¥×¥¿ \fIdirp\fP ¤¬Ìµ¸ú¡£
234 .\"O .SH "CONFORMING TO"
236 SVr4, 4.3BSD, POSIX.1-2001.
243 .\"O are specified in POSIX.1-2001.
244 .\"O The remaining fields are available on many, but not all systems.
246 .\"O programs can check for the availability of the fields not defined
247 .\"O in POSIX.1 by testing whether the macros
248 .\"O .BR _DIRENT_HAVE_D_NAMLEN ,
249 .\"O .BR _DIRENT_HAVE_D_RECLEN ,
250 .\"O .BR _DIRENT_HAVE_D_OFF ,
252 .\"O .B _DIRENT_HAVE_D_TYPE
258 ¤À¤±¤¬ POSIX.1-2001 ¤Çµ¬Äꤵ¤ì¤Æ¤¤¤ë¡£
259 »Ä¤ê¤Î¥Õ¥£¡¼¥ë¥É¤Ï¿¤¯¤Î¥·¥¹¥Æ¥à¤Ë¸ºß¤¹¤ë¤¬¡¢Á´¤Æ¤Î¥·¥¹¥Æ¥à¤Ë
260 ¸ºß¤¹¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤¡£
261 glibc ¤Ç¤Ï¡¢¥×¥í¥°¥é¥à¤¬ POSIX.1 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¥Õ¥£¡¼¥ë¥É¤¬
262 ÍøÍѤǤ¤ë¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
263 ¥Á¥§¥Ã¥¯¤¹¤ë¤Ë¤Ï¡¢¥Þ¥¯¥í
264 .BR _DIRENT_HAVE_D_NAMLEN ,
265 .BR _DIRENT_HAVE_D_RECLEN ,
266 .BR _DIRENT_HAVE_D_OFF ,
267 .B _DIRENT_HAVE_D_TYPE
268 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¤«¤ò¥Æ¥¹¥È¤¹¤ì¤Ð¤è¤¤¡£
270 .\"O Other than Linux, the
272 .\"O field is available mainly only on BSD systems.
274 ¥Õ¥£¡¼¥ë¥É¤Ï¡¢Linux °Ê³°¤Ç¤Ï¡¢
275 ¼ç¤Ë BSD ·Ï¤Î¥·¥¹¥Æ¥à¤Ë¤À¤±Â¸ºß¤¹¤ë¡£
276 .\"O This field makes it possible to avoid the expense of calling
278 .\"O if further actions depend on the type of the file.
279 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤ò»È¤¦¤È¡¢
280 ¤½¤Î¸å¤ÎÆ°ºî¤¬¥Õ¥¡¥¤¥ë¤Î¼ïÊ̤ˤè¤ê·è¤Þ¤ë¾ì¹ç¤Ë¡¢
282 ¤ò¸Æ¤Ó½Ð¤¹¥³¥¹¥È¤òÈò¤±¤ë¤³¤È¤¬¤Ç¤¤ë¡£
285 .\"O feature test macro is defined,
286 .\"O then glibc defines the following macro constants
287 .\"O for the value returned in
291 ¤¬ÄêµÁ¤µ¤ì¤¿¾ì¹ç¡¢glibc ¤Ï
293 ¤ÇÊÖ¤µ¤ì¤ëÃͤȤ·¤Æ°Ê²¼¤Î¥Þ¥¯¥íÄê¿ô¤òÄêµÁ¤¹¤ë¡£
296 .\"O This is a block device.
297 ¥Ö¥í¥Ã¥¯¡¦¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
300 .\"O This is a character device.
301 ¥¥ã¥é¥¯¥¿¡¦¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
304 .\"O This is a directory.
308 .\"O This is a named pipe (FIFO).
309 ̾Á°ÉÕ¤¥Ñ¥¤¥× (FIFO) ¤Ç¤¢¤ë¡£
312 .\"O This is a symbolic link.
313 ¥·¥ó¥Ü¥ê¥Ã¥¯¡¦¥ê¥ó¥¯¤Ç¤¢¤ë¡£
316 .\"O This is a regular file.
317 Ä̾ï¤Î¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë¡£
320 .\"O This is a UNIX domain socket.
321 UNIX ¥É¥á¥¤¥ó¥½¥±¥Ã¥È¤Ç¤¢¤ë¡£
324 .\"O The file type is unknown.
325 ¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤¬ÉÔÌÀ¤Ç¤¢¤ë¡£
326 .\" The glibc manual says that on some systems this is the only
329 .\"O If the file type could not be determined, the value
333 ¥Õ¥¡¥¤¥ë¼ïÊ̤ò·èÄê¤Ç¤¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢
340 .\"O .\" kernel 2.6.27
341 .\"O .\" The same sentence is in readdir.2
342 .\"O only some file systems (among them: Btrfs, ext2, etx3, and ext4)
343 .\"O have full support returning the file type in
345 .\"O All applications must properly handle a return of
346 .\"O .BR DT_UNKNOWN .
349 .\" Ʊ¤¸ÀâÌÀʸ¤¬ readdir.2 ¤Ë¤â¤¢¤ë¡£
351 ¤Ç¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤òÊÖ¤¹µ¡Ç½¤¬´°Á´¤Ë¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤Î¤Ï¡¢
352 ¤¤¤¯¤Ä¤«¤Î¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤Ë¤ª¤¤¤Æ¤Î¤ß¤Ç¤¢¤ë
353 (Btrfs, ext2, ext3, ext4 ¤Ï¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë)¡£
354 ¤É¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤â¡¢
356 ¤¬ÊÖ¤µ¤ì¤¿ºÝ¤ËŬÀڤ˽èÍý¤Ç¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
358 .\"O Since POSIX.1 does not specify the size of the
360 .\"O field, and other nonstandard fields may precede that field within the
362 .\"O structure, portable applications that use
363 .\"O .BR readdir_r ()
364 .\"O should allocate the buffer whose address is passed in
369 ¥Õ¥£¡¼¥ë¥É¤Î¥µ¥¤¥º¤Ïµ¬Äꤵ¤ì¤Æ¤ª¤é¤º¡¢
373 ¤Î¸å¤í¤Ë¾¤ÎÈóɸ½à¤Î¥Õ¥£¡¼¥ë¥É¤¬¤¢¤ë¤«¤â¤·¤ì¤Ê¤¤¤Î¤Ç¡¢
374 °Ü¿¢À¤¬É¬Íפʥ¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç
378 ¤ËÅϤ¹¥Ð¥Ã¥Õ¥¡¤ò¼¡¤Î¤è¤¦¤Ë¤·¤Æ³ä¤êÅö¤Æ¤ë¤Ù¤¤Ç¤¢¤ë¡£
382 len = offsetof(struct dirent, d_name) +
383 pathconf(dirpath, _PC_NAME_MAX) + 1
384 entryp = malloc(len);
388 .\"O (POSIX.1 requires that
390 .\"O is the last field in a
391 .\"O .IR "struct dirent" .)
396 ¤Ç¤¢¤ë¤³¤È¤òÍ׵ᤷ¤Æ¤¤¤ë¡£)