OSDN Git Service

(split) LDP: Update release pages
[linuxjm/LDP_man-pages.git] / release / man2 / getdents.2
1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
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 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
26 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
27 .\"   Derived from 'readdir.2'.
28 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
29 .\"
30 .\"*******************************************************************
31 .\"
32 .\" This file was generated with po4a. Translate the source file.
33 .\"
34 .\"*******************************************************************
35 .\"
36 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
37 .\"         all rights reserved.
38 .\" Translated Sat Feb 22 20:15:56 JST 1997
39 .\"         by HANATAKA Shinya <hanataka@abyss.rim.or.jp>
40 .\" Updated Sun Oct 12 JST 2003 by Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated Wed Jul 30 JST 2008 by Kentaro Shirakata <argrath@ub32.org>
42 .\" Updated 2009-02-12 by Kentaro Shirakata <argrath@ub32.org>
43 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
44 .\" Updated 2013-03-26, Akihiro MOTOKI <amotoki@gmail.com>
45 .\"
46 .TH GETDENTS 2 2012\-08\-03 Linux "Linux Programmer's Manual"
47 .SH 名前
48 getdents \- ディレクトリ・エントリを取得する
49 .SH 書式
50 .nf
51 \fBint getdents(unsigned int \fP\fIfd\fP\fB, struct linux_dirent *\fP\fIdirp\fP\fB,\fP
52 \fB             unsigned int \fP\fIcount\fP\fB);\fP
53 .fi
54
55 \fI注\fP: このシステムコールには glibc のラッパー関数は存在しない。「注意」の節を参照。
56 .SH 説明
57 これはあなたの関心を引くような関数ではない。 POSIX 準拠の C ライブラリインターフェースについては \fBreaddir\fP(3)  を見ること。
58 このページは、カーネルシステムコールの生のインターフェースについて 記載したものである。
59 .PP
60 \fBgetdents\fP()  システムコールは、オープン済みのファイルディスクリプタ \fIfd\fP で参照されるディレクトリから
61 \fIlinux_dirent\fP 構造体をいくつか読み出し、 \fIdirp\fP が指しているバッファに格納する。 \fIcount\fP
62 引き数はそのバッファのサイズを示す。
63 .PP
64 \fIlinux_dirent\fP 構造体は以下のように宣言されている:
65 .PP
66 .in +4n
67 .nf
68 struct linux_dirent {
69     unsigned long  d_ino;     /* Inode number */
70     unsigned long  d_off;     /* Offset to next \fIlinux_dirent\fP */
71     unsigned short d_reclen;  /* Length of this \fIlinux_dirent\fP */
72     char           d_name[];  /* Filename (null\-terminated) */
73                       /* length is actually (d_reclen \- 2 \-
74                          offsetof(struct linux_dirent, d_name)) */
75     /*
76     char           pad;       // Zero padding byte
77     char           d_type;    // File type (only since Linux
78                               // 2.6.4); offset is (d_reclen \- 1)
79     */
80
81 }
82 .fi
83 .in
84 .PP
85 \fId_ino\fP は inode 番号である。 \fId_off\fP はディレクトリの先頭から次の \fIlinux_dirent\fP の先頭までの距離である。
86 \fId_reclen\fP はこの \fIlinux_dirent\fP 全体のサイズである。 \fId_name\fP
87 はヌル(null)文字で終わるファイル名である。
88
89 \fId_type\fP は、構造体の最後のバイトであり、ファイルタイプを示す。 \fId_type\fP は以下の値の一つを取る
90 (\fI<dirent.h>\fP で定義されている)。
91 .TP  12
92 \fBDT_BLK\fP
93 ブロックデバイスである。
94 .TP 
95 \fBDT_CHR\fP
96 キャラクタデバイスである。
97 .TP 
98 \fBDT_DIR\fP
99 ディレクトリである。
100 .TP 
101 \fBDT_FIFO\fP
102 名前付きパイプ (FIFO) である。
103 .TP 
104 \fBDT_LNK\fP
105 シンボリックリンクである。
106 .TP 
107 \fBDT_REG\fP
108 通常のファイルである。
109 .TP 
110 \fBDT_SOCK\fP
111 UNIX ドメインソケットである。
112 .TP 
113 \fBDT_UNKNOWN\fP
114 ファイルタイプが不明である。
115 .PP
116 \fId_type\fP フィールドは Linux 2.6.4 から実装されている。 これは \fIlinux_dirent\fP
117 構造体のうち、以前はゼロで埋められていた空間に配置されている。 従って、2.6.3 以前のカーネルでは、このフィールドにアクセスしようとすると 常に値
118 0 (\fBDT_UNKNOWN\fP)  が返される。
119 .PP
120 .\" kernel 2.6.27
121 .\" The same sentence is in readdir.2
122 現在のところ、 \fId_type\fP でファイルタイプを返す機能が完全にサポートされているのは、 いくつかのファイルシステムにおいてのみである
123 (Btrfs, ext2, ext3, ext4 はサポートしている)。 どのアプリケーションも、 \fBDT_UNKNOWN\fP
124 が返された際に適切に処理できなければならない。
125 .SH 返り値
126 成功した場合は、読み込んだバイト数が返される。 ディレクトリの終わりならば 0 が返される。 エラーの場合は \-1 を返され、 \fIerrno\fP
127 に適切な値が設定される。
128 .SH エラー
129 .TP 
130 \fBEBADF\fP
131 ファイルディスクリプタ \fIfd\fP が不正である。
132 .TP 
133 \fBEFAULT\fP
134 引き数が呼び出したプロセスのアドレス空間外を指している。
135 .TP 
136 \fBEINVAL\fP
137 結果用のバッファーが小さすぎる。
138 .TP 
139 \fBENOENT\fP
140 そのようなディレクトリは存在しない。
141 .TP 
142 \fBENOTDIR\fP
143 ファイルディスクリプタがディレクトリを参照していない。
144 .SH 準拠
145 .\" SVr4 documents additional ENOLINK, EIO error conditions.
146 SVr4.
147 .SH 注意
148 glibc はこのシステムコールに対するラッパー関数を提供していないので、 \fBsyscall\fP(2)  を使って呼び出すこと。
149 \fIlinux_dirent\fP 構造体は自分で定義する必要がある。しかし、たいていはこのシステムコールではなく \fBreaddir\fP(3)
150 を使うべき場面のことが多い。
151
152 このシステムコールは \fBreaddir\fP(2)  を置き換えるものである。
153
154 元々の Linux の \fBgetdents\fP() システムコールは、大きなファイルシステムと
155 大きなファイルオフセットを扱うことができなかった。
156 その結果、Linux 2.4 で \fBgetdents64\fP() が追加された。
157 \fBgetdents64\fP() では、\fIlinux_dirent\fP 構造体のフィールド \fId_ino\fP と
158 \fId_off\fP でビット幅の大きなデータ型が使われている。
159 .SH 例
160 .\" FIXME: This program uses the older getdents() system call
161 .\" and the structure with smaller field widths.
162 下記のプログラムは \fBgetdents\fP()  の使用例を示したものである。 以下は、このプログラムを ext2 ディレクトリで実行した際に得られる
163 出力の例である。
164
165 .in +4n
166 .nf
167 $\fB ./a.out /testfs/\fP
168 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=120 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
169 i\-node#  file type  d_reclen  d_off   d_name
170        2  directory    16         12  .
171        2  directory    16         24  ..
172       11  directory    24         44  lost+found
173       12  regular      16         56  a
174   228929  directory    16         68  sub
175    16353  directory    16         80  sub2
176   130817  directory    16       4096  sub3
177 .fi
178 .in
179 .SS プログラムのソース
180 \&
181 .nf
182 #define _GNU_SOURCE
183 #include <dirent.h>     /* Defines DT_* constants */
184 #include <fcntl.h>
185 #include <stdio.h>
186 #include <unistd.h>
187 #include <stdlib.h>
188 #include <sys/stat.h>
189 #include <sys/syscall.h>
190
191 #define handle_error(msg) \e
192         do { perror(msg); exit(EXIT_FAILURE); } while (0)
193
194 struct linux_dirent {
195     long           d_ino;
196     off_t          d_off;
197     unsigned short d_reclen;
198     char           d_name[];
199 };
200
201 #define BUF_SIZE 1024
202
203 int
204 main(int argc, char *argv[])
205 {
206     int fd, nread;
207     char buf[BUF_SIZE];
208     struct linux_dirent *d;
209     int bpos;
210     char d_type;
211
212     fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
213     if (fd == \-1)
214         handle_error("open");
215
216     for ( ; ; ) {
217         nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
218         if (nread == \-1)
219             handle_error("getdents");
220
221         if (nread == 0)
222             break;
223
224         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
225         printf("i\-node#  file type  d_reclen  d_off   d_name\en");
226         for (bpos = 0; bpos < nread;) {
227             d = (struct linux_dirent *) (buf + bpos);
228             printf("%8ld  ", d\->d_ino);
229             d_type = *(buf + bpos + d\->d_reclen \- 1);
230             printf("%\-10s ", (d_type == DT_REG) ?  "regular" :
231                              (d_type == DT_DIR) ?  "directory" :
232                              (d_type == DT_FIFO) ? "FIFO" :
233                              (d_type == DT_SOCK) ? "socket" :
234                              (d_type == DT_LNK) ?  "symlink" :
235                              (d_type == DT_BLK) ?  "block dev" :
236                              (d_type == DT_CHR) ?  "char dev" : "???");
237             printf("%4d %10lld  %s\en", d\->d_reclen,
238                     (long long) d\->d_off, d\->d_name);
239             bpos += d\->d_reclen;
240         }
241     }
242
243     exit(EXIT_SUCCESS);
244 }
245 .fi
246 .SH 関連項目
247 \fBreaddir\fP(2), \fBreaddir\fP(3)
248 .SH この文書について
249 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.54 の一部
250 である。プロジェクトの説明とバグ報告に関する情報は
251 http://www.kernel.org/doc/man\-pages/ に書かれている。