#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 globfree() function frees any memory allocated by glob() associated with pglob.
The argument pattern is a pointer to a path name pattern to be expanded. The glob() function matches all accessible path names against this pattern and develops a list of all path names that match. In order to have access to a path name, 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:
* ? [
- size_t gl_pathc
- Count of paths matched by pattern.
- char **gl_pathv
- Pointer to a list of matched path names.
- size_t gl_offs
- Slots to reserve at the beginning of gl_pathv.
The glob() function stores the number of matched path names into pglob->gl_pathc and a pointer to a list of pointers to path names into pglob->gl_pathv. The path names are in sort order as defined by the current setting of the LC_COLLATE category. The first pointer after the last path name is a NULL pointer. If the pattern does not match any path names, the returned number of matched paths is set to zero, and the contents of pglob->gl_pathv are implementation-dependent.
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>:
The GLOB_APPEND flag can be used to append a new set of path names 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():
- a.
- Zero or more NULL pointers, as specified by GLOB_DOOFFS and pglob->gl_offs.
- b.
- Pointers to the path names that were in the pglob->gl_pathv list before the call, in the same order as before.
- c.
- Pointers to the new path names generated by the second call, in the specified order.
The following constants are defined as error return values for glob():
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.
The globfree() function returns no value.
If a utility needs to see if a path name matches a given pattern, it can use fnmatch(3C) .
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 path name if wildcards are specified, but wants to treat the pattern as just a string otherwise.
The new path names generated by a subsequent call with GLOB_APPEND are not sorted together with the previous path names. This mirrors the way that the shell handles path name expansion when multiple expansions are done on a command line.
Applications that need tilde and parameter expansion should use the wordexp() function.
ls -l *.cbut 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); ...