1 .\" Copyright (c) 1989, 1991, 1993, 1994
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 4. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)glob.3 8.3 (Berkeley) 4/16/94
31 .\" $FreeBSD: src/lib/libc/gen/glob.3,v 1.32 2007/01/09 00:27:54 imp Exp $
42 .Nd generate pathnames matching a pattern
47 .Fa "const char *restrict pattern"
49 .Fa "int (*errfunc)(const char *epath, int errno)"
50 .Fa "glob_t *restrict pglob"
55 .Fa "const char *restrict pattern"
57 .Fa "int (^errblk)(const char *epath, int errno)"
58 .Fa "glob_t *restrict pglob"
69 is a pathname generator that implements the rules for file name pattern
70 matching used by the shell.
74 defines the structure type
76 which contains at least the following fields:
79 size_t gl_pathc; /* count of total paths so far */
80 int gl_matchc; /* count of paths matching pattern */
81 size_t gl_offs; /* reserved at beginning of gl_pathv */
82 int gl_flags; /* returned flags */
83 char **gl_pathv; /* list of paths matching pattern */
89 is a pointer to a pathname pattern to be expanded.
93 matches all accessible pathnames against the pattern and creates
94 a list of the pathnames that match.
95 In order to have access to a pathname,
97 requires search permission on every component of a path except the last
98 and read permission on each directory of any filename component of
100 that contains any of the special characters
109 stores the number of matched pathnames into the
111 field, and a pointer to a list of pointers to pathnames into the
114 The first pointer after the last pathname is
116 If the pattern does not match any pathnames, the returned number of
117 matched paths is set to zero.
119 It is the caller's responsibility to create the structure pointed to by
123 function allocates other space as needed, including the memory pointed
129 is used to modify the behavior of
133 is the bitwise inclusive
135 of any of the following
138 .Bl -tag -width GLOB_ALTDIRFUNC
140 Append pathnames generated to the ones from a previous call (or calls)
145 will be the total matches found by this call and the previous call(s).
146 The pathnames are appended to, not merged with the pathnames returned by
147 the previous call(s).
148 Between calls, the caller must not change the setting of the
150 flag, nor change the value of
154 is set, nor (obviously) call
164 is used to specify how many
166 pointers to prepend to the beginning
178 pathname pointers, followed by a
184 to return when it encounters a directory that it cannot open or read.
187 continues to find matches.
189 Each pathname that is a directory that matches
196 does not match any pathname, then
201 with the number of total pathnames set to 1, and the number of matched
203 The effect of backslash escaping is present in the pattern returned.
205 By default, a backslash
207 character is used to escape the following character in the pattern,
208 avoiding any special interpretation of the character.
211 is set, backslash escaping is disabled.
213 By default, the pathnames are sorted in ascending
216 this flag prevents that sorting (speeding up
220 The following values may also be included in
222 however, they are non-standard extensions to
224 .Bl -tag -width GLOB_ALTDIRFUNC
225 .It Dv GLOB_ALTDIRFUNC
226 The following additional fields in the pglob structure have been
227 initialized with alternate functions for glob to use to open, read,
228 and close directories and to get stat information on names found
229 in those directories.
231 void *(*gl_opendir)(const char * name);
232 struct dirent *(*gl_readdir)(void *);
233 void (*gl_closedir)(void *);
234 int (*gl_lstat)(const char *name, struct stat *st);
235 int (*gl_stat)(const char *name, struct stat *st);
238 This extension is provided to allow programs such as
240 to provide globbing from directories stored on tape.
242 Pre-process the pattern string to expand
248 is left unexpanded for historical reasons (and
250 does the same thing to
258 function if the pattern included globbing characters.
259 See the description of the usage of the
261 structure member for more details.
265 but it only appends the
267 if it does not contain any of the special characters ``*'', ``?'' or ``[''.
269 is provided to simplify implementing the historic
271 globbing behavior and should probably not be used anywhere else.
273 Expand patterns that start with
275 to user name home directories.
277 Limit the total number of returned pathnames to the value provided in
281 This option should be set for programs
282 that can be coerced into a denial of service attack
283 via patterns that expand to a very large number of matches,
284 such as a long string of
288 If, during the search, a directory is encountered that cannot be opened
295 .Fa \*(lp*errfunc\*(rp Ns ( Fa path , errno ) .
296 This may be unintuitive: a pattern like
303 is not a directory, resulting in a
306 The error routine can suppress this action by testing for
312 flag will still cause an immediate
313 return when this happens.
319 stops the scan and returns
325 to reflect any paths already matched.
326 This also happens if an error is encountered and
330 regardless of the return value of
335 is not set and either
341 returns zero, the error is ignored.
342 #ifdef UNIFDEF_BLOCKS
348 except that the error callback is a block pointer instead of a function
354 function frees any space associated with
356 from a previous call(s) to
357 #ifdef UNIFDEF_BLOCKS
365 On successful completion,
367 #ifdef UNIFDEF_BLOCKS
374 In addition, the fields of
376 contain the values described below:
377 .Bl -tag -width GLOB_NOCHECK
379 contains the total number of matched pathnames so far.
380 This includes other matches from previous invocations of
382 #ifdef UNIFDEF_BLOCKS
390 contains the number of matched pathnames in the current invocation of
391 #ifdef UNIFDEF_BLOCKS
399 contains a copy of the
401 argument with the bit
405 contained any of the special characters ``*'', ``?'' or ``['', cleared
408 contains a pointer to a
409 .Dv NULL Ns -terminated
410 list of matched pathnames.
413 is zero, the contents of
420 #ifdef UNIFDEF_BLOCKS
424 terminates due to an error, it sets errno and returns one of the
425 following non-zero constants, which are defined in the include
428 .Bl -tag -width GLOB_NOCHECK
430 An attempt to allocate memory failed, or if
434 was specified in the flags and
435 .Fa pglob\->gl_matchc
436 or more patterns were matched.
438 The scan was stopped because an error was encountered and either
441 .Fa \*(lp*errfunc\*(rp\*(lp\*(rp
444 The pattern did not match a pathname and
453 are still set as specified above.
455 A rough equivalent of
457 can be obtained with the
459 .Bd -literal -offset indent
463 glob("*.c", GLOB_DOOFFS, NULL, &g);
464 glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
465 g.gl_pathv[0] = "ls";
466 g.gl_pathv[1] = "-l";
467 execvp("ls", g.gl_pathv);
472 #ifdef UNIFDEF_BLOCKS
479 will not match filenames that begin with a period
480 unless this is specifically requested (e.g., by ".*").
486 The current implementation of the
492 Collating symbol expressions, equivalence class expressions and
493 character class expressions are not supported.
496 .Dv GLOB_ALTDIRFUNC ,
507 are extensions to the
510 should not be used by applications striving for strict
517 functions first appeared in
519 #ifdef UNIFDEF_BLOCKS
522 function first appeared in Mac OS X 10.6.
527 may cause unchecked errors.
531 #ifdef UNIFDEF_BLOCKS
538 may fail and set errno for any of the errors specified for the