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

g.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &g);
glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
g.gl_pathv[0] = "ls";
g.gl_pathv[1] = "-l";
execvp("ls", g.gl_pathv);
.Ed
.Sh ERRORS
The
.Fn glob
function may fail and set
.Va errno
for any of the errors specified for the library routines
.Xr stat 2 ,
.Xr closedir 3 ,
.Xr opendir 3 ,
.Xr readdir 3 ,
.Xr malloc 3 ,
and
.Xr free 3 .
.Sh SEE ALSO
.Xr sh 1 ,
.Xr fnmatch 3 ,
.Xr regex 3 ,
.Xr glob 7
.Sh STANDARDS
The
.Fn glob
function is expected to conform to
.St -p1003.2
and
.St -xpg4.2 .
Note, however, that the flags
.Dv GLOB_ALTDIRFUNC ,
.Dv GLOB_BRACE ,
.Dv GLOB_KEEPSTAT ,
.Dv GLOB_MAGCHAR ,
.Dv GLOB_NOMAGIC ,
.Dv GLOB_QUOTE ,
.Dv GLOB_TILDE ,
and
.Dv GLOB_LIMIT
and the fields
.Fa gl_matchc ,
.Fa gl_statv
and
.Fa gl_flags
should not be used by applications striving for strict standards conformance.
.Sh HISTORY
The
.Fn glob
and
.Fn globfree
functions first appeared in
.Bx 4.4 .
.Sh BUGS
Patterns longer than
.Dv MAXPATHLEN
may cause unchecked errors.