1
by Francesco Paolo Lovergine
rules: linking manpages-posix not manpages. |
1 |
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved |
2
by Francesco Paolo Lovergine
* Alligned to linux main manpages edition. |
2 |
.TH "GLOB" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual" |
1
by Francesco Paolo Lovergine
rules: linking manpages-posix not manpages. |
3 |
.\" glob |
4 |
.SH NAME |
|
5 |
glob, globfree \- generate pathnames matching a pattern
|
|
6 |
.SH SYNOPSIS |
|
7 |
.LP
|
|
8 |
\fB#include <glob.h>
|
|
9 |
.br
|
|
10 |
.sp
|
|
11 |
int glob(const char *restrict\fP \fIpattern\fP\fB, int\fP \fIflags\fP\fB, |
|
12 |
.br
|
|
13 |
\ \ \ \ \ \ int(*\fP\fIerrfunc\fP\fB)(const char *\fP\fIepath\fP\fB, |
|
14 |
int\fP
|
|
15 |
\fIeerrno\fP\fB), |
|
16 |
.br
|
|
17 |
\ \ \ \ \ \ glob_t *restrict\fP \fIpglob\fP\fB); |
|
18 |
.br
|
|
19 |
void globfree(glob_t *\fP\fIpglob\fP\fB); |
|
20 |
.br
|
|
21 |
\fP
|
|
22 |
.SH DESCRIPTION |
|
23 |
.LP
|
|
24 |
The \fIglob\fP() function is a pathname generator that shall implement |
|
25 |
the rules defined in the Shell and Utilities volume of |
|
26 |
IEEE\ Std\ 1003.1-2001, Section 2.13, Pattern Matching Notation, with |
|
27 |
optional support for rule 3 in the Shell and Utilities volume of IEEE\ Std\ 1003.1-2001, |
|
28 |
Section 2.13.3, Patterns Used for Filename Expansion. |
|
29 |
.LP
|
|
30 |
The structure type \fBglob_t\fP is defined in \fI<glob.h>\fP and includes |
|
31 |
at least |
|
32 |
the following members: |
|
33 |
.TS C |
|
34 |
center; l1 l1 l. |
|
35 |
\fBMember Type\fP \fBMember Name\fP \fBDescription\fP |
|
36 |
\fBsize_t\fP \fIgl_pathc\fP Count of paths matched by \fIpattern\fP. |
|
37 |
\fBchar **\fP \fIgl_pathv\fP Pointer to a list of matched pathnames. |
|
38 |
\fBsize_t\fP \fIgl_offs\fP Slots to reserve at the beginning of \fIgl_pathv\fP. |
|
39 |
.TE
|
|
40 |
.LP
|
|
41 |
The argument \fIpattern\fP is a pointer to a pathname pattern to be |
|
42 |
expanded. The \fIglob\fP() function shall match all |
|
43 |
accessible pathnames against this pattern and develop a list of all |
|
44 |
pathnames that match. In order to have access to a pathname, |
|
45 |
\fIglob\fP() requires search permission on every component of a path |
|
46 |
except the last, and read permission on each directory of any |
|
47 |
filename component of \fIpattern\fP that contains any of the following |
|
48 |
special characters: \fB'*'\fP , \fB'?'\fP , and |
|
49 |
\fB'['\fP . |
|
50 |
.LP
|
|
51 |
The \fIglob\fP() function shall store the number of matched pathnames |
|
52 |
into \fIpglob\fP->\fIgl_pathc\fP and a pointer to a |
|
53 |
list of pointers to pathnames into \fIpglob\fP->\fIgl_pathv\fP. The |
|
54 |
pathnames shall be in sort order as defined by the current |
|
55 |
setting of the \fILC_COLLATE\fP category; see the Base Definitions |
|
56 |
volume of IEEE\ Std\ 1003.1-2001, Section 7.3.2, LC_COLLATE. The first |
|
57 |
pointer after the last pathname shall be a null |
|
58 |
pointer. If the pattern does not match any pathnames, the returned |
|
59 |
number of matched paths is set to 0, and the contents of |
|
60 |
\fIpglob\fP->\fIgl_pathv\fP are implementation-defined. |
|
61 |
.LP
|
|
62 |
It is the caller's responsibility to create the structure pointed |
|
63 |
to by \fIpglob\fP. The \fIglob\fP() function shall allocate |
|
64 |
other space as needed, including the memory pointed to by \fIgl_pathv\fP. |
|
65 |
The \fIglobfree\fP() function shall free any space |
|
66 |
associated with \fIpglob\fP from a previous call to \fIglob\fP(). |
|
67 |
.LP
|
|
68 |
The \fIflags\fP argument is used to control the behavior of \fIglob\fP(). |
|
69 |
The value of \fIflags\fP is a bitwise-inclusive OR |
|
70 |
of zero or more of the following constants, which are defined in \fI<glob.h>\fP: |
|
71 |
.TP 7 |
|
72 |
GLOB_APPEND |
|
73 |
Append pathnames generated to the ones from a previous call to \fIglob\fP(). |
|
74 |
.TP 7 |
|
75 |
GLOB_DOOFFS |
|
76 |
Make use of \fIpglob\fP->\fIgl_offs\fP. If this flag is set, \fIpglob\fP->\fIgl_offs\fP |
|
77 |
is used to specify how many |
|
78 |
null pointers to add to the beginning of \fIpglob\fP->\fIgl_pathv\fP. |
|
79 |
In other words, \fIpglob\fP->\fIgl_pathv\fP shall |
|
80 |
point to \fIpglob\fP->\fIgl_offs\fP null pointers, followed by \fIpglob\fP->\fIgl_pathc\fP |
|
81 |
pathname pointers, followed by |
|
82 |
a null pointer. |
|
83 |
.TP 7 |
|
84 |
GLOB_ERR |
|
85 |
Cause \fIglob\fP() to return when it encounters a directory that it |
|
86 |
cannot open or read. Ordinarily, \fIglob\fP() continues |
|
87 |
to find matches. |
|
88 |
.TP 7 |
|
89 |
GLOB_MARK |
|
90 |
Each pathname that is a directory that matches \fIpattern\fP shall |
|
91 |
have a slash appended. |
|
92 |
.TP 7 |
|
93 |
GLOB_NOCHECK |
|
94 |
Supports rule 3 in the Shell and Utilities volume of IEEE\ Std\ 1003.1-2001, |
|
95 |
Section 2.13.3, Patterns Used for Filename Expansion. If \fIpattern\fP |
|
96 |
does not |
|
97 |
match any pathname, then \fIglob\fP() shall return a list consisting |
|
98 |
of only \fIpattern\fP, and the number of matched pathnames |
|
99 |
is 1. |
|
100 |
.TP 7 |
|
101 |
GLOB_NOESCAPE |
|
102 |
Disable backslash escaping. |
|
103 |
.TP 7 |
|
104 |
GLOB_NOSORT |
|
105 |
Ordinarily, \fIglob\fP() sorts the matching pathnames according to |
|
106 |
the current setting of the \fILC_COLLATE\fP category; see |
|
107 |
the Base Definitions volume of IEEE\ Std\ 1003.1-2001, Section 7.3.2, |
|
108 |
LC_COLLATE. When this flag is used, the order of pathnames returned |
|
109 |
is unspecified. |
|
110 |
.sp
|
|
111 |
.LP
|
|
112 |
The GLOB_APPEND flag can be used to append a new set of pathnames |
|
113 |
to those found in a previous call to \fIglob\fP(). The |
|
114 |
following rules apply to applications when two or more calls to \fIglob\fP() |
|
115 |
are made with the same value of \fIpglob\fP and |
|
116 |
without intervening calls to \fIglobfree\fP(): |
|
117 |
.IP " 1." 4 |
|
118 |
The first such call shall not set GLOB_APPEND. All subsequent calls |
|
119 |
shall set it. |
|
120 |
.LP
|
|
121 |
.IP " 2." 4 |
|
122 |
All the calls shall set GLOB_DOOFFS, or all shall not set it. |
|
123 |
.LP
|
|
124 |
.IP " 3." 4 |
|
125 |
After the second call, \fIpglob\fP->\fIgl_pathv\fP points to a list |
|
126 |
containing the following: |
|
127 |
.RS
|
|
128 |
.IP " a." 4 |
|
129 |
Zero or more null pointers, as specified by GLOB_DOOFFS and \fIpglob\fP->\fIgl_offs\fP. |
|
130 |
.LP
|
|
131 |
.IP " b." 4 |
|
132 |
Pointers to the pathnames that were in the \fIpglob\fP->\fIgl_pathv\fP |
|
133 |
list before the call, in the same order as |
|
134 |
before. |
|
135 |
.LP
|
|
136 |
.IP " c." 4 |
|
137 |
Pointers to the new pathnames generated by the second call, in the |
|
138 |
specified order. |
|
139 |
.LP
|
|
140 |
.RE
|
|
141 |
.LP
|
|
142 |
.IP " 4." 4 |
|
143 |
The count returned in \fIpglob\fP->\fIgl_pathc\fP shall be the total |
|
144 |
number of pathnames from the two calls. |
|
145 |
.LP
|
|
146 |
.IP " 5." 4 |
|
147 |
The application can change any of the fields after a call to \fIglob\fP(). |
|
148 |
If it does, the application shall reset them to the |
|
149 |
original value before a subsequent call, using the same \fIpglob\fP |
|
150 |
value, to \fIglobfree\fP() or \fIglob\fP() with the |
|
151 |
GLOB_APPEND flag. |
|
152 |
.LP
|
|
153 |
.LP
|
|
154 |
If, during the search, a directory is encountered that cannot be opened |
|
155 |
or read and \fIerrfunc\fP is not a null pointer, |
|
156 |
\fIglob\fP() calls (\fI*errfunc\fP()) with two arguments: |
|
157 |
.IP " 1." 4 |
|
158 |
The \fIepath\fP argument is a pointer to the path that failed. |
|
159 |
.LP
|
|
160 |
.IP " 2." 4 |
|
161 |
The \fIeerrno\fP argument is the value of \fIerrno\fP from the failure, |
|
162 |
as set by \fIopendir\fP(), \fIreaddir\fP(), or \fIstat\fP(). (Other |
|
163 |
values may be used to report other errors not explicitly documented |
|
164 |
for those |
|
165 |
functions.) |
|
166 |
.LP
|
|
167 |
.LP
|
|
168 |
If (\fI*errfunc\fP()) is called and returns non-zero, or if the GLOB_ERR |
|
169 |
flag is set in \fIflags\fP, \fIglob\fP() shall stop |
|
170 |
the scan and return GLOB_ABORTED after setting \fIgl_pathc\fP and |
|
171 |
\fIgl_pathv\fP in \fIpglob\fP to reflect the paths already |
|
172 |
scanned. If GLOB_ERR is not set and either \fIerrfunc\fP is a null |
|
173 |
pointer or (\fI*errfunc\fP()) returns 0, the error shall be |
|
174 |
ignored. |
|
175 |
.LP
|
|
176 |
The \fIglob\fP() function shall not fail because of large files. |
|
177 |
.SH RETURN VALUE |
|
178 |
.LP
|
|
179 |
Upon successful completion, \fIglob\fP() shall return 0. The argument |
|
180 |
\fIpglob\fP->\fIgl_pathc\fP shall return the number |
|
181 |
of matched pathnames and the argument \fIpglob\fP->\fIgl_pathv\fP |
|
182 |
shall contain a pointer to a null-terminated list of matched |
|
183 |
and sorted pathnames. However, if \fIpglob\fP->\fIgl_pathc\fP is 0, |
|
184 |
the content of \fIpglob\fP->\fIgl_pathv\fP is |
|
185 |
undefined. |
|
186 |
.LP
|
|
187 |
The \fIglobfree\fP() function shall not return a value. |
|
188 |
.LP
|
|
189 |
If \fIglob\fP() terminates due to an error, it shall return one of |
|
190 |
the non-zero constants defined in \fI<glob.h>\fP. The arguments \fIpglob\fP->\fIgl_pathc\fP |
|
191 |
and |
|
192 |
\fIpglob\fP->\fIgl_pathv\fP are still set as defined above. |
|
193 |
.SH ERRORS |
|
194 |
.LP
|
|
195 |
The \fIglob\fP() function shall fail and return the corresponding |
|
196 |
value if: |
|
197 |
.TP 7 |
|
198 |
GLOB_ABORTED |
|
199 |
The scan was stopped because GLOB_ERR was set or (\fI*errfunc\fP()) |
|
200 |
returned non-zero. |
|
201 |
.TP 7 |
|
202 |
GLOB_NOMATCH |
|
203 |
The pattern does not match any existing pathname, and GLOB_NOCHECK |
|
204 |
was not set in flags. |
|
205 |
.TP 7 |
|
206 |
GLOB_NOSPACE |
|
207 |
An attempt to allocate memory failed. |
|
208 |
.sp
|
|
209 |
.LP
|
|
210 |
\fIThe following sections are informative.\fP |
|
211 |
.SH EXAMPLES |
|
212 |
.LP
|
|
213 |
One use of the GLOB_DOOFFS flag is by applications that build an argument |
|
214 |
list for use with \fIexecv\fP(), \fIexecve\fP(), or \fIexecvp\fP(). |
|
215 |
Suppose, for example, that an application wants to do the equivalent |
|
216 |
of: |
|
217 |
.sp
|
|
218 |
.RS
|
|
219 |
.nf
|
|
220 |
||
221 |
\fBls -l *.c
|
|
222 |
\fP
|
|
223 |
.fi
|
|
224 |
.RE
|
|
225 |
.LP
|
|
226 |
but for some reason: |
|
227 |
.sp
|
|
228 |
.RS
|
|
229 |
.nf
|
|
230 |
||
231 |
\fBsystem("ls -l *.c")
|
|
232 |
\fP
|
|
233 |
.fi
|
|
234 |
.RE
|
|
235 |
.LP
|
|
236 |
is not acceptable. The application could obtain approximately the |
|
237 |
same result using the sequence: |
|
238 |
.sp
|
|
239 |
.RS
|
|
240 |
.nf
|
|
241 |
||
242 |
\fBglobbuf.gl_offs = 2;
|
|
243 |
glob("*.c", GLOB_DOOFFS, NULL, &globbuf); |
|
244 |
globbuf.gl_pathv[0] = "ls"; |
|
245 |
globbuf.gl_pathv[1] = "-l"; |
|
246 |
execvp("ls", &globbuf.gl_pathv[0]); |
|
247 |
\fP
|
|
248 |
.fi
|
|
249 |
.RE
|
|
250 |
.LP
|
|
251 |
Using the same example: |
|
252 |
.sp
|
|
253 |
.RS
|
|
254 |
.nf
|
|
255 |
||
256 |
\fBls -l *.c *.h
|
|
257 |
\fP
|
|
258 |
.fi
|
|
259 |
.RE
|
|
260 |
.LP
|
|
261 |
could be approximately simulated using GLOB_APPEND as follows: |
|
262 |
.sp
|
|
263 |
.RS
|
|
264 |
.nf
|
|
265 |
||
266 |
\fBglobbuf.gl_offs = 2;
|
|
267 |
glob("*.c", GLOB_DOOFFS, NULL, &globbuf); |
|
268 |
glob("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf); |
|
269 |
\&...
|
|
270 |
\fP
|
|
271 |
.fi
|
|
272 |
.RE
|
|
273 |
.SH APPLICATION USAGE |
|
274 |
.LP
|
|
275 |
This function is not provided for the purpose of enabling utilities |
|
276 |
to perform pathname expansion on their arguments, as this |
|
277 |
operation is performed by the shell, and utilities are explicitly |
|
278 |
not expected to redo this. Instead, it is provided for |
|
279 |
applications that need to do pathname expansion on strings obtained |
|
280 |
from other sources, such as a pattern typed by a user or read |
|
281 |
from a file. |
|
282 |
.LP
|
|
283 |
If a utility needs to see if a pathname matches a given pattern, it |
|
284 |
can use \fIfnmatch\fP(). |
|
285 |
.LP
|
|
286 |
Note that \fIgl_pathc\fP and \fIgl_pathv\fP have meaning even if \fIglob\fP() |
|
287 |
fails. This allows \fIglob\fP() to report |
|
288 |
partial results in the event of an error. However, if \fIgl_pathc\fP |
|
289 |
is 0, \fIgl_pathv\fP is unspecified even if \fIglob\fP() |
|
290 |
did not return an error. |
|
291 |
.LP
|
|
292 |
The GLOB_NOCHECK option could be used when an application wants to |
|
293 |
expand a pathname if wildcards are specified, but wants to |
|
294 |
treat the pattern as just a string otherwise. The \fIsh\fP utility |
|
295 |
might use this for |
|
296 |
option-arguments, for example. |
|
297 |
.LP
|
|
298 |
The new pathnames generated by a subsequent call with GLOB_APPEND |
|
299 |
are not sorted together with the previous pathnames. This |
|
300 |
mirrors the way that the shell handles pathname expansion when multiple |
|
301 |
expansions are done on a command line. |
|
302 |
.LP
|
|
303 |
Applications that need tilde and parameter expansion should use \fIwordexp\fP(). |
|
304 |
.SH RATIONALE |
|
305 |
.LP
|
|
306 |
It was claimed that the GLOB_DOOFFS flag is unnecessary because it |
|
307 |
could be simulated using: |
|
308 |
.sp
|
|
309 |
.RS
|
|
310 |
.nf
|
|
311 |
||
312 |
\fBnew = (char **)malloc((n + pglob->gl_pathc + 1)
|
|
313 |
* sizeof(char *)); |
|
314 |
(void) memcpy(new+n, pglob->gl_pathv, |
|
315 |
pglob->gl_pathc * sizeof(char *)); |
|
316 |
(void) memset(new, 0, n * sizeof(char *)); |
|
317 |
free(pglob->gl_pathv); |
|
318 |
pglob->gl_pathv = new; |
|
319 |
\fP
|
|
320 |
.fi
|
|
321 |
.RE
|
|
322 |
.LP
|
|
323 |
However, this assumes that the memory pointed to by \fIgl_pathv\fP |
|
324 |
is a block that was separately created using \fImalloc\fP(). This |
|
325 |
is not necessarily the case. An application should make no assumptions |
|
326 |
about |
|
327 |
how the memory referenced by fields in \fIpglob\fP was allocated. |
|
328 |
It might have been obtained from \fImalloc\fP() in a large chunk and |
|
329 |
then carved up within \fIglob\fP(), or it might have been |
|
330 |
created using a different memory allocator. It is not the intent of |
|
331 |
the standard developers to specify or imply how the memory used |
|
332 |
by \fIglob\fP() is managed. |
|
333 |
.LP
|
|
334 |
The GLOB_APPEND flag would be used when an application wants to expand |
|
335 |
several different patterns into a single list. |
|
336 |
.SH FUTURE DIRECTIONS |
|
337 |
.LP
|
|
338 |
None. |
|
339 |
.SH SEE ALSO |
|
340 |
.LP
|
|
341 |
\fIexec\fP() , \fIfnmatch\fP() , \fIopendir\fP() , \fIreaddir\fP() |
|
342 |
, \fIstat\fP() , \fIwordexp\fP() , the Base Definitions volume of |
|
343 |
IEEE\ Std\ 1003.1-2001, \fI<glob.h>\fP, the Shell and Utilities volume |
|
344 |
of IEEE\ Std\ 1003.1-2001 |
|
345 |
.SH COPYRIGHT |
|
346 |
Portions of this text are reprinted and reproduced in electronic form |
|
347 |
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology |
|
348 |
-- Portable Operating System Interface (POSIX), The Open Group Base |
|
349 |
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of |
|
350 |
Electrical and Electronics Engineers, Inc and The Open Group. In the |
|
351 |
event of any discrepancy between this version and the original IEEE and |
|
352 |
The Open Group Standard, the original IEEE and The Open Group Standard |
|
353 |
is the referee document. The original Standard can be obtained online at |
|
354 |
http://www.opengroup.org/unix/online.html . |