1GLOB(P)                    POSIX Programmer's Manual                   GLOB(P)
2
3
4

NAME

6       glob, globfree - generate pathnames matching a pattern
7

SYNOPSIS

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

DESCRIPTION

18       The  glob()  function  is a pathname generator that shall implement the
19       rules   defined   in   the    Shell    and    Utilities    volume    of
20       IEEE Std 1003.1-2001,  Section  2.13,  Pattern  Matching Notation, with
21       optional support for rule 3  in  the  Shell  and  Utilities  volume  of
22       IEEE Std 1003.1-2001, Section 2.13.3, Patterns Used for Filename Expan‐
23       sion.
24
25       The structure type glob_t is defined in <glob.h> and includes at  least
26       the following members:
27
28       Member Type Member Name Description
29       size_t      gl_pathc    Count of paths matched by pattern.
30       char **     gl_pathv    Pointer to a list of matched pathnames.
31       size_t      gl_offs     Slots to reserve at the beginning of gl_pathv.
32
33       The argument pattern is a pointer to a pathname pattern to be expanded.
34       The glob() function shall match all accessible pathnames  against  this
35       pattern  and  develop  a  list of all pathnames that match. In order to
36       have access to a pathname, glob() requires search permission  on  every
37       component of a path except the last, and read permission on each direc‐
38       tory of any filename component of pattern that contains any of the fol‐
39       lowing special characters: '*' , '?' , and '[' .
40
41       The  glob()  function  shall store the number of matched pathnames into
42       pglob->gl_pathc and a pointer to a list of pointers to  pathnames  into
43       pglob->gl_pathv. The pathnames shall be in sort order as defined by the
44       current setting of the LC_COLLATE category; see  the  Base  Definitions
45       volume  of  IEEE Std 1003.1-2001,  Section 7.3.2, LC_COLLATE. The first
46       pointer after the last pathname shall be a null pointer. If the pattern
47       does  not  match any pathnames, the returned number of matched paths is
48       set to 0, and  the  contents  of  pglob->gl_pathv  are  implementation-
49       defined.
50
51       It is the caller's responsibility to create the structure pointed to by
52       pglob. The glob()  function  shall  allocate  other  space  as  needed,
53       including  the  memory pointed to by gl_pathv.  The globfree() function
54       shall free any space associated with pglob  from  a  previous  call  to
55       glob().
56
57       The  flags  argument  is  used  to control the behavior of glob().  The
58       value of flags is a bitwise-inclusive OR of zero or more of the follow‐
59       ing constants, which are defined in <glob.h>:
60
61       GLOB_APPEND
62              Append  pathnames  generated to the ones from a previous call to
63              glob().
64
65       GLOB_DOOFFS
66              Make use of pglob->gl_offs. If this flag is set,  pglob->gl_offs
67              is  used  to specify how many null pointers to add to the begin‐
68              ning of pglob->gl_pathv.  In other words, pglob->gl_pathv  shall
69              point    to    pglob->gl_offs   null   pointers,   followed   by
70              pglob->gl_pathc pathname pointers, followed by a null pointer.
71
72       GLOB_ERR
73              Cause glob() to return when it encounters a  directory  that  it
74              cannot  open  or  read.  Ordinarily,  glob()  continues  to find
75              matches.
76
77       GLOB_MARK
78              Each pathname that is a directory  that  matches  pattern  shall
79              have a slash appended.
80
81       GLOB_NOCHECK
82              Supports   rule   3   in  the  Shell  and  Utilities  volume  of
83              IEEE Std 1003.1-2001, Section 2.13.3, Patterns Used for Filename
84              Expansion.  If  pattern does not match any pathname, then glob()
85              shall return a list consisting of only pattern, and  the  number
86              of matched pathnames is 1.
87
88       GLOB_NOESCAPE
89              Disable backslash escaping.
90
91       GLOB_NOSORT
92              Ordinarily, glob() sorts the matching pathnames according to the
93              current setting of the LC_COLLATE category; see the Base Defini‐
94              tions volume of IEEE Std 1003.1-2001, Section 7.3.2, LC_COLLATE.
95              When this flag is used,  the  order  of  pathnames  returned  is
96              unspecified.
97
98
99       The  GLOB_APPEND  flag  can be used to append a new set of pathnames to
100       those found in a previous call to glob(). The following rules apply  to
101       applications  when  two  or more calls to glob() are made with the same
102       value of pglob and without intervening calls to globfree():
103
104        1. The first such call shall not set GLOB_APPEND. All subsequent calls
105           shall set it.
106
107        2. All the calls shall set GLOB_DOOFFS, or all shall not set it.
108
109        3. After  the second call, pglob->gl_pathv points to a list containing
110           the following:
111
112            a. Zero or more null pointers, as  specified  by  GLOB_DOOFFS  and
113               pglob->gl_offs.
114
115            b. Pointers to the pathnames that were in the pglob->gl_pathv list
116               before the call, in the same order as before.
117
118            c. Pointers to the new pathnames generated by the second call,  in
119               the specified order.
120
121        4. The  count returned in pglob->gl_pathc shall be the total number of
122           pathnames from the two calls.
123
124        5. The application can change any  of  the  fields  after  a  call  to
125           glob().  If it does, the application shall reset them to the origi‐
126           nal value before a subsequent call, using the same pglob value,  to
127           globfree() or glob() with the GLOB_APPEND flag.
128
129       If, during the search, a directory is encountered that cannot be opened
130       or read and errfunc is not a null pointer,  glob()  calls  (*errfunc())
131       with two arguments:
132
133        1. The epath argument is a pointer to the path that failed.
134
135        2. The  eerrno argument is the value of errno from the failure, as set
136           by opendir(), readdir(), or stat(). (Other values may  be  used  to
137           report other errors not explicitly documented for those functions.)
138
139       If (*errfunc()) is called and returns non-zero, or if the GLOB_ERR flag
140       is set in flags, glob() shall stop the  scan  and  return  GLOB_ABORTED
141       after  setting  gl_pathc  and  gl_pathv  in  pglob to reflect the paths
142       already scanned. If GLOB_ERR is not set and either errfunc  is  a  null
143       pointer or (*errfunc()) returns 0, the error shall be ignored.
144
145       The glob() function shall not fail because of large files.
146

RETURN VALUE

148       Upon  successful  completion,  glob()  shall  return  0.  The  argument
149       pglob->gl_pathc shall return the number of matched  pathnames  and  the
150       argument  pglob->gl_pathv  shall contain a pointer to a null-terminated
151       list of matched and sorted pathnames. However, if pglob->gl_pathc is 0,
152       the content of pglob->gl_pathv is undefined.
153
154       The globfree() function shall not return a value.
155
156       If  glob()  terminates due to an error, it shall return one of the non-
157       zero constants defined in <glob.h>. The arguments  pglob->gl_pathc  and
158       pglob->gl_pathv are still set as defined above.
159

ERRORS

161       The glob() function shall fail and return the corresponding value if:
162
163       GLOB_ABORTED
164              The  scan  was  stopped because GLOB_ERR was set or (*errfunc())
165              returned non-zero.
166
167       GLOB_NOMATCH
168              The  pattern  does  not  match  any   existing   pathname,   and
169              GLOB_NOCHECK was not set in flags.
170
171       GLOB_NOSPACE
172              An attempt to allocate memory failed.
173
174
175       The following sections are informative.
176

EXAMPLES

178       One  use of the GLOB_DOOFFS flag is by applications that build an argu‐
179       ment list for use with execv(), execve(), or  execvp().   Suppose,  for
180       example, that an application wants to do the equivalent of:
181
182
183              ls -l *.c
184
185       but for some reason:
186
187
188              system("ls -l *.c")
189
190       is  not acceptable. The application could obtain approximately the same
191       result using the sequence:
192
193
194              globbuf.gl_offs = 2;
195              glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
196              globbuf.gl_pathv[0] = "ls";
197              globbuf.gl_pathv[1] = "-l";
198              execvp("ls", &globbuf.gl_pathv[0]);
199
200       Using the same example:
201
202
203              ls -l *.c *.h
204
205       could be approximately simulated using GLOB_APPEND as follows:
206
207
208              globbuf.gl_offs = 2;
209              glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
210              glob("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf);
211              ...
212

APPLICATION USAGE

214       This function is not provided for the purpose of enabling utilities  to
215       perform  pathname  expansion  on  their arguments, as this operation is
216       performed by the shell, and utilities are explicitly  not  expected  to
217       redo  this.  Instead,  it  is provided for applications that need to do
218       pathname expansion on strings obtained from other sources,  such  as  a
219       pattern typed by a user or read from a file.
220
221       If a utility needs to see if a pathname matches a given pattern, it can
222       use fnmatch().
223
224       Note that gl_pathc and gl_pathv have meaning even if glob() fails. This
225       allows  glob() to report partial results in the event of an error. How‐
226       ever, if gl_pathc is 0, gl_pathv is unspecified even if glob() did  not
227       return an error.
228
229       The  GLOB_NOCHECK  option  could  be  used when an application wants to
230       expand a pathname if wildcards are specified, but wants  to  treat  the
231       pattern  as  just a string otherwise. The sh utility might use this for
232       option-arguments, for example.
233
234       The new pathnames generated by a subsequent call with  GLOB_APPEND  are
235       not  sorted  together with the previous pathnames. This mirrors the way
236       that the shell handles pathname expansion when multiple expansions  are
237       done on a command line.
238
239       Applications  that  need tilde and parameter expansion should use word‐
240       exp().
241

RATIONALE

243       It was claimed that the GLOB_DOOFFS  flag  is  unnecessary  because  it
244       could be simulated using:
245
246
247              new = (char **)malloc((n + pglob->gl_pathc + 1)
248                     * sizeof(char *));
249              (void) memcpy(new+n, pglob->gl_pathv,
250                     pglob->gl_pathc * sizeof(char *));
251              (void) memset(new, 0, n * sizeof(char *));
252              free(pglob->gl_pathv);
253              pglob->gl_pathv = new;
254
255       However, this assumes that the memory pointed to by gl_pathv is a block
256       that was separately created using malloc(). This is not necessarily the
257       case.  An  application  should make no assumptions about how the memory
258       referenced by fields in  pglob  was  allocated.   It  might  have  been
259       obtained  from  malloc()  in  a  large  chunk and then carved up within
260       glob(), or it might have been created using a different memory  alloca‐
261       tor.  It  is  not  the  intent of the standard developers to specify or
262       imply how the memory used by glob() is managed.
263
264       The GLOB_APPEND flag would be used when an application wants to  expand
265       several different patterns into a single list.
266

FUTURE DIRECTIONS

268       None.
269

SEE ALSO

271       exec()  ,  fnmatch() , opendir() , readdir() , stat() , wordexp() , the
272       Base Definitions volume of IEEE Std 1003.1-2001,  <glob.h>,  the  Shell
273       and Utilities volume of IEEE Std 1003.1-2001
274
276       Portions  of  this text are reprinted and reproduced in electronic form
277       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
278       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
279       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
280       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
281       event of any discrepancy between this version and the original IEEE and
282       The  Open Group Standard, the original IEEE and The Open Group Standard
283       is the referee document. The original Standard can be  obtained  online
284       at http://www.opengroup.org/unix/online.html .
285
286
287
288IEEE/The Open Group                  2003                              GLOB(P)
Impressum