.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl) .\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl) .\" 2007-07-30 Ulrich Drepper , mtk: .\" Rework discussion of nonstandard structure fields. .\" 2008-09-11, mtk, Document readdir_r(). .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka .\" all rights reserved. .\" Translated 1997-12-24, HIROFUMI Nishizuka .\" Updated & Modified 2002-03-24, Yuichi SATO .\" Updated & Modified 2005-01-16, Yuichi SATO .\" Updated & Modified 2005-09-06, Akihiro MOTOKI .\" Updated 2008-08-11, Akihiro MOTOKI , LDP v3.05 .\" Updated 2013-03-26, Akihiro MOTOKI .\" Updated 2013-05-01, Akihiro MOTOKI .\" Updated 2013-07-22, Akihiro MOTOKI .\" .TH READDIR 3 2013\-06\-21 "" "Linux Programmer's Manual" .SH 名前 readdir, readdir_r \- ディレクトリを読み込む .SH 書式 .nf \fB#include \fP .sp \fBstruct dirent *readdir(DIR *\fP\fIdirp\fP\fB);\fP .sp \fBint readdir_r(DIR *\fP\fIdirp\fP\fB, struct dirent *\fP\fIentry\fP\fB, struct dirent **\fP\fIresult\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .ad l .in .sp \fBreaddir_r\fP(): .RS 4 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE .RE .ad b .SH 説明 \fBreaddir\fP() 関数は、\fIdirp\fP が指すディレクトリストリームの中で、 次のディレクトリエントリを表す \fIdirent\fP 構造体へのポインタを返す。 ディレクトリストリームの末尾に達した場合や、 エラーが発生した場合は、 NULL を返す。 .PP Linux では \fIdirent\fP 構造体は以下のように定義されている。 .PP .in +4n .nf struct dirent { ino_t d_ino; /* inode 番号 */ off_t d_off; /* オフセットではない; 注意を参照 */ unsigned short d_reclen; /* このレコードの長さ */ unsigned char d_type; /* ファイル種別。全ファイルシステム */ でサポートされているわけではない */ char d_name[256]; /* ファイル名 */ }; .fi .in .PP \fIdirent\fP 構造体のフィールドで POSIX.1 で要求されているのは、 \fId_name\fP[] と (XSI 拡張での) \fId_ino\fP だけである。 \fId_name\fP[] はその大きさも規定されておらず、 このフィールドには最大で \fBNAME_MAX\fP 個の文字と、それに続く終端の ヌルバイト (\(aq\e0\(aq)が格納される。 他のフィールドは非標準であり、全てのシステムに存在するわけではない。 詳細については、下記の「注意」を参照のこと。 .PP \fBreaddir\fP() によって返されるデータは、それ以降の同じストリームに対する \fBreaddir\fP() の呼び出しによって上書きされる可能性がある。 \fBreaddir_r\fP() 関数は \fBreaddir\fP() のリエントラント版である。 この関数はディレクトリストリーム \fIdirp\fP から次のディレクトリエントリを読み込み、 \fIentry\fP が指す呼び出し元が割り当てたバッファにそのエントリを格納して返す (このバッファの割り当てについては「注意」の節を参照のこと)。 返されるエントリへのポインタが \fI*result\fP に格納される。ディレクトリストリームの末尾に達した場合は、 NULL が \fI*result\fP に格納される。 .SH 返り値 成功すると、 \fBreaddir\fP() は \fIdirent\fP 構造体へのポインタを返す。 (この構造体は静的に割り当てられているかもしれない。 このポインタを \fBfree\fP(3) しようとしないこと。) ディレクトリストリームの末尾に達した場合には、NULL が返され、 \fIerrno\fP は変化しない。 エラーが発生した場合、NULL が返され、 \fIerrno\fP が適切に設定される。 成功すると、 \fBreaddir_r\fP() 関数は 0 を返す。 エラーの場合、(「エラー」の節のリストに載っている) 正のエラー番号を返す。 ディレクトリストリームの末尾に達した場合、 \fBreaddir_r\fP() は返り値として 0 を返し、 \fI*result\fP に NULL を格納する。 .SH エラー .TP \fBEBADF\fP ディレクトリストリームディスクリプタ \fIdirp\fP が無効である。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" \fBreaddir\fP() 関数はスレッドセーフではない。 .LP \fBreaddir_r\fP() 関数はスレッドセーフである。 .SH 準拠 SVr4, 4.3BSD, POSIX.1\-2001. .SH 注意 フィールド \fId_name\fP と \fId_ino\fP だけが POSIX.1\-2001 で規定されている。 残りのフィールドは多くのシステムに存在するが、全てのシステムに 存在するわけではない。 glibc では、プログラムが POSIX.1 で定義されていないフィールドが 利用できるかをチェックすることができる。 チェックするには、マクロ \fB_DIRENT_HAVE_D_NAMLEN\fP, \fB_DIRENT_HAVE_D_RECLEN\fP, \fB_DIRENT_HAVE_D_OFF\fP, \fB_DIRENT_HAVE_D_TYPE\fP が定義されているかをテストすればよい。 .\" https://lwn.net/Articles/544298/ \fId_off\fP で返される値は \fBtelldir\fP(3) が返す値と同じで、ディレクトリストリーム内の現在の位置を示す。 フィールドの型や名前はこうなっていますが、最近のファイルシステムでは \fId_off\fP フィールドが何らかのディレクトリオフセットであることはめったいにない。アプリケーションプログラムでは、必ずこの値を内容を意識せず単なる値として扱うべきであり、その内容について前提を持つべきではない。 \fId_type\fP フィールドは、Linux 以外では、 主に BSD 系のシステムにだけ存在する。 このフィールドを使うと、 その後の動作がファイルの種別により決まる場合に、 \fBlstat\fP(2) を呼び出すコストを避けることができる。 機能検査マクロ \fB_BSD_SOURCE\fP が定義された場合、glibc は \fId_type\fP で返される値として以下のマクロ定数を定義する。 .TP 12 \fBDT_BLK\fP ブロックデバイスである。 .TP \fBDT_CHR\fP キャラクタデバイスである。 .TP \fBDT_DIR\fP ディレクトリである。 .TP \fBDT_FIFO\fP 名前付きパイプ (FIFO) である。 .TP \fBDT_LNK\fP シンボリックリンクである。 .TP \fBDT_REG\fP 通常のファイルである。 .TP \fBDT_SOCK\fP UNIX ドメインソケットである。 .TP \fBDT_UNKNOWN\fP .\" The glibc manual says that on some systems this is the only .\" value returned ファイルタイプが不明である。 .PP ファイル種別を決定できなかった場合には、 \fId_type\fP に \fBDT_UNKNOWN\fP が入る。 .\" kernel 2.6.27 .\" The same sentence is in getdents.2 現在のところ、 \fId_type\fP でファイルタイプを返す機能が完全にサポートされているのは、 いくつかのファイルシステムにおいてのみである (Btrfs, ext2, ext3, ext4 はサポートしている)。 どのアプリケーションも、 \fBDT_UNKNOWN\fP が返された際に適切に処理できなければならない。 POSIX.1 では \fId_name\fP フィールドのサイズは規定されておらず、 \fIdirent\fP 構造体の \fId_name\fP の後ろに他の非標準のフィールドがあるかもしれないので、 移植性が必要なアプリケーションで \fBreaddir_r\fP() を使う場合は \fIentry\fP に渡すバッファを次のようにして割り当てるべきである。 .in +4n .nf name_max = pathconf(dirpath, _PC_NAME_MAX); if (name_max == \-1) /* 上限が定義されていない、またはエラー */ name_max = 255; /* 適当な値を入れる */ len = offsetof(struct dirent, d_name) + name_max + 1; entryp = malloc(len); .fi .in (POSIX.1 では \fIstruct dirent\fP の最後のフィールドが \fId_name\fP であることを要求している。) .SH 関連項目 \fBgetdents\fP(2), \fBread\fP(2), \fBclosedir\fP(3), \fBdirfd\fP(3), \fBftw\fP(3), \fBoffsetof\fP(3), \fBopendir\fP(3), \fBrewinddir\fP(3), \fBscandir\fP(3), \fBseekdir\fP(3), \fBtelldir\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。