xref: /freebsd/lib/libc/gen/glob.c (revision 869eb80c1625f067f5c29996b1dd483e3be845b7)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1989, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Guido van Rossum.
758f0484fSRodney W. Grimes  *
83c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
93c87aa1dSDavid Chisnall  * All rights reserved.
103c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
113c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
123c87aa1dSDavid Chisnall  *
1358f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1458f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1558f0484fSRodney W. Grimes  * are met:
1658f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1758f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1858f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
2058f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  */
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
4058f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
41b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
42b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes /*
4558f0484fSRodney W. Grimes  * glob(3) -- a superset of the one defined in POSIX 1003.2.
4658f0484fSRodney W. Grimes  *
4758f0484fSRodney W. Grimes  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
4858f0484fSRodney W. Grimes  *
4958f0484fSRodney W. Grimes  * Optional extra services, controlled by flags not defined by POSIX:
5058f0484fSRodney W. Grimes  *
5158f0484fSRodney W. Grimes  * GLOB_QUOTE:
5258f0484fSRodney W. Grimes  *	Escaping convention: \ inhibits any special meaning the following
5358f0484fSRodney W. Grimes  *	character might have (except \ at end of string is retained).
5458f0484fSRodney W. Grimes  * GLOB_MAGCHAR:
5558f0484fSRodney W. Grimes  *	Set in gl_flags if pattern contained a globbing character.
5658f0484fSRodney W. Grimes  * GLOB_NOMAGIC:
5758f0484fSRodney W. Grimes  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
5858f0484fSRodney W. Grimes  *	not contain any magic characters.  [Used in csh style globbing]
5958f0484fSRodney W. Grimes  * GLOB_ALTDIRFUNC:
6058f0484fSRodney W. Grimes  *	Use alternately specified directory access functions.
6158f0484fSRodney W. Grimes  * GLOB_TILDE:
6258f0484fSRodney W. Grimes  *	expand ~user/foo to the /home/dir/of/user/foo
6358f0484fSRodney W. Grimes  * GLOB_BRACE:
6458f0484fSRodney W. Grimes  *	expand {1,2}{a,b} to 1a 1b 2a 2b
6558f0484fSRodney W. Grimes  * gl_matchc:
6658f0484fSRodney W. Grimes  *	Number of matches in the current invocation of glob.
6758f0484fSRodney W. Grimes  */
6858f0484fSRodney W. Grimes 
69e9346e01STim J. Robbins /*
70e9346e01STim J. Robbins  * Some notes on multibyte character support:
71e9346e01STim J. Robbins  * 1. Patterns with illegal byte sequences match nothing - even if
72e9346e01STim J. Robbins  *    GLOB_NOCHECK is specified.
73e9346e01STim J. Robbins  * 2. Illegal byte sequences in filenames are handled by treating them as
74aa3d69a6SAndrey A. Chernov  *    single-byte characters with a values of such bytes of the sequence
75e9346e01STim J. Robbins  *    cast to wchar_t.
76e9346e01STim J. Robbins  * 3. State-dependent encodings are not currently supported.
77e9346e01STim J. Robbins  */
78e9346e01STim J. Robbins 
7958f0484fSRodney W. Grimes #include <sys/param.h>
8058f0484fSRodney W. Grimes #include <sys/stat.h>
8158f0484fSRodney W. Grimes 
8258f0484fSRodney W. Grimes #include <ctype.h>
8358f0484fSRodney W. Grimes #include <dirent.h>
8458f0484fSRodney W. Grimes #include <errno.h>
8558f0484fSRodney W. Grimes #include <glob.h>
86e9346e01STim J. Robbins #include <limits.h>
8758f0484fSRodney W. Grimes #include <pwd.h>
88e9346e01STim J. Robbins #include <stdint.h>
8958f0484fSRodney W. Grimes #include <stdio.h>
9058f0484fSRodney W. Grimes #include <stdlib.h>
9158f0484fSRodney W. Grimes #include <string.h>
9258f0484fSRodney W. Grimes #include <unistd.h>
93e9346e01STim J. Robbins #include <wchar.h>
9458f0484fSRodney W. Grimes 
951daad8f5SAndrey A. Chernov #include "collate.h"
961daad8f5SAndrey A. Chernov 
97daab0b01SMarcel Moolenaar /*
98daab0b01SMarcel Moolenaar  * glob(3) expansion limits. Stop the expansion if any of these limits
99daab0b01SMarcel Moolenaar  * is reached. This caps the runtime in the face of DoS attacks. See
100daab0b01SMarcel Moolenaar  * also CVE-2010-2632
101daab0b01SMarcel Moolenaar  */
102daab0b01SMarcel Moolenaar #define	GLOB_LIMIT_BRACE	128	/* number of brace calls */
103daab0b01SMarcel Moolenaar #define	GLOB_LIMIT_PATH		65536	/* number of path elements */
104daab0b01SMarcel Moolenaar #define	GLOB_LIMIT_READDIR	16384	/* number of readdirs */
105daab0b01SMarcel Moolenaar #define	GLOB_LIMIT_STAT		1024	/* number of stat system calls */
106daab0b01SMarcel Moolenaar #define	GLOB_LIMIT_STRING	ARG_MAX	/* maximum total size for paths */
107daab0b01SMarcel Moolenaar 
108daab0b01SMarcel Moolenaar struct glob_limit {
109daab0b01SMarcel Moolenaar 	size_t	l_brace_cnt;
110daab0b01SMarcel Moolenaar 	size_t	l_path_lim;
111daab0b01SMarcel Moolenaar 	size_t	l_readdir_cnt;
112daab0b01SMarcel Moolenaar 	size_t	l_stat_cnt;
113daab0b01SMarcel Moolenaar 	size_t	l_string_cnt;
114daab0b01SMarcel Moolenaar };
115daab0b01SMarcel Moolenaar 
116aa3d69a6SAndrey A. Chernov #define	DOT		L'.'
117aa3d69a6SAndrey A. Chernov #define	EOS		L'\0'
118aa3d69a6SAndrey A. Chernov #define	LBRACKET	L'['
119aa3d69a6SAndrey A. Chernov #define	NOT		L'!'
120aa3d69a6SAndrey A. Chernov #define	QUESTION	L'?'
121aa3d69a6SAndrey A. Chernov #define	QUOTE		L'\\'
122aa3d69a6SAndrey A. Chernov #define	RANGE		L'-'
123aa3d69a6SAndrey A. Chernov #define	RBRACKET	L']'
124aa3d69a6SAndrey A. Chernov #define	SEP		L'/'
125aa3d69a6SAndrey A. Chernov #define	STAR		L'*'
126aa3d69a6SAndrey A. Chernov #define	TILDE		L'~'
127aa3d69a6SAndrey A. Chernov #define	LBRACE		L'{'
128aa3d69a6SAndrey A. Chernov #define	RBRACE		L'}'
129aa3d69a6SAndrey A. Chernov #define	COMMA		L','
13058f0484fSRodney W. Grimes 
131e9346e01STim J. Robbins #define	M_QUOTE		0x8000000000ULL
132e9346e01STim J. Robbins #define	M_PROTECT	0x4000000000ULL
133e9346e01STim J. Robbins #define	M_MASK		0xffffffffffULL
134e9346e01STim J. Robbins #define	M_CHAR		0x00ffffffffULL
13558f0484fSRodney W. Grimes 
136e9346e01STim J. Robbins typedef uint_fast64_t Char;
13758f0484fSRodney W. Grimes 
138e9346e01STim J. Robbins #define	CHAR(c)		((Char)((c)&M_CHAR))
13958f0484fSRodney W. Grimes #define	META(c)		((Char)((c)|M_QUOTE))
140aed721ecSAndrey A. Chernov #define	UNPROT(c)	((c) & ~M_PROTECT)
141aa3d69a6SAndrey A. Chernov #define	M_ALL		META(L'*')
142aa3d69a6SAndrey A. Chernov #define	M_END		META(L']')
143aa3d69a6SAndrey A. Chernov #define	M_NOT		META(L'!')
144aa3d69a6SAndrey A. Chernov #define	M_ONE		META(L'?')
145aa3d69a6SAndrey A. Chernov #define	M_RNG		META(L'-')
146aa3d69a6SAndrey A. Chernov #define	M_SET		META(L'[')
14758f0484fSRodney W. Grimes #define	ismeta(c)	(((c)&M_QUOTE) != 0)
14809264d74SAndrey A. Chernov #ifdef DEBUG
149aed721ecSAndrey A. Chernov #define	isprot(c)	(((c)&M_PROTECT) != 0)
15009264d74SAndrey A. Chernov #endif
15158f0484fSRodney W. Grimes 
152b231cb39SDavid E. O'Brien static int	 compare(const void *, const void *);
15309264d74SAndrey A. Chernov static int	 g_Ctoc(const Char *, char *, size_t);
154b231cb39SDavid E. O'Brien static int	 g_lstat(Char *, struct stat *, glob_t *);
155b231cb39SDavid E. O'Brien static DIR	*g_opendir(Char *, glob_t *);
15634a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t);
15758f0484fSRodney W. Grimes #ifdef notdef
158b231cb39SDavid E. O'Brien static Char	*g_strcat(Char *, const Char *);
15958f0484fSRodney W. Grimes #endif
160b231cb39SDavid E. O'Brien static int	 g_stat(Char *, struct stat *, glob_t *);
16109264d74SAndrey A. Chernov static int	 glob0(const Char *, glob_t *, struct glob_limit *,
16209264d74SAndrey A. Chernov     const char *);
163daab0b01SMarcel Moolenaar static int	 glob1(Char *, glob_t *, struct glob_limit *);
164daab0b01SMarcel Moolenaar static int	 glob2(Char *, Char *, Char *, Char *, glob_t *,
165daab0b01SMarcel Moolenaar     struct glob_limit *);
166daab0b01SMarcel Moolenaar static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
167daab0b01SMarcel Moolenaar     struct glob_limit *);
16809264d74SAndrey A. Chernov static int	 globextend(const Char *, glob_t *, struct glob_limit *,
16909264d74SAndrey A. Chernov     const char *);
170487dbd92SPeter Wemm static const Char *
171b231cb39SDavid E. O'Brien 		 globtilde(const Char *, Char *, size_t, glob_t *);
17209264d74SAndrey A. Chernov static int	 globexp0(const Char *, glob_t *, struct glob_limit *,
17309264d74SAndrey A. Chernov     const char *);
174daab0b01SMarcel Moolenaar static int	 globexp1(const Char *, glob_t *, struct glob_limit *);
175bd7a9850SAndrey A. Chernov static int	 globexp2(const Char *, const Char *, glob_t *,
176daab0b01SMarcel Moolenaar     struct glob_limit *);
17709264d74SAndrey A. Chernov static int	 globfinal(glob_t *, struct glob_limit *, size_t,
17809264d74SAndrey A. Chernov     const char *);
179b231cb39SDavid E. O'Brien static int	 match(Char *, Char *, Char *);
18058f0484fSRodney W. Grimes #ifdef DEBUG
181b231cb39SDavid E. O'Brien static void	 qprintf(const char *, Char *);
18258f0484fSRodney W. Grimes #endif
18358f0484fSRodney W. Grimes 
18458f0484fSRodney W. Grimes int
1850d6d372cSEitan Adler glob(const char * __restrict pattern, int flags,
1860d6d372cSEitan Adler 	 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
18758f0484fSRodney W. Grimes {
188daab0b01SMarcel Moolenaar 	struct glob_limit limit = { 0, 0, 0, 0, 0 };
1891cec70adSXin LI 	const char *patnext;
190e9346e01STim J. Robbins 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
191e9346e01STim J. Robbins 	mbstate_t mbs;
192e9346e01STim J. Robbins 	wchar_t wc;
193e9346e01STim J. Robbins 	size_t clen;
194aed721ecSAndrey A. Chernov 	int too_long;
19558f0484fSRodney W. Grimes 
1961cec70adSXin LI 	patnext = pattern;
19758f0484fSRodney W. Grimes 	if (!(flags & GLOB_APPEND)) {
19858f0484fSRodney W. Grimes 		pglob->gl_pathc = 0;
19958f0484fSRodney W. Grimes 		pglob->gl_pathv = NULL;
20058f0484fSRodney W. Grimes 		if (!(flags & GLOB_DOOFFS))
20158f0484fSRodney W. Grimes 			pglob->gl_offs = 0;
20258f0484fSRodney W. Grimes 	}
20375dc5f1aSMike Heffner 	if (flags & GLOB_LIMIT) {
204daab0b01SMarcel Moolenaar 		limit.l_path_lim = pglob->gl_matchc;
205daab0b01SMarcel Moolenaar 		if (limit.l_path_lim == 0)
206daab0b01SMarcel Moolenaar 			limit.l_path_lim = GLOB_LIMIT_PATH;
207daab0b01SMarcel Moolenaar 	}
20858f0484fSRodney W. Grimes 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
20958f0484fSRodney W. Grimes 	pglob->gl_errfunc = errfunc;
21058f0484fSRodney W. Grimes 	pglob->gl_matchc = 0;
21158f0484fSRodney W. Grimes 
21258f0484fSRodney W. Grimes 	bufnext = patbuf;
213487dbd92SPeter Wemm 	bufend = bufnext + MAXPATHLEN - 1;
214aed721ecSAndrey A. Chernov 	too_long = 1;
215e9346e01STim J. Robbins 	if (flags & GLOB_NOESCAPE) {
216e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
217aed721ecSAndrey A. Chernov 		while (bufnext <= bufend) {
218e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
219e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
220a5ab035dSAndrey A. Chernov 				return (globfinal(pglob, &limit,
221a5ab035dSAndrey A. Chernov 				    pglob->gl_pathc, pattern));
222aed721ecSAndrey A. Chernov 			else if (clen == 0) {
223aed721ecSAndrey A. Chernov 				too_long = 0;
224e9346e01STim J. Robbins 				break;
225aed721ecSAndrey A. Chernov 			}
226e9346e01STim J. Robbins 			*bufnext++ = wc;
227e9346e01STim J. Robbins 			patnext += clen;
228e9346e01STim J. Robbins 		}
229e9346e01STim J. Robbins 	} else {
23058f0484fSRodney W. Grimes 		/* Protect the quoted characters. */
231e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
232aed721ecSAndrey A. Chernov 		while (bufnext <= bufend) {
233aa3d69a6SAndrey A. Chernov 			if (*patnext == '\\') {
234aa3d69a6SAndrey A. Chernov 				if (*++patnext == '\0') {
235aed721ecSAndrey A. Chernov 					*bufnext++ = QUOTE;
236e9346e01STim J. Robbins 					continue;
23758f0484fSRodney W. Grimes 				}
238e9346e01STim J. Robbins 				prot = M_PROTECT;
239e9346e01STim J. Robbins 			} else
240e9346e01STim J. Robbins 				prot = 0;
241e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
242e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
243a5ab035dSAndrey A. Chernov 				return (globfinal(pglob, &limit,
244a5ab035dSAndrey A. Chernov 				    pglob->gl_pathc, pattern));
245aed721ecSAndrey A. Chernov 			else if (clen == 0) {
246aed721ecSAndrey A. Chernov 				too_long = 0;
247e9346e01STim J. Robbins 				break;
248aed721ecSAndrey A. Chernov 			}
249aed721ecSAndrey A. Chernov 			*bufnext++ = wc | prot;
250e9346e01STim J. Robbins 			patnext += clen;
25158f0484fSRodney W. Grimes 		}
25258f0484fSRodney W. Grimes 	}
253aed721ecSAndrey A. Chernov 	if (too_long)
254a5ab035dSAndrey A. Chernov 		return (globfinal(pglob, &limit, pglob->gl_pathc, pattern));
25558f0484fSRodney W. Grimes 	*bufnext = EOS;
25658f0484fSRodney W. Grimes 
25758f0484fSRodney W. Grimes 	if (flags & GLOB_BRACE)
25809264d74SAndrey A. Chernov 	    return (globexp0(patbuf, pglob, &limit, pattern));
25958f0484fSRodney W. Grimes 	else
26009264d74SAndrey A. Chernov 	    return (glob0(patbuf, pglob, &limit, pattern));
261bd7a9850SAndrey A. Chernov }
262bd7a9850SAndrey A. Chernov 
263bd7a9850SAndrey A. Chernov static int
26409264d74SAndrey A. Chernov globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
26509264d74SAndrey A. Chernov     const char *origpat) {
266bd7a9850SAndrey A. Chernov 	int rv;
267bd7a9850SAndrey A. Chernov 	size_t oldpathc;
268bd7a9850SAndrey A. Chernov 
269bd7a9850SAndrey A. Chernov 	/* Protect a single {}, for find(1), like csh */
270bd7a9850SAndrey A. Chernov 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
271bd7a9850SAndrey A. Chernov 		if ((pglob->gl_flags & GLOB_LIMIT) &&
272bd7a9850SAndrey A. Chernov 		    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
273*869eb80cSAndrey A. Chernov 			errno = E2BIG;
274bd7a9850SAndrey A. Chernov 			return (GLOB_NOSPACE);
275bd7a9850SAndrey A. Chernov 		}
27609264d74SAndrey A. Chernov 		return (glob0(pattern, pglob, limit, origpat));
277bd7a9850SAndrey A. Chernov 	}
278bd7a9850SAndrey A. Chernov 
279bd7a9850SAndrey A. Chernov 	oldpathc = pglob->gl_pathc;
280bd7a9850SAndrey A. Chernov 
281bd7a9850SAndrey A. Chernov 	if ((rv = globexp1(pattern, pglob, limit)) != 0)
282bd7a9850SAndrey A. Chernov 		return rv;
28309264d74SAndrey A. Chernov 
28409264d74SAndrey A. Chernov 	return (globfinal(pglob, limit, oldpathc, origpat));
28558f0484fSRodney W. Grimes }
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes /*
28858f0484fSRodney W. Grimes  * Expand recursively a glob {} pattern. When there is no more expansion
28958f0484fSRodney W. Grimes  * invoke the standard globbing routine to glob the rest of the magic
29058f0484fSRodney W. Grimes  * characters
29158f0484fSRodney W. Grimes  */
292487dbd92SPeter Wemm static int
293daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
29458f0484fSRodney W. Grimes {
295bd7a9850SAndrey A. Chernov 	const Char* ptr;
29658f0484fSRodney W. Grimes 
297bd7a9850SAndrey A. Chernov 	if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
298daab0b01SMarcel Moolenaar 		if ((pglob->gl_flags & GLOB_LIMIT) &&
299daab0b01SMarcel Moolenaar 		    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
300*869eb80cSAndrey A. Chernov 			errno = E2BIG;
301daab0b01SMarcel Moolenaar 			return (GLOB_NOSPACE);
302daab0b01SMarcel Moolenaar 		}
303bd7a9850SAndrey A. Chernov 		return (globexp2(ptr, pattern, pglob, limit));
304bd7a9850SAndrey A. Chernov 	}
305daab0b01SMarcel Moolenaar 
30609264d74SAndrey A. Chernov 	return (glob0(pattern, pglob, limit, NULL));
30758f0484fSRodney W. Grimes }
30858f0484fSRodney W. Grimes 
30958f0484fSRodney W. Grimes 
31058f0484fSRodney W. Grimes /*
31158f0484fSRodney W. Grimes  * Recursive brace globbing helper. Tries to expand a single brace.
31258f0484fSRodney W. Grimes  * If it succeeds then it invokes globexp1 with the new pattern.
31358f0484fSRodney W. Grimes  * If it fails then it tries to glob the rest of the pattern and returns.
31458f0484fSRodney W. Grimes  */
315487dbd92SPeter Wemm static int
316bd7a9850SAndrey A. Chernov globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
317daab0b01SMarcel Moolenaar     struct glob_limit *limit)
31858f0484fSRodney W. Grimes {
319bd7a9850SAndrey A. Chernov 	int     i, rv;
32058f0484fSRodney W. Grimes 	Char   *lm, *ls;
321369316a8SAndrey A. Chernov 	const Char *pe, *pm, *pm1, *pl;
322487dbd92SPeter Wemm 	Char    patbuf[MAXPATHLEN];
32358f0484fSRodney W. Grimes 
32458f0484fSRodney W. Grimes 	/* copy part up to the brace */
32558f0484fSRodney W. Grimes 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
32658f0484fSRodney W. Grimes 		continue;
327487dbd92SPeter Wemm 	*lm = EOS;
32858f0484fSRodney W. Grimes 	ls = lm;
32958f0484fSRodney W. Grimes 
33058f0484fSRodney W. Grimes 	/* Find the balanced brace */
331bd7a9850SAndrey A. Chernov 	for (i = 0, pe = ++ptr; *pe != EOS; pe++)
33258f0484fSRodney W. Grimes 		if (*pe == LBRACKET) {
33358f0484fSRodney W. Grimes 			/* Ignore everything between [] */
33458f0484fSRodney W. Grimes 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
33558f0484fSRodney W. Grimes 				continue;
33658f0484fSRodney W. Grimes 			if (*pe == EOS) {
33758f0484fSRodney W. Grimes 				/*
33858f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
33958f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
34058f0484fSRodney W. Grimes 				 */
34158f0484fSRodney W. Grimes 				pe = pm;
34258f0484fSRodney W. Grimes 			}
34358f0484fSRodney W. Grimes 		}
34458f0484fSRodney W. Grimes 		else if (*pe == LBRACE)
34558f0484fSRodney W. Grimes 			i++;
34658f0484fSRodney W. Grimes 		else if (*pe == RBRACE) {
34758f0484fSRodney W. Grimes 			if (i == 0)
34858f0484fSRodney W. Grimes 				break;
34958f0484fSRodney W. Grimes 			i--;
35058f0484fSRodney W. Grimes 		}
35158f0484fSRodney W. Grimes 
35258f0484fSRodney W. Grimes 	/* Non matching braces; just glob the pattern */
353bd7a9850SAndrey A. Chernov 	if (i != 0 || *pe == EOS)
35409264d74SAndrey A. Chernov 		return (glob0(pattern, pglob, limit, NULL));
35558f0484fSRodney W. Grimes 
35658f0484fSRodney W. Grimes 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
35758f0484fSRodney W. Grimes 		switch (*pm) {
35858f0484fSRodney W. Grimes 		case LBRACKET:
35958f0484fSRodney W. Grimes 			/* Ignore everything between [] */
360369316a8SAndrey A. Chernov 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
36158f0484fSRodney W. Grimes 				continue;
36258f0484fSRodney W. Grimes 			if (*pm == EOS) {
36358f0484fSRodney W. Grimes 				/*
36458f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
36558f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
36658f0484fSRodney W. Grimes 				 */
367369316a8SAndrey A. Chernov 				pm = pm1;
36858f0484fSRodney W. Grimes 			}
36958f0484fSRodney W. Grimes 			break;
37058f0484fSRodney W. Grimes 
37158f0484fSRodney W. Grimes 		case LBRACE:
37258f0484fSRodney W. Grimes 			i++;
37358f0484fSRodney W. Grimes 			break;
37458f0484fSRodney W. Grimes 
37558f0484fSRodney W. Grimes 		case RBRACE:
37658f0484fSRodney W. Grimes 			if (i) {
37758f0484fSRodney W. Grimes 			    i--;
37858f0484fSRodney W. Grimes 			    break;
37958f0484fSRodney W. Grimes 			}
38058f0484fSRodney W. Grimes 			/* FALLTHROUGH */
38158f0484fSRodney W. Grimes 		case COMMA:
38258f0484fSRodney W. Grimes 			if (i && *pm == COMMA)
38358f0484fSRodney W. Grimes 				break;
38458f0484fSRodney W. Grimes 			else {
38558f0484fSRodney W. Grimes 				/* Append the current string */
38658f0484fSRodney W. Grimes 				for (lm = ls; (pl < pm); *lm++ = *pl++)
38758f0484fSRodney W. Grimes 					continue;
38858f0484fSRodney W. Grimes 				/*
38958f0484fSRodney W. Grimes 				 * Append the rest of the pattern after the
39058f0484fSRodney W. Grimes 				 * closing brace
39158f0484fSRodney W. Grimes 				 */
39258f0484fSRodney W. Grimes 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
39358f0484fSRodney W. Grimes 					continue;
39458f0484fSRodney W. Grimes 
39558f0484fSRodney W. Grimes 				/* Expand the current pattern */
39658f0484fSRodney W. Grimes #ifdef DEBUG
39758f0484fSRodney W. Grimes 				qprintf("globexp2:", patbuf);
39858f0484fSRodney W. Grimes #endif
399bd7a9850SAndrey A. Chernov 				rv = globexp1(patbuf, pglob, limit);
400bd7a9850SAndrey A. Chernov 				if (rv)
401bd7a9850SAndrey A. Chernov 					return (rv);
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 				/* move after the comma, to the next string */
40458f0484fSRodney W. Grimes 				pl = pm + 1;
40558f0484fSRodney W. Grimes 			}
40658f0484fSRodney W. Grimes 			break;
40758f0484fSRodney W. Grimes 
40858f0484fSRodney W. Grimes 		default:
40958f0484fSRodney W. Grimes 			break;
41058f0484fSRodney W. Grimes 		}
41185529174SEitan Adler 	return (0);
41258f0484fSRodney W. Grimes }
41358f0484fSRodney W. Grimes 
41458f0484fSRodney W. Grimes 
41558f0484fSRodney W. Grimes 
41658f0484fSRodney W. Grimes /*
41758f0484fSRodney W. Grimes  * expand tilde from the passwd file.
41858f0484fSRodney W. Grimes  */
41958f0484fSRodney W. Grimes static const Char *
4201cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
42158f0484fSRodney W. Grimes {
42258f0484fSRodney W. Grimes 	struct passwd *pwd;
423aa3d69a6SAndrey A. Chernov 	char *h, *sc;
42458f0484fSRodney W. Grimes 	const Char *p;
42562f187a4SWarner Losh 	Char *b, *eb;
426aa3d69a6SAndrey A. Chernov 	wchar_t wc;
427aa3d69a6SAndrey A. Chernov 	wchar_t wbuf[MAXPATHLEN];
428aa3d69a6SAndrey A. Chernov 	wchar_t *wbufend, *dc;
429aa3d69a6SAndrey A. Chernov 	size_t clen;
430aa3d69a6SAndrey A. Chernov 	mbstate_t mbs;
431aa3d69a6SAndrey A. Chernov 	int too_long;
43258f0484fSRodney W. Grimes 
43358f0484fSRodney W. Grimes 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
43485529174SEitan Adler 		return (pattern);
43558f0484fSRodney W. Grimes 
43662f187a4SWarner Losh 	/*
43762f187a4SWarner Losh 	 * Copy up to the end of the string or /
43862f187a4SWarner Losh 	 */
43962f187a4SWarner Losh 	eb = &patbuf[patbuf_len - 1];
440aa3d69a6SAndrey A. Chernov 	for (p = pattern + 1, b = patbuf;
4417455a07aSAndrey A. Chernov 	    b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
44258f0484fSRodney W. Grimes 		continue;
44358f0484fSRodney W. Grimes 
4447455a07aSAndrey A. Chernov 	if (*p != EOS && UNPROT(*p) != SEP)
445f4d4982eSAndrey A. Chernov 		return (NULL);
44658f0484fSRodney W. Grimes 
447aa3d69a6SAndrey A. Chernov 	*b = EOS;
448aa3d69a6SAndrey A. Chernov 	h = NULL;
449aa3d69a6SAndrey A. Chernov 
450aa3d69a6SAndrey A. Chernov 	if (patbuf[0] == EOS) {
45158f0484fSRodney W. Grimes 		/*
4523fa69daeSWarner Losh 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
4533fa69daeSWarner Losh 		 * we're not running setuid or setgid) and then trying
4543fa69daeSWarner Losh 		 * the password file
45558f0484fSRodney W. Grimes 		 */
4564539e95aSTim J. Robbins 		if (issetugid() != 0 ||
4579fcbcd02SJohn Birrell 		    (h = getenv("HOME")) == NULL) {
458eb8eee5aSAndrey A. Chernov 			if (((h = getlogin()) != NULL &&
459eb8eee5aSAndrey A. Chernov 			     (pwd = getpwnam(h)) != NULL) ||
460eb8eee5aSAndrey A. Chernov 			    (pwd = getpwuid(getuid())) != NULL)
46158f0484fSRodney W. Grimes 				h = pwd->pw_dir;
462eb8eee5aSAndrey A. Chernov 			else
46385529174SEitan Adler 				return (pattern);
46458f0484fSRodney W. Grimes 		}
46558f0484fSRodney W. Grimes 	}
46658f0484fSRodney W. Grimes 	else {
46758f0484fSRodney W. Grimes 		/*
46858f0484fSRodney W. Grimes 		 * Expand a ~user
46958f0484fSRodney W. Grimes 		 */
47009264d74SAndrey A. Chernov 		if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
471f4d4982eSAndrey A. Chernov 			return (NULL);
472f4d4982eSAndrey A. Chernov 		if ((pwd = getpwnam((char *)wbuf)) == NULL)
47385529174SEitan Adler 			return (pattern);
47458f0484fSRodney W. Grimes 		else
47558f0484fSRodney W. Grimes 			h = pwd->pw_dir;
47658f0484fSRodney W. Grimes 	}
47758f0484fSRodney W. Grimes 
47858f0484fSRodney W. Grimes 	/* Copy the home directory */
479aa3d69a6SAndrey A. Chernov 	dc = wbuf;
480aa3d69a6SAndrey A. Chernov 	sc = h;
481aa3d69a6SAndrey A. Chernov 	wbufend = wbuf + MAXPATHLEN - 1;
482aa3d69a6SAndrey A. Chernov 	too_long = 1;
483aa3d69a6SAndrey A. Chernov 	memset(&mbs, 0, sizeof(mbs));
484aa3d69a6SAndrey A. Chernov 	while (dc <= wbufend) {
485aa3d69a6SAndrey A. Chernov 		clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
486aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1 || clen == (size_t)-2) {
487aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
488aa3d69a6SAndrey A. Chernov 			wc = (unsigned char)*sc;
489aa3d69a6SAndrey A. Chernov 			clen = 1;
490aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
491aa3d69a6SAndrey A. Chernov 		}
492aa3d69a6SAndrey A. Chernov 		if ((*dc++ = wc) == EOS) {
493aa3d69a6SAndrey A. Chernov 			too_long = 0;
494aa3d69a6SAndrey A. Chernov 			break;
495aa3d69a6SAndrey A. Chernov 		}
496aa3d69a6SAndrey A. Chernov 		sc += clen;
497aa3d69a6SAndrey A. Chernov 	}
498aa3d69a6SAndrey A. Chernov 	if (too_long)
499f4d4982eSAndrey A. Chernov 		return (NULL);
500aa3d69a6SAndrey A. Chernov 
501aa3d69a6SAndrey A. Chernov 	dc = wbuf;
502aed721ecSAndrey A. Chernov 	for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
503aed721ecSAndrey A. Chernov 		continue;
504aa3d69a6SAndrey A. Chernov 	if (*dc != EOS)
505f4d4982eSAndrey A. Chernov 		return (NULL);
50658f0484fSRodney W. Grimes 
50758f0484fSRodney W. Grimes 	/* Append the rest of the pattern */
508aa3d69a6SAndrey A. Chernov 	if (*p != EOS) {
509aa3d69a6SAndrey A. Chernov 		too_long = 1;
510aa3d69a6SAndrey A. Chernov 		while (b <= eb) {
511aa3d69a6SAndrey A. Chernov 			if ((*b++ = *p++) == EOS) {
512aa3d69a6SAndrey A. Chernov 				too_long = 0;
513aa3d69a6SAndrey A. Chernov 				break;
514aa3d69a6SAndrey A. Chernov 			}
515aa3d69a6SAndrey A. Chernov 		}
516aa3d69a6SAndrey A. Chernov 		if (too_long)
517f4d4982eSAndrey A. Chernov 			return (NULL);
518aa3d69a6SAndrey A. Chernov 	} else
51962f187a4SWarner Losh 		*b = EOS;
52058f0484fSRodney W. Grimes 
52185529174SEitan Adler 	return (patbuf);
52258f0484fSRodney W. Grimes }
52358f0484fSRodney W. Grimes 
52458f0484fSRodney W. Grimes 
52558f0484fSRodney W. Grimes /*
52658f0484fSRodney W. Grimes  * The main glob() routine: compiles the pattern (optionally processing
52758f0484fSRodney W. Grimes  * quotes), calls glob1() to do the real pattern matching, and finally
52858f0484fSRodney W. Grimes  * sorts the list (unless unsorted operation is requested).  Returns 0
5294a59c3abSMike Heffner  * if things went well, nonzero if errors occurred.
53058f0484fSRodney W. Grimes  */
53158f0484fSRodney W. Grimes static int
532bd7a9850SAndrey A. Chernov glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
53309264d74SAndrey A. Chernov     const char *origpat) {
53458f0484fSRodney W. Grimes 	const Char *qpatnext;
5355b3fab81SGordon Tetlow 	int err;
5364b767fa6SAndrey A. Chernov 	size_t oldpathc;
5375b3fab81SGordon Tetlow 	Char *bufnext, c, patbuf[MAXPATHLEN];
53858f0484fSRodney W. Grimes 
539487dbd92SPeter Wemm 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
540f4d4982eSAndrey A. Chernov 	if (qpatnext == NULL) {
541*869eb80cSAndrey A. Chernov 		errno = E2BIG;
542f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
543f4d4982eSAndrey A. Chernov 	}
54458f0484fSRodney W. Grimes 	oldpathc = pglob->gl_pathc;
54558f0484fSRodney W. Grimes 	bufnext = patbuf;
54658f0484fSRodney W. Grimes 
54758f0484fSRodney W. Grimes 	/* We don't need to check for buffer overflow any more. */
54858f0484fSRodney W. Grimes 	while ((c = *qpatnext++) != EOS) {
54958f0484fSRodney W. Grimes 		switch (c) {
55058f0484fSRodney W. Grimes 		case LBRACKET:
55158f0484fSRodney W. Grimes 			c = *qpatnext;
55258f0484fSRodney W. Grimes 			if (c == NOT)
55358f0484fSRodney W. Grimes 				++qpatnext;
55458f0484fSRodney W. Grimes 			if (*qpatnext == EOS ||
55534a08754SMike Makonnen 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
55658f0484fSRodney W. Grimes 				*bufnext++ = LBRACKET;
55758f0484fSRodney W. Grimes 				if (c == NOT)
55858f0484fSRodney W. Grimes 					--qpatnext;
55958f0484fSRodney W. Grimes 				break;
56058f0484fSRodney W. Grimes 			}
56158f0484fSRodney W. Grimes 			*bufnext++ = M_SET;
56258f0484fSRodney W. Grimes 			if (c == NOT)
56358f0484fSRodney W. Grimes 				*bufnext++ = M_NOT;
56458f0484fSRodney W. Grimes 			c = *qpatnext++;
56558f0484fSRodney W. Grimes 			do {
56658f0484fSRodney W. Grimes 				*bufnext++ = CHAR(c);
56758f0484fSRodney W. Grimes 				if (*qpatnext == RANGE &&
56858f0484fSRodney W. Grimes 				    (c = qpatnext[1]) != RBRACKET) {
56958f0484fSRodney W. Grimes 					*bufnext++ = M_RNG;
57058f0484fSRodney W. Grimes 					*bufnext++ = CHAR(c);
57158f0484fSRodney W. Grimes 					qpatnext += 2;
57258f0484fSRodney W. Grimes 				}
57358f0484fSRodney W. Grimes 			} while ((c = *qpatnext++) != RBRACKET);
57458f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
57558f0484fSRodney W. Grimes 			*bufnext++ = M_END;
57658f0484fSRodney W. Grimes 			break;
57758f0484fSRodney W. Grimes 		case QUESTION:
57858f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
57958f0484fSRodney W. Grimes 			*bufnext++ = M_ONE;
58058f0484fSRodney W. Grimes 			break;
58158f0484fSRodney W. Grimes 		case STAR:
58258f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
58358f0484fSRodney W. Grimes 			/* collapse adjacent stars to one,
58458f0484fSRodney W. Grimes 			 * to avoid exponential behavior
58558f0484fSRodney W. Grimes 			 */
58658f0484fSRodney W. Grimes 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
58758f0484fSRodney W. Grimes 			    *bufnext++ = M_ALL;
58858f0484fSRodney W. Grimes 			break;
58958f0484fSRodney W. Grimes 		default:
59058f0484fSRodney W. Grimes 			*bufnext++ = CHAR(c);
59158f0484fSRodney W. Grimes 			break;
59258f0484fSRodney W. Grimes 		}
59358f0484fSRodney W. Grimes 	}
59458f0484fSRodney W. Grimes 	*bufnext = EOS;
59558f0484fSRodney W. Grimes #ifdef DEBUG
59658f0484fSRodney W. Grimes 	qprintf("glob0:", patbuf);
59758f0484fSRodney W. Grimes #endif
59858f0484fSRodney W. Grimes 
599bae8632fSJonathan Lemon 	if ((err = glob1(patbuf, pglob, limit)) != 0)
60058f0484fSRodney W. Grimes 		return(err);
60158f0484fSRodney W. Grimes 
60209264d74SAndrey A. Chernov 	if (origpat != NULL)
60309264d74SAndrey A. Chernov 		return (globfinal(pglob, limit, oldpathc, origpat));
60409264d74SAndrey A. Chernov 
60509264d74SAndrey A. Chernov 	return (0);
60609264d74SAndrey A. Chernov }
60709264d74SAndrey A. Chernov 
60809264d74SAndrey A. Chernov static int
60909264d74SAndrey A. Chernov globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc,
61009264d74SAndrey A. Chernov     const char *origpat) {
61158f0484fSRodney W. Grimes 	/*
61209264d74SAndrey A. Chernov 	 * If there was no match we are going to append the origpat
61358f0484fSRodney W. Grimes 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
61409264d74SAndrey A. Chernov 	 * and the origpat did not contain any magic characters
61558f0484fSRodney W. Grimes 	 * GLOB_NOMAGIC is there just for compatibility with csh.
61658f0484fSRodney W. Grimes 	 */
6174a59c3abSMike Heffner 	if (pglob->gl_pathc == oldpathc) {
618a5ab035dSAndrey A. Chernov 		if ((pglob->gl_flags & GLOB_NOCHECK) ||
61958f0484fSRodney W. Grimes 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
620a5ab035dSAndrey A. Chernov 		    !(pglob->gl_flags & GLOB_MAGCHAR)))
62109264d74SAndrey A. Chernov 			return (globextend(NULL, pglob, limit, origpat));
6224a59c3abSMike Heffner 		else
6234a59c3abSMike Heffner 			return (GLOB_NOMATCH);
6244a59c3abSMike Heffner 	}
6254a59c3abSMike Heffner 	if (!(pglob->gl_flags & GLOB_NOSORT))
62658f0484fSRodney W. Grimes 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
62758f0484fSRodney W. Grimes 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
62809264d74SAndrey A. Chernov 
62958f0484fSRodney W. Grimes 	return (0);
63058f0484fSRodney W. Grimes }
63158f0484fSRodney W. Grimes 
63258f0484fSRodney W. Grimes static int
6331cec70adSXin LI compare(const void *p, const void *q)
63458f0484fSRodney W. Grimes {
635aa3d69a6SAndrey A. Chernov 	return (strcoll(*(char **)p, *(char **)q));
63658f0484fSRodney W. Grimes }
63758f0484fSRodney W. Grimes 
63858f0484fSRodney W. Grimes static int
639daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
64058f0484fSRodney W. Grimes {
641487dbd92SPeter Wemm 	Char pathbuf[MAXPATHLEN];
64258f0484fSRodney W. Grimes 
64358f0484fSRodney W. Grimes 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
64458f0484fSRodney W. Grimes 	if (*pattern == EOS)
64558f0484fSRodney W. Grimes 		return (0);
646487dbd92SPeter Wemm 	return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
647487dbd92SPeter Wemm 	    pattern, pglob, limit));
64858f0484fSRodney W. Grimes }
64958f0484fSRodney W. Grimes 
65058f0484fSRodney W. Grimes /*
65158f0484fSRodney W. Grimes  * The functions glob2 and glob3 are mutually recursive; there is one level
65258f0484fSRodney W. Grimes  * of recursion for each segment in the pattern that contains one or more
65358f0484fSRodney W. Grimes  * meta characters.
65458f0484fSRodney W. Grimes  */
65558f0484fSRodney W. Grimes static int
6561cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
657daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
65858f0484fSRodney W. Grimes {
65958f0484fSRodney W. Grimes 	struct stat sb;
66058f0484fSRodney W. Grimes 	Char *p, *q;
66158f0484fSRodney W. Grimes 	int anymeta;
66258f0484fSRodney W. Grimes 
66358f0484fSRodney W. Grimes 	/*
66458f0484fSRodney W. Grimes 	 * Loop over pattern segments until end of pattern or until
66558f0484fSRodney W. Grimes 	 * segment with meta character found.
66658f0484fSRodney W. Grimes 	 */
66758f0484fSRodney W. Grimes 	for (anymeta = 0;;) {
66858f0484fSRodney W. Grimes 		if (*pattern == EOS) {		/* End of pattern? */
66958f0484fSRodney W. Grimes 			*pathend = EOS;
67058f0484fSRodney W. Grimes 			if (g_lstat(pathbuf, &sb, pglob))
67158f0484fSRodney W. Grimes 				return (0);
67258f0484fSRodney W. Grimes 
673daab0b01SMarcel Moolenaar 			if ((pglob->gl_flags & GLOB_LIMIT) &&
674daab0b01SMarcel Moolenaar 			    limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
675*869eb80cSAndrey A. Chernov 				errno = E2BIG;
676daab0b01SMarcel Moolenaar 				return (GLOB_NOSPACE);
677daab0b01SMarcel Moolenaar 			}
678aed721ecSAndrey A. Chernov 			if ((pglob->gl_flags & GLOB_MARK) &&
679aed721ecSAndrey A. Chernov 			    UNPROT(pathend[-1]) != SEP &&
680aed721ecSAndrey A. Chernov 			    (S_ISDIR(sb.st_mode) ||
681aed721ecSAndrey A. Chernov 			    (S_ISLNK(sb.st_mode) &&
682aed721ecSAndrey A. Chernov 			    g_stat(pathbuf, &sb, pglob) == 0 &&
68358f0484fSRodney W. Grimes 			    S_ISDIR(sb.st_mode)))) {
684f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
685*869eb80cSAndrey A. Chernov 					errno = E2BIG;
686f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
687f4d4982eSAndrey A. Chernov 				}
68858f0484fSRodney W. Grimes 				*pathend++ = SEP;
68958f0484fSRodney W. Grimes 				*pathend = EOS;
69058f0484fSRodney W. Grimes 			}
69158f0484fSRodney W. Grimes 			++pglob->gl_matchc;
69209264d74SAndrey A. Chernov 			return (globextend(pathbuf, pglob, limit, NULL));
69358f0484fSRodney W. Grimes 		}
69458f0484fSRodney W. Grimes 
69558f0484fSRodney W. Grimes 		/* Find end of next segment, copy tentatively to pathend. */
69658f0484fSRodney W. Grimes 		q = pathend;
69758f0484fSRodney W. Grimes 		p = pattern;
698aed721ecSAndrey A. Chernov 		while (*p != EOS && UNPROT(*p) != SEP) {
69958f0484fSRodney W. Grimes 			if (ismeta(*p))
70058f0484fSRodney W. Grimes 				anymeta = 1;
701f4d4982eSAndrey A. Chernov 			if (q + 1 > pathend_last) {
702*869eb80cSAndrey A. Chernov 				errno = E2BIG;
703f4d4982eSAndrey A. Chernov 				return (GLOB_NOSPACE);
704f4d4982eSAndrey A. Chernov 			}
70558f0484fSRodney W. Grimes 			*q++ = *p++;
70658f0484fSRodney W. Grimes 		}
70758f0484fSRodney W. Grimes 
70858f0484fSRodney W. Grimes 		if (!anymeta) {		/* No expansion, do next segment. */
70958f0484fSRodney W. Grimes 			pathend = q;
71058f0484fSRodney W. Grimes 			pattern = p;
711aed721ecSAndrey A. Chernov 			while (UNPROT(*pattern) == SEP) {
712f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
713*869eb80cSAndrey A. Chernov 					errno = E2BIG;
714f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
715f4d4982eSAndrey A. Chernov 				}
71658f0484fSRodney W. Grimes 				*pathend++ = *pattern++;
717487dbd92SPeter Wemm 			}
71858f0484fSRodney W. Grimes 		} else			/* Need expansion, recurse. */
71985529174SEitan Adler 			return (glob3(pathbuf, pathend, pathend_last, pattern,
72085529174SEitan Adler 			    p, pglob, limit));
72158f0484fSRodney W. Grimes 	}
72258f0484fSRodney W. Grimes 	/* NOTREACHED */
72358f0484fSRodney W. Grimes }
72458f0484fSRodney W. Grimes 
72558f0484fSRodney W. Grimes static int
7261cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
7271cec70adSXin LI       Char *pattern, Char *restpattern,
728daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
72958f0484fSRodney W. Grimes {
730b231cb39SDavid E. O'Brien 	struct dirent *dp;
73158f0484fSRodney W. Grimes 	DIR *dirp;
732*869eb80cSAndrey A. Chernov 	int err, too_long, saverrno, saverrno2;
733e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
73458f0484fSRodney W. Grimes 
7353e2981e8SCraig Rodrigues 	struct dirent *(*readdirfunc)(DIR *);
73658f0484fSRodney W. Grimes 
737aed721ecSAndrey A. Chernov 	if (pathend > pathend_last) {
738*869eb80cSAndrey A. Chernov 		errno = E2BIG;
739f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
740aed721ecSAndrey A. Chernov 	}
741f4d4982eSAndrey A. Chernov 	*pathend = EOS;
742aed721ecSAndrey A. Chernov 	if (pglob->gl_errfunc != NULL &&
74309264d74SAndrey A. Chernov 	    g_Ctoc(pathbuf, buf, sizeof(buf))) {
744*869eb80cSAndrey A. Chernov 		errno = E2BIG;
745aed721ecSAndrey A. Chernov 		return (GLOB_NOSPACE);
746aed721ecSAndrey A. Chernov 	}
74758f0484fSRodney W. Grimes 
748*869eb80cSAndrey A. Chernov 	saverrno = errno;
749aed721ecSAndrey A. Chernov 	errno = 0;
75058f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
751196d61a9SAndrey A. Chernov 		if (errno == ENOENT || errno == ENOTDIR)
752196d61a9SAndrey A. Chernov 			return (0);
753aed721ecSAndrey A. Chernov 		if ((pglob->gl_errfunc != NULL &&
754aed721ecSAndrey A. Chernov 		    pglob->gl_errfunc(buf, errno)) ||
755*869eb80cSAndrey A. Chernov 		    (pglob->gl_flags & GLOB_ERR)) {
756*869eb80cSAndrey A. Chernov 			if (errno == 0)
757*869eb80cSAndrey A. Chernov 				errno = saverrno;
758d67355c5SAndrey A. Chernov 			return (GLOB_ABORTED);
759*869eb80cSAndrey A. Chernov 		}
760*869eb80cSAndrey A. Chernov 		if (errno == 0)
761*869eb80cSAndrey A. Chernov 			errno = saverrno;
76258f0484fSRodney W. Grimes 		return (0);
76358f0484fSRodney W. Grimes 	}
76458f0484fSRodney W. Grimes 
76558f0484fSRodney W. Grimes 	err = 0;
76658f0484fSRodney W. Grimes 
7673e2981e8SCraig Rodrigues 	/* pglob->gl_readdir takes a void *, fix this manually */
76858f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7693e2981e8SCraig Rodrigues 		readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
77058f0484fSRodney W. Grimes 	else
77158f0484fSRodney W. Grimes 		readdirfunc = readdir;
7723e2981e8SCraig Rodrigues 
773aed721ecSAndrey A. Chernov 	errno = 0;
7743e2981e8SCraig Rodrigues 	/* Search directory for matching names. */
7753e2981e8SCraig Rodrigues 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
7761cec70adSXin LI 		char *sc;
777b231cb39SDavid E. O'Brien 		Char *dc;
778e9346e01STim J. Robbins 		wchar_t wc;
779e9346e01STim J. Robbins 		size_t clen;
780e9346e01STim J. Robbins 		mbstate_t mbs;
78158f0484fSRodney W. Grimes 
782daab0b01SMarcel Moolenaar 		if ((pglob->gl_flags & GLOB_LIMIT) &&
783daab0b01SMarcel Moolenaar 		    limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
784*869eb80cSAndrey A. Chernov 			errno = E2BIG;
785f4d4982eSAndrey A. Chernov 			err = GLOB_NOSPACE;
786daab0b01SMarcel Moolenaar 			break;
787daab0b01SMarcel Moolenaar 		}
788daab0b01SMarcel Moolenaar 
78958f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
790e04d8562SAndrey A. Chernov 		if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
791e04d8562SAndrey A. Chernov 			errno = 0;
79258f0484fSRodney W. Grimes 			continue;
793e04d8562SAndrey A. Chernov 		}
794e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
795487dbd92SPeter Wemm 		dc = pathend;
7961cec70adSXin LI 		sc = dp->d_name;
797aed721ecSAndrey A. Chernov 		too_long = 1;
798aed721ecSAndrey A. Chernov 		while (dc <= pathend_last) {
799e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
800e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
801aa3d69a6SAndrey A. Chernov 				/* XXX See initial comment #2. */
802aa3d69a6SAndrey A. Chernov 				wc = (unsigned char)*sc;
803e9346e01STim J. Robbins 				clen = 1;
804e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
805e9346e01STim J. Robbins 			}
806aed721ecSAndrey A. Chernov 			if ((*dc++ = wc) == EOS) {
807aed721ecSAndrey A. Chernov 				too_long = 0;
808e9346e01STim J. Robbins 				break;
809aed721ecSAndrey A. Chernov 			}
810e9346e01STim J. Robbins 			sc += clen;
811e9346e01STim J. Robbins 		}
81215cb7866SAndrey A. Chernov 		if (too_long && ((pglob->gl_errfunc != NULL &&
81315cb7866SAndrey A. Chernov 		    pglob->gl_errfunc(buf, ENAMETOOLONG)) ||
81415cb7866SAndrey A. Chernov 		    (pglob->gl_flags & GLOB_ERR))) {
81515cb7866SAndrey A. Chernov 			errno = ENAMETOOLONG;
81615cb7866SAndrey A. Chernov 			err = GLOB_ABORTED;
81715cb7866SAndrey A. Chernov 			break;
81815cb7866SAndrey A. Chernov 		}
819aed721ecSAndrey A. Chernov 		if (too_long || !match(pathend, pattern, restpattern)) {
82058f0484fSRodney W. Grimes 			*pathend = EOS;
821e04d8562SAndrey A. Chernov 			errno = 0;
82258f0484fSRodney W. Grimes 			continue;
82358f0484fSRodney W. Grimes 		}
824487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
825487dbd92SPeter Wemm 		    pglob, limit);
82658f0484fSRodney W. Grimes 		if (err)
82758f0484fSRodney W. Grimes 			break;
828aed721ecSAndrey A. Chernov 		errno = 0;
82958f0484fSRodney W. Grimes 	}
83058f0484fSRodney W. Grimes 
831*869eb80cSAndrey A. Chernov 	saverrno2 = errno;
83258f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
83358f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
83458f0484fSRodney W. Grimes 	else
83558f0484fSRodney W. Grimes 		closedir(dirp);
836*869eb80cSAndrey A. Chernov 	errno = saverrno2;
837aed721ecSAndrey A. Chernov 
838aed721ecSAndrey A. Chernov 	if (err)
83958f0484fSRodney W. Grimes 		return (err);
840aed721ecSAndrey A. Chernov 
841aed721ecSAndrey A. Chernov 	if (dp == NULL && errno != 0 && ((pglob->gl_errfunc != NULL &&
842aed721ecSAndrey A. Chernov 	    pglob->gl_errfunc(buf, errno)) || (pglob->gl_flags & GLOB_ERR)))
843aed721ecSAndrey A. Chernov 		return (GLOB_ABORTED);
844aed721ecSAndrey A. Chernov 
845*869eb80cSAndrey A. Chernov 	if (errno == 0)
846*869eb80cSAndrey A. Chernov 		errno = saverrno;
847aed721ecSAndrey A. Chernov 	return (0);
84858f0484fSRodney W. Grimes }
84958f0484fSRodney W. Grimes 
85058f0484fSRodney W. Grimes 
85158f0484fSRodney W. Grimes /*
852b4c19408SEd Maste  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
85358f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
85458f0484fSRodney W. Grimes  *
85558f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
85658f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
85758f0484fSRodney W. Grimes  * behavior.
85858f0484fSRodney W. Grimes  *
85958f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
86058f0484fSRodney W. Grimes  *
86158f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
86258f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
86358f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
86458f0484fSRodney W. Grimes  */
86558f0484fSRodney W. Grimes static int
866aed721ecSAndrey A. Chernov globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
86709264d74SAndrey A. Chernov     const char *origpat)
86858f0484fSRodney W. Grimes {
869b231cb39SDavid E. O'Brien 	char **pathv;
8704b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
87158f0484fSRodney W. Grimes 	char *copy;
87258f0484fSRodney W. Grimes 	const Char *p;
87358f0484fSRodney W. Grimes 
874daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
875daab0b01SMarcel Moolenaar 	    pglob->gl_matchc > limit->l_path_lim) {
876*869eb80cSAndrey A. Chernov 		errno = E2BIG;
87775dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
87875dc5f1aSMike Heffner 	}
879813c96dbSJonathan Lemon 
88058f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
88143cc14e0SMarcel Moolenaar 	/* realloc(NULL, newsize) is equivalent to malloc(newsize). */
88243cc14e0SMarcel Moolenaar 	pathv = realloc((void *)pglob->gl_pathv, newsize);
883b628fac5SMarcel Moolenaar 	if (pathv == NULL)
88458f0484fSRodney W. Grimes 		return (GLOB_NOSPACE);
88558f0484fSRodney W. Grimes 
88658f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
88758f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
88858f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
8894b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
89058f0484fSRodney W. Grimes 			*--pathv = NULL;
89158f0484fSRodney W. Grimes 	}
89258f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
89358f0484fSRodney W. Grimes 
89409264d74SAndrey A. Chernov 	if (origpat != NULL)
89509264d74SAndrey A. Chernov 		copy = strdup(origpat);
89609264d74SAndrey A. Chernov 	else {
897aed721ecSAndrey A. Chernov 		for (p = path; *p++ != EOS;)
89858f0484fSRodney W. Grimes 			continue;
899e9346e01STim J. Robbins 		len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
900bd7a9850SAndrey A. Chernov 		if ((copy = malloc(len)) != NULL) {
90109264d74SAndrey A. Chernov 			if (g_Ctoc(path, copy, len)) {
902bd7a9850SAndrey A. Chernov 				free(copy);
903*869eb80cSAndrey A. Chernov 				errno = E2BIG;
904daab0b01SMarcel Moolenaar 				return (GLOB_NOSPACE);
905daab0b01SMarcel Moolenaar 			}
90609264d74SAndrey A. Chernov 		}
90709264d74SAndrey A. Chernov 	}
90809264d74SAndrey A. Chernov 	if (copy != NULL) {
909bd7a9850SAndrey A. Chernov 		limit->l_string_cnt += strlen(copy) + 1;
910bd7a9850SAndrey A. Chernov 		if ((pglob->gl_flags & GLOB_LIMIT) &&
911bd7a9850SAndrey A. Chernov 		    limit->l_string_cnt >= GLOB_LIMIT_STRING) {
91276e2bc01SPeter Wemm 			free(copy);
913*869eb80cSAndrey A. Chernov 			errno = E2BIG;
91476e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
91576e2bc01SPeter Wemm 		}
91658f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
91758f0484fSRodney W. Grimes 	}
91858f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
91958f0484fSRodney W. Grimes 	return (copy == NULL ? GLOB_NOSPACE : 0);
92058f0484fSRodney W. Grimes }
92158f0484fSRodney W. Grimes 
92258f0484fSRodney W. Grimes /*
92358f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
92458f0484fSRodney W. Grimes  * pattern causes a recursion level.
92558f0484fSRodney W. Grimes  */
92658f0484fSRodney W. Grimes static int
9271cec70adSXin LI match(Char *name, Char *pat, Char *patend)
92858f0484fSRodney W. Grimes {
92958f0484fSRodney W. Grimes 	int ok, negate_range;
93058f0484fSRodney W. Grimes 	Char c, k;
9311daad8f5SAndrey A. Chernov 	struct xlocale_collate *table =
9321daad8f5SAndrey A. Chernov 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
93358f0484fSRodney W. Grimes 
93458f0484fSRodney W. Grimes 	while (pat < patend) {
93558f0484fSRodney W. Grimes 		c = *pat++;
93658f0484fSRodney W. Grimes 		switch (c & M_MASK) {
93758f0484fSRodney W. Grimes 		case M_ALL:
93858f0484fSRodney W. Grimes 			if (pat == patend)
93958f0484fSRodney W. Grimes 				return (1);
94058f0484fSRodney W. Grimes 			do
94158f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
94258f0484fSRodney W. Grimes 				    return (1);
94358f0484fSRodney W. Grimes 			while (*name++ != EOS);
94458f0484fSRodney W. Grimes 			return (0);
94558f0484fSRodney W. Grimes 		case M_ONE:
94658f0484fSRodney W. Grimes 			if (*name++ == EOS)
94758f0484fSRodney W. Grimes 				return (0);
94858f0484fSRodney W. Grimes 			break;
94958f0484fSRodney W. Grimes 		case M_SET:
95058f0484fSRodney W. Grimes 			ok = 0;
95158f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
95258f0484fSRodney W. Grimes 				return (0);
953eef722c3SAndrey A. Chernov 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
95458f0484fSRodney W. Grimes 				++pat;
95558f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
95658f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
9571daad8f5SAndrey A. Chernov 					if (table->__collate_load_error ?
958aa3d69a6SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) &&
959aa3d69a6SAndrey A. Chernov 					    CHAR(k) <= CHAR(pat[1]) :
960aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(c),
961aa3d69a6SAndrey A. Chernov 					    CHAR(k)) <= 0 &&
962aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(k),
963aa3d69a6SAndrey A. Chernov 					    CHAR(pat[1])) <= 0)
96458f0484fSRodney W. Grimes 						ok = 1;
96558f0484fSRodney W. Grimes 					pat += 2;
96658f0484fSRodney W. Grimes 				} else if (c == k)
96758f0484fSRodney W. Grimes 					ok = 1;
96858f0484fSRodney W. Grimes 			if (ok == negate_range)
96958f0484fSRodney W. Grimes 				return (0);
97058f0484fSRodney W. Grimes 			break;
97158f0484fSRodney W. Grimes 		default:
97258f0484fSRodney W. Grimes 			if (*name++ != c)
97358f0484fSRodney W. Grimes 				return (0);
97458f0484fSRodney W. Grimes 			break;
97558f0484fSRodney W. Grimes 		}
97658f0484fSRodney W. Grimes 	}
97758f0484fSRodney W. Grimes 	return (*name == EOS);
97858f0484fSRodney W. Grimes }
97958f0484fSRodney W. Grimes 
98058f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
98158f0484fSRodney W. Grimes void
9821cec70adSXin LI globfree(glob_t *pglob)
98358f0484fSRodney W. Grimes {
9844b767fa6SAndrey A. Chernov 	size_t i;
985b231cb39SDavid E. O'Brien 	char **pp;
98658f0484fSRodney W. Grimes 
98758f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
98858f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
98958f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
99058f0484fSRodney W. Grimes 			if (*pp)
99158f0484fSRodney W. Grimes 				free(*pp);
99258f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
99376e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
99458f0484fSRodney W. Grimes 	}
99558f0484fSRodney W. Grimes }
99658f0484fSRodney W. Grimes 
99758f0484fSRodney W. Grimes static DIR *
9981cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
99958f0484fSRodney W. Grimes {
1000e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
100158f0484fSRodney W. Grimes 
1002aa3d69a6SAndrey A. Chernov 	if (*str == EOS)
100358f0484fSRodney W. Grimes 		strcpy(buf, ".");
100476e2bc01SPeter Wemm 	else {
100509264d74SAndrey A. Chernov 		if (g_Ctoc(str, buf, sizeof(buf))) {
1006196d61a9SAndrey A. Chernov 			errno = ENAMETOOLONG;
100776e2bc01SPeter Wemm 			return (NULL);
100876e2bc01SPeter Wemm 		}
1009196d61a9SAndrey A. Chernov 	}
101058f0484fSRodney W. Grimes 
101158f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
101258f0484fSRodney W. Grimes 		return ((*pglob->gl_opendir)(buf));
101358f0484fSRodney W. Grimes 
101458f0484fSRodney W. Grimes 	return (opendir(buf));
101558f0484fSRodney W. Grimes }
101658f0484fSRodney W. Grimes 
101758f0484fSRodney W. Grimes static int
10181cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
101958f0484fSRodney W. Grimes {
1020e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
102158f0484fSRodney W. Grimes 
102209264d74SAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf))) {
102376e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
102476e2bc01SPeter Wemm 		return (-1);
102576e2bc01SPeter Wemm 	}
102658f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
102758f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
102858f0484fSRodney W. Grimes 	return (lstat(buf, sb));
102958f0484fSRodney W. Grimes }
103058f0484fSRodney W. Grimes 
103158f0484fSRodney W. Grimes static int
10321cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
103358f0484fSRodney W. Grimes {
1034e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
103558f0484fSRodney W. Grimes 
103609264d74SAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf))) {
103776e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
103876e2bc01SPeter Wemm 		return (-1);
103976e2bc01SPeter Wemm 	}
104058f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
104158f0484fSRodney W. Grimes 		return ((*pglob->gl_stat)(buf, sb));
104258f0484fSRodney W. Grimes 	return (stat(buf, sb));
104358f0484fSRodney W. Grimes }
104458f0484fSRodney W. Grimes 
104534a08754SMike Makonnen static const Char *
104634a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
104758f0484fSRodney W. Grimes {
10481cec70adSXin LI 
104958f0484fSRodney W. Grimes 	do {
105058f0484fSRodney W. Grimes 		if (*str == ch)
105158f0484fSRodney W. Grimes 			return (str);
105258f0484fSRodney W. Grimes 	} while (*str++);
105358f0484fSRodney W. Grimes 	return (NULL);
105458f0484fSRodney W. Grimes }
105558f0484fSRodney W. Grimes 
105676e2bc01SPeter Wemm static int
105709264d74SAndrey A. Chernov g_Ctoc(const Char *str, char *buf, size_t len)
105858f0484fSRodney W. Grimes {
1059e9346e01STim J. Robbins 	mbstate_t mbs;
1060e9346e01STim J. Robbins 	size_t clen;
106158f0484fSRodney W. Grimes 
1062e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1063e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
106409264d74SAndrey A. Chernov 		clen = wcrtomb(buf, CHAR(*str), &mbs);
1065aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1) {
1066aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
106709264d74SAndrey A. Chernov 			*buf = (char)CHAR(*str);
1068aa3d69a6SAndrey A. Chernov 			clen = 1;
1069aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
1070aa3d69a6SAndrey A. Chernov 		}
107109264d74SAndrey A. Chernov 		if (CHAR(*str) == EOS)
107276e2bc01SPeter Wemm 			return (0);
107309264d74SAndrey A. Chernov 		str++;
1074e9346e01STim J. Robbins 		buf += clen;
1075e9346e01STim J. Robbins 		len -= clen;
107658f0484fSRodney W. Grimes 	}
107727d52f69SPeter Wemm 	return (1);
107827d52f69SPeter Wemm }
107958f0484fSRodney W. Grimes 
108058f0484fSRodney W. Grimes #ifdef DEBUG
108158f0484fSRodney W. Grimes static void
10821cec70adSXin LI qprintf(const char *str, Char *s)
108358f0484fSRodney W. Grimes {
1084b231cb39SDavid E. O'Brien 	Char *p;
108558f0484fSRodney W. Grimes 
1086bd7a9850SAndrey A. Chernov 	(void)printf("%s\n", str);
1087bd7a9850SAndrey A. Chernov 	if (s != NULL) {
1088bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1089bd7a9850SAndrey A. Chernov 			(void)printf("%c", (char)CHAR(*p));
109058f0484fSRodney W. Grimes 		(void)printf("\n");
1091bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1092bd7a9850SAndrey A. Chernov 			(void)printf("%c", (isprot(*p) ? '\\' : ' '));
109358f0484fSRodney W. Grimes 		(void)printf("\n");
1094bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1095bd7a9850SAndrey A. Chernov 			(void)printf("%c", (ismeta(*p) ? '_' : ' '));
109658f0484fSRodney W. Grimes 		(void)printf("\n");
109758f0484fSRodney W. Grimes 	}
1098bd7a9850SAndrey A. Chernov }
109958f0484fSRodney W. Grimes #endif
1100