]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/glob.c
Libc-1353.11.2.tar.gz
[apple/libc.git] / gen / FreeBSD / glob.c
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 *
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 *
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.
21 * 3. Neither the name of the University nor the names of its contributors
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
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wstrict-prototypes"
40
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>
45 __FBSDID("$FreeBSD$");
46
47 #include "xlocale_private.h"
48
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
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
79 * single-byte characters with a values of such bytes of the sequence
80 * cast to wchar_t.
81 * 3. State-dependent encodings are not currently supported.
82 */
83
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>
91 #include <limits.h>
92 #include <pwd.h>
93 #include <stdint.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include <wchar.h>
99 #include <malloc_private.h>
100
101 #include "collate.h"
102
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 */
113
114 struct glob_limit {
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;
120 };
121
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','
136
137 #define M_QUOTE 0x8000000000ULL
138 #define M_PROTECT 0x4000000000ULL
139 #define M_MASK 0xffffffffffULL
140 #define M_CHAR 0x00ffffffffULL
141
142 typedef uint_fast64_t Char;
143
144 #define CHAR(c) ((Char)((c)&M_CHAR))
145 #define META(c) ((Char)((c)|M_QUOTE))
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'[')
153 #define ismeta(c) (((c)&M_QUOTE) != 0)
154 #ifdef DEBUG
155 #define isprot(c) (((c)&M_PROTECT) != 0)
156 #endif
157
158
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
165
166 __private_extern__ int compare(const void *, const void *);
167 __private_extern__ int g_Ctoc(const Char *, char *, size_t, locale_t);
168 static int g_lstat(Char *, struct stat *, glob_t *, locale_t);
169 static DIR *g_opendir(Char *, glob_t *, locale_t);
170 __private_extern__ const Char *g_strchr(const Char *, wchar_t);
171 #ifdef notdef
172 static Char *g_strcat(Char *, const Char *);
173 #endif
174 static int g_stat(Char *, struct stat *, glob_t *, locale_t);
175 static int glob0(const Char *, glob_t *, struct glob_limit *,
176 const char *,locale_t);
177 static int glob1(Char *, glob_t *, struct glob_limit *, locale_t);
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);
188 static int globexp1(const Char *, glob_t *, struct glob_limit *, locale_t);
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 *);
196 #ifdef DEBUG
197 static void qprintf(const char *, Char *);
198 #endif
199
200 static int
201 __glob(const char *pattern, glob_t *pglob)
202 {
203 struct glob_limit limit = { 0, 0, 0, 0, 0 };
204 const char *patnext;
205 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
206 mbstate_t mbs;
207 wchar_t wc;
208 size_t clen;
209 int too_long;
210 locale_t loc = __current_locale();
211 int mb_cur_max = MB_CUR_MAX_L(loc);
212
213 patnext = pattern;
214 if (!(pglob->gl_flags & GLOB_APPEND)) {
215 pglob->gl_pathc = 0;
216 pglob->gl_pathv = NULL;
217 if (!(pglob->gl_flags & GLOB_DOOFFS))
218 pglob->gl_offs = 0;
219 }
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 }
225 pglob->gl_matchc = 0;
226
227 bufnext = patbuf;
228 bufend = bufnext + MAXPATHLEN - 1;
229 too_long = 1;
230 if (pglob->gl_flags & GLOB_NOESCAPE) {
231 memset(&mbs, 0, sizeof(mbs));
232 while (bufend - bufnext >= mb_cur_max) {
233 clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc);
234 if (clen == (size_t)-1 || clen == (size_t)-2)
235 return (err_nomatch(pglob, &limit, pattern, loc));
236 else if (clen == 0) {
237 too_long = 0;
238 break;
239 }
240 *bufnext++ = wc;
241 patnext += clen;
242 }
243 } else {
244 /* Protect the quoted characters. */
245 memset(&mbs, 0, sizeof(mbs));
246 while (bufend - bufnext >= mb_cur_max) {
247 if (*patnext == '\\') {
248 if (*++patnext == '\0') {
249 *bufnext++ = QUOTE;
250 continue;
251 }
252 prot = M_PROTECT;
253 } else
254 prot = 0;
255 clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc);
256 if (clen == (size_t)-1 || clen == (size_t)-2)
257 return (err_nomatch(pglob, &limit, pattern, loc));
258 else if (clen == 0) {
259 too_long = 0;
260 break;
261 }
262 *bufnext++ = wc | prot;
263 patnext += clen;
264 }
265 }
266 if (too_long)
267 return (err_nomatch(pglob, &limit, pattern, loc));
268 *bufnext = EOS;
269
270 if (pglob->gl_flags & GLOB_BRACE)
271 return globexp0(patbuf, pglob, &limit, pattern, loc);
272 else
273 return glob0(patbuf, pglob, &limit, pattern, loc);
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);
296 }
297 #endif /* __BLOCKS__ */
298
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
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
329 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit, locale_t loc)
330 {
331 const Char* ptr;
332
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));
340 }
341
342 return (glob0(pattern, pglob, limit, NULL, loc));
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
352 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
353 struct glob_limit *limit, locale_t loc)
354 {
355 int i, rv;
356 Char *lm, *ls;
357 const Char *pe, *pm, *pm1, *pl;
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 */
367 for (i = 0, pe = ++ptr; *pe != EOS; pe++)
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 */
389 if (i != 0 || *pe == EOS)
390 return (glob0(pattern, pglob, limit, NULL, loc));
391
392 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
393 switch (*pm) {
394 case LBRACKET:
395 /* Ignore everything between [] */
396 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
397 continue;
398 if (*pm == EOS) {
399 /*
400 * We could not find a matching RBRACKET.
401 * Ignore and just look for RBRACE
402 */
403 pm = pm1;
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
435 rv = globexp1(patbuf, pglob, limit, loc);
436 if (rv)
437 return (rv);
438
439 /* move after the comma, to the next string */
440 pl = pm + 1;
441 }
442 break;
443
444 default:
445 break;
446 }
447 return (0);
448 }
449
450
451
452 #ifndef BUILDING_VARIANT
453 /*
454 * expand tilde from the passwd file.
455 */
456 __private_extern__ const Char *
457 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob, locale_t loc)
458 {
459 struct passwd *pwd;
460 char *h, *sc;
461 const Char *p;
462 Char *b, *eb;
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;
469
470 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
471 return (pattern);
472
473 /*
474 * Copy up to the end of the string or /
475 */
476 eb = &patbuf[patbuf_len - 1];
477 for (p = pattern + 1, b = patbuf;
478 b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
479 continue;
480
481 if (*p != EOS && UNPROT(*p) != SEP)
482 return (NULL);
483
484 *b = EOS;
485 h = NULL;
486
487 if (patbuf[0] == EOS) {
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 */
493 if (issetugid() != 0 ||
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
500 return (pattern);
501 }
502 }
503 else {
504 /*
505 * Expand a ~user
506 */
507 if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf), loc))
508 return (NULL);
509 if ((pwd = getpwnam((char *)wbuf)) == NULL)
510 return (pattern);
511 else
512 h = pwd->pw_dir;
513 }
514
515 /* Copy the home directory */
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)
540 continue;
541 if (*dc != EOS)
542 return (NULL);
543
544 /* Append the rest of the pattern */
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;
557
558 return (patbuf);
559 }
560 #endif /* BUILDING_VARIANT */
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
570 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
571 const char *origpat, locale_t loc)
572 {
573 const Char *qpatnext;
574 int err;
575 size_t oldpathc;
576 Char *bufnext, c, patbuf[MAXPATHLEN];
577
578 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob, loc);
579 if (qpatnext == NULL) {
580 errno = E2BIG;
581 return (GLOB_NOSPACE);
582 }
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 ||
594 g_strchr(qpatnext+1, RBRACKET) == NULL) {
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,
623 * to ensure "**" at the end continues to match the
624 * empty string
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
639 if ((err = glob1(patbuf, pglob, limit, loc)) != 0)
640 return(err);
641
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
654 if (!(pglob->gl_flags & GLOB_NOSORT))
655 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
656 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
657
658 return (0);
659 }
660
661 #ifndef BUILDING_VARIANT
662 __private_extern__ int
663 compare(const void *p, const void *q)
664 {
665 return(strcoll(*(char **)p, *(char **)q));
666 }
667 #endif /* BUILDING_VARIANT */
668
669 static int
670 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit, locale_t loc)
671 {
672 Char pathbuf[MAXPATHLEN];
673
674 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
675 if (*pattern == EOS)
676 return (0);
677 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
678 pattern, pglob, limit, loc));
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
687 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
688 glob_t *pglob, struct glob_limit *limit, locale_t loc)
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;
701 if (g_lstat(pathbuf, &sb, pglob, loc))
702 return (0);
703
704 if ((pglob->gl_flags & GLOB_LIMIT) &&
705 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
706 errno = E2BIG;
707 return (GLOB_NOSPACE);
708 }
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 }
719 *pathend++ = SEP;
720 *pathend = EOS;
721 }
722 ++pglob->gl_matchc;
723 return (globextend(pathbuf, pglob, limit, NULL, loc));
724 }
725
726 /* Find end of next segment, copy tentatively to pathend. */
727 q = pathend;
728 p = pattern;
729 while (*p != EOS && UNPROT(*p) != SEP) {
730 if (ismeta(*p))
731 anymeta = 1;
732 if (q + 1 > pathend_last) {
733 errno = E2BIG;
734 return (GLOB_NOSPACE);
735 }
736 *q++ = *p++;
737 }
738
739 if (!anymeta) { /* No expansion, do next segment. */
740 pathend = q;
741 pattern = p;
742 while (UNPROT(*pattern) == SEP) {
743 if (pathend + 1 > pathend_last) {
744 errno = E2BIG;
745 return (GLOB_NOSPACE);
746 }
747 *pathend++ = *pattern++;
748 }
749 } else /* Need expansion, recurse. */
750 return(glob3(pathbuf, pathend, pathend_last, pattern,
751 p, pglob, limit, loc));
752 }
753 /* NOTREACHED */
754 }
755
756 static int
757 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
758 Char *pattern, Char *restpattern,
759 glob_t *pglob, struct glob_limit *limit, locale_t loc)
760 {
761 struct dirent *dp;
762 DIR *dirp;
763 int err, too_long, saverrno, saverrno2;
764 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
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
774 if (pathend > pathend_last) {
775 errno = E2BIG;
776 return (GLOB_NOSPACE);
777 }
778 *pathend = EOS;
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;
786 errno = 0;
787
788 if ((dirp = g_opendir(pathbuf, pglob, loc)) == NULL) {
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);
796 }
797
798 err = 0;
799
800 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
801 readdirfunc = pglob->gl_readdir;
802 else
803 readdirfunc = readdir;
804
805 errno = 0;
806 /* Search directory for matching names. */
807 while ((dp = (*readdirfunc)(dirp)) != NULL) {
808 char *sc;
809 Char *dc;
810 wchar_t wc;
811 size_t clen;
812 mbstate_t mbs;
813
814 if ((pglob->gl_flags & GLOB_LIMIT) &&
815 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
816 errno = E2BIG;
817 err = GLOB_NOSPACE;
818 break;
819 }
820
821 /* Initial DOT must be matched literally. */
822 if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
823 errno = 0;
824 continue;
825 }
826 memset(&mbs, 0, sizeof(mbs));
827 dc = pathend;
828 sc = dp->d_name;
829 too_long = 1;
830 while (dc <= pathend_last) {
831 clen = mbrtowc_l(&wc, sc, MB_LEN_MAX, &mbs, loc);
832 if (clen == (size_t)-1 || clen == (size_t)-2) {
833 /* XXX See initial comment #2. */
834 wc = (unsigned char)*sc;
835 clen = 1;
836 memset(&mbs, 0, sizeof(mbs));
837 }
838 if ((*dc++ = wc) == EOS) {
839 too_long = 0;
840 break;
841 }
842 sc += clen;
843 }
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)) {
850 *pathend = EOS;
851 errno = 0;
852 continue;
853 }
854 if (errno == 0)
855 errno = saverrno;
856 err = glob2(pathbuf, --dc, pathend_last, restpattern,
857 pglob, limit, loc);
858 if (err)
859 break;
860 errno = 0;
861 }
862
863 saverrno2 = errno;
864 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
865 (*pglob->gl_closedir)(dirp);
866 else
867 closedir(dirp);
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);
880 }
881
882
883 #ifndef BUILDING_VARIANT
884 /*
885 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
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 */
898 __private_extern__ int
899 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
900 const char *origpat, locale_t loc)
901 {
902 char **pathv;
903 size_t i, newn, len;
904 char *copy;
905 const Char *p;
906
907 if ((pglob->gl_flags & GLOB_LIMIT) &&
908 pglob->gl_matchc > limit->l_path_lim) {
909 errno = E2BIG;
910 return (GLOB_NOSPACE);
911 }
912
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
919 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
920 /* first time around -- clear initial gl_offs items */
921 pathv += pglob->gl_offs;
922 for (i = pglob->gl_offs + 1; --i > 0; )
923 *--pathv = NULL;
924 }
925 pglob->gl_pathv = pathv;
926
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) {
945 free(copy);
946 errno = E2BIG;
947 return (GLOB_NOSPACE);
948 }
949 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
950 }
951 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
952 return (copy == NULL ? GLOB_NOSPACE : 0);
953 }
954
955 /*
956 * pattern matching function for filenames.
957 */
958 __private_extern__ int
959 match(Char *name, Char *pat, Char *patend, locale_t loc)
960 {
961 int ok, negate_range;
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)
1001 ok = 1;
1002 if (ok == negate_range)
1003 goto fail;
1004 break;
1005 default:
1006 if (*name++ != c)
1007 goto fail;
1008 break;
1009 }
1010 }
1011 if (*name == EOS)
1012 return (1);
1013
1014 fail:
1015 if (nextn == NULL)
1016 break;
1017 pat = nextp;
1018 name = nextn;
1019 }
1020 return (0);
1021 }
1022
1023 /* Free allocated data belonging to a glob_t structure. */
1024 void
1025 globfree(glob_t *pglob)
1026 {
1027 size_t i;
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 }
1039 #endif /* !BUILDING_VARIANT */
1040
1041 static DIR *
1042 g_opendir(Char *str, glob_t *pglob, locale_t loc)
1043 {
1044 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1045
1046 if (*str == EOS)
1047 strcpy(buf, ".");
1048 else {
1049 if (g_Ctoc(str, buf, sizeof(buf), loc)) {
1050 errno = ENAMETOOLONG;
1051 return (NULL);
1052 }
1053 }
1054
1055 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1056 return ((*pglob->gl_opendir)(buf));
1057
1058 return (opendir(buf));
1059 }
1060
1061 static int
1062 g_lstat(Char *fn, struct stat *sb, glob_t *pglob, locale_t loc)
1063 {
1064 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1065
1066 if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
1067 errno = ENAMETOOLONG;
1068 return (-1);
1069 }
1070 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1071 return((*pglob->gl_lstat)(buf, sb));
1072 return (lstat(buf, sb));
1073 }
1074
1075 static int
1076 g_stat(Char *fn, struct stat *sb, glob_t *pglob, locale_t loc)
1077 {
1078 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1079
1080 if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
1081 errno = ENAMETOOLONG;
1082 return (-1);
1083 }
1084 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1085 return ((*pglob->gl_stat)(buf, sb));
1086 return (stat(buf, sb));
1087 }
1088
1089 #ifndef BUILDING_VARIANT
1090 __private_extern__ const Char *
1091 g_strchr(const Char *str, wchar_t ch)
1092 {
1093
1094 do {
1095 if (*str == ch)
1096 return (str);
1097 } while (*str++);
1098 return (NULL);
1099 }
1100
1101 __private_extern__ int
1102 g_Ctoc(const Char *str, char *buf, size_t len, locale_t loc)
1103 {
1104 mbstate_t mbs;
1105 size_t clen;
1106 int mb_cur_max = MB_CUR_MAX_L(loc);
1107
1108 memset(&mbs, 0, sizeof(mbs));
1109 while (len >= mb_cur_max) {
1110 clen = wcrtomb_l(buf, *str, &mbs, loc);
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)
1118 return (0);
1119 str++;
1120 buf += clen;
1121 len -= clen;
1122 }
1123 return (1);
1124 }
1125 #endif /* !BUILDING_VARIANT */
1126
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
1157 #ifdef DEBUG
1158 static void
1159 qprintf(const char *str, Char *s)
1160 {
1161 Char *p;
1162
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 }
1175 }
1176 #endif
1177 #pragma clang diagnostic pop