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