1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
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 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
24 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
25 .\" Derived from 'readdir.2'.
26 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
29 .\" all rights reserved.
30 .\" Translated Sat Feb 22 20:15:56 JST 1997
31 .\" by HANATAKA Shinya <hanataka@abyss.rim.or.jp>
32 .\" Updated Sun Oct 12 JST 2003 by Kentaro Shirakata <argrath@ub32.org>
33 .\" Updated Wed Jul 30 JST 2008 by Kentaro Shirakata <argrath@ub32.org>
34 .\" Updated 2009-02-12 by Kentaro Shirakata <argrath@ub32.org>
36 .\"WORD: directory ¥Ç¥£¥ì¥¯¥È¥ê
37 .\"WORD: entry ¥¨¥ó¥È¥ê
38 .\"WORD: memory area ¥á¥â¥êÎΰè
39 .\"WORD: parameter ¥Ñ¥é¥á¡¼¥¿¡¼
40 .\"WORD: inode number inode ÈÖ¹æ
41 .\"WORD: null ¥Ì¥ëʸ»ú
43 .TH GETDENTS 2 2009-07-04 "Linux" "Linux Programmer's Manual"
45 getdents \- ¥Ç¥£¥ì¥¯¥È¥ê¡¦¥¨¥ó¥È¥ê¤ò¼èÆÀ¤¹¤ë
48 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
49 .BI " unsigned int " count );
52 ¤³¤ì¤Ï¤¢¤Ê¤¿¤Î´Ø¿´¤ò°ú¤¯¤è¤¦¤Ê´Ø¿ô¤Ç¤Ï¤Ê¤¤¡£
53 POSIX ½àµò¤Î C ¥é¥¤¥Ö¥é¥ê¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤Ä¤¤¤Æ¤Ï
56 ¤³¤Î¥Ú¡¼¥¸¤Ï¡¢¥«¡¼¥Í¥ë¥·¥¹¥Æ¥à¥³¡¼¥ë¤ÎÀ¸¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤Ä¤¤¤Æ
60 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¡¢¥ª¡¼¥×¥óºÑ¤ß¤Î¥Õ¥¡¥¤¥ëµ½Ò»Ò
62 ¤Ç»²¾È¤µ¤ì¤ë¥Ç¥£¥ì¥¯¥È¥ê¤«¤é
64 ¹½Â¤ÂΤò¤¤¤¯¤Ä¤«Æɤ߽Ф·¡¢
66 ¤¬»Ø¤·¤Æ¤¤¤ë¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤¹¤ë¡£
68 °ú¤¿ô¤Ï¤½¤Î¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤ò¼¨¤¹¡£
71 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤ËÀë¸À¤µ¤ì¤Æ¤¤¤ë:
76 unsigned long d_ino; /* Inode number */
77 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
78 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
79 char d_name[]; /* Filename (null-terminated) */
80 /* length is actually (d_reclen \- 2 \-
81 offsetof(struct linux_dirent, d_name) */
83 char pad; // Zero padding byte
84 char d_type; // File type (only since Linux 2.6.4;
85 // offset is (d_reclen \- 1))
95 ¤Ï¥Ç¥£¥ì¥¯¥È¥ê¤ÎÀèƬ¤«¤é¼¡¤Î
97 ¤ÎÀèƬ¤Þ¤Ç¤Îµ÷Î¥¤Ç¤¢¤ë¡£
103 ¤Ï¥Ì¥ë(null)ʸ»ú¤Ç½ª¤ï¤ë¥Õ¥¡¥¤¥ë̾¤Ç¤¢¤ë¡£
106 ¤Ï¡¢¹½Â¤ÂΤκǸå¤Î¥Ð¥¤¥È¤Ç¤¢¤ê¡¢¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤ò¼¨¤¹¡£
108 ¤Ï°Ê²¼¤ÎÃͤΰì¤Ä¤ò¼è¤ë
113 ¥Ö¥í¥Ã¥¯¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
116 ¥¥ã¥é¥¯¥¿¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
122 ̾Á°¤Ä¤¥Ñ¥¤¥× (FIFO) ¤Ç¤¢¤ë¡£
125 ¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¤¢¤ë¡£
128 Ä̾ï¤Î¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë¡£
131 Unix ¥É¥á¥¤¥ó¥½¥±¥Ã¥È¤Ç¤¢¤ë¡£
134 ¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤ÏÉÔÌÀ¤Ç¤¢¤ë¡£
137 ¥Õ¥£¡¼¥ë¥É¤Ï Linux 2.6.4 ¤«¤é¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
140 ¹½Â¤ÂΤΤ¦¤Á¡¢°ÊÁ°¤Ï¥¼¥í¤ÇËä¤á¤é¤ì¤Æ¤¤¤¿¶õ´Ö¤ËÇÛÃÖ¤µ¤ì¤Æ¤¤¤ë¡£
141 ½¾¤Ã¤Æ¡¢2.6.3 °ÊÁ°¤Î¥«¡¼¥Í¥ë¤Ç¤Ï¡¢¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë¥¢¥¯¥»¥¹¤·¤è¤¦¤È¤¹¤ë¤È
148 .\" Ʊ¤¸ÀâÌÀʸ¤¬ readdir.2 ¤Ë¤â¤¢¤ë¡£
150 ¤Ç¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤òÊÖ¤¹µ¡Ç½¤¬´°Á´¤Ë¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤Î¤Ï¡¢
151 ¤¤¤¯¤Ä¤«¤Î¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤Ë¤ª¤¤¤Æ¤Î¤ß¤Ç¤¢¤ë
152 (Btrfs, ext2, ext3, ext4 ¤Ï¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë)¡£
153 ¤É¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤â¡¢
155 ¤¬ÊÖ¤µ¤ì¤¿ºÝ¤ËŬÀڤ˽èÍý¤Ç¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
157 À®¸ù¤·¤¿¾ì¹ç¤Ï¡¢Æɤ߹þ¤ó¤À¥Ð¥¤¥È¿ô¤¬ÊÖ¤µ¤ì¤ë¡£
158 ¥Ç¥£¥ì¥¯¥È¥ê¤Î½ª¤ï¤ê¤Ê¤é¤Ð 0 ¤¬ÊÖ¤µ¤ì¤ë¡£
159 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï \-1 ¤òÊÖ¤µ¤ì¡¢
161 ¤ËŬÀÚ¤ÊÃͤ¬ÀßÄꤵ¤ì¤ë¡£
170 °ú¤¿ô¤¬¡¢¸Æ¤Ó½Ð¤·¤¿¥×¥í¥»¥¹¤Î¥¢¥É¥ì¥¹¶õ´Ö³°¤ò»Ø¤·¤Æ¤¤¤ë¡£
173 ·ë²ÌÍѤΥХåե¡¡¼¤¬¾®¤µ¤¹¤®¤ë¡£
176 ¤½¤Î¤è¤¦¤Ê¥Ç¥£¥ì¥¯¥È¥ê¤Ï¸ºß¤·¤Ê¤¤¡£
179 ¥Õ¥¡¥¤¥ëµ½Ò»Ò¤¬¥Ç¥£¥ì¥¯¥È¥ê¤ò»²¾È¤·¤Æ¤¤¤Ê¤¤¡£
182 .\" SVr4 ¤Ç¤Ï¡¢Â¾¤Ë ENOLINK, EIO ¥¨¥é¡¼¾õÂ֤ˤĤ¤¤Æ¤âµ½Ò¤µ¤ì¤Æ¤¤¤ë¡£
184 glibc ¤Ï¤³¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÂФ¹¤ë¥é¥Ã¥Ñ¡¼´Ø¿ô¤òÄ󶡤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢
186 ¤ò»È¤Ã¤Æ¸Æ¤Ó½Ð¤¹¤³¤È¡£
188 ¹½Â¤ÂΤϼ«Ê¬¤ÇÄêµÁ¤¹¤ëɬÍפ¬¤¢¤ë¡£
192 ¤òÃÖ¤´¹¤¨¤ë¤â¤Î¤Ç¤¢¤ë¡£
196 ¤Î»ÈÍÑÎã¤ò¼¨¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
197 °Ê²¼¤Ï¡¢¤³¤Î¥×¥í¥°¥é¥à¤ò ext2 ¥Ç¥£¥ì¥¯¥È¥ê¤Ç¼Â¹Ô¤·¤¿ºÝ¤ËÆÀ¤é¤ì¤ë
202 .RB "$" " ./a.out /testfs/"
203 --------------- nread=120 ---------------
204 i-node# file type d_reclen d_off d_name
207 11 directory 24 44 lost+found
209 228929 directory 16 68 sub
210 16353 directory 16 80 sub2
211 130817 directory 16 4096 sub3
214 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
218 #include <dirent.h> /* Defines DT_* constants */
223 #include <sys/stat.h>
224 #include <sys/syscall.h>
226 #define handle_error(msg) \\
227 do { perror(msg); exit(EXIT_FAILURE); } while (0)
229 struct linux_dirent {
232 unsigned short d_reclen;
236 #define BUF_SIZE 1024
239 main(int argc, char *argv[])
243 struct linux_dirent *d;
247 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
249 handle_error("open");
252 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
254 handle_error("getdents");
259 printf("\--------------- nread=%d ---------------\\n", nread);
260 printf("i\-node# file type d_reclen d_off d_name\\n");
261 for (bpos = 0; bpos < nread;) {
262 d = (struct linux_dirent *) (buf + bpos);
263 printf("%8ld ", d\->d_ino);
264 d_type = *(buf + bpos + d\->d_reclen - 1);
265 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
266 (d_type == DT_DIR) ? "directory" :
267 (d_type == DT_FIFO) ? "FIFO" :
268 (d_type == DT_SOCK) ? "socket" :
269 (d_type == DT_LNK) ? "symlink" :
270 (d_type == DT_BLK) ? "block dev" :
271 (d_type == DT_CHR) ? "char dev" : "???");
272 printf("%4d %10lld %s\\n", d\->d_reclen,
273 (long long) d\->d_off, (char *) d->d_name);
274 bpos += d\->d_reclen;