GLOB(3P)
โจ GLOB(3P) โจ
okay so this one is from the POSIX Programmer's Manual, and before we even get into it, bestie, read this first:
๐ฆ PROLOG ๐ฆ
this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ โ so go consult the corresponding Linux manual page for the details of Linux behavior โ or, plot twist, the interface may not even be implemented on Linux at all. ๐
๐ NAME ๐
glob, globfree โ generate pathnames matching a pattern. that's the whole vibe. ๐
๐ SYNOPSIS ๐
#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);
๐ DESCRIPTION ๐
so glob() is a pathname generator. it shall implement the rules defined in the Shell and Utilities volume of POSIX.1โ2017, Section 2.13, Pattern Matching Notation, with optional support for rule 3 in the Shell and Utilities volume of POSIX.1โ2017, Section 2.13.3, Patterns Used for Filename Expansion. periodt.
the structure type glob_t is defined in <glob.h> and it includes at least these members:
size_tgl_pathcโ Count of paths matched bypattern. ๐งฎchar **gl_pathvโ Pointer to a list of matched pathnames. ๐size_tgl_offsโ Slots to reserve at the beginning ofgl_pathv. ๐ช๐ช
the argument pattern is a pointer to a pathname pattern to be expanded. glob() shall match all accessible pathnames against this pattern and develop a list of all pathnames that match. and here's the tea on access: 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 these special characters: '*', '?', and '['. ๐
glob() 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. and 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 โ so like, don't go poking at that. ๐
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(). no leaks, we don't do that here. ๐ซถ
๐ the flags ๐
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 toglob(). ๐งตGLOB_DOOFFSโ Make use ofpglob->gl_offs. If this flag is set,pglob->gl_offsis used to specify how many null pointers to add to the beginning ofpglob->gl_pathv. in other words,pglob->gl_pathvshall point topglob->gl_offsnull pointers, followed bypglob->gl_pathcpathname pointers, followed by a null pointer.GLOB_ERRโ Causeglob()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 matchespatternshall have a<slash>appended. ๐โก๏ธ๐/GLOB_NOCHECKโ Supports rule 3 in the Shell and Utilities volume of POSIX.1โ2017, Section 2.13.3, Patterns Used for Filename Expansion. ifpatterndoes not match any pathname, thenglob()shall return a list consisting of onlypattern, and the number of matched pathnames is1.GLOB_NOESCAPEโ Disable backslash escaping. ๐ โ๏ธ\GLOB_NOSORTโ Ordinarily,glob()sorts the matching pathnames according to the current setting of theLC_COLLATEcategory; see the Base Definitions volume of POSIX.1โ2017, Section 7.3.2,LC_COLLATE. when this flag is used, the order of pathnames returned is unspecified.
๐ง the GLOB_APPEND rules ๐ง
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_pathvpoints to a list containing the following:- a. Zero or more null pointers, as specified by
GLOB_DOOFFSandpglob->gl_offs. - b. Pointers to the pathnames that were in the
pglob->gl_pathvlist before the call, in the same order as before. - c. Pointers to the new pathnames generated by the second call, in the specified order.
- a. Zero or more null pointers, as specified by
- The count returned in
pglob->gl_pathcshall 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 samepglobvalue, toglobfree()orglob()with theGLOB_APPENDflag.
๐ญ when stuff goes wrong ๐ญ
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
epathargument is a pointer to the path that failed. - The
eerrnoargument is the value oferrnofrom the failure, as set byopendir(),readdir(), orstat(). (Other values may be used to report other errors not explicitly documented for those functions.)
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.
and one iconic little guarantee: the glob() function shall not fail because of large files. ๐ช
๐ RETURN VALUE ๐
upon successful completion, glob() shall return 0. the argument pglob->gl_pathc shall return the number of matched pathnames and the argument pglob->gl_pathv shall contain a pointer to a null-terminated list of matched and sorted pathnames. however, if pglob->gl_pathc is 0, the content of pglob->gl_pathv is undefined.
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 โ yes, even on the fail path, they mean something. ๐ซก
๐ ERRORS ๐
the glob() function shall fail and return the corresponding value if:
GLOB_ABORTEDโ The scan was stopped becauseGLOB_ERRwas set or(()*errfunc )returned non-zero.GLOB_NOMATCHโ The pattern does not match any existing pathname, andGLOB_NOCHECKwas not set inflags.GLOB_NOSPACEโ An attempt to allocate memory failed. ๐ต
fyi bestie: the following sections are informative. ๐
๐ EXAMPLES ๐
one use of the GLOB_DOOFFS flag is by applications that build an argument list for use with execv(), execve(), or execvp(). suppose, for example, that an application wants to do the equivalent of:
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);
...
๐ธ APPLICATION USAGE ๐ธ
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. 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(). ๐
โจ RATIONALE โจ
okay so bestie, picture this: someone was like โGLOB_DOOFFS is so unnecessaryโ and they were like โwe can just simulate it with this:โ ๐
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;
But wait. That whole little trick assumes that the memory pointed to by gl_pathv is a block that was separately created using malloc(). This is not necessarily the case, bestie! 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. โจ
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. Literally none. Thatโs it. The future is empty, bestie. ๐
๐ SEE ALSO ๐
exec,fdopendir(),fnmatch(),fstatat(),readdir(), Section 2.6, Word Expansions- The Base Definitions volume of
POSIX.1โ2017, Section 7.3.2,LC_COLLATE,<glob.h>
๐ COPYRIGHT ๐
okay legal bestie moment, hereโs the exact copyright tea โ:
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
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 .
Thatโs the whole page, bestie. This came from IEEE/The Open Group, 2017, and itโs GLOB(3P). Stay globbing ๐ ๐ฆ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms