#include <glob.h> int glob(const char *restrict pattern, int flags, int(*errfunc)(const char *epath, int eerrno), glob_t *restrict pglob); void globfree(glob_t *pglob);
The structure type glob_t is defined in <glob.h> and includes at least the following members:
|
The argument pattern is a pointer to a pathname pattern to be expanded. The glob() function shall match all accessible pathnames against this pattern and develop 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 '['.
The glob() function shall store 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 shall be in sort order as defined by the current setting of the LC_COLLATE category; see the Base Definitions volume of POSIX.1-2017, Section 7.3.2, LC_COLLATE. The first pointer after the last pathname shall be a null pointer. If the pattern does not match any pathnames, the returned number of matched paths is set to 0, and the contents of pglob->gl_pathv are implementation-defined.
It is the caller's responsibility to create the structure pointed to by pglob. The glob() function shall allocate other space as needed, including the memory pointed to by gl_pathv. The globfree() function shall free 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>:
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():
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:
If (()*errfunc ) is called and returns non-zero, or if the GLOB_ERR flag is set in flags, glob() shall stop the scan and return 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 shall be ignored.
The glob() function shall not fail because of large files.
The globfree() function shall not return a value.
If glob() terminates due to an error, it shall return 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.
The following sections are informative.
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); ...
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. The sh utility might use this 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.
Applications that need tilde and parameter expansion should use wordexp().
new = (char **)malloc((n + pglob->gl_pathc + 1) * sizeof(char *)); (void) memcpy(new+n, pglob->gl_pathv, pglob->gl_pathc * sizeof(char *)); (void) memset(new, 0, n * sizeof(char *)); free(pglob->gl_pathv); pglob->gl_pathv = new;
However, this assumes that the memory pointed to by gl_pathv is a block that was separately created using malloc(). This is not necessarily the case. An application should make no assumptions about how the memory referenced by fields in pglob was allocated. It might have been obtained from malloc() in a large chunk and then carved up within glob(), or it might have been created using a different memory allocator. It is not the intent of the standard developers to specify or imply how the memory used by glob() is managed.
The GLOB_APPEND flag would be used when an application wants to expand several different patterns into a single list.
The Base Definitions volume of POSIX.1-2017, Section 7.3.2, LC_COLLATE, <glob.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .