1GLOB(3)                    Linux Programmer's Manual                   GLOB(3)
2
3
4

NAME

6       glob,  globfree  -  find pathnames matching a pattern, free memory from
7       glob()
8

SYNOPSIS

10       #include <glob.h>
11
12       int glob(const char *pattern, int flags,
13                int (*errfunc) (const char *epath, int eerrno),
14                glob_t *pglob);
15       void globfree(glob_t *pglob);
16

DESCRIPTION

18       The glob() function searches for all  the  pathnames  matching  pattern
19       according  to  the  rules  used  by  the shell (see glob(7)).  No tilde
20       expansion or parameter substitution is done; if  you  want  these,  use
21       wordexp(3).
22
23       The globfree() function frees the dynamically allocated storage from an
24       earlier call to glob().
25
26       The results of a glob() call are stored in the structure pointed to  by
27       pglob.   This  structure  is  of type glob_t (declared in <glob.h>) and
28       includes the following elements defined by POSIX.2 (more may be present
29       as an extension):
30
31           typedef struct {
32               size_t   gl_pathc;    /* Count of paths matched so far  */
33               char   **gl_pathv;    /* List of matched pathnames.  */
34               size_t   gl_offs;     /* Slots to reserve in gl_pathv.  */
35           } glob_t;
36
37       Results are stored in dynamically allocated storage.
38
39       The  argument  flags  is  made up of the bitwise OR of zero or more the
40       following symbolic constants, which modify the behavior of glob():
41
42       GLOB_ERR
43              Return upon a read error (because a directory does not have read
44              permission,  for example).  By default, glob() attempts carry on
45              despite errors, reading all of the directories that it can.
46
47       GLOB_MARK
48              Append a slash to each path which corresponds to a directory.
49
50       GLOB_NOSORT
51              Don't sort the returned pathnames.  The only reason to  do  this
52              is  to save processing time.  By default, the returned pathnames
53              are sorted.
54
55       GLOB_DOOFFS
56              Reserve pglob->gl_offs slots at the beginning  of  the  list  of
57              strings in pglob->pathv.  The reserved slots contain NULL point‐
58              ers.
59
60       GLOB_NOCHECK
61              If no pattern matches, return the original pattern.  By default,
62              glob() returns GLOB_NOMATCH if there are no matches.
63
64       GLOB_APPEND
65              Append  the  results  of  this  call  to  the  vector of results
66              returned by a previous call to glob().  Do not set this flag  on
67              the first invocation of glob().
68
69       GLOB_NOESCAPE
70              Don't  allow  backslash ('\') to be used as an escape character.
71              Normally, a backslash can be used to quote the following charac‐
72              ter,  providing  a  mechanism  to  turn  off the special meaning
73              metacharacters.
74
75       flags may also include any of the following, which are  GNU  extensions
76       and not defined by POSIX.2:
77
78       GLOB_PERIOD
79              Allow  a  leading  period  to  be matched by metacharacters.  By
80              default, metacharacters can't match a leading period.
81
82       GLOB_ALTDIRFUNC
83              Use alternative functions pglob->gl_closedir, pglob->gl_readdir,
84              pglob->gl_opendir,  pglob->gl_lstat, and pglob->gl_stat for file
85              system access instead of the normal library functions.
86
87       GLOB_BRACE
88              Expand csh(1) style brace expressions of the form {a,b}.   Brace
89              expressions  can  be  nested.  Thus, for example, specifying the
90              pattern "{foo/{,cat,dog},bar}" would return the same results  as
91              four separate glob() calls using the strings: "foo/", "foo/cat",
92              "foo/dog", and "bar".
93
94       GLOB_NOMAGIC
95              If the pattern contains no  metacharacters  then  it  should  be
96              returned  as  the  sole  matching word, even if there is no file
97              with that name.
98
99       GLOB_TILDE
100              Carry out tilde expansion.  If a tilde ('~') is the only charac‐
101              ter  in the pattern, or an initial tilde is followed immediately
102              by a slash ('/'), then the home directory of the caller is  sub‐
103              stituted  for  the  tilde.  If an initial tilde is followed by a
104              username (e.g., "~andrea/bin"), then the tilde and username  are
105              substituted by the home directory of that user.  If the username
106              is invalid, or the home directory cannot be determined, then  no
107              substitution is performed.
108
109       GLOB_TILDE_CHECK
110              This  provides behavior similar to that of GLOB_TILDE.  The dif‐
111              ference is that if the username is invalid, or the  home  direc‐
112              tory  cannot  be  determined,  then instead of using the pattern
113              itself as the name, glob() returns GLOB_NOMATCH to  indicate  an
114              error.
115
116       GLOB_ONLYDIR
117              This  is  a hint to glob() that the caller is interested only in
118              directories that match the pattern.  If the  implementation  can
119              easily  determine file-type information, then nondirectory files
120              are not returned to the caller.  However, the caller must  still
121              check that returned files are directories.  (The purpose of this
122              flag is merely to optimize performance when the caller is inter‐
123              ested only in directories.)
124
125       If  errfunc is not NULL, it will be called in case of an error with the
126       arguments epath, a pointer to the path which failed,  and  eerrno,  the
127       value  of  errno as returned from one of the calls to opendir(3), read‐
128       dir(3), or stat(2).  If errfunc returns nonzero, or if GLOB_ERR is set,
129       glob() will terminate after the call to errfunc.
130
131       Upon  successful return, pglob->gl_pathc contains the number of matched
132       pathnames and pglob->gl_pathv contains a pointer to the list of  point‐
133       ers to matched pathnames.  The list of pointers is terminated by a NULL
134       pointer.
135
136       It is possible to  call  glob()  several  times.   In  that  case,  the
137       GLOB_APPEND flag has to be set in flags on the second and later invoca‐
138       tions.
139
140       As a GNU extension, pglob->gl_flags is set to the flags specified, ored
141       with GLOB_MAGCHAR if any metacharacters were found.
142

RETURN VALUE

144       On  successful completion, glob() returns zero.  Other possible returns
145       are:
146
147       GLOB_NOSPACE
148              for running out of memory,
149
150       GLOB_ABORTED
151              for a read error, and
152
153       GLOB_NOMATCH
154              for no found matches.
155

CONFORMING TO

157       POSIX.2, POSIX.1-2001.
158

NOTES

160       The structure elements gl_pathc and gl_offs are declared as  size_t  in
161       glibc  2.1, as they should be according to POSIX.2, but are declared as
162       int in libc4, libc5 and glibc 2.0.
163

BUGS

165       The glob() function may fail due  to  failure  of  underlying  function
166       calls,  such  as malloc(3) or opendir(3).  These will store their error
167       code in errno.
168

EXAMPLE

170       One example of use is the following code, which simulates typing
171
172           ls -l *.c ../*.c
173
174       in the shell:
175
176           glob_t globbuf;
177
178           globbuf.gl_offs = 2;
179           glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
180           glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
181           globbuf.gl_pathv[0] = "ls";
182           globbuf.gl_pathv[1] = "-l";
183           execvp("ls", &globbuf.gl_pathv[0]);
184

SEE ALSO

186       ls(1), sh(1),  stat(2),  exec(3),  fnmatch(3),  malloc(3),  opendir(3),
187       readdir(3), wordexp(3), glob(7)
188

COLOPHON

190       This  page  is  part of release 3.25 of the Linux man-pages project.  A
191       description of the project, and information about reporting  bugs,  can
192       be found at http://www.kernel.org/doc/man-pages/.
193
194
195
196GNU                               2007-10-10                           GLOB(3)
Impressum