GLOB(3)

NAME
glob, globfree - find pathnames matching a pattern, free memory from glob()

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
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.

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> as

typedef struct
{
    int gl_pathc;	    /* Count of paths matched so far  */
    char **gl_pathv;    /* List of matched pathnames.  */
    int gl_offs;        /* Slots to be reserved in `gl_pathv'.  */
    int gl_flags;       /* Flags for globbing  */
} 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
    which means to return upon read error (because a directory does not have read permission, for example).

GLOB_MARK
    which means to append a slash to each path which corresponds to a directory,

GLOB_NOSORT
    which means don't sort the returned pathnames (they are by default),

GLOB_DOOFS
    which means that pglob->gl_offs slots will be reserved at the beginning of the list of strings in pglob->pathv,

GLOB_NOCHECK
    which means that, if no pattern matches, to return the original pattern,

GLOB_APPEND
    which means to append to the results of a previous call. Do not set this flag on the first invocation of glob().

GLOB_NOESCAPE
    which means that meta characters cannot be quoted by backspaces, and

GLOB_PERIOD
    which means that a leading period can be matched by meta characters.

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(), readdir(), 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.

RETURN VALUES
On successful completion, glob() returns zero. Other possible returns are:

GLOB_NOSPACE
    for running out of memory,

GLOB_ABEND
    for a read error, and

GLOB_NOMATCH
    for no found matches.

EXAMPLES
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_DOOFS, NULL, &globbuf);
glob("../*.c", GLOB_DOOFS | GLOB_APPEND, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";
globbuf.gl_pathv[1] = "-l";
execvp("ls", &globbuf.gl_pathv[0]);

CONFORMS TO
proposed POSIX.2

BUGS
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.

POSIX.2 is not yet an approved standard; the information in this manpage is subject to change.

SEE ALSO
ls(1), sh(1), exec(2), stat(2), malloc(3), opendir(3), readdir(3), wordexp(3), glob(7)