xref: /minix3/lib/libc/gen/glob.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: glob.c,v 1.35 2013/03/20 23:44:47 lukem Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1989, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Guido van Rossum.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: glob.c,v 1.35 2013/03/20 23:44:47 lukem Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras  * glob(3) -- a superset of the one defined in POSIX 1003.2.
462fe8fb19SBen Gras  *
472fe8fb19SBen Gras  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
482fe8fb19SBen Gras  *
492fe8fb19SBen Gras  * Optional extra services, controlled by flags not defined by POSIX:
502fe8fb19SBen Gras  *
512fe8fb19SBen Gras  * GLOB_MAGCHAR:
522fe8fb19SBen Gras  *	Set in gl_flags if pattern contained a globbing character.
532fe8fb19SBen Gras  * GLOB_NOMAGIC:
542fe8fb19SBen Gras  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
552fe8fb19SBen Gras  *	not contain any magic characters.  [Used in csh style globbing]
562fe8fb19SBen Gras  * GLOB_ALTDIRFUNC:
572fe8fb19SBen Gras  *	Use alternately specified directory access functions.
582fe8fb19SBen Gras  * GLOB_TILDE:
592fe8fb19SBen Gras  *	expand ~user/foo to the /home/dir/of/user/foo
602fe8fb19SBen Gras  * GLOB_BRACE:
612fe8fb19SBen Gras  *	expand {1,2}{a,b} to 1a 1b 2a 2b
622fe8fb19SBen Gras  * GLOB_PERIOD:
632fe8fb19SBen Gras  *	allow metacharacters to match leading dots in filenames.
642fe8fb19SBen Gras  * GLOB_NO_DOTDIRS:
652fe8fb19SBen Gras  *	. and .. are hidden from wildcards, even if GLOB_PERIOD is set.
662fe8fb19SBen Gras  * gl_matchc:
672fe8fb19SBen Gras  *	Number of matches in the current invocation of glob.
682fe8fb19SBen Gras  */
692fe8fb19SBen Gras 
702fe8fb19SBen Gras #include "namespace.h"
712fe8fb19SBen Gras #include <sys/param.h>
722fe8fb19SBen Gras #include <sys/stat.h>
732fe8fb19SBen Gras 
742fe8fb19SBen Gras #include <assert.h>
752fe8fb19SBen Gras #include <ctype.h>
762fe8fb19SBen Gras #include <dirent.h>
772fe8fb19SBen Gras #include <errno.h>
782fe8fb19SBen Gras #include <glob.h>
792fe8fb19SBen Gras #include <pwd.h>
802fe8fb19SBen Gras #include <stdio.h>
812fe8fb19SBen Gras #include <stddef.h>
822fe8fb19SBen Gras #include <stdlib.h>
832fe8fb19SBen Gras #include <string.h>
842fe8fb19SBen Gras #include <unistd.h>
852fe8fb19SBen Gras 
862fe8fb19SBen Gras #ifdef HAVE_NBTOOL_CONFIG_H
872fe8fb19SBen Gras #define NO_GETPW_R
882fe8fb19SBen Gras #endif
892fe8fb19SBen Gras 
902fe8fb19SBen Gras #define	GLOB_LIMIT_STRING	65536	/* number of readdirs */
912fe8fb19SBen Gras #define	GLOB_LIMIT_STAT		128	/* number of stat system calls */
922fe8fb19SBen Gras #define	GLOB_LIMIT_READDIR	16384	/* total buffer size of path strings */
932fe8fb19SBen Gras #define	GLOB_LIMIT_PATH		1024	/* number of path elements */
942fe8fb19SBen Gras #define GLOB_LIMIT_BRACE	128	/* Number of brace calls */
952fe8fb19SBen Gras 
962fe8fb19SBen Gras struct glob_limit {
972fe8fb19SBen Gras 	size_t l_string;
982fe8fb19SBen Gras 	size_t l_stat;
992fe8fb19SBen Gras 	size_t l_readdir;
1002fe8fb19SBen Gras 	size_t l_brace;
1012fe8fb19SBen Gras };
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras /*
1042fe8fb19SBen Gras  * XXX: For NetBSD 1.4.x compatibility. (kill me l8r)
1052fe8fb19SBen Gras  */
1062fe8fb19SBen Gras #ifndef _DIAGASSERT
1072fe8fb19SBen Gras #define _DIAGASSERT(a)
1082fe8fb19SBen Gras #endif
1092fe8fb19SBen Gras 
1102fe8fb19SBen Gras #define	DOLLAR		'$'
1112fe8fb19SBen Gras #define	DOT		'.'
1122fe8fb19SBen Gras #define	EOS		'\0'
1132fe8fb19SBen Gras #define	LBRACKET	'['
1142fe8fb19SBen Gras #define	NOT		'!'
1152fe8fb19SBen Gras #define	QUESTION	'?'
1162fe8fb19SBen Gras #define	QUOTE		'\\'
1172fe8fb19SBen Gras #define	RANGE		'-'
1182fe8fb19SBen Gras #define	RBRACKET	']'
1192fe8fb19SBen Gras #define	SEP		'/'
1202fe8fb19SBen Gras #define	STAR		'*'
1212fe8fb19SBen Gras #define	TILDE		'~'
1222fe8fb19SBen Gras #define	UNDERSCORE	'_'
1232fe8fb19SBen Gras #define	LBRACE		'{'
1242fe8fb19SBen Gras #define	RBRACE		'}'
1252fe8fb19SBen Gras #define	SLASH		'/'
1262fe8fb19SBen Gras #define	COMMA		','
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras #ifndef USE_8BIT_CHARS
1292fe8fb19SBen Gras 
1302fe8fb19SBen Gras #define	M_QUOTE		0x8000
1312fe8fb19SBen Gras #define	M_PROTECT	0x4000
1322fe8fb19SBen Gras #define	M_MASK		0xffff
1332fe8fb19SBen Gras #define	M_ASCII		0x00ff
1342fe8fb19SBen Gras 
135*84d9c625SLionel Sambuc typedef unsigned short Char;
1362fe8fb19SBen Gras 
1372fe8fb19SBen Gras #else
1382fe8fb19SBen Gras 
1392fe8fb19SBen Gras #define	M_QUOTE		(Char)0x80
1402fe8fb19SBen Gras #define	M_PROTECT	(Char)0x40
1412fe8fb19SBen Gras #define	M_MASK		(Char)0xff
1422fe8fb19SBen Gras #define	M_ASCII		(Char)0x7f
1432fe8fb19SBen Gras 
1442fe8fb19SBen Gras typedef char Char;
1452fe8fb19SBen Gras 
1462fe8fb19SBen Gras #endif
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras 
1492fe8fb19SBen Gras #define	CHAR(c)		((Char)((c)&M_ASCII))
1502fe8fb19SBen Gras #define	META(c)		((Char)((c)|M_QUOTE))
1512fe8fb19SBen Gras #define	M_ALL		META('*')
1522fe8fb19SBen Gras #define	M_END		META(']')
1532fe8fb19SBen Gras #define	M_NOT		META('!')
1542fe8fb19SBen Gras #define	M_ONE		META('?')
1552fe8fb19SBen Gras #define	M_RNG		META('-')
1562fe8fb19SBen Gras #define	M_SET		META('[')
1572fe8fb19SBen Gras #define	ismeta(c)	(((c)&M_QUOTE) != 0)
1582fe8fb19SBen Gras 
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras static int	 compare(const void *, const void *);
1612fe8fb19SBen Gras static int	 g_Ctoc(const Char *, char *, size_t);
1622fe8fb19SBen Gras static int	 g_lstat(Char *, __gl_stat_t  *, glob_t *);
1632fe8fb19SBen Gras static DIR	*g_opendir(Char *, glob_t *);
1642fe8fb19SBen Gras static Char	*g_strchr(const Char *, int);
1652fe8fb19SBen Gras static int	 g_stat(Char *, __gl_stat_t *, glob_t *);
1662fe8fb19SBen Gras static int	 glob0(const Char *, glob_t *, struct glob_limit *);
1672fe8fb19SBen Gras static int	 glob1(Char *, glob_t *, struct glob_limit *);
1682fe8fb19SBen Gras static int	 glob2(Char *, Char *, Char *, const Char *, glob_t *,
1692fe8fb19SBen Gras     struct glob_limit *);
1702fe8fb19SBen Gras static int	 glob3(Char *, Char *, Char *, const Char *, const Char *,
1712fe8fb19SBen Gras     const Char *, glob_t *, struct glob_limit *);
1722fe8fb19SBen Gras static int	 globextend(const Char *, glob_t *, struct glob_limit *);
1732fe8fb19SBen Gras static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
1742fe8fb19SBen Gras static int	 globexp1(const Char *, glob_t *, struct glob_limit *);
1752fe8fb19SBen Gras static int	 globexp2(const Char *, const Char *, glob_t *, int *,
1762fe8fb19SBen Gras     struct glob_limit *);
1772fe8fb19SBen Gras static int	 match(const Char *, const Char *, const Char *);
1782fe8fb19SBen Gras #ifdef DEBUG
1792fe8fb19SBen Gras static void	 qprintf(const char *, Char *);
1802fe8fb19SBen Gras #endif
1812fe8fb19SBen Gras 
1822fe8fb19SBen Gras int
glob(const char * __restrict pattern,int flags,int (* errfunc)(const char *,int),glob_t * __restrict pglob)183f14fb602SLionel Sambuc glob(const char * __restrict pattern, int flags, int (*errfunc)(const char *,
184f14fb602SLionel Sambuc     int), glob_t * __restrict pglob)
1852fe8fb19SBen Gras {
186*84d9c625SLionel Sambuc 	const unsigned char *patnext;
1872fe8fb19SBen Gras 	int c;
1882fe8fb19SBen Gras 	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
1892fe8fb19SBen Gras 	struct glob_limit limit = { 0, 0, 0, 0 };
1902fe8fb19SBen Gras 
1912fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
1922fe8fb19SBen Gras 
193*84d9c625SLionel Sambuc 	patnext = (const unsigned char *) pattern;
1942fe8fb19SBen Gras 	if (!(flags & GLOB_APPEND)) {
1952fe8fb19SBen Gras 		pglob->gl_pathc = 0;
1962fe8fb19SBen Gras 		pglob->gl_pathv = NULL;
1972fe8fb19SBen Gras 		if (!(flags & GLOB_DOOFFS))
1982fe8fb19SBen Gras 			pglob->gl_offs = 0;
1992fe8fb19SBen Gras 	}
2002fe8fb19SBen Gras 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
2012fe8fb19SBen Gras 	pglob->gl_errfunc = errfunc;
2022fe8fb19SBen Gras 	pglob->gl_matchc = 0;
2032fe8fb19SBen Gras 
2042fe8fb19SBen Gras 	bufnext = patbuf;
2052fe8fb19SBen Gras 	bufend = bufnext + MAXPATHLEN;
2062fe8fb19SBen Gras 	if (flags & GLOB_NOESCAPE) {
2072fe8fb19SBen Gras 		while (bufnext < bufend && (c = *patnext++) != EOS)
2082fe8fb19SBen Gras 			*bufnext++ = c;
2092fe8fb19SBen Gras 	} else {
2102fe8fb19SBen Gras 		/* Protect the quoted characters. */
2112fe8fb19SBen Gras 		while (bufnext < bufend && (c = *patnext++) != EOS)
2122fe8fb19SBen Gras 			if (c == QUOTE) {
2132fe8fb19SBen Gras 				if ((c = *patnext++) == EOS) {
2142fe8fb19SBen Gras 					c = QUOTE;
2152fe8fb19SBen Gras 					--patnext;
2162fe8fb19SBen Gras 				}
2172fe8fb19SBen Gras 				*bufnext++ = c | M_PROTECT;
2182fe8fb19SBen Gras 			}
2192fe8fb19SBen Gras 			else
2202fe8fb19SBen Gras 				*bufnext++ = c;
2212fe8fb19SBen Gras 	}
2222fe8fb19SBen Gras 	*bufnext = EOS;
2232fe8fb19SBen Gras 
2242fe8fb19SBen Gras 	if (flags & GLOB_BRACE)
2252fe8fb19SBen Gras 	    return globexp1(patbuf, pglob, &limit);
2262fe8fb19SBen Gras 	else
2272fe8fb19SBen Gras 	    return glob0(patbuf, pglob, &limit);
2282fe8fb19SBen Gras }
2292fe8fb19SBen Gras 
2302fe8fb19SBen Gras /*
2312fe8fb19SBen Gras  * Expand recursively a glob {} pattern. When there is no more expansion
2322fe8fb19SBen Gras  * invoke the standard globbing routine to glob the rest of the magic
2332fe8fb19SBen Gras  * characters
2342fe8fb19SBen Gras  */
2352fe8fb19SBen Gras static int
globexp1(const Char * pattern,glob_t * pglob,struct glob_limit * limit)2362fe8fb19SBen Gras globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
2372fe8fb19SBen Gras {
2382fe8fb19SBen Gras 	const Char* ptr = pattern;
2392fe8fb19SBen Gras 	int rv;
2402fe8fb19SBen Gras 
2412fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
2422fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
2432fe8fb19SBen Gras 
2442fe8fb19SBen Gras 	if ((pglob->gl_flags & GLOB_LIMIT) &&
2452fe8fb19SBen Gras 	    limit->l_brace++ >= GLOB_LIMIT_BRACE) {
2462fe8fb19SBen Gras 		errno = 0;
2472fe8fb19SBen Gras 		return GLOB_NOSPACE;
2482fe8fb19SBen Gras 	}
2492fe8fb19SBen Gras 
2502fe8fb19SBen Gras 	/* Protect a single {}, for find(1), like csh */
2512fe8fb19SBen Gras 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
2522fe8fb19SBen Gras 		return glob0(pattern, pglob, limit);
2532fe8fb19SBen Gras 
2542fe8fb19SBen Gras 	while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
2552fe8fb19SBen Gras 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
2562fe8fb19SBen Gras 			return rv;
2572fe8fb19SBen Gras 
2582fe8fb19SBen Gras 	return glob0(pattern, pglob, limit);
2592fe8fb19SBen Gras }
2602fe8fb19SBen Gras 
2612fe8fb19SBen Gras 
2622fe8fb19SBen Gras /*
2632fe8fb19SBen Gras  * Recursive brace globbing helper. Tries to expand a single brace.
2642fe8fb19SBen Gras  * If it succeeds then it invokes globexp1 with the new pattern.
2652fe8fb19SBen Gras  * If it fails then it tries to glob the rest of the pattern and returns.
2662fe8fb19SBen Gras  */
2672fe8fb19SBen Gras static int
globexp2(const Char * ptr,const Char * pattern,glob_t * pglob,int * rv,struct glob_limit * limit)2682fe8fb19SBen Gras globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
2692fe8fb19SBen Gras     struct glob_limit *limit)
2702fe8fb19SBen Gras {
2712fe8fb19SBen Gras 	int     i;
2722fe8fb19SBen Gras 	Char   *lm, *ls;
2732fe8fb19SBen Gras 	const Char *pe, *pm, *pl;
2742fe8fb19SBen Gras 	Char    patbuf[MAXPATHLEN + 1];
2752fe8fb19SBen Gras 
2762fe8fb19SBen Gras 	_DIAGASSERT(ptr != NULL);
2772fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
2782fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
2792fe8fb19SBen Gras 	_DIAGASSERT(rv != NULL);
2802fe8fb19SBen Gras 
2812fe8fb19SBen Gras 	/* copy part up to the brace */
2822fe8fb19SBen Gras 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
2832fe8fb19SBen Gras 		continue;
2842fe8fb19SBen Gras 	ls = lm;
2852fe8fb19SBen Gras 
2862fe8fb19SBen Gras 	/* Find the balanced brace */
2872fe8fb19SBen Gras 	for (i = 0, pe = ++ptr; *pe; pe++)
2882fe8fb19SBen Gras 		if (*pe == LBRACKET) {
2892fe8fb19SBen Gras 			/* Ignore everything between [] */
2902fe8fb19SBen Gras 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
2912fe8fb19SBen Gras 				continue;
2922fe8fb19SBen Gras 			if (*pe == EOS) {
2932fe8fb19SBen Gras 				/*
2942fe8fb19SBen Gras 				 * We could not find a matching RBRACKET.
2952fe8fb19SBen Gras 				 * Ignore and just look for RBRACE
2962fe8fb19SBen Gras 				 */
2972fe8fb19SBen Gras 				pe = pm;
2982fe8fb19SBen Gras 			}
2992fe8fb19SBen Gras 		}
3002fe8fb19SBen Gras 		else if (*pe == LBRACE)
3012fe8fb19SBen Gras 			i++;
3022fe8fb19SBen Gras 		else if (*pe == RBRACE) {
3032fe8fb19SBen Gras 			if (i == 0)
3042fe8fb19SBen Gras 				break;
3052fe8fb19SBen Gras 			i--;
3062fe8fb19SBen Gras 		}
3072fe8fb19SBen Gras 
3082fe8fb19SBen Gras 	/* Non matching braces; just glob the pattern */
3092fe8fb19SBen Gras 	if (i != 0 || *pe == EOS) {
3102fe8fb19SBen Gras 		/*
3112fe8fb19SBen Gras 		 * we use `pattern', not `patbuf' here so that that
3122fe8fb19SBen Gras 		 * unbalanced braces are passed to the match
3132fe8fb19SBen Gras 		 */
3142fe8fb19SBen Gras 		*rv = glob0(pattern, pglob, limit);
3152fe8fb19SBen Gras 		return 0;
3162fe8fb19SBen Gras 	}
3172fe8fb19SBen Gras 
3182fe8fb19SBen Gras 	for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
3192fe8fb19SBen Gras 		switch (*pm) {
3202fe8fb19SBen Gras 		case LBRACKET:
3212fe8fb19SBen Gras 			/* Ignore everything between [] */
3222fe8fb19SBen Gras 			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
3232fe8fb19SBen Gras 				continue;
3242fe8fb19SBen Gras 			if (*pm == EOS) {
3252fe8fb19SBen Gras 				/*
3262fe8fb19SBen Gras 				 * We could not find a matching RBRACKET.
3272fe8fb19SBen Gras 				 * Ignore and just look for RBRACE
3282fe8fb19SBen Gras 				 */
3292fe8fb19SBen Gras 				pm = pl;
3302fe8fb19SBen Gras 			}
3312fe8fb19SBen Gras 			break;
3322fe8fb19SBen Gras 
3332fe8fb19SBen Gras 		case LBRACE:
3342fe8fb19SBen Gras 			i++;
3352fe8fb19SBen Gras 			break;
3362fe8fb19SBen Gras 
3372fe8fb19SBen Gras 		case RBRACE:
3382fe8fb19SBen Gras 			if (i) {
3392fe8fb19SBen Gras 				i--;
3402fe8fb19SBen Gras 				break;
3412fe8fb19SBen Gras 			}
3422fe8fb19SBen Gras 			/* FALLTHROUGH */
3432fe8fb19SBen Gras 		case COMMA:
3442fe8fb19SBen Gras 			if (i && *pm == COMMA)
3452fe8fb19SBen Gras 				break;
3462fe8fb19SBen Gras 			else {
3472fe8fb19SBen Gras 				/* Append the current string */
3482fe8fb19SBen Gras 				for (lm = ls; (pl < pm); *lm++ = *pl++)
3492fe8fb19SBen Gras 					continue;
3502fe8fb19SBen Gras 				/*
3512fe8fb19SBen Gras 				 * Append the rest of the pattern after the
3522fe8fb19SBen Gras 				 * closing brace
3532fe8fb19SBen Gras 				 */
3542fe8fb19SBen Gras 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
3552fe8fb19SBen Gras 					continue;
3562fe8fb19SBen Gras 
3572fe8fb19SBen Gras 				/* Expand the current pattern */
3582fe8fb19SBen Gras #ifdef DEBUG
3592fe8fb19SBen Gras 				qprintf("globexp2", patbuf);
3602fe8fb19SBen Gras #endif
3612fe8fb19SBen Gras 				*rv = globexp1(patbuf, pglob, limit);
3622fe8fb19SBen Gras 
3632fe8fb19SBen Gras 				/* move after the comma, to the next string */
3642fe8fb19SBen Gras 				pl = pm + 1;
3652fe8fb19SBen Gras 			}
3662fe8fb19SBen Gras 			break;
3672fe8fb19SBen Gras 
3682fe8fb19SBen Gras 		default:
3692fe8fb19SBen Gras 			break;
3702fe8fb19SBen Gras 		}
3712fe8fb19SBen Gras 	}
3722fe8fb19SBen Gras 	*rv = 0;
3732fe8fb19SBen Gras 	return 0;
3742fe8fb19SBen Gras }
3752fe8fb19SBen Gras 
3762fe8fb19SBen Gras 
3772fe8fb19SBen Gras 
3782fe8fb19SBen Gras /*
3792fe8fb19SBen Gras  * expand tilde from the passwd file.
3802fe8fb19SBen Gras  */
3812fe8fb19SBen Gras static const Char *
globtilde(const Char * pattern,Char * patbuf,size_t patsize,glob_t * pglob)3822fe8fb19SBen Gras globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
3832fe8fb19SBen Gras {
3842fe8fb19SBen Gras 	struct passwd *pwd;
3852fe8fb19SBen Gras 	const char *h;
3862fe8fb19SBen Gras 	const Char *p;
3872fe8fb19SBen Gras 	Char *b;
3882fe8fb19SBen Gras 	char *d;
3892fe8fb19SBen Gras 	Char *pend = &patbuf[patsize / sizeof(Char)];
3902fe8fb19SBen Gras #ifndef NO_GETPW_R
3912fe8fb19SBen Gras 	struct passwd pwres;
3922fe8fb19SBen Gras 	char pwbuf[1024];
3932fe8fb19SBen Gras #endif
3942fe8fb19SBen Gras 
3952fe8fb19SBen Gras 	pend--;
3962fe8fb19SBen Gras 
3972fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
3982fe8fb19SBen Gras 	_DIAGASSERT(patbuf != NULL);
3992fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
4002fe8fb19SBen Gras 
4012fe8fb19SBen Gras 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
4022fe8fb19SBen Gras 		return pattern;
4032fe8fb19SBen Gras 
4042fe8fb19SBen Gras 	/* Copy up to the end of the string or / */
4052fe8fb19SBen Gras 	for (p = pattern + 1, d = (char *)(void *)patbuf;
4062fe8fb19SBen Gras 	     d < (char *)(void *)pend && *p && *p != SLASH;
4072fe8fb19SBen Gras 	     *d++ = *p++)
4082fe8fb19SBen Gras 		continue;
4092fe8fb19SBen Gras 
4102fe8fb19SBen Gras 	if (d == (char *)(void *)pend)
4112fe8fb19SBen Gras 		return NULL;
4122fe8fb19SBen Gras 
4132fe8fb19SBen Gras 	*d = EOS;
4142fe8fb19SBen Gras 	d = (char *)(void *)patbuf;
4152fe8fb19SBen Gras 
4162fe8fb19SBen Gras 	if (*d == EOS) {
4172fe8fb19SBen Gras 		/*
4182fe8fb19SBen Gras 		 * handle a plain ~ or ~/ by expanding $HOME
4192fe8fb19SBen Gras 		 * first and then trying the password file
4202fe8fb19SBen Gras 		 */
4212fe8fb19SBen Gras 		if ((h = getenv("HOME")) == NULL) {
4222fe8fb19SBen Gras #ifdef NO_GETPW_R
4232fe8fb19SBen Gras 			if ((pwd = getpwuid(getuid())) == NULL)
4242fe8fb19SBen Gras #else
4252fe8fb19SBen Gras 			if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
4262fe8fb19SBen Gras 			    &pwd) != 0 || pwd == NULL)
4272fe8fb19SBen Gras #endif
4282fe8fb19SBen Gras 				return pattern;
4292fe8fb19SBen Gras 			else
4302fe8fb19SBen Gras 				h = pwd->pw_dir;
4312fe8fb19SBen Gras 		}
4322fe8fb19SBen Gras 	}
4332fe8fb19SBen Gras 	else {
4342fe8fb19SBen Gras 		/*
4352fe8fb19SBen Gras 		 * Expand a ~user
4362fe8fb19SBen Gras 		 */
4372fe8fb19SBen Gras #ifdef NO_GETPW_R
4382fe8fb19SBen Gras 		if ((pwd = getpwnam(d)) == NULL)
4392fe8fb19SBen Gras #else
4402fe8fb19SBen Gras 		if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
4412fe8fb19SBen Gras 		    pwd == NULL)
4422fe8fb19SBen Gras #endif
4432fe8fb19SBen Gras 			return pattern;
4442fe8fb19SBen Gras 		else
4452fe8fb19SBen Gras 			h = pwd->pw_dir;
4462fe8fb19SBen Gras 	}
4472fe8fb19SBen Gras 
4482fe8fb19SBen Gras 	/* Copy the home directory */
4492fe8fb19SBen Gras 	for (b = patbuf; b < pend && *h; *b++ = *h++)
4502fe8fb19SBen Gras 		continue;
4512fe8fb19SBen Gras 
4522fe8fb19SBen Gras 	if (b == pend)
4532fe8fb19SBen Gras 		return NULL;
4542fe8fb19SBen Gras 
4552fe8fb19SBen Gras 	/* Append the rest of the pattern */
4562fe8fb19SBen Gras 	while (b < pend && (*b++ = *p++) != EOS)
4572fe8fb19SBen Gras 		continue;
4582fe8fb19SBen Gras 
4592fe8fb19SBen Gras 	if (b == pend)
4602fe8fb19SBen Gras 		return NULL;
4612fe8fb19SBen Gras 
4622fe8fb19SBen Gras 	return patbuf;
4632fe8fb19SBen Gras }
4642fe8fb19SBen Gras 
4652fe8fb19SBen Gras 
4662fe8fb19SBen Gras /*
4672fe8fb19SBen Gras  * The main glob() routine: compiles the pattern (optionally processing
4682fe8fb19SBen Gras  * quotes), calls glob1() to do the real pattern matching, and finally
4692fe8fb19SBen Gras  * sorts the list (unless unsorted operation is requested).  Returns 0
4702fe8fb19SBen Gras  * if things went well, nonzero if errors occurred.  It is not an error
4712fe8fb19SBen Gras  * to find no matches.
4722fe8fb19SBen Gras  */
4732fe8fb19SBen Gras static int
glob0(const Char * pattern,glob_t * pglob,struct glob_limit * limit)4742fe8fb19SBen Gras glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
4752fe8fb19SBen Gras {
4762fe8fb19SBen Gras 	const Char *qpatnext;
4772fe8fb19SBen Gras 	int c, error;
4782fe8fb19SBen Gras 	__gl_size_t oldpathc;
4792fe8fb19SBen Gras 	Char *bufnext, patbuf[MAXPATHLEN+1];
4802fe8fb19SBen Gras 
4812fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
4822fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
4832fe8fb19SBen Gras 
4842fe8fb19SBen Gras 	if ((qpatnext = globtilde(pattern, patbuf, sizeof(patbuf),
4852fe8fb19SBen Gras 	    pglob)) == NULL)
4862fe8fb19SBen Gras 		return GLOB_ABEND;
4872fe8fb19SBen Gras 	oldpathc = pglob->gl_pathc;
4882fe8fb19SBen Gras 	bufnext = patbuf;
4892fe8fb19SBen Gras 
4902fe8fb19SBen Gras 	/* We don't need to check for buffer overflow any more. */
4912fe8fb19SBen Gras 	while ((c = *qpatnext++) != EOS) {
4922fe8fb19SBen Gras 		switch (c) {
4932fe8fb19SBen Gras 		case LBRACKET:
4942fe8fb19SBen Gras 			c = *qpatnext;
4952fe8fb19SBen Gras 			if (c == NOT)
4962fe8fb19SBen Gras 				++qpatnext;
4972fe8fb19SBen Gras 			if (*qpatnext == EOS ||
4982fe8fb19SBen Gras 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
4992fe8fb19SBen Gras 				*bufnext++ = LBRACKET;
5002fe8fb19SBen Gras 				if (c == NOT)
5012fe8fb19SBen Gras 					--qpatnext;
5022fe8fb19SBen Gras 				break;
5032fe8fb19SBen Gras 			}
5042fe8fb19SBen Gras 			*bufnext++ = M_SET;
5052fe8fb19SBen Gras 			if (c == NOT)
5062fe8fb19SBen Gras 				*bufnext++ = M_NOT;
5072fe8fb19SBen Gras 			c = *qpatnext++;
5082fe8fb19SBen Gras 			do {
5092fe8fb19SBen Gras 				*bufnext++ = CHAR(c);
5102fe8fb19SBen Gras 				if (*qpatnext == RANGE &&
5112fe8fb19SBen Gras 				    (c = qpatnext[1]) != RBRACKET) {
5122fe8fb19SBen Gras 					*bufnext++ = M_RNG;
5132fe8fb19SBen Gras 					*bufnext++ = CHAR(c);
5142fe8fb19SBen Gras 					qpatnext += 2;
5152fe8fb19SBen Gras 				}
5162fe8fb19SBen Gras 			} while ((c = *qpatnext++) != RBRACKET);
5172fe8fb19SBen Gras 			pglob->gl_flags |= GLOB_MAGCHAR;
5182fe8fb19SBen Gras 			*bufnext++ = M_END;
5192fe8fb19SBen Gras 			break;
5202fe8fb19SBen Gras 		case QUESTION:
5212fe8fb19SBen Gras 			pglob->gl_flags |= GLOB_MAGCHAR;
5222fe8fb19SBen Gras 			*bufnext++ = M_ONE;
5232fe8fb19SBen Gras 			break;
5242fe8fb19SBen Gras 		case STAR:
5252fe8fb19SBen Gras 			pglob->gl_flags |= GLOB_MAGCHAR;
5262fe8fb19SBen Gras 			/* collapse adjacent stars to one [or three if globstar]
5272fe8fb19SBen Gras 			 * to avoid exponential behavior
5282fe8fb19SBen Gras 			 */
5292fe8fb19SBen Gras 			if (bufnext == patbuf || bufnext[-1] != M_ALL ||
5302fe8fb19SBen Gras 			    ((pglob->gl_flags & GLOB_STAR) != 0 &&
5312fe8fb19SBen Gras 			    (bufnext - 1 == patbuf || bufnext[-2] != M_ALL ||
5322fe8fb19SBen Gras 			    bufnext - 2 == patbuf || bufnext[-3] != M_ALL)))
5332fe8fb19SBen Gras 				*bufnext++ = M_ALL;
5342fe8fb19SBen Gras 			break;
5352fe8fb19SBen Gras 		default:
5362fe8fb19SBen Gras 			*bufnext++ = CHAR(c);
5372fe8fb19SBen Gras 			break;
5382fe8fb19SBen Gras 		}
5392fe8fb19SBen Gras 	}
5402fe8fb19SBen Gras 	*bufnext = EOS;
5412fe8fb19SBen Gras #ifdef DEBUG
5422fe8fb19SBen Gras 	qprintf("glob0", patbuf);
5432fe8fb19SBen Gras #endif
5442fe8fb19SBen Gras 
5452fe8fb19SBen Gras 	if ((error = glob1(patbuf, pglob, limit)) != 0)
5462fe8fb19SBen Gras 		return error;
5472fe8fb19SBen Gras 
5482fe8fb19SBen Gras 	if (pglob->gl_pathc == oldpathc) {
5492fe8fb19SBen Gras 		/*
5502fe8fb19SBen Gras 		 * If there was no match we are going to append the pattern
5512fe8fb19SBen Gras 		 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
5522fe8fb19SBen Gras 		 * specified and the pattern did not contain any magic
5532fe8fb19SBen Gras 		 * characters GLOB_NOMAGIC is there just for compatibility
5542fe8fb19SBen Gras 		 * with csh.
5552fe8fb19SBen Gras 		 */
5562fe8fb19SBen Gras 		if ((pglob->gl_flags & GLOB_NOCHECK) ||
5572fe8fb19SBen Gras 		    ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR))
5582fe8fb19SBen Gras 		     == GLOB_NOMAGIC)) {
5592fe8fb19SBen Gras 			return globextend(pattern, pglob, limit);
5602fe8fb19SBen Gras 		} else {
5612fe8fb19SBen Gras 			return GLOB_NOMATCH;
5622fe8fb19SBen Gras 		}
5632fe8fb19SBen Gras 	} else if (!(pglob->gl_flags & GLOB_NOSORT)) {
5642fe8fb19SBen Gras 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
5652fe8fb19SBen Gras 		    (size_t)pglob->gl_pathc - oldpathc, sizeof(char *),
5662fe8fb19SBen Gras 		    compare);
5672fe8fb19SBen Gras 	}
5682fe8fb19SBen Gras 
5692fe8fb19SBen Gras 	return 0;
5702fe8fb19SBen Gras }
5712fe8fb19SBen Gras 
5722fe8fb19SBen Gras static int
compare(const void * p,const void * q)5732fe8fb19SBen Gras compare(const void *p, const void *q)
5742fe8fb19SBen Gras {
5752fe8fb19SBen Gras 
5762fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
5772fe8fb19SBen Gras 	_DIAGASSERT(q != NULL);
5782fe8fb19SBen Gras 
5792fe8fb19SBen Gras 	return strcoll(*(const char * const *)p, *(const char * const *)q);
5802fe8fb19SBen Gras }
5812fe8fb19SBen Gras 
5822fe8fb19SBen Gras static int
glob1(Char * pattern,glob_t * pglob,struct glob_limit * limit)5832fe8fb19SBen Gras glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
5842fe8fb19SBen Gras {
5852fe8fb19SBen Gras 	Char pathbuf[MAXPATHLEN+1];
5862fe8fb19SBen Gras 
5872fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
5882fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
5892fe8fb19SBen Gras 
5902fe8fb19SBen Gras 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
5912fe8fb19SBen Gras 	if (*pattern == EOS)
5922fe8fb19SBen Gras 		return 0;
5932fe8fb19SBen Gras 	/*
5942fe8fb19SBen Gras 	 * we save one character so that we can use ptr >= limit,
5952fe8fb19SBen Gras 	 * in the general case when we are appending non nul chars only.
5962fe8fb19SBen Gras 	 */
5972fe8fb19SBen Gras 	return glob2(pathbuf, pathbuf,
5982fe8fb19SBen Gras 	    pathbuf + (sizeof(pathbuf) / sizeof(*pathbuf)) - 1, pattern,
5992fe8fb19SBen Gras 	    pglob, limit);
6002fe8fb19SBen Gras }
6012fe8fb19SBen Gras 
6022fe8fb19SBen Gras /*
6032fe8fb19SBen Gras  * The functions glob2 and glob3 are mutually recursive; there is one level
6042fe8fb19SBen Gras  * of recursion for each segment in the pattern that contains one or more
6052fe8fb19SBen Gras  * meta characters.
6062fe8fb19SBen Gras  */
6072fe8fb19SBen Gras static int
glob2(Char * pathbuf,Char * pathend,Char * pathlim,const Char * pattern,glob_t * pglob,struct glob_limit * limit)6082fe8fb19SBen Gras glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
6092fe8fb19SBen Gras     glob_t *pglob, struct glob_limit *limit)
6102fe8fb19SBen Gras {
6112fe8fb19SBen Gras 	__gl_stat_t sb;
6122fe8fb19SBen Gras 	const Char *p;
6132fe8fb19SBen Gras 	Char *q;
6142fe8fb19SBen Gras 	int anymeta;
6152fe8fb19SBen Gras 
6162fe8fb19SBen Gras 	_DIAGASSERT(pathbuf != NULL);
6172fe8fb19SBen Gras 	_DIAGASSERT(pathend != NULL);
6182fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
6192fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
6202fe8fb19SBen Gras 
6212fe8fb19SBen Gras #ifdef DEBUG
6222fe8fb19SBen Gras 	qprintf("glob2", pathbuf);
6232fe8fb19SBen Gras #endif
6242fe8fb19SBen Gras 	/*
6252fe8fb19SBen Gras 	 * Loop over pattern segments until end of pattern or until
6262fe8fb19SBen Gras 	 * segment with meta character found.
6272fe8fb19SBen Gras 	 */
6282fe8fb19SBen Gras 	for (anymeta = 0;;) {
6292fe8fb19SBen Gras 		if (*pattern == EOS) {		/* End of pattern? */
6302fe8fb19SBen Gras 			*pathend = EOS;
6312fe8fb19SBen Gras 			if (g_lstat(pathbuf, &sb, pglob))
6322fe8fb19SBen Gras 				return 0;
6332fe8fb19SBen Gras 
6342fe8fb19SBen Gras 			if ((pglob->gl_flags & GLOB_LIMIT) &&
6352fe8fb19SBen Gras 			    limit->l_stat++ >= GLOB_LIMIT_STAT) {
6362fe8fb19SBen Gras 				errno = 0;
6372fe8fb19SBen Gras 				*pathend++ = SEP;
6382fe8fb19SBen Gras 				*pathend = EOS;
6392fe8fb19SBen Gras 				return GLOB_NOSPACE;
6402fe8fb19SBen Gras 			}
6412fe8fb19SBen Gras 			if (((pglob->gl_flags & GLOB_MARK) &&
6422fe8fb19SBen Gras 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
6432fe8fb19SBen Gras 			    (S_ISLNK(sb.st_mode) &&
6442fe8fb19SBen Gras 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
6452fe8fb19SBen Gras 			    S_ISDIR(sb.st_mode)))) {
6462fe8fb19SBen Gras 				if (pathend >= pathlim)
6472fe8fb19SBen Gras 					return GLOB_ABORTED;
6482fe8fb19SBen Gras 				*pathend++ = SEP;
6492fe8fb19SBen Gras 				*pathend = EOS;
6502fe8fb19SBen Gras 			}
6512fe8fb19SBen Gras 			++pglob->gl_matchc;
6522fe8fb19SBen Gras 			return globextend(pathbuf, pglob, limit);
6532fe8fb19SBen Gras 		}
6542fe8fb19SBen Gras 
6552fe8fb19SBen Gras 		/* Find end of next segment, copy tentatively to pathend. */
6562fe8fb19SBen Gras 		q = pathend;
6572fe8fb19SBen Gras 		p = pattern;
6582fe8fb19SBen Gras 		while (*p != EOS && *p != SEP) {
6592fe8fb19SBen Gras 			if (ismeta(*p))
6602fe8fb19SBen Gras 				anymeta = 1;
6612fe8fb19SBen Gras 			if (q >= pathlim)
6622fe8fb19SBen Gras 				return GLOB_ABORTED;
6632fe8fb19SBen Gras 			*q++ = *p++;
6642fe8fb19SBen Gras 		}
6652fe8fb19SBen Gras 
666*84d9c625SLionel Sambuc                 if (!anymeta) {
6672fe8fb19SBen Gras 			pathend = q;
6682fe8fb19SBen Gras 			pattern = p;
6692fe8fb19SBen Gras 			while (*pattern == SEP) {
6702fe8fb19SBen Gras 				if (pathend >= pathlim)
6712fe8fb19SBen Gras 					return GLOB_ABORTED;
6722fe8fb19SBen Gras 				*pathend++ = *pattern++;
6732fe8fb19SBen Gras 			}
6742fe8fb19SBen Gras 		} else			/* Need expansion, recurse. */
6752fe8fb19SBen Gras 			return glob3(pathbuf, pathend, pathlim, pattern, p,
6762fe8fb19SBen Gras 			    pattern, pglob, limit);
6772fe8fb19SBen Gras 	}
6782fe8fb19SBen Gras 	/* NOTREACHED */
6792fe8fb19SBen Gras }
6802fe8fb19SBen Gras 
6812fe8fb19SBen Gras static int
glob3(Char * pathbuf,Char * pathend,Char * pathlim,const Char * pattern,const Char * restpattern,const Char * pglobstar,glob_t * pglob,struct glob_limit * limit)6822fe8fb19SBen Gras glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
6832fe8fb19SBen Gras     const Char *restpattern, const Char *pglobstar, glob_t *pglob,
6842fe8fb19SBen Gras     struct glob_limit *limit)
6852fe8fb19SBen Gras {
6862fe8fb19SBen Gras 	struct dirent *dp;
6872fe8fb19SBen Gras 	DIR *dirp;
6882fe8fb19SBen Gras 	__gl_stat_t sbuf;
6892fe8fb19SBen Gras 	int error;
6902fe8fb19SBen Gras 	char buf[MAXPATHLEN];
6912fe8fb19SBen Gras 	int globstar = 0;
6922fe8fb19SBen Gras 	int chase_symlinks = 0;
6932fe8fb19SBen Gras 	const Char *termstar = NULL;
6942fe8fb19SBen Gras 
6952fe8fb19SBen Gras 	/*
6962fe8fb19SBen Gras 	 * The readdirfunc declaration can't be prototyped, because it is
6972fe8fb19SBen Gras 	 * assigned, below, to two functions which are prototyped in glob.h
6982fe8fb19SBen Gras 	 * and dirent.h as taking pointers to differently typed opaque
6992fe8fb19SBen Gras 	 * structures.
7002fe8fb19SBen Gras 	 */
7012fe8fb19SBen Gras 	struct dirent *(*readdirfunc)(void *);
7022fe8fb19SBen Gras 
7032fe8fb19SBen Gras 	_DIAGASSERT(pathbuf != NULL);
7042fe8fb19SBen Gras 	_DIAGASSERT(pathend != NULL);
7052fe8fb19SBen Gras 	_DIAGASSERT(pattern != NULL);
7062fe8fb19SBen Gras 	_DIAGASSERT(restpattern != NULL);
7072fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
7082fe8fb19SBen Gras 
7092fe8fb19SBen Gras 	*pathend = EOS;
7102fe8fb19SBen Gras 	errno = 0;
7112fe8fb19SBen Gras 
7122fe8fb19SBen Gras 	while (pglobstar < restpattern) {
7132fe8fb19SBen Gras 		if ((pglobstar[0] & M_MASK) == M_ALL &&
7142fe8fb19SBen Gras 		    (pglobstar[1] & M_MASK) == M_ALL) {
7152fe8fb19SBen Gras 			globstar = 1;
7162fe8fb19SBen Gras 			chase_symlinks = (pglobstar[2] & M_MASK) == M_ALL;
7172fe8fb19SBen Gras 			termstar = pglobstar + (2 + chase_symlinks);
7182fe8fb19SBen Gras 			break;
7192fe8fb19SBen Gras 		}
7202fe8fb19SBen Gras 		pglobstar++;
7212fe8fb19SBen Gras 	}
7222fe8fb19SBen Gras 
7232fe8fb19SBen Gras 	if (globstar) {
7242fe8fb19SBen Gras 		error = pglobstar == pattern && termstar == restpattern ?
7252fe8fb19SBen Gras 		    *restpattern == EOS ?
7262fe8fb19SBen Gras 		    glob2(pathbuf, pathend, pathlim, restpattern - 1, pglob,
7272fe8fb19SBen Gras 		    limit) :
7282fe8fb19SBen Gras 		    glob2(pathbuf, pathend, pathlim, restpattern + 1, pglob,
7292fe8fb19SBen Gras 		    limit) :
7302fe8fb19SBen Gras 		    glob3(pathbuf, pathend, pathlim, pattern, restpattern,
7312fe8fb19SBen Gras 		    termstar, pglob, limit);
7322fe8fb19SBen Gras 		if (error)
7332fe8fb19SBen Gras 			return error;
7342fe8fb19SBen Gras 		*pathend = EOS;
7352fe8fb19SBen Gras 	}
7362fe8fb19SBen Gras 
7372fe8fb19SBen Gras 	if (*pathbuf && (g_lstat(pathbuf, &sbuf, pglob) ||
7382fe8fb19SBen Gras 	    !S_ISDIR(sbuf.st_mode)
7392fe8fb19SBen Gras #ifdef S_IFLINK
7402fe8fb19SBen Gras 	     && ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode))
7412fe8fb19SBen Gras #endif
7422fe8fb19SBen Gras 	    ))
7432fe8fb19SBen Gras 		return 0;
7442fe8fb19SBen Gras 
7452fe8fb19SBen Gras 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
7462fe8fb19SBen Gras 		if (pglob->gl_errfunc) {
7472fe8fb19SBen Gras 			if (g_Ctoc(pathbuf, buf, sizeof(buf)))
7482fe8fb19SBen Gras 				return GLOB_ABORTED;
7492fe8fb19SBen Gras 			if (pglob->gl_errfunc(buf, errno) ||
7502fe8fb19SBen Gras 			    pglob->gl_flags & GLOB_ERR)
7512fe8fb19SBen Gras 				return GLOB_ABORTED;
7522fe8fb19SBen Gras 		}
7532fe8fb19SBen Gras 		/*
7542fe8fb19SBen Gras 		 * Posix/XOpen: glob should return when it encounters a
7552fe8fb19SBen Gras 		 * directory that it cannot open or read
7562fe8fb19SBen Gras 		 * XXX: Should we ignore ENOTDIR and ENOENT though?
7572fe8fb19SBen Gras 		 * I think that Posix had in mind EPERM...
7582fe8fb19SBen Gras 		 */
7592fe8fb19SBen Gras 		if (pglob->gl_flags & GLOB_ERR)
7602fe8fb19SBen Gras 			return GLOB_ABORTED;
7612fe8fb19SBen Gras 
7622fe8fb19SBen Gras 		return 0;
7632fe8fb19SBen Gras 	}
7642fe8fb19SBen Gras 
7652fe8fb19SBen Gras 	error = 0;
7662fe8fb19SBen Gras 
7672fe8fb19SBen Gras 	/* Search directory for matching names. */
7682fe8fb19SBen Gras 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7692fe8fb19SBen Gras 		readdirfunc = pglob->gl_readdir;
7702fe8fb19SBen Gras 	else
771f14fb602SLionel Sambuc 		readdirfunc = (struct dirent *(*)(void *)) readdir;
7722fe8fb19SBen Gras 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
773*84d9c625SLionel Sambuc 		unsigned char *sc;
7742fe8fb19SBen Gras 		Char *dc;
7752fe8fb19SBen Gras 
7762fe8fb19SBen Gras 		if ((pglob->gl_flags & GLOB_LIMIT) &&
7772fe8fb19SBen Gras 		    limit->l_readdir++ >= GLOB_LIMIT_READDIR) {
7782fe8fb19SBen Gras 			errno = 0;
7792fe8fb19SBen Gras 			*pathend++ = SEP;
7802fe8fb19SBen Gras 			*pathend = EOS;
781f14fb602SLionel Sambuc 			error = GLOB_NOSPACE;
782f14fb602SLionel Sambuc 			break;
7832fe8fb19SBen Gras 		}
7842fe8fb19SBen Gras 
7852fe8fb19SBen Gras 		/*
7862fe8fb19SBen Gras 		 * Initial DOT must be matched literally, unless we have
7872fe8fb19SBen Gras 		 * GLOB_PERIOD set.
7882fe8fb19SBen Gras 		 */
7892fe8fb19SBen Gras 		if ((pglob->gl_flags & GLOB_PERIOD) == 0)
7902fe8fb19SBen Gras 			if (dp->d_name[0] == DOT && *pattern != DOT)
7912fe8fb19SBen Gras 				continue;
7922fe8fb19SBen Gras 		/*
7932fe8fb19SBen Gras 		 * If GLOB_NO_DOTDIRS is set, . and .. vanish.
7942fe8fb19SBen Gras 		 */
7952fe8fb19SBen Gras 		if ((pglob->gl_flags & GLOB_NO_DOTDIRS) &&
7962fe8fb19SBen Gras 		    (dp->d_name[0] == DOT) &&
7972fe8fb19SBen Gras 		    ((dp->d_name[1] == EOS) ||
7982fe8fb19SBen Gras 		     ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS))))
7992fe8fb19SBen Gras 			continue;
8002fe8fb19SBen Gras 		/*
8012fe8fb19SBen Gras 		 * The resulting string contains EOS, so we can
8022fe8fb19SBen Gras 		 * use the pathlim character, if it is the nul
8032fe8fb19SBen Gras 		 */
804*84d9c625SLionel Sambuc 		for (sc = (unsigned char *) dp->d_name, dc = pathend;
8052fe8fb19SBen Gras 		     dc <= pathlim && (*dc++ = *sc++) != EOS;)
8062fe8fb19SBen Gras 			continue;
8072fe8fb19SBen Gras 
8082fe8fb19SBen Gras 		/*
8092fe8fb19SBen Gras 		 * Have we filled the buffer without seeing EOS?
8102fe8fb19SBen Gras 		 */
8112fe8fb19SBen Gras 		if (dc > pathlim && *pathlim != EOS) {
8122fe8fb19SBen Gras 			/*
8132fe8fb19SBen Gras 			 * Abort when requested by caller, otherwise
8142fe8fb19SBen Gras 			 * reset pathend back to last SEP and continue
8152fe8fb19SBen Gras 			 * with next dir entry.
8162fe8fb19SBen Gras 			 */
8172fe8fb19SBen Gras 			if (pglob->gl_flags & GLOB_ERR) {
8182fe8fb19SBen Gras 				error = GLOB_ABORTED;
8192fe8fb19SBen Gras 				break;
8202fe8fb19SBen Gras 			}
8212fe8fb19SBen Gras 			else {
8222fe8fb19SBen Gras 				*pathend = EOS;
8232fe8fb19SBen Gras 				continue;
8242fe8fb19SBen Gras 			}
8252fe8fb19SBen Gras 		}
8262fe8fb19SBen Gras 
8272fe8fb19SBen Gras 		if (globstar) {
8282fe8fb19SBen Gras #ifdef S_IFLNK
8292fe8fb19SBen Gras 			if (!chase_symlinks &&
8302fe8fb19SBen Gras 			    (g_lstat(pathbuf, &sbuf, pglob) ||
8312fe8fb19SBen Gras 			    S_ISLNK(sbuf.st_mode)))
8322fe8fb19SBen Gras 				continue;
8332fe8fb19SBen Gras #endif
8342fe8fb19SBen Gras 
8352fe8fb19SBen Gras 			if (!match(pathend, pattern, termstar))
8362fe8fb19SBen Gras 				continue;
8372fe8fb19SBen Gras 
8382fe8fb19SBen Gras 			if (--dc < pathlim - 2)
8392fe8fb19SBen Gras 				*dc++ = SEP;
8402fe8fb19SBen Gras 			*dc = EOS;
8412fe8fb19SBen Gras 			error = glob2(pathbuf, dc, pathlim, pglobstar,
8422fe8fb19SBen Gras 			    pglob, limit);
8432fe8fb19SBen Gras 			if (error)
8442fe8fb19SBen Gras 				break;
8452fe8fb19SBen Gras 			*pathend = EOS;
8462fe8fb19SBen Gras 		} else {
8472fe8fb19SBen Gras 			if (!match(pathend, pattern, restpattern)) {
8482fe8fb19SBen Gras 				*pathend = EOS;
8492fe8fb19SBen Gras 				continue;
8502fe8fb19SBen Gras 			}
8512fe8fb19SBen Gras 			error = glob2(pathbuf, --dc, pathlim, restpattern,
8522fe8fb19SBen Gras 			    pglob, limit);
8532fe8fb19SBen Gras 			if (error)
8542fe8fb19SBen Gras 				break;
8552fe8fb19SBen Gras 		}
8562fe8fb19SBen Gras 	}
8572fe8fb19SBen Gras 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8582fe8fb19SBen Gras 		(*pglob->gl_closedir)(dirp);
8592fe8fb19SBen Gras 	else
8602fe8fb19SBen Gras 		closedir(dirp);
8612fe8fb19SBen Gras 
8622fe8fb19SBen Gras 	/*
8632fe8fb19SBen Gras 	 * Again Posix X/Open issue with regards to error handling.
8642fe8fb19SBen Gras 	 */
8652fe8fb19SBen Gras 	if ((error || errno) && (pglob->gl_flags & GLOB_ERR))
8662fe8fb19SBen Gras 		return GLOB_ABORTED;
8672fe8fb19SBen Gras 
8682fe8fb19SBen Gras 	return error;
8692fe8fb19SBen Gras }
8702fe8fb19SBen Gras 
8712fe8fb19SBen Gras 
8722fe8fb19SBen Gras /*
8732fe8fb19SBen Gras  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
8742fe8fb19SBen Gras  * add the new item, and update gl_pathc.
8752fe8fb19SBen Gras  *
8762fe8fb19SBen Gras  * This assumes the BSD realloc, which only copies the block when its size
8772fe8fb19SBen Gras  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
8782fe8fb19SBen Gras  * behavior.
8792fe8fb19SBen Gras  *
8802fe8fb19SBen Gras  * Return 0 if new item added, error code if memory couldn't be allocated.
8812fe8fb19SBen Gras  *
8822fe8fb19SBen Gras  * Invariant of the glob_t structure:
8832fe8fb19SBen Gras  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
8842fe8fb19SBen Gras  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
8852fe8fb19SBen Gras  */
8862fe8fb19SBen Gras static int
globextend(const Char * path,glob_t * pglob,struct glob_limit * limit)8872fe8fb19SBen Gras globextend(const Char *path, glob_t *pglob, struct glob_limit *limit)
8882fe8fb19SBen Gras {
8892fe8fb19SBen Gras 	char **pathv;
8902fe8fb19SBen Gras 	size_t i, newsize, len;
8912fe8fb19SBen Gras 	char *copy;
8922fe8fb19SBen Gras 	const Char *p;
8932fe8fb19SBen Gras 
8942fe8fb19SBen Gras 	_DIAGASSERT(path != NULL);
8952fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
8962fe8fb19SBen Gras 
8972fe8fb19SBen Gras 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
8982fe8fb19SBen Gras 	if ((pglob->gl_flags & GLOB_LIMIT) &&
8992fe8fb19SBen Gras 	    newsize > GLOB_LIMIT_PATH * sizeof(*pathv))
9002fe8fb19SBen Gras 		goto nospace;
9012fe8fb19SBen Gras 	pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) :
9022fe8fb19SBen Gras 	    malloc(newsize);
9032fe8fb19SBen Gras 	if (pathv == NULL)
9042fe8fb19SBen Gras 		return GLOB_NOSPACE;
9052fe8fb19SBen Gras 
9062fe8fb19SBen Gras 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
9072fe8fb19SBen Gras 		/* first time around -- clear initial gl_offs items */
9082fe8fb19SBen Gras 		pathv += pglob->gl_offs;
9092fe8fb19SBen Gras 		for (i = pglob->gl_offs + 1; --i > 0; )
9102fe8fb19SBen Gras 			*--pathv = NULL;
9112fe8fb19SBen Gras 	}
9122fe8fb19SBen Gras 	pglob->gl_pathv = pathv;
9132fe8fb19SBen Gras 
9142fe8fb19SBen Gras 	for (p = path; *p++;)
9152fe8fb19SBen Gras 		continue;
9162fe8fb19SBen Gras 	len = (size_t)(p - path);
9172fe8fb19SBen Gras 	limit->l_string += len;
9182fe8fb19SBen Gras 	if ((copy = malloc(len)) != NULL) {
9192fe8fb19SBen Gras 		if (g_Ctoc(path, copy, len)) {
9202fe8fb19SBen Gras 			free(copy);
9212fe8fb19SBen Gras 			return GLOB_ABORTED;
9222fe8fb19SBen Gras 		}
9232fe8fb19SBen Gras 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
9242fe8fb19SBen Gras 	}
9252fe8fb19SBen Gras 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
9262fe8fb19SBen Gras 
9272fe8fb19SBen Gras 	if ((pglob->gl_flags & GLOB_LIMIT) &&
9282fe8fb19SBen Gras 	    (newsize + limit->l_string) >= GLOB_LIMIT_STRING)
9292fe8fb19SBen Gras 		goto nospace;
9302fe8fb19SBen Gras 
9312fe8fb19SBen Gras 	return copy == NULL ? GLOB_NOSPACE : 0;
9322fe8fb19SBen Gras nospace:
9332fe8fb19SBen Gras 	errno = 0;
9342fe8fb19SBen Gras 	return GLOB_NOSPACE;
9352fe8fb19SBen Gras }
9362fe8fb19SBen Gras 
9372fe8fb19SBen Gras 
9382fe8fb19SBen Gras /*
9392fe8fb19SBen Gras  * pattern matching function for filenames.  Each occurrence of the *
9402fe8fb19SBen Gras  * pattern causes a recursion level.
9412fe8fb19SBen Gras  */
9422fe8fb19SBen Gras static int
match(const Char * name,const Char * pat,const Char * patend)9432fe8fb19SBen Gras match(const Char *name, const Char *pat, const Char *patend)
9442fe8fb19SBen Gras {
9452fe8fb19SBen Gras 	int ok, negate_range;
9462fe8fb19SBen Gras 	Char c, k;
9472fe8fb19SBen Gras 
9482fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
9492fe8fb19SBen Gras 	_DIAGASSERT(pat != NULL);
9502fe8fb19SBen Gras 	_DIAGASSERT(patend != NULL);
9512fe8fb19SBen Gras 
9522fe8fb19SBen Gras 	while (pat < patend) {
9532fe8fb19SBen Gras 		c = *pat++;
9542fe8fb19SBen Gras 		switch (c & M_MASK) {
9552fe8fb19SBen Gras 		case M_ALL:
9562fe8fb19SBen Gras 			while (pat < patend && (*pat & M_MASK) == M_ALL)
9572fe8fb19SBen Gras 				pat++;	/* eat consecutive '*' */
9582fe8fb19SBen Gras 			if (pat == patend)
9592fe8fb19SBen Gras 				return 1;
9602fe8fb19SBen Gras 			for (; !match(name, pat, patend); name++)
9612fe8fb19SBen Gras 				if (*name == EOS)
9622fe8fb19SBen Gras 					return 0;
9632fe8fb19SBen Gras 			return 1;
9642fe8fb19SBen Gras 		case M_ONE:
9652fe8fb19SBen Gras 			if (*name++ == EOS)
9662fe8fb19SBen Gras 				return 0;
9672fe8fb19SBen Gras 			break;
9682fe8fb19SBen Gras 		case M_SET:
9692fe8fb19SBen Gras 			ok = 0;
9702fe8fb19SBen Gras 			if ((k = *name++) == EOS)
9712fe8fb19SBen Gras 				return 0;
9722fe8fb19SBen Gras 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
9732fe8fb19SBen Gras 				++pat;
9742fe8fb19SBen Gras 			while (((c = *pat++) & M_MASK) != M_END)
9752fe8fb19SBen Gras 				if ((*pat & M_MASK) == M_RNG) {
9762fe8fb19SBen Gras 					if (c <= k && k <= pat[1])
9772fe8fb19SBen Gras 						ok = 1;
9782fe8fb19SBen Gras 					pat += 2;
9792fe8fb19SBen Gras 				} else if (c == k)
9802fe8fb19SBen Gras 					ok = 1;
9812fe8fb19SBen Gras 			if (ok == negate_range)
9822fe8fb19SBen Gras 				return 0;
9832fe8fb19SBen Gras 			break;
9842fe8fb19SBen Gras 		default:
9852fe8fb19SBen Gras 			if (*name++ != c)
9862fe8fb19SBen Gras 				return 0;
9872fe8fb19SBen Gras 			break;
9882fe8fb19SBen Gras 		}
9892fe8fb19SBen Gras 	}
9902fe8fb19SBen Gras 	return *name == EOS;
9912fe8fb19SBen Gras }
9922fe8fb19SBen Gras 
9932fe8fb19SBen Gras /* Free allocated data belonging to a glob_t structure. */
9942fe8fb19SBen Gras void
globfree(glob_t * pglob)9952fe8fb19SBen Gras globfree(glob_t *pglob)
9962fe8fb19SBen Gras {
9972fe8fb19SBen Gras 	size_t i;
9982fe8fb19SBen Gras 	char **pp;
9992fe8fb19SBen Gras 
10002fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
10012fe8fb19SBen Gras 
10022fe8fb19SBen Gras 	if (pglob->gl_pathv != NULL) {
10032fe8fb19SBen Gras 		pp = pglob->gl_pathv + pglob->gl_offs;
10042fe8fb19SBen Gras 		for (i = pglob->gl_pathc; i--; ++pp)
10052fe8fb19SBen Gras 			if (*pp)
10062fe8fb19SBen Gras 				free(*pp);
10072fe8fb19SBen Gras 		free(pglob->gl_pathv);
10082fe8fb19SBen Gras 		pglob->gl_pathv = NULL;
10092fe8fb19SBen Gras 		pglob->gl_pathc = 0;
10102fe8fb19SBen Gras 	}
10112fe8fb19SBen Gras }
10122fe8fb19SBen Gras 
10132fe8fb19SBen Gras #ifndef __LIBC12_SOURCE__
10142fe8fb19SBen Gras int
glob_pattern_p(const char * pattern,int quote)10152fe8fb19SBen Gras glob_pattern_p(const char *pattern, int quote)
10162fe8fb19SBen Gras {
10172fe8fb19SBen Gras 	int range = 0;
10182fe8fb19SBen Gras 
10192fe8fb19SBen Gras 	for (; *pattern; pattern++)
10202fe8fb19SBen Gras 		switch (*pattern) {
10212fe8fb19SBen Gras 		case QUESTION:
10222fe8fb19SBen Gras 		case STAR:
10232fe8fb19SBen Gras 			return 1;
10242fe8fb19SBen Gras 
10252fe8fb19SBen Gras 		case QUOTE:
10262fe8fb19SBen Gras 			if (quote && pattern[1] != EOS)
10272fe8fb19SBen Gras 			      ++pattern;
10282fe8fb19SBen Gras 			break;
10292fe8fb19SBen Gras 
10302fe8fb19SBen Gras 		case LBRACKET:
10312fe8fb19SBen Gras 			range = 1;
10322fe8fb19SBen Gras 			break;
10332fe8fb19SBen Gras 
10342fe8fb19SBen Gras 		case RBRACKET:
10352fe8fb19SBen Gras 			if (range)
10362fe8fb19SBen Gras 			      return 1;
10372fe8fb19SBen Gras 			break;
10382fe8fb19SBen Gras 		default:
10392fe8fb19SBen Gras 			break;
10402fe8fb19SBen Gras 		}
10412fe8fb19SBen Gras 
10422fe8fb19SBen Gras 	  return 0;
10432fe8fb19SBen Gras }
10442fe8fb19SBen Gras #endif
10452fe8fb19SBen Gras 
10462fe8fb19SBen Gras static DIR *
g_opendir(Char * str,glob_t * pglob)10472fe8fb19SBen Gras g_opendir(Char *str, glob_t *pglob)
10482fe8fb19SBen Gras {
10492fe8fb19SBen Gras 	char buf[MAXPATHLEN];
10502fe8fb19SBen Gras 
10512fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
10522fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
10532fe8fb19SBen Gras 
10542fe8fb19SBen Gras 	if (!*str)
10552fe8fb19SBen Gras 		(void)strlcpy(buf, ".", sizeof(buf));
10562fe8fb19SBen Gras 	else {
10572fe8fb19SBen Gras 		if (g_Ctoc(str, buf, sizeof(buf)))
10582fe8fb19SBen Gras 			return NULL;
10592fe8fb19SBen Gras 	}
10602fe8fb19SBen Gras 
10612fe8fb19SBen Gras 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
10622fe8fb19SBen Gras 		return (*pglob->gl_opendir)(buf);
10632fe8fb19SBen Gras 
10642fe8fb19SBen Gras 	return opendir(buf);
10652fe8fb19SBen Gras }
10662fe8fb19SBen Gras 
10672fe8fb19SBen Gras static int
g_lstat(Char * fn,__gl_stat_t * sb,glob_t * pglob)10682fe8fb19SBen Gras g_lstat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
10692fe8fb19SBen Gras {
10702fe8fb19SBen Gras 	char buf[MAXPATHLEN];
10712fe8fb19SBen Gras 
10722fe8fb19SBen Gras 	_DIAGASSERT(fn != NULL);
10732fe8fb19SBen Gras 	_DIAGASSERT(sb != NULL);
10742fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
10752fe8fb19SBen Gras 
10762fe8fb19SBen Gras 	if (g_Ctoc(fn, buf, sizeof(buf)))
10772fe8fb19SBen Gras 		return -1;
10782fe8fb19SBen Gras 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
10792fe8fb19SBen Gras 		return (*pglob->gl_lstat)(buf, sb);
10802fe8fb19SBen Gras 	return lstat(buf, sb);
10812fe8fb19SBen Gras }
10822fe8fb19SBen Gras 
10832fe8fb19SBen Gras static int
g_stat(Char * fn,__gl_stat_t * sb,glob_t * pglob)10842fe8fb19SBen Gras g_stat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
10852fe8fb19SBen Gras {
10862fe8fb19SBen Gras 	char buf[MAXPATHLEN];
10872fe8fb19SBen Gras 
10882fe8fb19SBen Gras 	_DIAGASSERT(fn != NULL);
10892fe8fb19SBen Gras 	_DIAGASSERT(sb != NULL);
10902fe8fb19SBen Gras 	_DIAGASSERT(pglob != NULL);
10912fe8fb19SBen Gras 
10922fe8fb19SBen Gras 	if (g_Ctoc(fn, buf, sizeof(buf)))
10932fe8fb19SBen Gras 		return -1;
10942fe8fb19SBen Gras 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
10952fe8fb19SBen Gras 		return (*pglob->gl_stat)(buf, sb);
10962fe8fb19SBen Gras 	return stat(buf, sb);
10972fe8fb19SBen Gras }
10982fe8fb19SBen Gras 
10992fe8fb19SBen Gras static Char *
g_strchr(const Char * str,int ch)11002fe8fb19SBen Gras g_strchr(const Char *str, int ch)
11012fe8fb19SBen Gras {
11022fe8fb19SBen Gras 
11032fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
11042fe8fb19SBen Gras 
11052fe8fb19SBen Gras 	do {
11062fe8fb19SBen Gras 		if (*str == ch)
11072fe8fb19SBen Gras 			return __UNCONST(str);
11082fe8fb19SBen Gras 	} while (*str++);
11092fe8fb19SBen Gras 	return NULL;
11102fe8fb19SBen Gras }
11112fe8fb19SBen Gras 
11122fe8fb19SBen Gras static int
g_Ctoc(const Char * str,char * buf,size_t len)11132fe8fb19SBen Gras g_Ctoc(const Char *str, char *buf, size_t len)
11142fe8fb19SBen Gras {
11152fe8fb19SBen Gras 	char *dc;
11162fe8fb19SBen Gras 
11172fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
11182fe8fb19SBen Gras 	_DIAGASSERT(buf != NULL);
11192fe8fb19SBen Gras 
11202fe8fb19SBen Gras 	if (len == 0)
11212fe8fb19SBen Gras 		return 1;
11222fe8fb19SBen Gras 
11232fe8fb19SBen Gras 	for (dc = buf; len && (*dc++ = *str++) != EOS; len--)
11242fe8fb19SBen Gras 		continue;
11252fe8fb19SBen Gras 
11262fe8fb19SBen Gras 	return len == 0;
11272fe8fb19SBen Gras }
11282fe8fb19SBen Gras 
11292fe8fb19SBen Gras #ifdef DEBUG
11302fe8fb19SBen Gras static void
qprintf(const char * str,Char * s)11312fe8fb19SBen Gras qprintf(const char *str, Char *s)
11322fe8fb19SBen Gras {
11332fe8fb19SBen Gras 	Char *p;
11342fe8fb19SBen Gras 
11352fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
11362fe8fb19SBen Gras 	_DIAGASSERT(s != NULL);
11372fe8fb19SBen Gras 
11382fe8fb19SBen Gras 	(void)printf("%s:\n", str);
11392fe8fb19SBen Gras 	for (p = s; *p; p++)
11402fe8fb19SBen Gras 		(void)printf("%c", CHAR(*p));
11412fe8fb19SBen Gras 	(void)printf("\n");
11422fe8fb19SBen Gras 	for (p = s; *p; p++)
11432fe8fb19SBen Gras 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
11442fe8fb19SBen Gras 	(void)printf("\n");
11452fe8fb19SBen Gras 	for (p = s; *p; p++)
11462fe8fb19SBen Gras 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
11472fe8fb19SBen Gras 	(void)printf("\n");
11482fe8fb19SBen Gras }
11492fe8fb19SBen Gras #endif
1150