1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified Wed Jul 28 11:12:17 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Modified Mon May 13 23:08:50 1996 by Martin Schulze (joey@linux.de)
27 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
28 .\" Modified 990912 by aeb
30 .\" Added description of GLOB_TILDE_NOMATCH
31 .\" Expanded the description of various flags
32 .\" Various wording fixes.
34 .TH GLOB 3 2021-03-22 "GNU" "Linux Programmer's Manual"
36 glob, globfree \- find pathnames matching a pattern, free memory from glob()
41 .BI "int glob(const char *restrict " pattern ", int " flags ,
42 .BI " int (*" errfunc ")(const char *" epath ", int " eerrno ),
43 .BI " glob_t *restrict " pglob );
44 .BI "void globfree(glob_t *" pglob );
49 function searches for all the pathnames matching
51 according to the rules used by the shell (see
53 No tilde expansion or parameter substitution is done; if you want
59 function frees the dynamically allocated storage from an earlier call
65 call are stored in the structure pointed to by
67 This structure is of type
71 and includes the following elements defined by POSIX.2 (more may be
72 present as an extension):
77 size_t gl_pathc; /* Count of paths matched so far */
78 char **gl_pathv; /* List of matched pathnames. */
79 size_t gl_offs; /* Slots to reserve in \fIgl_pathv\fP. */
84 Results are stored in dynamically allocated storage.
88 is made up of the bitwise OR of zero or more the following symbolic
89 constants, which modify the behavior of
93 Return upon a read error (because a directory does not
94 have read permission, for example).
97 attempts carry on despite errors,
98 reading all of the directories that it can.
101 Append a slash to each path which corresponds to a directory.
104 Don't sort the returned pathnames.
105 The only reason to do this is to save processing time.
106 By default, the returned pathnames are sorted.
111 slots at the beginning of the list of strings in
113 The reserved slots contain null pointers.
116 If no pattern matches, return the original pattern.
121 if there are no matches.
124 Append the results of this call to the vector of results
125 returned by a previous call to
127 Do not set this flag on the first invocation of
131 Don't allow backslash (\(aq\e\(aq) to be used as an escape
133 Normally, a backslash can be used to quote the following character,
134 providing a mechanism to turn off the special meaning
138 may also include any of the following, which are GNU
139 extensions and not defined by POSIX.2:
142 Allow a leading period to be matched by metacharacters.
143 By default, metacharacters can't match a leading period.
146 Use alternative functions
147 .IR pglob\->gl_closedir ,
148 .IR pglob\->gl_readdir ,
149 .IR pglob\->gl_opendir ,
150 .IR pglob\->gl_lstat ", and"
152 for filesystem access instead of the normal library
158 style brace expressions of the form
160 Brace expressions can be nested.
161 Thus, for example, specifying the pattern
162 "{foo/{,cat,dog},bar}" would return the same results as four separate
164 calls using the strings:
172 If the pattern contains no metacharacters,
173 then it should be returned as the sole matching word,
174 even if there is no file with that name.
177 Carry out tilde expansion.
178 If a tilde (\(aq\(ti\(aq) is the only character in the pattern,
179 or an initial tilde is followed immediately by a slash (\(aq/\(aq),
180 then the home directory of the caller is substituted for
182 If an initial tilde is followed by a username (e.g., "\(tiandrea/bin"),
183 then the tilde and username are substituted by the home directory
185 If the username is invalid, or the home directory cannot be
186 determined, then no substitution is performed.
189 This provides behavior similar to that of
191 The difference is that if the username is invalid, or the
192 home directory cannot be determined, then
193 instead of using the pattern itself as the name,
197 to indicate an error.
204 that the caller is interested only in directories that match the pattern.
205 If the implementation can easily determine file-type information,
206 then nondirectory files are not returned to the caller.
207 However, the caller must still check that returned files
209 (The purpose of this flag is merely to optimize performance when
210 the caller is interested only in directories.)
215 it will be called in case of an error with the arguments
217 a pointer to the path which failed, and
221 as returned from one of the calls to
228 returns nonzero, or if
232 will terminate after the call to
235 Upon successful return,
237 contains the number of matched pathnames and
239 contains a pointer to the list of pointers to matched pathnames.
240 The list of pointers is terminated by a null pointer.
242 It is possible to call
247 flag has to be set in
249 on the second and later invocations.
253 is set to the flags specified,
257 if any metacharacters were found.
259 On successful completion,
262 Other possible returns are:
265 for running out of memory,
268 for a read error, and
271 for no found matches.
273 For an explanation of the terms used in this section, see
281 Interface Attribute Value
285 MT-Unsafe race:utent env
286 sig:ALRM timer locale
290 T} Thread safety MT-Safe
299 signifies that if any of the functions
304 are used in parallel in different threads of a program,
305 then data races could occur.
307 calls those functions,
308 so we use race:utent to remind users.
310 POSIX.1-2001, POSIX.1-2008, POSIX.2.
312 The structure elements
318 in glibc 2.1, as they should be according to POSIX.2,
325 function may fail due to failure of underlying function calls, such as
329 These will store their error code in
332 One example of use is the following code, which simulates typing
347 glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
348 glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
349 globbuf.gl_pathv[0] = "ls";
350 globbuf.gl_pathv[1] = "\-l";
351 execvp("ls", &globbuf.gl_pathv[0]);