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

NAME

12       glob, globfree — generate pathnames matching a pattern
13

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

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

RATIONALE

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

FUTURE DIRECTIONS

263       None.
264

SEE ALSO

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