]> git.saurik.com Git - apple/libc.git/blame - gen/glob.3
Libc-498.1.1.tar.gz
[apple/libc.git] / gen / glob.3
CommitLineData
224c7076
A
1.\" Copyright (c) 1989, 1991, 1993, 1994
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Guido van Rossum.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
21.\"
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
32.\" SUCH DAMAGE.
33.\"
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 $
36.\"
37.Dd September 1, 2004
38.Dt GLOB 3
39.Os
40.Sh NAME
41.Nm glob ,
42.Nm globfree
43.Nd generate pathnames matching a pattern
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In glob.h
48.Ft int
49.Fo glob
50.Fa "const char *restrict pattern"
51.Fa "int flags"
52.Fa "int (*errfunc)(const char *epath, int eerno)"
53.Fa "glob_t *restrict pglob"
54.Fc
55.Ft void
56.Fo globfree
57.Fa "glob_t *pglob"
58.Fc
59.Sh DESCRIPTION
60The
61.Fn glob
62function
63is a pathname generator that implements the rules for file name pattern
64matching used by the shell.
65.Pp
66The include file
67.In glob.h
68defines the structure type
69.Fa glob_t ,
70which contains at least the following fields:
71.Bd -literal
72typedef struct {
73 int gl_pathc; /* count of total paths so far */
74 int gl_matchc; /* count of paths matching pattern */
75 int gl_offs; /* reserved at beginning of gl_pathv */
76 int gl_flags; /* returned flags */
77 char **gl_pathv; /* list of paths matching pattern */
78} glob_t;
79.Ed
80.Pp
81The argument
82.Fa pattern
83is a pointer to a pathname pattern to be expanded.
84The
85.Fn glob
86argument
87matches all accessible pathnames against the pattern and creates
88a list of the pathnames that match.
89In order to have access to a pathname,
90.Fn glob
91requires search permission on every component of a path except the last
92and read permission on each directory of any filename component of
93.Fa pattern
94that contains any of the special characters
95.Ql * ,
96.Ql ?\&
97or
98.Ql \&[ .
99.Pp
100The
101.Fn glob
102argument
103stores the number of matched pathnames into the
104.Fa gl_pathc
105field, and a pointer to a list of pointers to pathnames into the
106.Fa gl_pathv
107field.
108The first pointer after the last pathname is
109.Dv NULL .
110If the pattern does not match any pathnames, the returned number of
111matched paths is set to zero.
112.Pp
113It is the caller's responsibility to create the structure pointed to by
114.Fa pglob .
115The
116.Fn glob
117function allocates other space as needed, including the memory pointed
118to by
119.Fa gl_pathv .
120.Pp
121The argument
122.Fa flags
123is used to modify the behavior of
124.Fn glob .
125The value of
126.Fa flags
127is the bitwise inclusive
128.Tn OR
129of any of the following
130values defined in
131.In glob.h :
132.Bl -tag -width GLOB_ALTDIRFUNC
133.It Dv GLOB_APPEND
134Append pathnames generated to the ones from a previous call (or calls)
135to
136.Fn glob .
137The value of
138.Fa gl_pathc
139will be the total matches found by this call and the previous call(s).
140The pathnames are appended to, not merged with the pathnames returned by
141the previous call(s).
142Between calls, the caller must not change the setting of the
143.Dv GLOB_DOOFFS
144flag, nor change the value of
145.Fa gl_offs
146when
147.Dv GLOB_DOOFFS
148is set, nor (obviously) call
149.Fn globfree
150for
151.Fa pglob .
152.It Dv GLOB_DOOFFS
153Make use of the
154.Fa gl_offs
155field.
156If this flag is set,
157.Fa gl_offs
158is used to specify how many
159.Dv NULL
160pointers to prepend to the beginning
161of the
162.Fa gl_pathv
163field.
164In other words,
165.Fa gl_pathv
166will point to
167.Fa gl_offs
168.Dv NULL
169pointers,
170followed by
171.Fa gl_pathc
172pathname pointers, followed by a
173.Dv NULL
174pointer.
175.It Dv GLOB_ERR
176Causes
177.Fn glob
178to return when it encounters a directory that it cannot open or read.
179Ordinarily,
180.Fn glob
181continues to find matches.
182.It Dv GLOB_MARK
183Each pathname that is a directory that matches
184.Fa pattern
185has a slash
186appended.
187.It Dv GLOB_NOCHECK
188If
189.Fa pattern
190does not match any pathname, then
191.Fn glob
192returns a list
193consisting of only
194.Fa pattern ,
195with the number of total pathnames set to 1, and the number of matched
196pathnames set to 0.
197The effect of backslash escaping is present in the pattern returned.
198.It Dv GLOB_NOESCAPE
199By default, a backslash
200.Pq Ql \e
201character is used to escape the following character in the pattern,
202avoiding any special interpretation of the character.
203If
204.Dv GLOB_NOESCAPE
205is set, backslash escaping is disabled.
206.It Dv GLOB_NOSORT
207By default, the pathnames are sorted in ascending
208.Tn ASCII
209order;
210this flag prevents that sorting (speeding up
211.Fn glob ) .
212.El
213.Pp
214The following values may also be included in
215.Fa flags ,
216however, they are non-standard extensions to
217.St -p1003.2 .
218.Bl -tag -width GLOB_ALTDIRFUNC
219.It Dv GLOB_ALTDIRFUNC
220The following additional fields in the pglob structure have been
221initialized with alternate functions for glob to use to open, read,
222and close directories and to get stat information on names found
223in those directories.
224.Bd -literal
225void *(*gl_opendir)(const char * name);
226struct dirent *(*gl_readdir)(void *);
227void (*gl_closedir)(void *);
228int (*gl_lstat)(const char *name, struct stat *st);
229int (*gl_stat)(const char *name, struct stat *st);
230.Ed
231.Pp
232This extension is provided to allow programs such as
233.Xr restore 8
234to provide globbing from directories stored on tape.
235.It Dv GLOB_BRACE
236Pre-process the pattern string to expand
237.Ql {pat,pat,...}
238strings like
239.Xr csh 1 .
240The pattern
241.Ql {}
242is left unexpanded for historical reasons (and
243.Xr csh 1
244does the same thing to
245ease typing
246of
247.Xr find 1
248patterns).
249.It Dv GLOB_MAGCHAR
250Set by the
251.Fn glob
252function if the pattern included globbing characters.
253See the description of the usage of the
254.Fa gl_matchc
255structure member for more details.
256.It Dv GLOB_NOMAGIC
257Is the same as
258.Dv GLOB_NOCHECK
259but it only appends the
260.Fa pattern
261if it does not contain any of the special characters ``*'', ``?'' or ``[''.
262.Dv GLOB_NOMAGIC
263is provided to simplify implementing the historic
264.Xr csh 1
265globbing behavior and should probably not be used anywhere else.
266.It Dv GLOB_TILDE
267Expand patterns that start with
268.Ql ~
269to user name home directories.
270.It Dv GLOB_LIMIT
271Limit the total number of returned pathnames to the value provided in
272.Fa gl_matchc
273(default
274.Dv ARG_MAX ) .
275This option should be set for programs
276that can be coerced into a denial of service attack
277via patterns that expand to a very large number of matches,
278such as a long string of
279.Ql */../*/.. .
280.El
281.Pp
282If, during the search, a directory is encountered that cannot be opened
283or read and
284.Fa errfunc
285is
286.Pf non- Dv NULL ,
287.Fn glob
288calls
289.Fa \*(lp*errfunc\*(rp Ns ( Fa path , errno ) .
290This may be unintuitive: a pattern like
291.Ql */Makefile
292will try to
293.Xr stat 2
294.Ql foo/Makefile
295even if
296.Ql foo
297is not a directory, resulting in a
298call to
299.Fa errfunc .
300The error routine can suppress this action by testing for
301.Er ENOENT
302and
303.Er ENOTDIR ;
304however, the
305.Dv GLOB_ERR
306flag will still cause an immediate
307return when this happens.
308.Pp
309If
310.Fa errfunc
311returns non-zero,
312.Fn glob
313stops the scan and returns
314.Dv GLOB_ABORTED
315after setting
316.Fa gl_pathc
317and
318.Fa gl_pathv
319to reflect any paths already matched.
320This also happens if an error is encountered and
321.Dv GLOB_ERR
322is set in
323.Fa flags ,
324regardless of the return value of
325.Fa errfunc ,
326if called.
327If
328.Dv GLOB_ERR
329is not set and either
330.Fa errfunc
331is
332.Dv NULL
333or
334.Fa errfunc
335returns zero, the error is ignored.
336.Pp
337The
338.Fn globfree
339function frees any space associated with
340.Fa pglob
341from a previous call(s) to
342.Fn glob .
343.Sh RETURN VALUES
344On successful completion,
345.Fn glob
346returns zero.
347In addition, the fields of
348.Fa pglob
349contain the values described below:
350.Bl -tag -width GLOB_NOCHECK
351.It Fa gl_pathc
352contains the total number of matched pathnames so far.
353This includes other matches from previous invocations of
354.Fn glob
355if
356.Dv GLOB_APPEND
357was specified.
358.It Fa gl_matchc
359contains the number of matched pathnames in the current invocation of
360.Fn glob .
361.It Fa gl_flags
362contains a copy of the
363.Fa flags
364argument with the bit
365.Dv GLOB_MAGCHAR
366set if
367.Fa pattern
368contained any of the special characters ``*'', ``?'' or ``['', cleared
369if not.
370.It Fa gl_pathv
371contains a pointer to a
372.Dv NULL Ns -terminated
373list of matched pathnames.
374However, if
375.Fa gl_pathc
376is zero, the contents of
377.Fa gl_pathv
378are undefined.
379.El
380.Pp
381If
382.Fn glob
383terminates due to an error, it sets errno and returns one of the
384following non-zero constants, which are defined in the include
385file
386.In glob.h :
387.Bl -tag -width GLOB_NOCHECK
388.It Dv GLOB_NOSPACE
389An attempt to allocate memory failed, or if
390.Fa errno
391was 0
392.Dv GLOB_LIMIT
393was specified in the flags and
394.Fa pglob\->gl_matchc
395or more patterns were matched.
396.It Dv GLOB_ABORTED
397The scan was stopped because an error was encountered and either
398.Dv GLOB_ERR
399was set or
400.Fa \*(lp*errfunc\*(rp\*(lp\*(rp
401returned non-zero.
402.It Dv GLOB_NOMATCH
403The pattern did not match a pathname and
404.Dv GLOB_NOCHECK
405was not set.
406.El
407.Pp
408The arguments
409.Fa pglob\->gl_pathc
410and
411.Fa pglob\->gl_pathv
412are still set as specified above.
413.Sh EXAMPLES
414A rough equivalent of
415.Ql "ls -l *.c *.h"
416can be obtained with the
417following code:
418.Bd -literal -offset indent
419glob_t g;
420
421g.gl_offs = 2;
422glob("*.c", GLOB_DOOFFS, NULL, &g);
423glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
424g.gl_pathv[0] = "ls";
425g.gl_pathv[1] = "-l";
426execvp("ls", g.gl_pathv);
427.Ed
428.Sh CAVEATS
429The
430.Fn glob
431function will not match filenames that begin with a period
432unless this is specifically requested (e.g., by ".*").
433.Sh SEE ALSO
434.Xr sh 1 ,
435.Xr fnmatch 3 ,
436.Xr regexp 3
437.Sh STANDARDS
438The current implementation of the
439.Fn glob
440function
441.Em does not
442conform to
443.St -p1003.2 .
444Collating symbol expressions, equivalence class expressions and
445character class expressions are not supported.
446.Pp
447The flags
448.Dv GLOB_ALTDIRFUNC ,
449.Dv GLOB_BRACE ,
450.Dv GLOB_LIMIT ,
451.Dv GLOB_MAGCHAR ,
452.Dv GLOB_NOMAGIC ,
453and
454.Dv GLOB_TILDE ,
455and the fields
456.Fa gl_matchc
457and
458.Fa gl_flags
459are extensions to the
460.Tn POSIX
461standard and
462should not be used by applications striving for strict
463conformance.
464.Sh HISTORY
465The
466.Fn glob
467and
468.Fn globfree
469functions first appeared in
470.Bx 4.4 .
471.Sh BUGS
472Patterns longer than
473.Dv MAXPATHLEN
474may cause unchecked errors.
475.Pp
476The
477.Fn glob
478argument
479may fail and set errno for any of the errors specified for the
480library routines
481.Xr stat 2 ,
482.Xr closedir 3 ,
483.Xr opendir 3 ,
484.Xr readdir 3 ,
485.Xr malloc 3 ,
486and
487.Xr free 3 .