xref: /freebsd/lib/libc/gen/glob.c (revision 4b767fa67f1464207f106e0906b84fef42db3a00)
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  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
40b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
41b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes /*
4458f0484fSRodney W. Grimes  * glob(3) -- a superset of the one defined in POSIX 1003.2.
4558f0484fSRodney W. Grimes  *
4658f0484fSRodney W. Grimes  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
4758f0484fSRodney W. Grimes  *
4858f0484fSRodney W. Grimes  * Optional extra services, controlled by flags not defined by POSIX:
4958f0484fSRodney W. Grimes  *
5058f0484fSRodney W. Grimes  * GLOB_QUOTE:
5158f0484fSRodney W. Grimes  *	Escaping convention: \ inhibits any special meaning the following
5258f0484fSRodney W. Grimes  *	character might have (except \ at end of string is retained).
5358f0484fSRodney W. Grimes  * GLOB_MAGCHAR:
5458f0484fSRodney W. Grimes  *	Set in gl_flags if pattern contained a globbing character.
5558f0484fSRodney W. Grimes  * GLOB_NOMAGIC:
5658f0484fSRodney W. Grimes  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
5758f0484fSRodney W. Grimes  *	not contain any magic characters.  [Used in csh style globbing]
5858f0484fSRodney W. Grimes  * GLOB_ALTDIRFUNC:
5958f0484fSRodney W. Grimes  *	Use alternately specified directory access functions.
6058f0484fSRodney W. Grimes  * GLOB_TILDE:
6158f0484fSRodney W. Grimes  *	expand ~user/foo to the /home/dir/of/user/foo
6258f0484fSRodney W. Grimes  * GLOB_BRACE:
6358f0484fSRodney W. Grimes  *	expand {1,2}{a,b} to 1a 1b 2a 2b
6458f0484fSRodney W. Grimes  * gl_matchc:
6558f0484fSRodney W. Grimes  *	Number of matches in the current invocation of glob.
6658f0484fSRodney W. Grimes  */
6758f0484fSRodney W. Grimes 
68e9346e01STim J. Robbins /*
69e9346e01STim J. Robbins  * Some notes on multibyte character support:
70e9346e01STim J. Robbins  * 1. Patterns with illegal byte sequences match nothing - even if
71e9346e01STim J. Robbins  *    GLOB_NOCHECK is specified.
72e9346e01STim J. Robbins  * 2. Illegal byte sequences in filenames are handled by treating them as
73e9346e01STim J. Robbins  *    single-byte characters with a value of the first byte of the sequence
74e9346e01STim J. Robbins  *    cast to wchar_t.
75e9346e01STim J. Robbins  * 3. State-dependent encodings are not currently supported.
76e9346e01STim J. Robbins  */
77e9346e01STim J. Robbins 
7858f0484fSRodney W. Grimes #include <sys/param.h>
7958f0484fSRodney W. Grimes #include <sys/stat.h>
8058f0484fSRodney W. Grimes 
8158f0484fSRodney W. Grimes #include <ctype.h>
8258f0484fSRodney W. Grimes #include <dirent.h>
8358f0484fSRodney W. Grimes #include <errno.h>
8458f0484fSRodney W. Grimes #include <glob.h>
85e9346e01STim J. Robbins #include <limits.h>
8658f0484fSRodney W. Grimes #include <pwd.h>
87e9346e01STim J. Robbins #include <stdint.h>
8858f0484fSRodney W. Grimes #include <stdio.h>
8958f0484fSRodney W. Grimes #include <stdlib.h>
9058f0484fSRodney W. Grimes #include <string.h>
9158f0484fSRodney W. Grimes #include <unistd.h>
92e9346e01STim J. Robbins #include <wchar.h>
9358f0484fSRodney W. Grimes 
94edcfa072SAndrey A. Chernov #include "collate.h"
95edcfa072SAndrey A. Chernov 
9658f0484fSRodney W. Grimes #define	DOLLAR		'$'
9758f0484fSRodney W. Grimes #define	DOT		'.'
9858f0484fSRodney W. Grimes #define	EOS		'\0'
9958f0484fSRodney W. Grimes #define	LBRACKET	'['
10058f0484fSRodney W. Grimes #define	NOT		'!'
10158f0484fSRodney W. Grimes #define	QUESTION	'?'
10258f0484fSRodney W. Grimes #define	QUOTE		'\\'
10358f0484fSRodney W. Grimes #define	RANGE		'-'
10458f0484fSRodney W. Grimes #define	RBRACKET	']'
10558f0484fSRodney W. Grimes #define	SEP		'/'
10658f0484fSRodney W. Grimes #define	STAR		'*'
10758f0484fSRodney W. Grimes #define	TILDE		'~'
10858f0484fSRodney W. Grimes #define	UNDERSCORE	'_'
10958f0484fSRodney W. Grimes #define	LBRACE		'{'
11058f0484fSRodney W. Grimes #define	RBRACE		'}'
11158f0484fSRodney W. Grimes #define	SLASH		'/'
11258f0484fSRodney W. Grimes #define	COMMA		','
11358f0484fSRodney W. Grimes 
11458f0484fSRodney W. Grimes #ifndef DEBUG
11558f0484fSRodney W. Grimes 
116e9346e01STim J. Robbins #define	M_QUOTE		0x8000000000ULL
117e9346e01STim J. Robbins #define	M_PROTECT	0x4000000000ULL
118e9346e01STim J. Robbins #define	M_MASK		0xffffffffffULL
119e9346e01STim J. Robbins #define	M_CHAR		0x00ffffffffULL
12058f0484fSRodney W. Grimes 
121e9346e01STim J. Robbins typedef uint_fast64_t Char;
12258f0484fSRodney W. Grimes 
12358f0484fSRodney W. Grimes #else
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes #define	M_QUOTE		0x80
12658f0484fSRodney W. Grimes #define	M_PROTECT	0x40
12758f0484fSRodney W. Grimes #define	M_MASK		0xff
128e9346e01STim J. Robbins #define	M_CHAR		0x7f
12958f0484fSRodney W. Grimes 
13058f0484fSRodney W. Grimes typedef char Char;
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes #endif
13358f0484fSRodney W. Grimes 
13458f0484fSRodney W. Grimes 
135e9346e01STim J. Robbins #define	CHAR(c)		((Char)((c)&M_CHAR))
13658f0484fSRodney W. Grimes #define	META(c)		((Char)((c)|M_QUOTE))
13758f0484fSRodney W. Grimes #define	M_ALL		META('*')
13858f0484fSRodney W. Grimes #define	M_END		META(']')
13958f0484fSRodney W. Grimes #define	M_NOT		META('!')
14058f0484fSRodney W. Grimes #define	M_ONE		META('?')
14158f0484fSRodney W. Grimes #define	M_RNG		META('-')
14258f0484fSRodney W. Grimes #define	M_SET		META('[')
14358f0484fSRodney W. Grimes #define	ismeta(c)	(((c)&M_QUOTE) != 0)
14458f0484fSRodney W. Grimes 
14558f0484fSRodney W. Grimes 
146b231cb39SDavid E. O'Brien static int	 compare(const void *, const void *);
1474b767fa6SAndrey A. Chernov static int	 g_Ctoc(const Char *, char *, size_t);
148b231cb39SDavid E. O'Brien static int	 g_lstat(Char *, struct stat *, glob_t *);
149b231cb39SDavid E. O'Brien static DIR	*g_opendir(Char *, glob_t *);
150e9346e01STim J. Robbins static Char	*g_strchr(Char *, wchar_t);
15158f0484fSRodney W. Grimes #ifdef notdef
152b231cb39SDavid E. O'Brien static Char	*g_strcat(Char *, const Char *);
15358f0484fSRodney W. Grimes #endif
154b231cb39SDavid E. O'Brien static int	 g_stat(Char *, struct stat *, glob_t *);
1554b767fa6SAndrey A. Chernov static int	 glob0(const Char *, glob_t *, size_t *);
1564b767fa6SAndrey A. Chernov static int	 glob1(Char *, glob_t *, size_t *);
1574b767fa6SAndrey A. Chernov static int	 glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
1584b767fa6SAndrey A. Chernov static int	 glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
1594b767fa6SAndrey A. Chernov static int	 globextend(const Char *, glob_t *, size_t *);
160487dbd92SPeter Wemm static const Char *
161b231cb39SDavid E. O'Brien 		 globtilde(const Char *, Char *, size_t, glob_t *);
1624b767fa6SAndrey A. Chernov static int	 globexp1(const Char *, glob_t *, size_t *);
1634b767fa6SAndrey A. Chernov static int	 globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
164b231cb39SDavid E. O'Brien static int	 match(Char *, Char *, Char *);
16558f0484fSRodney W. Grimes #ifdef DEBUG
166b231cb39SDavid E. O'Brien static void	 qprintf(const char *, Char *);
16758f0484fSRodney W. Grimes #endif
16858f0484fSRodney W. Grimes 
16958f0484fSRodney W. Grimes int
17058f0484fSRodney W. Grimes glob(pattern, flags, errfunc, pglob)
17158f0484fSRodney W. Grimes 	const char *pattern;
172b231cb39SDavid E. O'Brien 	int flags, (*errfunc)(const char *, int);
17358f0484fSRodney W. Grimes 	glob_t *pglob;
17458f0484fSRodney W. Grimes {
17558f0484fSRodney W. Grimes 	const u_char *patnext;
1764b767fa6SAndrey A. Chernov 	size_t limit;
177e9346e01STim J. Robbins 	Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
178e9346e01STim J. Robbins 	mbstate_t mbs;
179e9346e01STim J. Robbins 	wchar_t wc;
180e9346e01STim J. Robbins 	size_t clen;
18158f0484fSRodney W. Grimes 
18258f0484fSRodney W. Grimes 	patnext = (u_char *) pattern;
18358f0484fSRodney W. Grimes 	if (!(flags & GLOB_APPEND)) {
18458f0484fSRodney W. Grimes 		pglob->gl_pathc = 0;
18558f0484fSRodney W. Grimes 		pglob->gl_pathv = NULL;
18658f0484fSRodney W. Grimes 		if (!(flags & GLOB_DOOFFS))
18758f0484fSRodney W. Grimes 			pglob->gl_offs = 0;
18858f0484fSRodney W. Grimes 	}
18975dc5f1aSMike Heffner 	if (flags & GLOB_LIMIT) {
190bae8632fSJonathan Lemon 		limit = pglob->gl_matchc;
19175dc5f1aSMike Heffner 		if (limit == 0)
19275dc5f1aSMike Heffner 			limit = ARG_MAX;
19375dc5f1aSMike Heffner 	} else
194bae8632fSJonathan Lemon 		limit = 0;
19558f0484fSRodney W. Grimes 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
19658f0484fSRodney W. Grimes 	pglob->gl_errfunc = errfunc;
19758f0484fSRodney W. Grimes 	pglob->gl_matchc = 0;
19858f0484fSRodney W. Grimes 
19958f0484fSRodney W. Grimes 	bufnext = patbuf;
200487dbd92SPeter Wemm 	bufend = bufnext + MAXPATHLEN - 1;
201e9346e01STim J. Robbins 	if (flags & GLOB_NOESCAPE) {
202e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
203e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
204e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
205e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
206e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
207e9346e01STim J. Robbins 			else if (clen == 0)
208e9346e01STim J. Robbins 				break;
209e9346e01STim J. Robbins 			*bufnext++ = wc;
210e9346e01STim J. Robbins 			patnext += clen;
211e9346e01STim J. Robbins 		}
212e9346e01STim J. Robbins 	} else {
21358f0484fSRodney W. Grimes 		/* Protect the quoted characters. */
214e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
215e9346e01STim J. Robbins 		while (bufend - bufnext >= MB_CUR_MAX) {
216e9346e01STim J. Robbins 			if (*patnext == QUOTE) {
217e9346e01STim J. Robbins 				if (*++patnext == EOS) {
218e9346e01STim J. Robbins 					*bufnext++ = QUOTE | M_PROTECT;
219e9346e01STim J. Robbins 					continue;
22058f0484fSRodney W. Grimes 				}
221e9346e01STim J. Robbins 				prot = M_PROTECT;
222e9346e01STim J. Robbins 			} else
223e9346e01STim J. Robbins 				prot = 0;
224e9346e01STim J. Robbins 			clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
225e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
226e9346e01STim J. Robbins 				return (GLOB_NOMATCH);
227e9346e01STim J. Robbins 			else if (clen == 0)
228e9346e01STim J. Robbins 				break;
229e9346e01STim J. Robbins 			*bufnext++ = wc | prot;
230e9346e01STim J. Robbins 			patnext += clen;
23158f0484fSRodney W. Grimes 		}
23258f0484fSRodney W. Grimes 	}
23358f0484fSRodney W. Grimes 	*bufnext = EOS;
23458f0484fSRodney W. Grimes 
23558f0484fSRodney W. Grimes 	if (flags & GLOB_BRACE)
236bae8632fSJonathan Lemon 	    return globexp1(patbuf, pglob, &limit);
23758f0484fSRodney W. Grimes 	else
238bae8632fSJonathan Lemon 	    return glob0(patbuf, pglob, &limit);
23958f0484fSRodney W. Grimes }
24058f0484fSRodney W. Grimes 
24158f0484fSRodney W. Grimes /*
24258f0484fSRodney W. Grimes  * Expand recursively a glob {} pattern. When there is no more expansion
24358f0484fSRodney W. Grimes  * invoke the standard globbing routine to glob the rest of the magic
24458f0484fSRodney W. Grimes  * characters
24558f0484fSRodney W. Grimes  */
246487dbd92SPeter Wemm static int
247487dbd92SPeter Wemm globexp1(pattern, pglob, limit)
24858f0484fSRodney W. Grimes 	const Char *pattern;
24958f0484fSRodney W. Grimes 	glob_t *pglob;
2504b767fa6SAndrey A. Chernov 	size_t *limit;
25158f0484fSRodney W. Grimes {
25258f0484fSRodney W. Grimes 	const Char* ptr = pattern;
25358f0484fSRodney W. Grimes 	int rv;
25458f0484fSRodney W. Grimes 
25558f0484fSRodney W. Grimes 	/* Protect a single {}, for find(1), like csh */
25658f0484fSRodney W. Grimes 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
257bae8632fSJonathan Lemon 		return glob0(pattern, pglob, limit);
25858f0484fSRodney W. Grimes 
25958f0484fSRodney W. Grimes 	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
260bae8632fSJonathan Lemon 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
26158f0484fSRodney W. Grimes 			return rv;
26258f0484fSRodney W. Grimes 
263bae8632fSJonathan Lemon 	return glob0(pattern, pglob, limit);
26458f0484fSRodney W. Grimes }
26558f0484fSRodney W. Grimes 
26658f0484fSRodney W. Grimes 
26758f0484fSRodney W. Grimes /*
26858f0484fSRodney W. Grimes  * Recursive brace globbing helper. Tries to expand a single brace.
26958f0484fSRodney W. Grimes  * If it succeeds then it invokes globexp1 with the new pattern.
27058f0484fSRodney W. Grimes  * If it fails then it tries to glob the rest of the pattern and returns.
27158f0484fSRodney W. Grimes  */
272487dbd92SPeter Wemm static int
273487dbd92SPeter Wemm globexp2(ptr, pattern, pglob, rv, limit)
27458f0484fSRodney W. Grimes 	const Char *ptr, *pattern;
27558f0484fSRodney W. Grimes 	glob_t *pglob;
2764b767fa6SAndrey A. Chernov 	int *rv;
2774b767fa6SAndrey A. Chernov 	size_t *limit;
27858f0484fSRodney W. Grimes {
27958f0484fSRodney W. Grimes 	int     i;
28058f0484fSRodney W. Grimes 	Char   *lm, *ls;
281369316a8SAndrey A. Chernov 	const Char *pe, *pm, *pm1, *pl;
282487dbd92SPeter Wemm 	Char    patbuf[MAXPATHLEN];
28358f0484fSRodney W. Grimes 
28458f0484fSRodney W. Grimes 	/* copy part up to the brace */
28558f0484fSRodney W. Grimes 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
28658f0484fSRodney W. Grimes 		continue;
287487dbd92SPeter Wemm 	*lm = EOS;
28858f0484fSRodney W. Grimes 	ls = lm;
28958f0484fSRodney W. Grimes 
29058f0484fSRodney W. Grimes 	/* Find the balanced brace */
29158f0484fSRodney W. Grimes 	for (i = 0, pe = ++ptr; *pe; pe++)
29258f0484fSRodney W. Grimes 		if (*pe == LBRACKET) {
29358f0484fSRodney W. Grimes 			/* Ignore everything between [] */
29458f0484fSRodney W. Grimes 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
29558f0484fSRodney W. Grimes 				continue;
29658f0484fSRodney W. Grimes 			if (*pe == EOS) {
29758f0484fSRodney W. Grimes 				/*
29858f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
29958f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
30058f0484fSRodney W. Grimes 				 */
30158f0484fSRodney W. Grimes 				pe = pm;
30258f0484fSRodney W. Grimes 			}
30358f0484fSRodney W. Grimes 		}
30458f0484fSRodney W. Grimes 		else if (*pe == LBRACE)
30558f0484fSRodney W. Grimes 			i++;
30658f0484fSRodney W. Grimes 		else if (*pe == RBRACE) {
30758f0484fSRodney W. Grimes 			if (i == 0)
30858f0484fSRodney W. Grimes 				break;
30958f0484fSRodney W. Grimes 			i--;
31058f0484fSRodney W. Grimes 		}
31158f0484fSRodney W. Grimes 
31258f0484fSRodney W. Grimes 	/* Non matching braces; just glob the pattern */
31358f0484fSRodney W. Grimes 	if (i != 0 || *pe == EOS) {
314bae8632fSJonathan Lemon 		*rv = glob0(patbuf, pglob, limit);
31558f0484fSRodney W. Grimes 		return 0;
31658f0484fSRodney W. Grimes 	}
31758f0484fSRodney W. Grimes 
31858f0484fSRodney W. Grimes 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
31958f0484fSRodney W. Grimes 		switch (*pm) {
32058f0484fSRodney W. Grimes 		case LBRACKET:
32158f0484fSRodney W. Grimes 			/* Ignore everything between [] */
322369316a8SAndrey A. Chernov 			for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
32358f0484fSRodney W. Grimes 				continue;
32458f0484fSRodney W. Grimes 			if (*pm == EOS) {
32558f0484fSRodney W. Grimes 				/*
32658f0484fSRodney W. Grimes 				 * We could not find a matching RBRACKET.
32758f0484fSRodney W. Grimes 				 * Ignore and just look for RBRACE
32858f0484fSRodney W. Grimes 				 */
329369316a8SAndrey A. Chernov 				pm = pm1;
33058f0484fSRodney W. Grimes 			}
33158f0484fSRodney W. Grimes 			break;
33258f0484fSRodney W. Grimes 
33358f0484fSRodney W. Grimes 		case LBRACE:
33458f0484fSRodney W. Grimes 			i++;
33558f0484fSRodney W. Grimes 			break;
33658f0484fSRodney W. Grimes 
33758f0484fSRodney W. Grimes 		case RBRACE:
33858f0484fSRodney W. Grimes 			if (i) {
33958f0484fSRodney W. Grimes 			    i--;
34058f0484fSRodney W. Grimes 			    break;
34158f0484fSRodney W. Grimes 			}
34258f0484fSRodney W. Grimes 			/* FALLTHROUGH */
34358f0484fSRodney W. Grimes 		case COMMA:
34458f0484fSRodney W. Grimes 			if (i && *pm == COMMA)
34558f0484fSRodney W. Grimes 				break;
34658f0484fSRodney W. Grimes 			else {
34758f0484fSRodney W. Grimes 				/* Append the current string */
34858f0484fSRodney W. Grimes 				for (lm = ls; (pl < pm); *lm++ = *pl++)
34958f0484fSRodney W. Grimes 					continue;
35058f0484fSRodney W. Grimes 				/*
35158f0484fSRodney W. Grimes 				 * Append the rest of the pattern after the
35258f0484fSRodney W. Grimes 				 * closing brace
35358f0484fSRodney W. Grimes 				 */
35458f0484fSRodney W. Grimes 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
35558f0484fSRodney W. Grimes 					continue;
35658f0484fSRodney W. Grimes 
35758f0484fSRodney W. Grimes 				/* Expand the current pattern */
35858f0484fSRodney W. Grimes #ifdef DEBUG
35958f0484fSRodney W. Grimes 				qprintf("globexp2:", patbuf);
36058f0484fSRodney W. Grimes #endif
361bae8632fSJonathan Lemon 				*rv = globexp1(patbuf, pglob, limit);
36258f0484fSRodney W. Grimes 
36358f0484fSRodney W. Grimes 				/* move after the comma, to the next string */
36458f0484fSRodney W. Grimes 				pl = pm + 1;
36558f0484fSRodney W. Grimes 			}
36658f0484fSRodney W. Grimes 			break;
36758f0484fSRodney W. Grimes 
36858f0484fSRodney W. Grimes 		default:
36958f0484fSRodney W. Grimes 			break;
37058f0484fSRodney W. Grimes 		}
37158f0484fSRodney W. Grimes 	*rv = 0;
37258f0484fSRodney W. Grimes 	return 0;
37358f0484fSRodney W. Grimes }
37458f0484fSRodney W. Grimes 
37558f0484fSRodney W. Grimes 
37658f0484fSRodney W. Grimes 
37758f0484fSRodney W. Grimes /*
37858f0484fSRodney W. Grimes  * expand tilde from the passwd file.
37958f0484fSRodney W. Grimes  */
38058f0484fSRodney W. Grimes static const Char *
38162f187a4SWarner Losh globtilde(pattern, patbuf, patbuf_len, pglob)
38258f0484fSRodney W. Grimes 	const Char *pattern;
38358f0484fSRodney W. Grimes 	Char *patbuf;
38462f187a4SWarner Losh 	size_t patbuf_len;
38558f0484fSRodney W. Grimes 	glob_t *pglob;
38658f0484fSRodney W. Grimes {
38758f0484fSRodney W. Grimes 	struct passwd *pwd;
38858f0484fSRodney W. Grimes 	char *h;
38958f0484fSRodney W. Grimes 	const Char *p;
39062f187a4SWarner Losh 	Char *b, *eb;
39158f0484fSRodney W. Grimes 
39258f0484fSRodney W. Grimes 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
39358f0484fSRodney W. Grimes 		return pattern;
39458f0484fSRodney W. Grimes 
39562f187a4SWarner Losh 	/*
39662f187a4SWarner Losh 	 * Copy up to the end of the string or /
39762f187a4SWarner Losh 	 */
39862f187a4SWarner Losh 	eb = &patbuf[patbuf_len - 1];
39962f187a4SWarner Losh 	for (p = pattern + 1, h = (char *) patbuf;
40062f187a4SWarner Losh 	    h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
40158f0484fSRodney W. Grimes 		continue;
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 	*h = EOS;
40458f0484fSRodney W. Grimes 
40558f0484fSRodney W. Grimes 	if (((char *) patbuf)[0] == EOS) {
40658f0484fSRodney W. Grimes 		/*
4073fa69daeSWarner Losh 		 * handle a plain ~ or ~/ by expanding $HOME first (iff
4083fa69daeSWarner Losh 		 * we're not running setuid or setgid) and then trying
4093fa69daeSWarner Losh 		 * the password file
41058f0484fSRodney W. Grimes 		 */
4114539e95aSTim J. Robbins 		if (issetugid() != 0 ||
4129fcbcd02SJohn Birrell 		    (h = getenv("HOME")) == NULL) {
413eb8eee5aSAndrey A. Chernov 			if (((h = getlogin()) != NULL &&
414eb8eee5aSAndrey A. Chernov 			     (pwd = getpwnam(h)) != NULL) ||
415eb8eee5aSAndrey A. Chernov 			    (pwd = getpwuid(getuid())) != NULL)
41658f0484fSRodney W. Grimes 				h = pwd->pw_dir;
417eb8eee5aSAndrey A. Chernov 			else
418eb8eee5aSAndrey A. Chernov 				return pattern;
41958f0484fSRodney W. Grimes 		}
42058f0484fSRodney W. Grimes 	}
42158f0484fSRodney W. Grimes 	else {
42258f0484fSRodney W. Grimes 		/*
42358f0484fSRodney W. Grimes 		 * Expand a ~user
42458f0484fSRodney W. Grimes 		 */
42558f0484fSRodney W. Grimes 		if ((pwd = getpwnam((char*) patbuf)) == NULL)
42658f0484fSRodney W. Grimes 			return pattern;
42758f0484fSRodney W. Grimes 		else
42858f0484fSRodney W. Grimes 			h = pwd->pw_dir;
42958f0484fSRodney W. Grimes 	}
43058f0484fSRodney W. Grimes 
43158f0484fSRodney W. Grimes 	/* Copy the home directory */
43262f187a4SWarner Losh 	for (b = patbuf; b < eb && *h; *b++ = *h++)
43358f0484fSRodney W. Grimes 		continue;
43458f0484fSRodney W. Grimes 
43558f0484fSRodney W. Grimes 	/* Append the rest of the pattern */
43662f187a4SWarner Losh 	while (b < eb && (*b++ = *p++) != EOS)
43758f0484fSRodney W. Grimes 		continue;
43862f187a4SWarner Losh 	*b = EOS;
43958f0484fSRodney W. Grimes 
44058f0484fSRodney W. Grimes 	return patbuf;
44158f0484fSRodney W. Grimes }
44258f0484fSRodney W. Grimes 
44358f0484fSRodney W. Grimes 
44458f0484fSRodney W. Grimes /*
44558f0484fSRodney W. Grimes  * The main glob() routine: compiles the pattern (optionally processing
44658f0484fSRodney W. Grimes  * quotes), calls glob1() to do the real pattern matching, and finally
44758f0484fSRodney W. Grimes  * sorts the list (unless unsorted operation is requested).  Returns 0
4484a59c3abSMike Heffner  * if things went well, nonzero if errors occurred.
44958f0484fSRodney W. Grimes  */
45058f0484fSRodney W. Grimes static int
451bae8632fSJonathan Lemon glob0(pattern, pglob, limit)
45258f0484fSRodney W. Grimes 	const Char *pattern;
45358f0484fSRodney W. Grimes 	glob_t *pglob;
4544b767fa6SAndrey A. Chernov 	size_t *limit;
45558f0484fSRodney W. Grimes {
45658f0484fSRodney W. Grimes 	const Char *qpatnext;
4574b767fa6SAndrey A. Chernov 	int c, err;
4584b767fa6SAndrey A. Chernov 	size_t oldpathc;
459487dbd92SPeter Wemm 	Char *bufnext, patbuf[MAXPATHLEN];
46058f0484fSRodney W. Grimes 
461487dbd92SPeter Wemm 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
46258f0484fSRodney W. Grimes 	oldpathc = pglob->gl_pathc;
46358f0484fSRodney W. Grimes 	bufnext = patbuf;
46458f0484fSRodney W. Grimes 
46558f0484fSRodney W. Grimes 	/* We don't need to check for buffer overflow any more. */
46658f0484fSRodney W. Grimes 	while ((c = *qpatnext++) != EOS) {
46758f0484fSRodney W. Grimes 		switch (c) {
46858f0484fSRodney W. Grimes 		case LBRACKET:
46958f0484fSRodney W. Grimes 			c = *qpatnext;
47058f0484fSRodney W. Grimes 			if (c == NOT)
47158f0484fSRodney W. Grimes 				++qpatnext;
47258f0484fSRodney W. Grimes 			if (*qpatnext == EOS ||
47358f0484fSRodney W. Grimes 			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
47458f0484fSRodney W. Grimes 				*bufnext++ = LBRACKET;
47558f0484fSRodney W. Grimes 				if (c == NOT)
47658f0484fSRodney W. Grimes 					--qpatnext;
47758f0484fSRodney W. Grimes 				break;
47858f0484fSRodney W. Grimes 			}
47958f0484fSRodney W. Grimes 			*bufnext++ = M_SET;
48058f0484fSRodney W. Grimes 			if (c == NOT)
48158f0484fSRodney W. Grimes 				*bufnext++ = M_NOT;
48258f0484fSRodney W. Grimes 			c = *qpatnext++;
48358f0484fSRodney W. Grimes 			do {
48458f0484fSRodney W. Grimes 				*bufnext++ = CHAR(c);
48558f0484fSRodney W. Grimes 				if (*qpatnext == RANGE &&
48658f0484fSRodney W. Grimes 				    (c = qpatnext[1]) != RBRACKET) {
48758f0484fSRodney W. Grimes 					*bufnext++ = M_RNG;
48858f0484fSRodney W. Grimes 					*bufnext++ = CHAR(c);
48958f0484fSRodney W. Grimes 					qpatnext += 2;
49058f0484fSRodney W. Grimes 				}
49158f0484fSRodney W. Grimes 			} while ((c = *qpatnext++) != RBRACKET);
49258f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
49358f0484fSRodney W. Grimes 			*bufnext++ = M_END;
49458f0484fSRodney W. Grimes 			break;
49558f0484fSRodney W. Grimes 		case QUESTION:
49658f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
49758f0484fSRodney W. Grimes 			*bufnext++ = M_ONE;
49858f0484fSRodney W. Grimes 			break;
49958f0484fSRodney W. Grimes 		case STAR:
50058f0484fSRodney W. Grimes 			pglob->gl_flags |= GLOB_MAGCHAR;
50158f0484fSRodney W. Grimes 			/* collapse adjacent stars to one,
50258f0484fSRodney W. Grimes 			 * to avoid exponential behavior
50358f0484fSRodney W. Grimes 			 */
50458f0484fSRodney W. Grimes 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
50558f0484fSRodney W. Grimes 			    *bufnext++ = M_ALL;
50658f0484fSRodney W. Grimes 			break;
50758f0484fSRodney W. Grimes 		default:
50858f0484fSRodney W. Grimes 			*bufnext++ = CHAR(c);
50958f0484fSRodney W. Grimes 			break;
51058f0484fSRodney W. Grimes 		}
51158f0484fSRodney W. Grimes 	}
51258f0484fSRodney W. Grimes 	*bufnext = EOS;
51358f0484fSRodney W. Grimes #ifdef DEBUG
51458f0484fSRodney W. Grimes 	qprintf("glob0:", patbuf);
51558f0484fSRodney W. Grimes #endif
51658f0484fSRodney W. Grimes 
517bae8632fSJonathan Lemon 	if ((err = glob1(patbuf, pglob, limit)) != 0)
51858f0484fSRodney W. Grimes 		return(err);
51958f0484fSRodney W. Grimes 
52058f0484fSRodney W. Grimes 	/*
52158f0484fSRodney W. Grimes 	 * If there was no match we are going to append the pattern
52258f0484fSRodney W. Grimes 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
52358f0484fSRodney W. Grimes 	 * and the pattern did not contain any magic characters
52458f0484fSRodney W. Grimes 	 * GLOB_NOMAGIC is there just for compatibility with csh.
52558f0484fSRodney W. Grimes 	 */
5264a59c3abSMike Heffner 	if (pglob->gl_pathc == oldpathc) {
5274a59c3abSMike Heffner 		if (((pglob->gl_flags & GLOB_NOCHECK) ||
52858f0484fSRodney W. Grimes 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
52958f0484fSRodney W. Grimes 			!(pglob->gl_flags & GLOB_MAGCHAR))))
530bae8632fSJonathan Lemon 			return(globextend(pattern, pglob, limit));
5314a59c3abSMike Heffner 		else
5324a59c3abSMike Heffner 			return(GLOB_NOMATCH);
5334a59c3abSMike Heffner 	}
5344a59c3abSMike Heffner 	if (!(pglob->gl_flags & GLOB_NOSORT))
53558f0484fSRodney W. Grimes 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
53658f0484fSRodney W. Grimes 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
53758f0484fSRodney W. Grimes 	return(0);
53858f0484fSRodney W. Grimes }
53958f0484fSRodney W. Grimes 
54058f0484fSRodney W. Grimes static int
54158f0484fSRodney W. Grimes compare(p, q)
54258f0484fSRodney W. Grimes 	const void *p, *q;
54358f0484fSRodney W. Grimes {
54458f0484fSRodney W. Grimes 	return(strcmp(*(char **)p, *(char **)q));
54558f0484fSRodney W. Grimes }
54658f0484fSRodney W. Grimes 
54758f0484fSRodney W. Grimes static int
548bae8632fSJonathan Lemon glob1(pattern, pglob, limit)
54958f0484fSRodney W. Grimes 	Char *pattern;
55058f0484fSRodney W. Grimes 	glob_t *pglob;
5514b767fa6SAndrey A. Chernov 	size_t *limit;
55258f0484fSRodney W. Grimes {
553487dbd92SPeter Wemm 	Char pathbuf[MAXPATHLEN];
55458f0484fSRodney W. Grimes 
55558f0484fSRodney W. Grimes 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
55658f0484fSRodney W. Grimes 	if (*pattern == EOS)
55758f0484fSRodney W. Grimes 		return(0);
558487dbd92SPeter Wemm 	return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
559487dbd92SPeter Wemm 	    pattern, pglob, limit));
56058f0484fSRodney W. Grimes }
56158f0484fSRodney W. Grimes 
56258f0484fSRodney W. Grimes /*
56358f0484fSRodney W. Grimes  * The functions glob2 and glob3 are mutually recursive; there is one level
56458f0484fSRodney W. Grimes  * of recursion for each segment in the pattern that contains one or more
56558f0484fSRodney W. Grimes  * meta characters.
56658f0484fSRodney W. Grimes  */
56758f0484fSRodney W. Grimes static int
568487dbd92SPeter Wemm glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit)
569487dbd92SPeter Wemm 	Char *pathbuf, *pathend, *pathend_last, *pattern;
57058f0484fSRodney W. Grimes 	glob_t *pglob;
5714b767fa6SAndrey A. Chernov 	size_t *limit;
57258f0484fSRodney W. Grimes {
57358f0484fSRodney W. Grimes 	struct stat sb;
57458f0484fSRodney W. Grimes 	Char *p, *q;
57558f0484fSRodney W. Grimes 	int anymeta;
57658f0484fSRodney W. Grimes 
57758f0484fSRodney W. Grimes 	/*
57858f0484fSRodney W. Grimes 	 * Loop over pattern segments until end of pattern or until
57958f0484fSRodney W. Grimes 	 * segment with meta character found.
58058f0484fSRodney W. Grimes 	 */
58158f0484fSRodney W. Grimes 	for (anymeta = 0;;) {
58258f0484fSRodney W. Grimes 		if (*pattern == EOS) {		/* End of pattern? */
58358f0484fSRodney W. Grimes 			*pathend = EOS;
58458f0484fSRodney W. Grimes 			if (g_lstat(pathbuf, &sb, pglob))
58558f0484fSRodney W. Grimes 				return(0);
58658f0484fSRodney W. Grimes 
58758f0484fSRodney W. Grimes 			if (((pglob->gl_flags & GLOB_MARK) &&
58858f0484fSRodney W. Grimes 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
58958f0484fSRodney W. Grimes 			    || (S_ISLNK(sb.st_mode) &&
59058f0484fSRodney W. Grimes 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
59158f0484fSRodney W. Grimes 			    S_ISDIR(sb.st_mode)))) {
592487dbd92SPeter Wemm 				if (pathend + 1 > pathend_last)
5934a59c3abSMike Heffner 					return (GLOB_ABORTED);
59458f0484fSRodney W. Grimes 				*pathend++ = SEP;
59558f0484fSRodney W. Grimes 				*pathend = EOS;
59658f0484fSRodney W. Grimes 			}
59758f0484fSRodney W. Grimes 			++pglob->gl_matchc;
598bae8632fSJonathan Lemon 			return(globextend(pathbuf, pglob, limit));
59958f0484fSRodney W. Grimes 		}
60058f0484fSRodney W. Grimes 
60158f0484fSRodney W. Grimes 		/* Find end of next segment, copy tentatively to pathend. */
60258f0484fSRodney W. Grimes 		q = pathend;
60358f0484fSRodney W. Grimes 		p = pattern;
60458f0484fSRodney W. Grimes 		while (*p != EOS && *p != SEP) {
60558f0484fSRodney W. Grimes 			if (ismeta(*p))
60658f0484fSRodney W. Grimes 				anymeta = 1;
607487dbd92SPeter Wemm 			if (q + 1 > pathend_last)
6084a59c3abSMike Heffner 				return (GLOB_ABORTED);
60958f0484fSRodney W. Grimes 			*q++ = *p++;
61058f0484fSRodney W. Grimes 		}
61158f0484fSRodney W. Grimes 
61258f0484fSRodney W. Grimes 		if (!anymeta) {		/* No expansion, do next segment. */
61358f0484fSRodney W. Grimes 			pathend = q;
61458f0484fSRodney W. Grimes 			pattern = p;
615487dbd92SPeter Wemm 			while (*pattern == SEP) {
616487dbd92SPeter Wemm 				if (pathend + 1 > pathend_last)
6174a59c3abSMike Heffner 					return (GLOB_ABORTED);
61858f0484fSRodney W. Grimes 				*pathend++ = *pattern++;
619487dbd92SPeter Wemm 			}
62058f0484fSRodney W. Grimes 		} else			/* Need expansion, recurse. */
621487dbd92SPeter Wemm 			return(glob3(pathbuf, pathend, pathend_last, pattern, p,
622487dbd92SPeter Wemm 			    pglob, limit));
62358f0484fSRodney W. Grimes 	}
62458f0484fSRodney W. Grimes 	/* NOTREACHED */
62558f0484fSRodney W. Grimes }
62658f0484fSRodney W. Grimes 
62758f0484fSRodney W. Grimes static int
628487dbd92SPeter Wemm glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit)
629487dbd92SPeter Wemm 	Char *pathbuf, *pathend, *pathend_last, *pattern, *restpattern;
63058f0484fSRodney W. Grimes 	glob_t *pglob;
6314b767fa6SAndrey A. Chernov 	size_t *limit;
63258f0484fSRodney W. Grimes {
633b231cb39SDavid E. O'Brien 	struct dirent *dp;
63458f0484fSRodney W. Grimes 	DIR *dirp;
63558f0484fSRodney W. Grimes 	int err;
63658f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
63758f0484fSRodney W. Grimes 
63858f0484fSRodney W. Grimes 	/*
63958f0484fSRodney W. Grimes 	 * The readdirfunc declaration can't be prototyped, because it is
64058f0484fSRodney W. Grimes 	 * assigned, below, to two functions which are prototyped in glob.h
64158f0484fSRodney W. Grimes 	 * and dirent.h as taking pointers to differently typed opaque
64258f0484fSRodney W. Grimes 	 * structures.
64358f0484fSRodney W. Grimes 	 */
64458f0484fSRodney W. Grimes 	struct dirent *(*readdirfunc)();
64558f0484fSRodney W. Grimes 
646487dbd92SPeter Wemm 	if (pathend > pathend_last)
6474a59c3abSMike Heffner 		return (GLOB_ABORTED);
64858f0484fSRodney W. Grimes 	*pathend = EOS;
64958f0484fSRodney W. Grimes 	errno = 0;
65058f0484fSRodney W. Grimes 
65158f0484fSRodney W. Grimes 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
65258f0484fSRodney W. Grimes 		/* TODO: don't call for ENOENT or ENOTDIR? */
65358f0484fSRodney W. Grimes 		if (pglob->gl_errfunc) {
65427d52f69SPeter Wemm 			if (g_Ctoc(pathbuf, buf, sizeof(buf)))
6554a59c3abSMike Heffner 				return (GLOB_ABORTED);
65658f0484fSRodney W. Grimes 			if (pglob->gl_errfunc(buf, errno) ||
65758f0484fSRodney W. Grimes 			    pglob->gl_flags & GLOB_ERR)
6584a59c3abSMike Heffner 				return (GLOB_ABORTED);
65958f0484fSRodney W. Grimes 		}
66058f0484fSRodney W. Grimes 		return(0);
66158f0484fSRodney W. Grimes 	}
66258f0484fSRodney W. Grimes 
66358f0484fSRodney W. Grimes 	err = 0;
66458f0484fSRodney W. Grimes 
66558f0484fSRodney W. Grimes 	/* Search directory for matching names. */
66658f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
66758f0484fSRodney W. Grimes 		readdirfunc = pglob->gl_readdir;
66858f0484fSRodney W. Grimes 	else
66958f0484fSRodney W. Grimes 		readdirfunc = readdir;
67058f0484fSRodney W. Grimes 	while ((dp = (*readdirfunc)(dirp))) {
671b231cb39SDavid E. O'Brien 		u_char *sc;
672b231cb39SDavid E. O'Brien 		Char *dc;
673e9346e01STim J. Robbins 		wchar_t wc;
674e9346e01STim J. Robbins 		size_t clen;
675e9346e01STim J. Robbins 		mbstate_t mbs;
67658f0484fSRodney W. Grimes 
67758f0484fSRodney W. Grimes 		/* Initial DOT must be matched literally. */
67858f0484fSRodney W. Grimes 		if (dp->d_name[0] == DOT && *pattern != DOT)
67958f0484fSRodney W. Grimes 			continue;
680e9346e01STim J. Robbins 		memset(&mbs, 0, sizeof(mbs));
681487dbd92SPeter Wemm 		dc = pathend;
682487dbd92SPeter Wemm 		sc = (u_char *) dp->d_name;
683e9346e01STim J. Robbins 		while (dc < pathend_last) {
684e9346e01STim J. Robbins 			clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
685e9346e01STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
686e9346e01STim J. Robbins 				wc = *sc;
687e9346e01STim J. Robbins 				clen = 1;
688e9346e01STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
689e9346e01STim J. Robbins 			}
690e9346e01STim J. Robbins 			if ((*dc++ = wc) == EOS)
691e9346e01STim J. Robbins 				break;
692e9346e01STim J. Robbins 			sc += clen;
693e9346e01STim J. Robbins 		}
69458f0484fSRodney W. Grimes 		if (!match(pathend, pattern, restpattern)) {
69558f0484fSRodney W. Grimes 			*pathend = EOS;
69658f0484fSRodney W. Grimes 			continue;
69758f0484fSRodney W. Grimes 		}
698487dbd92SPeter Wemm 		err = glob2(pathbuf, --dc, pathend_last, restpattern,
699487dbd92SPeter Wemm 		    pglob, limit);
70058f0484fSRodney W. Grimes 		if (err)
70158f0484fSRodney W. Grimes 			break;
70258f0484fSRodney W. Grimes 	}
70358f0484fSRodney W. Grimes 
70458f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
70558f0484fSRodney W. Grimes 		(*pglob->gl_closedir)(dirp);
70658f0484fSRodney W. Grimes 	else
70758f0484fSRodney W. Grimes 		closedir(dirp);
70858f0484fSRodney W. Grimes 	return(err);
70958f0484fSRodney W. Grimes }
71058f0484fSRodney W. Grimes 
71158f0484fSRodney W. Grimes 
71258f0484fSRodney W. Grimes /*
71358f0484fSRodney W. Grimes  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
71458f0484fSRodney W. Grimes  * add the new item, and update gl_pathc.
71558f0484fSRodney W. Grimes  *
71658f0484fSRodney W. Grimes  * This assumes the BSD realloc, which only copies the block when its size
71758f0484fSRodney W. Grimes  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
71858f0484fSRodney W. Grimes  * behavior.
71958f0484fSRodney W. Grimes  *
72058f0484fSRodney W. Grimes  * Return 0 if new item added, error code if memory couldn't be allocated.
72158f0484fSRodney W. Grimes  *
72258f0484fSRodney W. Grimes  * Invariant of the glob_t structure:
72358f0484fSRodney W. Grimes  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
72458f0484fSRodney W. Grimes  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
72558f0484fSRodney W. Grimes  */
72658f0484fSRodney W. Grimes static int
727bae8632fSJonathan Lemon globextend(path, pglob, limit)
72858f0484fSRodney W. Grimes 	const Char *path;
72958f0484fSRodney W. Grimes 	glob_t *pglob;
7304b767fa6SAndrey A. Chernov 	size_t *limit;
73158f0484fSRodney W. Grimes {
732b231cb39SDavid E. O'Brien 	char **pathv;
7334b767fa6SAndrey A. Chernov 	size_t i, newsize, len;
73458f0484fSRodney W. Grimes 	char *copy;
73558f0484fSRodney W. Grimes 	const Char *p;
73658f0484fSRodney W. Grimes 
73775dc5f1aSMike Heffner 	if (*limit && pglob->gl_pathc > *limit) {
73875dc5f1aSMike Heffner 		errno = 0;
73975dc5f1aSMike Heffner 		return (GLOB_NOSPACE);
74075dc5f1aSMike Heffner 	}
741813c96dbSJonathan Lemon 
74258f0484fSRodney W. Grimes 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
74358f0484fSRodney W. Grimes 	pathv = pglob->gl_pathv ?
74458f0484fSRodney W. Grimes 		    realloc((char *)pglob->gl_pathv, newsize) :
74558f0484fSRodney W. Grimes 		    malloc(newsize);
74676e2bc01SPeter Wemm 	if (pathv == NULL) {
74776e2bc01SPeter Wemm 		if (pglob->gl_pathv) {
74876e2bc01SPeter Wemm 			free(pglob->gl_pathv);
74976e2bc01SPeter Wemm 			pglob->gl_pathv = NULL;
75076e2bc01SPeter Wemm 		}
75158f0484fSRodney W. Grimes 		return(GLOB_NOSPACE);
75276e2bc01SPeter Wemm 	}
75358f0484fSRodney W. Grimes 
75458f0484fSRodney W. Grimes 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
75558f0484fSRodney W. Grimes 		/* first time around -- clear initial gl_offs items */
75658f0484fSRodney W. Grimes 		pathv += pglob->gl_offs;
7574b767fa6SAndrey A. Chernov 		for (i = pglob->gl_offs + 1; --i > 0; )
75858f0484fSRodney W. Grimes 			*--pathv = NULL;
75958f0484fSRodney W. Grimes 	}
76058f0484fSRodney W. Grimes 	pglob->gl_pathv = pathv;
76158f0484fSRodney W. Grimes 
76258f0484fSRodney W. Grimes 	for (p = path; *p++;)
76358f0484fSRodney W. Grimes 		continue;
764e9346e01STim J. Robbins 	len = MB_CUR_MAX * (size_t)(p - path);	/* XXX overallocation */
76527d52f69SPeter Wemm 	if ((copy = malloc(len)) != NULL) {
76627d52f69SPeter Wemm 		if (g_Ctoc(path, copy, len)) {
76776e2bc01SPeter Wemm 			free(copy);
76876e2bc01SPeter Wemm 			return (GLOB_NOSPACE);
76976e2bc01SPeter Wemm 		}
77058f0484fSRodney W. Grimes 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
77158f0484fSRodney W. Grimes 	}
77258f0484fSRodney W. Grimes 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
77358f0484fSRodney W. Grimes 	return(copy == NULL ? GLOB_NOSPACE : 0);
77458f0484fSRodney W. Grimes }
77558f0484fSRodney W. Grimes 
77658f0484fSRodney W. Grimes /*
77758f0484fSRodney W. Grimes  * pattern matching function for filenames.  Each occurrence of the *
77858f0484fSRodney W. Grimes  * pattern causes a recursion level.
77958f0484fSRodney W. Grimes  */
78058f0484fSRodney W. Grimes static int
78158f0484fSRodney W. Grimes match(name, pat, patend)
782b231cb39SDavid E. O'Brien 	Char *name, *pat, *patend;
78358f0484fSRodney W. Grimes {
78458f0484fSRodney W. Grimes 	int ok, negate_range;
78558f0484fSRodney W. Grimes 	Char c, k;
78658f0484fSRodney W. Grimes 
78758f0484fSRodney W. Grimes 	while (pat < patend) {
78858f0484fSRodney W. Grimes 		c = *pat++;
78958f0484fSRodney W. Grimes 		switch (c & M_MASK) {
79058f0484fSRodney W. Grimes 		case M_ALL:
79158f0484fSRodney W. Grimes 			if (pat == patend)
79258f0484fSRodney W. Grimes 				return(1);
79358f0484fSRodney W. Grimes 			do
79458f0484fSRodney W. Grimes 			    if (match(name, pat, patend))
79558f0484fSRodney W. Grimes 				    return(1);
79658f0484fSRodney W. Grimes 			while (*name++ != EOS);
79758f0484fSRodney W. Grimes 			return(0);
79858f0484fSRodney W. Grimes 		case M_ONE:
79958f0484fSRodney W. Grimes 			if (*name++ == EOS)
80058f0484fSRodney W. Grimes 				return(0);
80158f0484fSRodney W. Grimes 			break;
80258f0484fSRodney W. Grimes 		case M_SET:
80358f0484fSRodney W. Grimes 			ok = 0;
80458f0484fSRodney W. Grimes 			if ((k = *name++) == EOS)
80558f0484fSRodney W. Grimes 				return(0);
80658f0484fSRodney W. Grimes 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
80758f0484fSRodney W. Grimes 				++pat;
80858f0484fSRodney W. Grimes 			while (((c = *pat++) & M_MASK) != M_END)
80958f0484fSRodney W. Grimes 				if ((*pat & M_MASK) == M_RNG) {
81021d58869SAndrey A. Chernov 					if (__collate_load_error ?
81121d58869SAndrey A. Chernov 					    CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
81221d58869SAndrey A. Chernov 					       __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
813edcfa072SAndrey A. Chernov 					    && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
814b92a8919SAndrey A. Chernov 					   )
81558f0484fSRodney W. Grimes 						ok = 1;
81658f0484fSRodney W. Grimes 					pat += 2;
81758f0484fSRodney W. Grimes 				} else if (c == k)
81858f0484fSRodney W. Grimes 					ok = 1;
81958f0484fSRodney W. Grimes 			if (ok == negate_range)
82058f0484fSRodney W. Grimes 				return(0);
82158f0484fSRodney W. Grimes 			break;
82258f0484fSRodney W. Grimes 		default:
82358f0484fSRodney W. Grimes 			if (*name++ != c)
82458f0484fSRodney W. Grimes 				return(0);
82558f0484fSRodney W. Grimes 			break;
82658f0484fSRodney W. Grimes 		}
82758f0484fSRodney W. Grimes 	}
82858f0484fSRodney W. Grimes 	return(*name == EOS);
82958f0484fSRodney W. Grimes }
83058f0484fSRodney W. Grimes 
83158f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
83258f0484fSRodney W. Grimes void
83358f0484fSRodney W. Grimes globfree(pglob)
83458f0484fSRodney W. Grimes 	glob_t *pglob;
83558f0484fSRodney W. Grimes {
8364b767fa6SAndrey A. Chernov 	size_t i;
837b231cb39SDavid E. O'Brien 	char **pp;
83858f0484fSRodney W. Grimes 
83958f0484fSRodney W. Grimes 	if (pglob->gl_pathv != NULL) {
84058f0484fSRodney W. Grimes 		pp = pglob->gl_pathv + pglob->gl_offs;
84158f0484fSRodney W. Grimes 		for (i = pglob->gl_pathc; i--; ++pp)
84258f0484fSRodney W. Grimes 			if (*pp)
84358f0484fSRodney W. Grimes 				free(*pp);
84458f0484fSRodney W. Grimes 		free(pglob->gl_pathv);
84576e2bc01SPeter Wemm 		pglob->gl_pathv = NULL;
84658f0484fSRodney W. Grimes 	}
84758f0484fSRodney W. Grimes }
84858f0484fSRodney W. Grimes 
84958f0484fSRodney W. Grimes static DIR *
85058f0484fSRodney W. Grimes g_opendir(str, pglob)
851b231cb39SDavid E. O'Brien 	Char *str;
85258f0484fSRodney W. Grimes 	glob_t *pglob;
85358f0484fSRodney W. Grimes {
85458f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
85558f0484fSRodney W. Grimes 
85658f0484fSRodney W. Grimes 	if (!*str)
85758f0484fSRodney W. Grimes 		strcpy(buf, ".");
85876e2bc01SPeter Wemm 	else {
85927d52f69SPeter Wemm 		if (g_Ctoc(str, buf, sizeof(buf)))
86076e2bc01SPeter Wemm 			return (NULL);
86176e2bc01SPeter Wemm 	}
86258f0484fSRodney W. Grimes 
86358f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
86458f0484fSRodney W. Grimes 		return((*pglob->gl_opendir)(buf));
86558f0484fSRodney W. Grimes 
86658f0484fSRodney W. Grimes 	return(opendir(buf));
86758f0484fSRodney W. Grimes }
86858f0484fSRodney W. Grimes 
86958f0484fSRodney W. Grimes static int
87058f0484fSRodney W. Grimes g_lstat(fn, sb, pglob)
871b231cb39SDavid E. O'Brien 	Char *fn;
87258f0484fSRodney W. Grimes 	struct stat *sb;
87358f0484fSRodney W. Grimes 	glob_t *pglob;
87458f0484fSRodney W. Grimes {
87558f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
87658f0484fSRodney W. Grimes 
87727d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
87876e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
87976e2bc01SPeter Wemm 		return (-1);
88076e2bc01SPeter Wemm 	}
88158f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
88258f0484fSRodney W. Grimes 		return((*pglob->gl_lstat)(buf, sb));
88358f0484fSRodney W. Grimes 	return(lstat(buf, sb));
88458f0484fSRodney W. Grimes }
88558f0484fSRodney W. Grimes 
88658f0484fSRodney W. Grimes static int
88758f0484fSRodney W. Grimes g_stat(fn, sb, pglob)
888b231cb39SDavid E. O'Brien 	Char *fn;
88958f0484fSRodney W. Grimes 	struct stat *sb;
89058f0484fSRodney W. Grimes 	glob_t *pglob;
89158f0484fSRodney W. Grimes {
89258f0484fSRodney W. Grimes 	char buf[MAXPATHLEN];
89358f0484fSRodney W. Grimes 
89427d52f69SPeter Wemm 	if (g_Ctoc(fn, buf, sizeof(buf))) {
89576e2bc01SPeter Wemm 		errno = ENAMETOOLONG;
89676e2bc01SPeter Wemm 		return (-1);
89776e2bc01SPeter Wemm 	}
89858f0484fSRodney W. Grimes 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
89958f0484fSRodney W. Grimes 		return((*pglob->gl_stat)(buf, sb));
90058f0484fSRodney W. Grimes 	return(stat(buf, sb));
90158f0484fSRodney W. Grimes }
90258f0484fSRodney W. Grimes 
90358f0484fSRodney W. Grimes static Char *
90458f0484fSRodney W. Grimes g_strchr(str, ch)
90558f0484fSRodney W. Grimes 	Char *str;
906e9346e01STim J. Robbins 	wchar_t ch;
90758f0484fSRodney W. Grimes {
90858f0484fSRodney W. Grimes 	do {
90958f0484fSRodney W. Grimes 		if (*str == ch)
91058f0484fSRodney W. Grimes 			return (str);
91158f0484fSRodney W. Grimes 	} while (*str++);
91258f0484fSRodney W. Grimes 	return (NULL);
91358f0484fSRodney W. Grimes }
91458f0484fSRodney W. Grimes 
91576e2bc01SPeter Wemm static int
91627d52f69SPeter Wemm g_Ctoc(str, buf, len)
91727d52f69SPeter Wemm 	const Char *str;
91827d52f69SPeter Wemm 	char *buf;
9194b767fa6SAndrey A. Chernov 	size_t len;
92058f0484fSRodney W. Grimes {
921e9346e01STim J. Robbins 	mbstate_t mbs;
922e9346e01STim J. Robbins 	size_t clen;
92358f0484fSRodney W. Grimes 
924e9346e01STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
925e9346e01STim J. Robbins 	while (len >= MB_CUR_MAX) {
926e9346e01STim J. Robbins 		clen = wcrtomb(buf, *str, &mbs);
927e9346e01STim J. Robbins 		if (clen == (size_t)-1)
928e9346e01STim J. Robbins 			return (1);
929e9346e01STim J. Robbins 		if (*str == L'\0')
93076e2bc01SPeter Wemm 			return (0);
931e9346e01STim J. Robbins 		str++;
932e9346e01STim J. Robbins 		buf += clen;
933e9346e01STim J. Robbins 		len -= clen;
93458f0484fSRodney W. Grimes 	}
93527d52f69SPeter Wemm 	return (1);
93627d52f69SPeter Wemm }
93758f0484fSRodney W. Grimes 
93858f0484fSRodney W. Grimes #ifdef DEBUG
93958f0484fSRodney W. Grimes static void
94058f0484fSRodney W. Grimes qprintf(str, s)
94158f0484fSRodney W. Grimes 	const char *str;
942b231cb39SDavid E. O'Brien 	Char *s;
94358f0484fSRodney W. Grimes {
944b231cb39SDavid E. O'Brien 	Char *p;
94558f0484fSRodney W. Grimes 
94658f0484fSRodney W. Grimes 	(void)printf("%s:\n", str);
94758f0484fSRodney W. Grimes 	for (p = s; *p; p++)
94858f0484fSRodney W. Grimes 		(void)printf("%c", CHAR(*p));
94958f0484fSRodney W. Grimes 	(void)printf("\n");
95058f0484fSRodney W. Grimes 	for (p = s; *p; p++)
95158f0484fSRodney W. Grimes 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
95258f0484fSRodney W. Grimes 	(void)printf("\n");
95358f0484fSRodney W. Grimes 	for (p = s; *p; p++)
95458f0484fSRodney W. Grimes 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
95558f0484fSRodney W. Grimes 	(void)printf("\n");
95658f0484fSRodney W. Grimes }
95758f0484fSRodney W. Grimes #endif
958