1glob(3)                    Library Functions Manual                    glob(3)
2
3
4

NAME

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

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ATTRIBUTES

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

STANDARDS

177       POSIX.1-2008.
178

HISTORY

180       POSIX.1-2001, POSIX.2.
181

NOTES

183       The  structure  elements gl_pathc and gl_offs are declared as size_t in
184       glibc 2.1, as they should be according to POSIX.2, but are declared  as
185       int in glibc 2.0.
186

BUGS

188       The  glob()  function  may  fail  due to failure of underlying function
189       calls, such as malloc(3) or opendir(3).  These will store  their  error
190       code in errno.
191

EXAMPLES

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

SEE ALSO

209       ls(1),  sh(1),  stat(2),  exec(3),  fnmatch(3),  malloc(3), opendir(3),
210       readdir(3), wordexp(3), glob(7)
211
212
213
214Linux man-pages 6.04              2023-03-30                           glob(3)
Impressum