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

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       glob, globfree — generate pathnames matching a pattern
14

SYNOPSIS

16       #include <glob.h>
17
18       int glob(const char *restrict pattern, int flags,
19           int(*errfunc)(const char *epath, int eerrno),
20           glob_t *restrict pglob);
21       void globfree(glob_t *pglob);
22

DESCRIPTION

24       The glob() function is a pathname generator that  shall  implement  the
25       rules  defined  in the Shell and Utilities volume of POSIX.1‐2008, Sec‐
26       tion 2.13, Pattern Matching Notation, with optional support for rule  3
27       in the Shell and Utilities volume of POSIX.1‐2008, Section 2.13.3, Pat‐
28       terns Used for Filename Expansion.
29
30       The structure type glob_t is defined in <glob.h> and includes at  least
31       the following members:
32
33       ┌──────────────┬──────────────┬─────────────────────────────────────────┐
34Member Type   Member Name  Description               
35       ├──────────────┼──────────────┼─────────────────────────────────────────┤
36size_t        gl_pathc      │ Count of paths matched by pattern.      │
37char **       gl_pathv      │ Pointer to a list of matched pathnames. │
38size_t        gl_offs       │ Slots  to  reserve  at the beginning of │
39       │              │              │ gl_pathv.                               │
40       └──────────────┴──────────────┴─────────────────────────────────────────┘
41       The argument pattern is a pointer to a pathname pattern to be expanded.
42       The  glob()  function shall match all accessible pathnames against this
43       pattern and develop a list of all pathnames that  match.  In  order  to
44       have  access  to a pathname, glob() requires search permission on every
45       component of a path except the last, and read permission on each direc‐
46       tory of any filename component of pattern that contains any of the fol‐
47       lowing special characters: '*', '?', and '['.
48
49       The glob() function shall store the number of  matched  pathnames  into
50       pglob->gl_pathc  and  a pointer to a list of pointers to pathnames into
51       pglob->gl_pathv. The pathnames shall be in sort order as defined by the
52       current  setting  of  the LC_COLLATE category; see the Base Definitions
53       volume of POSIX.1‐2008, Section 7.3.2, LC_COLLATE.  The  first  pointer
54       after  the  last  pathname shall be a null pointer. If the pattern does
55       not match any pathnames, the returned number of matched paths is set to
56       0, and the contents of pglob->gl_pathv are implementation-defined.
57
58       It is the caller's responsibility to create the structure pointed to by
59       pglob.  The glob() function  shall  allocate  other  space  as  needed,
60       including  the  memory pointed to by gl_pathv.  The globfree() function
61       shall free any space associated with pglob  from  a  previous  call  to
62       glob().
63
64       The  flags  argument  is  used  to control the behavior of glob().  The
65       value of flags is a bitwise-inclusive OR of zero or more of the follow‐
66       ing constants, which are defined in <glob.h>:
67
68       GLOB_APPEND   Append  pathnames  generated  to the ones from a previous
69                     call to glob().
70
71       GLOB_DOOFFS   Make  use  of  pglob->gl_offs.  If  this  flag  is   set,
72                     pglob->gl_offs  is used to specify how many null pointers
73                     to add to the  beginning  of  pglob->gl_pathv.  In  other
74                     words, pglob->gl_pathv shall point to pglob->gl_offs null
75                     pointers, followed by pglob->gl_pathc pathname  pointers,
76                     followed by a null pointer.
77
78       GLOB_ERR      Cause  glob()  to  return  when it encounters a directory
79                     that it cannot open or read.  Ordinarily, glob()  contin‐
80                     ues to find matches.
81
82       GLOB_MARK     Each  pathname  that  is a directory that matches pattern
83                     shall have a <slash> appended.
84
85       GLOB_NOCHECK  Supports rule 3 in the  Shell  and  Utilities  volume  of
86                     POSIX.1‐2008,  Section 2.13.3, Patterns Used for Filename
87                     Expansion.  If pattern does not match any pathname,  then
88                     glob()  shall  return  a list consisting of only pattern,
89                     and the number of matched pathnames is 1.
90
91       GLOB_NOESCAPE Disable backslash escaping.
92
93       GLOB_NOSORT   Ordinarily, glob() sorts the matching pathnames according
94                     to  the  current  setting of the LC_COLLATE category; see
95                     the Base  Definitions  volume  of  POSIX.1‐2008,  Section
96                     7.3.2,  LC_COLLATE.  When this flag is used, the order of
97                     pathnames returned is unspecified.
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
140       flag   is  set  in  flags,  glob()  shall  stop  the  scan  and  return
141       GLOB_ABORTED after setting gl_pathc and gl_pathv in  pglob  to  reflect
142       the paths already scanned. If GLOB_ERR is not set and either errfunc is
143       a null 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  The  scan  was  stopped  because  GLOB_ERR  was  set   or
164                     (()*errfunc ) returned non-zero.
165
166       GLOB_NOMATCH  The  pattern  does  not  match any existing pathname, and
167                     GLOB_NOCHECK was not set in flags.
168
169       GLOB_NOSPACE  An attempt to allocate memory failed.
170
171       The following sections are informative.
172

EXAMPLES

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

APPLICATION USAGE

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

RATIONALE

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

FUTURE DIRECTIONS

258       None.
259

SEE ALSO

261       exec, fdopendir(), fnmatch(), fstatat(), readdir(), Section  2.6,  Word
262       Expansions
263
264       The Base Definitions volume of POSIX.1‐2008, Section 7.3.2, LC_COLLATE,
265       <glob.h>
266
268       Portions of this text are reprinted and reproduced in  electronic  form
269       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
270       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
271       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
272       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
273       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
274       event of any discrepancy between this version and the original IEEE and
275       The  Open Group Standard, the original IEEE and The Open Group Standard
276       is the referee document. The original Standard can be  obtained  online
277       at http://www.unix.org/online.html .
278
279       Any  typographical  or  formatting  errors that appear in this page are
280       most likely to have been introduced during the conversion of the source
281       files  to  man page format. To report such errors, see https://www.ker
282       nel.org/doc/man-pages/reporting_bugs.html .
283
284
285
286IEEE/The Open Group                  2013                             GLOB(3P)
Impressum