glob(3C) glob(3C)
NAME
glob, globfree - generate pathnames matching a pattern
SYNOPSIS
#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);
DESCRIPTION
glob is a pathname generator.
The structure type glob_t is defined in the header <glob_h>.
pattern is a pointer to a pathname pattern to be expanded.
glob matches all accessible pathnames against this pattern and
develops a list of all 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 following special characters:
* ? [
glob stores the number of matched pathnames into pglob->
gl_pathc and a pointer to a list of pointers to pathnames into
pglob-> gl_pathv. The pathnames are in sort order as defined
by the current setting of the LC_COLLATE category. The first
pointer after the last pathname is a null pointer. If the
pattern does not match any pathnames, the returned number of
matched paths is set to zero, and the contents of pglob->
gl_pathv are undefined.
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
globfree function frees any space associated with pglob from a
previous call to glob.
The flags argument is used to control the behavior of glob.
The value of flags is a bitwise inclusive OR of zero or more
of the following constants, which are defined in the header
<glob.h>:
Copyright 1994 Novell, Inc. Page 1
glob(3C) glob(3C)
GLOB_APPEND Append newly formed pathnames to previously
obtained ones
GLOB_DOOFFS A specification of the number of null
pointers that should be added to the start
of pglob-> gl_pathv
GLOB_ERR This is to ensure that if an error occurs,
glob is returned
GLOB_MARK A slash is appended to each directory
pathname matching pattern
GLOB_NOCHECK If there is no match between a pathname and
pattern then a list is returned which
contains pattern only
GLOB_NOESCAPE disable backslash escaping
GLOB_NOSORT Pathnames that are returned are not to be
sorted
GLOB_APPEND can be used to append a new set of pathnames to
those found in a previous call to glob. The following rules
apply when two or more calls to glob are made with the same
value of pglob and without intervening calls to globfree.
1.
The first such call must not set GLOB_APPEND. All
subsequent calls must set it.
2.
All the calls must set GLOB_DOOFFS, or all must not set
it.
3.
After the second call, pglob-> gl_pathv points to a list
containing the following:
a.
Zero or more null pointers, as specified by
GLOB_DOOFFS and pglob-> gl_offs.
b.
Pointers to the pathnames that were in the pglob->
gl_pathv list before the call, in the same order as
Copyright 1994 Novell, Inc. Page 2
glob(3C) glob(3C)
before.
c.
Pointers to the new pathnames generated by the
second call, in the specified order.
4.
The count returned in pglob-> gl_pathc will be the total
number of pathnames from the two calls.
5.
The application can change any of the fields after a call
to glob. If it does, it must reset them to the original
value before a subsequent call, using the same pglob
value, to globfree or glob with the GLOB_APPEND flag.
If, during the search, a directory is encountered that cannot
be opened or read and errfunc is not a null pointer, glob
calls (*errfunc) with two arguments:
1.
The epath argument is a pointer to the path that failed.
2.
The eerrno argument is the value of errno from the
failure, as set by the opendir, readdir or stat
functions. (Other values may be used to report other
errors not explicitly documented for those functions.)
Return Values
On successful completion, glob returns zero. The argument
pglob-> gl_pathv returns the number of matched pathnames and
the argument pglob-> gl_pathw contains a pointer to a null-
terminated list of matched and sorted pathnames. However, if
pglob-> gl_pathc is zero, the contents of pglob-> gl_pathv is
undefined.
The globfree function returns no value.
If glob terminates due to an error, it returns one of the
non-zero constants defined in <glob.h>. The arguments pglob->
gl_pathc and pglob-> gl_pathv are still set as defined above.
Errors
The following constants are defined as error return values for
glob:
Copyright 1994 Novell, Inc. Page 3
glob(3C) glob(3C)
GLOB_ABORTED Because (*errfunc) returned zero or GLOB_ERR
was set, scanning was terminated.
GLOB_NOMATCH There is no match between the pattern and
any pathname that exists. GLOB_NOCHECK was
not set in flags.
GLOB_NOSPACE Memory allocation failure
If (*errfunc) is called and returns non-zero, or if the
GLOB_ERR flag is set in flags, glob stops the scan and returns
GLOB_ABORTED after setting gl_pathc and gl_pathv in pglob to
reflect the paths already scanned. If GLOB_ERR is not set and
either errfunc is a null pointer or (*errfunc) returns zero,
the error is ignored.
USAGE
This function is not provided for the purpose of enabling
utilities to perform pathname expansion on their arguments, as
this operation is performed by the shell, and utilities are
explicitly not expected to redo this. Instead, it is provided
for applications that need to do pathname expansion on strings
obtained from other sources, such as a pattern typed by a user
or read from a file.
If a utility needs to see if a pathname matches a given
pattern, it can use fnmatch.
Note that gl_pathc and gl_pathv have meaning even if glob
fails. This allows glob to report partial results in the
event of an error. However, if gl_pathc is zero, gl_pathv is
unspecified even if glob did not return an error.
The GLOB_NOCHECK option could be used when an application
wants to expand a pathname if wildcards are specified, but
wants to treat the pattern as just a string otherwise.
The new pathnames generated by a subsequent call with
GLOB_APPEND are not sorted together with the previous
pathnames. This mirrors the way that the shell handles
pathname expansion when multiple expansions are done on a
command line.
Examples
One use of the GLOB_DOOFFS flag is by applications that build
an argument list for use with the execv, execve, or execvp
Copyright 1994 Novell, Inc. Page 4
glob(3C) glob(3C)
functions. Suppose, for example, that an application wants to
do the equivalent of:
ls -l *.c
but for some reason:
system("ls -l *.c")
is not acceptable. The application could obtain approximately
the same result using the sequence:
globbuf.gl_offs = 2;
glob ("*.c", GLOB_DOOFFS, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";
globbuf.gl_pathv[1] = "-l";
execvp ("ls", &globbuf.gl_pathv[0]);
Using the same example:
ls -l *.c *.h
could be approximately simulated using GLOB_APPEND as follows:
globbuf.gl_offs = 2;
glob ("*.c", GLOB_DOOFFS, NULL, &globbuf);
glob ("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf);
...
REFERENCES
glob(5), fnmatch(3C), stat(2).
Copyright 1994 Novell, Inc. Page 5