xref: /openbsd/lib/libc/gen/glob.3 (revision d32639f6)
1.\"	$OpenBSD: glob.3,v 1.39 2022/09/11 06:38:10 jmc 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: September 11 2022 $
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.In 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.In glob.h
53defines the structure type
54.Vt glob_t ,
55which contains at least the following fields:
56.Bd -literal
57typedef struct {
58	size_t gl_pathc;	/* count of total paths so far */
59	size_t gl_matchc;	/* count of paths matching pattern */
60	size_t 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 OR of any of the following values defined in
107.In glob.h :
108.Bl -tag -width GLOB_ALTDIRFUNC
109.It Dv GLOB_APPEND
110Append pathnames generated to the ones from a previous call (or calls)
111to
112.Fn glob .
113The value of
114.Fa gl_pathc
115will be the total matches found by this call and the previous call(s).
116The pathnames are appended to, not merged with the pathnames returned by
117the previous call(s).
118Between calls, the caller must not change the setting of the
119.Dv GLOB_DOOFFS
120flag, nor change the value of
121.Fa gl_offs
122when
123.Dv GLOB_DOOFFS
124is set, nor (obviously) call
125.Fn globfree
126for
127.Fa pglob .
128.It Dv GLOB_DOOFFS
129Make use of the
130.Fa gl_offs
131field.
132If this flag is set,
133.Fa gl_offs
134is used to specify how many
135null pointers to prepend to the beginning
136of the
137.Fa gl_pathv
138field.
139In other words,
140.Fa gl_pathv
141will point to
142.Fa gl_offs
143null pointers,
144followed by
145.Fa gl_pathc
146pathname pointers, followed by a null pointer.
147.It Dv GLOB_ERR
148Causes
149.Fn glob
150to return when it encounters a directory that it cannot open or read.
151Ordinarily,
152.Fn glob
153continues to find matches.
154.It Dv GLOB_MARK
155Each pathname that is a directory that matches
156.Fa pattern
157has a slash
158appended.
159.It Dv GLOB_NOCHECK
160If
161.Fa pattern
162does not match any pathname, then
163.Fn glob
164returns a list
165consisting of only
166.Fa pattern ,
167with the number of total pathnames set to 1, and the number of matched
168pathnames set to 0.
169.It Dv GLOB_NOESCAPE
170Normally, every occurrence of a backslash
171.Pq Ql \e
172followed by a character in
173.Fa pattern
174is replaced by that character.
175This is done to negate any special meaning for the character.
176If the
177.Dv GLOB_NOESCAPE
178flag is set, a backslash character is treated as an ordinary character.
179.It Dv GLOB_NOSORT
180By default, the pathnames are sorted in ascending ASCII order;
181this flag prevents that sorting (speeding up
182.Fn glob ) .
183.El
184.Pp
185The following values may also be included in
186.Fa flags ,
187however, they are non-standard extensions to
188.St -p1003.2 .
189.Bl -tag -width GLOB_ALTDIRFUNC
190.It Dv GLOB_ALTDIRFUNC
191The following additional fields in the
192.Fa pglob
193structure have been
194initialized with alternate functions for
195.Fn glob
196to use to open, read, and close directories and to get stat information
197on names found in those directories:
198.Bd -literal
199	void *(*gl_opendir)(const char *);
200	struct dirent *(*gl_readdir)(void *);
201	void (*gl_closedir)(void *);
202	int (*gl_lstat)(const char *, struct stat *);
203	int (*gl_stat)(const char *, struct stat *);
204.Ed
205.Pp
206This extension is provided to allow programs such as
207.Xr restore 8
208to provide globbing from directories stored on tape.
209.It Dv GLOB_BRACE
210Pre-process the pattern string to expand
211.Ql {pat,pat,...}
212strings like
213.Xr csh 1 .
214The pattern
215.Ql {}
216is left unexpanded for historical reasons.
217.Xr ( csh 1
218does the same thing to ease typing of
219.Xr find 1
220patterns.)
221.It Dv GLOB_KEEPSTAT
222Retain a copy of the
223.Xr stat 2
224information retrieved for matching paths in the
225.Fa gl_statv
226array:
227.Bd -literal -offset indent
228struct stat **gl_statv;
229.Ed
230.Pp
231This option may be used to avoid
232.Xr lstat 2
233lookups in cases where they are expensive.
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
246if it does not contain any of the special characters
247.Ql * ,
248.Ql \&? ,
249or
250.Ql \&[ .
251.Dv GLOB_NOMAGIC
252is provided to simplify implementing the historic
253.Xr csh 1
254globbing behavior and should probably not be used anywhere else.
255.It Dv GLOB_QUOTE
256This option has no effect and is included for backwards
257compatibility with older sources.
258.It Dv GLOB_TILDE
259Expand patterns that start with
260.Ql ~
261to user name home directories.
262.It Dv GLOB_LIMIT
263Limit the amount of memory used to store matched strings to
264.Li 64K ,
265the number of
266.Xr stat 2
267calls to 2048, and the number of
268.Xr readdir 3
269calls to 16K.
270This option should be set for programs that can be coerced to a denial of
271service attack via patterns that expand to a very large number of matches,
272such as a long string of
273.Ql */../*/.. .
274.El
275.Pp
276If, during the search, a directory is encountered that cannot be opened
277or read and
278.Fa errfunc
279is non-null,
280.Fn glob
281calls
282.Fn (*errfunc) path errno .
283This may be unintuitive: a pattern like
284.Dq */Makefile
285will try to
286.Xr stat 2
287.Dq foo/Makefile
288even if
289.Dq foo
290is not a directory, resulting in a call to
291.Fa errfunc .
292The error routine can suppress this action by testing for
293.Er ENOENT
294and
295.Er ENOTDIR ;
296however, the
297.Dv GLOB_ERR
298flag will still cause an immediate return when this happens.
299.Pp
300If
301.Fa errfunc
302returns non-zero,
303.Fn glob
304stops the scan and returns
305.Dv GLOB_ABORTED
306after setting
307.Fa gl_pathc
308and
309.Fa gl_pathv
310to reflect any paths already matched.
311This also happens if an error is encountered and
312.Dv GLOB_ERR
313is set in
314.Fa flags ,
315regardless of the return value of
316.Fa errfunc ,
317if called.
318If
319.Dv GLOB_ERR
320is not set and either
321.Fa errfunc
322is
323.Dv NULL
324or
325.Fa errfunc
326returns zero, the error is ignored.
327.Pp
328The
329.Fn globfree
330function frees any space associated with
331.Fa pglob
332from a previous call(s) to
333.Fn glob .
334.Sh RETURN VALUES
335On successful completion,
336.Fn glob
337returns zero.
338In addition the fields of
339.Fa pglob
340contain the values described below:
341.Bl -tag -width GLOB_NOCHECK
342.It Fa gl_pathc
343Contains the total number of matched pathnames so far.
344This includes other matches from previous invocations of
345.Fn glob
346if
347.Dv GLOB_APPEND
348was specified.
349.It Fa gl_matchc
350Contains the number of matched pathnames in the current invocation of
351.Fn glob .
352.It Fa gl_flags
353Contains a copy of the
354.Fa flags
355parameter with the bit
356.Dv GLOB_MAGCHAR
357set if
358.Fa pattern
359contained any of the special characters
360.Ql * ,
361.Ql \&? ,
362or
363.Ql \&[ ,
364cleared if not.
365.It Fa gl_pathv
366Contains a pointer to a null-terminated list of matched pathnames.
367However, if
368.Fa gl_pathc
369is zero, the contents of
370.Fa gl_pathv
371are undefined.
372.It Fa gl_statv
373If the
374.Dv GLOB_KEEPSTAT
375flag was set,
376.Fa gl_statv
377contains a pointer to a null-terminated list of matched
378.Xr stat 2
379objects corresponding to the paths in
380.Fa gl_pathc .
381.El
382.Pp
383If
384.Fn glob
385terminates due to an error, it sets
386.Va errno
387and returns one of the following non-zero constants, which are defined
388in the include file
389.In glob.h :
390.Bl -tag -width GLOB_NOCHECK
391.It Dv GLOB_NOSPACE
392An attempt to allocate memory failed, or if
393.Va errno
394was 0
395.Li GLOB_LIMIT
396was specified in the flags and
397.Li ARG_MAX or more
398patterns were matched.
399.It Dv GLOB_ABORTED
400The scan was stopped because an error was encountered and either
401.Dv GLOB_ERR
402was set, or
403.Fa errfunc
404returned non-zero.
405.It Dv GLOB_NOMATCH
406The pattern did not match a pathname and
407.Dv GLOB_NOCHECK
408was not set.
409.It Dv GLOB_NOSYS
410The requested function is not supported by this version of
411.Fn glob .
412.El
413.Pp
414The arguments
415.Fa pglob\->gl_pathc
416and
417.Fa pglob\->gl_pathv
418are still set as specified above.
419.Sh EXAMPLES
420A rough equivalent of
421.Ql "ls -l *.c *.h"
422can be obtained with the following code:
423.Bd -literal -offset indent
424glob_t g;
425
426g.gl_offs = 2;
427glob("*.c", GLOB_DOOFFS, NULL, &g);
428glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
429g.gl_pathv[0] = "ls";
430g.gl_pathv[1] = "-l";
431execvp("ls", g.gl_pathv);
432.Ed
433.Sh ERRORS
434The
435.Fn glob
436function may fail and set
437.Va errno
438for any of the errors specified for the library routines
439.Xr stat 2 ,
440.Xr closedir 3 ,
441.Xr opendir 3 ,
442.Xr readdir 3 ,
443.Xr malloc 3 ,
444and
445.Xr free 3 .
446.Sh SEE ALSO
447.Xr sh 1 ,
448.Xr fnmatch 3 ,
449.Xr regex 3 ,
450.Xr glob 7
451.Sh STANDARDS
452The
453.Fn glob
454function is expected to conform to
455.St -p1003.2
456and
457.St -xpg4.2 .
458Note, however, that the flags
459.Dv GLOB_ALTDIRFUNC ,
460.Dv GLOB_BRACE ,
461.Dv GLOB_KEEPSTAT ,
462.Dv GLOB_MAGCHAR ,
463.Dv GLOB_NOMAGIC ,
464.Dv GLOB_QUOTE ,
465.Dv GLOB_TILDE ,
466and
467.Dv GLOB_LIMIT
468and the fields
469.Fa gl_matchc ,
470.Fa gl_statv
471and
472.Fa gl_flags
473should not be used by applications striving for strict standards conformance.
474.Sh HISTORY
475A stand-alone program,
476.Pa /etc/glob ,
477first appeared in
478.At v1 .
479In PWB/UNIX 1.0 this functionality was incorporated into the shell itself.
480.Pp
481The
482.Fn glob
483and
484.Fn globfree
485functions in their current form first appeared in the C library of
486.Bx 4.3 Reno .
487.Sh CAVEATS
488On systems other than
489.Ox ,
490the
491.Dv LC_COLLATE
492.Xr locale 1
493category can affect the sort order; see CAVEATS in
494.Xr setlocale 3
495for details.
496.Sh BUGS
497Patterns longer than
498.Dv PATH_MAX
499may cause unchecked errors.
500