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