OSDN Git Service

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