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