summaryrefslogtreecommitdiffstats
path: root/upstream/archlinux/man3p/glob.3p
blob: 99b4aed52c1ae3c3aa8d87ff4504183ed5cc944b (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
'\" et
.TH GLOB "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
glob,
globfree
\(em generate pathnames matching a pattern
.SH SYNOPSIS
.LP
.nf
#include <glob.h>
.P
int glob(const char *restrict \fIpattern\fP, int \fIflags\fP,
    int(*\fIerrfunc\fP)(const char *\fIepath\fP, int \fIeerrno\fP),
    glob_t *restrict \fIpglob\fP);
void globfree(glob_t *\fIpglob\fP);
.fi
.SH DESCRIPTION
The
\fIglob\fR()
function is a pathname generator that shall implement the rules
defined in the Shell and Utilities volume of POSIX.1\(hy2017,
.IR "Section 2.13" ", " "Pattern Matching Notation",
with optional support for rule 3 in the Shell and Utilities volume of POSIX.1\(hy2017,
.IR "Section 2.13.3" ", " "Patterns Used for Filename Expansion".
.P
The structure type
.BR glob_t
is defined in
.IR <glob.h> 
and includes at least the following members:
.TS
center box tab(!);
cB | cB | cB
lw(1.25i)B | lw(1.25i)I | lw(2.5i).
Member Type!Member Name!Description
_
size_t!gl_pathc!Count of paths matched by \fIpattern\fP.
char **!gl_pathv!Pointer to a list of matched pathnames.
size_t!gl_offs!T{
Slots to reserve at the beginning of \fIgl_pathv\fP.
T}
.TE
.P
The argument
.IR pattern
is a pointer to a pathname pattern to be expanded. The
\fIglob\fR()
function shall match all accessible pathnames against this pattern and
develop a list of all pathnames that match. In order to have access to
a pathname,
\fIglob\fR()
requires search permission on every component of a path except the
last, and read permission on each directory of any filename component
of
.IR pattern
that contains any of the following special characters:
.BR '*' ,
.BR '?' ,
and
.BR '[' .
.P
The
\fIglob\fR()
function shall store the number of matched pathnames into
\fIpglob\fP\->\fIgl_pathc\fP and a pointer to a list of pointers to
pathnames into \fIpglob\fP\->\fIgl_pathv\fP. The pathnames shall be in
sort order as defined by the current setting of the
.IR LC_COLLATE
category; see the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 7.3.2" ", " "LC_COLLATE".
The first pointer after the last pathname shall be a null pointer. If
the pattern does not match any pathnames, the returned number of matched
paths is set to 0, and the contents of \fIpglob\fP\->\fIgl_pathv\fP
are implementation-defined.
.P
It is the caller's responsibility to create the structure pointed to by
.IR pglob .
The
\fIglob\fR()
function shall allocate other space as needed, including the memory
pointed to by
.IR gl_pathv .
The
\fIglobfree\fR()
function shall free any space associated with
.IR pglob
from a previous call to
\fIglob\fR().
.P
The
.IR flags
argument is used to control the behavior of
\fIglob\fR().
The value of
.IR flags
is a bitwise-inclusive OR of zero or more of the following
constants, which are defined in
.IR <glob.h> :
.IP GLOB_APPEND 14
Append pathnames generated to the ones from a previous call to
\fIglob\fR().
.IP GLOB_DOOFFS 14
Make use of \fIpglob\fP\->\fIgl_offs\fP. If this flag is set,
\fIpglob\fP\->\fIgl_offs\fP is used to specify how many null pointers
to add to the beginning of \fIpglob\fP\->\fIgl_pathv\fP. In other
words, \fIpglob\fP\->\fIgl_pathv\fP shall point to
\fIpglob\fP\->\fIgl_offs\fP null pointers, followed by
\fIpglob\fP\->\fIgl_pathc\fP pathname pointers, followed by a null
pointer.
.IP GLOB_ERR 14
Cause
\fIglob\fR()
to return when it encounters a directory that it cannot open or read.
Ordinarily,
\fIglob\fR()
continues to find matches.
.IP GLOB_MARK 14
Each pathname that is a directory that matches
.IR pattern
shall have a
<slash>
appended.
.IP GLOB_NOCHECK 14
Supports rule 3 in the Shell and Utilities volume of POSIX.1\(hy2017,
.IR "Section 2.13.3" ", " "Patterns Used for Filename Expansion".
If
.IR pattern
does not match any pathname, then
\fIglob\fR()
shall return a list consisting of only
.IR pattern ,
and the number of matched pathnames is 1.
.IP GLOB_NOESCAPE 14
Disable backslash escaping.
.IP GLOB_NOSORT 14
Ordinarily,
\fIglob\fR()
sorts the matching pathnames according to the current setting of the
.IR LC_COLLATE
category; see the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 7.3.2" ", " "LC_COLLATE".
When this flag is used, the order of pathnames returned is unspecified.
.P
The GLOB_APPEND
flag can be used to append a new set of pathnames to those found in a
previous call to
\fIglob\fR().
The following rules apply to applications when two or more calls to
\fIglob\fR()
are made with the same value of
.IR pglob
and without intervening calls to
\fIglobfree\fR():
.IP " 1." 4
The first such call shall not set GLOB_APPEND. All subsequent calls
shall set it.
.IP " 2." 4
All the calls shall set GLOB_DOOFFS, or all shall not set it.
.IP " 3." 4
After the second call, \fIpglob\fP\->\fIgl_pathv\fP points to a list
containing the following:
.RS 4 
.IP " a." 4
Zero or more null pointers, as specified by GLOB_DOOFFS and
\fIpglob\fP\->\fIgl_offs\fP.
.IP " b." 4
Pointers to the pathnames that were in the
\fIpglob\fP\->\fIgl_pathv\fP list before the call, in the same order
as before.
.IP " c." 4
Pointers to the new pathnames generated by the second call, in the
specified order.
.RE
.IP " 4." 4
The count returned in \fIpglob\fP\->\fIgl_pathc\fP shall be the total
number of pathnames from the two calls.
.IP " 5." 4
The application can change any of the fields after a call to
\fIglob\fR().
If it does, the application shall reset them to the original value
before a subsequent call, using the same
.IR pglob
value, to
\fIglobfree\fR()
or
\fIglob\fR()
with the GLOB_APPEND flag.
.P
If, during the search, a directory is encountered that cannot be opened
or read and
.IR errfunc
is not a null pointer,
\fIglob\fR()
calls
\fI(\fR()*errfunc )
with two arguments:
.IP " 1." 4
The
.IR epath
argument is a pointer to the path that failed.
.IP " 2." 4
The
.IR eerrno
argument is the value of
.IR errno
from the failure, as set by
\fIopendir\fR(),
\fIreaddir\fR(),
or
\fIstat\fR().
(Other values may be used to report other errors not explicitly
documented for those functions.)
.P
If
\fI(\fR()*errfunc )
is called and returns non-zero, or if the GLOB_ERR flag is set in
.IR flags ,
\fIglob\fR()
shall stop the scan and return GLOB_ABORTED after setting
.IR gl_pathc
and
.IR gl_pathv
in
.IR pglob
to reflect the paths already scanned. If GLOB_ERR is not set and
either
.IR errfunc
is a null pointer or
\fI(\fR()*errfunc )
returns 0, the error shall be ignored.
.P
The
\fIglob\fR()
function shall not fail because of large files.
.SH "RETURN VALUE"
Upon successful completion,
\fIglob\fR()
shall return 0. The argument \fIpglob\fP\->\fIgl_pathc\fP shall
return the number of matched pathnames and the argument
\fIpglob\fP\->\fIgl_pathv\fP shall contain a pointer to a
null-terminated list of matched and sorted pathnames. However, if
\fIpglob\fP\->\fIgl_pathc\fP is 0, the content of
\fIpglob\fP\->\fIgl_pathv\fP is undefined.
.P
The
\fIglobfree\fR()
function shall not return a value.
.P
If
\fIglob\fR()
terminates due to an error, it shall return one of the non-zero
constants defined in
.IR <glob.h> .
The arguments \fIpglob\fP\->\fIgl_pathc\fP and
\fIpglob\fP\->\fIgl_pathv\fP are still set as defined above.
.SH ERRORS
The
\fIglob\fR()
function shall fail and return the corresponding value if:
.IP GLOB_ABORTED 14
The scan was stopped because GLOB_ERR was set or
\fI(\fR()*errfunc )
returned non-zero.
.IP GLOB_NOMATCH 14
The pattern does not match any existing pathname, and GLOB_NOCHECK was
not set in flags.
.IP GLOB_NOSPACE 14
An attempt to allocate memory failed.
.LP
.IR "The following sections are informative."
.SH "EXAMPLES"
One use of the GLOB_DOOFFS flag is by applications that
build an argument list for use with
\fIexecv\fR(),
\fIexecve\fR(),
or
\fIexecvp\fR().
Suppose, for example, that an application wants to do the equivalent of:
.sp
.RS 4
.nf

ls -l *.c
.fi
.P
.RE
.P
but for some reason:
.sp
.RS 4
.nf

system("ls -l *.c")
.fi
.P
.RE
.P
is not acceptable. The application could obtain approximately the same
result using the sequence:
.sp
.RS 4
.nf

globbuf.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
globbuf.gl_pathv[0] = "ls";
globbuf.gl_pathv[1] = "-l";
execvp("ls", &globbuf.gl_pathv[0]);
.fi
.P
.RE
.P
Using the same example:
.sp
.RS 4
.nf

ls -l *.c *.h
.fi
.P
.RE
.P
could be approximately simulated using GLOB_APPEND as follows:
.sp
.RS 4
.nf

globbuf.gl_offs = 2;
glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
glob("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf);
\&...
.fi
.P
.RE
.SH "APPLICATION USAGE"
This function is not provided for the purpose of enabling utilities to
perform pathname expansion on their arguments, as this operation is
performed by the shell, and utilities are explicitly not expected to
redo this. Instead, it is provided for applications that need to do
pathname expansion on strings obtained from other sources, such as a
pattern typed by a user or read from a file.
.P
If a utility needs to see if a pathname matches a given pattern, it can
use
\fIfnmatch\fR().
.P
Note that
.IR gl_pathc
and
.IR gl_pathv
have meaning even if
\fIglob\fR()
fails. This allows
\fIglob\fR()
to report partial results in the event of an error. However, if
.IR gl_pathc
is 0,
.IR gl_pathv
is unspecified even if
\fIglob\fR()
did not return an error.
.P
The GLOB_NOCHECK option could be used when an application wants to
expand a pathname if wildcards are specified, but wants to treat the
pattern as just a string otherwise. The
.IR sh
utility might use this for option-arguments, for example.
.P
The new pathnames generated by a subsequent call with GLOB_APPEND are
not sorted together with the previous pathnames. This mirrors the way
that the shell handles pathname expansion when multiple expansions are
done on a command line.
.P
Applications that need tilde and parameter expansion should use
\fIwordexp\fR().
.SH RATIONALE
It was claimed that the GLOB_DOOFFS flag is unnecessary because it
could be simulated using:
.sp
.RS 4
.nf

new = (char **)malloc((n + pglob->gl_pathc + 1)
       * sizeof(char *));
(void) memcpy(new+n, pglob->gl_pathv,
       pglob->gl_pathc * sizeof(char *));
(void) memset(new, 0, n * sizeof(char *));
free(pglob->gl_pathv);
pglob->gl_pathv = new;
.fi
.P
.RE
.P
However, this assumes that the memory pointed to by
.IR gl_pathv
is a block that was separately created using
\fImalloc\fR().
This is not necessarily the case. An application should make no
assumptions about how the memory referenced by fields in
.IR pglob
was allocated. It might have been obtained from
\fImalloc\fR()
in a large chunk and then carved up within
\fIglob\fR(),
or it might have been created using a different memory allocator. It
is not the intent of the standard developers to specify or imply how
the memory used by
\fIglob\fR()
is managed.
.P
The GLOB_APPEND flag would be used when an application wants to expand
several different patterns into a single list.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIexec\fR\^",
.IR "\fIfdopendir\fR\^(\|)",
.IR "\fIfnmatch\fR\^(\|)",
.IR "\fIfstatat\fR\^(\|)",
.IR "\fIreaddir\fR\^(\|)",
.IR "Section 2.6" ", " "Word Expansions"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 7.3.2" ", " "LC_COLLATE",
.IR "\fB<glob.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .