OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / release / man2 / getdents.2
1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
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 .\" 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>
27 .\"
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>
35 .\"
36 .\"WORD:        directory               ¥Ç¥£¥ì¥¯¥È¥ê
37 .\"WORD:        entry                   ¥¨¥ó¥È¥ê
38 .\"WORD:        memory area             ¥á¥â¥êÎΰè
39 .\"WORD:        parameter               ¥Ñ¥é¥á¡¼¥¿¡¼
40 .\"WORD:        inode number            inode ÈÖ¹æ
41 .\"WORD:        null                    ¥Ì¥ëʸ»ú
42 .\"
43 .TH GETDENTS 2  2009-07-04 "Linux" "Linux Programmer's Manual"
44 .SH Ì¾Á°
45 getdents \- ¥Ç¥£¥ì¥¯¥È¥ê¡¦¥¨¥ó¥È¥ê¤ò¼èÆÀ¤¹¤ë
46 .SH ½ñ¼°
47 .nf
48 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
49 .BI "             unsigned int " count );
50 .fi
51 .SH ÀâÌÀ
52 ¤³¤ì¤Ï¤¢¤Ê¤¿¤Î´Ø¿´¤ò°ú¤¯¤è¤¦¤Ê´Ø¿ô¤Ç¤Ï¤Ê¤¤¡£
53 POSIX ½àµò¤Î C ¥é¥¤¥Ö¥é¥ê¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤Ä¤¤¤Æ¤Ï
54 .BR readdir (3)
55 ¤ò¸«¤ë¤³¤È¡£
56 ¤³¤Î¥Ú¡¼¥¸¤Ï¡¢¥«¡¼¥Í¥ë¥·¥¹¥Æ¥à¥³¡¼¥ë¤ÎÀ¸¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ë¤Ä¤¤¤Æ
57 µ­ºÜ¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
58 .PP
59 .BR getdents ()
60 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¡¢¥ª¡¼¥×¥óºÑ¤ß¤Î¥Õ¥¡¥¤¥ëµ­½Ò»Ò
61 .I fd
62 ¤Ç»²¾È¤µ¤ì¤ë¥Ç¥£¥ì¥¯¥È¥ê¤«¤é
63 .I linux_dirent
64 ¹½Â¤ÂΤò¤¤¤¯¤Ä¤«Æɤ߽Ф·¡¢
65 .I dirp
66 ¤¬»Ø¤·¤Æ¤¤¤ë¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤¹¤ë¡£
67 .I count
68 °ú¤­¿ô¤Ï¤½¤Î¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤ò¼¨¤¹¡£
69 .PP
70 .I linux_dirent
71 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤ËÀë¸À¤µ¤ì¤Æ¤¤¤ë:
72 .PP
73 .in +4n
74 .nf
75 struct linux_dirent {
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) */
82     /*
83     char           pad;       // Zero padding byte
84     char           d_type;    // File type (only since Linux 2.6.4;
85                               // offset is (d_reclen \- 1))
86     */
87
88 }
89 .fi
90 .in
91 .PP
92 .I d_ino
93 ¤Ï inode ÈÖ¹æ¤Ç¤¢¤ë¡£
94 .I d_off
95 ¤Ï¥Ç¥£¥ì¥¯¥È¥ê¤ÎÀèƬ¤«¤é¼¡¤Î
96 .I linux_dirent
97 ¤ÎÀèƬ¤Þ¤Ç¤Îµ÷Î¥¤Ç¤¢¤ë¡£
98 .I d_reclen
99 ¤Ï¤³¤Î
100 .I linux_dirent
101 Á´ÂΤΥµ¥¤¥º¤Ç¤¢¤ë¡£
102 .I d_name
103 ¤Ï¥Ì¥ë(null)ʸ»ú¤Ç½ª¤ï¤ë¥Õ¥¡¥¤¥ë̾¤Ç¤¢¤ë¡£
104
105 .I d_type
106 ¤Ï¡¢¹½Â¤ÂΤκǸå¤Î¥Ð¥¤¥È¤Ç¤¢¤ê¡¢¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤ò¼¨¤¹¡£
107 .I d_type
108 ¤Ï°Ê²¼¤ÎÃͤΰì¤Ä¤ò¼è¤ë
109 .RI ( <dirent.h>
110 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë)¡£
111 .TP 12
112 .B DT_BLK
113 ¥Ö¥í¥Ã¥¯¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
114 .TP
115 .B DT_CHR
116 ¥­¥ã¥é¥¯¥¿¥Ç¥Ð¥¤¥¹¤Ç¤¢¤ë¡£
117 .TP
118 .B DT_DIR
119 ¥Ç¥£¥ì¥¯¥È¥ê¤Ç¤¢¤ë¡£
120 .TP
121 .B DT_FIFO
122 ̾Á°¤Ä¤­¥Ñ¥¤¥× (FIFO) ¤Ç¤¢¤ë¡£
123 .TP
124 .B DT_LNK
125 ¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¤¢¤ë¡£
126 .TP
127 .B DT_REG
128 Ä̾ï¤Î¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë¡£
129 .TP
130 .B DT_SOCK
131 Unix ¥É¥á¥¤¥ó¥½¥±¥Ã¥È¤Ç¤¢¤ë¡£
132 .TP
133 .B DT_UNKNOWN
134 ¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤ÏÉÔÌÀ¤Ç¤¢¤ë¡£
135 .PP
136 .I d_type
137 ¥Õ¥£¡¼¥ë¥É¤Ï Linux 2.6.4 ¤«¤é¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
138 ¤³¤ì¤Ï
139 .I linux_dirent
140 ¹½Â¤ÂΤΤ¦¤Á¡¢°ÊÁ°¤Ï¥¼¥í¤ÇËä¤á¤é¤ì¤Æ¤¤¤¿¶õ´Ö¤ËÇÛÃÖ¤µ¤ì¤Æ¤¤¤ë¡£
141 ½¾¤Ã¤Æ¡¢2.6.3 °ÊÁ°¤Î¥«¡¼¥Í¥ë¤Ç¤Ï¡¢¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ë¥¢¥¯¥»¥¹¤·¤è¤¦¤È¤¹¤ë¤È
142 ¾ï¤ËÃÍ 0
143 .RB ( DT_UNKNOWN )
144 ¤¬ÊÖ¤µ¤ì¤ë¡£
145 .PP
146 ¸½ºß¤Î¤È¤³¤í¡¢
147 .\" ¥«¡¼¥Í¥ë 2.6.27
148 .\" Æ±¤¸ÀâÌÀʸ¤¬ readdir.2 ¤Ë¤â¤¢¤ë¡£
149 .I d_type
150 ¤Ç¥Õ¥¡¥¤¥ë¥¿¥¤¥×¤òÊÖ¤¹µ¡Ç½¤¬´°Á´¤Ë¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤Î¤Ï¡¢
151 ¤¤¤¯¤Ä¤«¤Î¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤Ë¤ª¤¤¤Æ¤Î¤ß¤Ç¤¢¤ë
152 (Btrfs, ext2, ext3, ext4 ¤Ï¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë)¡£
153 ¤É¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤â¡¢
154 .B DT_UNKNOWN
155 ¤¬ÊÖ¤µ¤ì¤¿ºÝ¤ËŬÀڤ˽èÍý¤Ç¤­¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
156 .SH ÊÖ¤êÃÍ
157 À®¸ù¤·¤¿¾ì¹ç¤Ï¡¢Æɤ߹þ¤ó¤À¥Ð¥¤¥È¿ô¤¬ÊÖ¤µ¤ì¤ë¡£
158 ¥Ç¥£¥ì¥¯¥È¥ê¤Î½ª¤ï¤ê¤Ê¤é¤Ð 0 ¤¬ÊÖ¤µ¤ì¤ë¡£
159 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï \-1 ¤òÊÖ¤µ¤ì¡¢
160 .I errno
161 ¤ËŬÀÚ¤ÊÃͤ¬ÀßÄꤵ¤ì¤ë¡£
162 .SH ¥¨¥é¡¼
163 .TP
164 .B EBADF
165 ¥Õ¥¡¥¤¥ëµ­½Ò»Ò
166 .I fd
167 ¤Ï̵¸ú¤Ç¤¢¤ë¡£
168 .TP
169 .B EFAULT
170 °ú¤­¿ô¤¬¡¢¸Æ¤Ó½Ð¤·¤¿¥×¥í¥»¥¹¤Î¥¢¥É¥ì¥¹¶õ´Ö³°¤ò»Ø¤·¤Æ¤¤¤ë¡£
171 .TP
172 .B EINVAL
173 ·ë²ÌÍѤΥХåե¡¡¼¤¬¾®¤µ¤¹¤®¤ë¡£
174 .TP
175 .B ENOENT
176 ¤½¤Î¤è¤¦¤Ê¥Ç¥£¥ì¥¯¥È¥ê¤Ï¸ºß¤·¤Ê¤¤¡£
177 .TP
178 .B ENOTDIR
179 ¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤¬¥Ç¥£¥ì¥¯¥È¥ê¤ò»²¾È¤·¤Æ¤¤¤Ê¤¤¡£
180 .SH ½àµò
181 SVr4.
182 .\" SVr4 ¤Ç¤Ï¡¢Â¾¤Ë ENOLINK, EIO ¥¨¥é¡¼¾õÂ֤ˤĤ¤¤Æ¤âµ­½Ò¤µ¤ì¤Æ¤¤¤ë¡£
183 .SH Ãí°Õ
184 glibc ¤Ï¤³¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÂФ¹¤ë¥é¥Ã¥Ñ¡¼´Ø¿ô¤òÄ󶡤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢
185 .BR syscall (2)
186 ¤ò»È¤Ã¤Æ¸Æ¤Ó½Ð¤¹¤³¤È¡£
187 .I linux_dirent
188 ¹½Â¤ÂΤϼ«Ê¬¤ÇÄêµÁ¤¹¤ëɬÍפ¬¤¢¤ë¡£
189
190 ¤³¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï
191 .BR readdir (2)
192 ¤òÃÖ¤­´¹¤¨¤ë¤â¤Î¤Ç¤¢¤ë¡£
193 .SH Îã
194 ²¼µ­¤Î¥×¥í¥°¥é¥à¤Ï
195 .BR getdents ()
196 ¤Î»ÈÍÑÎã¤ò¼¨¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
197 °Ê²¼¤Ï¡¢¤³¤Î¥×¥í¥°¥é¥à¤ò ext2 ¥Ç¥£¥ì¥¯¥È¥ê¤Ç¼Â¹Ô¤·¤¿ºÝ¤ËÆÀ¤é¤ì¤ë
198 ½ÐÎϤÎÎã¤Ç¤¢¤ë¡£
199
200 .in +4n
201 .nf
202 .RB "$" " ./a.out /testfs/"
203 --------------- nread=120 ---------------
204 i-node#  file type  d_reclen  d_off   d_name
205        2  directory    16         12  .
206        2  directory    16         24  ..
207       11  directory    24         44  lost+found
208       12  regular      16         56  a
209   228929  directory    16         68  sub
210    16353  directory    16         80  sub2
211   130817  directory    16       4096  sub3
212 .fi
213 .in
214 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
215 \&
216 .nf
217 #define _GNU_SOURCE
218 #include <dirent.h>     /* Defines DT_* constants */
219 #include <fcntl.h>
220 #include <stdio.h>
221 #include <unistd.h>
222 #include <stdlib.h>
223 #include <sys/stat.h>
224 #include <sys/syscall.h>
225
226 #define handle_error(msg) \\
227         do { perror(msg); exit(EXIT_FAILURE); } while (0)
228
229 struct linux_dirent {
230     long           d_ino;
231     off_t          d_off;
232     unsigned short d_reclen;
233     char           d_name[];
234 };
235
236 #define BUF_SIZE 1024
237
238 int
239 main(int argc, char *argv[])
240 {
241     int fd, nread;
242     char buf[BUF_SIZE];
243     struct linux_dirent *d;
244     int bpos;
245     char d_type;
246
247     fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
248     if (fd == \-1)
249         handle_error("open");
250
251     for ( ; ; ) {
252         nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
253         if (nread == \-1)
254             handle_error("getdents");
255
256         if (nread == 0)
257             break;
258
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;
275         }
276     }
277
278     exit(EXIT_SUCCESS);
279 }
280 .fi
281 .SH ´ØÏ¢¹àÌÜ
282 .BR readdir (2),
283 .BR readdir (3)