.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" 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. .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Sat Jul 24 18:26:16 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Thu Apr 11 17:11:33 1996 by Andries Brouwer (aeb@cwi.nl): .\" Corrected type of compar routines, as suggested by .\" Miguel Barreiro (enano@avalon.yaix.es). Added example. .\" Modified Sun Sep 24 20:15:46 2000 by aeb, following Petter Reinholdtsen. .\" Modified 2001-12-26 by aeb, following Joey. Added versionsort. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH SCANDIR 3 2012\-04\-26 GNU "Linux Programmer's Manual" .SH 名前 scandir, alphasort, versionsort \- ディレクトリを走査する .SH 書式 .nf \fB#include \fP .sp \fBint scandir(const char *\fP\fIdirp\fP\fB, struct dirent ***\fP\fInamelist\fP\fB,\fP .RS \fBint (*\fP\fIfilter\fP\fB)(const struct dirent *),\fP \fBint (*\fP\fIcompar\fP\fB)(const struct dirent **, const struct dirent **));\fP .RE .sp \fBint alphasort(const void *\fP\fIa\fP\fB, const void *\fP\fIb\fP\fB);\fP .sp \fBint versionsort(const void *\fP\fIa\fP\fB, const void *\fP\fIb\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp \fBscandir\fP(), \fBalphasort\fP(): _BSD_SOURCE || _SVID_SOURCE .br \fBversionsort\fP(): _GNU_SOURCE .SH 説明 関数 \fBscandir\fP() はディレクトリ \fIdirp\fP を走査し、 ディレクトリの各エントリを引き数として \fBfilter\fP() を呼び出す。 \fBfilter\fP() が 0 以外の値を返すエントリは \fBmalloc\fP(3) によって 確保された文字列に保存され、比較関数 \fBcompar\fP() を用いて \fBqsort\fP(3) によりソートされ、 \fBmalloc\fP(3) により確保された配列 \fInamelist\fP にまとめられる。 \fIfilter\fP が NULL ならば、すべてのエントリが選択される。 .LP 比較関数 \fIcompar\fP() には \fBalphasort\fP() 関数と \fBversionsort\fP() 関数を使うことができる。 \fBalphasort\fP() は \fBstrcoll\fP(3) を用いてディレクトリエントリをソートし、 \fBversionsort\fP() は文字列 \fI(*a)\->d_name\fP と \fI(*b)\->d_name\fP に対して \fBstrverscmp\fP(3) を用いる。 .SH 返り値 関数 \fBscandir\fP() は選択されたエントリの数か、 (エラーが発生した場合) \-1 を返す。 .PP 関数 \fBalphasort\fP() と \fBversionsort\fP() は 1 番目の引き数が 2 番目の引き数に対して、 [小さい/等しい/大きい] かに応じて、0 より [小さい/等しい/大きい] 値を返す。 .SH エラー .TP \fBENOENT\fP \fIdirp\fP で指定されたパスが存在しない。 .TP \fBENOMEM\fP 操作を完了するのに十分なメモリがない。 .TP \fBENOTDIR\fP \fIdirp\fP で指定されたパスがディレクトリではない。 .SH バージョン \fBversionsort\fP() は、glibc バージョン 2.1 で追加された。 .SH 準拠 \fBalphasort\fP() と \fBscandir\fP() は POSIX.1\-2008 で規定されており、広く利用可能である。 \fBversionsort\fP() は GNU 拡張である。 .LP 関数 \fBscandir\fP() と \fBalphasort\fP() は 4.3BSD から取り入れられ、Linux では libc4 から使用可能になった。 libc4 と libc5 では以下のようなもっと詳細なプロトタイプを使っている。 .sp .nf int alphasort(const struct dirent ** a, const struct dirent **b); .fi .sp しかし glibc 2.0 では不正確な BSD のプロトタイプに戻った。 .LP 関数 \fBversionsort\fP() は GNU の拡張であり、glibc 2.1 以降で使用可能である。 .LP glibc 2.1 以降では \fBalphasort\fP() は \fBstrcoll\fP(3) を呼び出す。 \fBalphasort\fP() は以前は \fBstrcmp\fP(3) を使っていた。 .SH 例 .nf #define _SVID_SOURCE /* カレントディレクトリのファイルを逆順に出力する */ #include int main(void) { struct dirent **namelist; int n; n = scandir(".", &namelist, NULL, alphasort); if (n < 0) perror("scandir"); else { while (n\-\-) { printf("%s\en", namelist[n]\->d_name); free(namelist[n]); } free(namelist); } } .fi .SH 関連項目 \fBclosedir\fP(3), \fBfnmatch\fP(3), \fBopendir\fP(3), \fBreaddir\fP(3), \fBrewinddir\fP(3), \fBscandirat\fP(3), \fBseekdir\fP(3), \fBstrcmp\fP(3), \fBstrcoll\fP(3), \fBstrverscmp\fP(3), \fBtelldir\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。