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 *restrict pattern, int flags,
13                int (*errfunc)(const char *epath, int eerrno),
14                glob_t *restrict pglob);
15       void globfree(glob_t *pglob);
16

DESCRIPTION

18       The glob() function searches for all the pathnames matching pattern ac‐
19       cording  to the rules used by the shell (see glob(7)).  No tilde expan‐
20       sion or parameter substitution is done; if you want  these,  use  word‐
21       exp(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 in‐
28       cludes 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 re‐
66              turned 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 de‐
80              fault, 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
85              filesystem 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 re‐
96              turned  as the sole matching word, even if there is no file with
97              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 it‐
113              self as the name, glob() returns GLOB_NOMATCH to indicate an er‐
114              ror.
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

ATTRIBUTES

157       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
158       tributes(7).
159
160       ┌───────────┬───────────────┬──────────────────────────────────────────┐
161Interface  Attribute     Value                                    
162       ├───────────┼───────────────┼──────────────────────────────────────────┤
163glob()     │ Thread safety │ MT-Unsafe race:utent env sig:ALRM timer  │
164       │           │               │ locale                                   │
165       ├───────────┼───────────────┼──────────────────────────────────────────┤
166globfree() │ Thread safety │ MT-Safe                                  │
167       └───────────┴───────────────┴──────────────────────────────────────────┘
168       In the above table, utent in race:utent signifies that if  any  of  the
169       functions setutent(3), getutent(3), or endutent(3) are used in parallel
170       in different threads of a program, then data races could occur.  glob()
171       calls those functions, so we use race:utent to remind users.
172

CONFORMING TO

174       POSIX.1-2001, POSIX.1-2008, POSIX.2.
175

NOTES

177       The  structure  elements gl_pathc and gl_offs are declared as size_t in
178       glibc 2.1, as they should be according to POSIX.2, but are declared  as
179       int in glibc 2.0.
180

BUGS

182       The  glob()  function  may  fail  due to failure of underlying function
183       calls, such as malloc(3) or opendir(3).  These will store  their  error
184       code in errno.
185

EXAMPLES

187       One example of use is the following code, which simulates typing
188
189           ls -l *.c ../*.c
190
191       in the shell:
192
193           glob_t globbuf;
194
195           globbuf.gl_offs = 2;
196           glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
197           glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
198           globbuf.gl_pathv[0] = "ls";
199           globbuf.gl_pathv[1] = "-l";
200           execvp("ls", &globbuf.gl_pathv[0]);
201

SEE ALSO

203       ls(1),  sh(1),  stat(2),  exec(3),  fnmatch(3),  malloc(3), opendir(3),
204       readdir(3), wordexp(3), glob(7)
205

COLOPHON

207       This page is part of release 5.12 of the Linux  man-pages  project.   A
208       description  of  the project, information about reporting bugs, and the
209       latest    version    of    this    page,    can     be     found     at
210       https://www.kernel.org/doc/man-pages/.
211
212
213
214GNU                               2021-03-22                           GLOB(3)
Impressum