OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / man3 / readdir.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
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 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
29 .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
30 .\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
31 .\"     Rework discussion of nonstandard structure fields.
32 .\" 2008-09-11, mtk, Document readdir_r().
33 .\"
34 .\"*******************************************************************
35 .\"
36 .\" This file was generated with po4a. Translate the source file.
37 .\"
38 .\"*******************************************************************
39 .TH READDIR 3 2010\-09\-10 "" "Linux Programmer's Manual"
40 .SH 名前
41 readdir, readdir_r \- ディレクトリを読み込む
42 .SH 書式
43 .nf
44 \fB#include <dirent.h>\fP
45 .sp
46 \fBstruct dirent *readdir(DIR *\fP\fIdirp\fP\fB);\fP
47 .sp
48 \fBint readdir_r(DIR *\fP\fIdirp\fP\fB, struct dirent *\fP\fIentry\fP\fB, struct dirent **\fP\fIresult\fP\fB);\fP
49 .fi
50 .sp
51 .in -4n
52 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
53 .ad l
54 .in
55 .sp
56 \fBreaddir_r\fP():
57 .RS 4
58 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
59 || _POSIX_SOURCE
60 .RE
61 .ad b
62 .SH 説明
63 \fBreaddir\fP()  関数は、\fIdirp\fP が指すディレクトリストリームの中で、 次のディレクトリエントリを表す \fIdirent\fP
64 構造体へのポインタを返す。 ディレクトリストリームの末尾に達した場合や、 エラーが発生した場合は、 NULL を返す。
65 .PP
66 Linux では \fIdirent\fP 構造体は以下のように定義されている。
67 .PP
68 .in +4n
69 .nf
70 struct dirent {
71     ino_t          d_ino;       /* inode 番号 */
72     off_t          d_off;       /* 次の dirent へのオフセット */
73     unsigned short d_reclen;    /* このレコードの長さ */
74     unsigned char  d_type;      /* ファイル種別。全ファイルシステム */
75                                    でサポートされているわけではない */
76     char           d_name[256]; /* ファイル名 */
77 };
78 .fi
79 .in
80 .PP
81 \fIdirent\fP 構造体のフィールドで POSIX.1 で要求されているのは、 \fId_name\fP[] と (XSI 拡張での)  \fId_ino\fP
82 だけである。 \fId_name\fP[] はその大きさも規定されておらず、 このフィールドには最大で \fBNAME_MAX\fP 個の文字と、それに続く終端の
83 NULL バイトが格納される。 他のフィールドは非標準であり、全てのシステムに存在するわけではない。 詳細については、下記の「注意」を参照のこと。
84 .PP
85 \fBreaddir\fP()  によって返されるデータは、それ以降の同じストリームに対する \fBreaddir\fP()
86 の呼び出しによって上書きされる可能性がある。
87
88 \fBreaddir_r\fP()  関数は \fBreaddir\fP()  のリエントラント版である。 この関数はディレクトリストリーム \fIdirp\fP
89 から次のディレクトリエントリを読み込み、 \fIentry\fP が指す呼び出し元が割り当てたバッファにそのエントリを格納して返す
90 (このバッファの割り当てについては「注意」の節を参照のこと)。 返されるエントリへのポインタが \fI*result\fP
91 に格納される。ディレクトリストリームの末尾に達した場合は、 NULL が \fI*result\fP に格納される。
92 .SH 返り値
93 成功すると、 \fBreaddir\fP()  は \fIdirent\fP 構造体へのポインタを返す。 (この構造体は静的に割り当てられているかもしれない。
94 このポインタを \fBfree\fP(3)  しようとしないこと。)  ディレクトリストリームの末尾に達した場合には、NULL が返され、 \fIerrno\fP
95 は変化しない。 エラーが発生した場合、NULL が返され、 \fIerrno\fP が適切に設定される。
96
97 成功すると、 \fBreaddir_r\fP()  関数は 0 を返す。 エラーの場合、(「エラー」の節のリストに載っている) 正のエラー番号を返す。
98 ディレクトリストリームの末尾に達した場合、 \fBreaddir_r\fP()  は返り値として 0 を返し、 \fI*result\fP に NULL
99 を格納する。
100 .SH エラー
101 .TP 
102 \fBEBADF\fP
103 ディレクトリストリームディスクリプタ \fIdirp\fP が無効である。
104 .SH 準拠
105 SVr4, 4.3BSD, POSIX.1\-2001.
106 .SH 注意
107 フィールド \fId_name\fP と \fId_ino\fP だけが POSIX.1\-2001 で規定されている。
108 残りのフィールドは多くのシステムに存在するが、全てのシステムに 存在するわけではない。 glibc では、プログラムが POSIX.1
109 で定義されていないフィールドが 利用できるかをチェックすることができる。 チェックするには、マクロ \fB_DIRENT_HAVE_D_NAMLEN\fP,
110 \fB_DIRENT_HAVE_D_RECLEN\fP, \fB_DIRENT_HAVE_D_OFF\fP, \fB_DIRENT_HAVE_D_TYPE\fP
111 が定義されているかをテストすればよい。
112
113 \fId_type\fP フィールドは、Linux 以外では、 主に BSD 系のシステムにだけ存在する。 このフィールドを使うと、
114 その後の動作がファイルの種別により決まる場合に、 \fBlstat\fP(2)  を呼び出すコストを避けることができる。 機能検査マクロ
115 \fB_BSD_SOURCE\fP が定義された場合、glibc は \fId_type\fP で返される値として以下のマクロ定数を定義する。
116 .TP  12
117 \fBDT_BLK\fP
118 ブロックデバイスである。
119 .TP 
120 \fBDT_CHR\fP
121 キャラクタデバイスである。
122 .TP 
123 \fBDT_DIR\fP
124 ディレクトリである。
125 .TP 
126 \fBDT_FIFO\fP
127 名前付きパイプ (FIFO) である。
128 .TP 
129 \fBDT_LNK\fP
130 シンボリックリンクである。
131 .TP 
132 \fBDT_REG\fP
133 通常のファイルである。
134 .TP 
135 \fBDT_SOCK\fP
136 UNIX ドメインソケットである。
137 .TP 
138 \fBDT_UNKNOWN\fP
139 .\" The glibc manual says that on some systems this is the only
140 .\" value returned
141 ファイルタイプが不明である。
142 .PP
143 ファイル種別を決定できなかった場合には、 \fId_type\fP に \fBDT_UNKNOWN\fP が入る。
144
145 .\" kernel 2.6.27
146 .\" The same sentence is in getdents.2
147 現在のところ、 \fId_type\fP でファイルタイプを返す機能が完全にサポートされているのは、 いくつかのファイルシステムにおいてのみである
148 (Btrfs, ext2, ext3, ext4 はサポートしている)。 どのアプリケーションも、 \fBDT_UNKNOWN\fP
149 が返された際に適切に処理できなければならない。
150
151 POSIX.1 では \fId_name\fP フィールドのサイズは規定されておらず、 \fIdirent\fP 構造体の \fId_name\fP
152 の後ろに他の非標準のフィールドがあるかもしれないので、 移植性が必要なアプリケーションで \fBreaddir_r\fP()  を使う場合は \fIentry\fP
153 に渡すバッファを次のようにして割り当てるべきである。
154 .in +4n
155 .nf
156
157 len = offsetof(struct dirent, d_name) +
158           pathconf(dirpath, _PC_NAME_MAX) + 1
159 entryp = malloc(len);
160
161 .fi
162 .in
163 (POSIX.1 では \fIstruct dirent\fP の最後のフィールドが \fId_name\fP であることを要求している。)
164 .SH 関連項目
165 \fBgetdents\fP(2), \fBread\fP(2), \fBclosedir\fP(3), \fBdirfd\fP(3), \fBftw\fP(3),
166 \fBoffsetof\fP(3), \fBopendir\fP(3), \fBrewinddir\fP(3), \fBscandir\fP(3),
167 \fBseekdir\fP(3), \fBtelldir\fP(3)