xref: /freebsd/lib/libc/gen/glob-compat11.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
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  * from: $FreeBSD$
38  */
39 
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
42 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #define	_WANT_FREEBSD11_STAT
48 #include <sys/stat.h>
49 
50 #include <ctype.h>
51 #define	_WANT_FREEBSD11_DIRENT
52 #include <dirent.h>
53 #include <errno.h>
54 #include <glob.h>
55 #include <limits.h>
56 #include <pwd.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <wchar.h>
63 
64 #include "collate.h"
65 #include "gen-compat.h"
66 #include "glob-compat11.h"
67 
68 /*
69  * glob(3) expansion limits. Stop the expansion if any of these limits
70  * is reached. This caps the runtime in the face of DoS attacks. See
71  * also CVE-2010-2632
72  */
73 #define	GLOB_LIMIT_BRACE	128	/* number of brace calls */
74 #define	GLOB_LIMIT_PATH		65536	/* number of path elements */
75 #define	GLOB_LIMIT_READDIR	16384	/* number of readdirs */
76 #define	GLOB_LIMIT_STAT		1024	/* number of stat system calls */
77 #define	GLOB_LIMIT_STRING	ARG_MAX	/* maximum total size for paths */
78 
79 struct glob_limit {
80 	size_t	l_brace_cnt;
81 	size_t	l_path_lim;
82 	size_t	l_readdir_cnt;
83 	size_t	l_stat_cnt;
84 	size_t	l_string_cnt;
85 };
86 
87 #define	DOT		L'.'
88 #define	EOS		L'\0'
89 #define	LBRACKET	L'['
90 #define	NOT		L'!'
91 #define	QUESTION	L'?'
92 #define	QUOTE		L'\\'
93 #define	RANGE		L'-'
94 #define	RBRACKET	L']'
95 #define	SEP		L'/'
96 #define	STAR		L'*'
97 #define	TILDE		L'~'
98 #define	LBRACE		L'{'
99 #define	RBRACE		L'}'
100 #define	COMMA		L','
101 
102 #define	M_QUOTE		0x8000000000ULL
103 #define	M_PROTECT	0x4000000000ULL
104 #define	M_MASK		0xffffffffffULL
105 #define	M_CHAR		0x00ffffffffULL
106 
107 typedef uint_fast64_t Char;
108 
109 #define	CHAR(c)		((Char)((c)&M_CHAR))
110 #define	META(c)		((Char)((c)|M_QUOTE))
111 #define	UNPROT(c)	((c) & ~M_PROTECT)
112 #define	M_ALL		META(L'*')
113 #define	M_END		META(L']')
114 #define	M_NOT		META(L'!')
115 #define	M_ONE		META(L'?')
116 #define	M_RNG		META(L'-')
117 #define	M_SET		META(L'[')
118 #define	ismeta(c)	(((c)&M_QUOTE) != 0)
119 #ifdef DEBUG
120 #define	isprot(c)	(((c)&M_PROTECT) != 0)
121 #endif
122 
123 static int	 compare(const void *, const void *);
124 static int	 g_Ctoc(const Char *, char *, size_t);
125 static int	 g_lstat(Char *, struct freebsd11_stat *, glob11_t *);
126 static DIR	*g_opendir(Char *, glob11_t *);
127 static const Char *g_strchr(const Char *, wchar_t);
128 #ifdef notdef
129 static Char	*g_strcat(Char *, const Char *);
130 #endif
131 static int	 g_stat(Char *, struct freebsd11_stat *, glob11_t *);
132 static int	 glob0(const Char *, glob11_t *, struct glob_limit *,
133     const char *);
134 static int	 glob1(Char *, glob11_t *, struct glob_limit *);
135 static int	 glob2(Char *, Char *, Char *, Char *, glob11_t *,
136     struct glob_limit *);
137 static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob11_t *,
138     struct glob_limit *);
139 static int	 globextend(const Char *, glob11_t *, struct glob_limit *,
140     const char *);
141 static const Char *
142 		 globtilde(const Char *, Char *, size_t, glob11_t *);
143 static int	 globexp0(const Char *, glob11_t *, struct glob_limit *,
144     const char *);
145 static int	 globexp1(const Char *, glob11_t *, struct glob_limit *);
146 static int	 globexp2(const Char *, const Char *, glob11_t *,
147     struct glob_limit *);
148 static int	 globfinal(glob11_t *, struct glob_limit *, size_t,
149     const char *);
150 static int	 match(Char *, Char *, Char *);
151 static int	 err_nomatch(glob11_t *, struct glob_limit *, const char *);
152 static int	 err_aborted(glob11_t *, int, char *);
153 #ifdef DEBUG
154 static void	 qprintf(const char *, Char *);
155 #endif
156 
157 int
158 freebsd11_glob(const char * __restrict pattern, int flags,
159 	 int (*errfunc)(const char *, int), glob11_t * __restrict pglob)
160 {
161 	struct glob_limit limit = { 0, 0, 0, 0, 0 };
162 	const char *patnext;
163 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
164 	mbstate_t mbs;
165 	wchar_t wc;
166 	size_t clen;
167 	int too_long;
168 
169 	patnext = pattern;
170 	if (!(flags & GLOB_APPEND)) {
171 		pglob->gl_pathc = 0;
172 		pglob->gl_pathv = NULL;
173 		if (!(flags & GLOB_DOOFFS))
174 			pglob->gl_offs = 0;
175 	}
176 	if (flags & GLOB_LIMIT) {
177 		limit.l_path_lim = pglob->gl_matchc;
178 		if (limit.l_path_lim == 0)
179 			limit.l_path_lim = GLOB_LIMIT_PATH;
180 	}
181 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
182 	pglob->gl_errfunc = errfunc;
183 	pglob->gl_matchc = 0;
184 
185 	bufnext = patbuf;
186 	bufend = bufnext + MAXPATHLEN - 1;
187 	too_long = 1;
188 	if (flags & GLOB_NOESCAPE) {
189 		memset(&mbs, 0, sizeof(mbs));
190 		while (bufnext <= bufend) {
191 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
192 			if (clen == (size_t)-1 || clen == (size_t)-2)
193 				return (err_nomatch(pglob, &limit, pattern));
194 			else if (clen == 0) {
195 				too_long = 0;
196 				break;
197 			}
198 			*bufnext++ = wc;
199 			patnext += clen;
200 		}
201 	} else {
202 		/* Protect the quoted characters. */
203 		memset(&mbs, 0, sizeof(mbs));
204 		while (bufnext <= bufend) {
205 			if (*patnext == '\\') {
206 				if (*++patnext == '\0') {
207 					*bufnext++ = QUOTE;
208 					continue;
209 				}
210 				prot = M_PROTECT;
211 			} else
212 				prot = 0;
213 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
214 			if (clen == (size_t)-1 || clen == (size_t)-2)
215 				return (err_nomatch(pglob, &limit, pattern));
216 			else if (clen == 0) {
217 				too_long = 0;
218 				break;
219 			}
220 			*bufnext++ = wc | prot;
221 			patnext += clen;
222 		}
223 	}
224 	if (too_long)
225 		return (err_nomatch(pglob, &limit, pattern));
226 	*bufnext = EOS;
227 
228 	if (flags & GLOB_BRACE)
229 	    return (globexp0(patbuf, pglob, &limit, pattern));
230 	else
231 	    return (glob0(patbuf, pglob, &limit, pattern));
232 }
233 
234 static int
235 globexp0(const Char *pattern, glob11_t *pglob, struct glob_limit *limit,
236     const char *origpat) {
237 	int rv;
238 	size_t oldpathc;
239 
240 	/* Protect a single {}, for find(1), like csh */
241 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
242 		if ((pglob->gl_flags & GLOB_LIMIT) &&
243 		    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
244 			errno = E2BIG;
245 			return (GLOB_NOSPACE);
246 		}
247 		return (glob0(pattern, pglob, limit, origpat));
248 	}
249 
250 	oldpathc = pglob->gl_pathc;
251 
252 	if ((rv = globexp1(pattern, pglob, limit)) != 0)
253 		return rv;
254 
255 	return (globfinal(pglob, limit, oldpathc, origpat));
256 }
257 
258 /*
259  * Expand recursively a glob {} pattern. When there is no more expansion
260  * invoke the standard globbing routine to glob the rest of the magic
261  * characters
262  */
263 static int
264 globexp1(const Char *pattern, glob11_t *pglob, struct glob_limit *limit)
265 {
266 	const Char* ptr;
267 
268 	if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
269 		if ((pglob->gl_flags & GLOB_LIMIT) &&
270 		    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
271 			errno = E2BIG;
272 			return (GLOB_NOSPACE);
273 		}
274 		return (globexp2(ptr, pattern, pglob, limit));
275 	}
276 
277 	return (glob0(pattern, pglob, limit, NULL));
278 }
279 
280 
281 /*
282  * Recursive brace globbing helper. Tries to expand a single brace.
283  * If it succeeds then it invokes globexp1 with the new pattern.
284  * If it fails then it tries to glob the rest of the pattern and returns.
285  */
286 static int
287 globexp2(const Char *ptr, const Char *pattern, glob11_t *pglob,
288     struct glob_limit *limit)
289 {
290 	int     i, rv;
291 	Char   *lm, *ls;
292 	const Char *pe, *pm, *pm1, *pl;
293 	Char    patbuf[MAXPATHLEN];
294 
295 	/* copy part up to the brace */
296 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
297 		continue;
298 	*lm = EOS;
299 	ls = lm;
300 
301 	/* Find the balanced brace */
302 	for (i = 0, pe = ++ptr; *pe != EOS; pe++)
303 		if (*pe == LBRACKET) {
304 			/* Ignore everything between [] */
305 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
306 				continue;
307 			if (*pe == EOS) {
308 				/*
309 				 * We could not find a matching RBRACKET.
310 				 * Ignore and just look for RBRACE
311 				 */
312 				pe = pm;
313 			}
314 		}
315 		else if (*pe == LBRACE)
316 			i++;
317 		else if (*pe == RBRACE) {
318 			if (i == 0)
319 				break;
320 			i--;
321 		}
322 
323 	/* Non matching braces; just glob the pattern */
324 	if (i != 0 || *pe == EOS)
325 		return (glob0(pattern, pglob, limit, NULL));
326 
327 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
328 		switch (*pm) {
329 		case LBRACKET:
330 			/* Ignore everything between [] */
331 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
332 				continue;
333 			if (*pm == EOS) {
334 				/*
335 				 * We could not find a matching RBRACKET.
336 				 * Ignore and just look for RBRACE
337 				 */
338 				pm = pm1;
339 			}
340 			break;
341 
342 		case LBRACE:
343 			i++;
344 			break;
345 
346 		case RBRACE:
347 			if (i) {
348 			    i--;
349 			    break;
350 			}
351 			/* FALLTHROUGH */
352 		case COMMA:
353 			if (i && *pm == COMMA)
354 				break;
355 			else {
356 				/* Append the current string */
357 				for (lm = ls; (pl < pm); *lm++ = *pl++)
358 					continue;
359 				/*
360 				 * Append the rest of the pattern after the
361 				 * closing brace
362 				 */
363 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
364 					continue;
365 
366 				/* Expand the current pattern */
367 #ifdef DEBUG
368 				qprintf("globexp2:", patbuf);
369 #endif
370 				rv = globexp1(patbuf, pglob, limit);
371 				if (rv)
372 					return (rv);
373 
374 				/* move after the comma, to the next string */
375 				pl = pm + 1;
376 			}
377 			break;
378 
379 		default:
380 			break;
381 		}
382 	return (0);
383 }
384 
385 
386 
387 /*
388  * expand tilde from the passwd file.
389  */
390 static const Char *
391 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob11_t *pglob)
392 {
393 	struct passwd *pwd;
394 	char *h, *sc;
395 	const Char *p;
396 	Char *b, *eb;
397 	wchar_t wc;
398 	wchar_t wbuf[MAXPATHLEN];
399 	wchar_t *wbufend, *dc;
400 	size_t clen;
401 	mbstate_t mbs;
402 	int too_long;
403 
404 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
405 		return (pattern);
406 
407 	/*
408 	 * Copy up to the end of the string or /
409 	 */
410 	eb = &patbuf[patbuf_len - 1];
411 	for (p = pattern + 1, b = patbuf;
412 	    b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
413 		continue;
414 
415 	if (*p != EOS && UNPROT(*p) != SEP)
416 		return (NULL);
417 
418 	*b = EOS;
419 	h = NULL;
420 
421 	if (patbuf[0] == EOS) {
422 		/*
423 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
424 		 * we're not running setuid or setgid) and then trying
425 		 * the password file
426 		 */
427 		if (issetugid() != 0 ||
428 		    (h = getenv("HOME")) == NULL) {
429 			if (((h = getlogin()) != NULL &&
430 			     (pwd = getpwnam(h)) != NULL) ||
431 			    (pwd = getpwuid(getuid())) != NULL)
432 				h = pwd->pw_dir;
433 			else
434 				return (pattern);
435 		}
436 	}
437 	else {
438 		/*
439 		 * Expand a ~user
440 		 */
441 		if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
442 			return (NULL);
443 		if ((pwd = getpwnam((char *)wbuf)) == NULL)
444 			return (pattern);
445 		else
446 			h = pwd->pw_dir;
447 	}
448 
449 	/* Copy the home directory */
450 	dc = wbuf;
451 	sc = h;
452 	wbufend = wbuf + MAXPATHLEN - 1;
453 	too_long = 1;
454 	memset(&mbs, 0, sizeof(mbs));
455 	while (dc <= wbufend) {
456 		clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
457 		if (clen == (size_t)-1 || clen == (size_t)-2) {
458 			/* XXX See initial comment #2. */
459 			wc = (unsigned char)*sc;
460 			clen = 1;
461 			memset(&mbs, 0, sizeof(mbs));
462 		}
463 		if ((*dc++ = wc) == EOS) {
464 			too_long = 0;
465 			break;
466 		}
467 		sc += clen;
468 	}
469 	if (too_long)
470 		return (NULL);
471 
472 	dc = wbuf;
473 	for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
474 		continue;
475 	if (*dc != EOS)
476 		return (NULL);
477 
478 	/* Append the rest of the pattern */
479 	if (*p != EOS) {
480 		too_long = 1;
481 		while (b <= eb) {
482 			if ((*b++ = *p++) == EOS) {
483 				too_long = 0;
484 				break;
485 			}
486 		}
487 		if (too_long)
488 			return (NULL);
489 	} else
490 		*b = EOS;
491 
492 	return (patbuf);
493 }
494 
495 
496 /*
497  * The main glob() routine: compiles the pattern (optionally processing
498  * quotes), calls glob1() to do the real pattern matching, and finally
499  * sorts the list (unless unsorted operation is requested).  Returns 0
500  * if things went well, nonzero if errors occurred.
501  */
502 static int
503 glob0(const Char *pattern, glob11_t *pglob, struct glob_limit *limit,
504     const char *origpat) {
505 	const Char *qpatnext;
506 	int err;
507 	size_t oldpathc;
508 	Char *bufnext, c, patbuf[MAXPATHLEN];
509 
510 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
511 	if (qpatnext == NULL) {
512 		errno = E2BIG;
513 		return (GLOB_NOSPACE);
514 	}
515 	oldpathc = pglob->gl_pathc;
516 	bufnext = patbuf;
517 
518 	/* We don't need to check for buffer overflow any more. */
519 	while ((c = *qpatnext++) != EOS) {
520 		switch (c) {
521 		case LBRACKET:
522 			c = *qpatnext;
523 			if (c == NOT)
524 				++qpatnext;
525 			if (*qpatnext == EOS ||
526 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
527 				*bufnext++ = LBRACKET;
528 				if (c == NOT)
529 					--qpatnext;
530 				break;
531 			}
532 			*bufnext++ = M_SET;
533 			if (c == NOT)
534 				*bufnext++ = M_NOT;
535 			c = *qpatnext++;
536 			do {
537 				*bufnext++ = CHAR(c);
538 				if (*qpatnext == RANGE &&
539 				    (c = qpatnext[1]) != RBRACKET) {
540 					*bufnext++ = M_RNG;
541 					*bufnext++ = CHAR(c);
542 					qpatnext += 2;
543 				}
544 			} while ((c = *qpatnext++) != RBRACKET);
545 			pglob->gl_flags |= GLOB_MAGCHAR;
546 			*bufnext++ = M_END;
547 			break;
548 		case QUESTION:
549 			pglob->gl_flags |= GLOB_MAGCHAR;
550 			*bufnext++ = M_ONE;
551 			break;
552 		case STAR:
553 			pglob->gl_flags |= GLOB_MAGCHAR;
554 			/* collapse adjacent stars to one,
555 			 * to avoid exponential behavior
556 			 */
557 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
558 			    *bufnext++ = M_ALL;
559 			break;
560 		default:
561 			*bufnext++ = CHAR(c);
562 			break;
563 		}
564 	}
565 	*bufnext = EOS;
566 #ifdef DEBUG
567 	qprintf("glob0:", patbuf);
568 #endif
569 
570 	if ((err = glob1(patbuf, pglob, limit)) != 0)
571 		return(err);
572 
573 	if (origpat != NULL)
574 		return (globfinal(pglob, limit, oldpathc, origpat));
575 
576 	return (0);
577 }
578 
579 static int
580 globfinal(glob11_t *pglob, struct glob_limit *limit, size_t oldpathc,
581     const char *origpat) {
582 	if (pglob->gl_pathc == oldpathc)
583 		return (err_nomatch(pglob, limit, origpat));
584 
585 	if (!(pglob->gl_flags & GLOB_NOSORT))
586 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
587 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
588 
589 	return (0);
590 }
591 
592 static int
593 compare(const void *p, const void *q)
594 {
595 	return (strcoll(*(char **)p, *(char **)q));
596 }
597 
598 static int
599 glob1(Char *pattern, glob11_t *pglob, struct glob_limit *limit)
600 {
601 	Char pathbuf[MAXPATHLEN];
602 
603 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
604 	if (*pattern == EOS)
605 		return (0);
606 	return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
607 	    pattern, pglob, limit));
608 }
609 
610 /*
611  * The functions glob2 and glob3 are mutually recursive; there is one level
612  * of recursion for each segment in the pattern that contains one or more
613  * meta characters.
614  */
615 static int
616 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
617       glob11_t *pglob, struct glob_limit *limit)
618 {
619 	struct freebsd11_stat sb;
620 	Char *p, *q;
621 	int anymeta;
622 
623 	/*
624 	 * Loop over pattern segments until end of pattern or until
625 	 * segment with meta character found.
626 	 */
627 	for (anymeta = 0;;) {
628 		if (*pattern == EOS) {		/* End of pattern? */
629 			*pathend = EOS;
630 			if (g_lstat(pathbuf, &sb, pglob))
631 				return (0);
632 
633 			if ((pglob->gl_flags & GLOB_LIMIT) &&
634 			    limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
635 				errno = E2BIG;
636 				return (GLOB_NOSPACE);
637 			}
638 			if ((pglob->gl_flags & GLOB_MARK) &&
639 			    UNPROT(pathend[-1]) != SEP &&
640 			    (S_ISDIR(sb.st_mode) ||
641 			    (S_ISLNK(sb.st_mode) &&
642 			    g_stat(pathbuf, &sb, pglob) == 0 &&
643 			    S_ISDIR(sb.st_mode)))) {
644 				if (pathend + 1 > pathend_last) {
645 					errno = E2BIG;
646 					return (GLOB_NOSPACE);
647 				}
648 				*pathend++ = SEP;
649 				*pathend = EOS;
650 			}
651 			++pglob->gl_matchc;
652 			return (globextend(pathbuf, pglob, limit, NULL));
653 		}
654 
655 		/* Find end of next segment, copy tentatively to pathend. */
656 		q = pathend;
657 		p = pattern;
658 		while (*p != EOS && UNPROT(*p) != SEP) {
659 			if (ismeta(*p))
660 				anymeta = 1;
661 			if (q + 1 > pathend_last) {
662 				errno = E2BIG;
663 				return (GLOB_NOSPACE);
664 			}
665 			*q++ = *p++;
666 		}
667 
668 		if (!anymeta) {		/* No expansion, do next segment. */
669 			pathend = q;
670 			pattern = p;
671 			while (UNPROT(*pattern) == SEP) {
672 				if (pathend + 1 > pathend_last) {
673 					errno = E2BIG;
674 					return (GLOB_NOSPACE);
675 				}
676 				*pathend++ = *pattern++;
677 			}
678 		} else			/* Need expansion, recurse. */
679 			return (glob3(pathbuf, pathend, pathend_last, pattern,
680 			    p, pglob, limit));
681 	}
682 	/* NOTREACHED */
683 }
684 
685 static int
686 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
687       Char *pattern, Char *restpattern,
688       glob11_t *pglob, struct glob_limit *limit)
689 {
690 	struct freebsd11_dirent *dp;
691 	DIR *dirp;
692 	int err, too_long, saverrno, saverrno2;
693 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
694 
695 	struct freebsd11_dirent *(*readdirfunc)(DIR *);
696 
697 	if (pathend > pathend_last) {
698 		errno = E2BIG;
699 		return (GLOB_NOSPACE);
700 	}
701 	*pathend = EOS;
702 	if (pglob->gl_errfunc != NULL &&
703 	    g_Ctoc(pathbuf, buf, sizeof(buf))) {
704 		errno = E2BIG;
705 		return (GLOB_NOSPACE);
706 	}
707 
708 	saverrno = errno;
709 	errno = 0;
710 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
711 		if (errno == ENOENT || errno == ENOTDIR)
712 			return (0);
713 		err = err_aborted(pglob, errno, buf);
714 		if (errno == 0)
715 			errno = saverrno;
716 		return (err);
717 	}
718 
719 	err = 0;
720 
721 	/* pglob->gl_readdir takes a void *, fix this manually */
722 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
723 		readdirfunc =
724 		    (struct freebsd11_dirent *(*)(DIR *))pglob->gl_readdir;
725 	else
726 		readdirfunc = freebsd11_readdir;
727 
728 	errno = 0;
729 	/* Search directory for matching names. */
730 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
731 		char *sc;
732 		Char *dc;
733 		wchar_t wc;
734 		size_t clen;
735 		mbstate_t mbs;
736 
737 		if ((pglob->gl_flags & GLOB_LIMIT) &&
738 		    limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
739 			errno = E2BIG;
740 			err = GLOB_NOSPACE;
741 			break;
742 		}
743 
744 		/* Initial DOT must be matched literally. */
745 		if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
746 			errno = 0;
747 			continue;
748 		}
749 		memset(&mbs, 0, sizeof(mbs));
750 		dc = pathend;
751 		sc = dp->d_name;
752 		too_long = 1;
753 		while (dc <= pathend_last) {
754 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
755 			if (clen == (size_t)-1 || clen == (size_t)-2) {
756 				/* XXX See initial comment #2. */
757 				wc = (unsigned char)*sc;
758 				clen = 1;
759 				memset(&mbs, 0, sizeof(mbs));
760 			}
761 			if ((*dc++ = wc) == EOS) {
762 				too_long = 0;
763 				break;
764 			}
765 			sc += clen;
766 		}
767 		if (too_long && (err = err_aborted(pglob, ENAMETOOLONG,
768 		    buf))) {
769 			errno = ENAMETOOLONG;
770 			break;
771 		}
772 		if (too_long || !match(pathend, pattern, restpattern)) {
773 			*pathend = EOS;
774 			errno = 0;
775 			continue;
776 		}
777 		if (errno == 0)
778 			errno = saverrno;
779 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
780 		    pglob, limit);
781 		if (err)
782 			break;
783 		errno = 0;
784 	}
785 
786 	saverrno2 = errno;
787 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
788 		(*pglob->gl_closedir)(dirp);
789 	else
790 		closedir(dirp);
791 	errno = saverrno2;
792 
793 	if (err)
794 		return (err);
795 
796 	if (dp == NULL && errno != 0 &&
797 	    (err = err_aborted(pglob, errno, buf)))
798 		return (err);
799 
800 	if (errno == 0)
801 		errno = saverrno;
802 	return (0);
803 }
804 
805 
806 /*
807  * Extend the gl_pathv member of a glob11_t structure to accommodate a new item,
808  * add the new item, and update gl_pathc.
809  *
810  * This assumes the BSD realloc, which only copies the block when its size
811  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
812  * behavior.
813  *
814  * Return 0 if new item added, error code if memory couldn't be allocated.
815  *
816  * Invariant of the glob11_t structure:
817  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
818  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
819  */
820 static int
821 globextend(const Char *path, glob11_t *pglob, struct glob_limit *limit,
822     const char *origpat)
823 {
824 	char **pathv;
825 	size_t i, newn, len;
826 	char *copy;
827 	const Char *p;
828 
829 	if ((pglob->gl_flags & GLOB_LIMIT) &&
830 	    pglob->gl_matchc > limit->l_path_lim) {
831 		errno = E2BIG;
832 		return (GLOB_NOSPACE);
833 	}
834 
835 	newn = 2 + pglob->gl_pathc + pglob->gl_offs;
836 	/* reallocarray(NULL, newn, size) is equivalent to malloc(newn*size). */
837 	pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
838 	if (pathv == NULL)
839 		return (GLOB_NOSPACE);
840 
841 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
842 		/* first time around -- clear initial gl_offs items */
843 		pathv += pglob->gl_offs;
844 		for (i = pglob->gl_offs + 1; --i > 0; )
845 			*--pathv = NULL;
846 	}
847 	pglob->gl_pathv = pathv;
848 
849 	if (origpat != NULL)
850 		copy = strdup(origpat);
851 	else {
852 		for (p = path; *p++ != EOS;)
853 			continue;
854 		len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
855 		if ((copy = malloc(len)) != NULL) {
856 			if (g_Ctoc(path, copy, len)) {
857 				free(copy);
858 				errno = E2BIG;
859 				return (GLOB_NOSPACE);
860 			}
861 		}
862 	}
863 	if (copy != NULL) {
864 		limit->l_string_cnt += strlen(copy) + 1;
865 		if ((pglob->gl_flags & GLOB_LIMIT) &&
866 		    limit->l_string_cnt >= GLOB_LIMIT_STRING) {
867 			free(copy);
868 			errno = E2BIG;
869 			return (GLOB_NOSPACE);
870 		}
871 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
872 	}
873 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
874 	return (copy == NULL ? GLOB_NOSPACE : 0);
875 }
876 
877 /*
878  * pattern matching function for filenames.
879  */
880 static int
881 match(Char *name, Char *pat, Char *patend)
882 {
883 	int ok, negate_range;
884 	Char c, k, *nextp, *nextn;
885 	struct xlocale_collate *table =
886 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
887 
888 	nextn = NULL;
889 	nextp = NULL;
890 
891 	while (1) {
892 		while (pat < patend) {
893 			c = *pat++;
894 			switch (c & M_MASK) {
895 			case M_ALL:
896 				if (pat == patend)
897 					return (1);
898 				if (*name == EOS)
899 					return (0);
900 				nextn = name + 1;
901 				nextp = pat - 1;
902 				break;
903 			case M_ONE:
904 				if (*name++ == EOS)
905 					goto fail;
906 				break;
907 			case M_SET:
908 				ok = 0;
909 				if ((k = *name++) == EOS)
910 					goto fail;
911 				negate_range = ((*pat & M_MASK) == M_NOT);
912 				if (negate_range != 0)
913 					++pat;
914 				while (((c = *pat++) & M_MASK) != M_END)
915 					if ((*pat & M_MASK) == M_RNG) {
916 						if (table->__collate_load_error ?
917 						    CHAR(c) <= CHAR(k) &&
918 						    CHAR(k) <= CHAR(pat[1]) :
919 						    __wcollate_range_cmp(CHAR(c),
920 						    CHAR(k)) <= 0 &&
921 						    __wcollate_range_cmp(CHAR(k),
922 						    CHAR(pat[1])) <= 0)
923 							ok = 1;
924 						pat += 2;
925 					} else if (c == k)
926 						ok = 1;
927 				if (ok == negate_range)
928 					goto fail;
929 				break;
930 			default:
931 				if (*name++ != c)
932 					goto fail;
933 				break;
934 			}
935 		}
936 		if (*name == EOS)
937 			return (1);
938 
939 	fail:
940 		if (nextn == NULL)
941 			break;
942 		pat = nextp;
943 		name = nextn;
944 	}
945 	return (0);
946 }
947 
948 /* Free allocated data belonging to a glob11_t structure. */
949 void
950 freebsd11_globfree(glob11_t *pglob)
951 {
952 	size_t i;
953 	char **pp;
954 
955 	if (pglob->gl_pathv != NULL) {
956 		pp = pglob->gl_pathv + pglob->gl_offs;
957 		for (i = pglob->gl_pathc; i--; ++pp)
958 			if (*pp)
959 				free(*pp);
960 		free(pglob->gl_pathv);
961 		pglob->gl_pathv = NULL;
962 	}
963 }
964 
965 static DIR *
966 g_opendir(Char *str, glob11_t *pglob)
967 {
968 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
969 
970 	if (*str == EOS)
971 		strcpy(buf, ".");
972 	else {
973 		if (g_Ctoc(str, buf, sizeof(buf))) {
974 			errno = ENAMETOOLONG;
975 			return (NULL);
976 		}
977 	}
978 
979 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
980 		return ((*pglob->gl_opendir)(buf));
981 
982 	return (opendir(buf));
983 }
984 
985 static int
986 g_lstat(Char *fn, struct freebsd11_stat *sb, glob11_t *pglob)
987 {
988 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
989 
990 	if (g_Ctoc(fn, buf, sizeof(buf))) {
991 		errno = ENAMETOOLONG;
992 		return (-1);
993 	}
994 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
995 		return((*pglob->gl_lstat)(buf, sb));
996 	return (freebsd11_lstat(buf, sb));
997 }
998 
999 static int
1000 g_stat(Char *fn, struct freebsd11_stat *sb, glob11_t *pglob)
1001 {
1002 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1003 
1004 	if (g_Ctoc(fn, buf, sizeof(buf))) {
1005 		errno = ENAMETOOLONG;
1006 		return (-1);
1007 	}
1008 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1009 		return ((*pglob->gl_stat)(buf, sb));
1010 	return (freebsd11_stat(buf, sb));
1011 }
1012 
1013 static const Char *
1014 g_strchr(const Char *str, wchar_t ch)
1015 {
1016 
1017 	do {
1018 		if (*str == ch)
1019 			return (str);
1020 	} while (*str++);
1021 	return (NULL);
1022 }
1023 
1024 static int
1025 g_Ctoc(const Char *str, char *buf, size_t len)
1026 {
1027 	mbstate_t mbs;
1028 	size_t clen;
1029 
1030 	memset(&mbs, 0, sizeof(mbs));
1031 	while (len >= MB_CUR_MAX) {
1032 		clen = wcrtomb(buf, CHAR(*str), &mbs);
1033 		if (clen == (size_t)-1) {
1034 			/* XXX See initial comment #2. */
1035 			*buf = (char)CHAR(*str);
1036 			clen = 1;
1037 			memset(&mbs, 0, sizeof(mbs));
1038 		}
1039 		if (CHAR(*str) == EOS)
1040 			return (0);
1041 		str++;
1042 		buf += clen;
1043 		len -= clen;
1044 	}
1045 	return (1);
1046 }
1047 
1048 static int
1049 err_nomatch(glob11_t *pglob, struct glob_limit *limit, const char *origpat) {
1050 	/*
1051 	 * If there was no match we are going to append the origpat
1052 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
1053 	 * and the origpat did not contain any magic characters
1054 	 * GLOB_NOMAGIC is there just for compatibility with csh.
1055 	 */
1056 	if ((pglob->gl_flags & GLOB_NOCHECK) ||
1057 	    ((pglob->gl_flags & GLOB_NOMAGIC) &&
1058 	    !(pglob->gl_flags & GLOB_MAGCHAR)))
1059 		return (globextend(NULL, pglob, limit, origpat));
1060 	return (GLOB_NOMATCH);
1061 }
1062 
1063 static int
1064 err_aborted(glob11_t *pglob, int err, char *buf) {
1065 	if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) ||
1066 	    (pglob->gl_flags & GLOB_ERR))
1067 		return (GLOB_ABORTED);
1068 	return (0);
1069 }
1070 
1071 #ifdef DEBUG
1072 static void
1073 qprintf(const char *str, Char *s)
1074 {
1075 	Char *p;
1076 
1077 	(void)printf("%s\n", str);
1078 	if (s != NULL) {
1079 		for (p = s; *p != EOS; p++)
1080 			(void)printf("%c", (char)CHAR(*p));
1081 		(void)printf("\n");
1082 		for (p = s; *p != EOS; p++)
1083 			(void)printf("%c", (isprot(*p) ? '\\' : ' '));
1084 		(void)printf("\n");
1085 		for (p = s; *p != EOS; p++)
1086 			(void)printf("%c", (ismeta(*p) ? '_' : ' '));
1087 		(void)printf("\n");
1088 	}
1089 }
1090 #endif
1091 
1092 __sym_compat(glob, freebsd11_glob, FBSD_1.0);
1093 __sym_compat(globfree, freebsd11_globfree, FBSD_1.0);
1094