globのヘルプ・マニュアル
日本語 英語
glob --help
man glob
GLOB(3) Linux Programmer’s Manual GLOB(3)
名前
glob, globfree - パターンにマッチするパス名を見付ける。glob() によって
確保されたメモリ領域を解放する。
書式
#include
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);
説明
glob() 関数はシェルが用いているルール (glob(7) 参照) に基づいてパターン
pattern にマッチするすべてのパス名を検索する。チルダ (~) の展開やパラメ
ータ置換は行われない。それらを行いたい場合は wordexp(3) を使うとよい。
globfree() 関数は前に呼ばれた glob() により動的に確保された記憶領域を解
放する。
glob() の結果は pglob がポイントする構造体に返される。 pglob は glob_t
型の構造体である。 glob_t 型は 内で宣言されており、以下の要 素
を含んでいる。これらの要素は POSIX.2 で定義されている (さらに多くの要素
が拡張として入っているかもしれない)。
typedef struct {
size_t gl_pathc; /* 今までにマッチしたパスの数 */
char **gl_pathv; /* マッチしたパス名のリスト */
size_t gl_offs; /* gl_pathv 内に確保するスロット数 */
} glob_t;
結果は動的に確保された記憶領域に入れられる。
パラメータ flags には以下の示す定数のうち、指定したいものをビットごとの
OR で与える (一つも指定しなくてもよい)。これによって glob() の動作を変
更できる。
GLOB_ERR
(例えば、ディレクトリに読み取り許可属性が無い場合などで) 読み 取
りエラーが発生した際に関数から戻る。デフォルトでは、エラーに関わ
らず読み取り可能なディレクトリを全てについて読み取りを実行しよう
とする。
GLOB_MARK
ディレクトリに対応する各々のパスにスラッシュを付加する。
GLOB_NOSORT
返されるパス名のソートを行わない。ソートを行わない理由は、処理時
間を節約するためだけである。デフォルトでは、返されるパス名はソー
トされる。
GLOB_DOOFFS
pglob->pathv の文字列リストの先頭に pglob->gl_offs スロット分の
領域を予約する。予約されたスロットには NULL ポインタが入る。
GLOB_NOCHECK
マッチするパターンがなければ、元のパターンを返す。デフォルトでは
、 glob() はマッチするパターンがなければ GLOB_NOMATCH を返す。
GLOB_APPEND
この呼び出しでの結果を直前の glob() の呼び出しで返された結果のベ
クトルに追加する。最初の glob() の呼び出しの際にはこのフラグを設
定してはいけない。
GLOB_NOESCAPE
バ ックスラッシュ ('\') をエスケープ用文字として使用できない。通
常は、バックスラッシュを使って、次に続く文字をクォートすることで
、特別な意味を持つメタキャラクタを無効することができる。
flags には以下に示すものも指定できる。これらは GNU で拡張されたもので
、POSIX.2 では定義されていない。
GLOB_PERIOD
先頭のピリオドがメタキャラクタにマッチできるようにする。デフォル
トでは、メタキャラクタは先頭のピリオドにはマッチできない。
GLOB_ALTDIRFUNC
ファイルシステムにアクセスする際に、通常のライブラリ関数の代わり
に 代 替 関 数 pglob->gl_closedir, pglob->gl_readdir,
pglob->gl_opendir, pglob->gl_lstat, pglob->gl_stat が用いられる
。
GLOB_BRACE
{a,b} という形式の csh(1) スタイルの括弧表現を展開する。括弧表現
は 入 れ 子 に す る こ と が で き る 。 し た が っ て、例えば
、"{foo/{,cat,dog},bar}" というパターンを指定した場合に得られ る
結 果は、 4つの文字列 "foo/", "foo/cat", "foo/dog", "bar" のそれ
ぞれについて glob() を呼び出した場合と同じになる。
GLOB_NOMAGIC
パターンにメタキャラクタが含まれていない場合、マッチ結果として指
定されたパターンだけを返す。パターンで指定された名前のファイルが
存在しない場合であっても、そのパターンが返される。
GLOB_TILDE
チルダの展開を行う。チルダ ('~') がパターン内の唯一の文字の場 合
か 、先頭のチルダの直後の文字がスラッシュ ('/') の場合、チルダを
呼び出し者のホームディレクトリで置換する。先頭のチルダにユーザ名
が 続く場合 (例えば "~andrea/bin")、チルダとユーザ名をそのユーザ
のホームディレクトリで置換する。ユーザ名が無効な場合やホームディ
レクトリが決定できない場合は、置換は実行されない。
GLOB_TILDE_CHECK
こ の フ ラ グ を 指 定 すると GLOB_TILDE と同様の振舞いをする。
GLOB_TILDE との違いは、ユーザ名が無効だった場合やホームディレ ク
トリが決定できなかった場合に、パターン自身を使用するのではなく、
glob() がエラーを示す GLOB_NOMATCH を返すことである。
GLOB_ONLYDIR
このフラグは、 glob() に対する「ヒント」であり、呼び出し側がパタ
ーンにマッチするディレクトリにしか興味がないことを知らせる。実装
においてファイルの種別情報を簡単に決定できる場合は、ディレクトリ
でないファイルは呼び出し側に返されない。しかしながら、呼び出し側
では、返されたファイルリストがディレクトリかどうかを確認しなけれ
ばならない。 (このフラグが存在するのは、呼び出し側がディレクトリ
にしか興味がない際に性能を最適化する目的のためだけである。)
errfunc が NULL でなければ、エラーが起こった場合には関数 errfunc が呼び
出 さ れ る。関数の引数には、失敗したパス名 epath と errno (opendir(3),
readdir(3), stat(2). のいずれかによってセットされた値) が与えられる 。
errfunc が 0 以 外の値を返すかもしくは GLOB_ERR がセットされた場合
glob() は errfunc の呼び出し後に終了する。
呼び出しが成功して戻った場合 pglob->gl_pathc にはマッチしたパス名が含ま
れ、 pglob->gl_pathv はマッチしたパス名へのポインタのリストへのポインタ
となる。ポインタのリストは NULL ポインタで終端される。
glob() を何度か続けて呼び出すことができる。その際2回目以降の呼び出し で
は GLOB_APPEND フラグが flags に設定されていなければならない。
GNU の拡張として、 pglob->gl_flags には指定したフラグがセットされる。も
し一つでもメタキャラクタが見付かればこのフラグと GLOB_MAGCHAR と の OR
を取った結果がセットされる。
返り値
呼び出しが成功して完了すると glob() は 0 を返す。それ以外の返り値は以下
の通り:
GLOB_NOSPACE
メモリを使い果たした
GLOB_ABORTED
読み取りエラー
GLOB_NOMATCH
一つもマッチしなかった
準拠
POSIX.2, POSIX.1-2001.
注意
glibc 2.1 では、 gl_pathc と gl_offs は POSIX.2 で指定されているよう に
size_t として宣言されている。 libc4, libc5, glibc 2.0 では、 int として
宣言されている。
バグ
glob() 関数はその中で呼び出している malloc(3) や opendir(3) などの関 数
の呼び出しで失敗が起こると失敗する。これにより errno にそのエラーコード
が入る。
例
使用法の一例を以下に示す。以下はシェルで
ls -l *.c ../*.c
をタイプした場合をシミュレートしている。
glob_t globbuf;
globbuf.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";
globbuf.gl_pathv[1] = "-l";
execvp("ls", &globbuf.gl_pathv[0]);
関連項目
ls(1), sh(1), stat(2), exec(3), fnmatch(3), malloc(3), opendir(3),
readdir(3), wordexp(3), glob(7)
GNU 2007-10-10 GLOB(3)
GLOB(3) Linux Programmer’s Manual GLOB(3)
NAME
glob, globfree - find pathnames matching a pattern, free memory from
glob()
SYNOPSIS
#include
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);
DESCRIPTION
The glob() function searches for all the pathnames matching pattern
according to the rules used by the shell (see glob(7)). No tilde
expansion or parameter substitution is done; if you want these, use
wordexp(3).
The globfree() function frees the dynamically allocated storage from an
earlier call to glob().
The results of a glob() call are stored in the structure pointed to by
pglob. This structure is of type glob_t (declared in ) and
includes the following elements defined by POSIX.2 (more may be present
as an extension):
typedef struct {
size_t gl_pathc; /* Count of paths matched so far */
char **gl_pathv; /* List of matched pathnames. */
size_t gl_offs; /* Slots to reserve in gl_pathv. */
} glob_t;
Results are stored in dynamically allocated storage.
The argument flags is made up of the bitwise OR of zero or more the
following symbolic constants, which modify the behavior of glob():
GLOB_ERR
Return upon a read error (because a directory does not have read
permission, for example). By default, glob() attempts carry on
despite errors, reading all of the directories that it can.
GLOB_MARK
Append a slash to each path which corresponds to a directory.
GLOB_NOSORT
Don’t sort the returned pathnames. The only reason to do this
is to save processing time. By default, the returned pathnames
are sorted.
GLOB_DOOFFS
Reserve pglob->gl_offs slots at the beginning of the list of
strings in pglob->pathv. The reserved slots contain NULL point-
ers.
GLOB_NOCHECK
If no pattern matches, return the original pattern. By default,
glob() returns GLOB_NOMATCH if there are no matches.
GLOB_APPEND
Append the results of this call to the vector of results
returned by a previous call to glob(). Do not set this flag on
the first invocation of glob().
GLOB_NOESCAPE
Don’t allow backslash ('\') to be used as an escape character.
Normally, a backslash can be used to quote the following charac-
ter, providing a mechanism to turn off the special meaning
metacharacters.
flags may also include any of the following, which are GNU extensions
and not defined by POSIX.2:
GLOB_PERIOD
Allow a leading period to be matched by metacharacters. By
default, metacharacters can’t match a leading period.
GLOB_ALTDIRFUNC
Use alternative functions pglob->gl_closedir, pglob->gl_readdir,
pglob->gl_opendir, pglob->gl_lstat, and pglob->gl_stat for file
system access instead of the normal library functions.
GLOB_BRACE
Expand csh(1) style brace expressions of the form {a,b}. Brace
expressions can be nested. Thus, for example, specifying the
pattern "{foo/{,cat,dog},bar}" would return the same results as
four separate glob() calls using the strings: "foo/", "foo/cat",
"foo/dog", and "bar".
GLOB_NOMAGIC
If the pattern contains no metacharacters then it should be
returned as the sole matching word, even if there is no file
with that name.
GLOB_TILDE
Carry out tilde expansion. If a tilde ('~') is the only charac-
ter in the pattern, or an initial tilde is followed immediately
by a slash ('/'), then the home directory of the caller is sub-
stituted for the tilde. If an initial tilde is followed by a
username (e.g., "~andrea/bin"), then the tilde and username are
substituted by the home directory of that user. If the username
is invalid, or the home directory cannot be determined, then no
substitution is performed.
GLOB_TILDE_CHECK
This provides behavior similar to that of GLOB_TILDE. The dif-
ference is that if the username is invalid, or the home direc-
tory cannot be determined, then instead of using the pattern
itself as the name, glob() returns GLOB_NOMATCH to indicate an
error.
GLOB_ONLYDIR
This is a hint to glob() that the caller is interested only in
directories that match the pattern. If the implementation can
easily determine file-type information, then non-directory files
are not returned to the caller. However, the caller must still
check that returned files are directories. (The purpose of this
flag is merely to optimize performance when the caller is inter-
ested only in directories.)
If errfunc is not NULL, it will be called in case of an error with the
arguments epath, a pointer to the path which failed, and eerrno, the
value of errno as returned from one of the calls to opendir(3), read-
dir(3), or stat(2). If errfunc returns non-zero, or if GLOB_ERR is
set, glob() will terminate after the call to errfunc.
Upon successful return, pglob->gl_pathc contains the number of matched
pathnames and pglob->gl_pathv contains a pointer to the list of
pointers to matched pathnames. The list of pointers is terminated by a
NULL pointer.
It is possible to call glob() several times. In that case, the
GLOB_APPEND flag has to be set in flags on the second and later invoca-
tions.
As a GNU extension, pglob->gl_flags is set to the flags specified, ored
with GLOB_MAGCHAR if any metacharacters were found.
RETURN VALUE
On successful completion, glob() returns zero. Other possible returns
are:
GLOB_NOSPACE
for running out of memory,
GLOB_ABORTED
for a read error, and
GLOB_NOMATCH
for no found matches.
CONFORMING TO
POSIX.2, POSIX.1-2001.
NOTES
The structure elements gl_pathc and gl_offs are declared as size_t in
glibc 2.1, as they should be according to POSIX.2, but are declared as
int in libc4, libc5 and glibc 2.0.
BUGS
The glob() function may fail due to failure of underlying function
calls, such as malloc(3) or opendir(3). These will store their error
code in errno.
EXAMPLE
One example of use is the following code, which simulates typing
ls -l *.c ../*.c
in the shell:
glob_t globbuf;
globbuf.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";
globbuf.gl_pathv[1] = "-l";
execvp("ls", &globbuf.gl_pathv[0]);
SEE ALSO
ls(1), sh(1), stat(2), exec(3), fnmatch(3), malloc(3), opendir(3),
readdir(3), wordexp(3), glob(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2007-10-10 GLOB(3)