"tag_17_242" id="tag_17_242">

NAME

glob, globfree — generate pathnames matching a pattern

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

The glob() function is a pathname generator that shall implement the rules defined in XCU 2.14 Pattern Matching Notation , with optional support for rule 3 in XCU 2.14.3 Patterns Used for Filename Expansion .

The structure type glob_t is defined in <glob.h> and includes at least the following members:

Member Type

Member Name

Description

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 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 XBD 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 globfree() function shall not modify errno if pglob was previously used by glob() and not yet freed.

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 an attempt to open or search a pathname as a directory, or an attempt to read an opened directory, fails because of an error condition that is related to file system contents and prevents glob() from expanding the pattern. If this flag is not set, glob() shall not treat such conditions as an error, and shall continue to look for matches. Other error conditions may also be treated the same way as error conditions that are related to file system contents.
GLOB_MARK
For each pathname that matches pattern and is determined to be a directory after pathname resolution, process the pathname so the result is as if the following steps are applied in order:
  1. If the pathname is <slash>, do not modify the pathname and skip the remaining steps.
  2. If the pathname is <slash><slash> and the implementation handles pathname resolution of a pathname starting with exactly two successive <slash> characters differently than it handles a pathname starting with only a single <slash>, do not modify the pathname and skip the remaining steps.
  3. If the pathname does not end with a <slash>, append a <slash> to the pathname and skip the remaining steps.
  4. A <slash> may be appended to the pathname.
  5. If there are multiple <slash> characters at the end of the pathname, all but one of those trailing <slash> characters may be removed from the pathname.
GLOB_NOCHECK
Supports rule 3 in XCU 2.14.3 Patterns Used for Filename Expansion . If pattern does not match any pathname, then glob() shall return a list consisting of only pattern, and the number of matched pathnames is 1.
GLOB_NOESCAPE
Disable backslash escaping.
GLOB_NOSORT
Ordinarily, glob() sorts the matching pathnames according to the current setting of the LC_COLLATE category; see XBD 7.3.2 LC_COLLATE . 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():

  1. The first such call shall not set GLOB_APPEND. All subsequent calls shall set it.
  2. All the calls shall set GLOB_DOOFFS, or all shall not set it.
  3. After the second call, pglob->gl_pathv points to a list containing the following:
    1. Zero or more null pointers, as specified by GLOB_DOOFFS and pglob->gl_offs.
    2. Pointers to the pathnames that were in the pglob->gl_pathv list before the call, in the same order as before.
    3. Pointers to the new pathnames generated by the second call, in the specified order.
  4. The count returned in pglob->gl_pathc shall be the total number of pathnames from the two calls.
  5. 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 globfree() or glob() with the GLOB_APPEND flag.

If errfunc is not a null pointer and, during the search, an attempt to open or search a pathname as a directory, or an attempt to read an opened directory, fails because of an error condition that prevents glob() from expanding the pattern, glob() calls (*errfunc()) with two arguments:

  1. The epath argument is a pointer to the path that failed.
  2. 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.)

If (*errfunc()) is called and returns non-zero, or, optionally, if errfunc is a null pointer and the attempt failed because of an error condition that is not related to file system contents, 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 set of error conditions that are considered to prevent glob() from expanding the pattern shall include [EACCES], [ENAMETOOLONG], and [ELOOP]. It is implementation-defined what other error conditions are included in the set.

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.

ERRORS

The glob() function shall fail and return the corresponding value if:

GLOB_ABORTED
The scan was stopped because (*errfunc()) was called and returned non-zero, or, optionally, errfunc was a null pointer and an attempt to open, read, or search a directory failed because of an error condition that is not related to file system contents, or GLOB_ERR was set.
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.

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 -ld -- *.c

but for some reason:

system("ls -ld -- *.c")

is not acceptable. The application could obtain the same result (except for error handling, omitted here for simplicity) using the sequence:

globbuf.gl_offs = 3;
glob("*.c", GLOB_DOOFFS|GLOB_NOCHECK, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";  // to establish the initial arguments
globbuf.gl_pathv[1] = "-ld"; // that sh -c "ls -ld --" would
globbuf.gl_pathv[2] = "--";  // produce for both examples
execvp("ls", &globbuf.gl_pathv[0]);

Using the same example:

ls -ld -- *.c *.h

could be simulated using GLOB_APPEND as follows:

globbuf.gl_offs = 3;
glob("*.c", GLOB_DOOFFS|GLOB_NOCHECK, NULL, &globbuf);
glob("*.h", GLOB_DOOFFS|GLOB_NOCHECK|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.

It is recommended that (*errfunc()) should always return a non-zero value if the eerrno parameter indicates an error condition that is not related to file system contents. See XRAT C.2.14.3 Patterns Used for Filename Expansion for information about which error conditions are related to file system contents.

Applications that need tilde and parameter expansion should use wordexp().

RATIONALE

It was claimed that the GLOB_DOOFFS flag is unnecessary because it could be simulated using:

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.

Earlier versions of this standard defined the behavior associated with the flag GLOB_MARK as: "Each pathname that is a directory that matches pattern shall have a <slash> appended." This was undesirable if the matched pathname was <slash> or if the matched pathname was <slash><slash> and the implementation treats a leading <slash><slash> differently than it treats a pathname with a single leading <slash>. Only a few implementations were known to conform to this requirement (maybe only one) and there was a lot of variation in the way other implementations behaved. The current wording allows many of the alternative behaviors that were observed, except that the pathnames "/" and "//" (if it is treated differently than "/") must not be modified.

Implementations should consider the following much simpler requirement (which is allowed by the current standard) when processing the GLOB_MARK flag: "Each pathname that matches pattern, is determined to be a directory after pathname resolution, and does not end with a <slash> shall have a <slash> appended."

Implementations differ as to which error conditions they consider prevent glob() from expanding the pattern. The standard requires that [EACCES], [ENAMETOOLONG], and [ELOOP] are included because in these cases the expansion could succeed if performed with a different effective user or group ID, or with an alternative pathname (shorter than {PATH_MAX}, or traversing fewer symbolic links).

Implementations are encouraged to call (*errfunc()) for all error conditions that are related to file system contents which occur when attempting to open or search a pathname as a directory or attempting to read an opened directory. The appropriate way to handle such errors varies according to the provenance of the pattern and what the application will do with the resulting pathnames, and should therefore be for the application to decide. For example, given the pattern "non-existing/*", some applications may want glob() to succeed and return an empty list because there are no existing files that match the pattern, but for others that would not be appropriate, such as if an application asks the user to name a directory containing files to be processed and the user makes a typing mistake when responding; the application will want to alert the user to the mistake instead of behaving as if the user had named an empty directory. If (*errfunc()) is called for [ENOENT] errors, the first application can ignore them in that function, but if (*errfunc()) is not called, the second application cannot achieve what it wants using glob(). Similar reasoning applies for the pattern "regular_file/*" and [ENOTDIR] errors.

FUTURE DIRECTIONS

A future version of this standard may require that (*errfunc()) is called for all error conditions that are related to file system contents which occur when attempting to open or search a pathname as a directory or attempting to read an opened directory.

SEE ALSO

exec , fdopendir , fnmatch , fstatat , readdir , wordexp

XBD 7.3.2 LC_COLLATE , <glob.h>

CHANGE HISTORY

First released in Issue 4. Derived from the ISO POSIX-2 standard.

Issue 5

Moved from POSIX2 C-language Binding to BASE.

Issue 6

The normative text is updated to avoid use of the term "must" for application requirements.

The restrict keyword is added to the glob() prototype for alignment with the ISO/IEC 9899:1999 standard.

Issue 8

Austin Group Defect 385 is applied, adding a requirement that globfree() does not modify errno when passed a pointer to a glob_t that can be freed.

Austin Group Defect 1255 is applied, changing the EXAMPLES section.

Austin Group Defects 1273 and 1275 are applied, clarifying how errors are treated when attempting to open or search a pathname as a directory or attempting to read an opened directory.

Austin Group Defect 1300 is applied, changing the description of GLOB_MARK.

Austin Group Defect 1444 is applied, correcting cross-references to wordexp .

End of informative text.