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