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