.\" Copyright (c) 1989, 1991 The Regents of the University of California.
.\" This code is derived from software contributed to Berkeley by
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" @(#)glob.3 5.7 (Berkeley) 12/2/92
.Nd generate pathnames matching a pattern
.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(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_NOCHECK
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.
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.
By default, the pathnames are sorted in ascending
this flag prevents that sorting (speeding up
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.
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
struct dirent *(*gl_readdir)();
This non-standard extension is provided to allow programs such
as restore to provide globbing from directories stored on tape.
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