Deleted Added
full compact
glob.3 (59243) glob.3 (83098)
1.\" Copyright (c) 1989 The Regents of the University of California.
2.\" All rights reserved.
1.\" $NetBSD: glob.3,v 1.17 2001/03/16 21:09:05 christos Exp $
3.\"
2.\"
3.\" Copyright (c) 1989, 1991, 1993, 1994
4.\" The Regents of the University of California. All rights reserved.
5.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Guido van Rossum.
6.\" This code is derived from software contributed to Berkeley by
7.\" Guido van Rossum.
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
6.\"
23.\"
7.\" Redistribution and use in source and binary forms are permitted provided
8.\" that: (1) source distributions retain this entire copyright notice and
9.\" comment, and (2) distributions including binaries display the following
10.\" acknowledgement: ``This product includes software developed by the
11.\" University of California, Berkeley and its contributors'' in the
12.\" documentation or other materials provided with the distribution and in
13.\" all advertising materials mentioning features or use of this software.
14.\" Neither the name of the University nor the names of its contributors may
15.\" be used to endorse or promote products derived from this software without
16.\" specific prior written permission.
17.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
20.\"
35.\"
21.\" @(#)glob.3 5.3 (Berkeley) 3/19/91
36.\" @(#)glob.3 8.3 (Berkeley) 4/16/94
22.\"
37.\"
23.TH GLOB 3 "March 19, 1991"
24.UC 7
25.SH NAME
26glob, globfree \- generate pathnames matching a pattern
27.SH SYNOPSIS
28.nf
29#include <glob.h>
30
31glob(const char *pattern, int flags,
32 const int (*errfunc)(char *, int), glob_t *pglob);
33
34void globfree(glob_t *pglob);
35.fi
36.SH DESCRIPTION
37.I Glob
38.Dd March 31, 1998
39.Dt GLOB 3
40.Os
41.Sh NAME
42.Nm glob ,
43.Nm globfree
44.Nd generate pathnames matching a pattern
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.Fd #include <glob.h>
49.Ft int
50.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(const char *, int)" "glob_t *pglob"
51.Ft void
52.Fn globfree "glob_t *pglob"
53.Sh DESCRIPTION
54The
55.Fn glob
56function
38is a pathname generator that implements the rules for file name pattern
39matching used by the shell.
57is a pathname generator that implements the rules for file name pattern
58matching used by the shell.
40.PP
59.Pp
41The include file
60The include file
42.I glob.h
61.Pa glob.h
43defines the structure type
62defines the structure type
44.IR glob_t ,
63.Fa glob_t ,
45which contains at least the following fields:
64which contains at least the following fields:
46.sp
47.RS
48.nf
49.ta .5i +\w'char **gl_pathv;\0\0\0'u
65.Bd -literal
50typedef struct {
51 int gl_pathc; /* count of total paths so far */
52 int gl_matchc; /* count of paths matching pattern */
53 int gl_offs; /* reserved at beginning of gl_pathv */
54 int gl_flags; /* returned flags */
55 char **gl_pathv; /* list of paths matching pattern */
56} glob_t;
66typedef struct {
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 */
72} glob_t;
57.fi
58.RE
59.PP
73.Ed
74.Pp
60The argument
75The argument
61.I pattern
76.Fa pattern
62is a pointer to a pathname pattern to be expanded.
77is a pointer to a pathname pattern to be expanded.
63.I Glob
78The
79.Fn glob
80argument
64matches all accessible pathnames against the pattern and creates
65a list of the pathnames that match.
66In order to have access to a pathname,
81matches all accessible pathnames against the pattern and creates
82a list of the pathnames that match.
83In order to have access to a pathname,
67.I glob
84.Fn glob
68requires search permission on every component of a path except the last
69and read permission on each directory of any filename component of
85requires search permission on every component of a path except the last
86and read permission on each directory of any filename component of
70.I pattern
71that contains any of the special characters ``*'', ``?'' or ``[''.
72.PP
73.I Glob
87.Fa pattern
88that contains any of the special characters
89.Ql * ,
90.Ql ?
91or
92.Ql [ .
93.Pp
94The
95.Fn glob
96argument
74stores the number of matched pathnames into the
97stores the number of matched pathnames into the
75.I gl_pathc
98.Fa gl_pathc
76field, and a pointer to a list of pointers to pathnames into the
99field, and a pointer to a list of pointers to pathnames into the
77.I gl_pathv
100.Fa gl_pathv
78field.
101field.
79The first pointer after the last pathname is NULL.
102The first pointer after the last pathname is
103.Dv NULL .
80If the pattern does not match any pathnames, the returned number of
81matched paths is set to zero.
104If the pattern does not match any pathnames, the returned number of
105matched paths is set to zero.
82.PP
106.Pp
83It is the caller's responsibility to create the structure pointed to by
107It is the caller's responsibility to create the structure pointed to by
84.IR pglob .
108.Fa pglob .
85The
109The
86.I glob
110.Fn glob
87function allocates other space as needed, including the memory pointed
88to by
111function allocates other space as needed, including the memory pointed
112to by
89.IR gl_pathv .
90.PP
113.Fa gl_pathv .
114.Pp
91The argument
115The argument
92.I flags
116.Fa flags
93is used to modify the behavior of
117is used to modify the behavior of
94.IR glob .
118.Fn glob .
95The value of
119The value of
96.I flags
97is the bitwise inclusive OR of any of the following
120.Fa flags
121is the bitwise inclusive
122.Tn OR
123of any of the following
98values defined in
124values defined in
99.IR glob.h :
100.TP
101GLOB_APPEND
125.Pa glob.h :
126.Bl -tag -width GLOB_ALTDIRFUNC
127.It Dv GLOB_APPEND
102Append pathnames generated to the ones from a previous call (or calls)
103to
128Append pathnames generated to the ones from a previous call (or calls)
129to
104.IR glob .
130.Fn glob .
105The value of
131The value of
106.I gl_pathc
132.Fa gl_pathc
107will be the total matches found by this call and the previous call(s).
108The pathnames are appended to, not merged with the pathnames returned by
109the previous call(s).
110Between calls, the caller must not change the setting of the
133will be the total matches found by this call and the previous call(s).
134The pathnames are appended to, not merged with the pathnames returned by
135the previous call(s).
136Between calls, the caller must not change the setting of the
111GLOB_DOOFFS flag, nor change the value of
112.I gl_offs
137.Dv GLOB_DOOFFS
138flag, nor change the value of
139.Fa gl_offs
113when
140when
114GLOB_DOOFFS is set, nor (obviously) call
115.I globfree
141.Dv GLOB_DOOFFS
142is set, nor (obviously) call
143.Fn globfree
116for
144for
117.I pglob.
118.TP
119GLOB_DOOFFS
145.Fa pglob .
146.It Dv GLOB_DOOFFS
120Make use of the
147Make use of the
121.I gl_offs
148.Fa gl_offs
122field.
123If this flag is set,
149field.
150If this flag is set,
124.I gl_offs
125is used to specify how many NULL pointers to prepend to the beginning
151.Fa gl_offs
152is used to specify how many
153.Dv NULL
154pointers to prepend to the beginning
126of the
155of the
127.I gl_pathv
156.Fa gl_pathv
128field.
129In other words,
157field.
158In other words,
130.I gl_pathv
159.Fa gl_pathv
131will point to
160will point to
132.I gl_offs
133NULL pointers,
161.Fa gl_offs
162.Dv NULL
163pointers,
134followed by
164followed by
135.I gl_pathc
136pathname pointers, followed by a NULL pointer.
137.TP
138GLOB_ERR
165.Fa gl_pathc
166pathname pointers, followed by a
167.Dv NULL
168pointer.
169.It Dv GLOB_ERR
139Causes
170Causes
140.I glob
171.Fn glob
141to return when it encounters a directory that it cannot open or read.
142Ordinarily,
172to return when it encounters a directory that it cannot open or read.
173Ordinarily,
143.I glob
174.Fn glob
144continues to find matches.
175continues to find matches.
145.TP
146GLOB_MARK
176.It Dv GLOB_MARK
147Each pathname that is a directory that matches
177Each pathname that is a directory that matches
148.I pattern
178.Fa pattern
149has a slash
150appended.
179has a slash
180appended.
151.TP
152GLOB_NOSORT
153By default, the pathnames are sorted in ascending ASCII order;
154this flag prevents that sorting (speeding up
155.IR glob ).
156.TP
157GLOB_NOCHECK
181.It Dv GLOB_NOCHECK
158If
182If
159.I pattern
183.Fa pattern
160does not match any pathname, then
184does not match any pathname, then
161.I glob
185.Fn glob
162returns a list
163consisting of only
186returns a list
187consisting of only
164.IR pattern ,
188.Fa pattern ,
165with the number of total pathnames is set to 1, and the number of matched
166pathnames set to 0.
189with the number of total pathnames is set to 1, and the number of matched
190pathnames set to 0.
167If
168.I GLOB_QUOTE
169is set, its effect is present in the pattern returned.
170.TP
171GLOB_QUOTE
172Use the backslash (``\e'') character for quoting: every occurrence of
173a backslash followed by a character in the pattern is replaced by that
174character, avoiding any special interpretation of the character.
175.TP
176GLOB_NOMAGIC
177Is the same as GLOB_NOCHECK but it only appends the
178.IR pattern
191.It Dv GLOB_NOSORT
192By default, the pathnames are sorted in ascending
193.Tn ASCII
194order;
195this flag prevents that sorting (speeding up
196.Fn glob ) .
197.El
198.Pp
199The following values may also be included in
200.Fa flags ,
201however, they are non-standard extensions to
202.St -p1003.2 .
203.Bl -tag -width GLOB_ALTDIRFUNC
204.It Dv GLOB_ALTDIRFUNC
205The following additional fields in the pglob structure have been
206initialized with alternate functions for glob to use to open, read,
207and close directories and to get stat information on names found
208in those directories.
209.Bd -literal
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);
215.Ed
216.Pp
217This extension is provided to allow programs such as
218.Xr restore 8
219to provide globbing from directories stored on tape.
220.It Dv GLOB_BRACE
221Pre-process the pattern string to expand
222.Ql {pat,pat,...}
223strings like
224.Xr csh 1 .
225The pattern
226.Ql {}
227is left unexpanded for historical reasons
228.Po
229.Xr csh 1
230does the same thing to ease typing of
231.Xr find 1
232patterns
233.Pc .
234.It Dv GLOB_MAGCHAR
235Set by the
236.Fn glob
237function if the pattern included globbing characters.
238See the description of the usage of the
239.Fa gl_matchc
240structure member for more details.
241.It Dv GLOB_NOMAGIC
242Is the same as
243.Dv GLOB_NOCHECK
244but it only appends the
245.Fa pattern
179if it does not contain any of the special characters ``*'', ``?'' or ``[''.
246if it does not contain any of the special characters ``*'', ``?'' or ``[''.
180GLOB_NOMAGIC is used to simplify implementing the globbing behavior in
181.IR csh(1).
182.PP
247.Dv GLOB_NOMAGIC
248is provided to simplify implementing the historic
249.Xr csh 1
250globbing behavior and should probably not be used anywhere else.
251.It Dv GLOB_NOESCAPE
252Disable the use of the backslash
253.Pq Ql \e
254character for quoting.
255.It Dv GLOB_TILDE
256Expand patterns that start with
257.Ql ~
258to user name home directories.
259.It Dv GLOB_LIMIT
260Limit the amount of memory used by matches to
261.Li ARG_MAX
262This option should be set for programs that can be coerced to a denial of
263service attack via patterns that expand to a very large number of matches,
264such as a long string of
265.Li */../*/..
266.El
267.Pp
183If, during the search, a directory is encountered that cannot be opened
184or read and
268If, during the search, a directory is encountered that cannot be opened
269or read and
185.I errfunc
186is non-NULL,
187.I glob
188calls (*\fIerrfunc\fP)(\fIpath\fP, \fIerrno\fP).
189This may be unintuitive: a pattern like ``*/Makefile'' will try to
190.IR stat (2)
191``foo/Makefile'' even if ``foo'' is not a directory, resulting in a
270.Fa errfunc
271is
272.Pf non- Dv NULL ,
273.Fn glob
274calls
275.Fa (*errfunc)(path, errno) .
276This may be unintuitive: a pattern like
277.Ql */Makefile
278will try to
279.Xr stat 2
280.Ql foo/Makefile
281even if
282.Ql foo
283is not a directory, resulting in a
192call to
284call to
193.IR errfunc .
194The error routine can suppress this action by testing for ENOENT and
195ENOTDIR; however, the GLOB_ERR flag will still cause an immediate
285.Fa errfunc .
286The error routine can suppress this action by testing for
287.Dv ENOENT
288and
289.Dv ENOTDIR ;
290however, the
291.Dv GLOB_ERR
292flag will still cause an immediate
196return when this happens.
293return when this happens.
197.PP
294.Pp
198If
295If
199.I errfunc
296.Fa errfunc
200returns non-zero,
297returns non-zero,
201.I glob
298.Fn glob
202stops the scan and returns
299stops the scan and returns
203.I GLOB_ABEND
300.Dv GLOB_ABORTED
204after setting
301after setting
205.I gl_pathc
302.Fa gl_pathc
206and
303and
207.I gl_pathv
304.Fa gl_pathv
208to reflect any paths already matched.
209This also happens if an error is encountered and
305to reflect any paths already matched.
306This also happens if an error is encountered and
210.I GLOB_ERR
307.Dv GLOB_ERR
211is set in
308is set in
212.IR flags ,
309.Fa flags ,
213regardless of the return value of
310regardless of the return value of
214.IR errfunc ,
311.Fa errfunc ,
215if called.
216If
312if called.
313If
217.I GLOB_ERR
314.Dv GLOB_ERR
218is not set and either
315is not set and either
219.I errfunc
220is NULL or
221.I errfunc
316.Fa errfunc
317is
318.Dv NULL
319or
320.Fa errfunc
222returns zero, the error is ignored.
321returns zero, the error is ignored.
223.PP
322.Pp
224The
323The
225.I globfree
324.Fn globfree
226function frees any space associated with
325function frees any space associated with
227.I pglob
326.Fa pglob
228from a previous call(s) to
327from a previous call(s) to
229.IR glob .
230.SH RETURNS
328.Fn glob .
329.Pp
330The historical
331.Dv GLOB_QUOTE
332flag is no longer supported.
333Per
334.St -p1003.2-92 ,
335backslash escaping of special characters is the default behaviour;
336it may be disabled by specifying the
337.Dv GLOB_NOESCAPE
338flag.
339.Sh RETURN VALUES
231On successful completion,
340On successful completion,
232.I glob
341.Fn glob
233returns zero.
234In addition the fields of
342returns zero.
343In addition the fields of
235.I pglob
344.Fa pglob
236contain the values described below:
345contain the values described below:
237.TP
238.I gl_pathc
346.Bl -tag -width GLOB_NOCHECK
347.It Fa gl_pathc
239contains the total number of matched pathnames so far.
348contains the total number of matched pathnames so far.
240This includes other matches from previous invocations of
241.I glob
242if
243.I GLOB_APPEND
349This includes other matches from previous invocations of
350.Fn glob
351if
352.Dv GLOB_APPEND
244was specified.
353was specified.
245.TP
246.I gl_matchc
354.It Fa gl_matchc
247contains the number of matched pathnames in the current invocation of
355contains the number of matched pathnames in the current invocation of
248.I glob.
249.TP
250.I gl_flags
251contains a copy of the
252.I flags
253parameter with the bit GLOB_MAGCHAR set if
254.I pattern
356.Fn glob .
357.It Fa gl_flags
358contains a copy of the
359.Fa flags
360parameter with the bit
361.Dv GLOB_MAGCHAR
362set if
363.Fa pattern
255contained any of the special characters ``*'', ``?'' or ``['', cleared
256if not.
364contained any of the special characters ``*'', ``?'' or ``['', cleared
365if not.
257.TP
258.I gl_pathv
259contains a pointer to a NULL-terminated list of matched pathnames.
366.It Fa gl_pathv
367contains a pointer to a
368.Dv NULL Ns -terminated
369list of matched pathnames.
260However, if
370However, if
261.I gl_pathc
371.Fa gl_pathc
262is zero, the contents of
372is zero, the contents of
263.I gl_pathv
373.Fa gl_pathv
264are undefined.
374are undefined.
265.PP
375.El
376.Pp
266If
377If
267.I glob
268terminates due to an error, it sets errno and returns one of the
269following non-zero constants, which are defined in the include
270file <glob.h>:
271.TP
272GLOB_NOSPACE
273An attempt to allocate memory failed.
274.TP
275GLOB_ABEND
378.Fn glob
379terminates due to an error, it sets
380.Va errno
381and returns one of the following non-zero constants, which are defined
382in the include file
383.Aq Pa glob.h :
384.Bl -tag -width GLOB_ABORTEDXXX
385.It Dv GLOB_ABORTED
276The scan was stopped because an error was encountered and either
386The scan was stopped because an error was encountered and either
277GLOB_ERR was set or (*\fIerrfunc\fR)() returned non-zero.
278.PP
387.Dv GLOB_ERR
388was set or
389.Fa (*errfunc)()
390returned non-zero.
391.It Dv GLOB_NOMATCH
392The pattern does not match any existing pathname, and
393.Dv GLOB_NOCHECK
394was not set int
395.Dv flags .
396.It Dv GLOB_NOSPACE
397An attempt to allocate memory failed, or if
398.Va errno
399was 0
400.Li GLOB_LIMIT
401was specified in the flags and
402.Li ARG_MAX
403patterns were matched.
404.El
405.Pp
406The historical
407.Dv GLOB_ABEND
408return constant is no longer supported. Portable applications should use the
409.Dv GLOB_ABORTED
410constant instead.
411.Pp
279The arguments
412The arguments
280.I pglob->gl_pathc
413.Fa pglob\->gl_pathc
281and
414and
282.I pglob->gl_pathv
415.Fa pglob\->gl_pathv
283are still set as specified above.
416are still set as specified above.
284.SH STANDARDS
285The
286.I glob
287function is expected to be POSIX 1003.2 compatible with the exception
288that the flag
289.I GLOB_QUOTE
290and the fields
291.I gl_matchc
292and
293.I gl_flags
294should not be used by applications striving for strict POSIX conformance.
295.SH EXAMPLE
296A rough equivalent of ``ls -l *.c *.h'' can be obtained with the
417.Sh ENVIRONMENT
418.Bl -tag -width HOME -compact
419.It Ev HOME
420If defined, used as the home directory of the current user in
421tilde expansions.
422.El
423.Sh EXAMPLE
424A rough equivalent of
425.Ql "ls -l *.c *.h"
426can be obtained with the
297following code:
427following code:
298.sp
299.nf
300.RS
428.Bd -literal -offset indent
301glob_t g;
302
303g.gl_offs = 2;
304glob("*.c", GLOB_DOOFFS, NULL, &g);
305glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
306g.gl_pathv[0] = "ls";
307g.gl_pathv[1] = "-l";
308execvp("ls", g.gl_pathv);
429glob_t g;
430
431g.gl_offs = 2;
432glob("*.c", GLOB_DOOFFS, NULL, &g);
433glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
434g.gl_pathv[0] = "ls";
435g.gl_pathv[1] = "-l";
436execvp("ls", g.gl_pathv);
309.RE
310.fi
311.SH SEE ALSO
312sh(1), fnmatch(3), wordexp(3), regexp(3)
313.SH BUGS
314Patterns longer than MAXPATHLEN may cause unchecked errors.
315.PP
316.I Glob
317may fail and set errno for any of the errors specified for the
318library routines
319.I stat (2),
320.I closedir (3),
321.I opendir (3),
322.I readdir (3),
323.I malloc (3),
437.Ed
438.Sh SEE ALSO
439.Xr sh 1 ,
440.Xr fnmatch 3 ,
441.Xr regexp 3
442.Sh STANDARDS
443The
444.Fn glob
445function is expected to be
446.St -p1003.2
447compatible with the exception
448that the flags
449.Dv GLOB_ALTDIRFUNC,
450.Dv GLOB_BRACE
451.Dv GLOB_MAGCHAR,
452.Dv GLOB_NOMAGIC,
453.Dv GLOB_TILDE,
324and
454and
325.I free (3).
326
455.Dv GLOB_LIMIT
456and the fields
457.Fa gl_matchc
458and
459.Fa gl_flags
460should not be used by applications striving for strict
461.Tn POSIX
462conformance.
463.Sh HISTORY
464The
465.Fn glob
466and
467.Fn globfree
468functions first appeared in
469.Bx 4.4 .
470.Sh BUGS
471Patterns longer than
472.Dv MAXPATHLEN
473may cause unchecked errors.
474.Pp
475The
476.Fn glob
477function may fail and set
478.Va errno
479for any of the errors specified for the library routines
480.Xr stat 2 ,
481.Xr closedir 3 ,
482.Xr opendir 3 ,
483.Xr readdir 3 ,
484.Xr malloc 3 ,
485and
486.Xr free 3 .