1 .\" $NetBSD: glob.3,v 1.42 2012/12/27 21:17:47 christos Exp $
3 .\" Copyright (c) 1989, 1991, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)glob.3 8.3 (Berkeley) 4/16/94
41 .Nd generate pathnames matching a pattern
47 .Fn glob "const char * restrict pattern" "int flags" "int (*errfunc)(const char *, int)" "glob_t * restrict pglob"
49 .Fn globfree "glob_t *pglob"
51 .Fn glob_pattern_p "const char *pattern" "int quote"
56 is a pathname generator that implements the rules for file name pattern
57 matching used by the shell.
61 defines the structure type
63 which contains at least the following fields:
66 size_t gl_pathc; /* count of total paths so far */
67 size_t gl_matchc; /* count of paths matching pattern */
68 size_t gl_offs; /* reserved at beginning of gl_pathv */
69 int gl_flags; /* returned flags */
70 char **gl_pathv; /* list of paths matching pattern */
76 is a pointer to a pathname pattern to be expanded.
80 matches all accessible pathnames against the pattern and creates
81 a list of the pathnames that match.
82 In order to have access to a pathname,
84 requires search permission on every component of a path except the last
85 and read permission on each directory of any filename component of
87 that contains any of the special characters
96 stores the number of matched pathnames into the
98 field, and a pointer to a list of pointers to pathnames into the
101 The first pointer after the last pathname is
103 If the pattern does not match any pathnames, the returned number of
104 matched paths is set to zero.
106 It is the caller's responsibility to create the structure pointed to by
110 function allocates other space as needed, including the memory pointed
116 is used to modify the behavior of
120 is the bitwise inclusive
122 of any of the following
125 .Bl -tag -width GLOB_ALTDIRFUNC
127 Append pathnames generated to the ones from a previous call (or calls)
132 will be the total matches found by this call and the previous call(s).
133 The pathnames are appended to, not merged with the pathnames returned by
134 the previous call(s).
135 Between calls, the caller must not change the setting of the
137 flag, nor change the value of
141 is set, nor (obviously) call
151 is used to specify how many
153 pointers to prepend to the beginning
165 pathname pointers, followed by a
171 to return when it encounters a directory that it cannot open or read.
174 continues to find matches.
176 Each pathname that is a directory that matches
183 does not match any pathname, then
188 with one level of backslash escapes removed,
189 the number of total pathnames set to 1, and the number of matched
192 By default, the pathnames are sorted in ascending
195 this flag prevents that sorting (speeding up
199 The following values may also be included in
201 however, they are non-standard extensions to
203 .Bl -tag -width GLOB_ALTDIRFUNC
204 .It Dv GLOB_ALTDIRFUNC
205 The following additional fields in the pglob structure have been
206 initialized with alternate functions for glob to use to open, read,
207 and close directories and to get stat information on names found
208 in those directories.
210 void *(*gl_opendir)(const char * name);
211 struct dirent *(*gl_readdir)(void *);
212 void (*gl_closedir)(void *);
213 int (*gl_lstat)(const char *name, struct stat *st);
214 int (*gl_stat)(const char *name, struct stat *st);
217 This extension is provided to allow programs such as
219 to provide globbing from directories stored on tape.
221 Pre-process the pattern string to expand
227 is left unexpanded for historical reasons
230 does the same thing to ease typing of
235 Limit the amount of memory used to store matched strings to
239 calls to 128, and the number of
242 This option should be set for programs that can be coerced to a denial of
243 service attack via patterns that expand to a very large number of matches,
244 such as a long string of
249 function if the pattern included globbing characters.
250 See the description of the usage of the
252 structure member for more details.
254 Disable the use of the backslash
256 character for quoting.
260 but it only appends the
262 if it does not contain any of the special characters ``*'', ``?'' or ``[''.
264 is provided to simplify implementing the historic
266 globbing behavior and should probably not be used anywhere else.
267 .It Dv GLOB_NO_DOTDIRS
272 from metacharacter matches, regardless of whether
274 is set and whether the pattern component begins with a literal period.
276 Allow metacharacters to match a leading period in a filename.
278 Indicates that two adjacent
280 characters will do a recursive match in all subdirs, without following
281 symbolic links and three adjacent
283 characters will also follow symbolic links.
285 Expand patterns that start with
287 to user name home directories.
290 If, during the search, a directory is encountered that cannot be opened
297 .Fa (*errfunc)(path, errno) .
298 This may be unintuitive: a pattern like
305 is not a directory, resulting in a
308 The error routine can suppress this action by testing for
314 flag will still cause an immediate
315 return when this happens.
321 stops the scan and returns
327 to reflect any paths already matched.
328 This also happens if an error is encountered and
332 regardless of the return value of
337 is not set and either
343 returns zero, the error is ignored.
347 function frees any space associated with
349 from a previous call(s) to
358 has any special characters that
365 argument is non-zero, then backslash quoted characters are ignored.
369 flag is no longer supported.
372 backslash escaping of special characters is the default behaviour;
373 it may be disabled by specifying the
377 On successful completion,
380 In addition the fields of
382 contain the values described below:
383 .Bl -tag -width GLOB_NOCHECK
385 contains the total number of matched pathnames so far.
386 This includes other matches from previous invocations of
392 contains the number of matched pathnames in the current invocation of
395 contains a copy of the
397 parameter with the bit
401 contained any of the special characters ``*'', ``?'' or ``['', cleared
404 contains a pointer to a
405 .Dv NULL Ns -terminated
406 list of matched pathnames.
409 is zero, the contents of
416 terminates due to an error, it sets
418 and returns one of the following non-zero constants, which are defined
421 .Bl -tag -width GLOB_ABORTEDXXX
423 The scan was stopped because an error was encountered and either
429 The pattern does not match any existing pathname, and
434 An attempt to allocate memory failed, or if
438 was specified in the flags and
440 patterns were matched.
445 return constant is no longer supported.
446 Portable applications should use the
451 .Fa pglob\-\*[Gt]gl_pathc
453 .Fa pglob\-\*[Gt]gl_pathv
454 are still set as specified above.
456 .Bl -tag -width HOME -compact
458 If defined, used as the home directory of the current user in
462 A rough equivalent of
464 can be obtained with the
466 .Bd -literal -offset indent
470 glob("*.c", GLOB_DOOFFS, NULL, \*[Am]g);
471 glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, \*[Am]g);
472 g.gl_pathv[0] = "ls";
473 g.gl_pathv[1] = "-l";
474 execvp("ls", g.gl_pathv);
484 function is expected to be
486 compatible with the exception
488 .Dv GLOB_ALTDIRFUNC ,
494 .Dv GLOB_NO_DOTDIRS ,
502 should not be used by applications striving for strict
510 functions first appeared in
514 function is modelled after the one found in glibc.
518 may cause unchecked errors.
522 function may fail and set
524 for any of the errors specified for the library routines