.\" Copyright (c) 1989, 1991 The Regents of the University of California.
.\" This code is derived from software contributed to Berkeley by
.\" %sccs.include.redist.man%
.\" @(#)glob.3 5.8 (Berkeley) %G%
.Nd generate pathnames matching a pattern
.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(const char *, int)" "glob_t *pglob"
.Fn globfree "glob_t *pglob"
is a pathname generator that implements the rules for file name pattern
matching used by the shell.
defines the structure type
which contains at least the following fields:
int gl_pathc; /* count of total paths so far */
int gl_matchc; /* count of paths matching pattern */
int gl_offs; /* reserved at beginning of gl_pathv */
int gl_flags; /* returned flags */
char **gl_pathv; /* list of paths matching pattern */
is a pointer to a pathname pattern to be expanded.
matches all accessible pathnames against the pattern and creates
a list of the pathnames that match.
In order to have access to a pathname,
requires search permission on every component of a path except the last
and read permission on each directory of any filename component of
that contains any of the special characters
stores the number of matched pathnames into the
field, and a pointer to a list of pointers to pathnames into the
The first pointer after the last pathname is
If the pattern does not match any pathnames, the returned number of
matched paths is set to zero.
It is the caller's responsibility to create the structure pointed to by
function allocates other space as needed, including the memory pointed
is used to modify the behavior of
.Bl -tag -width GLOB_ALTDIRFUNC
Append pathnames generated to the ones from a previous call (or calls)
will be the total matches found by this call and the previous call(s).
The pathnames are appended to, not merged with the pathnames returned by
Between calls, the caller must not change the setting of the
flag, nor change the value of
is set, nor (obviously) call
is used to specify how many
pointers to prepend to the beginning
pathname pointers, followed by a
to return when it encounters a directory that it cannot open or read.
continues to find matches.
Each pathname that is a directory that matches
does not match any pathname, then
with the number of total pathnames is set to 1, and the number of matched
is set, its effect is present in the pattern returned.
By default, the pathnames are sorted in ascending
this flag prevents that sorting (speeding up
The following values may also be included in
however, they are non-standard extensions to
.Bl -tag -width GLOB_ALTDIRFUNC
The following additional fields in the pglob structure have been
initialized with alternate functions for glob to use to open, read,
and close directories and to get stat information on names found
void *(*gl_opendir)(const char * name);
struct dirent *(*gl_readdir)(void *);
void (*gl_closedir)(void *);
int (*gl_lstat)(const char *name, struct stat *st);
int (*gl_stat)(const char *name, struct stat *st);
This extension is provided to allow programs such as
to provide globbing from directories stored on tape.
Pre-process the pattern string to expand
is left unexpanded for historical reasons
function if the pattern included globbing characters.
See the description of the usage of the
structure member for more details.
if it does not contain any of the special characters ``*'', ``?'' or ``[''.
is provided to simplify implementing the historic
globbing behavior and should probably not be used anywhere else.
character for quoting: every occurrence of
a backslash followed by a character in the pattern is replaced by that
character, avoiding any special interpretation of the character.
Expand patterns that start with
to user name home directories.
If, during the search, a directory is encountered that cannot be opened
.Fa (*errfunc)(path, errno) .
This may be unintuitive: a pattern like
is not a directory, resulting in a
The error routine can suppress this action by testing for
flag will still cause an immediate
return when this happens.
stops the scan and returns
to reflect any paths already matched.
This also happens if an error is encountered and
regardless of the return value of
returns zero, the error is ignored.
function frees any space associated with
from a previous call(s) to
On successful completion,
In addition the fields of
contain the values described below:
.Bl -tag -width GLOB_NOCHECK
contains the total number of matched pathnames so far.
This includes other matches from previous invocations of
contains the number of matched pathnames in the current invocation of
contained any of the special characters ``*'', ``?'' or ``['', cleared
list of matched pathnames.
terminates due to an error, it sets errno and returns one of the
following non-zero constants, which are defined in the include
.Bl -tag -width GLOB_NOCHECK
An attempt to allocate memory failed.
The scan was stopped because an error was encountered and either
are still set as specified above.
function is expected to be
compatible with the exception
should not be used by applications striving for strict
.Bd -literal -offset indent
glob("*.c", GLOB_DOOFFS, NULL, &g);
glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
execvp("ls", g.gl_pathv);
may cause unchecked errors.
may fail and set errno for any of the errors specified for the