]>
Commit | Line | Data |
---|---|---|
59e0d9fe A |
1 | /* |
2 | * Copyright (c) 1989, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Guido van Rossum. | |
7 | * | |
b061a43b A |
8 | * Copyright (c) 2011 The FreeBSD Foundation |
9 | * All rights reserved. | |
10 | * Portions of this software were developed by David Chisnall | |
11 | * under sponsorship from the FreeBSD Foundation. | |
12 | * | |
59e0d9fe A |
13 | * Redistribution and use in source and binary forms, with or without |
14 | * modification, are permitted provided that the following conditions | |
15 | * are met: | |
16 | * 1. Redistributions of source code must retain the above copyright | |
17 | * notice, this list of conditions and the following disclaimer. | |
18 | * 2. Redistributions in binary form must reproduce the above copyright | |
19 | * notice, this list of conditions and the following disclaimer in the | |
20 | * documentation and/or other materials provided with the distribution. | |
b061a43b | 21 | * 3. Neither the name of the University nor the names of its contributors |
59e0d9fe A |
22 | * may be used to endorse or promote products derived from this software |
23 | * without specific prior written permission. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
35 | * SUCH DAMAGE. | |
36 | */ | |
37 | ||
70ad1dc8 A |
38 | #pragma clang diagnostic push |
39 | #pragma clang diagnostic ignored "-Wstrict-prototypes" | |
40 | ||
59e0d9fe A |
41 | #if defined(LIBC_SCCS) && !defined(lint) |
42 | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; | |
43 | #endif /* LIBC_SCCS and not lint */ | |
44 | #include <sys/cdefs.h> | |
b061a43b | 45 | __FBSDID("$FreeBSD$"); |
59e0d9fe | 46 | |
ad3c9f2a A |
47 | #include "xlocale_private.h" |
48 | ||
59e0d9fe A |
49 | /* |
50 | * glob(3) -- a superset of the one defined in POSIX 1003.2. | |
51 | * | |
52 | * The [!...] convention to negate a range is supported (SysV, Posix, ksh). | |
53 | * | |
54 | * Optional extra services, controlled by flags not defined by POSIX: | |
55 | * | |
56 | * GLOB_QUOTE: | |
57 | * Escaping convention: \ inhibits any special meaning the following | |
58 | * character might have (except \ at end of string is retained). | |
59 | * GLOB_MAGCHAR: | |
60 | * Set in gl_flags if pattern contained a globbing character. | |
61 | * GLOB_NOMAGIC: | |
62 | * Same as GLOB_NOCHECK, but it will only append pattern if it did | |
63 | * not contain any magic characters. [Used in csh style globbing] | |
64 | * GLOB_ALTDIRFUNC: | |
65 | * Use alternately specified directory access functions. | |
66 | * GLOB_TILDE: | |
67 | * expand ~user/foo to the /home/dir/of/user/foo | |
68 | * GLOB_BRACE: | |
69 | * expand {1,2}{a,b} to 1a 1b 2a 2b | |
70 | * gl_matchc: | |
71 | * Number of matches in the current invocation of glob. | |
72 | */ | |
73 | ||
3d9156a7 A |
74 | /* |
75 | * Some notes on multibyte character support: | |
76 | * 1. Patterns with illegal byte sequences match nothing - even if | |
77 | * GLOB_NOCHECK is specified. | |
78 | * 2. Illegal byte sequences in filenames are handled by treating them as | |
b061a43b | 79 | * single-byte characters with a values of such bytes of the sequence |
3d9156a7 A |
80 | * cast to wchar_t. |
81 | * 3. State-dependent encodings are not currently supported. | |
82 | */ | |
83 | ||
59e0d9fe A |
84 | #include <sys/param.h> |
85 | #include <sys/stat.h> | |
86 | ||
87 | #include <ctype.h> | |
88 | #include <dirent.h> | |
89 | #include <errno.h> | |
90 | #include <glob.h> | |
3d9156a7 | 91 | #include <limits.h> |
59e0d9fe | 92 | #include <pwd.h> |
3d9156a7 | 93 | #include <stdint.h> |
59e0d9fe A |
94 | #include <stdio.h> |
95 | #include <stdlib.h> | |
96 | #include <string.h> | |
97 | #include <unistd.h> | |
3d9156a7 | 98 | #include <wchar.h> |
b061a43b | 99 | #include <malloc_private.h> |
59e0d9fe A |
100 | |
101 | #include "collate.h" | |
102 | ||
b061a43b A |
103 | /* |
104 | * glob(3) expansion limits. Stop the expansion if any of these limits | |
105 | * is reached. This caps the runtime in the face of DoS attacks. See | |
106 | * also CVE-2010-2632 | |
107 | */ | |
108 | #define GLOB_LIMIT_BRACE 128 /* number of brace calls */ | |
109 | #define GLOB_LIMIT_PATH 1024 /* number of path elements */ | |
110 | #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */ | |
111 | #define GLOB_LIMIT_STAT 128 /* number of stat system calls */ | |
112 | #define GLOB_LIMIT_STRING 65536 /* maximum total size for paths */ | |
ad3c9f2a A |
113 | |
114 | struct glob_limit { | |
b061a43b A |
115 | size_t l_brace_cnt; |
116 | size_t l_path_lim; | |
117 | size_t l_readdir_cnt; | |
118 | size_t l_stat_cnt; | |
119 | size_t l_string_cnt; | |
ad3c9f2a A |
120 | }; |
121 | ||
b061a43b A |
122 | #define DOT L'.' |
123 | #define EOS L'\0' | |
124 | #define LBRACKET L'[' | |
125 | #define NOT L'!' | |
126 | #define QUESTION L'?' | |
127 | #define QUOTE L'\\' | |
128 | #define RANGE L'-' | |
129 | #define RBRACKET L']' | |
130 | #define SEP L'/' | |
131 | #define STAR L'*' | |
132 | #define TILDE L'~' | |
133 | #define LBRACE L'{' | |
134 | #define RBRACE L'}' | |
135 | #define COMMA L',' | |
59e0d9fe | 136 | |
3d9156a7 A |
137 | #define M_QUOTE 0x8000000000ULL |
138 | #define M_PROTECT 0x4000000000ULL | |
139 | #define M_MASK 0xffffffffffULL | |
140 | #define M_CHAR 0x00ffffffffULL | |
59e0d9fe | 141 | |
3d9156a7 | 142 | typedef uint_fast64_t Char; |
59e0d9fe | 143 | |
3d9156a7 | 144 | #define CHAR(c) ((Char)((c)&M_CHAR)) |
59e0d9fe | 145 | #define META(c) ((Char)((c)|M_QUOTE)) |
b061a43b A |
146 | #define UNPROT(c) ((c) & ~M_PROTECT) |
147 | #define M_ALL META(L'*') | |
148 | #define M_END META(L']') | |
149 | #define M_NOT META(L'!') | |
150 | #define M_ONE META(L'?') | |
151 | #define M_RNG META(L'-') | |
152 | #define M_SET META(L'[') | |
59e0d9fe | 153 | #define ismeta(c) (((c)&M_QUOTE) != 0) |
b061a43b A |
154 | #ifdef DEBUG |
155 | #define isprot(c) (((c)&M_PROTECT) != 0) | |
156 | #endif | |
59e0d9fe A |
157 | |
158 | ||
ad3c9f2a A |
159 | #define compare __gl_compare |
160 | #define g_Ctoc __gl_g_Ctoc | |
161 | #define g_strchr __gl_g_strchr | |
162 | #define globextend __gl_globextend | |
163 | #define globtilde __gl_globtilde | |
164 | #define match __gl_match | |
b061a43b | 165 | |
ad3c9f2a A |
166 | __private_extern__ int compare(const void *, const void *); |
167 | __private_extern__ int g_Ctoc(const Char *, char *, size_t, locale_t); | |
ad3c9f2a A |
168 | static int g_lstat(Char *, struct stat *, glob_t *, locale_t); |
169 | static DIR *g_opendir(Char *, glob_t *, locale_t); | |
b061a43b | 170 | __private_extern__ const Char *g_strchr(const Char *, wchar_t); |
59e0d9fe A |
171 | #ifdef notdef |
172 | static Char *g_strcat(Char *, const Char *); | |
173 | #endif | |
ad3c9f2a | 174 | static int g_stat(Char *, struct stat *, glob_t *, locale_t); |
b061a43b A |
175 | static int glob0(const Char *, glob_t *, struct glob_limit *, |
176 | const char *,locale_t); | |
ad3c9f2a | 177 | static int glob1(Char *, glob_t *, struct glob_limit *, locale_t); |
b061a43b A |
178 | static int glob2(Char *, Char *, Char *, Char *, glob_t *, |
179 | struct glob_limit *, locale_t); | |
180 | static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, | |
181 | struct glob_limit *, locale_t); | |
182 | __private_extern__ int globextend(const Char *, glob_t *, struct glob_limit *, | |
183 | const char *, locale_t); | |
184 | __private_extern__ const Char * globtilde(const Char *, Char *, size_t, glob_t *, | |
185 | locale_t); | |
186 | static int globexp0(const Char *, glob_t *, struct glob_limit *, | |
187 | const char *, locale_t); | |
ad3c9f2a | 188 | static int globexp1(const Char *, glob_t *, struct glob_limit *, locale_t); |
b061a43b A |
189 | static int globexp2(const Char *, const Char *, glob_t *, |
190 | struct glob_limit *, locale_t); | |
191 | static int globfinal(glob_t *, struct glob_limit *, size_t, | |
192 | const char *, locale_t); | |
193 | __private_extern__ int match(Char *, Char *, Char *, locale_t); | |
194 | static int err_nomatch(glob_t *, struct glob_limit *, const char *, locale_t loc); | |
195 | static int err_aborted(glob_t *, int, char *); | |
59e0d9fe A |
196 | #ifdef DEBUG |
197 | static void qprintf(const char *, Char *); | |
198 | #endif | |
199 | ||
ad3c9f2a A |
200 | static int |
201 | __glob(const char *pattern, glob_t *pglob) | |
59e0d9fe | 202 | { |
b061a43b | 203 | struct glob_limit limit = { 0, 0, 0, 0, 0 }; |
fbd86d4c | 204 | const char *patnext; |
3d9156a7 A |
205 | Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot; |
206 | mbstate_t mbs; | |
207 | wchar_t wc; | |
208 | size_t clen; | |
b061a43b | 209 | int too_long; |
ad3c9f2a A |
210 | locale_t loc = __current_locale(); |
211 | int mb_cur_max = MB_CUR_MAX_L(loc); | |
59e0d9fe | 212 | |
fbd86d4c | 213 | patnext = pattern; |
ad3c9f2a | 214 | if (!(pglob->gl_flags & GLOB_APPEND)) { |
59e0d9fe A |
215 | pglob->gl_pathc = 0; |
216 | pglob->gl_pathv = NULL; | |
ad3c9f2a | 217 | if (!(pglob->gl_flags & GLOB_DOOFFS)) |
59e0d9fe A |
218 | pglob->gl_offs = 0; |
219 | } | |
b061a43b A |
220 | if (pglob->gl_flags & GLOB_LIMIT) { |
221 | limit.l_path_lim = pglob->gl_matchc; | |
222 | if (limit.l_path_lim == 0) | |
223 | limit.l_path_lim = GLOB_LIMIT_PATH; | |
224 | } | |
59e0d9fe A |
225 | pglob->gl_matchc = 0; |
226 | ||
227 | bufnext = patbuf; | |
228 | bufend = bufnext + MAXPATHLEN - 1; | |
b061a43b | 229 | too_long = 1; |
ad3c9f2a | 230 | if (pglob->gl_flags & GLOB_NOESCAPE) { |
3d9156a7 | 231 | memset(&mbs, 0, sizeof(mbs)); |
ad3c9f2a A |
232 | while (bufend - bufnext >= mb_cur_max) { |
233 | clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc); | |
3d9156a7 | 234 | if (clen == (size_t)-1 || clen == (size_t)-2) |
b061a43b A |
235 | return (err_nomatch(pglob, &limit, pattern, loc)); |
236 | else if (clen == 0) { | |
237 | too_long = 0; | |
3d9156a7 | 238 | break; |
b061a43b | 239 | } |
3d9156a7 A |
240 | *bufnext++ = wc; |
241 | patnext += clen; | |
242 | } | |
243 | } else { | |
59e0d9fe | 244 | /* Protect the quoted characters. */ |
3d9156a7 | 245 | memset(&mbs, 0, sizeof(mbs)); |
ad3c9f2a | 246 | while (bufend - bufnext >= mb_cur_max) { |
b061a43b A |
247 | if (*patnext == '\\') { |
248 | if (*++patnext == '\0') { | |
249 | *bufnext++ = QUOTE; | |
3d9156a7 | 250 | continue; |
59e0d9fe | 251 | } |
3d9156a7 A |
252 | prot = M_PROTECT; |
253 | } else | |
254 | prot = 0; | |
ad3c9f2a | 255 | clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc); |
3d9156a7 | 256 | if (clen == (size_t)-1 || clen == (size_t)-2) |
b061a43b A |
257 | return (err_nomatch(pglob, &limit, pattern, loc)); |
258 | else if (clen == 0) { | |
259 | too_long = 0; | |
3d9156a7 | 260 | break; |
b061a43b | 261 | } |
3d9156a7 A |
262 | *bufnext++ = wc | prot; |
263 | patnext += clen; | |
264 | } | |
59e0d9fe | 265 | } |
b061a43b A |
266 | if (too_long) |
267 | return (err_nomatch(pglob, &limit, pattern, loc)); | |
59e0d9fe A |
268 | *bufnext = EOS; |
269 | ||
ad3c9f2a | 270 | if (pglob->gl_flags & GLOB_BRACE) |
b061a43b | 271 | return globexp0(patbuf, pglob, &limit, pattern, loc); |
59e0d9fe | 272 | else |
b061a43b | 273 | return glob0(patbuf, pglob, &limit, pattern, loc); |
ad3c9f2a A |
274 | } |
275 | ||
276 | int | |
277 | glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) | |
278 | { | |
279 | #ifdef __BLOCKS__ | |
280 | pglob->gl_flags = flags & ~(GLOB_MAGCHAR | _GLOB_ERR_BLOCK); | |
281 | #else /* !__BLOCKS__ */ | |
282 | pglob->gl_flags = flags & ~GLOB_MAGCHAR; | |
283 | #endif /* __BLOCKS__ */ | |
284 | pglob->gl_errfunc = errfunc; | |
285 | return __glob(pattern, pglob); | |
286 | } | |
287 | ||
288 | #ifdef __BLOCKS__ | |
289 | int | |
290 | glob_b(const char *pattern, int flags, int (^errblk)(const char *, int), glob_t *pglob) | |
291 | { | |
292 | pglob->gl_flags = flags & ~GLOB_MAGCHAR; | |
293 | pglob->gl_flags |= _GLOB_ERR_BLOCK; | |
294 | pglob->gl_errblk = errblk; | |
295 | return __glob(pattern, pglob); | |
59e0d9fe | 296 | } |
ad3c9f2a | 297 | #endif /* __BLOCKS__ */ |
59e0d9fe | 298 | |
b061a43b A |
299 | static int |
300 | globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, | |
301 | const char *origpat, locale_t loc) { | |
302 | int rv; | |
303 | size_t oldpathc; | |
304 | ||
305 | /* Protect a single {}, for find(1), like csh */ | |
306 | if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) { | |
307 | if ((pglob->gl_flags & GLOB_LIMIT) && | |
308 | limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { | |
309 | errno = E2BIG; | |
310 | return (GLOB_NOSPACE); | |
311 | } | |
312 | return (glob0(pattern, pglob, limit, origpat, loc)); | |
313 | } | |
314 | ||
315 | oldpathc = pglob->gl_pathc; | |
316 | ||
317 | if ((rv = globexp1(pattern, pglob, limit, loc)) != 0) | |
318 | return rv; | |
319 | ||
320 | return (globfinal(pglob, limit, oldpathc, origpat, loc)); | |
321 | } | |
322 | ||
59e0d9fe A |
323 | /* |
324 | * Expand recursively a glob {} pattern. When there is no more expansion | |
325 | * invoke the standard globbing routine to glob the rest of the magic | |
326 | * characters | |
327 | */ | |
328 | static int | |
ad3c9f2a | 329 | globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit, locale_t loc) |
59e0d9fe | 330 | { |
b061a43b | 331 | const Char* ptr; |
59e0d9fe | 332 | |
b061a43b A |
333 | if ((ptr = g_strchr(pattern, LBRACE)) != NULL) { |
334 | if ((pglob->gl_flags & GLOB_LIMIT) && | |
335 | limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { | |
336 | errno = E2BIG; | |
337 | return (GLOB_NOSPACE); | |
338 | } | |
339 | return (globexp2(ptr, pattern, pglob, limit, loc)); | |
ad3c9f2a A |
340 | } |
341 | ||
b061a43b | 342 | return (glob0(pattern, pglob, limit, NULL, loc)); |
59e0d9fe A |
343 | } |
344 | ||
345 | ||
346 | /* | |
347 | * Recursive brace globbing helper. Tries to expand a single brace. | |
348 | * If it succeeds then it invokes globexp1 with the new pattern. | |
349 | * If it fails then it tries to glob the rest of the pattern and returns. | |
350 | */ | |
351 | static int | |
b061a43b A |
352 | globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, |
353 | struct glob_limit *limit, locale_t loc) | |
59e0d9fe | 354 | { |
b061a43b | 355 | int i, rv; |
59e0d9fe | 356 | Char *lm, *ls; |
fbd86d4c | 357 | const Char *pe, *pm, *pm1, *pl; |
59e0d9fe A |
358 | Char patbuf[MAXPATHLEN]; |
359 | ||
360 | /* copy part up to the brace */ | |
361 | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) | |
362 | continue; | |
363 | *lm = EOS; | |
364 | ls = lm; | |
365 | ||
366 | /* Find the balanced brace */ | |
b061a43b | 367 | for (i = 0, pe = ++ptr; *pe != EOS; pe++) |
59e0d9fe A |
368 | if (*pe == LBRACKET) { |
369 | /* Ignore everything between [] */ | |
370 | for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) | |
371 | continue; | |
372 | if (*pe == EOS) { | |
373 | /* | |
374 | * We could not find a matching RBRACKET. | |
375 | * Ignore and just look for RBRACE | |
376 | */ | |
377 | pe = pm; | |
378 | } | |
379 | } | |
380 | else if (*pe == LBRACE) | |
381 | i++; | |
382 | else if (*pe == RBRACE) { | |
383 | if (i == 0) | |
384 | break; | |
385 | i--; | |
386 | } | |
387 | ||
388 | /* Non matching braces; just glob the pattern */ | |
b061a43b A |
389 | if (i != 0 || *pe == EOS) |
390 | return (glob0(pattern, pglob, limit, NULL, loc)); | |
59e0d9fe A |
391 | |
392 | for (i = 0, pl = pm = ptr; pm <= pe; pm++) | |
393 | switch (*pm) { | |
394 | case LBRACKET: | |
395 | /* Ignore everything between [] */ | |
fbd86d4c | 396 | for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++) |
59e0d9fe A |
397 | continue; |
398 | if (*pm == EOS) { | |
399 | /* | |
400 | * We could not find a matching RBRACKET. | |
401 | * Ignore and just look for RBRACE | |
402 | */ | |
fbd86d4c | 403 | pm = pm1; |
59e0d9fe A |
404 | } |
405 | break; | |
406 | ||
407 | case LBRACE: | |
408 | i++; | |
409 | break; | |
410 | ||
411 | case RBRACE: | |
412 | if (i) { | |
413 | i--; | |
414 | break; | |
415 | } | |
416 | /* FALLTHROUGH */ | |
417 | case COMMA: | |
418 | if (i && *pm == COMMA) | |
419 | break; | |
420 | else { | |
421 | /* Append the current string */ | |
422 | for (lm = ls; (pl < pm); *lm++ = *pl++) | |
423 | continue; | |
424 | /* | |
425 | * Append the rest of the pattern after the | |
426 | * closing brace | |
427 | */ | |
428 | for (pl = pe + 1; (*lm++ = *pl++) != EOS;) | |
429 | continue; | |
430 | ||
431 | /* Expand the current pattern */ | |
432 | #ifdef DEBUG | |
433 | qprintf("globexp2:", patbuf); | |
434 | #endif | |
b061a43b A |
435 | rv = globexp1(patbuf, pglob, limit, loc); |
436 | if (rv) | |
437 | return (rv); | |
59e0d9fe A |
438 | |
439 | /* move after the comma, to the next string */ | |
440 | pl = pm + 1; | |
441 | } | |
442 | break; | |
443 | ||
444 | default: | |
445 | break; | |
446 | } | |
b061a43b | 447 | return (0); |
59e0d9fe A |
448 | } |
449 | ||
450 | ||
451 | ||
ad3c9f2a | 452 | #ifndef BUILDING_VARIANT |
59e0d9fe A |
453 | /* |
454 | * expand tilde from the passwd file. | |
455 | */ | |
ad3c9f2a | 456 | __private_extern__ const Char * |
b061a43b | 457 | globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob, locale_t loc) |
59e0d9fe A |
458 | { |
459 | struct passwd *pwd; | |
b061a43b | 460 | char *h, *sc; |
59e0d9fe A |
461 | const Char *p; |
462 | Char *b, *eb; | |
b061a43b A |
463 | wchar_t wc; |
464 | wchar_t wbuf[MAXPATHLEN]; | |
465 | wchar_t *wbufend, *dc; | |
466 | size_t clen; | |
467 | mbstate_t mbs; | |
468 | int too_long; | |
59e0d9fe A |
469 | |
470 | if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) | |
b061a43b | 471 | return (pattern); |
59e0d9fe A |
472 | |
473 | /* | |
474 | * Copy up to the end of the string or / | |
475 | */ | |
476 | eb = &patbuf[patbuf_len - 1]; | |
b061a43b A |
477 | for (p = pattern + 1, b = patbuf; |
478 | b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++) | |
59e0d9fe A |
479 | continue; |
480 | ||
b061a43b A |
481 | if (*p != EOS && UNPROT(*p) != SEP) |
482 | return (NULL); | |
483 | ||
484 | *b = EOS; | |
485 | h = NULL; | |
59e0d9fe | 486 | |
b061a43b | 487 | if (patbuf[0] == EOS) { |
59e0d9fe A |
488 | /* |
489 | * handle a plain ~ or ~/ by expanding $HOME first (iff | |
490 | * we're not running setuid or setgid) and then trying | |
491 | * the password file | |
492 | */ | |
3d9156a7 | 493 | if (issetugid() != 0 || |
59e0d9fe A |
494 | (h = getenv("HOME")) == NULL) { |
495 | if (((h = getlogin()) != NULL && | |
496 | (pwd = getpwnam(h)) != NULL) || | |
497 | (pwd = getpwuid(getuid())) != NULL) | |
498 | h = pwd->pw_dir; | |
499 | else | |
b061a43b | 500 | return (pattern); |
59e0d9fe A |
501 | } |
502 | } | |
503 | else { | |
504 | /* | |
505 | * Expand a ~user | |
506 | */ | |
b061a43b A |
507 | if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf), loc)) |
508 | return (NULL); | |
509 | if ((pwd = getpwnam((char *)wbuf)) == NULL) | |
510 | return (pattern); | |
59e0d9fe A |
511 | else |
512 | h = pwd->pw_dir; | |
513 | } | |
514 | ||
515 | /* Copy the home directory */ | |
b061a43b A |
516 | dc = wbuf; |
517 | sc = h; | |
518 | wbufend = wbuf + MAXPATHLEN - 1; | |
519 | too_long = 1; | |
520 | memset(&mbs, 0, sizeof(mbs)); | |
521 | while (dc <= wbufend) { | |
522 | clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs); | |
523 | if (clen == (size_t)-1 || clen == (size_t)-2) { | |
524 | /* XXX See initial comment #2. */ | |
525 | wc = (unsigned char)*sc; | |
526 | clen = 1; | |
527 | memset(&mbs, 0, sizeof(mbs)); | |
528 | } | |
529 | if ((*dc++ = wc) == EOS) { | |
530 | too_long = 0; | |
531 | break; | |
532 | } | |
533 | sc += clen; | |
534 | } | |
535 | if (too_long) | |
536 | return (NULL); | |
537 | ||
538 | dc = wbuf; | |
539 | for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT) | |
59e0d9fe | 540 | continue; |
b061a43b A |
541 | if (*dc != EOS) |
542 | return (NULL); | |
59e0d9fe A |
543 | |
544 | /* Append the rest of the pattern */ | |
b061a43b A |
545 | if (*p != EOS) { |
546 | too_long = 1; | |
547 | while (b <= eb) { | |
548 | if ((*b++ = *p++) == EOS) { | |
549 | too_long = 0; | |
550 | break; | |
551 | } | |
552 | } | |
553 | if (too_long) | |
554 | return (NULL); | |
555 | } else | |
556 | *b = EOS; | |
59e0d9fe | 557 | |
b061a43b | 558 | return (patbuf); |
59e0d9fe | 559 | } |
ad3c9f2a | 560 | #endif /* BUILDING_VARIANT */ |
59e0d9fe A |
561 | |
562 | ||
563 | /* | |
564 | * The main glob() routine: compiles the pattern (optionally processing | |
565 | * quotes), calls glob1() to do the real pattern matching, and finally | |
566 | * sorts the list (unless unsorted operation is requested). Returns 0 | |
567 | * if things went well, nonzero if errors occurred. | |
568 | */ | |
569 | static int | |
b061a43b A |
570 | glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, |
571 | const char *origpat, locale_t loc) | |
59e0d9fe A |
572 | { |
573 | const Char *qpatnext; | |
fbd86d4c A |
574 | int err; |
575 | size_t oldpathc; | |
576 | Char *bufnext, c, patbuf[MAXPATHLEN]; | |
59e0d9fe | 577 | |
b061a43b A |
578 | qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob, loc); |
579 | if (qpatnext == NULL) { | |
580 | errno = E2BIG; | |
581 | return (GLOB_NOSPACE); | |
582 | } | |
59e0d9fe A |
583 | oldpathc = pglob->gl_pathc; |
584 | bufnext = patbuf; | |
585 | ||
586 | /* We don't need to check for buffer overflow any more. */ | |
587 | while ((c = *qpatnext++) != EOS) { | |
588 | switch (c) { | |
589 | case LBRACKET: | |
590 | c = *qpatnext; | |
591 | if (c == NOT) | |
592 | ++qpatnext; | |
593 | if (*qpatnext == EOS || | |
fbd86d4c | 594 | g_strchr(qpatnext+1, RBRACKET) == NULL) { |
59e0d9fe A |
595 | *bufnext++ = LBRACKET; |
596 | if (c == NOT) | |
597 | --qpatnext; | |
598 | break; | |
599 | } | |
600 | *bufnext++ = M_SET; | |
601 | if (c == NOT) | |
602 | *bufnext++ = M_NOT; | |
603 | c = *qpatnext++; | |
604 | do { | |
605 | *bufnext++ = CHAR(c); | |
606 | if (*qpatnext == RANGE && | |
607 | (c = qpatnext[1]) != RBRACKET) { | |
608 | *bufnext++ = M_RNG; | |
609 | *bufnext++ = CHAR(c); | |
610 | qpatnext += 2; | |
611 | } | |
612 | } while ((c = *qpatnext++) != RBRACKET); | |
613 | pglob->gl_flags |= GLOB_MAGCHAR; | |
614 | *bufnext++ = M_END; | |
615 | break; | |
616 | case QUESTION: | |
617 | pglob->gl_flags |= GLOB_MAGCHAR; | |
618 | *bufnext++ = M_ONE; | |
619 | break; | |
620 | case STAR: | |
621 | pglob->gl_flags |= GLOB_MAGCHAR; | |
622 | /* collapse adjacent stars to one, | |
b061a43b A |
623 | * to ensure "**" at the end continues to match the |
624 | * empty string | |
59e0d9fe A |
625 | */ |
626 | if (bufnext == patbuf || bufnext[-1] != M_ALL) | |
627 | *bufnext++ = M_ALL; | |
628 | break; | |
629 | default: | |
630 | *bufnext++ = CHAR(c); | |
631 | break; | |
632 | } | |
633 | } | |
634 | *bufnext = EOS; | |
635 | #ifdef DEBUG | |
636 | qprintf("glob0:", patbuf); | |
637 | #endif | |
638 | ||
ad3c9f2a | 639 | if ((err = glob1(patbuf, pglob, limit, loc)) != 0) |
59e0d9fe A |
640 | return(err); |
641 | ||
b061a43b A |
642 | if (origpat != NULL) |
643 | return (globfinal(pglob, limit, oldpathc, origpat, loc)); | |
644 | ||
645 | return (0); | |
646 | } | |
647 | ||
648 | static int | |
649 | globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc, | |
650 | const char *origpat, locale_t loc) { | |
651 | if (pglob->gl_pathc == oldpathc) | |
652 | return (err_nomatch(pglob, limit, origpat, loc)); | |
653 | ||
59e0d9fe A |
654 | if (!(pglob->gl_flags & GLOB_NOSORT)) |
655 | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, | |
656 | pglob->gl_pathc - oldpathc, sizeof(char *), compare); | |
b061a43b A |
657 | |
658 | return (0); | |
59e0d9fe A |
659 | } |
660 | ||
ad3c9f2a A |
661 | #ifndef BUILDING_VARIANT |
662 | __private_extern__ int | |
fbd86d4c | 663 | compare(const void *p, const void *q) |
59e0d9fe | 664 | { |
ad3c9f2a | 665 | return(strcoll(*(char **)p, *(char **)q)); |
59e0d9fe | 666 | } |
ad3c9f2a | 667 | #endif /* BUILDING_VARIANT */ |
59e0d9fe A |
668 | |
669 | static int | |
ad3c9f2a | 670 | glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit, locale_t loc) |
59e0d9fe A |
671 | { |
672 | Char pathbuf[MAXPATHLEN]; | |
673 | ||
674 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ | |
675 | if (*pattern == EOS) | |
b061a43b A |
676 | return (0); |
677 | return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, | |
ad3c9f2a | 678 | pattern, pglob, limit, loc)); |
59e0d9fe A |
679 | } |
680 | ||
681 | /* | |
682 | * The functions glob2 and glob3 are mutually recursive; there is one level | |
683 | * of recursion for each segment in the pattern that contains one or more | |
684 | * meta characters. | |
685 | */ | |
686 | static int | |
fbd86d4c | 687 | glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, |
ad3c9f2a | 688 | glob_t *pglob, struct glob_limit *limit, locale_t loc) |
59e0d9fe A |
689 | { |
690 | struct stat sb; | |
691 | Char *p, *q; | |
692 | int anymeta; | |
693 | ||
694 | /* | |
695 | * Loop over pattern segments until end of pattern or until | |
696 | * segment with meta character found. | |
697 | */ | |
698 | for (anymeta = 0;;) { | |
699 | if (*pattern == EOS) { /* End of pattern? */ | |
700 | *pathend = EOS; | |
ad3c9f2a | 701 | if (g_lstat(pathbuf, &sb, pglob, loc)) |
b061a43b | 702 | return (0); |
59e0d9fe | 703 | |
ad3c9f2a | 704 | if ((pglob->gl_flags & GLOB_LIMIT) && |
b061a43b A |
705 | limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) { |
706 | errno = E2BIG; | |
707 | return (GLOB_NOSPACE); | |
ad3c9f2a | 708 | } |
b061a43b A |
709 | if ((pglob->gl_flags & GLOB_MARK) && |
710 | UNPROT(pathend[-1]) != SEP && | |
711 | (S_ISDIR(sb.st_mode) || | |
712 | (S_ISLNK(sb.st_mode) && | |
713 | g_stat(pathbuf, &sb, pglob, loc) == 0 && | |
714 | S_ISDIR(sb.st_mode)))) { | |
715 | if (pathend + 1 > pathend_last) { | |
716 | errno = E2BIG; | |
717 | return (GLOB_NOSPACE); | |
718 | } | |
59e0d9fe A |
719 | *pathend++ = SEP; |
720 | *pathend = EOS; | |
721 | } | |
722 | ++pglob->gl_matchc; | |
b061a43b | 723 | return (globextend(pathbuf, pglob, limit, NULL, loc)); |
59e0d9fe A |
724 | } |
725 | ||
726 | /* Find end of next segment, copy tentatively to pathend. */ | |
727 | q = pathend; | |
728 | p = pattern; | |
b061a43b | 729 | while (*p != EOS && UNPROT(*p) != SEP) { |
59e0d9fe A |
730 | if (ismeta(*p)) |
731 | anymeta = 1; | |
b061a43b A |
732 | if (q + 1 > pathend_last) { |
733 | errno = E2BIG; | |
734 | return (GLOB_NOSPACE); | |
735 | } | |
59e0d9fe A |
736 | *q++ = *p++; |
737 | } | |
738 | ||
739 | if (!anymeta) { /* No expansion, do next segment. */ | |
740 | pathend = q; | |
741 | pattern = p; | |
b061a43b A |
742 | while (UNPROT(*pattern) == SEP) { |
743 | if (pathend + 1 > pathend_last) { | |
744 | errno = E2BIG; | |
745 | return (GLOB_NOSPACE); | |
746 | } | |
59e0d9fe A |
747 | *pathend++ = *pattern++; |
748 | } | |
749 | } else /* Need expansion, recurse. */ | |
b061a43b A |
750 | return(glob3(pathbuf, pathend, pathend_last, pattern, |
751 | p, pglob, limit, loc)); | |
59e0d9fe A |
752 | } |
753 | /* NOTREACHED */ | |
754 | } | |
755 | ||
756 | static int | |
fbd86d4c A |
757 | glob3(Char *pathbuf, Char *pathend, Char *pathend_last, |
758 | Char *pattern, Char *restpattern, | |
ad3c9f2a | 759 | glob_t *pglob, struct glob_limit *limit, locale_t loc) |
59e0d9fe A |
760 | { |
761 | struct dirent *dp; | |
762 | DIR *dirp; | |
b061a43b A |
763 | int err, too_long, saverrno, saverrno2; |
764 | char buf[MAXPATHLEN + MB_LEN_MAX - 1]; | |
59e0d9fe A |
765 | |
766 | /* | |
767 | * The readdirfunc declaration can't be prototyped, because it is | |
768 | * assigned, below, to two functions which are prototyped in glob.h | |
769 | * and dirent.h as taking pointers to differently typed opaque | |
770 | * structures. | |
771 | */ | |
772 | struct dirent *(*readdirfunc)(); | |
773 | ||
b061a43b A |
774 | if (pathend > pathend_last) { |
775 | errno = E2BIG; | |
776 | return (GLOB_NOSPACE); | |
777 | } | |
59e0d9fe | 778 | *pathend = EOS; |
b061a43b A |
779 | if ((pglob->gl_errfunc != NULL || pglob->gl_errblk != NULL) && |
780 | g_Ctoc(pathbuf, buf, sizeof(buf), loc)) { | |
781 | errno = E2BIG; | |
782 | return (GLOB_NOSPACE); | |
783 | } | |
784 | ||
785 | saverrno = errno; | |
59e0d9fe A |
786 | errno = 0; |
787 | ||
ad3c9f2a | 788 | if ((dirp = g_opendir(pathbuf, pglob, loc)) == NULL) { |
b061a43b A |
789 | if (errno == ENOENT || errno == ENOTDIR) |
790 | return (0); | |
791 | ||
792 | err = err_aborted(pglob, errno, buf); | |
793 | if (errno == 0) | |
794 | errno = saverrno; | |
795 | return (err); | |
59e0d9fe A |
796 | } |
797 | ||
798 | err = 0; | |
799 | ||
59e0d9fe A |
800 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
801 | readdirfunc = pglob->gl_readdir; | |
802 | else | |
803 | readdirfunc = readdir; | |
b061a43b A |
804 | |
805 | errno = 0; | |
806 | /* Search directory for matching names. */ | |
807 | while ((dp = (*readdirfunc)(dirp)) != NULL) { | |
fbd86d4c | 808 | char *sc; |
59e0d9fe | 809 | Char *dc; |
3d9156a7 A |
810 | wchar_t wc; |
811 | size_t clen; | |
812 | mbstate_t mbs; | |
59e0d9fe | 813 | |
ad3c9f2a | 814 | if ((pglob->gl_flags & GLOB_LIMIT) && |
b061a43b A |
815 | limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) { |
816 | errno = E2BIG; | |
817 | err = GLOB_NOSPACE; | |
818 | break; | |
ad3c9f2a A |
819 | } |
820 | ||
59e0d9fe | 821 | /* Initial DOT must be matched literally. */ |
b061a43b A |
822 | if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) { |
823 | errno = 0; | |
59e0d9fe | 824 | continue; |
b061a43b | 825 | } |
3d9156a7 | 826 | memset(&mbs, 0, sizeof(mbs)); |
59e0d9fe | 827 | dc = pathend; |
fbd86d4c | 828 | sc = dp->d_name; |
b061a43b A |
829 | too_long = 1; |
830 | while (dc <= pathend_last) { | |
ad3c9f2a | 831 | clen = mbrtowc_l(&wc, sc, MB_LEN_MAX, &mbs, loc); |
3d9156a7 | 832 | if (clen == (size_t)-1 || clen == (size_t)-2) { |
b061a43b A |
833 | /* XXX See initial comment #2. */ |
834 | wc = (unsigned char)*sc; | |
3d9156a7 A |
835 | clen = 1; |
836 | memset(&mbs, 0, sizeof(mbs)); | |
837 | } | |
b061a43b A |
838 | if ((*dc++ = wc) == EOS) { |
839 | too_long = 0; | |
3d9156a7 | 840 | break; |
b061a43b | 841 | } |
3d9156a7 A |
842 | sc += clen; |
843 | } | |
b061a43b A |
844 | if (too_long && (err = err_aborted(pglob, ENAMETOOLONG, |
845 | buf))) { | |
846 | errno = ENAMETOOLONG; | |
847 | break; | |
848 | } | |
849 | if (too_long || !match(pathend, pattern, restpattern, loc)) { | |
59e0d9fe | 850 | *pathend = EOS; |
b061a43b | 851 | errno = 0; |
59e0d9fe A |
852 | continue; |
853 | } | |
b061a43b A |
854 | if (errno == 0) |
855 | errno = saverrno; | |
59e0d9fe | 856 | err = glob2(pathbuf, --dc, pathend_last, restpattern, |
ad3c9f2a | 857 | pglob, limit, loc); |
59e0d9fe A |
858 | if (err) |
859 | break; | |
b061a43b | 860 | errno = 0; |
59e0d9fe A |
861 | } |
862 | ||
b061a43b | 863 | saverrno2 = errno; |
59e0d9fe A |
864 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
865 | (*pglob->gl_closedir)(dirp); | |
866 | else | |
867 | closedir(dirp); | |
b061a43b A |
868 | errno = saverrno2; |
869 | ||
870 | if (err) | |
871 | return (err); | |
872 | ||
873 | if (dp == NULL && errno != 0 && | |
874 | (err = err_aborted(pglob, errno, buf))) | |
875 | return (err); | |
876 | ||
877 | if (errno == 0) | |
878 | errno = saverrno; | |
879 | return (0); | |
59e0d9fe A |
880 | } |
881 | ||
882 | ||
ad3c9f2a | 883 | #ifndef BUILDING_VARIANT |
59e0d9fe | 884 | /* |
b061a43b | 885 | * Extend the gl_pathv member of a glob_t structure to accommodate a new item, |
59e0d9fe A |
886 | * add the new item, and update gl_pathc. |
887 | * | |
888 | * This assumes the BSD realloc, which only copies the block when its size | |
889 | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic | |
890 | * behavior. | |
891 | * | |
892 | * Return 0 if new item added, error code if memory couldn't be allocated. | |
893 | * | |
894 | * Invariant of the glob_t structure: | |
895 | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and | |
896 | * gl_pathv points to (gl_offs + gl_pathc + 1) items. | |
897 | */ | |
ad3c9f2a | 898 | __private_extern__ int |
b061a43b A |
899 | globextend(const Char *path, glob_t *pglob, struct glob_limit *limit, |
900 | const char *origpat, locale_t loc) | |
59e0d9fe A |
901 | { |
902 | char **pathv; | |
b061a43b | 903 | size_t i, newn, len; |
59e0d9fe A |
904 | char *copy; |
905 | const Char *p; | |
906 | ||
ad3c9f2a | 907 | if ((pglob->gl_flags & GLOB_LIMIT) && |
b061a43b A |
908 | pglob->gl_matchc > limit->l_path_lim) { |
909 | errno = E2BIG; | |
910 | return (GLOB_NOSPACE); | |
59e0d9fe A |
911 | } |
912 | ||
b061a43b A |
913 | newn = 2 + pglob->gl_pathc + pglob->gl_offs; |
914 | /* reallocarray(NULL, newn, size) is equivalent to malloc(newn*size). */ | |
915 | pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv)); | |
916 | if (pathv == NULL) | |
917 | return (GLOB_NOSPACE); | |
918 | ||
59e0d9fe A |
919 | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
920 | /* first time around -- clear initial gl_offs items */ | |
921 | pathv += pglob->gl_offs; | |
fbd86d4c | 922 | for (i = pglob->gl_offs + 1; --i > 0; ) |
59e0d9fe A |
923 | *--pathv = NULL; |
924 | } | |
925 | pglob->gl_pathv = pathv; | |
926 | ||
b061a43b A |
927 | if (origpat != NULL) |
928 | copy = strdup(origpat); | |
929 | else { | |
930 | for (p = path; *p++ != EOS;) | |
931 | continue; | |
932 | len = MB_CUR_MAX_L(loc) * (size_t)(p - path); /* XXX overallocation */ | |
933 | if ((copy = malloc(len)) != NULL) { | |
934 | if (g_Ctoc(path, copy, len, loc)) { | |
935 | free(copy); | |
936 | errno = E2BIG; | |
937 | return (GLOB_NOSPACE); | |
938 | } | |
939 | } | |
940 | } | |
941 | if (copy != NULL) { | |
942 | limit->l_string_cnt += strlen(copy) + 1; | |
943 | if ((pglob->gl_flags & GLOB_LIMIT) && | |
944 | limit->l_string_cnt >= GLOB_LIMIT_STRING) { | |
59e0d9fe | 945 | free(copy); |
b061a43b | 946 | errno = E2BIG; |
59e0d9fe A |
947 | return (GLOB_NOSPACE); |
948 | } | |
949 | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; | |
950 | } | |
951 | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
b061a43b | 952 | return (copy == NULL ? GLOB_NOSPACE : 0); |
59e0d9fe A |
953 | } |
954 | ||
955 | /* | |
b061a43b | 956 | * pattern matching function for filenames. |
59e0d9fe | 957 | */ |
ad3c9f2a A |
958 | __private_extern__ int |
959 | match(Char *name, Char *pat, Char *patend, locale_t loc) | |
59e0d9fe A |
960 | { |
961 | int ok, negate_range; | |
b061a43b A |
962 | Char c, k, *nextp, *nextn; |
963 | ||
964 | nextn = NULL; | |
965 | nextp = NULL; | |
966 | ||
967 | while (1) { | |
968 | while (pat < patend) { | |
969 | c = *pat++; | |
970 | switch (c & M_MASK) { | |
971 | case M_ALL: | |
972 | if (pat == patend) | |
973 | return (1); | |
974 | if (*name == EOS) | |
975 | return (0); | |
976 | nextn = name + 1; | |
977 | nextp = pat - 1; | |
978 | break; | |
979 | case M_ONE: | |
980 | if (*name++ == EOS) | |
981 | goto fail; | |
982 | break; | |
983 | case M_SET: | |
984 | ok = 0; | |
985 | if ((k = *name++) == EOS) | |
986 | goto fail; | |
987 | negate_range = ((*pat & M_MASK) == M_NOT); | |
988 | if (negate_range != 0) | |
989 | ++pat; | |
990 | while (((c = *pat++) & M_MASK) != M_END) | |
991 | if ((*pat & M_MASK) == M_RNG) { | |
992 | if (loc->__collate_load_error ? | |
993 | CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) : | |
994 | __collate_range_cmp(CHAR(c), CHAR(k), loc) <= 0 | |
995 | && __collate_range_cmp(CHAR(k), CHAR(pat[1]), loc) <= 0 | |
996 | ) { | |
997 | ok = 1; | |
998 | } | |
999 | pat += 2; | |
1000 | } else if (c == k) | |
59e0d9fe | 1001 | ok = 1; |
b061a43b A |
1002 | if (ok == negate_range) |
1003 | goto fail; | |
1004 | break; | |
1005 | default: | |
1006 | if (*name++ != c) | |
1007 | goto fail; | |
1008 | break; | |
1009 | } | |
59e0d9fe | 1010 | } |
b061a43b A |
1011 | if (*name == EOS) |
1012 | return (1); | |
1013 | ||
1014 | fail: | |
1015 | if (nextn == NULL) | |
1016 | break; | |
1017 | pat = nextp; | |
1018 | name = nextn; | |
59e0d9fe | 1019 | } |
b061a43b | 1020 | return (0); |
59e0d9fe A |
1021 | } |
1022 | ||
1023 | /* Free allocated data belonging to a glob_t structure. */ | |
1024 | void | |
fbd86d4c | 1025 | globfree(glob_t *pglob) |
59e0d9fe | 1026 | { |
fbd86d4c | 1027 | size_t i; |
59e0d9fe A |
1028 | char **pp; |
1029 | ||
1030 | if (pglob->gl_pathv != NULL) { | |
1031 | pp = pglob->gl_pathv + pglob->gl_offs; | |
1032 | for (i = pglob->gl_pathc; i--; ++pp) | |
1033 | if (*pp) | |
1034 | free(*pp); | |
1035 | free(pglob->gl_pathv); | |
1036 | pglob->gl_pathv = NULL; | |
1037 | } | |
1038 | } | |
ad3c9f2a | 1039 | #endif /* !BUILDING_VARIANT */ |
59e0d9fe A |
1040 | |
1041 | static DIR * | |
ad3c9f2a | 1042 | g_opendir(Char *str, glob_t *pglob, locale_t loc) |
59e0d9fe | 1043 | { |
b061a43b | 1044 | char buf[MAXPATHLEN + MB_LEN_MAX - 1]; |
59e0d9fe | 1045 | |
b061a43b | 1046 | if (*str == EOS) |
59e0d9fe A |
1047 | strcpy(buf, "."); |
1048 | else { | |
b061a43b A |
1049 | if (g_Ctoc(str, buf, sizeof(buf), loc)) { |
1050 | errno = ENAMETOOLONG; | |
59e0d9fe | 1051 | return (NULL); |
b061a43b | 1052 | } |
59e0d9fe A |
1053 | } |
1054 | ||
1055 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
b061a43b | 1056 | return ((*pglob->gl_opendir)(buf)); |
59e0d9fe | 1057 | |
b061a43b | 1058 | return (opendir(buf)); |
59e0d9fe A |
1059 | } |
1060 | ||
1061 | static int | |
ad3c9f2a | 1062 | g_lstat(Char *fn, struct stat *sb, glob_t *pglob, locale_t loc) |
59e0d9fe | 1063 | { |
b061a43b | 1064 | char buf[MAXPATHLEN + MB_LEN_MAX - 1]; |
59e0d9fe | 1065 | |
ad3c9f2a | 1066 | if (g_Ctoc(fn, buf, sizeof(buf), loc)) { |
59e0d9fe A |
1067 | errno = ENAMETOOLONG; |
1068 | return (-1); | |
1069 | } | |
1070 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
1071 | return((*pglob->gl_lstat)(buf, sb)); | |
b061a43b | 1072 | return (lstat(buf, sb)); |
59e0d9fe A |
1073 | } |
1074 | ||
1075 | static int | |
ad3c9f2a | 1076 | g_stat(Char *fn, struct stat *sb, glob_t *pglob, locale_t loc) |
59e0d9fe | 1077 | { |
b061a43b | 1078 | char buf[MAXPATHLEN + MB_LEN_MAX - 1]; |
59e0d9fe | 1079 | |
ad3c9f2a | 1080 | if (g_Ctoc(fn, buf, sizeof(buf), loc)) { |
59e0d9fe A |
1081 | errno = ENAMETOOLONG; |
1082 | return (-1); | |
1083 | } | |
1084 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
b061a43b A |
1085 | return ((*pglob->gl_stat)(buf, sb)); |
1086 | return (stat(buf, sb)); | |
59e0d9fe A |
1087 | } |
1088 | ||
ad3c9f2a A |
1089 | #ifndef BUILDING_VARIANT |
1090 | __private_extern__ const Char * | |
fbd86d4c | 1091 | g_strchr(const Char *str, wchar_t ch) |
59e0d9fe | 1092 | { |
fbd86d4c | 1093 | |
59e0d9fe A |
1094 | do { |
1095 | if (*str == ch) | |
1096 | return (str); | |
1097 | } while (*str++); | |
1098 | return (NULL); | |
1099 | } | |
1100 | ||
ad3c9f2a A |
1101 | __private_extern__ int |
1102 | g_Ctoc(const Char *str, char *buf, size_t len, locale_t loc) | |
59e0d9fe | 1103 | { |
3d9156a7 A |
1104 | mbstate_t mbs; |
1105 | size_t clen; | |
ad3c9f2a | 1106 | int mb_cur_max = MB_CUR_MAX_L(loc); |
3d9156a7 A |
1107 | |
1108 | memset(&mbs, 0, sizeof(mbs)); | |
ad3c9f2a A |
1109 | while (len >= mb_cur_max) { |
1110 | clen = wcrtomb_l(buf, *str, &mbs, loc); | |
b061a43b A |
1111 | if (clen == (size_t)-1) { |
1112 | /* XXX See initial comment #2. */ | |
1113 | *buf = (char)CHAR(*str); | |
1114 | clen = 1; | |
1115 | memset(&mbs, 0, sizeof(mbs)); | |
1116 | } | |
1117 | if (CHAR(*str) == EOS) | |
59e0d9fe | 1118 | return (0); |
3d9156a7 A |
1119 | str++; |
1120 | buf += clen; | |
1121 | len -= clen; | |
59e0d9fe A |
1122 | } |
1123 | return (1); | |
1124 | } | |
ad3c9f2a | 1125 | #endif /* !BUILDING_VARIANT */ |
59e0d9fe | 1126 | |
b061a43b A |
1127 | static int |
1128 | err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat, locale_t loc) { | |
1129 | /* | |
1130 | * If there was no match we are going to append the origpat | |
1131 | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified | |
1132 | * and the origpat did not contain any magic characters | |
1133 | * GLOB_NOMAGIC is there just for compatibility with csh. | |
1134 | */ | |
1135 | if ((pglob->gl_flags & GLOB_NOCHECK) || | |
1136 | ((pglob->gl_flags & GLOB_NOMAGIC) && | |
1137 | !(pglob->gl_flags & GLOB_MAGCHAR))) | |
1138 | return (globextend(NULL, pglob, limit, origpat, loc)); | |
1139 | return (GLOB_NOMATCH); | |
1140 | } | |
1141 | ||
1142 | static int | |
1143 | err_aborted(glob_t *pglob, int err, char *buf) { | |
1144 | #ifdef __BLOCKS__ | |
1145 | if (pglob->gl_flags & _GLOB_ERR_BLOCK && pglob->gl_errblk(buf, errno)) { | |
1146 | return (GLOB_ABORTED); | |
1147 | } else | |
1148 | #endif /* __BLOCKS__ */ | |
1149 | if (pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, errno)) { | |
1150 | return (GLOB_ABORTED); | |
1151 | } else if (pglob->gl_flags & GLOB_ERR) { | |
1152 | return (GLOB_ABORTED); | |
1153 | } | |
1154 | return (0); | |
1155 | } | |
1156 | ||
59e0d9fe A |
1157 | #ifdef DEBUG |
1158 | static void | |
fbd86d4c | 1159 | qprintf(const char *str, Char *s) |
59e0d9fe A |
1160 | { |
1161 | Char *p; | |
1162 | ||
b061a43b A |
1163 | (void)printf("%s\n", str); |
1164 | if (s != NULL) { | |
1165 | for (p = s; *p != EOS; p++) | |
1166 | (void)printf("%c", (char)CHAR(*p)); | |
1167 | (void)printf("\n"); | |
1168 | for (p = s; *p != EOS; p++) | |
1169 | (void)printf("%c", (isprot(*p) ? '\\' : ' ')); | |
1170 | (void)printf("\n"); | |
1171 | for (p = s; *p != EOS; p++) | |
1172 | (void)printf("%c", (ismeta(*p) ? '_' : ' ')); | |
1173 | (void)printf("\n"); | |
1174 | } | |
59e0d9fe A |
1175 | } |
1176 | #endif | |
70ad1dc8 | 1177 | #pragma clang diagnostic pop |