static const Char *	 globtilde __P((const Char *, Char *, glob_t *));
static const Char *	globtilde __P((const Char *, Char *, size_t, glob_t *));
static const Char *	 globtilde __P((const Char *, Char *, glob_t *));static int	 globexp1 __P((const Char *, glob_t *));static int	 globexp2 __P((const Char *, const Char *, glob_t *, int *));static int	 match __P((Char *, Char *, Char *));#ifdef DEBUGstatic void	 qprintf __P((const char *, Char *));#endifintglob(pattern, flags, errfunc, pglob)	const char *pattern;	int flags, (*errfunc) __P((const char *, int));	glob_t *pglob;{	const u_char *patnext;	int c;	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];	patnext = (u_char *) pattern;	if (!(flags & GLOB_APPEND)) {		pglob->gl_pathc = 0;		pglob->gl_pathv = NULL;		if (!(flags & GLOB_DOOFFS))			pglob->gl_offs = 0;	}	pglob->gl_flags = flags & ~GLOB_MAGCHAR;	pglob->gl_errfunc = errfunc;	pglob->gl_matchc = 0;	bufnext = patbuf;	bufend = bufnext + MAXPATHLEN;	if (flags & GLOB_QUOTE) {		/* Protect the quoted characters. */		while (bufnext < bufend && (c = *patnext++) != EOS) 			if (c == QUOTE) {				if ((c = *patnext++) == EOS) {					c = QUOTE;					--patnext;				}				*bufnext++ = c | M_PROTECT;			}			else				*bufnext++ = c;	}	else 	    while (bufnext < bufend && (c = *patnext++) != EOS) 		    *bufnext++ = c;	*bufnext = EOS;	if (flags & GLOB_BRACE)	    return globexp1(patbuf, pglob);	else	    return glob0(patbuf, pglob);}/* * Expand recursively a glob {} pattern. When there is no more expansion * invoke the standard globbing routine to glob the rest of the magic * characters */static int globexp1(pattern, pglob)	const Char *pattern;	glob_t *pglob;{	const Char* ptr = pattern;	int rv;	/* Protect a single {}, for find(1), like csh */	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)		return glob0(pattern, pglob);	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)		if (!globexp2(ptr, pattern, pglob, &rv))			return rv;	return glob0(pattern, pglob);}/* * Recursive brace globbing helper. Tries to expand a single brace. * If it succeeds then it invokes globexp1 with the new pattern. * If it fails then it tries to glob the rest of the pattern and returns. */static int globexp2(ptr, pattern, pglob, rv)	const Char *ptr, *pattern;	glob_t *pglob;	int *rv;{	int     i;	Char   *lm, *ls;	const Char *pe, *pm, *pl;	Char    patbuf[MAXPATHLEN + 1];	/* copy part up to the brace */	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)		continue;	ls = lm;	/* Find the balanced brace */	for (i = 0, pe = ++ptr; *pe; pe++)		if (*pe == LBRACKET) {			/* Ignore everything between [] */			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)				continue;			if (*pe == EOS) {				/* 				 * We could not find a matching RBRACKET.				 * Ignore and just look for RBRACE				 */				pe = pm;			}		}		else if (*pe == LBRACE)			i++;		else if (*pe == RBRACE) {			if (i == 0)				break;			i--;		}	/* Non matching braces; just glob the pattern */	if (i != 0 || *pe == EOS) {		*rv = glob0(patbuf, pglob);		return 0;	}	for (i = 0, pl = pm = ptr; pm <= pe; pm++)		switch (*pm) {		case LBRACKET:			/* Ignore everything between [] */			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)				continue;			if (*pm == EOS) {				/* 				 * We could not find a matching RBRACKET.				 * Ignore and just look for RBRACE				 */				pm = pl;			}			break;		case LBRACE:			i++;			break;		case RBRACE:			if (i) {			    i--;			    break;			}			/* FALLTHROUGH */		case COMMA:			if (i && *pm == COMMA)				break;			else {				/* Append the current string */				for (lm = ls; (pl < pm); *lm++ = *pl++)					continue;				/* 				 * Append the rest of the pattern after the				 * closing brace				 */				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)					continue;				/* Expand the current pattern */#ifdef DEBUG				qprintf("globexp2:", patbuf);#endif				*rv = globexp1(patbuf, pglob);				/* move after the comma, to the next string */				pl = pm + 1;			}			break;		default:			break;		}	*rv = 0;	return 0;}/* * expand tilde from the passwd file. */static const Char *globtilde(pattern, patbuf, pglob)	const Char *pattern;	Char *patbuf;	glob_t *pglob;{	struct passwd *pwd;	char *h;	const Char *p;	Char *b;	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))		return pattern;	/* Copy up to the end of the string or / */	for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH; 	     *h++ = *p++)		continue;	*h = EOS;	if (((char *) patbuf)[0] == EOS) {		/* 		 * handle a plain ~ or ~/ by expanding $HOME 		 * first and then trying the password file		 */		if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {			if ((pwd = getpwuid(getuid())) == NULL)				return pattern;			else				h = pwd->pw_dir;		} else {			if (strlen(h) >= MAXPATHLEN-1)				return pattern;		}	}	else {		/*		 * Expand a ~user		 */		if ((pwd = getpwnam((char*) patbuf)) == NULL)			return pattern;		else			h = pwd->pw_dir;	}	/* Copy the home directory */	for (b = patbuf; *h; *b++ = *h++)		continue;		/* Append the rest of the pattern */	while ((*b++ = *p++) != EOS)		continue;	return patbuf;}