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