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 $
39 .Nd generate pathnames matching a pattern
45 .Fn glob "const char *pattern" "int flags" "int (*errfunc)(const char *, int)" "glob_t *pglob"
47 .Fn globfree "glob_t *pglob"
52 is a pathname generator that implements the rules for file name pattern
53 matching used by the shell.
57 defines the structure type
59 which contains at least the following fields:
62 size_t gl_pathc; /* count of total paths so far */
63 size_t gl_matchc; /* count of paths matching pattern */
64 size_t gl_offs; /* reserved at beginning of gl_pathv */
65 int gl_flags; /* returned flags */
66 char **gl_pathv; /* list of paths matching pattern */
72 is a pointer to a pathname pattern to be expanded.
76 matches all accessible pathnames against the pattern and creates
77 a list of the pathnames that match.
78 In order to have access to a pathname,
80 requires search permission on every component of a path except the last
81 and read permission on each directory of any filename component of
83 that contains any of the special characters
92 stores the number of matched pathnames into the
94 field, and a pointer to a list of pointers to pathnames into the
97 The first pointer after the last pathname is
99 If the pattern does not match any pathnames, the returned number of
100 matched paths is set to zero.
102 It is the caller's responsibility to create the structure pointed to by
106 function allocates other space as needed, including the memory pointed
112 is used to modify the behavior of
116 is the bitwise inclusive
118 of any of the following
121 .Bl -tag -width GLOB_ALTDIRFUNC
123 Append pathnames generated to the ones from a previous call (or calls)
128 will be the total matches found by this call and the previous call(s).
129 The pathnames are appended to, not merged with the pathnames returned by
130 the previous call(s).
131 Between calls, the caller must not change the setting of the
133 flag, nor change the value of
137 is set, nor (obviously) call
147 is used to specify how many
149 pointers to prepend to the beginning
161 pathname pointers, followed by a
167 to return when it encounters a directory that it cannot open or read.
170 continues to find matches.
172 Each pathname that is a directory that matches
179 does not match any pathname, then
184 with the number of total pathnames set to 1, and the number of matched
186 The effect of backslash escaping is present in the pattern returned.
188 By default, a backslash
190 character is used to escape the following character in the pattern,
191 avoiding any special interpretation of the character.
194 is set, backslash escaping is disabled.
196 By default, the pathnames are sorted in ascending
199 this flag prevents that sorting (speeding up
203 The following values may also be included in
205 however, they are non-standard extensions to
207 .Bl -tag -width GLOB_ALTDIRFUNC
208 .It Dv GLOB_ALTDIRFUNC
209 The following additional fields in the pglob structure have been
210 initialized with alternate functions for glob to use to open, read,
211 and close directories and to get stat information on names found
212 in those directories.
214 void *(*gl_opendir)(const char * name);
215 struct dirent *(*gl_readdir)(void *);
216 void (*gl_closedir)(void *);
217 int (*gl_lstat)(const char *name, struct stat *st);
218 int (*gl_stat)(const char *name, struct stat *st);
221 This extension is provided to allow programs such as
223 to provide globbing from directories stored on tape.
225 Pre-process the pattern string to expand
231 is left unexpanded for historical reasons (and
233 does the same thing to
241 function if the pattern included globbing characters.
242 See the description of the usage of the
244 structure member for more details.
248 but it only appends the
250 if it does not contain any of the special characters ``*'', ``?'' or ``[''.
252 is provided to simplify implementing the historic
254 globbing behavior and should probably not be used anywhere else.
256 Expand patterns that start with
258 to user name home directories.
260 Limit the total number of returned pathnames to the value provided in
264 This option should be set for programs
265 that can be coerced into a denial of service attack
266 via patterns that expand to a very large number of matches,
267 such as a long string of
271 If, during the search, a directory is encountered that cannot be opened
278 .Fa \*(lp*errfunc\*(rp Ns ( Fa path , errno ) .
279 This may be unintuitive: a pattern like
286 is not a directory, resulting in a
289 The error routine can suppress this action by testing for
295 flag will still cause an immediate
296 return when this happens.
302 stops the scan and returns
308 to reflect any paths already matched.
309 This also happens if an error is encountered and
313 regardless of the return value of
318 is not set and either
324 returns zero, the error is ignored.
328 function frees any space associated with
330 from a previous call(s) to
333 On successful completion,
336 In addition the fields of
338 contain the values described below:
339 .Bl -tag -width GLOB_NOCHECK
341 contains the total number of matched pathnames so far.
342 This includes other matches from previous invocations of
348 contains the number of matched pathnames in the current invocation of
351 contains a copy of the
353 argument with the bit
357 contained any of the special characters ``*'', ``?'' or ``['', cleared
360 contains a pointer to a
361 .Dv NULL Ns -terminated
362 list of matched pathnames.
365 is zero, the contents of
372 terminates due to an error, it sets errno and returns one of the
373 following non-zero constants, which are defined in the include
376 .Bl -tag -width GLOB_NOCHECK
378 An attempt to allocate memory failed, or if
382 was specified in the flags and
383 .Fa pglob\->gl_matchc
384 or more patterns were matched.
386 The scan was stopped because an error was encountered and either
389 .Fa \*(lp*errfunc\*(rp\*(lp\*(rp
392 The pattern did not match a pathname and
401 are still set as specified above.
403 A rough equivalent of
405 can be obtained with the
407 .Bd -literal -offset indent
411 glob("*.c", GLOB_DOOFFS, NULL, &g);
412 glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
413 g.gl_pathv[0] = "ls";
414 g.gl_pathv[1] = "-l";
415 execvp("ls", g.gl_pathv);
422 The current implementation of the
428 Collating symbol expressions, equivalence class expressions and
429 character class expressions are not supported.
432 .Dv GLOB_ALTDIRFUNC ,
443 are extensions to the
446 should not be used by applications striving for strict
453 functions first appeared in
458 may cause unchecked errors.
463 may fail and set errno for any of the errors specified for the