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 2016-10-08 "GNU" "Linux Programmer's Manual"
36 glob, globfree \- find pathnames matching a pattern, free memory from glob()
41 .BI "int glob(const char *" pattern ", int " flags ,
43 .BI " int (*" errfunc ") (const char *" epath ", int " eerrno ),
45 .BI " glob_t *" pglob );
47 .BI "void globfree(glob_t *" pglob );
52 function searches for all the pathnames matching
54 according to the rules used by the shell (see
56 No tilde expansion or parameter substitution is done; if you want
62 function frees the dynamically allocated storage from an earlier call
68 call are stored in the structure pointed to by
70 This structure is of type
74 and includes the following elements defined by POSIX.2 (more may be
75 present as an extension):
81 size_t gl_pathc; /* Count of paths matched so far */
82 char **gl_pathv; /* List of matched pathnames. */
83 size_t gl_offs; /* Slots to reserve in \fIgl_pathv\fP. */
88 Results are stored in dynamically allocated storage.
92 is made up of the bitwise OR of zero or more the following symbolic
93 constants, which modify the behavior of
97 Return upon a read error (because a directory does not
98 have read permission, for example).
101 attempts carry on despite errors,
102 reading all of the directories that it can.
105 Append a slash to each path which corresponds to a directory.
108 Don't sort the returned pathnames.
109 The only reason to do this is to save processing time.
110 By default, the returned pathnames are sorted.
115 slots at the beginning of the list of strings in
117 The reserved slots contain null pointers.
120 If no pattern matches, return the original pattern.
125 if there are no matches.
128 Append the results of this call to the vector of results
129 returned by a previous call to
131 Do not set this flag on the first invocation of
135 Don't allow backslash (\(aq\\\(aq) to be used as an escape
137 Normally, a backslash can be used to quote the following character,
138 providing a mechanism to turn off the special meaning
142 may also include any of the following, which are GNU
143 extensions and not defined by POSIX.2:
146 Allow a leading period to be matched by metacharacters.
147 By default, metacharacters can't match a leading period.
150 Use alternative functions
151 .IR pglob\->gl_closedir ,
152 .IR pglob\->gl_readdir ,
153 .IR pglob\->gl_opendir ,
154 .IR pglob\->gl_lstat ", and"
156 for filesystem access instead of the normal library
162 style brace expressions of the form
164 Brace expressions can be nested.
165 Thus, for example, specifying the pattern
166 "{foo/{,cat,dog},bar}" would return the same results as four separate
168 calls using the strings:
176 If the pattern contains no metacharacters,
177 then it should be returned as the sole matching word,
178 even if there is no file with that name.
181 Carry out tilde expansion.
182 If a tilde (\(aq~\(aq) is the only character in the pattern,
183 or an initial tilde is followed immediately by a slash (\(aq/\(aq),
184 then the home directory of the caller is substituted for
186 If an initial tilde is followed by a username (e.g., "~andrea/bin"),
187 then the tilde and username are substituted by the home directory
189 If the username is invalid, or the home directory cannot be
190 determined, then no substitution is performed.
193 This provides behavior similar to that of
195 The difference is that if the username is invalid, or the
196 home directory cannot be determined, then
197 instead of using the pattern itself as the name,
201 to indicate an error.
208 that the caller is interested only in directories that match the pattern.
209 If the implementation can easily determine file-type information,
210 then nondirectory files are not returned to the caller.
211 However, the caller must still check that returned files
213 (The purpose of this flag is merely to optimize performance when
214 the caller is interested only in directories.)
219 it will be called in case of an error with the arguments
221 a pointer to the path which failed, and
225 as returned from one of the calls to
232 returns nonzero, or if
236 will terminate after the call to
239 Upon successful return,
241 contains the number of matched pathnames and
243 contains a pointer to the list of pointers to matched pathnames.
244 The list of pointers is terminated by a null pointer.
246 It is possible to call
251 flag has to be set in
253 on the second and later invocations.
257 is set to the flags specified,
261 if any metacharacters were found.
263 On successful completion,
266 Other possible returns are:
269 for running out of memory,
272 for a read error, and
275 for no found matches.
277 For an explanation of the terms used in this section, see
283 Interface Attribute Value
287 MT-Unsafe race:utent env
289 sig:ALRM timer locale
293 T} Thread safety MT-Safe
300 signifies that if any of the functions
305 are used in parallel in different threads of a program,
306 then data races could occur.
308 calls those functions,
309 so we use race:utent to remind users.
311 POSIX.1-2001, POSIX.1-2008, POSIX.2.
313 The structure elements
319 in glibc 2.1, as they should be according to POSIX.2,
326 function may fail due to failure of underlying function calls, such as
330 These will store their error code in
333 One example of use is the following code, which simulates typing
346 glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
347 glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
348 globbuf.gl_pathv[0] = "ls";
349 globbuf.gl_pathv[1] = "\-l";
350 execvp("ls", &globbuf.gl_pathv[0]);