xref: /freebsd/lib/libc/gen/glob.c (revision 7455a07a9fc314266b9222fb81c8b839b72d491f)
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 
13158f0484fSRodney W. Grimes #ifndef DEBUG
13258f0484fSRodney W. Grimes 
133e9346e01STim J. Robbins #define	M_QUOTE		0x8000000000ULL
134e9346e01STim J. Robbins #define	M_PROTECT	0x4000000000ULL
135e9346e01STim J. Robbins #define	M_MASK		0xffffffffffULL
136e9346e01STim J. Robbins #define	M_CHAR		0x00ffffffffULL
13758f0484fSRodney W. Grimes 
138e9346e01STim J. Robbins typedef uint_fast64_t Char;
13958f0484fSRodney W. Grimes 
14058f0484fSRodney W. Grimes #else
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes #define	M_QUOTE		0x80
14358f0484fSRodney W. Grimes #define	M_PROTECT	0x40
14458f0484fSRodney W. Grimes #define	M_MASK		0xff
145e9346e01STim J. Robbins #define	M_CHAR		0x7f
14658f0484fSRodney W. Grimes 
14758f0484fSRodney W. Grimes typedef char Char;
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes #endif
15058f0484fSRodney W. Grimes 
15158f0484fSRodney W. Grimes 
152e9346e01STim J. Robbins #define	CHAR(c)		((Char)((c)&M_CHAR))
15358f0484fSRodney W. Grimes #define	META(c)		((Char)((c)|M_QUOTE))
154aed721ecSAndrey A. Chernov #define	UNPROT(c)	((c) & ~M_PROTECT)
155aa3d69a6SAndrey A. Chernov #define	M_ALL		META(L'*')
156aa3d69a6SAndrey A. Chernov #define	M_END		META(L']')
157aa3d69a6SAndrey A. Chernov #define	M_NOT		META(L'!')
158aa3d69a6SAndrey A. Chernov #define	M_ONE		META(L'?')
159aa3d69a6SAndrey A. Chernov #define	M_RNG		META(L'-')
160aa3d69a6SAndrey A. Chernov #define	M_SET		META(L'[')
16158f0484fSRodney W. Grimes #define	ismeta(c)	(((c)&M_QUOTE) != 0)
162aed721ecSAndrey A. Chernov #define	isprot(c)	(((c)&M_PROTECT) != 0)
16358f0484fSRodney W. Grimes 
16458f0484fSRodney W. Grimes 
165b231cb39SDavid E. O'Brien static int	 compare(const void *, const void *);
166aed721ecSAndrey A. Chernov static int	 g_Ctoc(const Char *, char *, size_t, int);
167b231cb39SDavid E. O'Brien static int	 g_lstat(Char *, struct stat *, glob_t *);
168b231cb39SDavid E. O'Brien static DIR	*g_opendir(Char *, glob_t *);
16934a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t);
17058f0484fSRodney W. Grimes #ifdef notdef
171b231cb39SDavid E. O'Brien static Char	*g_strcat(Char *, const Char *);
17258f0484fSRodney W. Grimes #endif
173b231cb39SDavid E. O'Brien static int	 g_stat(Char *, struct stat *, glob_t *);
174daab0b01SMarcel Moolenaar static int	 glob0(const Char *, glob_t *, struct glob_limit *);
175daab0b01SMarcel Moolenaar static int	 glob1(Char *, glob_t *, struct glob_limit *);
176daab0b01SMarcel Moolenaar static int	 glob2(Char *, Char *, Char *, Char *, glob_t *,
177daab0b01SMarcel Moolenaar     struct glob_limit *);
178daab0b01SMarcel Moolenaar static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
179daab0b01SMarcel Moolenaar     struct glob_limit *);
180aed721ecSAndrey A. Chernov static int	 globextend(const Char *, glob_t *, struct glob_limit *, int);
181487dbd92SPeter Wemm static const Char *
182b231cb39SDavid E. O'Brien 		 globtilde(const Char *, Char *, size_t, glob_t *);
183daab0b01SMarcel Moolenaar static int	 globexp1(const Char *, glob_t *, struct glob_limit *);
184daab0b01SMarcel Moolenaar static int	 globexp2(const Char *, const Char *, glob_t *, int *,
185daab0b01SMarcel Moolenaar     struct glob_limit *);
186b231cb39SDavid E. O'Brien static int	 match(Char *, Char *, Char *);
18758f0484fSRodney W. Grimes #ifdef DEBUG
188b231cb39SDavid E. O'Brien static void	 qprintf(const char *, Char *);
18958f0484fSRodney W. Grimes #endif
19058f0484fSRodney W. Grimes 
19158f0484fSRodney W. Grimes int
1920d6d372cSEitan Adler glob(const char * __restrict pattern, int flags,
1930d6d372cSEitan Adler 	 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
19458f0484fSRodney W. Grimes {
195daab0b01SMarcel Moolenaar 	struct glob_limit limit = { 0, 0, 0, 0, 0 };
1961cec70adSXin LI 	const char *patnext;
197e9346e01STim J. Robbins 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
198e9346e01STim J. Robbins 	mbstate_t mbs;
199e9346e01STim J. Robbins 	wchar_t wc;
200e9346e01STim J. Robbins 	size_t clen;
201aed721ecSAndrey A. Chernov 	int too_long;
20258f0484fSRodney W. Grimes 
2031cec70adSXin LI 	patnext = pattern;
20458f0484fSRodney W. Grimes 	if (!(flags & GLOB_APPEND)) {
20558f0484fSRodney W. Grimes 		pglob->gl_pathc = 0;
20658f0484fSRodney W. Grimes 		pglob->gl_pathv = NULL;
20758f0484fSRodney W. Grimes 		if (!(flags & GLOB_DOOFFS))
20858f0484fSRodney W. Grimes 			pglob->gl_offs = 0;
20958f0484fSRodney W. Grimes 	}
21075dc5f1aSMike Heffner 	if (flags & GLOB_LIMIT) {
211daab0b01SMarcel Moolenaar 		limit.l_path_lim = pglob->gl_matchc;
212daab0b01SMarcel Moolenaar 		if (limit.l_path_lim == 0)
213daab0b01SMarcel Moolenaar 			limit.l_path_lim = GLOB_LIMIT_PATH;
214daab0b01SMarcel Moolenaar 	}
21558f0484fSRodney W. Grimes 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
21658f0484fSRodney W. Grimes 	pglob->gl_errfunc = errfunc;
21758f0484fSRodney W. Grimes 	pglob->gl_matchc = 0;
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes 	bufnext = patbuf;
220487dbd92SPeter Wemm 	bufend = bufnext + MAXPATHLEN - 1;
221aed721ecSAndrey A. Chernov 	too_long = 1;
222e9346e01STim J. Robbins 	if (flags & GLOB_NOESCAPE) {
223e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
224aed721ecSAndrey A. Chernov 		while (bufnext <= bufend) {
225e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
226e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
227e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
228aed721ecSAndrey A. Chernov 			else if (clen == 0) {
229aed721ecSAndrey A. Chernov 				too_long = 0;
230e9346e01STim J. Robbins 				break;
231aed721ecSAndrey A. Chernov 			}
232e9346e01STim J. Robbins 			*bufnext++ = wc;
233e9346e01STim J. Robbins 			patnext += clen;
234e9346e01STim J. Robbins 		}
235e9346e01STim J. Robbins 	} else {
23658f0484fSRodney W. Grimes 		/* Protect the quoted characters. */
237e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
238aed721ecSAndrey A. Chernov 		while (bufnext <= bufend) {
239aa3d69a6SAndrey A. Chernov 			if (*patnext == '\\') {
240aa3d69a6SAndrey A. Chernov 				if (*++patnext == '\0') {
241aed721ecSAndrey A. Chernov 					*bufnext++ = QUOTE;
242e9346e01STim J. Robbins 					continue;
24358f0484fSRodney W. Grimes 				}
244e9346e01STim J. Robbins 				prot = M_PROTECT;
245e9346e01STim J. Robbins 			} else
246e9346e01STim J. Robbins 				prot = 0;
247e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
248e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
249e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
250aed721ecSAndrey A. Chernov 			else if (clen == 0) {
251aed721ecSAndrey A. Chernov 				too_long = 0;
252e9346e01STim J. Robbins 				break;
253aed721ecSAndrey A. Chernov 			}
254aed721ecSAndrey A. Chernov 			*bufnext++ = wc | prot;
255e9346e01STim J. Robbins 			patnext += clen;
25658f0484fSRodney W. Grimes 		}
25758f0484fSRodney W. Grimes 	}
258aed721ecSAndrey A. Chernov 	if (too_long)
259aed721ecSAndrey A. Chernov 		return (GLOB_NOMATCH);
26058f0484fSRodney W. Grimes 	*bufnext = EOS;
26158f0484fSRodney W. Grimes 
26258f0484fSRodney W. Grimes 	if (flags & GLOB_BRACE)
26385529174SEitan Adler 	    return (globexp1(patbuf, pglob, &limit));
26458f0484fSRodney W. Grimes 	else
26585529174SEitan Adler 	    return (glob0(patbuf, pglob, &limit));
26658f0484fSRodney W. Grimes }
26758f0484fSRodney W. Grimes 
26858f0484fSRodney W. Grimes /*
26958f0484fSRodney W. Grimes  * Expand recursively a glob {} pattern. When there is no more expansion
27058f0484fSRodney W. Grimes  * invoke the standard globbing routine to glob the rest of the magic
27158f0484fSRodney W. Grimes  * characters
27258f0484fSRodney W. Grimes  */
273487dbd92SPeter Wemm static int
274daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
27558f0484fSRodney W. Grimes {
27658f0484fSRodney W. Grimes 	const Char* ptr = pattern;
27758f0484fSRodney W. Grimes 	int rv;
27858f0484fSRodney W. Grimes 
279daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
280daab0b01SMarcel Moolenaar 	    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
281daab0b01SMarcel Moolenaar 		errno = 0;
282daab0b01SMarcel Moolenaar 		return (GLOB_NOSPACE);
283daab0b01SMarcel Moolenaar 	}
284daab0b01SMarcel Moolenaar 
28558f0484fSRodney W. Grimes 	/* Protect a single {}, for find(1), like csh */
28658f0484fSRodney W. Grimes 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
287bae8632fSJonathan Lemon 		return glob0(pattern, pglob, limit);
28858f0484fSRodney W. Grimes 
28934a08754SMike Makonnen 	while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
290bae8632fSJonathan Lemon 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
29158f0484fSRodney W. Grimes 			return rv;
29258f0484fSRodney W. Grimes 
293bae8632fSJonathan Lemon 	return glob0(pattern, pglob, limit);
29458f0484fSRodney W. Grimes }
29558f0484fSRodney W. Grimes 
29658f0484fSRodney W. Grimes 
29758f0484fSRodney W. Grimes /*
29858f0484fSRodney W. Grimes  * Recursive brace globbing helper. Tries to expand a single brace.
29958f0484fSRodney W. Grimes  * If it succeeds then it invokes globexp1 with the new pattern.
30058f0484fSRodney W. Grimes  * If it fails then it tries to glob the rest of the pattern and returns.
30158f0484fSRodney W. Grimes  */
302487dbd92SPeter Wemm static int
303daab0b01SMarcel Moolenaar globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
304daab0b01SMarcel Moolenaar     struct glob_limit *limit)
30558f0484fSRodney W. Grimes {
30658f0484fSRodney W. Grimes 	int     i;
30758f0484fSRodney W. Grimes 	Char   *lm, *ls;
308369316a8SAndrey A. Chernov 	const Char *pe, *pm, *pm1, *pl;
309487dbd92SPeter Wemm 	Char    patbuf[MAXPATHLEN];
31058f0484fSRodney W. Grimes 
31158f0484fSRodney W. Grimes 	/* copy part up to the brace */
31258f0484fSRodney W. Grimes 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
31358f0484fSRodney W. Grimes 		continue;
314487dbd92SPeter Wemm 	*lm = EOS;
31558f0484fSRodney W. Grimes 	ls = lm;
31658f0484fSRodney W. Grimes 
31758f0484fSRodney W. Grimes 	/* Find the balanced brace */
31858f0484fSRodney W. Grimes 	for (i = 0, pe = ++ptr; *pe; pe++)
31958f0484fSRodney W. Grimes 		if (*pe == LBRACKET) {
32058f0484fSRodney W. Grimes 			/* Ignore everything between [] */
32158f0484fSRodney W. Grimes 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
32258f0484fSRodney W. Grimes 				continue;
32358f0484fSRodney W. Grimes 			if (*pe == EOS) {
32458f0484fSRodney W. Grimes 				/*
32558f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
32658f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
32758f0484fSRodney W. Grimes 				 */
32858f0484fSRodney W. Grimes 				pe = pm;
32958f0484fSRodney W. Grimes 			}
33058f0484fSRodney W. Grimes 		}
33158f0484fSRodney W. Grimes 		else if (*pe == LBRACE)
33258f0484fSRodney W. Grimes 			i++;
33358f0484fSRodney W. Grimes 		else if (*pe == RBRACE) {
33458f0484fSRodney W. Grimes 			if (i == 0)
33558f0484fSRodney W. Grimes 				break;
33658f0484fSRodney W. Grimes 			i--;
33758f0484fSRodney W. Grimes 		}
33858f0484fSRodney W. Grimes 
33958f0484fSRodney W. Grimes 	/* Non matching braces; just glob the pattern */
34058f0484fSRodney W. Grimes 	if (i != 0 || *pe == EOS) {
341bae8632fSJonathan Lemon 		*rv = glob0(patbuf, pglob, limit);
34285529174SEitan Adler 		return (0);
34358f0484fSRodney W. Grimes 	}
34458f0484fSRodney W. Grimes 
34558f0484fSRodney W. Grimes 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
34658f0484fSRodney W. Grimes 		switch (*pm) {
34758f0484fSRodney W. Grimes 		case LBRACKET:
34858f0484fSRodney W. Grimes 			/* Ignore everything between [] */
349369316a8SAndrey A. Chernov 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
35058f0484fSRodney W. Grimes 				continue;
35158f0484fSRodney W. Grimes 			if (*pm == EOS) {
35258f0484fSRodney W. Grimes 				/*
35358f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
35458f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
35558f0484fSRodney W. Grimes 				 */
356369316a8SAndrey A. Chernov 				pm = pm1;
35758f0484fSRodney W. Grimes 			}
35858f0484fSRodney W. Grimes 			break;
35958f0484fSRodney W. Grimes 
36058f0484fSRodney W. Grimes 		case LBRACE:
36158f0484fSRodney W. Grimes 			i++;
36258f0484fSRodney W. Grimes 			break;
36358f0484fSRodney W. Grimes 
36458f0484fSRodney W. Grimes 		case RBRACE:
36558f0484fSRodney W. Grimes 			if (i) {
36658f0484fSRodney W. Grimes 			    i--;
36758f0484fSRodney W. Grimes 			    break;
36858f0484fSRodney W. Grimes 			}
36958f0484fSRodney W. Grimes 			/* FALLTHROUGH */
37058f0484fSRodney W. Grimes 		case COMMA:
37158f0484fSRodney W. Grimes 			if (i && *pm == COMMA)
37258f0484fSRodney W. Grimes 				break;
37358f0484fSRodney W. Grimes 			else {
37458f0484fSRodney W. Grimes 				/* Append the current string */
37558f0484fSRodney W. Grimes 				for (lm = ls; (pl < pm); *lm++ = *pl++)
37658f0484fSRodney W. Grimes 					continue;
37758f0484fSRodney W. Grimes 				/*
37858f0484fSRodney W. Grimes 				 * Append the rest of the pattern after the
37958f0484fSRodney W. Grimes 				 * closing brace
38058f0484fSRodney W. Grimes 				 */
38158f0484fSRodney W. Grimes 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
38258f0484fSRodney W. Grimes 					continue;
38358f0484fSRodney W. Grimes 
38458f0484fSRodney W. Grimes 				/* Expand the current pattern */
38558f0484fSRodney W. Grimes #ifdef DEBUG
38658f0484fSRodney W. Grimes 				qprintf("globexp2:", patbuf);
38758f0484fSRodney W. Grimes #endif
388bae8632fSJonathan Lemon 				*rv = globexp1(patbuf, pglob, limit);
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes 				/* move after the comma, to the next string */
39158f0484fSRodney W. Grimes 				pl = pm + 1;
39258f0484fSRodney W. Grimes 			}
39358f0484fSRodney W. Grimes 			break;
39458f0484fSRodney W. Grimes 
39558f0484fSRodney W. Grimes 		default:
39658f0484fSRodney W. Grimes 			break;
39758f0484fSRodney W. Grimes 		}
39858f0484fSRodney W. Grimes 	*rv = 0;
39985529174SEitan Adler 	return (0);
40058f0484fSRodney W. Grimes }
40158f0484fSRodney W. Grimes 
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 
40458f0484fSRodney W. Grimes /*
40558f0484fSRodney W. Grimes  * expand tilde from the passwd file.
40658f0484fSRodney W. Grimes  */
40758f0484fSRodney W. Grimes static const Char *
4081cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
40958f0484fSRodney W. Grimes {
41058f0484fSRodney W. Grimes 	struct passwd *pwd;
411aa3d69a6SAndrey A. Chernov 	char *h, *sc;
41258f0484fSRodney W. Grimes 	const Char *p;
41362f187a4SWarner Losh 	Char *b, *eb;
414aa3d69a6SAndrey A. Chernov 	wchar_t wc;
415aa3d69a6SAndrey A. Chernov 	wchar_t wbuf[MAXPATHLEN];
416aa3d69a6SAndrey A. Chernov 	wchar_t *wbufend, *dc;
417aa3d69a6SAndrey A. Chernov 	size_t clen;
418aa3d69a6SAndrey A. Chernov 	mbstate_t mbs;
419aa3d69a6SAndrey A. Chernov 	int too_long;
42058f0484fSRodney W. Grimes 
42158f0484fSRodney W. Grimes 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
42285529174SEitan Adler 		return (pattern);
42358f0484fSRodney W. Grimes 
42462f187a4SWarner Losh 	/*
42562f187a4SWarner Losh 	 * Copy up to the end of the string or /
42662f187a4SWarner Losh 	 */
42762f187a4SWarner Losh 	eb = &patbuf[patbuf_len - 1];
428aa3d69a6SAndrey A. Chernov 	for (p = pattern + 1, b = patbuf;
429*7455a07aSAndrey A. Chernov 	    b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
43058f0484fSRodney W. Grimes 		continue;
43158f0484fSRodney W. Grimes 
432*7455a07aSAndrey A. Chernov 	if (*p != EOS && UNPROT(*p) != SEP)
433f4d4982eSAndrey A. Chernov 		return (NULL);
43458f0484fSRodney W. Grimes 
435aa3d69a6SAndrey A. Chernov 	*b = EOS;
436aa3d69a6SAndrey A. Chernov 	h = NULL;
437aa3d69a6SAndrey A. Chernov 
438aa3d69a6SAndrey A. Chernov 	if (patbuf[0] == EOS) {
43958f0484fSRodney W. Grimes 		/*
4403fa69daeSWarner Losh 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
4413fa69daeSWarner Losh 		 * we're not running setuid or setgid) and then trying
4423fa69daeSWarner Losh 		 * the password file
44358f0484fSRodney W. Grimes 		 */
4444539e95aSTim J. Robbins 		if (issetugid() != 0 ||
4459fcbcd02SJohn Birrell 		    (h = getenv("HOME")) == NULL) {
446eb8eee5aSAndrey A. Chernov 			if (((h = getlogin()) != NULL &&
447eb8eee5aSAndrey A. Chernov 			     (pwd = getpwnam(h)) != NULL) ||
448eb8eee5aSAndrey A. Chernov 			    (pwd = getpwuid(getuid())) != NULL)
44958f0484fSRodney W. Grimes 				h = pwd->pw_dir;
450eb8eee5aSAndrey A. Chernov 			else
45185529174SEitan Adler 				return (pattern);
45258f0484fSRodney W. Grimes 		}
45358f0484fSRodney W. Grimes 	}
45458f0484fSRodney W. Grimes 	else {
45558f0484fSRodney W. Grimes 		/*
45658f0484fSRodney W. Grimes 		 * Expand a ~user
45758f0484fSRodney W. Grimes 		 */
458aed721ecSAndrey A. Chernov 		if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf), 0))
459f4d4982eSAndrey A. Chernov 			return (NULL);
460f4d4982eSAndrey A. Chernov 		if ((pwd = getpwnam((char *)wbuf)) == NULL)
46185529174SEitan Adler 			return (pattern);
46258f0484fSRodney W. Grimes 		else
46358f0484fSRodney W. Grimes 			h = pwd->pw_dir;
46458f0484fSRodney W. Grimes 	}
46558f0484fSRodney W. Grimes 
46658f0484fSRodney W. Grimes 	/* Copy the home directory */
467aa3d69a6SAndrey A. Chernov 	dc = wbuf;
468aa3d69a6SAndrey A. Chernov 	sc = h;
469aa3d69a6SAndrey A. Chernov 	wbufend = wbuf + MAXPATHLEN - 1;
470aa3d69a6SAndrey A. Chernov 	too_long = 1;
471aa3d69a6SAndrey A. Chernov 	memset(&mbs, 0, sizeof(mbs));
472aa3d69a6SAndrey A. Chernov 	while (dc <= wbufend) {
473aa3d69a6SAndrey A. Chernov 		clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
474aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1 || clen == (size_t)-2) {
475aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
476aa3d69a6SAndrey A. Chernov 			wc = (unsigned char)*sc;
477aa3d69a6SAndrey A. Chernov 			clen = 1;
478aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
479aa3d69a6SAndrey A. Chernov 		}
480aa3d69a6SAndrey A. Chernov 		if ((*dc++ = wc) == EOS) {
481aa3d69a6SAndrey A. Chernov 			too_long = 0;
482aa3d69a6SAndrey A. Chernov 			break;
483aa3d69a6SAndrey A. Chernov 		}
484aa3d69a6SAndrey A. Chernov 		sc += clen;
485aa3d69a6SAndrey A. Chernov 	}
486aa3d69a6SAndrey A. Chernov 	if (too_long)
487f4d4982eSAndrey A. Chernov 		return (NULL);
488aa3d69a6SAndrey A. Chernov 
489aa3d69a6SAndrey A. Chernov 	dc = wbuf;
490aed721ecSAndrey A. Chernov 	for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
491aed721ecSAndrey A. Chernov 		continue;
492aa3d69a6SAndrey A. Chernov 	if (*dc != EOS)
493f4d4982eSAndrey A. Chernov 		return (NULL);
49458f0484fSRodney W. Grimes 
49558f0484fSRodney W. Grimes 	/* Append the rest of the pattern */
496aa3d69a6SAndrey A. Chernov 	if (*p != EOS) {
497aa3d69a6SAndrey A. Chernov 		too_long = 1;
498aa3d69a6SAndrey A. Chernov 		while (b <= eb) {
499aa3d69a6SAndrey A. Chernov 			if ((*b++ = *p++) == EOS) {
500aa3d69a6SAndrey A. Chernov 				too_long = 0;
501aa3d69a6SAndrey A. Chernov 				break;
502aa3d69a6SAndrey A. Chernov 			}
503aa3d69a6SAndrey A. Chernov 		}
504aa3d69a6SAndrey A. Chernov 		if (too_long)
505f4d4982eSAndrey A. Chernov 			return (NULL);
506aa3d69a6SAndrey A. Chernov 	} else
50762f187a4SWarner Losh 		*b = EOS;
50858f0484fSRodney W. Grimes 
50985529174SEitan Adler 	return (patbuf);
51058f0484fSRodney W. Grimes }
51158f0484fSRodney W. Grimes 
51258f0484fSRodney W. Grimes 
51358f0484fSRodney W. Grimes /*
51458f0484fSRodney W. Grimes  * The main glob() routine: compiles the pattern (optionally processing
51558f0484fSRodney W. Grimes  * quotes), calls glob1() to do the real pattern matching, and finally
51658f0484fSRodney W. Grimes  * sorts the list (unless unsorted operation is requested).  Returns 0
5174a59c3abSMike Heffner  * if things went well, nonzero if errors occurred.
51858f0484fSRodney W. Grimes  */
51958f0484fSRodney W. Grimes static int
520daab0b01SMarcel Moolenaar glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
52158f0484fSRodney W. Grimes {
52258f0484fSRodney W. Grimes 	const Char *qpatnext;
5235b3fab81SGordon Tetlow 	int err;
5244b767fa6SAndrey A. Chernov 	size_t oldpathc;
5255b3fab81SGordon Tetlow 	Char *bufnext, c, patbuf[MAXPATHLEN];
52658f0484fSRodney W. Grimes 
527487dbd92SPeter Wemm 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
528f4d4982eSAndrey A. Chernov 	if (qpatnext == NULL) {
529f4d4982eSAndrey A. Chernov 		errno = 0;
530f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
531f4d4982eSAndrey A. Chernov 	}
53258f0484fSRodney W. Grimes 	oldpathc = pglob->gl_pathc;
53358f0484fSRodney W. Grimes 	bufnext = patbuf;
53458f0484fSRodney W. Grimes 
53558f0484fSRodney W. Grimes 	/* We don't need to check for buffer overflow any more. */
53658f0484fSRodney W. Grimes 	while ((c = *qpatnext++) != EOS) {
53758f0484fSRodney W. Grimes 		switch (c) {
53858f0484fSRodney W. Grimes 		case LBRACKET:
53958f0484fSRodney W. Grimes 			c = *qpatnext;
54058f0484fSRodney W. Grimes 			if (c == NOT)
54158f0484fSRodney W. Grimes 				++qpatnext;
54258f0484fSRodney W. Grimes 			if (*qpatnext == EOS ||
54334a08754SMike Makonnen 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
54458f0484fSRodney W. Grimes 				*bufnext++ = LBRACKET;
54558f0484fSRodney W. Grimes 				if (c == NOT)
54658f0484fSRodney W. Grimes 					--qpatnext;
54758f0484fSRodney W. Grimes 				break;
54858f0484fSRodney W. Grimes 			}
54958f0484fSRodney W. Grimes 			*bufnext++ = M_SET;
55058f0484fSRodney W. Grimes 			if (c == NOT)
55158f0484fSRodney W. Grimes 				*bufnext++ = M_NOT;
55258f0484fSRodney W. Grimes 			c = *qpatnext++;
55358f0484fSRodney W. Grimes 			do {
55458f0484fSRodney W. Grimes 				*bufnext++ = CHAR(c);
55558f0484fSRodney W. Grimes 				if (*qpatnext == RANGE &&
55658f0484fSRodney W. Grimes 				    (c = qpatnext[1]) != RBRACKET) {
55758f0484fSRodney W. Grimes 					*bufnext++ = M_RNG;
55858f0484fSRodney W. Grimes 					*bufnext++ = CHAR(c);
55958f0484fSRodney W. Grimes 					qpatnext += 2;
56058f0484fSRodney W. Grimes 				}
56158f0484fSRodney W. Grimes 			} while ((c = *qpatnext++) != RBRACKET);
56258f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
56358f0484fSRodney W. Grimes 			*bufnext++ = M_END;
56458f0484fSRodney W. Grimes 			break;
56558f0484fSRodney W. Grimes 		case QUESTION:
56658f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
56758f0484fSRodney W. Grimes 			*bufnext++ = M_ONE;
56858f0484fSRodney W. Grimes 			break;
56958f0484fSRodney W. Grimes 		case STAR:
57058f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
57158f0484fSRodney W. Grimes 			/* collapse adjacent stars to one,
57258f0484fSRodney W. Grimes 			 * to avoid exponential behavior
57358f0484fSRodney W. Grimes 			 */
57458f0484fSRodney W. Grimes 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
57558f0484fSRodney W. Grimes 			    *bufnext++ = M_ALL;
57658f0484fSRodney W. Grimes 			break;
57758f0484fSRodney W. Grimes 		default:
57858f0484fSRodney W. Grimes 			*bufnext++ = CHAR(c);
57958f0484fSRodney W. Grimes 			break;
58058f0484fSRodney W. Grimes 		}
58158f0484fSRodney W. Grimes 	}
58258f0484fSRodney W. Grimes 	*bufnext = EOS;
58358f0484fSRodney W. Grimes #ifdef DEBUG
58458f0484fSRodney W. Grimes 	qprintf("glob0:", patbuf);
58558f0484fSRodney W. Grimes #endif
58658f0484fSRodney W. Grimes 
587bae8632fSJonathan Lemon 	if ((err = glob1(patbuf, pglob, limit)) != 0)
58858f0484fSRodney W. Grimes 		return(err);
58958f0484fSRodney W. Grimes 
59058f0484fSRodney W. Grimes 	/*
59158f0484fSRodney W. Grimes 	 * If there was no match we are going to append the pattern
59258f0484fSRodney W. Grimes 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
59358f0484fSRodney W. Grimes 	 * and the pattern did not contain any magic characters
59458f0484fSRodney W. Grimes 	 * GLOB_NOMAGIC is there just for compatibility with csh.
59558f0484fSRodney W. Grimes 	 */
5964a59c3abSMike Heffner 	if (pglob->gl_pathc == oldpathc) {
5974a59c3abSMike Heffner 		if (((pglob->gl_flags & GLOB_NOCHECK) ||
59858f0484fSRodney W. Grimes 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
59958f0484fSRodney W. Grimes 			!(pglob->gl_flags & GLOB_MAGCHAR))))
600aed721ecSAndrey A. Chernov 			return (globextend(pattern, pglob, limit, 1));
6014a59c3abSMike Heffner 		else
6024a59c3abSMike Heffner 			return (GLOB_NOMATCH);
6034a59c3abSMike Heffner 	}
6044a59c3abSMike Heffner 	if (!(pglob->gl_flags & GLOB_NOSORT))
60558f0484fSRodney W. Grimes 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
60658f0484fSRodney W. Grimes 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
60758f0484fSRodney W. Grimes 	return (0);
60858f0484fSRodney W. Grimes }
60958f0484fSRodney W. Grimes 
61058f0484fSRodney W. Grimes static int
6111cec70adSXin LI compare(const void *p, const void *q)
61258f0484fSRodney W. Grimes {
613aa3d69a6SAndrey A. Chernov 	return (strcoll(*(char **)p, *(char **)q));
61458f0484fSRodney W. Grimes }
61558f0484fSRodney W. Grimes 
61658f0484fSRodney W. Grimes static int
617daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
61858f0484fSRodney W. Grimes {
619487dbd92SPeter Wemm 	Char pathbuf[MAXPATHLEN];
62058f0484fSRodney W. Grimes 
62158f0484fSRodney W. Grimes 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
62258f0484fSRodney W. Grimes 	if (*pattern == EOS)
62358f0484fSRodney W. Grimes 		return (0);
624487dbd92SPeter Wemm 	return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
625487dbd92SPeter Wemm 	    pattern, pglob, limit));
62658f0484fSRodney W. Grimes }
62758f0484fSRodney W. Grimes 
62858f0484fSRodney W. Grimes /*
62958f0484fSRodney W. Grimes  * The functions glob2 and glob3 are mutually recursive; there is one level
63058f0484fSRodney W. Grimes  * of recursion for each segment in the pattern that contains one or more
63158f0484fSRodney W. Grimes  * meta characters.
63258f0484fSRodney W. Grimes  */
63358f0484fSRodney W. Grimes static int
6341cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
635daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
63658f0484fSRodney W. Grimes {
63758f0484fSRodney W. Grimes 	struct stat sb;
63858f0484fSRodney W. Grimes 	Char *p, *q;
63958f0484fSRodney W. Grimes 	int anymeta;
64058f0484fSRodney W. Grimes 
64158f0484fSRodney W. Grimes 	/*
64258f0484fSRodney W. Grimes 	 * Loop over pattern segments until end of pattern or until
64358f0484fSRodney W. Grimes 	 * segment with meta character found.
64458f0484fSRodney W. Grimes 	 */
64558f0484fSRodney W. Grimes 	for (anymeta = 0;;) {
64658f0484fSRodney W. Grimes 		if (*pattern == EOS) {		/* End of pattern? */
64758f0484fSRodney W. Grimes 			*pathend = EOS;
64858f0484fSRodney W. Grimes 			if (g_lstat(pathbuf, &sb, pglob))
64958f0484fSRodney W. Grimes 				return (0);
65058f0484fSRodney W. Grimes 
651daab0b01SMarcel Moolenaar 			if ((pglob->gl_flags & GLOB_LIMIT) &&
652daab0b01SMarcel Moolenaar 			    limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
653daab0b01SMarcel Moolenaar 				errno = 0;
654daab0b01SMarcel Moolenaar 				return (GLOB_NOSPACE);
655daab0b01SMarcel Moolenaar 			}
656aed721ecSAndrey A. Chernov 			if ((pglob->gl_flags & GLOB_MARK) &&
657aed721ecSAndrey A. Chernov 			    UNPROT(pathend[-1]) != SEP &&
658aed721ecSAndrey A. Chernov 			    (S_ISDIR(sb.st_mode) ||
659aed721ecSAndrey A. Chernov 			    (S_ISLNK(sb.st_mode) &&
660aed721ecSAndrey A. Chernov 			    g_stat(pathbuf, &sb, pglob) == 0 &&
66158f0484fSRodney W. Grimes 			    S_ISDIR(sb.st_mode)))) {
662f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
663f4d4982eSAndrey A. Chernov 					errno = 0;
664f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
665f4d4982eSAndrey A. Chernov 				}
66658f0484fSRodney W. Grimes 				*pathend++ = SEP;
66758f0484fSRodney W. Grimes 				*pathend = EOS;
66858f0484fSRodney W. Grimes 			}
66958f0484fSRodney W. Grimes 			++pglob->gl_matchc;
670aed721ecSAndrey A. Chernov 			return (globextend(pathbuf, pglob, limit, 0));
67158f0484fSRodney W. Grimes 		}
67258f0484fSRodney W. Grimes 
67358f0484fSRodney W. Grimes 		/* Find end of next segment, copy tentatively to pathend. */
67458f0484fSRodney W. Grimes 		q = pathend;
67558f0484fSRodney W. Grimes 		p = pattern;
676aed721ecSAndrey A. Chernov 		while (*p != EOS && UNPROT(*p) != SEP) {
67758f0484fSRodney W. Grimes 			if (ismeta(*p))
67858f0484fSRodney W. Grimes 				anymeta = 1;
679f4d4982eSAndrey A. Chernov 			if (q + 1 > pathend_last) {
680f4d4982eSAndrey A. Chernov 				errno = 0;
681f4d4982eSAndrey A. Chernov 				return (GLOB_NOSPACE);
682f4d4982eSAndrey A. Chernov 			}
68358f0484fSRodney W. Grimes 			*q++ = *p++;
68458f0484fSRodney W. Grimes 		}
68558f0484fSRodney W. Grimes 
68658f0484fSRodney W. Grimes 		if (!anymeta) {		/* No expansion, do next segment. */
68758f0484fSRodney W. Grimes 			pathend = q;
68858f0484fSRodney W. Grimes 			pattern = p;
689aed721ecSAndrey A. Chernov 			while (UNPROT(*pattern) == SEP) {
690f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
691f4d4982eSAndrey A. Chernov 					errno = 0;
692f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
693f4d4982eSAndrey A. Chernov 				}
69458f0484fSRodney W. Grimes 				*pathend++ = *pattern++;
695487dbd92SPeter Wemm 			}
69658f0484fSRodney W. Grimes 		} else			/* Need expansion, recurse. */
69785529174SEitan Adler 			return (glob3(pathbuf, pathend, pathend_last, pattern,
69885529174SEitan Adler 			    p, pglob, limit));
69958f0484fSRodney W. Grimes 	}
70058f0484fSRodney W. Grimes 	/* NOTREACHED */
70158f0484fSRodney W. Grimes }
70258f0484fSRodney W. Grimes 
70358f0484fSRodney W. Grimes static int
7041cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
7051cec70adSXin LI       Char *pattern, Char *restpattern,
706daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
70758f0484fSRodney W. Grimes {
708b231cb39SDavid E. O'Brien 	struct dirent *dp;
70958f0484fSRodney W. Grimes 	DIR *dirp;
710aed721ecSAndrey A. Chernov 	int err, too_long, saverrno;
711e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
71258f0484fSRodney W. Grimes 
7133e2981e8SCraig Rodrigues 	struct dirent *(*readdirfunc)(DIR *);
71458f0484fSRodney W. Grimes 
715aed721ecSAndrey A. Chernov 	if (pathend > pathend_last) {
71658f0484fSRodney W. Grimes 		errno = 0;
717f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
718aed721ecSAndrey A. Chernov 	}
719f4d4982eSAndrey A. Chernov 	*pathend = EOS;
720aed721ecSAndrey A. Chernov 	if (pglob->gl_errfunc != NULL &&
721aed721ecSAndrey A. Chernov 	    g_Ctoc(pathbuf, buf, sizeof(buf), 0)) {
722aed721ecSAndrey A. Chernov 		errno = 0;
723aed721ecSAndrey A. Chernov 		return (GLOB_NOSPACE);
724aed721ecSAndrey A. Chernov 	}
72558f0484fSRodney W. Grimes 
726aed721ecSAndrey A. Chernov 	errno = 0;
72758f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
728196d61a9SAndrey A. Chernov 		if (errno == ENOENT || errno == ENOTDIR)
729196d61a9SAndrey A. Chernov 			return (0);
730aed721ecSAndrey A. Chernov 		if ((pglob->gl_errfunc != NULL &&
731aed721ecSAndrey A. Chernov 		    pglob->gl_errfunc(buf, errno)) ||
732aed721ecSAndrey A. Chernov 		    (pglob->gl_flags & GLOB_ERR))
733d67355c5SAndrey A. Chernov 			return (GLOB_ABORTED);
73458f0484fSRodney W. Grimes 		return (0);
73558f0484fSRodney W. Grimes 	}
73658f0484fSRodney W. Grimes 
73758f0484fSRodney W. Grimes 	err = 0;
73858f0484fSRodney W. Grimes 
7393e2981e8SCraig Rodrigues 	/* pglob->gl_readdir takes a void *, fix this manually */
74058f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7413e2981e8SCraig Rodrigues 		readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
74258f0484fSRodney W. Grimes 	else
74358f0484fSRodney W. Grimes 		readdirfunc = readdir;
7443e2981e8SCraig Rodrigues 
745aed721ecSAndrey A. Chernov 	errno = 0;
7463e2981e8SCraig Rodrigues 	/* Search directory for matching names. */
7473e2981e8SCraig Rodrigues 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
7481cec70adSXin LI 		char *sc;
749b231cb39SDavid E. O'Brien 		Char *dc;
750e9346e01STim J. Robbins 		wchar_t wc;
751e9346e01STim J. Robbins 		size_t clen;
752e9346e01STim J. Robbins 		mbstate_t mbs;
75358f0484fSRodney W. Grimes 
754daab0b01SMarcel Moolenaar 		if ((pglob->gl_flags & GLOB_LIMIT) &&
755daab0b01SMarcel Moolenaar 		    limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
756daab0b01SMarcel Moolenaar 			errno = 0;
757f4d4982eSAndrey A. Chernov 			err = GLOB_NOSPACE;
758daab0b01SMarcel Moolenaar 			break;
759daab0b01SMarcel Moolenaar 		}
760daab0b01SMarcel Moolenaar 
76158f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
762aed721ecSAndrey A. Chernov 		if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT)
76358f0484fSRodney W. Grimes 			continue;
764e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
765487dbd92SPeter Wemm 		dc = pathend;
7661cec70adSXin LI 		sc = dp->d_name;
767aed721ecSAndrey A. Chernov 		too_long = 1;
768aed721ecSAndrey A. Chernov 		while (dc <= pathend_last) {
769e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
770e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
771aa3d69a6SAndrey A. Chernov 				/* XXX See initial comment #2. */
772aa3d69a6SAndrey A. Chernov 				wc = (unsigned char)*sc;
773e9346e01STim J. Robbins 				clen = 1;
774e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
775e9346e01STim J. Robbins 			}
776aed721ecSAndrey A. Chernov 			if ((*dc++ = wc) == EOS) {
777aed721ecSAndrey A. Chernov 				too_long = 0;
778e9346e01STim J. Robbins 				break;
779aed721ecSAndrey A. Chernov 			}
780e9346e01STim J. Robbins 			sc += clen;
781e9346e01STim J. Robbins 		}
782aed721ecSAndrey A. Chernov 		if (too_long || !match(pathend, pattern, restpattern)) {
78358f0484fSRodney W. Grimes 			*pathend = EOS;
78458f0484fSRodney W. Grimes 			continue;
78558f0484fSRodney W. Grimes 		}
786487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
787487dbd92SPeter Wemm 		    pglob, limit);
78858f0484fSRodney W. Grimes 		if (err)
78958f0484fSRodney W. Grimes 			break;
790aed721ecSAndrey A. Chernov 		errno = 0;
79158f0484fSRodney W. Grimes 	}
79258f0484fSRodney W. Grimes 
793aed721ecSAndrey A. Chernov 	saverrno = errno;
79458f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
79558f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
79658f0484fSRodney W. Grimes 	else
79758f0484fSRodney W. Grimes 		closedir(dirp);
798aed721ecSAndrey A. Chernov 	errno = saverrno;
799aed721ecSAndrey A. Chernov 
800aed721ecSAndrey A. Chernov 	if (err)
80158f0484fSRodney W. Grimes 		return (err);
802aed721ecSAndrey A. Chernov 
803aed721ecSAndrey A. Chernov 	if (dp == NULL && errno != 0 && ((pglob->gl_errfunc != NULL &&
804aed721ecSAndrey A. Chernov 	    pglob->gl_errfunc(buf, errno)) || (pglob->gl_flags & GLOB_ERR)))
805aed721ecSAndrey A. Chernov 		return (GLOB_ABORTED);
806aed721ecSAndrey A. Chernov 
807aed721ecSAndrey A. Chernov 	return (0);
80858f0484fSRodney W. Grimes }
80958f0484fSRodney W. Grimes 
81058f0484fSRodney W. Grimes 
81158f0484fSRodney W. Grimes /*
812b4c19408SEd Maste  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
81358f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
81458f0484fSRodney W. Grimes  *
81558f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
81658f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
81758f0484fSRodney W. Grimes  * behavior.
81858f0484fSRodney W. Grimes  *
81958f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
82058f0484fSRodney W. Grimes  *
82158f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
82258f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
82358f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
82458f0484fSRodney W. Grimes  */
82558f0484fSRodney W. Grimes static int
826aed721ecSAndrey A. Chernov globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
827aed721ecSAndrey A. Chernov     int prot)
82858f0484fSRodney W. Grimes {
829b231cb39SDavid E. O'Brien 	char **pathv;
8304b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
83158f0484fSRodney W. Grimes 	char *copy;
83258f0484fSRodney W. Grimes 	const Char *p;
83358f0484fSRodney W. Grimes 
834daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
835daab0b01SMarcel Moolenaar 	    pglob->gl_matchc > limit->l_path_lim) {
83675dc5f1aSMike Heffner 		errno = 0;
83775dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
83875dc5f1aSMike Heffner 	}
839813c96dbSJonathan Lemon 
84058f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
84143cc14e0SMarcel Moolenaar 	/* realloc(NULL, newsize) is equivalent to malloc(newsize). */
84243cc14e0SMarcel Moolenaar 	pathv = realloc((void *)pglob->gl_pathv, newsize);
843b628fac5SMarcel Moolenaar 	if (pathv == NULL)
84458f0484fSRodney W. Grimes 		return (GLOB_NOSPACE);
84558f0484fSRodney W. Grimes 
84658f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
84758f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
84858f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
8494b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
85058f0484fSRodney W. Grimes 			*--pathv = NULL;
85158f0484fSRodney W. Grimes 	}
85258f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
85358f0484fSRodney W. Grimes 
854aed721ecSAndrey A. Chernov 	for (p = path; *p++ != EOS;)
85558f0484fSRodney W. Grimes 		continue;
856e9346e01STim J. Robbins 	len = MB_CUR_MAX * (size_t)(p - path);	/* XXX overallocation */
857aed721ecSAndrey A. Chernov 	if (prot)
858aed721ecSAndrey A. Chernov 		len += (size_t)(p - path) - 1;
859daab0b01SMarcel Moolenaar 	limit->l_string_cnt += len;
860daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
861daab0b01SMarcel Moolenaar 	    limit->l_string_cnt >= GLOB_LIMIT_STRING) {
862daab0b01SMarcel Moolenaar 		errno = 0;
863daab0b01SMarcel Moolenaar 		return (GLOB_NOSPACE);
864daab0b01SMarcel Moolenaar 	}
86527d52f69SPeter Wemm 	if ((copy = malloc(len)) != NULL) {
866aed721ecSAndrey A. Chernov 		if (g_Ctoc(path, copy, len, prot)) {
86776e2bc01SPeter Wemm 			free(copy);
868f4d4982eSAndrey A. Chernov 			errno = 0;
86976e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
87076e2bc01SPeter Wemm 		}
87158f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
87258f0484fSRodney W. Grimes 	}
87358f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
87458f0484fSRodney W. Grimes 	return (copy == NULL ? GLOB_NOSPACE : 0);
87558f0484fSRodney W. Grimes }
87658f0484fSRodney W. Grimes 
87758f0484fSRodney W. Grimes /*
87858f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
87958f0484fSRodney W. Grimes  * pattern causes a recursion level.
88058f0484fSRodney W. Grimes  */
88158f0484fSRodney W. Grimes static int
8821cec70adSXin LI match(Char *name, Char *pat, Char *patend)
88358f0484fSRodney W. Grimes {
88458f0484fSRodney W. Grimes 	int ok, negate_range;
88558f0484fSRodney W. Grimes 	Char c, k;
8861daad8f5SAndrey A. Chernov 	struct xlocale_collate *table =
8871daad8f5SAndrey A. Chernov 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
88858f0484fSRodney W. Grimes 
88958f0484fSRodney W. Grimes 	while (pat < patend) {
89058f0484fSRodney W. Grimes 		c = *pat++;
89158f0484fSRodney W. Grimes 		switch (c & M_MASK) {
89258f0484fSRodney W. Grimes 		case M_ALL:
89358f0484fSRodney W. Grimes 			if (pat == patend)
89458f0484fSRodney W. Grimes 				return (1);
89558f0484fSRodney W. Grimes 			do
89658f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
89758f0484fSRodney W. Grimes 				    return (1);
89858f0484fSRodney W. Grimes 			while (*name++ != EOS);
89958f0484fSRodney W. Grimes 			return (0);
90058f0484fSRodney W. Grimes 		case M_ONE:
90158f0484fSRodney W. Grimes 			if (*name++ == EOS)
90258f0484fSRodney W. Grimes 				return (0);
90358f0484fSRodney W. Grimes 			break;
90458f0484fSRodney W. Grimes 		case M_SET:
90558f0484fSRodney W. Grimes 			ok = 0;
90658f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
90758f0484fSRodney W. Grimes 				return (0);
908eef722c3SAndrey A. Chernov 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
90958f0484fSRodney W. Grimes 				++pat;
91058f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
91158f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
9121daad8f5SAndrey A. Chernov 					if (table->__collate_load_error ?
913aa3d69a6SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) &&
914aa3d69a6SAndrey A. Chernov 					    CHAR(k) <= CHAR(pat[1]) :
915aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(c),
916aa3d69a6SAndrey A. Chernov 					    CHAR(k)) <= 0 &&
917aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(k),
918aa3d69a6SAndrey A. Chernov 					    CHAR(pat[1])) <= 0)
91958f0484fSRodney W. Grimes 						ok = 1;
92058f0484fSRodney W. Grimes 					pat += 2;
92158f0484fSRodney W. Grimes 				} else if (c == k)
92258f0484fSRodney W. Grimes 					ok = 1;
92358f0484fSRodney W. Grimes 			if (ok == negate_range)
92458f0484fSRodney W. Grimes 				return (0);
92558f0484fSRodney W. Grimes 			break;
92658f0484fSRodney W. Grimes 		default:
92758f0484fSRodney W. Grimes 			if (*name++ != c)
92858f0484fSRodney W. Grimes 				return (0);
92958f0484fSRodney W. Grimes 			break;
93058f0484fSRodney W. Grimes 		}
93158f0484fSRodney W. Grimes 	}
93258f0484fSRodney W. Grimes 	return (*name == EOS);
93358f0484fSRodney W. Grimes }
93458f0484fSRodney W. Grimes 
93558f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
93658f0484fSRodney W. Grimes void
9371cec70adSXin LI globfree(glob_t *pglob)
93858f0484fSRodney W. Grimes {
9394b767fa6SAndrey A. Chernov 	size_t i;
940b231cb39SDavid E. O'Brien 	char **pp;
94158f0484fSRodney W. Grimes 
94258f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
94358f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
94458f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
94558f0484fSRodney W. Grimes 			if (*pp)
94658f0484fSRodney W. Grimes 				free(*pp);
94758f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
94876e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
94958f0484fSRodney W. Grimes 	}
95058f0484fSRodney W. Grimes }
95158f0484fSRodney W. Grimes 
95258f0484fSRodney W. Grimes static DIR *
9531cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
95458f0484fSRodney W. Grimes {
955e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
95658f0484fSRodney W. Grimes 
957aa3d69a6SAndrey A. Chernov 	if (*str == EOS)
95858f0484fSRodney W. Grimes 		strcpy(buf, ".");
95976e2bc01SPeter Wemm 	else {
960aed721ecSAndrey A. Chernov 		if (g_Ctoc(str, buf, sizeof(buf), 0)) {
961196d61a9SAndrey A. Chernov 			errno = ENAMETOOLONG;
96276e2bc01SPeter Wemm 			return (NULL);
96376e2bc01SPeter Wemm 		}
964196d61a9SAndrey A. Chernov 	}
96558f0484fSRodney W. Grimes 
96658f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
96758f0484fSRodney W. Grimes 		return ((*pglob->gl_opendir)(buf));
96858f0484fSRodney W. Grimes 
96958f0484fSRodney W. Grimes 	return (opendir(buf));
97058f0484fSRodney W. Grimes }
97158f0484fSRodney W. Grimes 
97258f0484fSRodney W. Grimes static int
9731cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
97458f0484fSRodney W. Grimes {
975e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
97658f0484fSRodney W. Grimes 
977aed721ecSAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf), 0)) {
97876e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
97976e2bc01SPeter Wemm 		return (-1);
98076e2bc01SPeter Wemm 	}
98158f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
98258f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
98358f0484fSRodney W. Grimes 	return (lstat(buf, sb));
98458f0484fSRodney W. Grimes }
98558f0484fSRodney W. Grimes 
98658f0484fSRodney W. Grimes static int
9871cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
98858f0484fSRodney W. Grimes {
989e9c01372SAndrey A. Chernov 	char buf[MAXPATHLEN + MB_LEN_MAX - 1];
99058f0484fSRodney W. Grimes 
991aed721ecSAndrey A. Chernov 	if (g_Ctoc(fn, buf, sizeof(buf), 0)) {
99276e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
99376e2bc01SPeter Wemm 		return (-1);
99476e2bc01SPeter Wemm 	}
99558f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
99658f0484fSRodney W. Grimes 		return ((*pglob->gl_stat)(buf, sb));
99758f0484fSRodney W. Grimes 	return (stat(buf, sb));
99858f0484fSRodney W. Grimes }
99958f0484fSRodney W. Grimes 
100034a08754SMike Makonnen static const Char *
100134a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
100258f0484fSRodney W. Grimes {
10031cec70adSXin LI 
100458f0484fSRodney W. Grimes 	do {
100558f0484fSRodney W. Grimes 		if (*str == ch)
100658f0484fSRodney W. Grimes 			return (str);
100758f0484fSRodney W. Grimes 	} while (*str++);
100858f0484fSRodney W. Grimes 	return (NULL);
100958f0484fSRodney W. Grimes }
101058f0484fSRodney W. Grimes 
101176e2bc01SPeter Wemm static int
1012aed721ecSAndrey A. Chernov g_Ctoc(const Char *str, char *buf, size_t len, int prot)
101358f0484fSRodney W. Grimes {
1014e9346e01STim J. Robbins 	mbstate_t mbs;
1015e9346e01STim J. Robbins 	size_t clen;
1016aed721ecSAndrey A. Chernov 	Char Ch;
101758f0484fSRodney W. Grimes 
1018aed721ecSAndrey A. Chernov 	Ch = *str;
1019e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1020e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
1021aed721ecSAndrey A. Chernov 		if (prot && isprot(Ch)) {
1022aed721ecSAndrey A. Chernov 			Ch = UNPROT(Ch);
1023aed721ecSAndrey A. Chernov 			*buf++ = '\\';
1024aed721ecSAndrey A. Chernov 			len--;
1025aed721ecSAndrey A. Chernov 			continue;
1026aed721ecSAndrey A. Chernov 		}
1027aed721ecSAndrey A. Chernov 		clen = wcrtomb(buf, CHAR(Ch), &mbs);
1028aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1) {
1029aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
1030aed721ecSAndrey A. Chernov 			*buf = (char)CHAR(Ch);
1031aa3d69a6SAndrey A. Chernov 			clen = 1;
1032aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
1033aa3d69a6SAndrey A. Chernov 		}
1034aed721ecSAndrey A. Chernov 		if (CHAR(Ch) == EOS)
103576e2bc01SPeter Wemm 			return (0);
1036aed721ecSAndrey A. Chernov 		Ch = *++str;
1037e9346e01STim J. Robbins 		buf += clen;
1038e9346e01STim J. Robbins 		len -= clen;
103958f0484fSRodney W. Grimes 	}
104027d52f69SPeter Wemm 	return (1);
104127d52f69SPeter Wemm }
104258f0484fSRodney W. Grimes 
104358f0484fSRodney W. Grimes #ifdef DEBUG
104458f0484fSRodney W. Grimes static void
10451cec70adSXin LI qprintf(const char *str, Char *s)
104658f0484fSRodney W. Grimes {
1047b231cb39SDavid E. O'Brien 	Char *p;
104858f0484fSRodney W. Grimes 
104958f0484fSRodney W. Grimes 	(void)printf("%s:\n", str);
105058f0484fSRodney W. Grimes 	for (p = s; *p; p++)
105158f0484fSRodney W. Grimes 		(void)printf("%c", CHAR(*p));
105258f0484fSRodney W. Grimes 	(void)printf("\n");
105358f0484fSRodney W. Grimes 	for (p = s; *p; p++)
105458f0484fSRodney W. Grimes 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
105558f0484fSRodney W. Grimes 	(void)printf("\n");
105658f0484fSRodney W. Grimes 	for (p = s; *p; p++)
105758f0484fSRodney W. Grimes 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
105858f0484fSRodney W. Grimes 	(void)printf("\n");
105958f0484fSRodney W. Grimes }
106058f0484fSRodney W. Grimes #endif
1061