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 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)glob.3 8.3 (Berkeley) 4/16/94
35 .\" $FreeBSD: src/lib/libc/gen/glob.3,v 1.30 2004/09/01 23:28:27 tjr Exp $
46 .Nd generate pathnames matching a pattern
51 .Fa "const char *restrict pattern"
53 .Fa "int (*errfunc)(const char *epath, int errno)"
54 .Fa "glob_t *restrict pglob"
59 .Fa "const char *restrict pattern"
61 .Fa "int (^errblk)(const char *epath, int errno)"
62 .Fa "glob_t *restrict pglob"
73 is a pathname generator that implements the rules for file name pattern
74 matching used by the shell.
78 defines the structure type
80 which contains at least the following fields:
83 int gl_pathc; /* count of total paths so far */
84 int gl_matchc; /* count of paths matching pattern */
85 int gl_offs; /* reserved at beginning of gl_pathv */
86 int gl_flags; /* returned flags */
87 char **gl_pathv; /* list of paths matching pattern */
93 is a pointer to a pathname pattern to be expanded.
97 matches all accessible pathnames against the pattern and creates
98 a list of the pathnames that match.
99 In order to have access to a pathname,
101 requires search permission on every component of a path except the last
102 and read permission on each directory of any filename component of
104 that contains any of the special characters
113 stores the number of matched pathnames into the
115 field, and a pointer to a list of pointers to pathnames into the
118 The first pointer after the last pathname is
120 If the pattern does not match any pathnames, the returned number of
121 matched paths is set to zero.
123 It is the caller's responsibility to create the structure pointed to by
127 function allocates other space as needed, including the memory pointed
133 is used to modify the behavior of
137 is the bitwise inclusive
139 of any of the following
142 .Bl -tag -width GLOB_ALTDIRFUNC
144 Append pathnames generated to the ones from a previous call (or calls)
149 will be the total matches found by this call and the previous call(s).
150 The pathnames are appended to, not merged with the pathnames returned by
151 the previous call(s).
152 Between calls, the caller must not change the setting of the
154 flag, nor change the value of
158 is set, nor (obviously) call
168 is used to specify how many
170 pointers to prepend to the beginning
182 pathname pointers, followed by a
188 to return when it encounters a directory that it cannot open or read.
191 continues to find matches.
193 Each pathname that is a directory that matches
200 does not match any pathname, then
205 with the number of total pathnames set to 1, and the number of matched
207 The effect of backslash escaping is present in the pattern returned.
209 By default, a backslash
211 character is used to escape the following character in the pattern,
212 avoiding any special interpretation of the character.
215 is set, backslash escaping is disabled.
217 By default, the pathnames are sorted in ascending
220 this flag prevents that sorting (speeding up
224 The following values may also be included in
226 however, they are non-standard extensions to
228 .Bl -tag -width GLOB_ALTDIRFUNC
229 .It Dv GLOB_ALTDIRFUNC
230 The following additional fields in the pglob structure have been
231 initialized with alternate functions for glob to use to open, read,
232 and close directories and to get stat information on names found
233 in those directories.
235 void *(*gl_opendir)(const char * name);
236 struct dirent *(*gl_readdir)(void *);
237 void (*gl_closedir)(void *);
238 int (*gl_lstat)(const char *name, struct stat *st);
239 int (*gl_stat)(const char *name, struct stat *st);
242 This extension is provided to allow programs such as
244 to provide globbing from directories stored on tape.
246 Pre-process the pattern string to expand
252 is left unexpanded for historical reasons (and
254 does the same thing to
262 function if the pattern included globbing characters.
263 See the description of the usage of the
265 structure member for more details.
269 but it only appends the
271 if it does not contain any of the special characters ``*'', ``?'' or ``[''.
273 is provided to simplify implementing the historic
275 globbing behavior and should probably not be used anywhere else.
277 Expand patterns that start with
279 to user name home directories.
281 Limit the total number of returned pathnames to the value provided in
285 This option should be set for programs
286 that can be coerced into a denial of service attack
287 via patterns that expand to a very large number of matches,
288 such as a long string of
292 If, during the search, a directory is encountered that cannot be opened
299 .Fa \*(lp*errfunc\*(rp Ns ( Fa path , errno ) .
300 This may be unintuitive: a pattern like
307 is not a directory, resulting in a
310 The error routine can suppress this action by testing for
316 flag will still cause an immediate
317 return when this happens.
323 stops the scan and returns
329 to reflect any paths already matched.
330 This also happens if an error is encountered and
334 regardless of the return value of
339 is not set and either
345 returns zero, the error is ignored.
346 #ifdef UNIFDEF_BLOCKS
352 except that the error callback is a block pointer instead of a function
358 function frees any space associated with
360 from a previous call(s) to
361 #ifdef UNIFDEF_BLOCKS
369 On successful completion,
371 #ifdef UNIFDEF_BLOCKS
378 In addition, the fields of
380 contain the values described below:
381 .Bl -tag -width GLOB_NOCHECK
383 contains the total number of matched pathnames so far.
384 This includes other matches from previous invocations of
386 #ifdef UNIFDEF_BLOCKS
394 contains the number of matched pathnames in the current invocation of
395 #ifdef UNIFDEF_BLOCKS
403 contains a copy of the
405 argument with the bit
409 contained any of the special characters ``*'', ``?'' or ``['', cleared
412 contains a pointer to a
413 .Dv NULL Ns -terminated
414 list of matched pathnames.
417 is zero, the contents of
424 #ifdef UNIFDEF_BLOCKS
428 terminates due to an error, it sets errno and returns one of the
429 following non-zero constants, which are defined in the include
432 .Bl -tag -width GLOB_NOCHECK
434 An attempt to allocate memory failed, or if
438 was specified in the flags and
439 .Fa pglob\->gl_matchc
440 or more patterns were matched.
442 The scan was stopped because an error was encountered and either
445 .Fa \*(lp*errfunc\*(rp\*(lp\*(rp
448 The pattern did not match a pathname and
457 are still set as specified above.
459 A rough equivalent of
461 can be obtained with the
463 .Bd -literal -offset indent
467 glob("*.c", GLOB_DOOFFS, NULL, &g);
468 glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
469 g.gl_pathv[0] = "ls";
470 g.gl_pathv[1] = "-l";
471 execvp("ls", g.gl_pathv);
476 #ifdef UNIFDEF_BLOCKS
483 will not match filenames that begin with a period
484 unless this is specifically requested (e.g., by ".*").
490 The current implementation of the
496 Collating symbol expressions, equivalence class expressions and
497 character class expressions are not supported.
500 .Dv GLOB_ALTDIRFUNC ,
511 are extensions to the
514 should not be used by applications striving for strict
521 functions first appeared in
523 #ifdef UNIFDEF_BLOCKS
526 function first appeared in Mac OS X 10.6.
531 may cause unchecked errors.
535 #ifdef UNIFDEF_BLOCKS
542 may fail and set errno for any of the errors specified for the