|

|
|
|
#include <glob.h>
int
glob(const char *pattern, int flags,
const int (*errfunc)(const char *, int), glob_t *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 {
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 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 file-
name 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 de-
fined 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_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() re-
turns a list consisting of only 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
glob()).
The following values may also be included in flags, however, they are
non-standard extensions to IEEE Std1003.2 (``POSIX'').
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 re-
store(8) to provide globbing from directories stored on
tape.
GLOB_BRACE Pre-process the pattern string to expand `{pat,pat,...}'
strings like csh(1). The pattern `{}' is left unexpand-
ed for historical reasons. (Csh(1) does the same thing
to ease typing of find(1) patterns.)
GLOB_MAGCHAR Set by the glob() function if the pattern included glob-
bing 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 char-
acters ``*'', ``?'' 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 oc-
currence of a backslash followed by a character in the
pattern is replaced by that character, avoiding any spe-
cial interpretation of the character.
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 globfree() function frees any space associated with pglob from a pre-
vious call(s) to glob().
RETURN VALUES
On successful completion, glob() returns 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() if GLOB_APPEND was specified.
gl_matchc contains the number of matched pathnames in the current in-
vocation of glob().
gl_flags contains a copy of the flags parameter 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() 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.
GLOB_ABEND The scan was stopped because an error was encountered and
either GLOB_ERR was set or (*errfunc)() returned non-zero.
The arguments pglob->gl_pathc and pglob->gl_pathv are still set as speci-
fied 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
The glob() function may fail and set errno for any of the errors speci-
fied for the library routines stat(2), closedir(3), opendir(3),
readdir(3), malloc(3), and free(3).
BSD Experimental April 16, 1994 4
|





|
 |
|
 |
Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved. |
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. |
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |