|
|
#include <glob.h>int glob(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob);
void globfree(glob_t *pglob);
glob( ) matches all accessible pathnames against this pattern and generates a list of all matching pathnames. A pointer to this list of matching pathnames, together with the number of generated pathnames, are stored by glob( ) on its return. The structure of type glob_t, pointed to by pglob, is used to store these information.
A pathname is accessible if glob( ) has search permission on every component of the pathname except the last one, and if glob( ) has read permission on each directory of a filename component in pattern when any of the following special characters is part of that component:
* ? [
The structure type glob_t is defined in the header file <glob.h> and includes at least the following members:
Member Type | Member Name | Description |
size_t |
gl_pathc
| Number of matched pathnames |
char ** |
gl_pathv
| Pointer to a list of matched pathnames |
size_t |
gl_offs
| Number of pointers to reserve at |
the beginning of gl_pathv array
|
gl_pathv
.
globfree(S) frees all memory associated with pglob, but does not free the pglob structure itself.
The default behavior of glob( ) can be modified by the flags argument, formed from a bitwise inclusive OR of zero or more of the following flags defined in <glob.h>:
->gl_pathv
is pglob->gl_offs
.
In other words, in the list of pointers pglob->gl_pathv
pointed to,
the first pglob->gl_offs
pointers are null,
the next pglob->gl_pathc
pointers are pathname pointers,
and the final pointer is a null pointer again.
->gl_pathv
a list consisting of only pattern and sets
pglob->gl_pathc
to 1.
->gl_pathv
contains the following after the second call:
->gl_offs
null pointers if
GLOB_DOOFFS is set
->gl_pathc
is updated to the total number of pathnames
from all previous calls,
including the latest one.
If the GLOB_ERR flag is set,
glob( )
stops the search immediately when it cannot open or
cannot read a directory during its search.
After setting gl_pathc
and gl_pathv
in pglob to reflect the paths already scanned,
it returns GLOB_ABORTED.
If the GLOB_ERR flag is not set and if errfunc is not a null pointer, glob( ) calls (*errfunc)() when it cannot open or cannot read a directory during its search. Two arguments are passed to (*errfunc)():
gl_pathc
and gl_pathv
.
Otherwise, i.e., if GLOB_ERR is not set, and if either errfunc is a null pointer or (*errfunc)() returns zero, the error is ignored.
Note that gl_pathc
and
gl_pathv
contain partial results even if
glob( )
fails.
However, if gl_pathc
is zero, the content of
gl_pathv
is meaningless even if
glob( )
did not return an error.
If wildcards are used in pattern to expand a pathname but otherwise the pattern should be treated simply as a string, the GLOB_NOCHECK flag could be used. For example, the sh utility might use this for option-arguments.
When pathnames are appended to previously generated pathnames, the new pathnames are not sorted together with pathnames generated in previous calls. This mirrors the shell's handling of pathname expansion when multiple expansions are done on a command line.
As an SCO OpenServer specific feature, glob( ) also examines its pattern argument and returns GLOB_BADPAT if it detects an invalid pathname pattern. For example, the pattern [[:alpha]] causes glob( ) to return GLOB_BADPAT because of the missing colon (:) after alpha. To use this feature and still retain XPG4 compliance for an application program, conditionally include the code that detects GLOB_BADPAT, as in:
switch (glob(pattern, flags, NULL, &globbuf)) { case 0: /* Code for match */ break; case GLOB_NOMATCH: /* Code for no match */ break; . . . #ifdef GLOB_BADPAT case GLOB_BADPAT: /* Bad pattern error handling */ break; #endif /* GLOB_BADPAT */ . . . }The rules used in expanding the pathname pattern are defined in the X/Open CAE Specification, Commands and Utilities, Issue 4, 1992 (XCU), Section 2.13, Pattern Matching Notation. Optional support for rule 3 in the XCU specification, Section 2.13.3, Patterns used for Filename Expansion, is enabled through the flag GLOB_NOCHECK.
->gl_pathc
, and
pglob->gl_pathv
points to
a sorted list of matched pathnames.
The pointer immediately after the last pathname is a null pointer.
The flags GLOB_NOCHECK and GLOB_NOSORT
can modify the values returned in the structure *pglob.
When no match is found,
glob( )
returns GLOB_NOMATCH,
gl_pathc
is zero and the content of gl_pathv
is meaningless.
glob( ) returns GLOB_NOSYS and sets errno to ENOSYS. globfree( ) returns and sets errno to ENOSYS.
->gl_pathc
and
pglob->gl_pathv
are still set as described above,
and may contain partial results.
but for some reason,
system("ls -l *.c *.h");is not acceptable. This can be approximately simulated by using GLOB_DOOFFS and GLOB_APPEND as follows:
... strcpy(str1, "*.c"); /* or obtain the strings from standard input */ strcpy(str2, "*.h"); ... globbuf.gl_offs = 2; /* reserve slots for "ls" and "-l" */ glob(str1, GLOB_DOOFFS, NULL, &globbuf); glob(str2, GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf); globbuf.gl_pathv[0] = "ls"; globbuf.gl_pathv[1] = "-l"; execvp("ls", &globbuf.gl_pathv[0]); /* simulate "ls -l *.c *.h" */
Use fnmatch(S) to examine if a pathname matches a given pattern.