OSDN Git Service

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