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