xref: /titanic_44/usr/src/cmd/ssh/libopenbsd-compat/common/glob.c (revision cf58b2543a341d4f5a661de47968a7016c3207ff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1989, 1993
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
67c478bd9Sstevel@tonic-gate  * Guido van Rossum.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
97c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
107c478bd9Sstevel@tonic-gate  * are met:
117c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
127c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
137c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
147c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
157c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
167c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
177c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
187c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
197c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
207c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
217c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
227c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
257c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
267c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
287c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
297c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
307c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
317c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
337c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
347c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "includes.h"
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
417c478bd9Sstevel@tonic-gate     !defined(GLOB_HAS_GL_MATCHC)
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
447c478bd9Sstevel@tonic-gate #if 0
457c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
467c478bd9Sstevel@tonic-gate #else
477c478bd9Sstevel@tonic-gate static char rcsid[] = "$OpenBSD: glob.c,v 1.20 2002/06/14 21:34:58 todd Exp $";
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * glob(3) -- a superset of the one defined in POSIX 1003.2.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Optional extra services, controlled by flags not defined by POSIX:
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * GLOB_QUOTE:
597c478bd9Sstevel@tonic-gate  *	Escaping convention: \ inhibits any special meaning the following
607c478bd9Sstevel@tonic-gate  *	character might have (except \ at end of string is retained).
617c478bd9Sstevel@tonic-gate  * GLOB_MAGCHAR:
627c478bd9Sstevel@tonic-gate  *	Set in gl_flags if pattern contained a globbing character.
637c478bd9Sstevel@tonic-gate  * GLOB_NOMAGIC:
647c478bd9Sstevel@tonic-gate  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
657c478bd9Sstevel@tonic-gate  *	not contain any magic characters.  [Used in csh style globbing]
667c478bd9Sstevel@tonic-gate  * GLOB_ALTDIRFUNC:
677c478bd9Sstevel@tonic-gate  *	Use alternately specified directory access functions.
687c478bd9Sstevel@tonic-gate  * GLOB_TILDE:
697c478bd9Sstevel@tonic-gate  *	expand ~user/foo to the /home/dir/of/user/foo
707c478bd9Sstevel@tonic-gate  * GLOB_BRACE:
717c478bd9Sstevel@tonic-gate  *	expand {1,2}{a,b} to 1a 1b 2a 2b
727c478bd9Sstevel@tonic-gate  * gl_matchc:
737c478bd9Sstevel@tonic-gate  *	Number of matches in the current invocation of glob.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	DOLLAR		'$'
787c478bd9Sstevel@tonic-gate #define	DOT		'.'
797c478bd9Sstevel@tonic-gate #define	EOS		'\0'
807c478bd9Sstevel@tonic-gate #define	LBRACKET	'['
817c478bd9Sstevel@tonic-gate #define	NOT		'!'
827c478bd9Sstevel@tonic-gate #define	QUESTION	'?'
837c478bd9Sstevel@tonic-gate #define	QUOTE		'\\'
847c478bd9Sstevel@tonic-gate #define	RANGE		'-'
857c478bd9Sstevel@tonic-gate #define	RBRACKET	']'
867c478bd9Sstevel@tonic-gate #define	SEP		'/'
877c478bd9Sstevel@tonic-gate #define	STAR		'*'
887c478bd9Sstevel@tonic-gate #undef TILDE			/* Some platforms may already define it */
897c478bd9Sstevel@tonic-gate #define	TILDE		'~'
907c478bd9Sstevel@tonic-gate #define	UNDERSCORE	'_'
917c478bd9Sstevel@tonic-gate #define	LBRACE		'{'
927c478bd9Sstevel@tonic-gate #define	RBRACE		'}'
937c478bd9Sstevel@tonic-gate #define	SLASH		'/'
947c478bd9Sstevel@tonic-gate #define	COMMA		','
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #ifndef DEBUG
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	M_QUOTE		0x8000
997c478bd9Sstevel@tonic-gate #define	M_PROTECT	0x4000
1007c478bd9Sstevel@tonic-gate #define	M_MASK		0xffff
1017c478bd9Sstevel@tonic-gate #define	M_ASCII		0x00ff
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef u_short Char;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	M_QUOTE		0x80
1087c478bd9Sstevel@tonic-gate #define	M_PROTECT	0x40
1097c478bd9Sstevel@tonic-gate #define	M_MASK		0xff
1107c478bd9Sstevel@tonic-gate #define	M_ASCII		0x7f
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate typedef char Char;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	CHAR(c)		((Char)((c)&M_ASCII))
1187c478bd9Sstevel@tonic-gate #define	META(c)		((Char)((c)|M_QUOTE))
1197c478bd9Sstevel@tonic-gate #define	M_ALL		META('*')
1207c478bd9Sstevel@tonic-gate #define	M_END		META(']')
1217c478bd9Sstevel@tonic-gate #define	M_NOT		META('!')
1227c478bd9Sstevel@tonic-gate #define	M_ONE		META('?')
1237c478bd9Sstevel@tonic-gate #define	M_RNG		META('-')
1247c478bd9Sstevel@tonic-gate #define	M_SET		META('[')
1257c478bd9Sstevel@tonic-gate #define	ismeta(c)	(((c)&M_QUOTE) != 0)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
128*cf58b254SGary Mills #define	GLOB_LIMIT_MALLOC	65536
129*cf58b254SGary Mills #define	GLOB_LIMIT_STAT		128
130*cf58b254SGary Mills #define	GLOB_LIMIT_READDIR	16384
131*cf58b254SGary Mills 
132*cf58b254SGary Mills #define	GLOB_INDEX_MALLOC	0
133*cf58b254SGary Mills #define	GLOB_INDEX_STAT		1
134*cf58b254SGary Mills #define	GLOB_INDEX_READDIR	2
135*cf58b254SGary Mills 
1367c478bd9Sstevel@tonic-gate static int	 compare(const void *, const void *);
1377c478bd9Sstevel@tonic-gate static int	 g_Ctoc(const Char *, char *, u_int);
1387c478bd9Sstevel@tonic-gate static int	 g_lstat(Char *, struct stat *, glob_t *);
1397c478bd9Sstevel@tonic-gate static DIR	*g_opendir(Char *, glob_t *);
1407c478bd9Sstevel@tonic-gate static Char	*g_strchr(Char *, int);
1417c478bd9Sstevel@tonic-gate static int	 g_stat(Char *, struct stat *, glob_t *);
142*cf58b254SGary Mills static int	 glob0(const Char *, glob_t *, size_t *);
1437c478bd9Sstevel@tonic-gate static int	 glob1(Char *, Char *, glob_t *, size_t *);
1447c478bd9Sstevel@tonic-gate static int	 glob2(Char *, Char *, Char *, Char *, Char *, Char *,
1457c478bd9Sstevel@tonic-gate 		    glob_t *, size_t *);
1467c478bd9Sstevel@tonic-gate static int	 glob3(Char *, Char *, Char *, Char *, Char *, Char *,
1477c478bd9Sstevel@tonic-gate 		    Char *, Char *, glob_t *, size_t *);
1487c478bd9Sstevel@tonic-gate static int	 globextend(const Char *, glob_t *, size_t *);
1497c478bd9Sstevel@tonic-gate static const Char *
1507c478bd9Sstevel@tonic-gate 		 globtilde(const Char *, Char *, size_t, glob_t *);
151*cf58b254SGary Mills static int	 globexp1(const Char *, glob_t *, size_t *);
152*cf58b254SGary Mills static int	 globexp2(const Char *, const Char *, glob_t *, int *,
153*cf58b254SGary Mills 			  size_t *);
1547c478bd9Sstevel@tonic-gate static int	 match(Char *, Char *, Char *);
1557c478bd9Sstevel@tonic-gate #ifdef DEBUG
1567c478bd9Sstevel@tonic-gate static void	 qprintf(const char *, Char *);
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
glob(pattern,flags,errfunc,pglob)1607c478bd9Sstevel@tonic-gate glob(pattern, flags, errfunc, pglob)
1617c478bd9Sstevel@tonic-gate 	const char *pattern;
1627c478bd9Sstevel@tonic-gate 	int flags, (*errfunc)(const char *, int);
1637c478bd9Sstevel@tonic-gate 	glob_t *pglob;
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	const u_char *patnext;
1667c478bd9Sstevel@tonic-gate 	int c;
1677c478bd9Sstevel@tonic-gate 	Char *bufnext, *bufend, patbuf[MAXPATHLEN];
168*cf58b254SGary Mills 	/* 0 = malloc(), 1 = stat(), 2 = readdir() */
169*cf58b254SGary Mills 	static size_t limit[] = { 0, 0, 0 };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	patnext = (u_char *) pattern;
1727c478bd9Sstevel@tonic-gate 	if (!(flags & GLOB_APPEND)) {
1737c478bd9Sstevel@tonic-gate 		pglob->gl_pathc = 0;
1747c478bd9Sstevel@tonic-gate 		pglob->gl_pathv = NULL;
1757c478bd9Sstevel@tonic-gate 		if (!(flags & GLOB_DOOFFS))
1767c478bd9Sstevel@tonic-gate 			pglob->gl_offs = 0;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
1797c478bd9Sstevel@tonic-gate 	pglob->gl_errfunc = errfunc;
1807c478bd9Sstevel@tonic-gate 	pglob->gl_matchc = 0;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	bufnext = patbuf;
1837c478bd9Sstevel@tonic-gate 	bufend = bufnext + MAXPATHLEN - 1;
1847c478bd9Sstevel@tonic-gate 	if (flags & GLOB_NOESCAPE)
1857c478bd9Sstevel@tonic-gate 		while (bufnext < bufend && (c = *patnext++) != EOS)
1867c478bd9Sstevel@tonic-gate 			*bufnext++ = c;
1877c478bd9Sstevel@tonic-gate 	else {
1887c478bd9Sstevel@tonic-gate 		/* Protect the quoted characters. */
1897c478bd9Sstevel@tonic-gate 		while (bufnext < bufend && (c = *patnext++) != EOS)
1907c478bd9Sstevel@tonic-gate 			if (c == QUOTE) {
1917c478bd9Sstevel@tonic-gate 				if ((c = *patnext++) == EOS) {
1927c478bd9Sstevel@tonic-gate 					c = QUOTE;
1937c478bd9Sstevel@tonic-gate 					--patnext;
1947c478bd9Sstevel@tonic-gate 				}
1957c478bd9Sstevel@tonic-gate 				*bufnext++ = c | M_PROTECT;
1967c478bd9Sstevel@tonic-gate 			} else
1977c478bd9Sstevel@tonic-gate 				*bufnext++ = c;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	*bufnext = EOS;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (flags & GLOB_BRACE)
202*cf58b254SGary Mills 		return globexp1(patbuf, pglob, limit);
2037c478bd9Sstevel@tonic-gate 	else
204*cf58b254SGary Mills 		return glob0(patbuf, pglob, limit);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Expand recursively a glob {} pattern. When there is no more expansion
2097c478bd9Sstevel@tonic-gate  * invoke the standard globbing routine to glob the rest of the magic
2107c478bd9Sstevel@tonic-gate  * characters
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int
globexp1(pattern,pglob,limit)213*cf58b254SGary Mills globexp1(pattern, pglob, limit)
2147c478bd9Sstevel@tonic-gate 	const Char *pattern;
2157c478bd9Sstevel@tonic-gate 	glob_t *pglob;
216*cf58b254SGary Mills 	size_t *limit;
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	const Char* ptr = pattern;
2197c478bd9Sstevel@tonic-gate 	int rv;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* Protect a single {}, for find(1), like csh */
2227c478bd9Sstevel@tonic-gate 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
223*cf58b254SGary Mills 		return glob0(pattern, pglob, limit);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
226*cf58b254SGary Mills 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
2277c478bd9Sstevel@tonic-gate 			return rv;
2287c478bd9Sstevel@tonic-gate 
229*cf58b254SGary Mills 	return glob0(pattern, pglob, limit);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Recursive brace globbing helper. Tries to expand a single brace.
2357c478bd9Sstevel@tonic-gate  * If it succeeds then it invokes globexp1 with the new pattern.
2367c478bd9Sstevel@tonic-gate  * If it fails then it tries to glob the rest of the pattern and returns.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate static int
globexp2(ptr,pattern,pglob,rv,limit)239*cf58b254SGary Mills globexp2(ptr, pattern, pglob, rv, limit)
2407c478bd9Sstevel@tonic-gate 	const Char *ptr, *pattern;
2417c478bd9Sstevel@tonic-gate 	glob_t *pglob;
2427c478bd9Sstevel@tonic-gate 	int *rv;
243*cf58b254SGary Mills 	size_t *limit;
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	int     i;
2467c478bd9Sstevel@tonic-gate 	Char   *lm, *ls;
2477c478bd9Sstevel@tonic-gate 	const Char *pe, *pm, *pl;
2487c478bd9Sstevel@tonic-gate 	Char    patbuf[MAXPATHLEN];
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* copy part up to the brace */
2517c478bd9Sstevel@tonic-gate 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
2527c478bd9Sstevel@tonic-gate 		;
2537c478bd9Sstevel@tonic-gate 	*lm = EOS;
2547c478bd9Sstevel@tonic-gate 	ls = lm;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/* Find the balanced brace */
2577c478bd9Sstevel@tonic-gate 	for (i = 0, pe = ++ptr; *pe; pe++)
2587c478bd9Sstevel@tonic-gate 		if (*pe == LBRACKET) {
2597c478bd9Sstevel@tonic-gate 			/* Ignore everything between [] */
2607c478bd9Sstevel@tonic-gate 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
2617c478bd9Sstevel@tonic-gate 				;
2627c478bd9Sstevel@tonic-gate 			if (*pe == EOS) {
2637c478bd9Sstevel@tonic-gate 				/*
2647c478bd9Sstevel@tonic-gate 				 * We could not find a matching RBRACKET.
2657c478bd9Sstevel@tonic-gate 				 * Ignore and just look for RBRACE
2667c478bd9Sstevel@tonic-gate 				 */
2677c478bd9Sstevel@tonic-gate 				pe = pm;
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 		} else if (*pe == LBRACE)
2707c478bd9Sstevel@tonic-gate 			i++;
2717c478bd9Sstevel@tonic-gate 		else if (*pe == RBRACE) {
2727c478bd9Sstevel@tonic-gate 			if (i == 0)
2737c478bd9Sstevel@tonic-gate 				break;
2747c478bd9Sstevel@tonic-gate 			i--;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/* Non matching braces; just glob the pattern */
2787c478bd9Sstevel@tonic-gate 	if (i != 0 || *pe == EOS) {
279*cf58b254SGary Mills 		*rv = glob0(patbuf, pglob, limit);
2807c478bd9Sstevel@tonic-gate 		return 0;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
2847c478bd9Sstevel@tonic-gate 		switch (*pm) {
2857c478bd9Sstevel@tonic-gate 		case LBRACKET:
2867c478bd9Sstevel@tonic-gate 			/* Ignore everything between [] */
2877c478bd9Sstevel@tonic-gate 			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
2887c478bd9Sstevel@tonic-gate 				;
2897c478bd9Sstevel@tonic-gate 			if (*pm == EOS) {
2907c478bd9Sstevel@tonic-gate 				/*
2917c478bd9Sstevel@tonic-gate 				 * We could not find a matching RBRACKET.
2927c478bd9Sstevel@tonic-gate 				 * Ignore and just look for RBRACE
2937c478bd9Sstevel@tonic-gate 				 */
2947c478bd9Sstevel@tonic-gate 				pm = pl;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		case LBRACE:
2997c478bd9Sstevel@tonic-gate 			i++;
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		case RBRACE:
3037c478bd9Sstevel@tonic-gate 			if (i) {
3047c478bd9Sstevel@tonic-gate 				i--;
3057c478bd9Sstevel@tonic-gate 				break;
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
3087c478bd9Sstevel@tonic-gate 		case COMMA:
3097c478bd9Sstevel@tonic-gate 			if (i && *pm == COMMA)
3107c478bd9Sstevel@tonic-gate 				break;
3117c478bd9Sstevel@tonic-gate 			else {
3127c478bd9Sstevel@tonic-gate 				/* Append the current string */
3137c478bd9Sstevel@tonic-gate 				for (lm = ls; (pl < pm); *lm++ = *pl++)
3147c478bd9Sstevel@tonic-gate 					;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 				/*
3177c478bd9Sstevel@tonic-gate 				 * Append the rest of the pattern after the
3187c478bd9Sstevel@tonic-gate 				 * closing brace
3197c478bd9Sstevel@tonic-gate 				 */
3207c478bd9Sstevel@tonic-gate 				for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
3217c478bd9Sstevel@tonic-gate 					;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 				/* Expand the current pattern */
3247c478bd9Sstevel@tonic-gate #ifdef DEBUG
3257c478bd9Sstevel@tonic-gate 				qprintf("globexp2:", patbuf);
3267c478bd9Sstevel@tonic-gate #endif
327*cf58b254SGary Mills 				*rv = globexp1(patbuf, pglob, limit);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 				/* move after the comma, to the next string */
3307c478bd9Sstevel@tonic-gate 				pl = pm + 1;
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 			break;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		default:
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	*rv = 0;
3397c478bd9Sstevel@tonic-gate 	return 0;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * expand tilde from the passwd file.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate static const Char *
globtilde(pattern,patbuf,patbuf_len,pglob)3487c478bd9Sstevel@tonic-gate globtilde(pattern, patbuf, patbuf_len, pglob)
3497c478bd9Sstevel@tonic-gate 	const Char *pattern;
3507c478bd9Sstevel@tonic-gate 	Char *patbuf;
3517c478bd9Sstevel@tonic-gate 	size_t patbuf_len;
3527c478bd9Sstevel@tonic-gate 	glob_t *pglob;
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
3557c478bd9Sstevel@tonic-gate 	char *h;
3567c478bd9Sstevel@tonic-gate 	const Char *p;
3577c478bd9Sstevel@tonic-gate 	Char *b, *eb;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
3607c478bd9Sstevel@tonic-gate 		return pattern;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* Copy up to the end of the string or / */
3637c478bd9Sstevel@tonic-gate 	eb = &patbuf[patbuf_len - 1];
3647c478bd9Sstevel@tonic-gate 	for (p = pattern + 1, h = (char *) patbuf;
3657c478bd9Sstevel@tonic-gate 	    h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
3667c478bd9Sstevel@tonic-gate 		;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	*h = EOS;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #if 0
3717c478bd9Sstevel@tonic-gate 	if (h == (char *)eb)
3727c478bd9Sstevel@tonic-gate 		return what;
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (((char *) patbuf)[0] == EOS) {
3767c478bd9Sstevel@tonic-gate 		/*
3777c478bd9Sstevel@tonic-gate 		 * handle a plain ~ or ~/ by expanding $HOME
3787c478bd9Sstevel@tonic-gate 		 * first and then trying the password file
3797c478bd9Sstevel@tonic-gate 		 */
3807c478bd9Sstevel@tonic-gate #if 0
3817c478bd9Sstevel@tonic-gate 		if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
3827c478bd9Sstevel@tonic-gate #endif
3837c478bd9Sstevel@tonic-gate 		if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
3847c478bd9Sstevel@tonic-gate 			if ((pwd = getpwuid(getuid())) == NULL)
3857c478bd9Sstevel@tonic-gate 				return pattern;
3867c478bd9Sstevel@tonic-gate 			else
3877c478bd9Sstevel@tonic-gate 				h = pwd->pw_dir;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	} else {
3907c478bd9Sstevel@tonic-gate 		/*
3917c478bd9Sstevel@tonic-gate 		 * Expand a ~user
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if ((pwd = getpwnam((char*) patbuf)) == NULL)
3947c478bd9Sstevel@tonic-gate 			return pattern;
3957c478bd9Sstevel@tonic-gate 		else
3967c478bd9Sstevel@tonic-gate 			h = pwd->pw_dir;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/* Copy the home directory */
4007c478bd9Sstevel@tonic-gate 	for (b = patbuf; b < eb && *h; *b++ = *h++)
4017c478bd9Sstevel@tonic-gate 		;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/* Append the rest of the pattern */
4047c478bd9Sstevel@tonic-gate 	while (b < eb && (*b++ = *p++) != EOS)
4057c478bd9Sstevel@tonic-gate 		;
4067c478bd9Sstevel@tonic-gate 	*b = EOS;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	return patbuf;
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * The main glob() routine: compiles the pattern (optionally processing
4147c478bd9Sstevel@tonic-gate  * quotes), calls glob1() to do the real pattern matching, and finally
4157c478bd9Sstevel@tonic-gate  * sorts the list (unless unsorted operation is requested).  Returns 0
4167c478bd9Sstevel@tonic-gate  * if things went well, nonzero if errors occurred.  It is not an error
4177c478bd9Sstevel@tonic-gate  * to find no matches.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate static int
glob0(pattern,pglob,limit)420*cf58b254SGary Mills glob0(pattern, pglob, limit)
4217c478bd9Sstevel@tonic-gate 	const Char *pattern;
4227c478bd9Sstevel@tonic-gate 	glob_t *pglob;
423*cf58b254SGary Mills 	size_t *limit;
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	const Char *qpatnext;
4267c478bd9Sstevel@tonic-gate 	int c, err, oldpathc;
4277c478bd9Sstevel@tonic-gate 	Char *bufnext, patbuf[MAXPATHLEN];
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
4307c478bd9Sstevel@tonic-gate 	oldpathc = pglob->gl_pathc;
4317c478bd9Sstevel@tonic-gate 	bufnext = patbuf;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* We don't need to check for buffer overflow any more. */
4347c478bd9Sstevel@tonic-gate 	while ((c = *qpatnext++) != EOS) {
4357c478bd9Sstevel@tonic-gate 		switch (c) {
4367c478bd9Sstevel@tonic-gate 		case LBRACKET:
4377c478bd9Sstevel@tonic-gate 			c = *qpatnext;
4387c478bd9Sstevel@tonic-gate 			if (c == NOT)
4397c478bd9Sstevel@tonic-gate 				++qpatnext;
4407c478bd9Sstevel@tonic-gate 			if (*qpatnext == EOS ||
4417c478bd9Sstevel@tonic-gate 			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
4427c478bd9Sstevel@tonic-gate 				*bufnext++ = LBRACKET;
4437c478bd9Sstevel@tonic-gate 				if (c == NOT)
4447c478bd9Sstevel@tonic-gate 					--qpatnext;
4457c478bd9Sstevel@tonic-gate 				break;
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 			*bufnext++ = M_SET;
4487c478bd9Sstevel@tonic-gate 			if (c == NOT)
4497c478bd9Sstevel@tonic-gate 				*bufnext++ = M_NOT;
4507c478bd9Sstevel@tonic-gate 			c = *qpatnext++;
4517c478bd9Sstevel@tonic-gate 			do {
4527c478bd9Sstevel@tonic-gate 				*bufnext++ = CHAR(c);
4537c478bd9Sstevel@tonic-gate 				if (*qpatnext == RANGE &&
4547c478bd9Sstevel@tonic-gate 				    (c = qpatnext[1]) != RBRACKET) {
4557c478bd9Sstevel@tonic-gate 					*bufnext++ = M_RNG;
4567c478bd9Sstevel@tonic-gate 					*bufnext++ = CHAR(c);
4577c478bd9Sstevel@tonic-gate 					qpatnext += 2;
4587c478bd9Sstevel@tonic-gate 				}
4597c478bd9Sstevel@tonic-gate 			} while ((c = *qpatnext++) != RBRACKET);
4607c478bd9Sstevel@tonic-gate 			pglob->gl_flags |= GLOB_MAGCHAR;
4617c478bd9Sstevel@tonic-gate 			*bufnext++ = M_END;
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 		case QUESTION:
4647c478bd9Sstevel@tonic-gate 			pglob->gl_flags |= GLOB_MAGCHAR;
4657c478bd9Sstevel@tonic-gate 			*bufnext++ = M_ONE;
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 		case STAR:
4687c478bd9Sstevel@tonic-gate 			pglob->gl_flags |= GLOB_MAGCHAR;
4697c478bd9Sstevel@tonic-gate 			/* collapse adjacent stars to one,
4707c478bd9Sstevel@tonic-gate 			 * to avoid exponential behavior
4717c478bd9Sstevel@tonic-gate 			 */
4727c478bd9Sstevel@tonic-gate 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
4737c478bd9Sstevel@tonic-gate 				*bufnext++ = M_ALL;
4747c478bd9Sstevel@tonic-gate 			break;
4757c478bd9Sstevel@tonic-gate 		default:
4767c478bd9Sstevel@tonic-gate 			*bufnext++ = CHAR(c);
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	*bufnext = EOS;
4817c478bd9Sstevel@tonic-gate #ifdef DEBUG
4827c478bd9Sstevel@tonic-gate 	qprintf("glob0:", patbuf);
4837c478bd9Sstevel@tonic-gate #endif
4847c478bd9Sstevel@tonic-gate 
485*cf58b254SGary Mills 	if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limit)) != 0)
4867c478bd9Sstevel@tonic-gate 		return(err);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * If there was no match we are going to append the pattern
4907c478bd9Sstevel@tonic-gate 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
4917c478bd9Sstevel@tonic-gate 	 * and the pattern did not contain any magic characters
4927c478bd9Sstevel@tonic-gate 	 * GLOB_NOMAGIC is there just for compatibility with csh.
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	if (pglob->gl_pathc == oldpathc) {
4957c478bd9Sstevel@tonic-gate 		if ((pglob->gl_flags & GLOB_NOCHECK) ||
4967c478bd9Sstevel@tonic-gate 		    ((pglob->gl_flags & GLOB_NOMAGIC) &&
4977c478bd9Sstevel@tonic-gate 		    !(pglob->gl_flags & GLOB_MAGCHAR)))
498*cf58b254SGary Mills 			return(globextend(pattern, pglob, limit));
4997c478bd9Sstevel@tonic-gate 		else
5007c478bd9Sstevel@tonic-gate 			return(GLOB_NOMATCH);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	if (!(pglob->gl_flags & GLOB_NOSORT))
5037c478bd9Sstevel@tonic-gate 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
5047c478bd9Sstevel@tonic-gate 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
5057c478bd9Sstevel@tonic-gate 	return(0);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate static int
compare(p,q)5097c478bd9Sstevel@tonic-gate compare(p, q)
5107c478bd9Sstevel@tonic-gate 	const void *p, *q;
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	return(strcmp(*(char **)p, *(char **)q));
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static int
glob1(pattern,pattern_last,pglob,limit)516*cf58b254SGary Mills glob1(pattern, pattern_last, pglob, limit)
5177c478bd9Sstevel@tonic-gate 	Char *pattern, *pattern_last;
5187c478bd9Sstevel@tonic-gate 	glob_t *pglob;
519*cf58b254SGary Mills 	size_t *limit;
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	Char pathbuf[MAXPATHLEN];
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
5247c478bd9Sstevel@tonic-gate 	if (*pattern == EOS)
5257c478bd9Sstevel@tonic-gate 		return(0);
5267c478bd9Sstevel@tonic-gate 	return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
5277c478bd9Sstevel@tonic-gate 	    pathbuf, pathbuf+MAXPATHLEN-1,
528*cf58b254SGary Mills 	    pattern, pattern_last, pglob, limit));
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * The functions glob2 and glob3 are mutually recursive; there is one level
5337c478bd9Sstevel@tonic-gate  * of recursion for each segment in the pattern that contains one or more
5347c478bd9Sstevel@tonic-gate  * meta characters.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate static int
glob2(pathbuf,pathbuf_last,pathend,pathend_last,pattern,pattern_last,pglob,limit)5377c478bd9Sstevel@tonic-gate glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
538*cf58b254SGary Mills     pattern_last, pglob, limit)
5397c478bd9Sstevel@tonic-gate 	Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
5407c478bd9Sstevel@tonic-gate 	Char *pattern, *pattern_last;
5417c478bd9Sstevel@tonic-gate 	glob_t *pglob;
542*cf58b254SGary Mills 	size_t *limit;
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	struct stat sb;
5457c478bd9Sstevel@tonic-gate 	Char *p, *q;
5467c478bd9Sstevel@tonic-gate 	int anymeta;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Loop over pattern segments until end of pattern or until
5507c478bd9Sstevel@tonic-gate 	 * segment with meta character found.
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	for (anymeta = 0;;) {
5537c478bd9Sstevel@tonic-gate 		if (*pattern == EOS) {		/* End of pattern? */
5547c478bd9Sstevel@tonic-gate 			*pathend = EOS;
5557c478bd9Sstevel@tonic-gate 			if (g_lstat(pathbuf, &sb, pglob))
5567c478bd9Sstevel@tonic-gate 				return(0);
5577c478bd9Sstevel@tonic-gate 
558*cf58b254SGary Mills 			if ((pglob->gl_flags & GLOB_LIMIT) &&
559*cf58b254SGary Mills 			    limit[GLOB_INDEX_STAT]++ >= GLOB_LIMIT_STAT) {
560*cf58b254SGary Mills 				errno = 0;
561*cf58b254SGary Mills 				*pathend++ = SEP;
562*cf58b254SGary Mills 				*pathend = EOS;
563*cf58b254SGary Mills 				return GLOB_NOSPACE;
564*cf58b254SGary Mills 			}
565*cf58b254SGary Mills 
5667c478bd9Sstevel@tonic-gate 			if (((pglob->gl_flags & GLOB_MARK) &&
5677c478bd9Sstevel@tonic-gate 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
5687c478bd9Sstevel@tonic-gate 			    (S_ISLNK(sb.st_mode) &&
5697c478bd9Sstevel@tonic-gate 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
5707c478bd9Sstevel@tonic-gate 			    S_ISDIR(sb.st_mode)))) {
5717c478bd9Sstevel@tonic-gate 				if (pathend+1 > pathend_last)
5727c478bd9Sstevel@tonic-gate 					return (1);
5737c478bd9Sstevel@tonic-gate 				*pathend++ = SEP;
5747c478bd9Sstevel@tonic-gate 				*pathend = EOS;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			++pglob->gl_matchc;
577*cf58b254SGary Mills 			return(globextend(pathbuf, pglob, limit));
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		/* Find end of next segment, copy tentatively to pathend. */
5817c478bd9Sstevel@tonic-gate 		q = pathend;
5827c478bd9Sstevel@tonic-gate 		p = pattern;
5837c478bd9Sstevel@tonic-gate 		while (*p != EOS && *p != SEP) {
5847c478bd9Sstevel@tonic-gate 			if (ismeta(*p))
5857c478bd9Sstevel@tonic-gate 				anymeta = 1;
5867c478bd9Sstevel@tonic-gate 			if (q+1 > pathend_last)
5877c478bd9Sstevel@tonic-gate 				return (1);
5887c478bd9Sstevel@tonic-gate 			*q++ = *p++;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if (!anymeta) {		/* No expansion, do next segment. */
5927c478bd9Sstevel@tonic-gate 			pathend = q;
5937c478bd9Sstevel@tonic-gate 			pattern = p;
5947c478bd9Sstevel@tonic-gate 			while (*pattern == SEP) {
5957c478bd9Sstevel@tonic-gate 				if (pathend+1 > pathend_last)
5967c478bd9Sstevel@tonic-gate 					return (1);
5977c478bd9Sstevel@tonic-gate 				*pathend++ = *pattern++;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 		} else
6007c478bd9Sstevel@tonic-gate 			/* Need expansion, recurse. */
6017c478bd9Sstevel@tonic-gate 			return(glob3(pathbuf, pathbuf_last, pathend,
6027c478bd9Sstevel@tonic-gate 			    pathend_last, pattern, pattern_last,
603*cf58b254SGary Mills 			    p, pattern_last, pglob, limit));
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate static int
glob3(pathbuf,pathbuf_last,pathend,pathend_last,pattern,pattern_last,restpattern,restpattern_last,pglob,limit)6097c478bd9Sstevel@tonic-gate glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
610*cf58b254SGary Mills     restpattern, restpattern_last, pglob, limit)
6117c478bd9Sstevel@tonic-gate 	Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
6127c478bd9Sstevel@tonic-gate 	Char *pattern, *pattern_last, *restpattern, *restpattern_last;
6137c478bd9Sstevel@tonic-gate 	glob_t *pglob;
614*cf58b254SGary Mills 	size_t *limit;
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	register struct dirent *dp;
6177c478bd9Sstevel@tonic-gate 	DIR *dirp;
6187c478bd9Sstevel@tonic-gate 	int err;
6197c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * The readdirfunc declaration can't be prototyped, because it is
6237c478bd9Sstevel@tonic-gate 	 * assigned, below, to two functions which are prototyped in glob.h
6247c478bd9Sstevel@tonic-gate 	 * and dirent.h as taking pointers to differently typed opaque
6257c478bd9Sstevel@tonic-gate 	 * structures.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	struct dirent *(*readdirfunc)();
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (pathend > pathend_last)
6307c478bd9Sstevel@tonic-gate 		return (1);
6317c478bd9Sstevel@tonic-gate 	*pathend = EOS;
6327c478bd9Sstevel@tonic-gate 	errno = 0;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
6357c478bd9Sstevel@tonic-gate 		/* TODO: don't call for ENOENT or ENOTDIR? */
6367c478bd9Sstevel@tonic-gate 		if (pglob->gl_errfunc) {
6377c478bd9Sstevel@tonic-gate 			if (g_Ctoc(pathbuf, buf, sizeof(buf)))
6387c478bd9Sstevel@tonic-gate 				return(GLOB_ABORTED);
6397c478bd9Sstevel@tonic-gate 			if (pglob->gl_errfunc(buf, errno) ||
6407c478bd9Sstevel@tonic-gate 			    pglob->gl_flags & GLOB_ERR)
6417c478bd9Sstevel@tonic-gate 				return(GLOB_ABORTED);
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		return(0);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	err = 0;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/* Search directory for matching names. */
6497c478bd9Sstevel@tonic-gate 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6507c478bd9Sstevel@tonic-gate 		readdirfunc = pglob->gl_readdir;
6517c478bd9Sstevel@tonic-gate 	else
6527c478bd9Sstevel@tonic-gate 		readdirfunc = readdir;
6537c478bd9Sstevel@tonic-gate 	while ((dp = (*readdirfunc)(dirp))) {
6547c478bd9Sstevel@tonic-gate 		register u_char *sc;
6557c478bd9Sstevel@tonic-gate 		register Char *dc;
6567c478bd9Sstevel@tonic-gate 
657*cf58b254SGary Mills 		if ((pglob->gl_flags & GLOB_LIMIT) &&
658*cf58b254SGary Mills 		    limit[GLOB_INDEX_READDIR]++ >= GLOB_LIMIT_READDIR) {
659*cf58b254SGary Mills 			errno = 0;
660*cf58b254SGary Mills 			*pathend++ = SEP;
661*cf58b254SGary Mills 			*pathend = EOS;
662*cf58b254SGary Mills 			return GLOB_NOSPACE;
663*cf58b254SGary Mills 		}
664*cf58b254SGary Mills 
6657c478bd9Sstevel@tonic-gate 		/* Initial DOT must be matched literally. */
6667c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == DOT && *pattern != DOT)
6677c478bd9Sstevel@tonic-gate 			continue;
6687c478bd9Sstevel@tonic-gate 		dc = pathend;
6697c478bd9Sstevel@tonic-gate 		sc = (u_char *) dp->d_name;
6707c478bd9Sstevel@tonic-gate 		while (dc < pathend_last && (*dc++ = *sc++) != EOS)
6717c478bd9Sstevel@tonic-gate 			;
6727c478bd9Sstevel@tonic-gate 		if (dc >= pathend_last) {
6737c478bd9Sstevel@tonic-gate 			*dc = EOS;
6747c478bd9Sstevel@tonic-gate 			err = 1;
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		if (!match(pathend, pattern, restpattern)) {
6797c478bd9Sstevel@tonic-gate 			*pathend = EOS;
6807c478bd9Sstevel@tonic-gate 			continue;
6817c478bd9Sstevel@tonic-gate 		}
6827c478bd9Sstevel@tonic-gate 		err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
683*cf58b254SGary Mills 		    restpattern, restpattern_last, pglob, limit);
6847c478bd9Sstevel@tonic-gate 		if (err)
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6897c478bd9Sstevel@tonic-gate 		(*pglob->gl_closedir)(dirp);
6907c478bd9Sstevel@tonic-gate 	else
6917c478bd9Sstevel@tonic-gate 		closedir(dirp);
6927c478bd9Sstevel@tonic-gate 	return(err);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
6987c478bd9Sstevel@tonic-gate  * add the new item, and update gl_pathc.
6997c478bd9Sstevel@tonic-gate  *
7007c478bd9Sstevel@tonic-gate  * This assumes the BSD realloc, which only copies the block when its size
7017c478bd9Sstevel@tonic-gate  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
7027c478bd9Sstevel@tonic-gate  * behavior.
7037c478bd9Sstevel@tonic-gate  *
7047c478bd9Sstevel@tonic-gate  * Return 0 if new item added, error code if memory couldn't be allocated.
7057c478bd9Sstevel@tonic-gate  *
7067c478bd9Sstevel@tonic-gate  * Invariant of the glob_t structure:
7077c478bd9Sstevel@tonic-gate  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
7087c478bd9Sstevel@tonic-gate  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate static int
globextend(path,pglob,limit)711*cf58b254SGary Mills globextend(path, pglob, limit)
7127c478bd9Sstevel@tonic-gate 	const Char *path;
7137c478bd9Sstevel@tonic-gate 	glob_t *pglob;
714*cf58b254SGary Mills 	size_t *limit;
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	register char **pathv;
7177c478bd9Sstevel@tonic-gate 	register int i;
7187c478bd9Sstevel@tonic-gate 	u_int newsize, len;
7197c478bd9Sstevel@tonic-gate 	char *copy;
7207c478bd9Sstevel@tonic-gate 	const Char *p;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
7237c478bd9Sstevel@tonic-gate 	pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
7247c478bd9Sstevel@tonic-gate 	    malloc(newsize);
7257c478bd9Sstevel@tonic-gate 	if (pathv == NULL) {
7267c478bd9Sstevel@tonic-gate 		if (pglob->gl_pathv) {
7277c478bd9Sstevel@tonic-gate 			free(pglob->gl_pathv);
7287c478bd9Sstevel@tonic-gate 			pglob->gl_pathv = NULL;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		return(GLOB_NOSPACE);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
7347c478bd9Sstevel@tonic-gate 		/* first time around -- clear initial gl_offs items */
7357c478bd9Sstevel@tonic-gate 		pathv += pglob->gl_offs;
7367c478bd9Sstevel@tonic-gate 		for (i = pglob->gl_offs; --i >= 0; )
7377c478bd9Sstevel@tonic-gate 			*--pathv = NULL;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	pglob->gl_pathv = pathv;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	for (p = path; *p++;)
7427c478bd9Sstevel@tonic-gate 		;
7437c478bd9Sstevel@tonic-gate 	len = (size_t)(p - path);
744*cf58b254SGary Mills 	limit[GLOB_INDEX_MALLOC] += len;
7457c478bd9Sstevel@tonic-gate 	if ((copy = malloc(len)) != NULL) {
7467c478bd9Sstevel@tonic-gate 		if (g_Ctoc(path, copy, len)) {
7477c478bd9Sstevel@tonic-gate 			free(copy);
7487c478bd9Sstevel@tonic-gate 			return(GLOB_NOSPACE);
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
7537c478bd9Sstevel@tonic-gate 	if ((pglob->gl_flags & GLOB_LIMIT) &&
754*cf58b254SGary Mills 		(newsize + limit[GLOB_INDEX_MALLOC]) >= GLOB_LIMIT_MALLOC) {
7557c478bd9Sstevel@tonic-gate 		errno = 0;
756*cf58b254SGary Mills 		return GLOB_NOSPACE;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	return(copy == NULL ? GLOB_NOSPACE : 0);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*
7647c478bd9Sstevel@tonic-gate  * pattern matching function for filenames.  Each occurrence of the *
7657c478bd9Sstevel@tonic-gate  * pattern causes a recursion level.
7667c478bd9Sstevel@tonic-gate  */
7677c478bd9Sstevel@tonic-gate static int
match(name,pat,patend)7687c478bd9Sstevel@tonic-gate match(name, pat, patend)
7697c478bd9Sstevel@tonic-gate 	register Char *name, *pat, *patend;
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	int ok, negate_range;
7727c478bd9Sstevel@tonic-gate 	Char c, k;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	while (pat < patend) {
7757c478bd9Sstevel@tonic-gate 		c = *pat++;
7767c478bd9Sstevel@tonic-gate 		switch (c & M_MASK) {
7777c478bd9Sstevel@tonic-gate 		case M_ALL:
7787c478bd9Sstevel@tonic-gate 			if (pat == patend)
7797c478bd9Sstevel@tonic-gate 				return(1);
7807c478bd9Sstevel@tonic-gate 			do
7817c478bd9Sstevel@tonic-gate 			    if (match(name, pat, patend))
7827c478bd9Sstevel@tonic-gate 				    return(1);
7837c478bd9Sstevel@tonic-gate 			while (*name++ != EOS)
7847c478bd9Sstevel@tonic-gate 				;
7857c478bd9Sstevel@tonic-gate 			return(0);
7867c478bd9Sstevel@tonic-gate 		case M_ONE:
7877c478bd9Sstevel@tonic-gate 			if (*name++ == EOS)
7887c478bd9Sstevel@tonic-gate 				return(0);
7897c478bd9Sstevel@tonic-gate 			break;
7907c478bd9Sstevel@tonic-gate 		case M_SET:
7917c478bd9Sstevel@tonic-gate 			ok = 0;
7927c478bd9Sstevel@tonic-gate 			if ((k = *name++) == EOS)
7937c478bd9Sstevel@tonic-gate 				return(0);
7947c478bd9Sstevel@tonic-gate 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
7957c478bd9Sstevel@tonic-gate 				++pat;
7967c478bd9Sstevel@tonic-gate 			while (((c = *pat++) & M_MASK) != M_END)
7977c478bd9Sstevel@tonic-gate 				if ((*pat & M_MASK) == M_RNG) {
7987c478bd9Sstevel@tonic-gate 					if (c <= k && k <= pat[1])
7997c478bd9Sstevel@tonic-gate 						ok = 1;
8007c478bd9Sstevel@tonic-gate 					pat += 2;
8017c478bd9Sstevel@tonic-gate 				} else if (c == k)
8027c478bd9Sstevel@tonic-gate 					ok = 1;
8037c478bd9Sstevel@tonic-gate 			if (ok == negate_range)
8047c478bd9Sstevel@tonic-gate 				return(0);
8057c478bd9Sstevel@tonic-gate 			break;
8067c478bd9Sstevel@tonic-gate 		default:
8077c478bd9Sstevel@tonic-gate 			if (*name++ != c)
8087c478bd9Sstevel@tonic-gate 				return(0);
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	return(*name == EOS);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /* Free allocated data belonging to a glob_t structure. */
8167c478bd9Sstevel@tonic-gate void
globfree(pglob)8177c478bd9Sstevel@tonic-gate globfree(pglob)
8187c478bd9Sstevel@tonic-gate 	glob_t *pglob;
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	register int i;
8217c478bd9Sstevel@tonic-gate 	register char **pp;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (pglob->gl_pathv != NULL) {
8247c478bd9Sstevel@tonic-gate 		pp = pglob->gl_pathv + pglob->gl_offs;
8257c478bd9Sstevel@tonic-gate 		for (i = pglob->gl_pathc; i--; ++pp)
8267c478bd9Sstevel@tonic-gate 			if (*pp)
8277c478bd9Sstevel@tonic-gate 				free(*pp);
8287c478bd9Sstevel@tonic-gate 		free(pglob->gl_pathv);
8297c478bd9Sstevel@tonic-gate 		pglob->gl_pathv = NULL;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate static DIR *
g_opendir(str,pglob)8347c478bd9Sstevel@tonic-gate g_opendir(str, pglob)
8357c478bd9Sstevel@tonic-gate 	register Char *str;
8367c478bd9Sstevel@tonic-gate 	glob_t *pglob;
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (!*str)
8417c478bd9Sstevel@tonic-gate 		strlcpy(buf, ".", sizeof buf);
8427c478bd9Sstevel@tonic-gate 	else {
8437c478bd9Sstevel@tonic-gate 		if (g_Ctoc(str, buf, sizeof(buf)))
8447c478bd9Sstevel@tonic-gate 			return(NULL);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8487c478bd9Sstevel@tonic-gate 		return((*pglob->gl_opendir)(buf));
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	return(opendir(buf));
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate static int
g_lstat(fn,sb,pglob)8547c478bd9Sstevel@tonic-gate g_lstat(fn, sb, pglob)
8557c478bd9Sstevel@tonic-gate 	register Char *fn;
8567c478bd9Sstevel@tonic-gate 	struct stat *sb;
8577c478bd9Sstevel@tonic-gate 	glob_t *pglob;
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (g_Ctoc(fn, buf, sizeof(buf)))
8627c478bd9Sstevel@tonic-gate 		return(-1);
8637c478bd9Sstevel@tonic-gate 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8647c478bd9Sstevel@tonic-gate 		return((*pglob->gl_lstat)(buf, sb));
8657c478bd9Sstevel@tonic-gate 	return(lstat(buf, sb));
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate static int
g_stat(fn,sb,pglob)8697c478bd9Sstevel@tonic-gate g_stat(fn, sb, pglob)
8707c478bd9Sstevel@tonic-gate 	register Char *fn;
8717c478bd9Sstevel@tonic-gate 	struct stat *sb;
8727c478bd9Sstevel@tonic-gate 	glob_t *pglob;
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (g_Ctoc(fn, buf, sizeof(buf)))
8777c478bd9Sstevel@tonic-gate 		return(-1);
8787c478bd9Sstevel@tonic-gate 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8797c478bd9Sstevel@tonic-gate 		return((*pglob->gl_stat)(buf, sb));
8807c478bd9Sstevel@tonic-gate 	return(stat(buf, sb));
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate static Char *
g_strchr(str,ch)8847c478bd9Sstevel@tonic-gate g_strchr(str, ch)
8857c478bd9Sstevel@tonic-gate 	Char *str;
8867c478bd9Sstevel@tonic-gate 	int ch;
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	do {
8897c478bd9Sstevel@tonic-gate 		if (*str == ch)
8907c478bd9Sstevel@tonic-gate 			return (str);
8917c478bd9Sstevel@tonic-gate 	} while (*str++);
8927c478bd9Sstevel@tonic-gate 	return (NULL);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate static int
g_Ctoc(str,buf,len)8967c478bd9Sstevel@tonic-gate g_Ctoc(str, buf, len)
8977c478bd9Sstevel@tonic-gate 	register const Char *str;
8987c478bd9Sstevel@tonic-gate 	char *buf;
8997c478bd9Sstevel@tonic-gate 	u_int len;
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	while (len--) {
9037c478bd9Sstevel@tonic-gate 		if ((*buf++ = *str++) == EOS)
9047c478bd9Sstevel@tonic-gate 			return (0);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	return (1);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate #ifdef DEBUG
9107c478bd9Sstevel@tonic-gate static void
qprintf(str,s)9117c478bd9Sstevel@tonic-gate qprintf(str, s)
9127c478bd9Sstevel@tonic-gate 	const char *str;
9137c478bd9Sstevel@tonic-gate 	register Char *s;
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	register Char *p;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	(void)printf("%s:\n", str);
9187c478bd9Sstevel@tonic-gate 	for (p = s; *p; p++)
9197c478bd9Sstevel@tonic-gate 		(void)printf("%c", CHAR(*p));
9207c478bd9Sstevel@tonic-gate 	(void)printf("\n");
9217c478bd9Sstevel@tonic-gate 	for (p = s; *p; p++)
9227c478bd9Sstevel@tonic-gate 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
9237c478bd9Sstevel@tonic-gate 	(void)printf("\n");
9247c478bd9Sstevel@tonic-gate 	for (p = s; *p; p++)
9257c478bd9Sstevel@tonic-gate 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
9267c478bd9Sstevel@tonic-gate 	(void)printf("\n");
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate #endif
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
9317c478bd9Sstevel@tonic-gate           !defined(GLOB_HAS_GL_MATCHC) */
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
934