Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed(1)

grep(1)

sh(1)

fnmatch(3C)

malloc(3C)

malloc(3X)

regexp(5)

glob(3C)

NAME

glob(), globfree() − file name generation function

SYNOPSIS

#include <glob.h>

int glob(

const char *pattern,
int flags,
int (*errfunc)(const char *, int),
glob_t *pglob

);

void globfree(glob_t *pglob);

DESCRIPTION

glob() is a pathname generator.  pattern is a pointer to a pathname pattern to be expanded.  If pattern contains any of the special characters *, ?, or [, pattern is matched against all accessible pathnames.  In order to have access to a pathname, glob() requires:

• Search permission on every component of a path except the last. 

• Read permission on each directory of any filename component of pattern that contains any of the above special characters. 

glob() stores the number of matched pathnames in pglob −> gl_pathc and a pointer to a sorted list of pathnames in pglob−>gl_pathv. The first pointer after the last pathname is a NULL pointer. 

It is the caller’s responsibility to allocate space for the structure pointed to by pglob. glob() allocates other space as needed, including the memory pointed to by gl_pathv. globfree() frees 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 the bit-wise inclusive OR of the following constants defined in <glob.h>:

GLOB_NOESCAPE Disable backslash escaping. 

GLOB_ERR Causes glob() to return when it first encounters a directory that it cannot open or read.  Ordinarily, glob() continues to find matches. 

GLOB_MARK Each pathname that matches pattern and is a directory has a / appended. 

GLOB_NOSORT Ordinarily, glob() sorts the matching pathnames according to the currently active collation sequence as defined by the LC_COLLATE category.  When this flag is used, the order of pathnames returned is unspecified. 

GLOB_NOCHECK If pattern does not match any pathname, glob() returns a list consisting of only pattern, and the number of matched pathnames is 1.

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 points to pglob−>gl_offs NULL pointers, followed by pglob−>gl_pathc pathname pointers, followed by a NULL pointer. 

GLOB_APPEND Append pathnames generated to the ones from a previous call to glob(). 

If GLOB_APPEND is specified in flags, the following rules apply:

• If the application set GLOB_DOOFFS in the first call to glob(), then it also sets it in all subsequent calls to glob(), as long as the same glob_t structure is used for appending. 

• If the application did not set GLOB_DOOFFS in the first call to glob(), then it does not set it in any subsequent calls to glob(), as long as the same glob_t structure is used for appending. 

• If GLOB_DOOFFS is set, the value of pglob->gl_offs must not be modified between calls to glob(). 

• After the second call, pglob->gl_pathv points to a list containing the following:

• Zero or more NULLs, as specified by GLOB_DOOFS and pglob−>gl_offs.

• Pointers to the pathnames that were in the pglob->gl_pathv list before the call, in the same order as before. 

• Pointers to the new pathnames generated by the second call, in the specified order. 

• The count returned in pglob->gl_pathc is the sum of the number of pathnames matched in the previous and current calls to glob(). 

• The application does not modify pglob->gl_pathc or pglob->gl_pathv between calls to glob(). 

If, during the search, a directory is encountered that cannot be opened or read and errfunc is not NULL, glob() calls (∗errfunc)() with two arguments:

• A pointer to the path that failed. 

• The value of errno from the failure. 

If errfunc is called and returns non-zero, 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 NULL or (∗errfunc)() returns zero, the error is ignored.

Pattern Matching Notation

The form of the patterns is the Pattern Matching Notation as qualified for Filename Expansion (see regexp(5)) with the following exceptions:

• Tilde (~) expansion is not performed. 

• Variable expansion is not performed. 

If a filename component ends with a plus sign (+) (indicating a context-dependent file), the plus sign must be explicitly matched by a plus sign in the pattern; it cannot be matched by either the asterisk or question mark special characters, or by bracket expressions. 

EXTERNAL INFLUENCES

Locale

The LC_COLLATE category determines the collating sequence used in compiling and executing regular expressions, and also the order of the returned paths if GLOB_NOSORT is not selected. 

The LC_CTYPE category determines the interpretation of text as single and/or multi-byte characters, and which characters are matched by character class expressions in regular expressions. 

International Code Set Support

Single- and multi-bye character code sets are supported. 

RETURN VALUE

If glob() terminates due to an error, it returns one of the following constants (defined in <glob.h>); otherwise, it returns zero. 

GLOB_NOSPACE An attempt to allocate memory failed. 

GLOB_ABORTED The scan was stopped because GLOB_ERR was set or (∗errfunc)() returned non-zero.

GLOB_NOMATCH The pattern does not match any existing pathname, and GLOB_NOCHECK was not set in flags.

In any case, the argument pglob−>gl_pathc returns the number of matched pathnames and the argument pglob−>gl_pathv contains a pointer to a null-terminated list of matched and sorted pathnames. 

However, if pglob−>gl_pathc is zero, the content of pglob−>gl_pathv is undefined. 

If the pattern argument passed to glob() is badly constructed, glob() returns zero and sets gl_pathc to zero unless GLOB_NOCHECK was set, in which case pattern is returned and gl_pathc is set to 1. 

WARNINGS

GLOB_APPEND must not be set in an initial call to glob(). 

AUTHOR

glob() and globfree() were developed by HP. 

SEE ALSO

ed(1), grep(1), sh(1), fnmatch(3C), malloc(3C), malloc(3X), regexp(5). 

STANDARDS CONFORMANCE

glob(): XPG4, POSIX.2

globfree(): XPG4, POSIX.2

Hewlett-Packard Company  —  HP-UX Release 9.0: August 1992

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026