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