.\" Copyright (c) 1989 The Regents of the University of California.
.\" This code is derived from software contributed to Berkeley by
.\" %sccs.include.redist.man%
.\" @(#)glob.3 5.2 (Berkeley) %G%
glob, globfree \- generate pathnames matching a pattern
glob(const char *pattern, int flags,
const int (*errfunc)(char *, int), glob_t *pglob);
void 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:
.ta .5i +\w'char **gl_pathv;\0\0\0'u
int gl_pathc; /* count of paths matching pattern */
int gl_offs; /* reserved at beginning of gl_pathv */
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 ``*'', ``?'' or ``[''.
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 NULL.
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
is the bitwise inclusive OR of any of the following
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
GLOB_DOOFFS flag, nor change the value of
GLOB_DOOFFS is set, nor (obviously) call
is used to specify how many NULL pointers to prepend to the beginning
pathname pointers, followed by a NULL pointer.
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
By default, the pathnames are sorted in ascending ASCII order;
this flag prevents that sorting (speeding up
does not match any pathname, then
and the number of matched pathnames is set to 1.
is set, its effect is present in the pattern returned.
Use the backslash (``\e'') 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.
If, during the search, a directory is encountered that cannot be opened
calls (*\fIerrfunc\fP)(\fIpath\fP, \fIerrno\fP).
This may be unintuitive: a pattern like ``*/Makefile'' will try to
``foo/Makefile'' even if ``foo'' is not a directory, resulting in a
The error routine can suppress this action by testing for ENOENT and
ENOTDIR; however, the GLOB_ERR 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,
returns the number of matched pathnames and
contains a pointer to a NULL-terminated 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
An attempt to allocate memory failed.
The scan was stopped because an error was encountered and either
GLOB_ERR was set or (*\fIerrfunc\fR)() returned non-zero.
are still set as specified above.
function is expected to be POSIX 1003.2 compatible with the exception
that the flag GLOB_QUOTE should not be used by applications striving
for strict POSIX conformance.
A rough equivalent of ``ls -l *.c *.h'' can be obtained with the
glob("*.c", GLOB_DOOFFS, NULL, &g);
glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
execvp("ls", g.gl_pathv);
sh(1), fnmatch(3), wordexp(3), regexp(3)
Patterns longer than MAXPATHLEN may cause unchecked errors.
may fail and set errno for any of the errors specified for the