Syntax | #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 is a pathname generator that implements the rules defined in XCU Pattern Matching Notation , with optional support for rule 3 in XCU Patterns Used for Filename Expansion . The structure type glob_t is defined in glob.h and includes at least the following members: size_t gl_pathc
| Count of paths matched by pattern. | char ** gl_pathv
| Pointer to a list of matched pathnames. | size_t gl_offs
| Slots to reserve at the beginning of gl_pathv. |
The argument pattern is a pointer to a pathname pattern to be expanded. The glob() function 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: '*', '?', and '['. 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 glob.h : GLOB_APPEND
| Append pathnames generated to the ones from a previous call to glob() . | GLOB_DOOFFS
| Make use of pglob->gl_offs. If this flag is set, pglob->gl_offs is used to specify how many null pointers to add to the beginning of pglob->gl_pathv. In other words, pglob->gl_pathv shall point to pglob->gl_offs null pointers, followed by pglob->gl_pathc pathname pointers, followed by a null pointer. | GLOB_ERR
| Cause 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 shall have a '/' appended. | GLOB_NOCHECK
| Supports rule 3 in XCU Patterns Used for Filename Expansion. If pattern does not match any pathname, then glob() returns 0 and a pointer to a list consisting of only pattern, and the number of matched pathnames is 1. If GLOB_NOCHECK is not set, and pattern does not match any pathname, then glob() returns GLOB_NOMATCH , and the number of matched pathnames is 0. | GLOB_NOESCAPE
| Disable backslash escaping. | GLOB_NOSORT
| Ordinarily, glob() sorts the matching pathnames according to the current setting of the LC_COLLATE category. When this flag is used, the order of pathnames returned is unspecified. |
The GLOB_APPEND flag can be used to append a new set of pathnames to those found in a previous call to glob() . The following rules apply to applications when two or more calls to glob() are made with the same value of pglob and without intervening calls to globfree() : The first such call shall not set GLOB_APPEND . All subsequent calls shall set it. All the calls shall set GLOB_DOOFFS , or all shall not set it. After the second call, pglob->gl_pathv points to a list containing the following: Zero or more null pointers, as specified by GLOB_DOOFFS and pglob->gl_offs. Pointers to the pathnames that were in the pglob->gl_pathv list before the call, in the same order as before. Pointers to the new pathnames generated by the second call, in the specified order.
The count returned in pglob->gl_pathc shall be the total number of pathnames from the two calls. The application can change any of the fields after a call to glob() . If it does, the application shall reset them to the original value before a subsequent call, using the same pglob value, to glob() with the GLOB_APPEND flag or globfree() .
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: The epath argument is a pointer to the path that failed. The eerrno argument is the value of errno from the failure, as set by opendir() , readdir() , or stat() .
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 0, the error is ignored. The following constants are defined as error return values: GLOB_ABORTED
| The scan was stopped because 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 in flags. | GLOB_NOSPACE
| An attempt to allocate memory failed. | Extension | GLOB_ABORTED
| Bad flag passed to glob() . | GLOB_BADPAT
| Bad pattern passed to glob() . (End) |
|
Notes | 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 0, 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. This can be used for option-arguments, for example. 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. |