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.7.2.11 2003/03/15 15:11:05 trhodes Exp $
36 .\" $DragonFly: src/lib/libc/gen/glob.3,v 1.2 2003/06/17 04:26:42 dillon Exp $
44 .Nd generate pathnames matching a pattern
50 .Fn glob "const char *pattern" "int flags" "int (*errfunc)(const char *, int)" "glob_t *pglob"
52 .Fn globfree "glob_t *pglob"
57 is a pathname generator that implements the rules for file name pattern
58 matching used by the shell.
62 defines the structure type
64 which contains at least the following fields:
67 int gl_pathc; /* count of total paths so far */
68 int gl_matchc; /* count of paths matching pattern */
69 int gl_offs; /* reserved at beginning of gl_pathv */
70 int gl_flags; /* returned flags */
71 char **gl_pathv; /* list of paths matching pattern */
77 is a pointer to a pathname pattern to be expanded.
81 matches all accessible pathnames against the pattern and creates
82 a list of the pathnames that match.
83 In order to have access to a pathname,
85 requires search permission on every component of a path except the last
86 and read permission on each directory of any filename component of
88 that contains any of the special characters
97 stores the number of matched pathnames into the
99 field, and a pointer to a list of pointers to pathnames into the
102 The first pointer after the last pathname is
104 If the pattern does not match any pathnames, the returned number of
105 matched paths is set to zero.
107 It is the caller's responsibility to create the structure pointed to by
111 function allocates other space as needed, including the memory pointed
117 is used to modify the behavior of
121 is the bitwise inclusive
123 of any of the following
126 .Bl -tag -width GLOB_ALTDIRFUNC
128 Append pathnames generated to the ones from a previous call (or calls)
133 will be the total matches found by this call and the previous call(s).
134 The pathnames are appended to, not merged with the pathnames returned by
135 the previous call(s).
136 Between calls, the caller must not change the setting of the
138 flag, nor change the value of
142 is set, nor (obviously) call
152 is used to specify how many
154 pointers to prepend to the beginning
166 pathname pointers, followed by a
172 to return when it encounters a directory that it cannot open or read.
175 continues to find matches.
177 Each pathname that is a directory that matches
184 does not match any pathname, then
189 with the number of total pathnames set to 1, and the number of matched
191 The effect of backslash escaping is present in the pattern returned.
193 By default, a backslash
195 character is used to escape the following character in the pattern,
196 avoiding any special interpretation of the character.
199 is set, backslash escaping is disabled.
201 By default, the pathnames are sorted in ascending
204 this flag prevents that sorting (speeding up
208 The following values may also be included in
210 however, they are non-standard extensions to
212 .Bl -tag -width GLOB_ALTDIRFUNC
213 .It Dv GLOB_ALTDIRFUNC
214 The following additional fields in the pglob structure have been
215 initialized with alternate functions for glob to use to open, read,
216 and close directories and to get stat information on names found
217 in those directories.
219 void *(*gl_opendir)(const char * name);
220 struct dirent *(*gl_readdir)(void *);
221 void (*gl_closedir)(void *);
222 int (*gl_lstat)(const char *name, struct stat *st);
223 int (*gl_stat)(const char *name, struct stat *st);
226 This extension is provided to allow programs such as
228 to provide globbing from directories stored on tape.
230 Pre-process the pattern string to expand
236 is left unexpanded for historical reasons (and
238 does the same thing to
246 function if the pattern included globbing characters.
247 See the description of the usage of the
249 structure member for more details.
253 but it only appends the
255 if it does not contain any of the special characters ``*'', ``?'' or ``[''.
257 is provided to simplify implementing the historic
259 globbing behavior and should probably not be used anywhere else.
261 Expand patterns that start with
263 to user name home directories.
265 Limit the total number of returned pathnames to the value provided in
269 This option should be set for programs
270 that can be coerced into a denial of service attack
271 via patterns that expand to a very large number of matches,
272 such as a long string of
276 If, during the search, a directory is encountered that cannot be opened
283 .Fa \*(lp*errfunc\*(rp Ns ( Fa path , errno ) .
284 This may be unintuitive: a pattern like
291 is not a directory, resulting in a
294 The error routine can suppress this action by testing for
300 flag will still cause an immediate
301 return when this happens.
307 stops the scan and returns
313 to reflect any paths already matched.
314 This also happens if an error is encountered and
318 regardless of the return value of
323 is not set and either
329 returns zero, the error is ignored.
333 function frees any space associated with
335 from a previous call(s) to
338 On successful completion,
341 In addition the fields of
343 contain the values described below:
344 .Bl -tag -width GLOB_NOCHECK
346 contains the total number of matched pathnames so far.
347 This includes other matches from previous invocations of
353 contains the number of matched pathnames in the current invocation of
356 contains a copy of the
358 argument with the bit
362 contained any of the special characters ``*'', ``?'' or ``['', cleared
365 contains a pointer to a
366 .Dv NULL Ns -terminated
367 list of matched pathnames.
370 is zero, the contents of
377 terminates due to an error, it sets errno and returns one of the
378 following non-zero constants, which are defined in the include
381 .Bl -tag -width GLOB_NOCHECK
383 An attempt to allocate memory failed, or if
387 was specified in the flags and
388 .Fa pglob\->gl_matchc
389 or more patterns were matched.
391 The scan was stopped because an error was encountered and either
394 .Fa \*(lp*errfunc\*(rp\*(lp\*(rp
397 The pattern did not match a pathname and
406 are still set as specified above.
408 A rough equivalent of
410 can be obtained with the
412 .Bd -literal -offset indent
416 glob("*.c", GLOB_DOOFFS, NULL, &g);
417 glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
418 g.gl_pathv[0] = "ls";
419 g.gl_pathv[1] = "-l";
420 execvp("ls", g.gl_pathv);
429 function is expected to be
431 compatible with the exception
433 .Dv GLOB_ALTDIRFUNC ,
444 should not be used by applications striving for strict
452 functions first appeared in
457 may cause unchecked errors.
462 may fail and set errno for any of the errors specified for the