1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" Modified Wed Jul 28 11:12:17 1993 by Rik Faith (faith@cs.unc.edu)
6 .\" Modified Mon May 13 23:08:50 1996 by Martin Schulze (joey@linux.de)
7 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
8 .\" Modified 990912 by aeb
10 .\" Added description of GLOB_TILDE_NOMATCH
11 .\" Expanded the description of various flags
12 .\" Various wording fixes.
14 .TH GLOB 3 2021-03-22 "Linux man-pages (unreleased)"
16 glob, globfree \- find pathnames matching a pattern, free memory from glob()
19 .RI ( libc ", " \-lc )
24 .BI "int glob(const char *restrict " pattern ", int " flags ,
25 .BI " int (*" errfunc ")(const char *" epath ", int " eerrno ),
26 .BI " glob_t *restrict " pglob );
27 .BI "void globfree(glob_t *" pglob );
32 function searches for all the pathnames matching
34 according to the rules used by the shell (see
36 No tilde expansion or parameter substitution is done; if you want
42 function frees the dynamically allocated storage from an earlier call
48 call are stored in the structure pointed to by
50 This structure is of type
54 and includes the following elements defined by POSIX.2 (more may be
55 present as an extension):
60 size_t gl_pathc; /* Count of paths matched so far */
61 char **gl_pathv; /* List of matched pathnames. */
62 size_t gl_offs; /* Slots to reserve in \fIgl_pathv\fP. */
67 Results are stored in dynamically allocated storage.
71 is made up of the bitwise OR of zero or more the following symbolic
72 constants, which modify the behavior of
76 Return upon a read error (because a directory does not
77 have read permission, for example).
80 attempts carry on despite errors,
81 reading all of the directories that it can.
84 Append a slash to each path which corresponds to a directory.
87 Don't sort the returned pathnames.
88 The only reason to do this is to save processing time.
89 By default, the returned pathnames are sorted.
94 slots at the beginning of the list of strings in
96 The reserved slots contain null pointers.
99 If no pattern matches, return the original pattern.
104 if there are no matches.
107 Append the results of this call to the vector of results
108 returned by a previous call to
110 Do not set this flag on the first invocation of
114 Don't allow backslash (\(aq\e\(aq) to be used as an escape
116 Normally, a backslash can be used to quote the following character,
117 providing a mechanism to turn off the special meaning
121 may also include any of the following, which are GNU
122 extensions and not defined by POSIX.2:
125 Allow a leading period to be matched by metacharacters.
126 By default, metacharacters can't match a leading period.
129 Use alternative functions
130 .IR pglob\->gl_closedir ,
131 .IR pglob\->gl_readdir ,
132 .IR pglob\->gl_opendir ,
133 .IR pglob\->gl_lstat ,
136 for filesystem access instead of the normal library
142 style brace expressions of the form
144 Brace expressions can be nested.
145 Thus, for example, specifying the pattern
146 "{foo/{,cat,dog},bar}" would return the same results as four separate
148 calls using the strings:
156 If the pattern contains no metacharacters,
157 then it should be returned as the sole matching word,
158 even if there is no file with that name.
161 Carry out tilde expansion.
162 If a tilde (\(aq\(ti\(aq) is the only character in the pattern,
163 or an initial tilde is followed immediately by a slash (\(aq/\(aq),
164 then the home directory of the caller is substituted for
166 If an initial tilde is followed by a username (e.g., "\(tiandrea/bin"),
167 then the tilde and username are substituted by the home directory
169 If the username is invalid, or the home directory cannot be
170 determined, then no substitution is performed.
173 This provides behavior similar to that of
175 The difference is that if the username is invalid, or the
176 home directory cannot be determined, then
177 instead of using the pattern itself as the name,
181 to indicate an error.
188 that the caller is interested only in directories that match the pattern.
189 If the implementation can easily determine file-type information,
190 then nondirectory files are not returned to the caller.
191 However, the caller must still check that returned files
193 (The purpose of this flag is merely to optimize performance when
194 the caller is interested only in directories.)
199 it will be called in case of an error with the arguments
201 a pointer to the path which failed, and
205 as returned from one of the calls to
212 returns nonzero, or if
216 will terminate after the call to
219 Upon successful return,
221 contains the number of matched pathnames and
223 contains a pointer to the list of pointers to matched pathnames.
224 The list of pointers is terminated by a null pointer.
226 It is possible to call
231 flag has to be set in
233 on the second and later invocations.
237 is set to the flags specified,
241 if any metacharacters were found.
243 On successful completion,
246 Other possible returns are:
249 for running out of memory,
252 for a read error, and
255 for no found matches.
257 For an explanation of the terms used in this section, see
265 Interface Attribute Value
269 MT-Unsafe race:utent env
270 sig:ALRM timer locale
274 T} Thread safety MT-Safe
283 signifies that if any of the functions
288 are used in parallel in different threads of a program,
289 then data races could occur.
291 calls those functions,
292 so we use race:utent to remind users.
294 POSIX.1-2001, POSIX.1-2008, POSIX.2.
296 The structure elements
302 in glibc 2.1, as they should be according to POSIX.2,
309 function may fail due to failure of underlying function calls, such as
313 These will store their error code in
316 One example of use is the following code, which simulates typing
331 glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
332 glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
333 globbuf.gl_pathv[0] = "ls";
334 globbuf.gl_pathv[1] = "\-l";
335 execvp("ls", &globbuf.gl_pathv[0]);