xref: /freebsd/lib/libc/gen/glob.c (revision f4d4982ea6b7a1d4f128f59ed266bbd16ba3a3cc)
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	SLASH		L'/'
130aa3d69a6SAndrey A. Chernov #define	COMMA		L','
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes #ifndef DEBUG
13358f0484fSRodney W. Grimes 
134e9346e01STim J. Robbins #define	M_QUOTE		0x8000000000ULL
135e9346e01STim J. Robbins #define	M_PROTECT	0x4000000000ULL
136e9346e01STim J. Robbins #define	M_MASK		0xffffffffffULL
137e9346e01STim J. Robbins #define	M_CHAR		0x00ffffffffULL
13858f0484fSRodney W. Grimes 
139e9346e01STim J. Robbins typedef uint_fast64_t Char;
14058f0484fSRodney W. Grimes 
14158f0484fSRodney W. Grimes #else
14258f0484fSRodney W. Grimes 
14358f0484fSRodney W. Grimes #define	M_QUOTE		0x80
14458f0484fSRodney W. Grimes #define	M_PROTECT	0x40
14558f0484fSRodney W. Grimes #define	M_MASK		0xff
146e9346e01STim J. Robbins #define	M_CHAR		0x7f
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes typedef char Char;
14958f0484fSRodney W. Grimes 
15058f0484fSRodney W. Grimes #endif
15158f0484fSRodney W. Grimes 
15258f0484fSRodney W. Grimes 
153e9346e01STim J. Robbins #define	CHAR(c)		((Char)((c)&M_CHAR))
15458f0484fSRodney W. Grimes #define	META(c)		((Char)((c)|M_QUOTE))
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)
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes 
164b231cb39SDavid E. O'Brien static int	 compare(const void *, const void *);
1654b767fa6SAndrey A. Chernov static int	 g_Ctoc(const Char *, char *, size_t);
166b231cb39SDavid E. O'Brien static int	 g_lstat(Char *, struct stat *, glob_t *);
167b231cb39SDavid E. O'Brien static DIR	*g_opendir(Char *, glob_t *);
16834a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t);
16958f0484fSRodney W. Grimes #ifdef notdef
170b231cb39SDavid E. O'Brien static Char	*g_strcat(Char *, const Char *);
17158f0484fSRodney W. Grimes #endif
172b231cb39SDavid E. O'Brien static int	 g_stat(Char *, struct stat *, glob_t *);
173daab0b01SMarcel Moolenaar static int	 glob0(const Char *, glob_t *, struct glob_limit *);
174daab0b01SMarcel Moolenaar static int	 glob1(Char *, glob_t *, struct glob_limit *);
175daab0b01SMarcel Moolenaar static int	 glob2(Char *, Char *, Char *, Char *, glob_t *,
176daab0b01SMarcel Moolenaar     struct glob_limit *);
177daab0b01SMarcel Moolenaar static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
178daab0b01SMarcel Moolenaar     struct glob_limit *);
179daab0b01SMarcel Moolenaar static int	 globextend(const Char *, glob_t *, struct glob_limit *);
180487dbd92SPeter Wemm static const Char *
181b231cb39SDavid E. O'Brien 		 globtilde(const Char *, Char *, size_t, glob_t *);
182daab0b01SMarcel Moolenaar static int	 globexp1(const Char *, glob_t *, struct glob_limit *);
183daab0b01SMarcel Moolenaar static int	 globexp2(const Char *, const Char *, glob_t *, int *,
184daab0b01SMarcel Moolenaar     struct glob_limit *);
185b231cb39SDavid E. O'Brien static int	 match(Char *, Char *, Char *);
18658f0484fSRodney W. Grimes #ifdef DEBUG
187b231cb39SDavid E. O'Brien static void	 qprintf(const char *, Char *);
18858f0484fSRodney W. Grimes #endif
18958f0484fSRodney W. Grimes 
19058f0484fSRodney W. Grimes int
1910d6d372cSEitan Adler glob(const char * __restrict pattern, int flags,
1920d6d372cSEitan Adler 	 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
19358f0484fSRodney W. Grimes {
194daab0b01SMarcel Moolenaar 	struct glob_limit limit = { 0, 0, 0, 0, 0 };
1951cec70adSXin LI 	const char *patnext;
196e9346e01STim J. Robbins 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
197e9346e01STim J. Robbins 	mbstate_t mbs;
198e9346e01STim J. Robbins 	wchar_t wc;
199e9346e01STim J. Robbins 	size_t clen;
20058f0484fSRodney W. Grimes 
2011cec70adSXin LI 	patnext = pattern;
20258f0484fSRodney W. Grimes 	if (!(flags & GLOB_APPEND)) {
20358f0484fSRodney W. Grimes 		pglob->gl_pathc = 0;
20458f0484fSRodney W. Grimes 		pglob->gl_pathv = NULL;
20558f0484fSRodney W. Grimes 		if (!(flags & GLOB_DOOFFS))
20658f0484fSRodney W. Grimes 			pglob->gl_offs = 0;
20758f0484fSRodney W. Grimes 	}
20875dc5f1aSMike Heffner 	if (flags & GLOB_LIMIT) {
209daab0b01SMarcel Moolenaar 		limit.l_path_lim = pglob->gl_matchc;
210daab0b01SMarcel Moolenaar 		if (limit.l_path_lim == 0)
211daab0b01SMarcel Moolenaar 			limit.l_path_lim = GLOB_LIMIT_PATH;
212daab0b01SMarcel Moolenaar 	}
21358f0484fSRodney W. Grimes 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
21458f0484fSRodney W. Grimes 	pglob->gl_errfunc = errfunc;
21558f0484fSRodney W. Grimes 	pglob->gl_matchc = 0;
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes 	bufnext = patbuf;
218487dbd92SPeter Wemm 	bufend = bufnext + MAXPATHLEN - 1;
219e9346e01STim J. Robbins 	if (flags & GLOB_NOESCAPE) {
220e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
221e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
222e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
223e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
224e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
225e9346e01STim J. Robbins 			else if (clen == 0)
226e9346e01STim J. Robbins 				break;
227e9346e01STim J. Robbins 			*bufnext++ = wc;
228e9346e01STim J. Robbins 			patnext += clen;
229e9346e01STim J. Robbins 		}
230e9346e01STim J. Robbins 	} else {
23158f0484fSRodney W. Grimes 		/* Protect the quoted characters. */
232e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
233e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
234aa3d69a6SAndrey A. Chernov 			if (*patnext == '\\') {
235aa3d69a6SAndrey A. Chernov 				if (*++patnext == '\0') {
236e9346e01STim J. Robbins 					*bufnext++ = QUOTE | M_PROTECT;
237e9346e01STim J. Robbins 					continue;
23858f0484fSRodney W. Grimes 				}
239e9346e01STim J. Robbins 				prot = M_PROTECT;
240e9346e01STim J. Robbins 			} else
241e9346e01STim J. Robbins 				prot = 0;
242e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
243e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
244e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
245e9346e01STim J. Robbins 			else if (clen == 0)
246e9346e01STim J. Robbins 				break;
247e9346e01STim J. Robbins 			*bufnext++ = wc | prot;
248e9346e01STim J. Robbins 			patnext += clen;
24958f0484fSRodney W. Grimes 		}
25058f0484fSRodney W. Grimes 	}
25158f0484fSRodney W. Grimes 	*bufnext = EOS;
25258f0484fSRodney W. Grimes 
25358f0484fSRodney W. Grimes 	if (flags & GLOB_BRACE)
25485529174SEitan Adler 	    return (globexp1(patbuf, pglob, &limit));
25558f0484fSRodney W. Grimes 	else
25685529174SEitan Adler 	    return (glob0(patbuf, pglob, &limit));
25758f0484fSRodney W. Grimes }
25858f0484fSRodney W. Grimes 
25958f0484fSRodney W. Grimes /*
26058f0484fSRodney W. Grimes  * Expand recursively a glob {} pattern. When there is no more expansion
26158f0484fSRodney W. Grimes  * invoke the standard globbing routine to glob the rest of the magic
26258f0484fSRodney W. Grimes  * characters
26358f0484fSRodney W. Grimes  */
264487dbd92SPeter Wemm static int
265daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
26658f0484fSRodney W. Grimes {
26758f0484fSRodney W. Grimes 	const Char* ptr = pattern;
26858f0484fSRodney W. Grimes 	int rv;
26958f0484fSRodney W. Grimes 
270daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
271daab0b01SMarcel Moolenaar 	    limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
272daab0b01SMarcel Moolenaar 		errno = 0;
273daab0b01SMarcel Moolenaar 		return (GLOB_NOSPACE);
274daab0b01SMarcel Moolenaar 	}
275daab0b01SMarcel Moolenaar 
27658f0484fSRodney W. Grimes 	/* Protect a single {}, for find(1), like csh */
27758f0484fSRodney W. Grimes 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
278bae8632fSJonathan Lemon 		return glob0(pattern, pglob, limit);
27958f0484fSRodney W. Grimes 
28034a08754SMike Makonnen 	while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
281bae8632fSJonathan Lemon 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
28258f0484fSRodney W. Grimes 			return rv;
28358f0484fSRodney W. Grimes 
284bae8632fSJonathan Lemon 	return glob0(pattern, pglob, limit);
28558f0484fSRodney W. Grimes }
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes 
28858f0484fSRodney W. Grimes /*
28958f0484fSRodney W. Grimes  * Recursive brace globbing helper. Tries to expand a single brace.
29058f0484fSRodney W. Grimes  * If it succeeds then it invokes globexp1 with the new pattern.
29158f0484fSRodney W. Grimes  * If it fails then it tries to glob the rest of the pattern and returns.
29258f0484fSRodney W. Grimes  */
293487dbd92SPeter Wemm static int
294daab0b01SMarcel Moolenaar globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
295daab0b01SMarcel Moolenaar     struct glob_limit *limit)
29658f0484fSRodney W. Grimes {
29758f0484fSRodney W. Grimes 	int     i;
29858f0484fSRodney W. Grimes 	Char   *lm, *ls;
299369316a8SAndrey A. Chernov 	const Char *pe, *pm, *pm1, *pl;
300487dbd92SPeter Wemm 	Char    patbuf[MAXPATHLEN];
30158f0484fSRodney W. Grimes 
30258f0484fSRodney W. Grimes 	/* copy part up to the brace */
30358f0484fSRodney W. Grimes 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
30458f0484fSRodney W. Grimes 		continue;
305487dbd92SPeter Wemm 	*lm = EOS;
30658f0484fSRodney W. Grimes 	ls = lm;
30758f0484fSRodney W. Grimes 
30858f0484fSRodney W. Grimes 	/* Find the balanced brace */
30958f0484fSRodney W. Grimes 	for (i = 0, pe = ++ptr; *pe; pe++)
31058f0484fSRodney W. Grimes 		if (*pe == LBRACKET) {
31158f0484fSRodney W. Grimes 			/* Ignore everything between [] */
31258f0484fSRodney W. Grimes 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
31358f0484fSRodney W. Grimes 				continue;
31458f0484fSRodney W. Grimes 			if (*pe == EOS) {
31558f0484fSRodney W. Grimes 				/*
31658f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
31758f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
31858f0484fSRodney W. Grimes 				 */
31958f0484fSRodney W. Grimes 				pe = pm;
32058f0484fSRodney W. Grimes 			}
32158f0484fSRodney W. Grimes 		}
32258f0484fSRodney W. Grimes 		else if (*pe == LBRACE)
32358f0484fSRodney W. Grimes 			i++;
32458f0484fSRodney W. Grimes 		else if (*pe == RBRACE) {
32558f0484fSRodney W. Grimes 			if (i == 0)
32658f0484fSRodney W. Grimes 				break;
32758f0484fSRodney W. Grimes 			i--;
32858f0484fSRodney W. Grimes 		}
32958f0484fSRodney W. Grimes 
33058f0484fSRodney W. Grimes 	/* Non matching braces; just glob the pattern */
33158f0484fSRodney W. Grimes 	if (i != 0 || *pe == EOS) {
332bae8632fSJonathan Lemon 		*rv = glob0(patbuf, pglob, limit);
33385529174SEitan Adler 		return (0);
33458f0484fSRodney W. Grimes 	}
33558f0484fSRodney W. Grimes 
33658f0484fSRodney W. Grimes 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
33758f0484fSRodney W. Grimes 		switch (*pm) {
33858f0484fSRodney W. Grimes 		case LBRACKET:
33958f0484fSRodney W. Grimes 			/* Ignore everything between [] */
340369316a8SAndrey A. Chernov 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
34158f0484fSRodney W. Grimes 				continue;
34258f0484fSRodney W. Grimes 			if (*pm == EOS) {
34358f0484fSRodney W. Grimes 				/*
34458f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
34558f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
34658f0484fSRodney W. Grimes 				 */
347369316a8SAndrey A. Chernov 				pm = pm1;
34858f0484fSRodney W. Grimes 			}
34958f0484fSRodney W. Grimes 			break;
35058f0484fSRodney W. Grimes 
35158f0484fSRodney W. Grimes 		case LBRACE:
35258f0484fSRodney W. Grimes 			i++;
35358f0484fSRodney W. Grimes 			break;
35458f0484fSRodney W. Grimes 
35558f0484fSRodney W. Grimes 		case RBRACE:
35658f0484fSRodney W. Grimes 			if (i) {
35758f0484fSRodney W. Grimes 			    i--;
35858f0484fSRodney W. Grimes 			    break;
35958f0484fSRodney W. Grimes 			}
36058f0484fSRodney W. Grimes 			/* FALLTHROUGH */
36158f0484fSRodney W. Grimes 		case COMMA:
36258f0484fSRodney W. Grimes 			if (i && *pm == COMMA)
36358f0484fSRodney W. Grimes 				break;
36458f0484fSRodney W. Grimes 			else {
36558f0484fSRodney W. Grimes 				/* Append the current string */
36658f0484fSRodney W. Grimes 				for (lm = ls; (pl < pm); *lm++ = *pl++)
36758f0484fSRodney W. Grimes 					continue;
36858f0484fSRodney W. Grimes 				/*
36958f0484fSRodney W. Grimes 				 * Append the rest of the pattern after the
37058f0484fSRodney W. Grimes 				 * closing brace
37158f0484fSRodney W. Grimes 				 */
37258f0484fSRodney W. Grimes 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
37358f0484fSRodney W. Grimes 					continue;
37458f0484fSRodney W. Grimes 
37558f0484fSRodney W. Grimes 				/* Expand the current pattern */
37658f0484fSRodney W. Grimes #ifdef DEBUG
37758f0484fSRodney W. Grimes 				qprintf("globexp2:", patbuf);
37858f0484fSRodney W. Grimes #endif
379bae8632fSJonathan Lemon 				*rv = globexp1(patbuf, pglob, limit);
38058f0484fSRodney W. Grimes 
38158f0484fSRodney W. Grimes 				/* move after the comma, to the next string */
38258f0484fSRodney W. Grimes 				pl = pm + 1;
38358f0484fSRodney W. Grimes 			}
38458f0484fSRodney W. Grimes 			break;
38558f0484fSRodney W. Grimes 
38658f0484fSRodney W. Grimes 		default:
38758f0484fSRodney W. Grimes 			break;
38858f0484fSRodney W. Grimes 		}
38958f0484fSRodney W. Grimes 	*rv = 0;
39085529174SEitan Adler 	return (0);
39158f0484fSRodney W. Grimes }
39258f0484fSRodney W. Grimes 
39358f0484fSRodney W. Grimes 
39458f0484fSRodney W. Grimes 
39558f0484fSRodney W. Grimes /*
39658f0484fSRodney W. Grimes  * expand tilde from the passwd file.
39758f0484fSRodney W. Grimes  */
39858f0484fSRodney W. Grimes static const Char *
3991cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
40058f0484fSRodney W. Grimes {
40158f0484fSRodney W. Grimes 	struct passwd *pwd;
402aa3d69a6SAndrey A. Chernov 	char *h, *sc;
40358f0484fSRodney W. Grimes 	const Char *p;
40462f187a4SWarner Losh 	Char *b, *eb;
405aa3d69a6SAndrey A. Chernov 	wchar_t wc;
406aa3d69a6SAndrey A. Chernov 	wchar_t wbuf[MAXPATHLEN];
407aa3d69a6SAndrey A. Chernov 	wchar_t *wbufend, *dc;
408aa3d69a6SAndrey A. Chernov 	size_t clen;
409aa3d69a6SAndrey A. Chernov 	mbstate_t mbs;
410aa3d69a6SAndrey A. Chernov 	int too_long;
41158f0484fSRodney W. Grimes 
41258f0484fSRodney W. Grimes 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
41385529174SEitan Adler 		return (pattern);
41458f0484fSRodney W. Grimes 
41562f187a4SWarner Losh 	/*
41662f187a4SWarner Losh 	 * Copy up to the end of the string or /
41762f187a4SWarner Losh 	 */
41862f187a4SWarner Losh 	eb = &patbuf[patbuf_len - 1];
419aa3d69a6SAndrey A. Chernov 	for (p = pattern + 1, b = patbuf;
420aa3d69a6SAndrey A. Chernov 	    b < eb && *p != EOS && *p != SLASH; *b++ = *p++)
42158f0484fSRodney W. Grimes 		continue;
42258f0484fSRodney W. Grimes 
423aa3d69a6SAndrey A. Chernov 	if (*p != EOS && *p != SLASH)
424*f4d4982eSAndrey A. Chernov 		return (NULL);
42558f0484fSRodney W. Grimes 
426aa3d69a6SAndrey A. Chernov 	*b = EOS;
427aa3d69a6SAndrey A. Chernov 	h = NULL;
428aa3d69a6SAndrey A. Chernov 
429aa3d69a6SAndrey A. Chernov 	if (patbuf[0] == EOS) {
43058f0484fSRodney W. Grimes 		/*
4313fa69daeSWarner Losh 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
4323fa69daeSWarner Losh 		 * we're not running setuid or setgid) and then trying
4333fa69daeSWarner Losh 		 * the password file
43458f0484fSRodney W. Grimes 		 */
4354539e95aSTim J. Robbins 		if (issetugid() != 0 ||
4369fcbcd02SJohn Birrell 		    (h = getenv("HOME")) == NULL) {
437eb8eee5aSAndrey A. Chernov 			if (((h = getlogin()) != NULL &&
438eb8eee5aSAndrey A. Chernov 			     (pwd = getpwnam(h)) != NULL) ||
439eb8eee5aSAndrey A. Chernov 			    (pwd = getpwuid(getuid())) != NULL)
44058f0484fSRodney W. Grimes 				h = pwd->pw_dir;
441eb8eee5aSAndrey A. Chernov 			else
44285529174SEitan Adler 				return (pattern);
44358f0484fSRodney W. Grimes 		}
44458f0484fSRodney W. Grimes 	}
44558f0484fSRodney W. Grimes 	else {
44658f0484fSRodney W. Grimes 		/*
44758f0484fSRodney W. Grimes 		 * Expand a ~user
44858f0484fSRodney W. Grimes 		 */
449*f4d4982eSAndrey A. Chernov 		if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
450*f4d4982eSAndrey A. Chernov 			return (NULL);
451*f4d4982eSAndrey A. Chernov 		if ((pwd = getpwnam((char *)wbuf)) == NULL)
45285529174SEitan Adler 			return (pattern);
45358f0484fSRodney W. Grimes 		else
45458f0484fSRodney W. Grimes 			h = pwd->pw_dir;
45558f0484fSRodney W. Grimes 	}
45658f0484fSRodney W. Grimes 
45758f0484fSRodney W. Grimes 	/* Copy the home directory */
458aa3d69a6SAndrey A. Chernov 	dc = wbuf;
459aa3d69a6SAndrey A. Chernov 	sc = h;
460aa3d69a6SAndrey A. Chernov 	wbufend = wbuf + MAXPATHLEN - 1;
461aa3d69a6SAndrey A. Chernov 	too_long = 1;
462aa3d69a6SAndrey A. Chernov 	memset(&mbs, 0, sizeof(mbs));
463aa3d69a6SAndrey A. Chernov 	while (dc <= wbufend) {
464aa3d69a6SAndrey A. Chernov 		clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
465aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1 || clen == (size_t)-2) {
466aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
467aa3d69a6SAndrey A. Chernov 			wc = (unsigned char)*sc;
468aa3d69a6SAndrey A. Chernov 			clen = 1;
469aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
470aa3d69a6SAndrey A. Chernov 		}
471aa3d69a6SAndrey A. Chernov 		if ((*dc++ = wc) == EOS) {
472aa3d69a6SAndrey A. Chernov 			too_long = 0;
473aa3d69a6SAndrey A. Chernov 			break;
474aa3d69a6SAndrey A. Chernov 		}
475aa3d69a6SAndrey A. Chernov 		sc += clen;
476aa3d69a6SAndrey A. Chernov 	}
477aa3d69a6SAndrey A. Chernov 	if (too_long)
478*f4d4982eSAndrey A. Chernov 		return (NULL);
479aa3d69a6SAndrey A. Chernov 
480aa3d69a6SAndrey A. Chernov 	dc = wbuf;
481aa3d69a6SAndrey A. Chernov 	for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++)
48258f0484fSRodney W. Grimes 		continue;
483aa3d69a6SAndrey A. Chernov 	if (*dc != EOS)
484*f4d4982eSAndrey A. Chernov 		return (NULL);
48558f0484fSRodney W. Grimes 
48658f0484fSRodney W. Grimes 	/* Append the rest of the pattern */
487aa3d69a6SAndrey A. Chernov 	if (*p != EOS) {
488aa3d69a6SAndrey A. Chernov 		too_long = 1;
489aa3d69a6SAndrey A. Chernov 		while (b <= eb) {
490aa3d69a6SAndrey A. Chernov 			if ((*b++ = *p++) == EOS) {
491aa3d69a6SAndrey A. Chernov 				too_long = 0;
492aa3d69a6SAndrey A. Chernov 				break;
493aa3d69a6SAndrey A. Chernov 			}
494aa3d69a6SAndrey A. Chernov 		}
495aa3d69a6SAndrey A. Chernov 		if (too_long)
496*f4d4982eSAndrey A. Chernov 			return (NULL);
497aa3d69a6SAndrey A. Chernov 	} else
49862f187a4SWarner Losh 		*b = EOS;
49958f0484fSRodney W. Grimes 
50085529174SEitan Adler 	return (patbuf);
50158f0484fSRodney W. Grimes }
50258f0484fSRodney W. Grimes 
50358f0484fSRodney W. Grimes 
50458f0484fSRodney W. Grimes /*
50558f0484fSRodney W. Grimes  * The main glob() routine: compiles the pattern (optionally processing
50658f0484fSRodney W. Grimes  * quotes), calls glob1() to do the real pattern matching, and finally
50758f0484fSRodney W. Grimes  * sorts the list (unless unsorted operation is requested).  Returns 0
5084a59c3abSMike Heffner  * if things went well, nonzero if errors occurred.
50958f0484fSRodney W. Grimes  */
51058f0484fSRodney W. Grimes static int
511daab0b01SMarcel Moolenaar glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
51258f0484fSRodney W. Grimes {
51358f0484fSRodney W. Grimes 	const Char *qpatnext;
5145b3fab81SGordon Tetlow 	int err;
5154b767fa6SAndrey A. Chernov 	size_t oldpathc;
5165b3fab81SGordon Tetlow 	Char *bufnext, c, patbuf[MAXPATHLEN];
51758f0484fSRodney W. Grimes 
518487dbd92SPeter Wemm 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
519*f4d4982eSAndrey A. Chernov 	if (qpatnext == NULL) {
520*f4d4982eSAndrey A. Chernov 		errno = 0;
521*f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
522*f4d4982eSAndrey A. Chernov 	}
52358f0484fSRodney W. Grimes 	oldpathc = pglob->gl_pathc;
52458f0484fSRodney W. Grimes 	bufnext = patbuf;
52558f0484fSRodney W. Grimes 
52658f0484fSRodney W. Grimes 	/* We don't need to check for buffer overflow any more. */
52758f0484fSRodney W. Grimes 	while ((c = *qpatnext++) != EOS) {
52858f0484fSRodney W. Grimes 		switch (c) {
52958f0484fSRodney W. Grimes 		case LBRACKET:
53058f0484fSRodney W. Grimes 			c = *qpatnext;
53158f0484fSRodney W. Grimes 			if (c == NOT)
53258f0484fSRodney W. Grimes 				++qpatnext;
53358f0484fSRodney W. Grimes 			if (*qpatnext == EOS ||
53434a08754SMike Makonnen 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
53558f0484fSRodney W. Grimes 				*bufnext++ = LBRACKET;
53658f0484fSRodney W. Grimes 				if (c == NOT)
53758f0484fSRodney W. Grimes 					--qpatnext;
53858f0484fSRodney W. Grimes 				break;
53958f0484fSRodney W. Grimes 			}
54058f0484fSRodney W. Grimes 			*bufnext++ = M_SET;
54158f0484fSRodney W. Grimes 			if (c == NOT)
54258f0484fSRodney W. Grimes 				*bufnext++ = M_NOT;
54358f0484fSRodney W. Grimes 			c = *qpatnext++;
54458f0484fSRodney W. Grimes 			do {
54558f0484fSRodney W. Grimes 				*bufnext++ = CHAR(c);
54658f0484fSRodney W. Grimes 				if (*qpatnext == RANGE &&
54758f0484fSRodney W. Grimes 				    (c = qpatnext[1]) != RBRACKET) {
54858f0484fSRodney W. Grimes 					*bufnext++ = M_RNG;
54958f0484fSRodney W. Grimes 					*bufnext++ = CHAR(c);
55058f0484fSRodney W. Grimes 					qpatnext += 2;
55158f0484fSRodney W. Grimes 				}
55258f0484fSRodney W. Grimes 			} while ((c = *qpatnext++) != RBRACKET);
55358f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
55458f0484fSRodney W. Grimes 			*bufnext++ = M_END;
55558f0484fSRodney W. Grimes 			break;
55658f0484fSRodney W. Grimes 		case QUESTION:
55758f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
55858f0484fSRodney W. Grimes 			*bufnext++ = M_ONE;
55958f0484fSRodney W. Grimes 			break;
56058f0484fSRodney W. Grimes 		case STAR:
56158f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
56258f0484fSRodney W. Grimes 			/* collapse adjacent stars to one,
56358f0484fSRodney W. Grimes 			 * to avoid exponential behavior
56458f0484fSRodney W. Grimes 			 */
56558f0484fSRodney W. Grimes 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
56658f0484fSRodney W. Grimes 			    *bufnext++ = M_ALL;
56758f0484fSRodney W. Grimes 			break;
56858f0484fSRodney W. Grimes 		default:
56958f0484fSRodney W. Grimes 			*bufnext++ = CHAR(c);
57058f0484fSRodney W. Grimes 			break;
57158f0484fSRodney W. Grimes 		}
57258f0484fSRodney W. Grimes 	}
57358f0484fSRodney W. Grimes 	*bufnext = EOS;
57458f0484fSRodney W. Grimes #ifdef DEBUG
57558f0484fSRodney W. Grimes 	qprintf("glob0:", patbuf);
57658f0484fSRodney W. Grimes #endif
57758f0484fSRodney W. Grimes 
578bae8632fSJonathan Lemon 	if ((err = glob1(patbuf, pglob, limit)) != 0)
57958f0484fSRodney W. Grimes 		return(err);
58058f0484fSRodney W. Grimes 
58158f0484fSRodney W. Grimes 	/*
58258f0484fSRodney W. Grimes 	 * If there was no match we are going to append the pattern
58358f0484fSRodney W. Grimes 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
58458f0484fSRodney W. Grimes 	 * and the pattern did not contain any magic characters
58558f0484fSRodney W. Grimes 	 * GLOB_NOMAGIC is there just for compatibility with csh.
58658f0484fSRodney W. Grimes 	 */
5874a59c3abSMike Heffner 	if (pglob->gl_pathc == oldpathc) {
5884a59c3abSMike Heffner 		if (((pglob->gl_flags & GLOB_NOCHECK) ||
58958f0484fSRodney W. Grimes 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
59058f0484fSRodney W. Grimes 			!(pglob->gl_flags & GLOB_MAGCHAR))))
591bae8632fSJonathan Lemon 			return (globextend(pattern, pglob, limit));
5924a59c3abSMike Heffner 		else
5934a59c3abSMike Heffner 			return (GLOB_NOMATCH);
5944a59c3abSMike Heffner 	}
5954a59c3abSMike Heffner 	if (!(pglob->gl_flags & GLOB_NOSORT))
59658f0484fSRodney W. Grimes 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
59758f0484fSRodney W. Grimes 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
59858f0484fSRodney W. Grimes 	return (0);
59958f0484fSRodney W. Grimes }
60058f0484fSRodney W. Grimes 
60158f0484fSRodney W. Grimes static int
6021cec70adSXin LI compare(const void *p, const void *q)
60358f0484fSRodney W. Grimes {
604aa3d69a6SAndrey A. Chernov 	return (strcoll(*(char **)p, *(char **)q));
60558f0484fSRodney W. Grimes }
60658f0484fSRodney W. Grimes 
60758f0484fSRodney W. Grimes static int
608daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
60958f0484fSRodney W. Grimes {
610487dbd92SPeter Wemm 	Char pathbuf[MAXPATHLEN];
61158f0484fSRodney W. Grimes 
61258f0484fSRodney W. Grimes 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
61358f0484fSRodney W. Grimes 	if (*pattern == EOS)
61458f0484fSRodney W. Grimes 		return (0);
615487dbd92SPeter Wemm 	return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
616487dbd92SPeter Wemm 	    pattern, pglob, limit));
61758f0484fSRodney W. Grimes }
61858f0484fSRodney W. Grimes 
61958f0484fSRodney W. Grimes /*
62058f0484fSRodney W. Grimes  * The functions glob2 and glob3 are mutually recursive; there is one level
62158f0484fSRodney W. Grimes  * of recursion for each segment in the pattern that contains one or more
62258f0484fSRodney W. Grimes  * meta characters.
62358f0484fSRodney W. Grimes  */
62458f0484fSRodney W. Grimes static int
6251cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
626daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
62758f0484fSRodney W. Grimes {
62858f0484fSRodney W. Grimes 	struct stat sb;
62958f0484fSRodney W. Grimes 	Char *p, *q;
63058f0484fSRodney W. Grimes 	int anymeta;
63158f0484fSRodney W. Grimes 
63258f0484fSRodney W. Grimes 	/*
63358f0484fSRodney W. Grimes 	 * Loop over pattern segments until end of pattern or until
63458f0484fSRodney W. Grimes 	 * segment with meta character found.
63558f0484fSRodney W. Grimes 	 */
63658f0484fSRodney W. Grimes 	for (anymeta = 0;;) {
63758f0484fSRodney W. Grimes 		if (*pattern == EOS) {		/* End of pattern? */
63858f0484fSRodney W. Grimes 			*pathend = EOS;
63958f0484fSRodney W. Grimes 			if (g_lstat(pathbuf, &sb, pglob))
64058f0484fSRodney W. Grimes 				return (0);
64158f0484fSRodney W. Grimes 
642daab0b01SMarcel Moolenaar 			if ((pglob->gl_flags & GLOB_LIMIT) &&
643daab0b01SMarcel Moolenaar 			    limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
644daab0b01SMarcel Moolenaar 				errno = 0;
645daab0b01SMarcel Moolenaar 				if (pathend + 1 > pathend_last)
646*f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
647daab0b01SMarcel Moolenaar 				*pathend++ = SEP;
648daab0b01SMarcel Moolenaar 				*pathend = EOS;
649daab0b01SMarcel Moolenaar 				return (GLOB_NOSPACE);
650daab0b01SMarcel Moolenaar 			}
65158f0484fSRodney W. Grimes 			if (((pglob->gl_flags & GLOB_MARK) &&
65258f0484fSRodney W. Grimes 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
65358f0484fSRodney W. Grimes 			    || (S_ISLNK(sb.st_mode) &&
65458f0484fSRodney W. Grimes 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
65558f0484fSRodney W. Grimes 			    S_ISDIR(sb.st_mode)))) {
656*f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
657*f4d4982eSAndrey A. Chernov 					errno = 0;
658*f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
659*f4d4982eSAndrey A. Chernov 				}
66058f0484fSRodney W. Grimes 				*pathend++ = SEP;
66158f0484fSRodney W. Grimes 				*pathend = EOS;
66258f0484fSRodney W. Grimes 			}
66358f0484fSRodney W. Grimes 			++pglob->gl_matchc;
664bae8632fSJonathan Lemon 			return (globextend(pathbuf, pglob, limit));
66558f0484fSRodney W. Grimes 		}
66658f0484fSRodney W. Grimes 
66758f0484fSRodney W. Grimes 		/* Find end of next segment, copy tentatively to pathend. */
66858f0484fSRodney W. Grimes 		q = pathend;
66958f0484fSRodney W. Grimes 		p = pattern;
67058f0484fSRodney W. Grimes 		while (*p != EOS && *p != SEP) {
67158f0484fSRodney W. Grimes 			if (ismeta(*p))
67258f0484fSRodney W. Grimes 				anymeta = 1;
673*f4d4982eSAndrey A. Chernov 			if (q + 1 > pathend_last) {
674*f4d4982eSAndrey A. Chernov 				errno = 0;
675*f4d4982eSAndrey A. Chernov 				return (GLOB_NOSPACE);
676*f4d4982eSAndrey A. Chernov 			}
67758f0484fSRodney W. Grimes 			*q++ = *p++;
67858f0484fSRodney W. Grimes 		}
67958f0484fSRodney W. Grimes 
68058f0484fSRodney W. Grimes 		if (!anymeta) {		/* No expansion, do next segment. */
68158f0484fSRodney W. Grimes 			pathend = q;
68258f0484fSRodney W. Grimes 			pattern = p;
683487dbd92SPeter Wemm 			while (*pattern == SEP) {
684*f4d4982eSAndrey A. Chernov 				if (pathend + 1 > pathend_last) {
685*f4d4982eSAndrey A. Chernov 					errno = 0;
686*f4d4982eSAndrey A. Chernov 					return (GLOB_NOSPACE);
687*f4d4982eSAndrey A. Chernov 				}
68858f0484fSRodney W. Grimes 				*pathend++ = *pattern++;
689487dbd92SPeter Wemm 			}
69058f0484fSRodney W. Grimes 		} else			/* Need expansion, recurse. */
69185529174SEitan Adler 			return (glob3(pathbuf, pathend, pathend_last, pattern,
69285529174SEitan Adler 			    p, pglob, limit));
69358f0484fSRodney W. Grimes 	}
69458f0484fSRodney W. Grimes 	/* NOTREACHED */
69558f0484fSRodney W. Grimes }
69658f0484fSRodney W. Grimes 
69758f0484fSRodney W. Grimes static int
6981cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
6991cec70adSXin LI       Char *pattern, Char *restpattern,
700daab0b01SMarcel Moolenaar       glob_t *pglob, struct glob_limit *limit)
70158f0484fSRodney W. Grimes {
702b231cb39SDavid E. O'Brien 	struct dirent *dp;
70358f0484fSRodney W. Grimes 	DIR *dirp;
70458f0484fSRodney W. Grimes 	int err;
70558f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
70658f0484fSRodney W. Grimes 
7073e2981e8SCraig Rodrigues 	struct dirent *(*readdirfunc)(DIR *);
70858f0484fSRodney W. Grimes 
70958f0484fSRodney W. Grimes 	errno = 0;
710*f4d4982eSAndrey A. Chernov 	if (pathend > pathend_last)
711*f4d4982eSAndrey A. Chernov 		return (GLOB_NOSPACE);
712*f4d4982eSAndrey A. Chernov 	*pathend = EOS;
71358f0484fSRodney W. Grimes 
71458f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
71558f0484fSRodney W. Grimes 		/* TODO: don't call for ENOENT or ENOTDIR? */
716*f4d4982eSAndrey A. Chernov 		if (pglob->gl_flags & GLOB_ERR)
7174a59c3abSMike Heffner 			return (GLOB_ABORTED);
718*f4d4982eSAndrey A. Chernov 		if (pglob->gl_errfunc) {
719*f4d4982eSAndrey A. Chernov 			if (g_Ctoc(pathbuf, buf, sizeof(buf))) {
720*f4d4982eSAndrey A. Chernov 				errno = 0;
721*f4d4982eSAndrey A. Chernov 				return (GLOB_NOSPACE);
722*f4d4982eSAndrey A. Chernov 			}
723*f4d4982eSAndrey A. Chernov 			if (pglob->gl_errfunc(buf, errno))
7244a59c3abSMike Heffner 				return (GLOB_ABORTED);
72558f0484fSRodney W. Grimes 		}
72658f0484fSRodney W. Grimes 		return (0);
72758f0484fSRodney W. Grimes 	}
72858f0484fSRodney W. Grimes 
72958f0484fSRodney W. Grimes 	err = 0;
73058f0484fSRodney W. Grimes 
7313e2981e8SCraig Rodrigues 	/* pglob->gl_readdir takes a void *, fix this manually */
73258f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7333e2981e8SCraig Rodrigues 		readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
73458f0484fSRodney W. Grimes 	else
73558f0484fSRodney W. Grimes 		readdirfunc = readdir;
7363e2981e8SCraig Rodrigues 
7373e2981e8SCraig Rodrigues 	/* Search directory for matching names. */
7383e2981e8SCraig Rodrigues 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
7391cec70adSXin LI 		char *sc;
740b231cb39SDavid E. O'Brien 		Char *dc;
741e9346e01STim J. Robbins 		wchar_t wc;
742e9346e01STim J. Robbins 		size_t clen;
743e9346e01STim J. Robbins 		mbstate_t mbs;
74458f0484fSRodney W. Grimes 
745daab0b01SMarcel Moolenaar 		if ((pglob->gl_flags & GLOB_LIMIT) &&
746daab0b01SMarcel Moolenaar 		    limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
747daab0b01SMarcel Moolenaar 			errno = 0;
748daab0b01SMarcel Moolenaar 			if (pathend + 1 > pathend_last)
749*f4d4982eSAndrey A. Chernov 				err = GLOB_NOSPACE;
750daab0b01SMarcel Moolenaar 			else {
751daab0b01SMarcel Moolenaar 				*pathend++ = SEP;
752daab0b01SMarcel Moolenaar 				*pathend = EOS;
753daab0b01SMarcel Moolenaar 				err = GLOB_NOSPACE;
754daab0b01SMarcel Moolenaar 			}
755daab0b01SMarcel Moolenaar 			break;
756daab0b01SMarcel Moolenaar 		}
757daab0b01SMarcel Moolenaar 
75858f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
759aa3d69a6SAndrey A. Chernov 		if (dp->d_name[0] == '.' && *pattern != DOT)
76058f0484fSRodney W. Grimes 			continue;
761e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
762487dbd92SPeter Wemm 		dc = pathend;
7631cec70adSXin LI 		sc = dp->d_name;
764e9346e01STim J. Robbins 		while (dc < pathend_last) {
765e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
766e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
767aa3d69a6SAndrey A. Chernov 				/* XXX See initial comment #2. */
768aa3d69a6SAndrey A. Chernov 				wc = (unsigned char)*sc;
769e9346e01STim J. Robbins 				clen = 1;
770e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
771e9346e01STim J. Robbins 			}
772e9346e01STim J. Robbins 			if ((*dc++ = wc) == EOS)
773e9346e01STim J. Robbins 				break;
774e9346e01STim J. Robbins 			sc += clen;
775e9346e01STim J. Robbins 		}
77658f0484fSRodney W. Grimes 		if (!match(pathend, pattern, restpattern)) {
77758f0484fSRodney W. Grimes 			*pathend = EOS;
77858f0484fSRodney W. Grimes 			continue;
77958f0484fSRodney W. Grimes 		}
780487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
781487dbd92SPeter Wemm 		    pglob, limit);
78258f0484fSRodney W. Grimes 		if (err)
78358f0484fSRodney W. Grimes 			break;
78458f0484fSRodney W. Grimes 	}
78558f0484fSRodney W. Grimes 
78658f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
78758f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
78858f0484fSRodney W. Grimes 	else
78958f0484fSRodney W. Grimes 		closedir(dirp);
79058f0484fSRodney W. Grimes 	return (err);
79158f0484fSRodney W. Grimes }
79258f0484fSRodney W. Grimes 
79358f0484fSRodney W. Grimes 
79458f0484fSRodney W. Grimes /*
795b4c19408SEd Maste  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
79658f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
79758f0484fSRodney W. Grimes  *
79858f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
79958f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
80058f0484fSRodney W. Grimes  * behavior.
80158f0484fSRodney W. Grimes  *
80258f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
80358f0484fSRodney W. Grimes  *
80458f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
80558f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
80658f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
80758f0484fSRodney W. Grimes  */
80858f0484fSRodney W. Grimes static int
809daab0b01SMarcel Moolenaar globextend(const Char *path, glob_t *pglob, struct glob_limit *limit)
81058f0484fSRodney W. Grimes {
811b231cb39SDavid E. O'Brien 	char **pathv;
8124b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
81358f0484fSRodney W. Grimes 	char *copy;
81458f0484fSRodney W. Grimes 	const Char *p;
81558f0484fSRodney W. Grimes 
816daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
817daab0b01SMarcel Moolenaar 	    pglob->gl_matchc > limit->l_path_lim) {
81875dc5f1aSMike Heffner 		errno = 0;
81975dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
82075dc5f1aSMike Heffner 	}
821813c96dbSJonathan Lemon 
82258f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
82343cc14e0SMarcel Moolenaar 	/* realloc(NULL, newsize) is equivalent to malloc(newsize). */
82443cc14e0SMarcel Moolenaar 	pathv = realloc((void *)pglob->gl_pathv, newsize);
825b628fac5SMarcel Moolenaar 	if (pathv == NULL)
82658f0484fSRodney W. Grimes 		return (GLOB_NOSPACE);
82758f0484fSRodney W. Grimes 
82858f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
82958f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
83058f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
8314b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
83258f0484fSRodney W. Grimes 			*--pathv = NULL;
83358f0484fSRodney W. Grimes 	}
83458f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
83558f0484fSRodney W. Grimes 
83658f0484fSRodney W. Grimes 	for (p = path; *p++;)
83758f0484fSRodney W. Grimes 		continue;
838e9346e01STim J. Robbins 	len = MB_CUR_MAX * (size_t)(p - path);	/* XXX overallocation */
839daab0b01SMarcel Moolenaar 	limit->l_string_cnt += len;
840daab0b01SMarcel Moolenaar 	if ((pglob->gl_flags & GLOB_LIMIT) &&
841daab0b01SMarcel Moolenaar 	    limit->l_string_cnt >= GLOB_LIMIT_STRING) {
842daab0b01SMarcel Moolenaar 		errno = 0;
843daab0b01SMarcel Moolenaar 		return (GLOB_NOSPACE);
844daab0b01SMarcel Moolenaar 	}
84527d52f69SPeter Wemm 	if ((copy = malloc(len)) != NULL) {
84627d52f69SPeter Wemm 		if (g_Ctoc(path, copy, len)) {
84776e2bc01SPeter Wemm 			free(copy);
848*f4d4982eSAndrey A. Chernov 			errno = 0;
84976e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
85076e2bc01SPeter Wemm 		}
85158f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
85258f0484fSRodney W. Grimes 	}
85358f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
85458f0484fSRodney W. Grimes 	return (copy == NULL ? GLOB_NOSPACE : 0);
85558f0484fSRodney W. Grimes }
85658f0484fSRodney W. Grimes 
85758f0484fSRodney W. Grimes /*
85858f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
85958f0484fSRodney W. Grimes  * pattern causes a recursion level.
86058f0484fSRodney W. Grimes  */
86158f0484fSRodney W. Grimes static int
8621cec70adSXin LI match(Char *name, Char *pat, Char *patend)
86358f0484fSRodney W. Grimes {
86458f0484fSRodney W. Grimes 	int ok, negate_range;
86558f0484fSRodney W. Grimes 	Char c, k;
8661daad8f5SAndrey A. Chernov 	struct xlocale_collate *table =
8671daad8f5SAndrey A. Chernov 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
86858f0484fSRodney W. Grimes 
86958f0484fSRodney W. Grimes 	while (pat < patend) {
87058f0484fSRodney W. Grimes 		c = *pat++;
87158f0484fSRodney W. Grimes 		switch (c & M_MASK) {
87258f0484fSRodney W. Grimes 		case M_ALL:
87358f0484fSRodney W. Grimes 			if (pat == patend)
87458f0484fSRodney W. Grimes 				return (1);
87558f0484fSRodney W. Grimes 			do
87658f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
87758f0484fSRodney W. Grimes 				    return (1);
87858f0484fSRodney W. Grimes 			while (*name++ != EOS);
87958f0484fSRodney W. Grimes 			return (0);
88058f0484fSRodney W. Grimes 		case M_ONE:
88158f0484fSRodney W. Grimes 			if (*name++ == EOS)
88258f0484fSRodney W. Grimes 				return (0);
88358f0484fSRodney W. Grimes 			break;
88458f0484fSRodney W. Grimes 		case M_SET:
88558f0484fSRodney W. Grimes 			ok = 0;
88658f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
88758f0484fSRodney W. Grimes 				return (0);
88858f0484fSRodney W. Grimes 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
88958f0484fSRodney W. Grimes 				++pat;
89058f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
89158f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
8921daad8f5SAndrey A. Chernov 					if (table->__collate_load_error ?
893aa3d69a6SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) &&
894aa3d69a6SAndrey A. Chernov 					    CHAR(k) <= CHAR(pat[1]) :
895aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(c),
896aa3d69a6SAndrey A. Chernov 					    CHAR(k)) <= 0 &&
897aa3d69a6SAndrey A. Chernov 					    __wcollate_range_cmp(CHAR(k),
898aa3d69a6SAndrey A. Chernov 					    CHAR(pat[1])) <= 0)
89958f0484fSRodney W. Grimes 						ok = 1;
90058f0484fSRodney W. Grimes 					pat += 2;
90158f0484fSRodney W. Grimes 				} else if (c == k)
90258f0484fSRodney W. Grimes 					ok = 1;
90358f0484fSRodney W. Grimes 			if (ok == negate_range)
90458f0484fSRodney W. Grimes 				return (0);
90558f0484fSRodney W. Grimes 			break;
90658f0484fSRodney W. Grimes 		default:
90758f0484fSRodney W. Grimes 			if (*name++ != c)
90858f0484fSRodney W. Grimes 				return (0);
90958f0484fSRodney W. Grimes 			break;
91058f0484fSRodney W. Grimes 		}
91158f0484fSRodney W. Grimes 	}
91258f0484fSRodney W. Grimes 	return (*name == EOS);
91358f0484fSRodney W. Grimes }
91458f0484fSRodney W. Grimes 
91558f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
91658f0484fSRodney W. Grimes void
9171cec70adSXin LI globfree(glob_t *pglob)
91858f0484fSRodney W. Grimes {
9194b767fa6SAndrey A. Chernov 	size_t i;
920b231cb39SDavid E. O'Brien 	char **pp;
92158f0484fSRodney W. Grimes 
92258f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
92358f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
92458f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
92558f0484fSRodney W. Grimes 			if (*pp)
92658f0484fSRodney W. Grimes 				free(*pp);
92758f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
92876e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
92958f0484fSRodney W. Grimes 	}
93058f0484fSRodney W. Grimes }
93158f0484fSRodney W. Grimes 
93258f0484fSRodney W. Grimes static DIR *
9331cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
93458f0484fSRodney W. Grimes {
93558f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
93658f0484fSRodney W. Grimes 
937aa3d69a6SAndrey A. Chernov 	if (*str == EOS)
93858f0484fSRodney W. Grimes 		strcpy(buf, ".");
93976e2bc01SPeter Wemm 	else {
94027d52f69SPeter Wemm 		if (g_Ctoc(str, buf, sizeof(buf)))
94176e2bc01SPeter Wemm 			return (NULL);
94276e2bc01SPeter Wemm 	}
94358f0484fSRodney W. Grimes 
94458f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
94558f0484fSRodney W. Grimes 		return ((*pglob->gl_opendir)(buf));
94658f0484fSRodney W. Grimes 
94758f0484fSRodney W. Grimes 	return (opendir(buf));
94858f0484fSRodney W. Grimes }
94958f0484fSRodney W. Grimes 
95058f0484fSRodney W. Grimes static int
9511cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
95258f0484fSRodney W. Grimes {
95358f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
95458f0484fSRodney W. Grimes 
95527d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
95676e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
95776e2bc01SPeter Wemm 		return (-1);
95876e2bc01SPeter Wemm 	}
95958f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
96058f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
96158f0484fSRodney W. Grimes 	return (lstat(buf, sb));
96258f0484fSRodney W. Grimes }
96358f0484fSRodney W. Grimes 
96458f0484fSRodney W. Grimes static int
9651cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
96658f0484fSRodney W. Grimes {
96758f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
96858f0484fSRodney W. Grimes 
96927d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
97076e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
97176e2bc01SPeter Wemm 		return (-1);
97276e2bc01SPeter Wemm 	}
97358f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
97458f0484fSRodney W. Grimes 		return ((*pglob->gl_stat)(buf, sb));
97558f0484fSRodney W. Grimes 	return (stat(buf, sb));
97658f0484fSRodney W. Grimes }
97758f0484fSRodney W. Grimes 
97834a08754SMike Makonnen static const Char *
97934a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
98058f0484fSRodney W. Grimes {
9811cec70adSXin LI 
98258f0484fSRodney W. Grimes 	do {
98358f0484fSRodney W. Grimes 		if (*str == ch)
98458f0484fSRodney W. Grimes 			return (str);
98558f0484fSRodney W. Grimes 	} while (*str++);
98658f0484fSRodney W. Grimes 	return (NULL);
98758f0484fSRodney W. Grimes }
98858f0484fSRodney W. Grimes 
98976e2bc01SPeter Wemm static int
9901cec70adSXin LI g_Ctoc(const Char *str, char *buf, size_t len)
99158f0484fSRodney W. Grimes {
992e9346e01STim J. Robbins 	mbstate_t mbs;
993e9346e01STim J. Robbins 	size_t clen;
99458f0484fSRodney W. Grimes 
995e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
996e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
9977e9488ddSAndrey A. Chernov 		clen = wcrtomb(buf, CHAR(*str), &mbs);
998aa3d69a6SAndrey A. Chernov 		if (clen == (size_t)-1) {
999aa3d69a6SAndrey A. Chernov 			/* XXX See initial comment #2. */
10007e9488ddSAndrey A. Chernov 			*buf = (char)CHAR(*str);
1001aa3d69a6SAndrey A. Chernov 			clen = 1;
1002aa3d69a6SAndrey A. Chernov 			memset(&mbs, 0, sizeof(mbs));
1003aa3d69a6SAndrey A. Chernov 		}
10047e9488ddSAndrey A. Chernov 		if (CHAR(*str) == EOS)
100576e2bc01SPeter Wemm 			return (0);
1006e9346e01STim J. Robbins 		str++;
1007e9346e01STim J. Robbins 		buf += clen;
1008e9346e01STim J. Robbins 		len -= clen;
100958f0484fSRodney W. Grimes 	}
101027d52f69SPeter Wemm 	return (1);
101127d52f69SPeter Wemm }
101258f0484fSRodney W. Grimes 
101358f0484fSRodney W. Grimes #ifdef DEBUG
101458f0484fSRodney W. Grimes static void
10151cec70adSXin LI qprintf(const char *str, Char *s)
101658f0484fSRodney W. Grimes {
1017b231cb39SDavid E. O'Brien 	Char *p;
101858f0484fSRodney W. Grimes 
101958f0484fSRodney W. Grimes 	(void)printf("%s:\n", str);
102058f0484fSRodney W. Grimes 	for (p = s; *p; p++)
102158f0484fSRodney W. Grimes 		(void)printf("%c", CHAR(*p));
102258f0484fSRodney W. Grimes 	(void)printf("\n");
102358f0484fSRodney W. Grimes 	for (p = s; *p; p++)
102458f0484fSRodney W. Grimes 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
102558f0484fSRodney W. Grimes 	(void)printf("\n");
102658f0484fSRodney W. Grimes 	for (p = s; *p; p++)
102758f0484fSRodney W. Grimes 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
102858f0484fSRodney W. Grimes 	(void)printf("\n");
102958f0484fSRodney W. Grimes }
103058f0484fSRodney W. Grimes #endif
1031