glob, globfree -- generate pathnames matching a pattern
Standard C Library (libc, -lc)
#include <glob.h>
int
glob(const char *pattern, int flags,
const int (*errfunc)(const char *, int), glob_t *pglob);
void
globfree(glob_t *pglob);
The glob() function is a pathname generator that implements the rules for
file name pattern matching used by the shell.
The include file glob.h defines the structure type glob_t, which contains
at least the following fields:
typedef struct {
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 */
} glob_t;
The argument pattern is a pointer to a pathname pattern to be expanded.
The glob() argument matches all accessible pathnames against the pattern
and creates a list of the pathnames that match. In order to have access
to a pathname, glob() requires search permission on every component of a
path except the last and read permission on each directory of any filename
component of pattern that contains any of the special characters
`*', `'? or [`'.
The glob() argument stores the number of matched pathnames into the
gl_pathc field, and a pointer to a list of pointers to pathnames into the
gl_pathv field. 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
pglob. The glob() function allocates other space as needed, including
the memory pointed to by gl_pathv.
The argument flags is used to modify the behavior of glob(). The value
of flags is the bitwise inclusive OR of any of the following values
defined in glob.h:
GLOB_APPEND Append pathnames generated to the ones from a previous
call (or calls) to glob(). The value of gl_pathc 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 the previous call(s). Between
calls, the caller must not change the setting of the
GLOB_DOOFFS flag, nor change the value of gl_offs when
GLOB_DOOFFS is set, nor (obviously) call globfree() for
pglob.
GLOB_DOOFFS Make use of the gl_offs field. If this flag is set,
gl_offs is used to specify how many NULL pointers to
prepend to the beginning of the gl_pathv field. In
other words, gl_pathv will point to gl_offs NULL pointers,
followed by gl_pathc pathname pointers, followed by
a NULL pointer.
GLOB_ERR Causes glob() to return when it encounters a directory
that it cannot open or read. Ordinarily, glob() continues
to find matches.
GLOB_MARK Each pathname that is a directory that matches pattern
has a slash appended.
GLOB_NOCHECK If pattern does not match any pathname, then glob()
returns a list consisting of only pattern, with the number
of total pathnames is set to 1, and the number of
matched pathnames set to 0.
GLOB_NOSORT By default, the pathnames are sorted in ascending ASCII
order; this flag prevents that sorting (speeding up
glob()).
The following values may also be included in flags, however, they are
non-standard extensions to IEEE Std 1003.2 (``POSIX.2'').
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 in those directories.
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
restore(8) to provide globbing from directories stored
on tape.
GLOB_BRACE Pre-process the pattern string to expand `{pat,pat,...}'
strings like csh(1). The pattern `{}' is left unexpanded
for historical reasons (csh(1) does the same
thing to ease typing of find(1) patterns).
GLOB_MAGCHAR Set by the glob() function if the pattern included globbing
characters. See the description of the usage of
the gl_matchc structure member for more details.
GLOB_NOMAGIC Is the same as GLOB_NOCHECK but it only appends the
pattern if it does not contain any of the special characters
``*'', ``?'' or ``[''. GLOB_NOMAGIC is provided
to simplify implementing the historic csh(1) globbing
behavior and should probably not be used anywhere else.
GLOB_NOESCAPE Disable the use of the backslash (`\') character for
quoting.
GLOB_TILDE Expand patterns that start with `~' to user name home
directories.
GLOB_LIMIT Limit the amount of memory used by matches to ARG_MAX
This option should be set for programs that can be
coerced to a denial of service attack via patterns that
expand to a very large number of matches, such as a long
string of */../*/..
If, during the search, a directory is encountered that cannot be opened
or read and errfunc is non-NULL, glob() calls (*errfunc)(path, errno).
This may be unintuitive: a pattern like `*/Makefile' will try to stat(2)
`foo/Makefile' even if `foo' is not a directory, resulting in a call to
errfunc. 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.
If errfunc returns non-zero, glob() stops the scan and returns
GLOB_ABORTED after setting gl_pathc and gl_pathv to reflect any paths
already matched. This also happens if an error is encountered and
GLOB_ERR is set in flags, regardless of the return value of errfunc, if
called. If GLOB_ERR is not set and either errfunc is NULL or errfunc
returns zero, the error is ignored.
The globfree() function frees any space associated with pglob from a previous
call(s) to glob().
The historical GLOB_QUOTE flag is no longer supported. Per IEEE Std
1003.2-1992 (``POSIX.2''), backslash escaping of special characters is
the default behaviour; it may be disabled by specifying the GLOB_NOESCAPE
flag.
On successful completion, glob() returns zero. In addition the fields of
pglob contain the values described below:
gl_pathc contains the total number of matched pathnames so far.
This includes other matches from previous invocations of
glob() if GLOB_APPEND was specified.
gl_matchc contains the number of matched pathnames in the current
invocation of glob().
gl_flags contains a copy of the flags parameter with the bit
GLOB_MAGCHAR set if pattern contained any of the special
characters ``*'', ``?'' or ``['', cleared if not.
gl_pathv contains a pointer to a NULL-terminated list of matched
pathnames. However, if gl_pathc is zero, the contents of
gl_pathv are undefined.
If glob() terminates due to an error, it sets errno and returns one of
the following non-zero constants, which are defined in the include file
<glob.h>:
GLOB_ABORTED The scan was stopped because an error was encountered
and either GLOB_ERR was set or (*errfunc)() returned
non-zero.
GLOB_NOMATCH The pattern does not match any existing pathname, and
GLOB_NOCHECK was not set int flags.
GLOB_NOSPACE An attempt to allocate memory failed, or if errno was 0
GLOB_LIMIT was specified in the flags and ARG_MAX patterns
were matched.
The historical GLOB_ABEND return constant is no longer supported. Portable
applications should use the GLOB_ABORTED constant instead.
The arguments pglob->gl_pathc and pglob->gl_pathv are still set as specified
above.
HOME If defined, used as the home directory of the current user in tilde
expansions.
A rough equivalent of `ls -l *.c *.h' can be obtained with the following
code:
glob_t g;
g.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &g);
glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
g.gl_pathv[0] = "ls";
g.gl_pathv[1] = "-l";
execvp("ls", g.gl_pathv);
sh(1), fnmatch(3), regexp(3)
The glob() function is expected to be IEEE Std 1003.2 (``POSIX.2'') compatible
with the exception that the flags GLOB_ALTDIRFUNC, GLOB_BRACE
GLOB_MAGCHAR, GLOB_NOMAGIC, GLOB_TILDE, and GLOB_LIMIT and the fields
gl_matchc and gl_flags should not be used by applications striving for
strict POSIX conformance.
The glob() and globfree() functions first appeared in 4.4BSD.
Patterns longer than MAXPATHLEN may cause unchecked errors.
The glob() function may fail and set errno for any of the errors specified
for the library routines stat(2), closedir(3), opendir(3),
readdir(3), malloc(3), and free(3).
FreeBSD 5.2.1 March 31, 1998 FreeBSD 5.2.1
GLOB(3) Linux Programmer's Manual GLOB(3)
glob, globfree - find pathnames matching a pattern, free memory from
glob()
#include <glob.h>
int glob(const char *pattern, int flags,
int errfunc(const char * epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);
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, which is a glob_t which is declared in <glob.h> 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 parameter flags is made up of bitwise OR of zero or more the following
symbolic constants, which modify the of behaviour of glob():
GLOB_ERR [Toc] [Back]
which means to return upon read error (because a directory does
not have read permission, for example),
GLOB_MARK [Toc] [Back]
which means to append a slash to each path which corresponds to
a directory,
GLOB_NOSORT [Toc] [Back]
which means don't sort the returned pathnames (they are by
default),
GLOB_DOOFFS [Toc] [Back]
which means that pglob->gl_offs slots will be reserved at the
beginning of the list of strings in pglob->pathv,
GLOB_NOCHECK [Toc] [Back]
which means that, if no pattern matches, to return the original
pattern,
GLOB_APPEND [Toc] [Back]
which means to append to the results of a previous call. Do not
set this flag on the first invocation of glob().
GLOB_NOESCAPE [Toc] [Back]
which means that meta characters cannot be quoted by backslashes.
The flags may also include some of the following, which are GNU extensions
and not defined by POSIX.2:
GLOB_PERIOD [Toc] [Back]
which means that a leading period can be matched by meta characters,
GLOB_ALTDIRFUNC [Toc] [Back]
which means that alternative functions pglob->gl_closedir,
pglob->gl_readdir, pglob->gl_opendir, pglob->gl_lstat, and
pglob->gl_stat are used for file system access instead of the
normal library functions,
GLOB_BRACE [Toc] [Back]
which means that csh(1) style brace expresions {a,b} are
expanded,
GLOB_NOMAGIC [Toc] [Back]
which means that the pattern is returned if it contains no
metacharacters,
GLOB_TILDE [Toc] [Back]
which means that tilde expansion is carried out, and
GLOB_ONLYDIR [Toc] [Back]
which means that only directories are matched.
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(), read-
dir(), or stat(). 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 a pointer to the list of matched pathnames.
The first pointer after the last pathname is NULL.
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 invocations.
As a GNU extension, pglob->gl_flags is set to the flags specified, ored
with GLOB_MAGCHAR if any metacharacters were found.
On successful completion, glob() returns zero. Other possible returns
are:
GLOB_NOSPACE [Toc] [Back]
for running out of memory,
GLOB_ABORTED [Toc] [Back]
for a read error, and
GLOB_NOMATCH [Toc] [Back]
for no found matches.
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]);
POSIX.2
The glob() function may fail due to failure of underlying function
calls, such as malloc() or opendir(). These will store their error
code in errno.
The structure elements gl_pathc and gl_offs are declared as size_t in
glibc 2.1, as they should according to POSIX.2, but are declared as int
in libc4, libc5 and glibc 2.0.
ls(1), sh(1), stat(2), exec(3), malloc(3), opendir(3), readdir(3),
wordexp(3)
GNU 1999-09-12 GLOB(3)
[ Back ] |