xref: /freebsd/lib/libc/gen/glob.c (revision e04d8562b64c3d6e2f868dabc640a015f6325d32)
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) {
273bd7a9850SAndrey A. Chernov 			errno = 0;
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) {
300daab0b01SMarcel Moolenaar 			errno = 0;
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) {
541f4d4982eSAndrey A. Chernov 		errno = 0;
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) {
675daab0b01SMarcel Moolenaar 				errno = 0;
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) {
685f4d4982eSAndrey A. Chernov 					errno = 0;
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) {
702f4d4982eSAndrey A. Chernov 				errno = 0;
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) {
713f4d4982eSAndrey A. Chernov 					errno = 0;
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;
732aed721ecSAndrey A. Chernov 	int err, too_long, saverrno;
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) {
73858f0484fSRodney W. Grimes 		errno = 0;
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))) {
744aed721ecSAndrey A. Chernov 		errno = 0;
745aed721ecSAndrey A. Chernov 		return (GLOB_NOSPACE);
746aed721ecSAndrey A. Chernov 	}
74758f0484fSRodney W. Grimes 
748aed721ecSAndrey A. Chernov 	errno = 0;
74958f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
750196d61a9SAndrey A. Chernov 		if (errno == ENOENT || errno == ENOTDIR)
751196d61a9SAndrey A. Chernov 			return (0);
752aed721ecSAndrey A. Chernov 		if ((pglob->gl_errfunc != NULL &&
753aed721ecSAndrey A. Chernov 		    pglob->gl_errfunc(buf, errno)) ||
754aed721ecSAndrey A. Chernov 		    (pglob->gl_flags & GLOB_ERR))
755d67355c5SAndrey A. Chernov 			return (GLOB_ABORTED);
75658f0484fSRodney W. Grimes 		return (0);
75758f0484fSRodney W. Grimes 	}
75858f0484fSRodney W. Grimes 
75958f0484fSRodney W. Grimes 	err = 0;
76058f0484fSRodney W. Grimes 
7613e2981e8SCraig Rodrigues 	/* pglob->gl_readdir takes a void *, fix this manually */
76258f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7633e2981e8SCraig Rodrigues 		readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
76458f0484fSRodney W. Grimes 	else
76558f0484fSRodney W. Grimes 		readdirfunc = readdir;
7663e2981e8SCraig Rodrigues 
767aed721ecSAndrey A. Chernov 	errno = 0;
7683e2981e8SCraig Rodrigues 	/* Search directory for matching names. */
7693e2981e8SCraig Rodrigues 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
7701cec70adSXin LI 		char *sc;
771b231cb39SDavid E. O'Brien 		Char *dc;
772e9346e01STim J. Robbins 		wchar_t wc;
773e9346e01STim J. Robbins 		size_t clen;
774e9346e01STim J. Robbins 		mbstate_t mbs;
77558f0484fSRodney W. Grimes 
776daab0b01SMarcel Moolenaar 		if ((pglob->gl_flags & GLOB_LIMIT) &&
777daab0b01SMarcel Moolenaar 		    limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
778daab0b01SMarcel Moolenaar 			errno = 0;
779f4d4982eSAndrey A. Chernov 			err = GLOB_NOSPACE;
780daab0b01SMarcel Moolenaar 			break;
781daab0b01SMarcel Moolenaar 		}
782daab0b01SMarcel Moolenaar 
78358f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
784*e04d8562SAndrey A. Chernov 		if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
785*e04d8562SAndrey A. Chernov 			errno = 0;
78658f0484fSRodney W. Grimes 			continue;
787*e04d8562SAndrey A. Chernov 		}
788e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
789487dbd92SPeter Wemm 		dc = pathend;
7901cec70adSXin LI 		sc = dp->d_name;
791aed721ecSAndrey A. Chernov 		too_long = 1;
792aed721ecSAndrey A. Chernov 		while (dc <= pathend_last) {
793e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
794e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
795aa3d69a6SAndrey A. Chernov 				/* XXX See initial comment #2. */
796aa3d69a6SAndrey A. Chernov 				wc = (unsigned char)*sc;
797e9346e01STim J. Robbins 				clen = 1;
798e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
799e9346e01STim J. Robbins 			}
800aed721ecSAndrey A. Chernov 			if ((*dc++ = wc) == EOS) {
801aed721ecSAndrey A. Chernov 				too_long = 0;
802e9346e01STim J. Robbins 				break;
803aed721ecSAndrey A. Chernov 			}
804e9346e01STim J. Robbins 			sc += clen;
805e9346e01STim J. Robbins 		}
806aed721ecSAndrey A. Chernov 		if (too_long || !match(pathend, pattern, restpattern)) {
80758f0484fSRodney W. Grimes 			*pathend = EOS;
808*e04d8562SAndrey A. Chernov 			errno = 0;
80958f0484fSRodney W. Grimes 			continue;
81058f0484fSRodney W. Grimes 		}
811487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
812487dbd92SPeter Wemm 		    pglob, limit);
81358f0484fSRodney W. Grimes 		if (err)
81458f0484fSRodney W. Grimes 			break;
815aed721ecSAndrey A. Chernov 		errno = 0;
81658f0484fSRodney W. Grimes 	}
81758f0484fSRodney W. Grimes 
818aed721ecSAndrey A. Chernov 	saverrno = errno;
81958f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
82058f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
82158f0484fSRodney W. Grimes 	else
82258f0484fSRodney W. Grimes 		closedir(dirp);
823aed721ecSAndrey A. Chernov 	errno = saverrno;
824aed721ecSAndrey A. Chernov 
825aed721ecSAndrey A. Chernov 	if (err)
82658f0484fSRodney W. Grimes 		return (err);
827aed721ecSAndrey A. Chernov 
828aed721ecSAndrey A. Chernov 	if (dp == NULL && errno != 0 && ((pglob->gl_errfunc != NULL &&
829aed721ecSAndrey A. Chernov 	    pglob->gl_errfunc(buf, errno)) || (pglob->gl_flags & GLOB_ERR)))
830aed721ecSAndrey A. Chernov 		return (GLOB_ABORTED);
831aed721ecSAndrey A. Chernov 
832aed721ecSAndrey A. Chernov 	return (0);
83358f0484fSRodney W. Grimes }
83458f0484fSRodney W. Grimes 
83558f0484fSRodney W. Grimes 
83658f0484fSRodney W. Grimes /*
837b4c19408SEd Maste  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
83858f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
83958f0484fSRodney W. Grimes  *
84058f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
84158f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
84258f0484fSRodney W. Grimes  * behavior.
84358f0484fSRodney W. Grimes  *
84458f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
84558f0484fSRodney W. Grimes  *
84658f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
84758f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
84858f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
84958f0484fSRodney W. Grimes  */
85058f0484fSRodney W. Grimes static int
851aed721ecSAndrey A. Chernov globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
85209264d74SAndrey A. Chernov     const char *origpat)
85358f0484fSRodney W. Grimes {
854b231cb39SDavid E. O'Brien 	char **pathv;
8554b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
85658f0484fSRodney W. Grimes 	char *copy;
85758f0484fSRodney W. Grimes 	const Char *p;
85858f0484fSRodney W. Grimes 
859daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
860daab0b01SMarcel Moolenaar 	    pglob->gl_matchc > limit->l_path_lim) {
86175dc5f1aSMike Heffner 		errno = 0;
86275dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
86375dc5f1aSMike Heffner 	}
864813c96dbSJonathan Lemon 
86558f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
86643cc14e0SMarcel Moolenaar 	/* realloc(NULL, newsize) is equivalent to malloc(newsize). */
86743cc14e0SMarcel Moolenaar 	pathv = realloc((void *)pglob->gl_pathv, newsize);
868b628fac5SMarcel Moolenaar 	if (pathv == NULL)
86958f0484fSRodney W. Grimes 		return (GLOB_NOSPACE);
87058f0484fSRodney W. Grimes 
87158f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
87258f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
87358f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
8744b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
87558f0484fSRodney W. Grimes 			*--pathv = NULL;
87658f0484fSRodney W. Grimes 	}
87758f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
87858f0484fSRodney W. Grimes 
87909264d74SAndrey A. Chernov 	if (origpat != NULL)
88009264d74SAndrey A. Chernov 		copy = strdup(origpat);
88109264d74SAndrey A. Chernov 	else {
882aed721ecSAndrey A. Chernov 		for (p = path; *p++ != EOS;)
88358f0484fSRodney W. Grimes 			continue;
884e9346e01STim J. Robbins 		len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
885bd7a9850SAndrey A. Chernov 		if ((copy = malloc(len)) != NULL) {
88609264d74SAndrey A. Chernov 			if (g_Ctoc(path, copy, len)) {
887bd7a9850SAndrey A. Chernov 				free(copy);
888daab0b01SMarcel Moolenaar 				errno = 0;
889daab0b01SMarcel Moolenaar 				return (GLOB_NOSPACE);
890daab0b01SMarcel Moolenaar 			}
89109264d74SAndrey A. Chernov 		}
89209264d74SAndrey A. Chernov 	}
89309264d74SAndrey A. Chernov 	if (copy != NULL) {
894bd7a9850SAndrey A. Chernov 		limit->l_string_cnt += strlen(copy) + 1;
895bd7a9850SAndrey A. Chernov 		if ((pglob->gl_flags & GLOB_LIMIT) &&
896bd7a9850SAndrey A. Chernov 		    limit->l_string_cnt >= GLOB_LIMIT_STRING) {
89776e2bc01SPeter Wemm 			free(copy);
898f4d4982eSAndrey A. Chernov 			errno = 0;
89976e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
90076e2bc01SPeter Wemm 		}
90158f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
90258f0484fSRodney W. Grimes 	}
90358f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
90458f0484fSRodney W. Grimes 	return (copy == NULL ? GLOB_NOSPACE : 0);
90558f0484fSRodney W. Grimes }
90658f0484fSRodney W. Grimes 
90758f0484fSRodney W. Grimes /*
90858f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
90958f0484fSRodney W. Grimes  * pattern causes a recursion level.
91058f0484fSRodney W. Grimes  */
91158f0484fSRodney W. Grimes static int
9121cec70adSXin LI match(Char *name, Char *pat, Char *patend)
91358f0484fSRodney W. Grimes {
91458f0484fSRodney W. Grimes 	int ok, negate_range;
91558f0484fSRodney W. Grimes 	Char c, k;
9161daad8f5SAndrey A. Chernov 	struct xlocale_collate *table =
9171daad8f5SAndrey A. Chernov 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
91858f0484fSRodney W. Grimes 
91958f0484fSRodney W. Grimes 	while (pat < patend) {
92058f0484fSRodney W. Grimes 		c = *pat++;
92158f0484fSRodney W. Grimes 		switch (c & M_MASK) {
92258f0484fSRodney W. Grimes 		case M_ALL:
92358f0484fSRodney W. Grimes 			if (pat == patend)
92458f0484fSRodney W. Grimes 				return (1);
92558f0484fSRodney W. Grimes 			do
92658f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
92758f0484fSRodney W. Grimes 				    return (1);
92858f0484fSRodney W. Grimes 			while (*name++ != EOS);
92958f0484fSRodney W. Grimes 			return (0);
93058f0484fSRodney W. Grimes 		case M_ONE:
93158f0484fSRodney W. Grimes 			if (*name++ == EOS)
93258f0484fSRodney W. Grimes 				return (0);
93358f0484fSRodney W. Grimes 			break;
93458f0484fSRodney W. Grimes 		case M_SET:
93558f0484fSRodney W. Grimes 			ok = 0;
93658f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
93758f0484fSRodney W. Grimes 				return (0);
938eef722c3SAndrey A. Chernov 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
93958f0484fSRodney W. Grimes 				++pat;
94058f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
94158f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
9421daad8f5SAndrey A. Chernov 					if (table->__collate_load_error ?
943aa3d69a6SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) &&
944aa3d69a6SAndrey A. Chernov 					    CHAR(k) <= CHAR(pat[1]) :
945aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(c),
946aa3d69a6SAndrey A. Chernov 					    CHAR(k)) <= 0 &&
947aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(k),
948aa3d69a6SAndrey A. Chernov 					    CHAR(pat[1])) <= 0)
94958f0484fSRodney W. Grimes 						ok = 1;
95058f0484fSRodney W. Grimes 					pat += 2;
95158f0484fSRodney W. Grimes 				} else if (c == k)
95258f0484fSRodney W. Grimes 					ok = 1;
95358f0484fSRodney W. Grimes 			if (ok == negate_range)
95458f0484fSRodney W. Grimes 				return (0);
95558f0484fSRodney W. Grimes 			break;
95658f0484fSRodney W. Grimes 		default:
95758f0484fSRodney W. Grimes 			if (*name++ != c)
95858f0484fSRodney W. Grimes 				return (0);
95958f0484fSRodney W. Grimes 			break;
96058f0484fSRodney W. Grimes 		}
96158f0484fSRodney W. Grimes 	}
96258f0484fSRodney W. Grimes 	return (*name == EOS);
96358f0484fSRodney W. Grimes }
96458f0484fSRodney W. Grimes 
96558f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
96658f0484fSRodney W. Grimes void
9671cec70adSXin LI globfree(glob_t *pglob)
96858f0484fSRodney W. Grimes {
9694b767fa6SAndrey A. Chernov 	size_t i;
970b231cb39SDavid E. O'Brien 	char **pp;
97158f0484fSRodney W. Grimes 
97258f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
97358f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
97458f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
97558f0484fSRodney W. Grimes 			if (*pp)
97658f0484fSRodney W. Grimes 				free(*pp);
97758f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
97876e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
97958f0484fSRodney W. Grimes 	}
98058f0484fSRodney W. Grimes }
98158f0484fSRodney W. Grimes 
98258f0484fSRodney W. Grimes static DIR *
9831cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
98458f0484fSRodney W. Grimes {
985e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
98658f0484fSRodney W. Grimes 
987aa3d69a6SAndrey A. Chernov 	if (*str == EOS)
98858f0484fSRodney W. Grimes 		strcpy(buf, ".");
98976e2bc01SPeter Wemm 	else {
99009264d74SAndrey A. Chernov 		if (g_Ctoc(str, buf, sizeof(buf))) {
991196d61a9SAndrey A. Chernov 			errno = ENAMETOOLONG;
99276e2bc01SPeter Wemm 			return (NULL);
99376e2bc01SPeter Wemm 		}
994196d61a9SAndrey A. Chernov 	}
99558f0484fSRodney W. Grimes 
99658f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
99758f0484fSRodney W. Grimes 		return ((*pglob->gl_opendir)(buf));
99858f0484fSRodney W. Grimes 
99958f0484fSRodney W. Grimes 	return (opendir(buf));
100058f0484fSRodney W. Grimes }
100158f0484fSRodney W. Grimes 
100258f0484fSRodney W. Grimes static int
10031cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
100458f0484fSRodney W. Grimes {
1005e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
100658f0484fSRodney W. Grimes 
100709264d74SAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf))) {
100876e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
100976e2bc01SPeter Wemm 		return (-1);
101076e2bc01SPeter Wemm 	}
101158f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
101258f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
101358f0484fSRodney W. Grimes 	return (lstat(buf, sb));
101458f0484fSRodney W. Grimes }
101558f0484fSRodney W. Grimes 
101658f0484fSRodney W. Grimes static int
10171cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
101858f0484fSRodney W. Grimes {
1019e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
102058f0484fSRodney W. Grimes 
102109264d74SAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf))) {
102276e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
102376e2bc01SPeter Wemm 		return (-1);
102476e2bc01SPeter Wemm 	}
102558f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
102658f0484fSRodney W. Grimes 		return ((*pglob->gl_stat)(buf, sb));
102758f0484fSRodney W. Grimes 	return (stat(buf, sb));
102858f0484fSRodney W. Grimes }
102958f0484fSRodney W. Grimes 
103034a08754SMike Makonnen static const Char *
103134a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
103258f0484fSRodney W. Grimes {
10331cec70adSXin LI 
103458f0484fSRodney W. Grimes 	do {
103558f0484fSRodney W. Grimes 		if (*str == ch)
103658f0484fSRodney W. Grimes 			return (str);
103758f0484fSRodney W. Grimes 	} while (*str++);
103858f0484fSRodney W. Grimes 	return (NULL);
103958f0484fSRodney W. Grimes }
104058f0484fSRodney W. Grimes 
104176e2bc01SPeter Wemm static int
104209264d74SAndrey A. Chernov g_Ctoc(const Char *str, char *buf, size_t len)
104358f0484fSRodney W. Grimes {
1044e9346e01STim J. Robbins 	mbstate_t mbs;
1045e9346e01STim J. Robbins 	size_t clen;
104658f0484fSRodney W. Grimes 
1047e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1048e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
104909264d74SAndrey A. Chernov 		clen = wcrtomb(buf, CHAR(*str), &mbs);
1050aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1) {
1051aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
105209264d74SAndrey A. Chernov 			*buf = (char)CHAR(*str);
1053aa3d69a6SAndrey A. Chernov 			clen = 1;
1054aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
1055aa3d69a6SAndrey A. Chernov 		}
105609264d74SAndrey A. Chernov 		if (CHAR(*str) == EOS)
105776e2bc01SPeter Wemm 			return (0);
105809264d74SAndrey A. Chernov 		str++;
1059e9346e01STim J. Robbins 		buf += clen;
1060e9346e01STim J. Robbins 		len -= clen;
106158f0484fSRodney W. Grimes 	}
106227d52f69SPeter Wemm 	return (1);
106327d52f69SPeter Wemm }
106458f0484fSRodney W. Grimes 
106558f0484fSRodney W. Grimes #ifdef DEBUG
106658f0484fSRodney W. Grimes static void
10671cec70adSXin LI qprintf(const char *str, Char *s)
106858f0484fSRodney W. Grimes {
1069b231cb39SDavid E. O'Brien 	Char *p;
107058f0484fSRodney W. Grimes 
1071bd7a9850SAndrey A. Chernov 	(void)printf("%s\n", str);
1072bd7a9850SAndrey A. Chernov 	if (s != NULL) {
1073bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1074bd7a9850SAndrey A. Chernov 			(void)printf("%c", (char)CHAR(*p));
107558f0484fSRodney W. Grimes 		(void)printf("\n");
1076bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1077bd7a9850SAndrey A. Chernov 			(void)printf("%c", (isprot(*p) ? '\\' : ' '));
107858f0484fSRodney W. Grimes 		(void)printf("\n");
1079bd7a9850SAndrey A. Chernov 		for (p = s; *p != EOS; p++)
1080bd7a9850SAndrey A. Chernov 			(void)printf("%c", (ismeta(*p) ? '_' : ' '));
108158f0484fSRodney W. Grimes 		(void)printf("\n");
108258f0484fSRodney W. Grimes 	}
1083bd7a9850SAndrey A. Chernov }
108458f0484fSRodney W. Grimes #endif
1085