---
name: "glob"
section: "3"
platform: "macos"
package: "Libc"
version: "1752.120.2"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/glob/3"
---

# glob(3)

generate pathnames matching a pattern

## NAME

**glob**, **glob\_b**, **globfree** – generate pathnames matching a pattern

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<glob.h\>**

*int*\

**glob**(*const char \* restrict pattern*, *int flags*, *int (\*errfunc)(const char \*epath, int errno)*, *glob\_t \* restrict pglob*);

*int*\

**glob\_b**(*const char \* restrict pattern*, *int flags*, *int (^errblk)(const char \*epath, int errno)*, *glob\_t \* restrict pglob*);

*void*\

**globfree**(*glob\_t \*pglob*);

## DESCRIPTION

The **glob**() function is a pathname generator that implements the rules for file name pattern matching used by the shell.

The include file ⟨glob.h⟩ defines the structure type *glob\_t*, which contains at least the following fields:

```
typedef struct {
	size_t gl_pathc;	/* count of total paths so far */
	int gl_matchc;		/* count of paths matching pattern */
	size_t gl_offs;		/* reserved at beginning of gl_pathv */
	int gl_flags;		/* returned flags */
	char **gl_pathv;	/* list of paths matching pattern */
} glob_t;
```

The argument *pattern* is a pointer to a pathname pattern to be expanded. The **glob**() argument matches all accessible pathnames against the pattern and creates a list of the 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 special characters ‘\*’, ‘?’ or ‘\[’.

The **glob**() argument stores the number of matched pathnames into the *gl\_pathc* field, and a pointer to a list of pointers to pathnames into the *gl\_pathv* field. The first pointer after the last pathname is NULL. If the pattern does not match any pathnames, the returned number of matched paths is set to zero.

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 argument *flags* is used to modify the behavior of **glob**(). The value of *flags* is the bitwise inclusive OR of any of the following values defined in ⟨glob.h⟩:

- GLOB\_APPEND

  Append pathnames generated to the ones from a previous call (or calls) to **glob**(). The value of *gl\_pathc* will be the total matches found by this call and the previous call(s). The pathnames are appended to, not merged with the pathnames returned by the previous call(s). Between calls, the caller must not change the setting of the GLOB\_DOOFFS flag, nor change the value of *gl\_offs* when GLOB\_DOOFFS is set, nor (obviously) call **globfree**() for *pglob*.

- GLOB\_DOOFFS

  Make use of the *gl\_offs* field. If this flag is set, *gl\_offs* is used to specify how many NULL pointers to prepend to the beginning of the *gl\_pathv* field. In other words, *gl\_pathv* will point to *gl\_offs* NULL pointers, followed by *gl\_pathc* pathname 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 pathname that is a directory that matches *pattern* has a slash appended.

- GLOB\_NOCHECK

  If *pattern* does not match any pathname, then **glob**() returns a list consisting of only *pattern*, with the number of total pathnames set to 1, and the number of matched pathnames set to 0. The effect of backslash escaping is present in the pattern returned.

- GLOB\_NOESCAPE

  By default, a backslash (‘\\’) character is used to escape the following character in the pattern, avoiding any special interpretation of the character. If GLOB\_NOESCAPE is set, backslash escaping is disabled.

- GLOB\_NOSORT

  By default, the pathnames are sorted in ascending collation order; this flag prevents that sorting (speeding up **glob**()).

The following values may also be included in *flags*, however, they are non-standard extensions to IEEE Std 1003.2 (“POSIX.2”).

- GLOB\_ALTDIRFUNC

  The following additional fields in the pglob structure have been initialized with alternate functions for glob to use to open, read, and close directories and to get stat information on names found in those directories.

  ```
  void *(*gl_opendir)(const char * name);
  struct dirent *(*gl_readdir)(void *);
  void (*gl_closedir)(void *);
  int (*gl_lstat)(const char *name, struct stat *st);
  int (*gl_stat)(const char *name, struct stat *st);
  ```

  This extension is provided to allow programs such as [restore(8)](/restore/8.md) to provide globbing from directories stored on tape.

- GLOB\_BRACE

  Pre-process the pattern string to expand ‘{pat,pat,...}’ strings like [csh(1)](/csh/1.md). The pattern ‘{}’ is left unexpanded for historical reasons (and [csh(1)](/csh/1.md) does the same thing to ease typing of [find(1)](/find/1.md) patterns).

- GLOB\_MAGCHAR

  Set by the **glob**() function if the pattern included globbing characters. See the description of the usage of the *gl\_matchc* structure member for more details.

- GLOB\_NOMAGIC

  Is the same as GLOB\_NOCHECK but it only appends the *pattern* if it does not contain any of the special characters \`\`\*'', \`\`?'' or \`\`\[''. GLOB\_NOMAGIC is provided to simplify implementing the historic [csh(1)](/csh/1.md) globbing behavior and should probably not be used anywhere else.

- GLOB\_TILDE

  Expand patterns that start with ‘~’ to user name home directories.

- GLOB\_LIMIT

  Limit the total number of returned pathnames to the value provided in *gl\_matchc* (default ARG\_MAX). This option should be set for programs that can be coerced into a denial of service attack via patterns that expand to a very large number of matches, such as a long string of ‘\*/../\*/..’.

If, during the search, a directory is encountered that cannot be opened or read and *errfunc* is non-NULL, **glob**() calls *\*errfunc*( *path*, *errno*,). This may be unintuitive: a pattern like ‘\*/Makefile’ will try to [stat(2)](/stat/2.md) ‘foo/Makefile’ even if ‘foo’ is not a directory, resulting in a call to *errfunc*. The error routine can suppress this action by testing for ENOENT and ENOTDIR; however, the GLOB\_ERR flag will still cause an immediate return when this happens.

If *errfunc* returns non-zero, **glob**() stops the scan and returns GLOB\_ABORTED after setting *gl\_pathc* and *gl\_pathv* to reflect any paths already matched. This also happens if an error is encountered and GLOB\_ERR is set in *flags*, regardless of the return value of *errfunc*, if called. If GLOB\_ERR is not set and either *errfunc* is NULL or *errfunc* returns zero, the error is ignored.

The **glob\_b**() function is like **glob**() except that the error callback is a block pointer instead of a function pointer.

The **globfree**() function frees any space associated with *pglob* from a previous call(s) to **glob**() or **glob\_b**().

## RETURN VALUES

On successful completion, **glob**() and **glob\_b**() return zero. In addition, the fields of *pglob* contain the values described below:

- *gl\_pathc*

  contains the total number of matched pathnames so far. This includes other matches from previous invocations of **glob**() or **glob\_b**() if GLOB\_APPEND was specified.

- *gl\_matchc*

  contains the number of matched pathnames in the current invocation of **glob**() or **glob\_b**().

- *gl\_flags*

  contains a copy of the *flags* argument with the bit GLOB\_MAGCHAR set if *pattern* contained any of the special characters \`\`\*'', \`\`?'' or \`\`\['', cleared if not.

- *gl\_pathv*

  contains a pointer to a NULL-terminated list of matched pathnames. However, if *gl\_pathc* is zero, the contents of *gl\_pathv* are undefined.

If **glob**() or **glob\_b**() terminates due to an error, it sets errno and returns one of the following non-zero constants, which are defined in the include file ⟨glob.h⟩:

- GLOB\_NOSPACE

  An attempt to allocate memory failed, or if *errno* was E2BIG, GLOB\_LIMIT was specified in the flags and *pglob-\>gl\_matchc* or more patterns were matched.

- GLOB\_ABORTED

  The scan was stopped because an error was encountered and either GLOB\_ERR was set or *\*errfunc* returned non-zero.

- GLOB\_NOMATCH

  The pattern did not match a pathname and GLOB\_NOCHECK was not set.

The arguments *pglob-\>gl\_pathc* and *pglob-\>gl\_pathv* are still set as specified above.

## EXAMPLES

A rough equivalent of ‘ls -l \*.c \*.h’ can be obtained with the following code:

```
glob_t g;
g.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &g);
glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
g.gl_pathv[0] = "ls";
g.gl_pathv[1] = "-l";
execvp("ls", g.gl_pathv);
```

## CAVEATS

The **glob**() and **glob\_b**() functions will not match filenames that begin with a period unless this is specifically requested (e.g., by ".\*").

## SEE ALSO

[sh(1)](/sh/1.md), [fnmatch(3)](/fnmatch/3.md), [regexp(3)](/regexp/3.md)

## STANDARDS

The current implementation of the **glob**() function *does* *not* conform to IEEE Std 1003.2 (“POSIX.2”). Collating symbol expressions, equivalence class expressions and character class expressions are not supported.

The flags GLOB\_ALTDIRFUNC, GLOB\_BRACE, GLOB\_LIMIT, GLOB\_MAGCHAR, GLOB\_NOMAGIC, and GLOB\_TILDE, and the fields *gl\_matchc* and *gl\_flags* are extensions to the POSIX standard and should not be used by applications striving for strict conformance.

## HISTORY

The **glob**() and **globfree**() functions first appeared in 4.4BSD. The **glob\_b**() function first appeared in Mac OS X 10.6.

## BUGS

Patterns longer than MAXPATHLEN may cause unchecked errors.

The **glob**() and **glob\_b**() functions may fail and set errno for any of the errors specified for the library routines [stat(2)](/stat/2.md), [closedir(3)](/closedir/3.md), [opendir(3)](/opendir/3.md), [readdir(3)](/readdir/3.md), [malloc(3)](/malloc/3.md), and [free(3)](/free/3.md).
