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