]>
Commit | Line | Data |
---|---|---|
bf5a7247 | 1 | .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) |
fea681da | 2 | .\" |
5fbde956 | 3 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
c08df37a | 4 | .\" |
fea681da MK |
5 | .\" Modified Wed Jul 28 11:12:17 1993 by Rik Faith (faith@cs.unc.edu) |
6 | .\" Modified Mon May 13 23:08:50 1996 by Martin Schulze (joey@linux.de) | |
7 | .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk) | |
8 | .\" Modified 990912 by aeb | |
7c2cb022 MK |
9 | .\" 2007-10-10 mtk |
10 | .\" Added description of GLOB_TILDE_NOMATCH | |
11 | .\" Expanded the description of various flags | |
12 | .\" Various wording fixes. | |
fea681da | 13 | .\" |
ab47278f | 14 | .TH GLOB 3 (date) "Linux man-pages (unreleased)" |
fea681da MK |
15 | .SH NAME |
16 | glob, globfree \- find pathnames matching a pattern, free memory from glob() | |
42009080 AC |
17 | .SH LIBRARY |
18 | Standard C library | |
19 | .RI ( libc ", " \-lc ) | |
fea681da MK |
20 | .SH SYNOPSIS |
21 | .nf | |
22 | .B #include <glob.h> | |
68e4db0a | 23 | .PP |
86ce8583 | 24 | .BI "int glob(const char *restrict " pattern ", int " flags , |
511bb71b | 25 | .BI " int (*" errfunc ")(const char *" epath ", int " eerrno ), |
86ce8583 | 26 | .BI " glob_t *restrict " pglob ); |
fea681da MK |
27 | .BI "void globfree(glob_t *" pglob ); |
28 | .fi | |
29 | .SH DESCRIPTION | |
30 | The | |
63aa9df0 | 31 | .BR glob () |
fea681da MK |
32 | function searches for all the pathnames matching |
33 | .I pattern | |
34 | according to the rules used by the shell (see | |
35 | .BR glob (7)). | |
36 | No tilde expansion or parameter substitution is done; if you want | |
37 | these, use | |
38 | .BR wordexp (3). | |
39 | .PP | |
40 | The | |
63aa9df0 | 41 | .BR globfree () |
fea681da MK |
42 | function frees the dynamically allocated storage from an earlier call |
43 | to | |
63aa9df0 | 44 | .BR glob (). |
fea681da MK |
45 | .PP |
46 | The results of a | |
63aa9df0 | 47 | .BR glob () |
fea681da | 48 | call are stored in the structure pointed to by |
7c2cb022 MK |
49 | .IR pglob . |
50 | This structure is of type | |
f19a0f03 | 51 | .I glob_t |
7c2cb022 MK |
52 | (declared in |
53 | .IR <glob.h> ) | |
fea681da MK |
54 | and includes the following elements defined by POSIX.2 (more may be |
55 | present as an extension): | |
56 | .PP | |
088a639b | 57 | .in +4n |
b8302363 | 58 | .EX |
b9f02710 MK |
59 | typedef struct { |
60 | size_t gl_pathc; /* Count of paths matched so far */ | |
61 | char **gl_pathv; /* List of matched pathnames. */ | |
a40ee90b | 62 | size_t gl_offs; /* Slots to reserve in \fIgl_pathv\fP. */ |
fea681da | 63 | } glob_t; |
b8302363 | 64 | .EE |
088a639b | 65 | .in |
fea681da MK |
66 | .PP |
67 | Results are stored in dynamically allocated storage. | |
68 | .PP | |
c4bb193f | 69 | The argument |
fea681da | 70 | .I flags |
7c2cb022 MK |
71 | is made up of the bitwise OR of zero or more the following symbolic |
72 | constants, which modify the behavior of | |
63aa9df0 | 73 | .BR glob (): |
fea681da MK |
74 | .TP |
75 | .B GLOB_ERR | |
7c2cb022 MK |
76 | Return upon a read error (because a directory does not |
77 | have read permission, for example). | |
78 | By default, | |
79 | .BR glob () | |
38f76cd2 | 80 | attempts carry on despite errors, |
7c2cb022 | 81 | reading all of the directories that it can. |
fea681da MK |
82 | .TP |
83 | .B GLOB_MARK | |
7c2cb022 | 84 | Append a slash to each path which corresponds to a directory. |
fea681da MK |
85 | .TP |
86 | .B GLOB_NOSORT | |
7c2cb022 MK |
87 | Don't sort the returned pathnames. |
88 | The only reason to do this is to save processing time. | |
89 | By default, the returned pathnames are sorted. | |
fea681da MK |
90 | .TP |
91 | .B GLOB_DOOFFS | |
7c2cb022 | 92 | Reserve |
94e9d9fe | 93 | .I pglob\->gl_offs |
7c2cb022 | 94 | slots at the beginning of the list of strings in |
94e9d9fe | 95 | .IR pglob\->pathv . |
b437fdd9 | 96 | The reserved slots contain null pointers. |
fea681da MK |
97 | .TP |
98 | .B GLOB_NOCHECK | |
7c2cb022 MK |
99 | If no pattern matches, return the original pattern. |
100 | By default, | |
101 | .BR glob () | |
102 | returns | |
103 | .B GLOB_NOMATCH | |
104 | if there are no matches. | |
fea681da MK |
105 | .TP |
106 | .B GLOB_APPEND | |
7c2cb022 MK |
107 | Append the results of this call to the vector of results |
108 | returned by a previous call to | |
109 | .BR glob (). | |
c13182ef | 110 | Do not set this flag on the first invocation of |
63aa9df0 | 111 | .BR glob (). |
fea681da MK |
112 | .TP |
113 | .B GLOB_NOESCAPE | |
d1a71985 | 114 | Don't allow backslash (\(aq\e\(aq) to be used as an escape |
7c2cb022 MK |
115 | character. |
116 | Normally, a backslash can be used to quote the following character, | |
117 | providing a mechanism to turn off the special meaning | |
118 | metacharacters. | |
fea681da | 119 | .PP |
7c2cb022 MK |
120 | .I flags |
121 | may also include any of the following, which are GNU | |
fea681da MK |
122 | extensions and not defined by POSIX.2: |
123 | .TP | |
124 | .B GLOB_PERIOD | |
7c2cb022 MK |
125 | Allow a leading period to be matched by metacharacters. |
126 | By default, metacharacters can't match a leading period. | |
fea681da MK |
127 | .TP |
128 | .B GLOB_ALTDIRFUNC | |
7c2cb022 | 129 | Use alternative functions |
94e9d9fe MK |
130 | .IR pglob\->gl_closedir , |
131 | .IR pglob\->gl_readdir , | |
132 | .IR pglob\->gl_opendir , | |
06e88ab8 | 133 | .IR pglob\->gl_lstat , |
134 | and | |
94e9d9fe | 135 | .I pglob\->gl_stat |
9ee4a2b6 | 136 | for filesystem access instead of the normal library |
7c2cb022 | 137 | functions. |
fea681da MK |
138 | .TP |
139 | .B GLOB_BRACE | |
7c2cb022 | 140 | Expand |
fea681da | 141 | .BR csh (1) |
c9942389 MK |
142 | style brace expressions of the form |
143 | .BR {a,b} . | |
7c2cb022 MK |
144 | Brace expressions can be nested. |
145 | Thus, for example, specifying the pattern | |
146 | "{foo/{,cat,dog},bar}" would return the same results as four separate | |
147 | .BR glob () | |
148 | calls using the strings: | |
149 | "foo/", | |
150 | "foo/cat", | |
151 | "foo/dog", | |
152 | and | |
153 | "bar". | |
fea681da MK |
154 | .TP |
155 | .B GLOB_NOMAGIC | |
6259c9b7 | 156 | If the pattern contains no metacharacters, |
7c2cb022 MK |
157 | then it should be returned as the sole matching word, |
158 | even if there is no file with that name. | |
fea681da MK |
159 | .TP |
160 | .B GLOB_TILDE | |
7c2cb022 | 161 | Carry out tilde expansion. |
af2d18b2 | 162 | If a tilde (\(aq\(ti\(aq) is the only character in the pattern, |
f81fb444 | 163 | or an initial tilde is followed immediately by a slash (\(aq/\(aq), |
7c2cb022 MK |
164 | then the home directory of the caller is substituted for |
165 | the tilde. | |
af2d18b2 | 166 | If an initial tilde is followed by a username (e.g., "\(tiandrea/bin"), |
7c2cb022 MK |
167 | then the tilde and username are substituted by the home directory |
168 | of that user. | |
169 | If the username is invalid, or the home directory cannot be | |
170 | determined, then no substitution is performed. | |
171 | .TP | |
172 | .B GLOB_TILDE_CHECK | |
173 | This provides behavior similar to that of | |
174 | .BR GLOB_TILDE . | |
175 | The difference is that if the username is invalid, or the | |
176 | home directory cannot be determined, then | |
177 | instead of using the pattern itself as the name, | |
178 | .BR glob () | |
179 | returns | |
1ae6b2c7 | 180 | .B GLOB_NOMATCH |
7c2cb022 | 181 | to indicate an error. |
fea681da MK |
182 | .TP |
183 | .B GLOB_ONLYDIR | |
38f76cd2 | 184 | This is a |
7c2cb022 MK |
185 | .I hint |
186 | to | |
187 | .BR glob () | |
188 | that the caller is interested only in directories that match the pattern. | |
189 | If the implementation can easily determine file-type information, | |
24b74457 | 190 | then nondirectory files are not returned to the caller. |
7c2cb022 MK |
191 | However, the caller must still check that returned files |
192 | are directories. | |
193 | (The purpose of this flag is merely to optimize performance when | |
ed145e47 | 194 | the caller is interested only in directories.) |
fea681da MK |
195 | .PP |
196 | If | |
197 | .I errfunc | |
8478ee02 | 198 | is not NULL, |
fea681da MK |
199 | it will be called in case of an error with the arguments |
200 | .IR epath , | |
201 | a pointer to the path which failed, and | |
202 | .IR eerrno , | |
203 | the value of | |
204 | .I errno | |
205 | as returned from one of the calls to | |
91fe9d5c MK |
206 | .BR opendir (3), |
207 | .BR readdir (3), | |
c13182ef | 208 | or |
91fe9d5c | 209 | .BR stat (2). |
c13182ef | 210 | If |
fea681da | 211 | .I errfunc |
c7094399 | 212 | returns nonzero, or if |
fea681da | 213 | .B GLOB_ERR |
c13182ef | 214 | is set, |
63aa9df0 | 215 | .BR glob () |
fea681da MK |
216 | will terminate after the call to |
217 | .IR errfunc . | |
218 | .PP | |
c13182ef | 219 | Upon successful return, |
94e9d9fe | 220 | .I pglob\->gl_pathc |
fea681da | 221 | contains the number of matched pathnames and |
94e9d9fe | 222 | .I pglob\->gl_pathv |
7c2cb022 | 223 | contains a pointer to the list of pointers to matched pathnames. |
b437fdd9 | 224 | The list of pointers is terminated by a null pointer. |
fea681da MK |
225 | .PP |
226 | It is possible to call | |
63aa9df0 | 227 | .BR glob () |
c13182ef MK |
228 | several times. |
229 | In that case, the | |
fea681da MK |
230 | .B GLOB_APPEND |
231 | flag has to be set in | |
232 | .I flags | |
233 | on the second and later invocations. | |
234 | .PP | |
235 | As a GNU extension, | |
94e9d9fe | 236 | .I pglob\->gl_flags |
c9942389 MK |
237 | is set to the flags specified, |
238 | .BR or ed | |
239 | with | |
fea681da MK |
240 | .B GLOB_MAGCHAR |
241 | if any metacharacters were found. | |
47297adb | 242 | .SH RETURN VALUE |
c13182ef | 243 | On successful completion, |
63aa9df0 | 244 | .BR glob () |
fea681da MK |
245 | returns zero. |
246 | Other possible returns are: | |
247 | .TP | |
248 | .B GLOB_NOSPACE | |
249 | for running out of memory, | |
250 | .TP | |
251 | .B GLOB_ABORTED | |
252 | for a read error, and | |
253 | .TP | |
254 | .B GLOB_NOMATCH | |
255 | for no found matches. | |
624e7579 ZL |
256 | .SH ATTRIBUTES |
257 | For an explanation of the terms used in this section, see | |
258 | .BR attributes (7). | |
c466875e MK |
259 | .ad l |
260 | .nh | |
624e7579 ZL |
261 | .TS |
262 | allbox; | |
c466875e | 263 | lb lb lbx |
624e7579 ZL |
264 | l l l. |
265 | Interface Attribute Value | |
266 | T{ | |
267 | .BR glob () | |
268 | T} Thread safety T{ | |
269 | MT-Unsafe race:utent env | |
624e7579 ZL |
270 | sig:ALRM timer locale |
271 | T} | |
272 | T{ | |
273 | .BR globfree () | |
274 | T} Thread safety MT-Safe | |
275 | .TE | |
c466875e MK |
276 | .hy |
277 | .ad | |
847e0d88 | 278 | .sp 1 |
624e7579 ZL |
279 | In the above table, |
280 | .I utent | |
281 | in | |
282 | .I race:utent | |
283 | signifies that if any of the functions | |
284 | .BR setutent (3), | |
285 | .BR getutent (3), | |
286 | or | |
287 | .BR endutent (3) | |
288 | are used in parallel in different threads of a program, | |
289 | then data races could occur. | |
bf7bc8b8 | 290 | .BR glob () |
624e7579 ZL |
291 | calls those functions, |
292 | so we use race:utent to remind users. | |
3113c7f3 | 293 | .SH STANDARDS |
ad6e6e50 | 294 | POSIX.1-2001, POSIX.1-2008, POSIX.2. |
2b2581ee MK |
295 | .SH NOTES |
296 | The structure elements | |
297 | .I gl_pathc | |
298 | and | |
299 | .I gl_offs | |
300 | are declared as | |
0daa9e92 | 301 | .I size_t |
7c2cb022 | 302 | in glibc 2.1, as they should be according to POSIX.2, |
2b2581ee MK |
303 | but are declared as |
304 | .I int | |
f51d8d4d | 305 | in glibc 2.0. |
2b2581ee MK |
306 | .SH BUGS |
307 | The | |
308 | .BR glob () | |
309 | function may fail due to failure of underlying function calls, such as | |
310 | .BR malloc (3) | |
311 | or | |
312 | .BR opendir (3). | |
313 | These will store their error code in | |
314 | .IR errno . | |
a14af333 | 315 | .SH EXAMPLES |
fea681da | 316 | One example of use is the following code, which simulates typing |
51f5698d | 317 | .PP |
296d592b | 318 | .in +4n |
bdd915e2 | 319 | .EX |
296d592b | 320 | ls \-l *.c ../*.c |
bdd915e2 | 321 | .EE |
296d592b | 322 | .in |
51f5698d | 323 | .PP |
defcceb3 | 324 | in the shell: |
e646a1ba | 325 | .PP |
296d592b | 326 | .in +4n |
e646a1ba | 327 | .EX |
fea681da MK |
328 | glob_t globbuf; |
329 | ||
330 | globbuf.gl_offs = 2; | |
331 | glob("*.c", GLOB_DOOFFS, NULL, &globbuf); | |
332 | glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf); | |
333 | globbuf.gl_pathv[0] = "ls"; | |
8c383102 | 334 | globbuf.gl_pathv[1] = "\-l"; |
fea681da | 335 | execvp("ls", &globbuf.gl_pathv[0]); |
b8302363 | 336 | .EE |
e646a1ba | 337 | .in |
47297adb | 338 | .SH SEE ALSO |
fea681da MK |
339 | .BR ls (1), |
340 | .BR sh (1), | |
341 | .BR stat (2), | |
342 | .BR exec (3), | |
2ff9c803 | 343 | .BR fnmatch (3), |
fea681da MK |
344 | .BR malloc (3), |
345 | .BR opendir (3), | |
346 | .BR readdir (3), | |
347 | .BR wordexp (3), | |
348 | .BR glob (7) |