glob() — Generate path names matching a pattern
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both |
Format
#define _XOPEN_SOURCE
#include <glob.h>
int glob(const char *__restrict__ pattern, int flags,
int (*errfunc)(const char *epath, int eerrno),
glob_t *__restrict__ pglob);
General description
The glob() function is a path name generator that implements the rules defined in topic about pattern matching notation in X/Open CAE Specification, Commands and Utilities, Issue 4, Version 2, with optional support for rule 3 in the topic about patterns used for file name expansion.
- gl_pathc
- Count of paths matched by pattern.
- gl_pathv
- Pointer to a list of matched file names.
- 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, see X/Open CAE Specification, System Interface Definitions, Issue 4, Version 2 topic, LC_COLLATE. 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 0, 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.
- GLOB_APPEND
- Append path names 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 will point to pglob->gl_offs NULL pointers, followed by pglob->gl_pathc path name 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 path name that is a directory that matches pattern has a slash appended.
- GLOB_NOCHECK
- Support rule 3 in the XCU specification, topic, Patterns Used for Filename Expansion. If pattern does not match any path name, then glob() returns a list consisting of only pattern, and the number of matched path names is 1.
- GLOB_NOESCAPE
- Disable backslash escaping.
- GLOB_NOSORT
- Ordinarily, glob() sorts the matching path names according to the current setting of the LC_COLLATE category, see the XBD specification, topic, LC_COLLATE. When this flag is used the order of path names returned is unspecified.
- The first such call must not set GLOB_APPEND. All subsequent calls must set it.
- All calls must set GLOB_DOOFFS, or all must 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 path names that were in the pglob->gl_pathv list before the call, in the same order as before.
- Pointers to the new path names generated by the second call, in the specified order.
- The count returned in pglob->gl_pathc will be the total number of path names from the two calls.
- 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.
- 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(). (Other values may be used to report other errors not explicitly documented for those functions.)
Returned value
If successful, glob() returns 0. The argument pglob->gl_pathc returns the number of matched path names and the argument pglob->gl_pathv contains a pointer to a NULL-terminated list of matched and sorted path names. However, if pglob->gl_pathc is 0, the content of pglob->gl_pathv is undefined.
- GLOB_ABORTED
- The scan was stopped because GLOB_ERR was set or (*errfunc()) returned nonzero.
- GLOB_NOMATCH
- The pattern does not match any existing path name, and GLOB_NOCHECK was notset in flags.
- GLOB_NOSPACE
- An attempt to allocate memory failed.
If (*errfunc()) is called and returns nonzero, 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.