GLOB

Section: C Library Functions (3)
Index Return to Main Contents

BSD mandoc
 

NAME

glob globfree - generate pathnames matching a pattern  

SYNOPSIS

Fd #include <glob.h> Ft int Fn glob const char *pattern int flags const int (*errfunc)(const char *, int) glob_t *pglob Ft void Fn globfree glob_t *pglob  

DESCRIPTION

The Fn 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 Fa glob_t , which contains at least the following fields:

typedef struct {
        int gl_pathc;           /* count of total paths so far */
        int gl_matchc;          /* count of paths matching pattern */
        int 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 Fa pattern is a pointer to a pathname pattern to be expanded. The Fn 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, Fn glob requires search permission on every component of a path except the last and read permission on each directory of any filename component of Fa pattern that contains any of the special characters `*' , `?' or `['

The Fn glob argument stores the number of matched pathnames into the Fa gl_pathc field, and a pointer to a list of pointers to pathnames into the Fa 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 Fa pglob . The Fn glob function allocates other space as needed, including the memory pointed to by Fa gl_pathv .

The argument Fa flags is used to modify the behavior of Fn glob . The value of Fa 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 Fn glob . The value of Fa 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 Fa gl_offs when GLOB_DOOFFS is set, nor (obviously) call Fn globfree for Fa pglob .
GLOB_DOOFFS
Make use of the Fa gl_offs field. If this flag is set, Fa gl_offs is used to specify how many NULL pointers to prepend to the beginning of the Fa gl_pathv field. In other words, Fa gl_pathv will point to Fa gl_offs NULL pointers, followed by Fa gl_pathc pathname pointers, followed by a NULL pointer.
GLOB_ERR
Causes Fn glob to return when it encounters a directory that it cannot open or read. Ordinarily, Fn glob continues to find matches.
GLOB_MARK
Each pathname that is a directory that matches Fa pattern has a slash appended.
GLOB_NOCHECK
If Fa pattern does not match any pathname, then Fn glob returns a list consisting of only Fa pattern , with the number of total pathnames is set to 1, and the number of matched pathnames set to 0. If GLOB_QUOTE is set, its effect is present in the pattern returned.
GLOB_NOSORT
By default, the pathnames are sorted in ascending ASCII order; this flag prevents that sorting (speeding up Fn glob ) .

The following values may also be included in Fa flags , however, they are non-standard extensions to St -p1003.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) to provide globbing from directories stored on tape.

GLOB_BRACE
Pre-process the pattern string to expand `{pat,pat,...}'

strings like csh(1).Thepattern `{}' is left unexpanded for historical reasons (Csh1 does the same thing to ease typing of find(1) patterns).

GLOB_MAGCHAR
Set by the Fn glob function if the pattern included globbing characters. See the description of the usage of the Fa gl_matchc structure member for more details.
GLOB_NOMAGIC
Is the same as GLOB_NOCHECK but it only appends the Fa pattern if it does not contain any of the special characters ``*'', ``?'' or ``[''. GLOB_NOMAGIC is provided to simplify implementing the historic csh(1) globbing behavior and should probably not be used anywhere else.
GLOB_QUOTE
Use the backslash (`\' ) character for quoting: every occurrence of a backslash followed by a character in the pattern is replaced by that character, avoiding any special interpretation of the character.
GLOB_TILDE
Expand patterns that start with `~' to user name home directories.

If, during the search, a directory is encountered that cannot be opened or read and Fa errfunc is non- NULL , Fn glob calls Fa (*errfunc)(path, errno) . This may be unintuitive: a pattern like `*/Makefile' will try to stat(2) `foo/Makefile' even if `foo' is not a directory, resulting in a call to Fa 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 Fa errfunc returns non-zero, Fn glob stops the scan and returns GLOB_ABEND after setting Fa gl_pathc and Fa gl_pathv to reflect any paths already matched. This also happens if an error is encountered and GLOB_ERR is set in Fa flags , regardless of the return value of Fa errfunc , if called. If GLOB_ERR is not set and either Fa errfunc is NULL or Fa errfunc returns zero, the error is ignored.

The Fn globfree function frees any space associated with Fa pglob from a previous call(s) to Fn glob .  

RETURN VALUES

On successful completion, Fn glob returns zero. In addition the fields of Fa pglob contain the values described below:

Fa gl_pathc
contains the total number of matched pathnames so far. This includes other matches from previous invocations of Fn glob if GLOB_APPEND was specified.
Fa gl_matchc
contains the number of matched pathnames in the current invocation of Fn glob .
Fa gl_flags
contains a copy of the Fa flags parameter with the bit GLOB_MAGCHAR set if Fa pattern contained any of the special characters ``*'', ``?'' or ``['', cleared if not.
Fa gl_pathv
contains a pointer to a NULL -terminated list of matched pathnames. However, if Fa gl_pathc is zero, the contents of Fa gl_pathv are undefined.

If Fn glob terminates due to an error, it sets errno and returns one of the following non-zero constants, which are defined in the include file Aq Pa glob.h :

GLOB_NOSPACE
An attempt to allocate memory failed.
GLOB_ABEND
The scan was stopped because an error was encountered and either GLOB_ERR was set or Fa (*errfunc)() returned non-zero.

The arguments Fa pglob->gl_pathc and Fa pglob->gl_pathv are still set as specified above.  

EXAMPLE

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);
 

SEE ALSO

sh(1), fnmatch(3), regexp(3)  

STANDARDS

The Fn glob function is expected to be St -p1003.2 compatible with the exception that the flags GLOB_ALTDIRFUNC, GLOB_BRACE GLOB_MAGCHAR, GLOB_NOMAGIC, GLOB_QUOTE, and GLOB_TILDE, and the fields Fa gl_matchc and Fa gl_flags should not be used by applications striving for strict POSIX conformance.  

HISTORY

The Fn glob and Fn globfree functions first appeared in BSD 4.4  

BUGS

Patterns longer than MAXPATHLEN may cause unchecked errors.

The Fn glob function may fail and set errno for any of the errors specified for the library routines stat(2), closedir(3), opendir(3), readdir(3), malloc(3), and free(3).


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUES
EXAMPLE
SEE ALSO
STANDARDS
HISTORY
BUGS

This document was created by man2html, using the manual pages.
Time: 15:52:54 GMT, January 15, 2023