xref: /freebsd/lib/libc/gen/glob.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
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  *
8*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
9*3c87aa1dSDavid Chisnall  * All rights reserved.
10*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
11*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
12*3c87aa1dSDavid 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
74e9346e01STim J. Robbins  *    single-byte characters with a value of the first byte 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 
95edcfa072SAndrey A. Chernov #include "collate.h"
96edcfa072SAndrey A. Chernov 
9758f0484fSRodney W. Grimes #define	DOLLAR		'$'
9858f0484fSRodney W. Grimes #define	DOT		'.'
9958f0484fSRodney W. Grimes #define	EOS		'\0'
10058f0484fSRodney W. Grimes #define	LBRACKET	'['
10158f0484fSRodney W. Grimes #define	NOT		'!'
10258f0484fSRodney W. Grimes #define	QUESTION	'?'
10358f0484fSRodney W. Grimes #define	QUOTE		'\\'
10458f0484fSRodney W. Grimes #define	RANGE		'-'
10558f0484fSRodney W. Grimes #define	RBRACKET	']'
10658f0484fSRodney W. Grimes #define	SEP		'/'
10758f0484fSRodney W. Grimes #define	STAR		'*'
10858f0484fSRodney W. Grimes #define	TILDE		'~'
10958f0484fSRodney W. Grimes #define	UNDERSCORE	'_'
11058f0484fSRodney W. Grimes #define	LBRACE		'{'
11158f0484fSRodney W. Grimes #define	RBRACE		'}'
11258f0484fSRodney W. Grimes #define	SLASH		'/'
11358f0484fSRodney W. Grimes #define	COMMA		','
11458f0484fSRodney W. Grimes 
11558f0484fSRodney W. Grimes #ifndef DEBUG
11658f0484fSRodney W. Grimes 
117e9346e01STim J. Robbins #define	M_QUOTE		0x8000000000ULL
118e9346e01STim J. Robbins #define	M_PROTECT	0x4000000000ULL
119e9346e01STim J. Robbins #define	M_MASK		0xffffffffffULL
120e9346e01STim J. Robbins #define	M_CHAR		0x00ffffffffULL
12158f0484fSRodney W. Grimes 
122e9346e01STim J. Robbins typedef uint_fast64_t Char;
12358f0484fSRodney W. Grimes 
12458f0484fSRodney W. Grimes #else
12558f0484fSRodney W. Grimes 
12658f0484fSRodney W. Grimes #define	M_QUOTE		0x80
12758f0484fSRodney W. Grimes #define	M_PROTECT	0x40
12858f0484fSRodney W. Grimes #define	M_MASK		0xff
129e9346e01STim J. Robbins #define	M_CHAR		0x7f
13058f0484fSRodney W. Grimes 
13158f0484fSRodney W. Grimes typedef char Char;
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes #endif
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes 
136e9346e01STim J. Robbins #define	CHAR(c)		((Char)((c)&M_CHAR))
13758f0484fSRodney W. Grimes #define	META(c)		((Char)((c)|M_QUOTE))
13858f0484fSRodney W. Grimes #define	M_ALL		META('*')
13958f0484fSRodney W. Grimes #define	M_END		META(']')
14058f0484fSRodney W. Grimes #define	M_NOT		META('!')
14158f0484fSRodney W. Grimes #define	M_ONE		META('?')
14258f0484fSRodney W. Grimes #define	M_RNG		META('-')
14358f0484fSRodney W. Grimes #define	M_SET		META('[')
14458f0484fSRodney W. Grimes #define	ismeta(c)	(((c)&M_QUOTE) != 0)
14558f0484fSRodney W. Grimes 
14658f0484fSRodney W. Grimes 
147b231cb39SDavid E. O'Brien static int	 compare(const void *, const void *);
1484b767fa6SAndrey A. Chernov static int	 g_Ctoc(const Char *, char *, size_t);
149b231cb39SDavid E. O'Brien static int	 g_lstat(Char *, struct stat *, glob_t *);
150b231cb39SDavid E. O'Brien static DIR	*g_opendir(Char *, glob_t *);
15134a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t);
15258f0484fSRodney W. Grimes #ifdef notdef
153b231cb39SDavid E. O'Brien static Char	*g_strcat(Char *, const Char *);
15458f0484fSRodney W. Grimes #endif
155b231cb39SDavid E. O'Brien static int	 g_stat(Char *, struct stat *, glob_t *);
1564b767fa6SAndrey A. Chernov static int	 glob0(const Char *, glob_t *, size_t *);
1574b767fa6SAndrey A. Chernov static int	 glob1(Char *, glob_t *, size_t *);
1584b767fa6SAndrey A. Chernov static int	 glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
1594b767fa6SAndrey A. Chernov static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
1604b767fa6SAndrey A. Chernov static int	 globextend(const Char *, glob_t *, size_t *);
161487dbd92SPeter Wemm static const Char *
162b231cb39SDavid E. O'Brien 		 globtilde(const Char *, Char *, size_t, glob_t *);
1634b767fa6SAndrey A. Chernov static int	 globexp1(const Char *, glob_t *, size_t *);
1644b767fa6SAndrey A. Chernov static int	 globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
165b231cb39SDavid E. O'Brien static int	 match(Char *, Char *, Char *);
16658f0484fSRodney W. Grimes #ifdef DEBUG
167b231cb39SDavid E. O'Brien static void	 qprintf(const char *, Char *);
16858f0484fSRodney W. Grimes #endif
16958f0484fSRodney W. Grimes 
17058f0484fSRodney W. Grimes int
1711cec70adSXin LI glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
17258f0484fSRodney W. Grimes {
1731cec70adSXin LI 	const char *patnext;
1744b767fa6SAndrey A. Chernov 	size_t limit;
175e9346e01STim J. Robbins 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
176e9346e01STim J. Robbins 	mbstate_t mbs;
177e9346e01STim J. Robbins 	wchar_t wc;
178e9346e01STim J. Robbins 	size_t clen;
17958f0484fSRodney W. Grimes 
1801cec70adSXin LI 	patnext = pattern;
18158f0484fSRodney W. Grimes 	if (!(flags & GLOB_APPEND)) {
18258f0484fSRodney W. Grimes 		pglob->gl_pathc = 0;
18358f0484fSRodney W. Grimes 		pglob->gl_pathv = NULL;
18458f0484fSRodney W. Grimes 		if (!(flags & GLOB_DOOFFS))
18558f0484fSRodney W. Grimes 			pglob->gl_offs = 0;
18658f0484fSRodney W. Grimes 	}
18775dc5f1aSMike Heffner 	if (flags & GLOB_LIMIT) {
188bae8632fSJonathan Lemon 		limit = pglob->gl_matchc;
18975dc5f1aSMike Heffner 		if (limit == 0)
19075dc5f1aSMike Heffner 			limit = ARG_MAX;
19175dc5f1aSMike Heffner 	} else
192bae8632fSJonathan Lemon 		limit = 0;
19358f0484fSRodney W. Grimes 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
19458f0484fSRodney W. Grimes 	pglob->gl_errfunc = errfunc;
19558f0484fSRodney W. Grimes 	pglob->gl_matchc = 0;
19658f0484fSRodney W. Grimes 
19758f0484fSRodney W. Grimes 	bufnext = patbuf;
198487dbd92SPeter Wemm 	bufend = bufnext + MAXPATHLEN - 1;
199e9346e01STim J. Robbins 	if (flags & GLOB_NOESCAPE) {
200e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
201e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
202e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
203e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
204e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
205e9346e01STim J. Robbins 			else if (clen == 0)
206e9346e01STim J. Robbins 				break;
207e9346e01STim J. Robbins 			*bufnext++ = wc;
208e9346e01STim J. Robbins 			patnext += clen;
209e9346e01STim J. Robbins 		}
210e9346e01STim J. Robbins 	} else {
21158f0484fSRodney W. Grimes 		/* Protect the quoted characters. */
212e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
213e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
214e9346e01STim J. Robbins 			if (*patnext == QUOTE) {
215e9346e01STim J. Robbins 				if (*++patnext == EOS) {
216e9346e01STim J. Robbins 					*bufnext++ = QUOTE | M_PROTECT;
217e9346e01STim J. Robbins 					continue;
21858f0484fSRodney W. Grimes 				}
219e9346e01STim J. Robbins 				prot = M_PROTECT;
220e9346e01STim J. Robbins 			} else
221e9346e01STim J. Robbins 				prot = 0;
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 | prot;
228e9346e01STim J. Robbins 			patnext += clen;
22958f0484fSRodney W. Grimes 		}
23058f0484fSRodney W. Grimes 	}
23158f0484fSRodney W. Grimes 	*bufnext = EOS;
23258f0484fSRodney W. Grimes 
23358f0484fSRodney W. Grimes 	if (flags & GLOB_BRACE)
234bae8632fSJonathan Lemon 	    return globexp1(patbuf, pglob, &limit);
23558f0484fSRodney W. Grimes 	else
236bae8632fSJonathan Lemon 	    return glob0(patbuf, pglob, &limit);
23758f0484fSRodney W. Grimes }
23858f0484fSRodney W. Grimes 
23958f0484fSRodney W. Grimes /*
24058f0484fSRodney W. Grimes  * Expand recursively a glob {} pattern. When there is no more expansion
24158f0484fSRodney W. Grimes  * invoke the standard globbing routine to glob the rest of the magic
24258f0484fSRodney W. Grimes  * characters
24358f0484fSRodney W. Grimes  */
244487dbd92SPeter Wemm static int
2451cec70adSXin LI globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
24658f0484fSRodney W. Grimes {
24758f0484fSRodney W. Grimes 	const Char* ptr = pattern;
24858f0484fSRodney W. Grimes 	int rv;
24958f0484fSRodney W. Grimes 
25058f0484fSRodney W. Grimes 	/* Protect a single {}, for find(1), like csh */
25158f0484fSRodney W. Grimes 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
252bae8632fSJonathan Lemon 		return glob0(pattern, pglob, limit);
25358f0484fSRodney W. Grimes 
25434a08754SMike Makonnen 	while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
255bae8632fSJonathan Lemon 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
25658f0484fSRodney W. Grimes 			return rv;
25758f0484fSRodney W. Grimes 
258bae8632fSJonathan Lemon 	return glob0(pattern, pglob, limit);
25958f0484fSRodney W. Grimes }
26058f0484fSRodney W. Grimes 
26158f0484fSRodney W. Grimes 
26258f0484fSRodney W. Grimes /*
26358f0484fSRodney W. Grimes  * Recursive brace globbing helper. Tries to expand a single brace.
26458f0484fSRodney W. Grimes  * If it succeeds then it invokes globexp1 with the new pattern.
26558f0484fSRodney W. Grimes  * If it fails then it tries to glob the rest of the pattern and returns.
26658f0484fSRodney W. Grimes  */
267487dbd92SPeter Wemm static int
2681cec70adSXin LI globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, size_t *limit)
26958f0484fSRodney W. Grimes {
27058f0484fSRodney W. Grimes 	int     i;
27158f0484fSRodney W. Grimes 	Char   *lm, *ls;
272369316a8SAndrey A. Chernov 	const Char *pe, *pm, *pm1, *pl;
273487dbd92SPeter Wemm 	Char    patbuf[MAXPATHLEN];
27458f0484fSRodney W. Grimes 
27558f0484fSRodney W. Grimes 	/* copy part up to the brace */
27658f0484fSRodney W. Grimes 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
27758f0484fSRodney W. Grimes 		continue;
278487dbd92SPeter Wemm 	*lm = EOS;
27958f0484fSRodney W. Grimes 	ls = lm;
28058f0484fSRodney W. Grimes 
28158f0484fSRodney W. Grimes 	/* Find the balanced brace */
28258f0484fSRodney W. Grimes 	for (i = 0, pe = ++ptr; *pe; pe++)
28358f0484fSRodney W. Grimes 		if (*pe == LBRACKET) {
28458f0484fSRodney W. Grimes 			/* Ignore everything between [] */
28558f0484fSRodney W. Grimes 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
28658f0484fSRodney W. Grimes 				continue;
28758f0484fSRodney W. Grimes 			if (*pe == EOS) {
28858f0484fSRodney W. Grimes 				/*
28958f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
29058f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
29158f0484fSRodney W. Grimes 				 */
29258f0484fSRodney W. Grimes 				pe = pm;
29358f0484fSRodney W. Grimes 			}
29458f0484fSRodney W. Grimes 		}
29558f0484fSRodney W. Grimes 		else if (*pe == LBRACE)
29658f0484fSRodney W. Grimes 			i++;
29758f0484fSRodney W. Grimes 		else if (*pe == RBRACE) {
29858f0484fSRodney W. Grimes 			if (i == 0)
29958f0484fSRodney W. Grimes 				break;
30058f0484fSRodney W. Grimes 			i--;
30158f0484fSRodney W. Grimes 		}
30258f0484fSRodney W. Grimes 
30358f0484fSRodney W. Grimes 	/* Non matching braces; just glob the pattern */
30458f0484fSRodney W. Grimes 	if (i != 0 || *pe == EOS) {
305bae8632fSJonathan Lemon 		*rv = glob0(patbuf, pglob, limit);
30658f0484fSRodney W. Grimes 		return 0;
30758f0484fSRodney W. Grimes 	}
30858f0484fSRodney W. Grimes 
30958f0484fSRodney W. Grimes 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
31058f0484fSRodney W. Grimes 		switch (*pm) {
31158f0484fSRodney W. Grimes 		case LBRACKET:
31258f0484fSRodney W. Grimes 			/* Ignore everything between [] */
313369316a8SAndrey A. Chernov 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
31458f0484fSRodney W. Grimes 				continue;
31558f0484fSRodney W. Grimes 			if (*pm == EOS) {
31658f0484fSRodney W. Grimes 				/*
31758f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
31858f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
31958f0484fSRodney W. Grimes 				 */
320369316a8SAndrey A. Chernov 				pm = pm1;
32158f0484fSRodney W. Grimes 			}
32258f0484fSRodney W. Grimes 			break;
32358f0484fSRodney W. Grimes 
32458f0484fSRodney W. Grimes 		case LBRACE:
32558f0484fSRodney W. Grimes 			i++;
32658f0484fSRodney W. Grimes 			break;
32758f0484fSRodney W. Grimes 
32858f0484fSRodney W. Grimes 		case RBRACE:
32958f0484fSRodney W. Grimes 			if (i) {
33058f0484fSRodney W. Grimes 			    i--;
33158f0484fSRodney W. Grimes 			    break;
33258f0484fSRodney W. Grimes 			}
33358f0484fSRodney W. Grimes 			/* FALLTHROUGH */
33458f0484fSRodney W. Grimes 		case COMMA:
33558f0484fSRodney W. Grimes 			if (i && *pm == COMMA)
33658f0484fSRodney W. Grimes 				break;
33758f0484fSRodney W. Grimes 			else {
33858f0484fSRodney W. Grimes 				/* Append the current string */
33958f0484fSRodney W. Grimes 				for (lm = ls; (pl < pm); *lm++ = *pl++)
34058f0484fSRodney W. Grimes 					continue;
34158f0484fSRodney W. Grimes 				/*
34258f0484fSRodney W. Grimes 				 * Append the rest of the pattern after the
34358f0484fSRodney W. Grimes 				 * closing brace
34458f0484fSRodney W. Grimes 				 */
34558f0484fSRodney W. Grimes 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
34658f0484fSRodney W. Grimes 					continue;
34758f0484fSRodney W. Grimes 
34858f0484fSRodney W. Grimes 				/* Expand the current pattern */
34958f0484fSRodney W. Grimes #ifdef DEBUG
35058f0484fSRodney W. Grimes 				qprintf("globexp2:", patbuf);
35158f0484fSRodney W. Grimes #endif
352bae8632fSJonathan Lemon 				*rv = globexp1(patbuf, pglob, limit);
35358f0484fSRodney W. Grimes 
35458f0484fSRodney W. Grimes 				/* move after the comma, to the next string */
35558f0484fSRodney W. Grimes 				pl = pm + 1;
35658f0484fSRodney W. Grimes 			}
35758f0484fSRodney W. Grimes 			break;
35858f0484fSRodney W. Grimes 
35958f0484fSRodney W. Grimes 		default:
36058f0484fSRodney W. Grimes 			break;
36158f0484fSRodney W. Grimes 		}
36258f0484fSRodney W. Grimes 	*rv = 0;
36358f0484fSRodney W. Grimes 	return 0;
36458f0484fSRodney W. Grimes }
36558f0484fSRodney W. Grimes 
36658f0484fSRodney W. Grimes 
36758f0484fSRodney W. Grimes 
36858f0484fSRodney W. Grimes /*
36958f0484fSRodney W. Grimes  * expand tilde from the passwd file.
37058f0484fSRodney W. Grimes  */
37158f0484fSRodney W. Grimes static const Char *
3721cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
37358f0484fSRodney W. Grimes {
37458f0484fSRodney W. Grimes 	struct passwd *pwd;
37558f0484fSRodney W. Grimes 	char *h;
37658f0484fSRodney W. Grimes 	const Char *p;
37762f187a4SWarner Losh 	Char *b, *eb;
37858f0484fSRodney W. Grimes 
37958f0484fSRodney W. Grimes 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
38058f0484fSRodney W. Grimes 		return pattern;
38158f0484fSRodney W. Grimes 
38262f187a4SWarner Losh 	/*
38362f187a4SWarner Losh 	 * Copy up to the end of the string or /
38462f187a4SWarner Losh 	 */
38562f187a4SWarner Losh 	eb = &patbuf[patbuf_len - 1];
38662f187a4SWarner Losh 	for (p = pattern + 1, h = (char *) patbuf;
38762f187a4SWarner Losh 	    h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
38858f0484fSRodney W. Grimes 		continue;
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes 	*h = EOS;
39158f0484fSRodney W. Grimes 
39258f0484fSRodney W. Grimes 	if (((char *) patbuf)[0] == EOS) {
39358f0484fSRodney W. Grimes 		/*
3943fa69daeSWarner Losh 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
3953fa69daeSWarner Losh 		 * we're not running setuid or setgid) and then trying
3963fa69daeSWarner Losh 		 * the password file
39758f0484fSRodney W. Grimes 		 */
3984539e95aSTim J. Robbins 		if (issetugid() != 0 ||
3999fcbcd02SJohn Birrell 		    (h = getenv("HOME")) == NULL) {
400eb8eee5aSAndrey A. Chernov 			if (((h = getlogin()) != NULL &&
401eb8eee5aSAndrey A. Chernov 			     (pwd = getpwnam(h)) != NULL) ||
402eb8eee5aSAndrey A. Chernov 			    (pwd = getpwuid(getuid())) != NULL)
40358f0484fSRodney W. Grimes 				h = pwd->pw_dir;
404eb8eee5aSAndrey A. Chernov 			else
405eb8eee5aSAndrey A. Chernov 				return pattern;
40658f0484fSRodney W. Grimes 		}
40758f0484fSRodney W. Grimes 	}
40858f0484fSRodney W. Grimes 	else {
40958f0484fSRodney W. Grimes 		/*
41058f0484fSRodney W. Grimes 		 * Expand a ~user
41158f0484fSRodney W. Grimes 		 */
41258f0484fSRodney W. Grimes 		if ((pwd = getpwnam((char*) patbuf)) == NULL)
41358f0484fSRodney W. Grimes 			return pattern;
41458f0484fSRodney W. Grimes 		else
41558f0484fSRodney W. Grimes 			h = pwd->pw_dir;
41658f0484fSRodney W. Grimes 	}
41758f0484fSRodney W. Grimes 
41858f0484fSRodney W. Grimes 	/* Copy the home directory */
41962f187a4SWarner Losh 	for (b = patbuf; b < eb && *h; *b++ = *h++)
42058f0484fSRodney W. Grimes 		continue;
42158f0484fSRodney W. Grimes 
42258f0484fSRodney W. Grimes 	/* Append the rest of the pattern */
42362f187a4SWarner Losh 	while (b < eb && (*b++ = *p++) != EOS)
42458f0484fSRodney W. Grimes 		continue;
42562f187a4SWarner Losh 	*b = EOS;
42658f0484fSRodney W. Grimes 
42758f0484fSRodney W. Grimes 	return patbuf;
42858f0484fSRodney W. Grimes }
42958f0484fSRodney W. Grimes 
43058f0484fSRodney W. Grimes 
43158f0484fSRodney W. Grimes /*
43258f0484fSRodney W. Grimes  * The main glob() routine: compiles the pattern (optionally processing
43358f0484fSRodney W. Grimes  * quotes), calls glob1() to do the real pattern matching, and finally
43458f0484fSRodney W. Grimes  * sorts the list (unless unsorted operation is requested).  Returns 0
4354a59c3abSMike Heffner  * if things went well, nonzero if errors occurred.
43658f0484fSRodney W. Grimes  */
43758f0484fSRodney W. Grimes static int
4381cec70adSXin LI glob0(const Char *pattern, glob_t *pglob, size_t *limit)
43958f0484fSRodney W. Grimes {
44058f0484fSRodney W. Grimes 	const Char *qpatnext;
4415b3fab81SGordon Tetlow 	int err;
4424b767fa6SAndrey A. Chernov 	size_t oldpathc;
4435b3fab81SGordon Tetlow 	Char *bufnext, c, patbuf[MAXPATHLEN];
44458f0484fSRodney W. Grimes 
445487dbd92SPeter Wemm 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
44658f0484fSRodney W. Grimes 	oldpathc = pglob->gl_pathc;
44758f0484fSRodney W. Grimes 	bufnext = patbuf;
44858f0484fSRodney W. Grimes 
44958f0484fSRodney W. Grimes 	/* We don't need to check for buffer overflow any more. */
45058f0484fSRodney W. Grimes 	while ((c = *qpatnext++) != EOS) {
45158f0484fSRodney W. Grimes 		switch (c) {
45258f0484fSRodney W. Grimes 		case LBRACKET:
45358f0484fSRodney W. Grimes 			c = *qpatnext;
45458f0484fSRodney W. Grimes 			if (c == NOT)
45558f0484fSRodney W. Grimes 				++qpatnext;
45658f0484fSRodney W. Grimes 			if (*qpatnext == EOS ||
45734a08754SMike Makonnen 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
45858f0484fSRodney W. Grimes 				*bufnext++ = LBRACKET;
45958f0484fSRodney W. Grimes 				if (c == NOT)
46058f0484fSRodney W. Grimes 					--qpatnext;
46158f0484fSRodney W. Grimes 				break;
46258f0484fSRodney W. Grimes 			}
46358f0484fSRodney W. Grimes 			*bufnext++ = M_SET;
46458f0484fSRodney W. Grimes 			if (c == NOT)
46558f0484fSRodney W. Grimes 				*bufnext++ = M_NOT;
46658f0484fSRodney W. Grimes 			c = *qpatnext++;
46758f0484fSRodney W. Grimes 			do {
46858f0484fSRodney W. Grimes 				*bufnext++ = CHAR(c);
46958f0484fSRodney W. Grimes 				if (*qpatnext == RANGE &&
47058f0484fSRodney W. Grimes 				    (c = qpatnext[1]) != RBRACKET) {
47158f0484fSRodney W. Grimes 					*bufnext++ = M_RNG;
47258f0484fSRodney W. Grimes 					*bufnext++ = CHAR(c);
47358f0484fSRodney W. Grimes 					qpatnext += 2;
47458f0484fSRodney W. Grimes 				}
47558f0484fSRodney W. Grimes 			} while ((c = *qpatnext++) != RBRACKET);
47658f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
47758f0484fSRodney W. Grimes 			*bufnext++ = M_END;
47858f0484fSRodney W. Grimes 			break;
47958f0484fSRodney W. Grimes 		case QUESTION:
48058f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
48158f0484fSRodney W. Grimes 			*bufnext++ = M_ONE;
48258f0484fSRodney W. Grimes 			break;
48358f0484fSRodney W. Grimes 		case STAR:
48458f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
48558f0484fSRodney W. Grimes 			/* collapse adjacent stars to one,
48658f0484fSRodney W. Grimes 			 * to avoid exponential behavior
48758f0484fSRodney W. Grimes 			 */
48858f0484fSRodney W. Grimes 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
48958f0484fSRodney W. Grimes 			    *bufnext++ = M_ALL;
49058f0484fSRodney W. Grimes 			break;
49158f0484fSRodney W. Grimes 		default:
49258f0484fSRodney W. Grimes 			*bufnext++ = CHAR(c);
49358f0484fSRodney W. Grimes 			break;
49458f0484fSRodney W. Grimes 		}
49558f0484fSRodney W. Grimes 	}
49658f0484fSRodney W. Grimes 	*bufnext = EOS;
49758f0484fSRodney W. Grimes #ifdef DEBUG
49858f0484fSRodney W. Grimes 	qprintf("glob0:", patbuf);
49958f0484fSRodney W. Grimes #endif
50058f0484fSRodney W. Grimes 
501bae8632fSJonathan Lemon 	if ((err = glob1(patbuf, pglob, limit)) != 0)
50258f0484fSRodney W. Grimes 		return(err);
50358f0484fSRodney W. Grimes 
50458f0484fSRodney W. Grimes 	/*
50558f0484fSRodney W. Grimes 	 * If there was no match we are going to append the pattern
50658f0484fSRodney W. Grimes 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
50758f0484fSRodney W. Grimes 	 * and the pattern did not contain any magic characters
50858f0484fSRodney W. Grimes 	 * GLOB_NOMAGIC is there just for compatibility with csh.
50958f0484fSRodney W. Grimes 	 */
5104a59c3abSMike Heffner 	if (pglob->gl_pathc == oldpathc) {
5114a59c3abSMike Heffner 		if (((pglob->gl_flags & GLOB_NOCHECK) ||
51258f0484fSRodney W. Grimes 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
51358f0484fSRodney W. Grimes 			!(pglob->gl_flags & GLOB_MAGCHAR))))
514bae8632fSJonathan Lemon 			return(globextend(pattern, pglob, limit));
5154a59c3abSMike Heffner 		else
5164a59c3abSMike Heffner 			return(GLOB_NOMATCH);
5174a59c3abSMike Heffner 	}
5184a59c3abSMike Heffner 	if (!(pglob->gl_flags & GLOB_NOSORT))
51958f0484fSRodney W. Grimes 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
52058f0484fSRodney W. Grimes 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
52158f0484fSRodney W. Grimes 	return(0);
52258f0484fSRodney W. Grimes }
52358f0484fSRodney W. Grimes 
52458f0484fSRodney W. Grimes static int
5251cec70adSXin LI compare(const void *p, const void *q)
52658f0484fSRodney W. Grimes {
52758f0484fSRodney W. Grimes 	return(strcmp(*(char **)p, *(char **)q));
52858f0484fSRodney W. Grimes }
52958f0484fSRodney W. Grimes 
53058f0484fSRodney W. Grimes static int
5311cec70adSXin LI glob1(Char *pattern, glob_t *pglob, size_t *limit)
53258f0484fSRodney W. Grimes {
533487dbd92SPeter Wemm 	Char pathbuf[MAXPATHLEN];
53458f0484fSRodney W. Grimes 
53558f0484fSRodney W. Grimes 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
53658f0484fSRodney W. Grimes 	if (*pattern == EOS)
53758f0484fSRodney W. Grimes 		return(0);
538487dbd92SPeter Wemm 	return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
539487dbd92SPeter Wemm 	    pattern, pglob, limit));
54058f0484fSRodney W. Grimes }
54158f0484fSRodney W. Grimes 
54258f0484fSRodney W. Grimes /*
54358f0484fSRodney W. Grimes  * The functions glob2 and glob3 are mutually recursive; there is one level
54458f0484fSRodney W. Grimes  * of recursion for each segment in the pattern that contains one or more
54558f0484fSRodney W. Grimes  * meta characters.
54658f0484fSRodney W. Grimes  */
54758f0484fSRodney W. Grimes static int
5481cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
5491cec70adSXin LI       glob_t *pglob, size_t *limit)
55058f0484fSRodney W. Grimes {
55158f0484fSRodney W. Grimes 	struct stat sb;
55258f0484fSRodney W. Grimes 	Char *p, *q;
55358f0484fSRodney W. Grimes 	int anymeta;
55458f0484fSRodney W. Grimes 
55558f0484fSRodney W. Grimes 	/*
55658f0484fSRodney W. Grimes 	 * Loop over pattern segments until end of pattern or until
55758f0484fSRodney W. Grimes 	 * segment with meta character found.
55858f0484fSRodney W. Grimes 	 */
55958f0484fSRodney W. Grimes 	for (anymeta = 0;;) {
56058f0484fSRodney W. Grimes 		if (*pattern == EOS) {		/* End of pattern? */
56158f0484fSRodney W. Grimes 			*pathend = EOS;
56258f0484fSRodney W. Grimes 			if (g_lstat(pathbuf, &sb, pglob))
56358f0484fSRodney W. Grimes 				return(0);
56458f0484fSRodney W. Grimes 
56558f0484fSRodney W. Grimes 			if (((pglob->gl_flags & GLOB_MARK) &&
56658f0484fSRodney W. Grimes 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
56758f0484fSRodney W. Grimes 			    || (S_ISLNK(sb.st_mode) &&
56858f0484fSRodney W. Grimes 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
56958f0484fSRodney W. Grimes 			    S_ISDIR(sb.st_mode)))) {
570487dbd92SPeter Wemm 				if (pathend + 1 > pathend_last)
5714a59c3abSMike Heffner 					return (GLOB_ABORTED);
57258f0484fSRodney W. Grimes 				*pathend++ = SEP;
57358f0484fSRodney W. Grimes 				*pathend = EOS;
57458f0484fSRodney W. Grimes 			}
57558f0484fSRodney W. Grimes 			++pglob->gl_matchc;
576bae8632fSJonathan Lemon 			return(globextend(pathbuf, pglob, limit));
57758f0484fSRodney W. Grimes 		}
57858f0484fSRodney W. Grimes 
57958f0484fSRodney W. Grimes 		/* Find end of next segment, copy tentatively to pathend. */
58058f0484fSRodney W. Grimes 		q = pathend;
58158f0484fSRodney W. Grimes 		p = pattern;
58258f0484fSRodney W. Grimes 		while (*p != EOS && *p != SEP) {
58358f0484fSRodney W. Grimes 			if (ismeta(*p))
58458f0484fSRodney W. Grimes 				anymeta = 1;
585487dbd92SPeter Wemm 			if (q + 1 > pathend_last)
5864a59c3abSMike Heffner 				return (GLOB_ABORTED);
58758f0484fSRodney W. Grimes 			*q++ = *p++;
58858f0484fSRodney W. Grimes 		}
58958f0484fSRodney W. Grimes 
59058f0484fSRodney W. Grimes 		if (!anymeta) {		/* No expansion, do next segment. */
59158f0484fSRodney W. Grimes 			pathend = q;
59258f0484fSRodney W. Grimes 			pattern = p;
593487dbd92SPeter Wemm 			while (*pattern == SEP) {
594487dbd92SPeter Wemm 				if (pathend + 1 > pathend_last)
5954a59c3abSMike Heffner 					return (GLOB_ABORTED);
59658f0484fSRodney W. Grimes 				*pathend++ = *pattern++;
597487dbd92SPeter Wemm 			}
59858f0484fSRodney W. Grimes 		} else			/* Need expansion, recurse. */
599487dbd92SPeter Wemm 			return(glob3(pathbuf, pathend, pathend_last, pattern, p,
600487dbd92SPeter Wemm 			    pglob, limit));
60158f0484fSRodney W. Grimes 	}
60258f0484fSRodney W. Grimes 	/* NOTREACHED */
60358f0484fSRodney W. Grimes }
60458f0484fSRodney W. Grimes 
60558f0484fSRodney W. Grimes static int
6061cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
6071cec70adSXin LI       Char *pattern, Char *restpattern,
6081cec70adSXin LI       glob_t *pglob, size_t *limit)
60958f0484fSRodney W. Grimes {
610b231cb39SDavid E. O'Brien 	struct dirent *dp;
61158f0484fSRodney W. Grimes 	DIR *dirp;
61258f0484fSRodney W. Grimes 	int err;
61358f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
61458f0484fSRodney W. Grimes 
61558f0484fSRodney W. Grimes 	/*
61658f0484fSRodney W. Grimes 	 * The readdirfunc declaration can't be prototyped, because it is
61758f0484fSRodney W. Grimes 	 * assigned, below, to two functions which are prototyped in glob.h
61858f0484fSRodney W. Grimes 	 * and dirent.h as taking pointers to differently typed opaque
61958f0484fSRodney W. Grimes 	 * structures.
62058f0484fSRodney W. Grimes 	 */
62158f0484fSRodney W. Grimes 	struct dirent *(*readdirfunc)();
62258f0484fSRodney W. Grimes 
623487dbd92SPeter Wemm 	if (pathend > pathend_last)
6244a59c3abSMike Heffner 		return (GLOB_ABORTED);
62558f0484fSRodney W. Grimes 	*pathend = EOS;
62658f0484fSRodney W. Grimes 	errno = 0;
62758f0484fSRodney W. Grimes 
62858f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
62958f0484fSRodney W. Grimes 		/* TODO: don't call for ENOENT or ENOTDIR? */
63058f0484fSRodney W. Grimes 		if (pglob->gl_errfunc) {
63127d52f69SPeter Wemm 			if (g_Ctoc(pathbuf, buf, sizeof(buf)))
6324a59c3abSMike Heffner 				return (GLOB_ABORTED);
63358f0484fSRodney W. Grimes 			if (pglob->gl_errfunc(buf, errno) ||
63458f0484fSRodney W. Grimes 			    pglob->gl_flags & GLOB_ERR)
6354a59c3abSMike Heffner 				return (GLOB_ABORTED);
63658f0484fSRodney W. Grimes 		}
63758f0484fSRodney W. Grimes 		return(0);
63858f0484fSRodney W. Grimes 	}
63958f0484fSRodney W. Grimes 
64058f0484fSRodney W. Grimes 	err = 0;
64158f0484fSRodney W. Grimes 
64258f0484fSRodney W. Grimes 	/* Search directory for matching names. */
64358f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
64458f0484fSRodney W. Grimes 		readdirfunc = pglob->gl_readdir;
64558f0484fSRodney W. Grimes 	else
64658f0484fSRodney W. Grimes 		readdirfunc = readdir;
64758f0484fSRodney W. Grimes 	while ((dp = (*readdirfunc)(dirp))) {
6481cec70adSXin LI 		char *sc;
649b231cb39SDavid E. O'Brien 		Char *dc;
650e9346e01STim J. Robbins 		wchar_t wc;
651e9346e01STim J. Robbins 		size_t clen;
652e9346e01STim J. Robbins 		mbstate_t mbs;
65358f0484fSRodney W. Grimes 
65458f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
65558f0484fSRodney W. Grimes 		if (dp->d_name[0] == DOT && *pattern != DOT)
65658f0484fSRodney W. Grimes 			continue;
657e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
658487dbd92SPeter Wemm 		dc = pathend;
6591cec70adSXin LI 		sc = dp->d_name;
660e9346e01STim J. Robbins 		while (dc < pathend_last) {
661e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
662e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
663e9346e01STim J. Robbins 				wc = *sc;
664e9346e01STim J. Robbins 				clen = 1;
665e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
666e9346e01STim J. Robbins 			}
667e9346e01STim J. Robbins 			if ((*dc++ = wc) == EOS)
668e9346e01STim J. Robbins 				break;
669e9346e01STim J. Robbins 			sc += clen;
670e9346e01STim J. Robbins 		}
67158f0484fSRodney W. Grimes 		if (!match(pathend, pattern, restpattern)) {
67258f0484fSRodney W. Grimes 			*pathend = EOS;
67358f0484fSRodney W. Grimes 			continue;
67458f0484fSRodney W. Grimes 		}
675487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
676487dbd92SPeter Wemm 		    pglob, limit);
67758f0484fSRodney W. Grimes 		if (err)
67858f0484fSRodney W. Grimes 			break;
67958f0484fSRodney W. Grimes 	}
68058f0484fSRodney W. Grimes 
68158f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
68258f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
68358f0484fSRodney W. Grimes 	else
68458f0484fSRodney W. Grimes 		closedir(dirp);
68558f0484fSRodney W. Grimes 	return(err);
68658f0484fSRodney W. Grimes }
68758f0484fSRodney W. Grimes 
68858f0484fSRodney W. Grimes 
68958f0484fSRodney W. Grimes /*
69058f0484fSRodney W. Grimes  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
69158f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
69258f0484fSRodney W. Grimes  *
69358f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
69458f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
69558f0484fSRodney W. Grimes  * behavior.
69658f0484fSRodney W. Grimes  *
69758f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
69858f0484fSRodney W. Grimes  *
69958f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
70058f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
70158f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
70258f0484fSRodney W. Grimes  */
70358f0484fSRodney W. Grimes static int
7041cec70adSXin LI globextend(const Char *path, glob_t *pglob, size_t *limit)
70558f0484fSRodney W. Grimes {
706b231cb39SDavid E. O'Brien 	char **pathv;
7074b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
70858f0484fSRodney W. Grimes 	char *copy;
70958f0484fSRodney W. Grimes 	const Char *p;
71058f0484fSRodney W. Grimes 
71175dc5f1aSMike Heffner 	if (*limit && pglob->gl_pathc > *limit) {
71275dc5f1aSMike Heffner 		errno = 0;
71375dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
71475dc5f1aSMike Heffner 	}
715813c96dbSJonathan Lemon 
71658f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
71758f0484fSRodney W. Grimes 	pathv = pglob->gl_pathv ?
71858f0484fSRodney W. Grimes 		    realloc((char *)pglob->gl_pathv, newsize) :
71958f0484fSRodney W. Grimes 		    malloc(newsize);
72076e2bc01SPeter Wemm 	if (pathv == NULL) {
72176e2bc01SPeter Wemm 		if (pglob->gl_pathv) {
72276e2bc01SPeter Wemm 			free(pglob->gl_pathv);
72376e2bc01SPeter Wemm 			pglob->gl_pathv = NULL;
72476e2bc01SPeter Wemm 		}
72558f0484fSRodney W. Grimes 		return(GLOB_NOSPACE);
72676e2bc01SPeter Wemm 	}
72758f0484fSRodney W. Grimes 
72858f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
72958f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
73058f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
7314b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
73258f0484fSRodney W. Grimes 			*--pathv = NULL;
73358f0484fSRodney W. Grimes 	}
73458f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
73558f0484fSRodney W. Grimes 
73658f0484fSRodney W. Grimes 	for (p = path; *p++;)
73758f0484fSRodney W. Grimes 		continue;
738e9346e01STim J. Robbins 	len = MB_CUR_MAX * (size_t)(p - path);	/* XXX overallocation */
73927d52f69SPeter Wemm 	if ((copy = malloc(len)) != NULL) {
74027d52f69SPeter Wemm 		if (g_Ctoc(path, copy, len)) {
74176e2bc01SPeter Wemm 			free(copy);
74276e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
74376e2bc01SPeter Wemm 		}
74458f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
74558f0484fSRodney W. Grimes 	}
74658f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
74758f0484fSRodney W. Grimes 	return(copy == NULL ? GLOB_NOSPACE : 0);
74858f0484fSRodney W. Grimes }
74958f0484fSRodney W. Grimes 
75058f0484fSRodney W. Grimes /*
75158f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
75258f0484fSRodney W. Grimes  * pattern causes a recursion level.
75358f0484fSRodney W. Grimes  */
75458f0484fSRodney W. Grimes static int
7551cec70adSXin LI match(Char *name, Char *pat, Char *patend)
75658f0484fSRodney W. Grimes {
75758f0484fSRodney W. Grimes 	int ok, negate_range;
75858f0484fSRodney W. Grimes 	Char c, k;
759*3c87aa1dSDavid Chisnall 	struct xlocale_collate *table =
760*3c87aa1dSDavid Chisnall 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
76158f0484fSRodney W. Grimes 
76258f0484fSRodney W. Grimes 	while (pat < patend) {
76358f0484fSRodney W. Grimes 		c = *pat++;
76458f0484fSRodney W. Grimes 		switch (c & M_MASK) {
76558f0484fSRodney W. Grimes 		case M_ALL:
76658f0484fSRodney W. Grimes 			if (pat == patend)
76758f0484fSRodney W. Grimes 				return(1);
76858f0484fSRodney W. Grimes 			do
76958f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
77058f0484fSRodney W. Grimes 				    return(1);
77158f0484fSRodney W. Grimes 			while (*name++ != EOS);
77258f0484fSRodney W. Grimes 			return(0);
77358f0484fSRodney W. Grimes 		case M_ONE:
77458f0484fSRodney W. Grimes 			if (*name++ == EOS)
77558f0484fSRodney W. Grimes 				return(0);
77658f0484fSRodney W. Grimes 			break;
77758f0484fSRodney W. Grimes 		case M_SET:
77858f0484fSRodney W. Grimes 			ok = 0;
77958f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
78058f0484fSRodney W. Grimes 				return(0);
78158f0484fSRodney W. Grimes 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
78258f0484fSRodney W. Grimes 				++pat;
78358f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
78458f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
785*3c87aa1dSDavid Chisnall 					if (table->__collate_load_error ?
78621d58869SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
787*3c87aa1dSDavid Chisnall 					       __collate_range_cmp(table, CHAR(c), CHAR(k)) <= 0
788*3c87aa1dSDavid Chisnall 					    && __collate_range_cmp(table, CHAR(k), CHAR(pat[1])) <= 0
789b92a8919SAndrey A. Chernov 					   )
79058f0484fSRodney W. Grimes 						ok = 1;
79158f0484fSRodney W. Grimes 					pat += 2;
79258f0484fSRodney W. Grimes 				} else if (c == k)
79358f0484fSRodney W. Grimes 					ok = 1;
79458f0484fSRodney W. Grimes 			if (ok == negate_range)
79558f0484fSRodney W. Grimes 				return(0);
79658f0484fSRodney W. Grimes 			break;
79758f0484fSRodney W. Grimes 		default:
79858f0484fSRodney W. Grimes 			if (*name++ != c)
79958f0484fSRodney W. Grimes 				return(0);
80058f0484fSRodney W. Grimes 			break;
80158f0484fSRodney W. Grimes 		}
80258f0484fSRodney W. Grimes 	}
80358f0484fSRodney W. Grimes 	return(*name == EOS);
80458f0484fSRodney W. Grimes }
80558f0484fSRodney W. Grimes 
80658f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
80758f0484fSRodney W. Grimes void
8081cec70adSXin LI globfree(glob_t *pglob)
80958f0484fSRodney W. Grimes {
8104b767fa6SAndrey A. Chernov 	size_t i;
811b231cb39SDavid E. O'Brien 	char **pp;
81258f0484fSRodney W. Grimes 
81358f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
81458f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
81558f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
81658f0484fSRodney W. Grimes 			if (*pp)
81758f0484fSRodney W. Grimes 				free(*pp);
81858f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
81976e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
82058f0484fSRodney W. Grimes 	}
82158f0484fSRodney W. Grimes }
82258f0484fSRodney W. Grimes 
82358f0484fSRodney W. Grimes static DIR *
8241cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
82558f0484fSRodney W. Grimes {
82658f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
82758f0484fSRodney W. Grimes 
82858f0484fSRodney W. Grimes 	if (!*str)
82958f0484fSRodney W. Grimes 		strcpy(buf, ".");
83076e2bc01SPeter Wemm 	else {
83127d52f69SPeter Wemm 		if (g_Ctoc(str, buf, sizeof(buf)))
83276e2bc01SPeter Wemm 			return (NULL);
83376e2bc01SPeter Wemm 	}
83458f0484fSRodney W. Grimes 
83558f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
83658f0484fSRodney W. Grimes 		return((*pglob->gl_opendir)(buf));
83758f0484fSRodney W. Grimes 
83858f0484fSRodney W. Grimes 	return(opendir(buf));
83958f0484fSRodney W. Grimes }
84058f0484fSRodney W. Grimes 
84158f0484fSRodney W. Grimes static int
8421cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
84358f0484fSRodney W. Grimes {
84458f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
84558f0484fSRodney W. Grimes 
84627d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
84776e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
84876e2bc01SPeter Wemm 		return (-1);
84976e2bc01SPeter Wemm 	}
85058f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
85158f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
85258f0484fSRodney W. Grimes 	return(lstat(buf, sb));
85358f0484fSRodney W. Grimes }
85458f0484fSRodney W. Grimes 
85558f0484fSRodney W. Grimes static int
8561cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
85758f0484fSRodney W. Grimes {
85858f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
85958f0484fSRodney W. Grimes 
86027d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
86176e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
86276e2bc01SPeter Wemm 		return (-1);
86376e2bc01SPeter Wemm 	}
86458f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
86558f0484fSRodney W. Grimes 		return((*pglob->gl_stat)(buf, sb));
86658f0484fSRodney W. Grimes 	return(stat(buf, sb));
86758f0484fSRodney W. Grimes }
86858f0484fSRodney W. Grimes 
86934a08754SMike Makonnen static const Char *
87034a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
87158f0484fSRodney W. Grimes {
8721cec70adSXin LI 
87358f0484fSRodney W. Grimes 	do {
87458f0484fSRodney W. Grimes 		if (*str == ch)
87558f0484fSRodney W. Grimes 			return (str);
87658f0484fSRodney W. Grimes 	} while (*str++);
87758f0484fSRodney W. Grimes 	return (NULL);
87858f0484fSRodney W. Grimes }
87958f0484fSRodney W. Grimes 
88076e2bc01SPeter Wemm static int
8811cec70adSXin LI g_Ctoc(const Char *str, char *buf, size_t len)
88258f0484fSRodney W. Grimes {
883e9346e01STim J. Robbins 	mbstate_t mbs;
884e9346e01STim J. Robbins 	size_t clen;
88558f0484fSRodney W. Grimes 
886e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
887e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
888e9346e01STim J. Robbins 		clen = wcrtomb(buf, *str, &mbs);
889e9346e01STim J. Robbins 		if (clen == (size_t)-1)
890e9346e01STim J. Robbins 			return (1);
891e9346e01STim J. Robbins 		if (*str == L'\0')
89276e2bc01SPeter Wemm 			return (0);
893e9346e01STim J. Robbins 		str++;
894e9346e01STim J. Robbins 		buf += clen;
895e9346e01STim J. Robbins 		len -= clen;
89658f0484fSRodney W. Grimes 	}
89727d52f69SPeter Wemm 	return (1);
89827d52f69SPeter Wemm }
89958f0484fSRodney W. Grimes 
90058f0484fSRodney W. Grimes #ifdef DEBUG
90158f0484fSRodney W. Grimes static void
9021cec70adSXin LI qprintf(const char *str, Char *s)
90358f0484fSRodney W. Grimes {
904b231cb39SDavid E. O'Brien 	Char *p;
90558f0484fSRodney W. Grimes 
90658f0484fSRodney W. Grimes 	(void)printf("%s:\n", str);
90758f0484fSRodney W. Grimes 	for (p = s; *p; p++)
90858f0484fSRodney W. Grimes 		(void)printf("%c", CHAR(*p));
90958f0484fSRodney W. Grimes 	(void)printf("\n");
91058f0484fSRodney W. Grimes 	for (p = s; *p; p++)
91158f0484fSRodney W. Grimes 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
91258f0484fSRodney W. Grimes 	(void)printf("\n");
91358f0484fSRodney W. Grimes 	for (p = s; *p; p++)
91458f0484fSRodney W. Grimes 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
91558f0484fSRodney W. Grimes 	(void)printf("\n");
91658f0484fSRodney W. Grimes }
91758f0484fSRodney W. Grimes #endif
918