glob.c revision 19276
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Guido van Rossum.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
381573Srgrimesstatic char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes/*
421573Srgrimes * glob(3) -- a superset of the one defined in POSIX 1003.2.
431573Srgrimes *
441573Srgrimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
451573Srgrimes *
461573Srgrimes * Optional extra services, controlled by flags not defined by POSIX:
471573Srgrimes *
481573Srgrimes * GLOB_QUOTE:
491573Srgrimes *	Escaping convention: \ inhibits any special meaning the following
501573Srgrimes *	character might have (except \ at end of string is retained).
511573Srgrimes * GLOB_MAGCHAR:
521573Srgrimes *	Set in gl_flags if pattern contained a globbing character.
531573Srgrimes * GLOB_NOMAGIC:
541573Srgrimes *	Same as GLOB_NOCHECK, but it will only append pattern if it did
551573Srgrimes *	not contain any magic characters.  [Used in csh style globbing]
561573Srgrimes * GLOB_ALTDIRFUNC:
571573Srgrimes *	Use alternately specified directory access functions.
581573Srgrimes * GLOB_TILDE:
591573Srgrimes *	expand ~user/foo to the /home/dir/of/user/foo
601573Srgrimes * GLOB_BRACE:
618870Srgrimes *	expand {1,2}{a,b} to 1a 1b 2a 2b
621573Srgrimes * gl_matchc:
631573Srgrimes *	Number of matches in the current invocation of glob.
641573Srgrimes */
651573Srgrimes
661573Srgrimes#include <sys/param.h>
671573Srgrimes#include <sys/stat.h>
681573Srgrimes
691573Srgrimes#include <ctype.h>
701573Srgrimes#include <dirent.h>
711573Srgrimes#include <errno.h>
721573Srgrimes#include <glob.h>
731573Srgrimes#include <pwd.h>
741573Srgrimes#include <stdio.h>
751573Srgrimes#include <stdlib.h>
761573Srgrimes#include <string.h>
771573Srgrimes#include <unistd.h>
781573Srgrimes
7919276Sache#include "collate.h"
8019276Sache
811573Srgrimes#define	DOLLAR		'$'
821573Srgrimes#define	DOT		'.'
831573Srgrimes#define	EOS		'\0'
841573Srgrimes#define	LBRACKET	'['
851573Srgrimes#define	NOT		'!'
861573Srgrimes#define	QUESTION	'?'
871573Srgrimes#define	QUOTE		'\\'
881573Srgrimes#define	RANGE		'-'
891573Srgrimes#define	RBRACKET	']'
901573Srgrimes#define	SEP		'/'
911573Srgrimes#define	STAR		'*'
921573Srgrimes#define	TILDE		'~'
931573Srgrimes#define	UNDERSCORE	'_'
941573Srgrimes#define	LBRACE		'{'
951573Srgrimes#define	RBRACE		'}'
961573Srgrimes#define	SLASH		'/'
971573Srgrimes#define	COMMA		','
981573Srgrimes
991573Srgrimes#ifndef DEBUG
1001573Srgrimes
1011573Srgrimes#define	M_QUOTE		0x8000
1021573Srgrimes#define	M_PROTECT	0x4000
1031573Srgrimes#define	M_MASK		0xffff
1041573Srgrimes#define	M_ASCII		0x00ff
1051573Srgrimes
1061573Srgrimestypedef u_short Char;
1071573Srgrimes
1081573Srgrimes#else
1091573Srgrimes
1101573Srgrimes#define	M_QUOTE		0x80
1111573Srgrimes#define	M_PROTECT	0x40
1121573Srgrimes#define	M_MASK		0xff
1131573Srgrimes#define	M_ASCII		0x7f
1141573Srgrimes
1151573Srgrimestypedef char Char;
1161573Srgrimes
1171573Srgrimes#endif
1181573Srgrimes
1191573Srgrimes
1201573Srgrimes#define	CHAR(c)		((Char)((c)&M_ASCII))
1211573Srgrimes#define	META(c)		((Char)((c)|M_QUOTE))
1221573Srgrimes#define	M_ALL		META('*')
1231573Srgrimes#define	M_END		META(']')
1241573Srgrimes#define	M_NOT		META('!')
1251573Srgrimes#define	M_ONE		META('?')
1261573Srgrimes#define	M_RNG		META('-')
1271573Srgrimes#define	M_SET		META('[')
1281573Srgrimes#define	ismeta(c)	(((c)&M_QUOTE) != 0)
1291573Srgrimes
1301573Srgrimes
1311573Srgrimesstatic int	 compare __P((const void *, const void *));
1321573Srgrimesstatic void	 g_Ctoc __P((const Char *, char *));
1331573Srgrimesstatic int	 g_lstat __P((Char *, struct stat *, glob_t *));
1341573Srgrimesstatic DIR	*g_opendir __P((Char *, glob_t *));
1351573Srgrimesstatic Char	*g_strchr __P((Char *, int));
1361573Srgrimes#ifdef notdef
1371573Srgrimesstatic Char	*g_strcat __P((Char *, const Char *));
1381573Srgrimes#endif
1391573Srgrimesstatic int	 g_stat __P((Char *, struct stat *, glob_t *));
1401573Srgrimesstatic int	 glob0 __P((const Char *, glob_t *));
1411573Srgrimesstatic int	 glob1 __P((Char *, glob_t *));
1421573Srgrimesstatic int	 glob2 __P((Char *, Char *, Char *, glob_t *));
1431573Srgrimesstatic int	 glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
1441573Srgrimesstatic int	 globextend __P((const Char *, glob_t *));
1451573Srgrimesstatic const Char *	 globtilde __P((const Char *, Char *, glob_t *));
1461573Srgrimesstatic int	 globexp1 __P((const Char *, glob_t *));
1471573Srgrimesstatic int	 globexp2 __P((const Char *, const Char *, glob_t *, int *));
1481573Srgrimesstatic int	 match __P((Char *, Char *, Char *));
1491573Srgrimes#ifdef DEBUG
1501573Srgrimesstatic void	 qprintf __P((const char *, Char *));
1511573Srgrimes#endif
1521573Srgrimes
1531573Srgrimesint
1541573Srgrimesglob(pattern, flags, errfunc, pglob)
1551573Srgrimes	const char *pattern;
1561573Srgrimes	int flags, (*errfunc) __P((const char *, int));
1571573Srgrimes	glob_t *pglob;
1581573Srgrimes{
1591573Srgrimes	const u_char *patnext;
1601573Srgrimes	int c;
1611573Srgrimes	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
1621573Srgrimes
1631573Srgrimes	patnext = (u_char *) pattern;
1641573Srgrimes	if (!(flags & GLOB_APPEND)) {
1651573Srgrimes		pglob->gl_pathc = 0;
1661573Srgrimes		pglob->gl_pathv = NULL;
1671573Srgrimes		if (!(flags & GLOB_DOOFFS))
1681573Srgrimes			pglob->gl_offs = 0;
1691573Srgrimes	}
1701573Srgrimes	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
1711573Srgrimes	pglob->gl_errfunc = errfunc;
1721573Srgrimes	pglob->gl_matchc = 0;
1731573Srgrimes
1741573Srgrimes	bufnext = patbuf;
1751573Srgrimes	bufend = bufnext + MAXPATHLEN;
1761573Srgrimes	if (flags & GLOB_QUOTE) {
1771573Srgrimes		/* Protect the quoted characters. */
1788870Srgrimes		while (bufnext < bufend && (c = *patnext++) != EOS)
1791573Srgrimes			if (c == QUOTE) {
1801573Srgrimes				if ((c = *patnext++) == EOS) {
1811573Srgrimes					c = QUOTE;
1821573Srgrimes					--patnext;
1831573Srgrimes				}
1841573Srgrimes				*bufnext++ = c | M_PROTECT;
1851573Srgrimes			}
1861573Srgrimes			else
1871573Srgrimes				*bufnext++ = c;
1881573Srgrimes	}
1898870Srgrimes	else
1908870Srgrimes	    while (bufnext < bufend && (c = *patnext++) != EOS)
1911573Srgrimes		    *bufnext++ = c;
1921573Srgrimes	*bufnext = EOS;
1931573Srgrimes
1941573Srgrimes	if (flags & GLOB_BRACE)
1951573Srgrimes	    return globexp1(patbuf, pglob);
1961573Srgrimes	else
1971573Srgrimes	    return glob0(patbuf, pglob);
1981573Srgrimes}
1991573Srgrimes
2001573Srgrimes/*
2011573Srgrimes * Expand recursively a glob {} pattern. When there is no more expansion
2021573Srgrimes * invoke the standard globbing routine to glob the rest of the magic
2031573Srgrimes * characters
2041573Srgrimes */
2051573Srgrimesstatic int globexp1(pattern, pglob)
2061573Srgrimes	const Char *pattern;
2071573Srgrimes	glob_t *pglob;
2081573Srgrimes{
2091573Srgrimes	const Char* ptr = pattern;
2101573Srgrimes	int rv;
2111573Srgrimes
2121573Srgrimes	/* Protect a single {}, for find(1), like csh */
2131573Srgrimes	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
2141573Srgrimes		return glob0(pattern, pglob);
2151573Srgrimes
2161573Srgrimes	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
2171573Srgrimes		if (!globexp2(ptr, pattern, pglob, &rv))
2181573Srgrimes			return rv;
2191573Srgrimes
2201573Srgrimes	return glob0(pattern, pglob);
2211573Srgrimes}
2221573Srgrimes
2231573Srgrimes
2241573Srgrimes/*
2251573Srgrimes * Recursive brace globbing helper. Tries to expand a single brace.
2261573Srgrimes * If it succeeds then it invokes globexp1 with the new pattern.
2271573Srgrimes * If it fails then it tries to glob the rest of the pattern and returns.
2281573Srgrimes */
2291573Srgrimesstatic int globexp2(ptr, pattern, pglob, rv)
2301573Srgrimes	const Char *ptr, *pattern;
2311573Srgrimes	glob_t *pglob;
2321573Srgrimes	int *rv;
2331573Srgrimes{
2341573Srgrimes	int     i;
2351573Srgrimes	Char   *lm, *ls;
2361573Srgrimes	const Char *pe, *pm, *pl;
2371573Srgrimes	Char    patbuf[MAXPATHLEN + 1];
2381573Srgrimes
2391573Srgrimes	/* copy part up to the brace */
2401573Srgrimes	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
2411573Srgrimes		continue;
2421573Srgrimes	ls = lm;
2431573Srgrimes
2441573Srgrimes	/* Find the balanced brace */
2451573Srgrimes	for (i = 0, pe = ++ptr; *pe; pe++)
2461573Srgrimes		if (*pe == LBRACKET) {
2471573Srgrimes			/* Ignore everything between [] */
2481573Srgrimes			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
2491573Srgrimes				continue;
2501573Srgrimes			if (*pe == EOS) {
2518870Srgrimes				/*
2521573Srgrimes				 * We could not find a matching RBRACKET.
2531573Srgrimes				 * Ignore and just look for RBRACE
2541573Srgrimes				 */
2551573Srgrimes				pe = pm;
2561573Srgrimes			}
2571573Srgrimes		}
2581573Srgrimes		else if (*pe == LBRACE)
2591573Srgrimes			i++;
2601573Srgrimes		else if (*pe == RBRACE) {
2611573Srgrimes			if (i == 0)
2621573Srgrimes				break;
2631573Srgrimes			i--;
2641573Srgrimes		}
2651573Srgrimes
2661573Srgrimes	/* Non matching braces; just glob the pattern */
2671573Srgrimes	if (i != 0 || *pe == EOS) {
2681573Srgrimes		*rv = glob0(patbuf, pglob);
2691573Srgrimes		return 0;
2701573Srgrimes	}
2711573Srgrimes
2721573Srgrimes	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
2731573Srgrimes		switch (*pm) {
2741573Srgrimes		case LBRACKET:
2751573Srgrimes			/* Ignore everything between [] */
2761573Srgrimes			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
2771573Srgrimes				continue;
2781573Srgrimes			if (*pm == EOS) {
2798870Srgrimes				/*
2801573Srgrimes				 * We could not find a matching RBRACKET.
2811573Srgrimes				 * Ignore and just look for RBRACE
2821573Srgrimes				 */
2831573Srgrimes				pm = pl;
2841573Srgrimes			}
2851573Srgrimes			break;
2861573Srgrimes
2871573Srgrimes		case LBRACE:
2881573Srgrimes			i++;
2891573Srgrimes			break;
2901573Srgrimes
2911573Srgrimes		case RBRACE:
2921573Srgrimes			if (i) {
2931573Srgrimes			    i--;
2941573Srgrimes			    break;
2951573Srgrimes			}
2961573Srgrimes			/* FALLTHROUGH */
2971573Srgrimes		case COMMA:
2981573Srgrimes			if (i && *pm == COMMA)
2991573Srgrimes				break;
3001573Srgrimes			else {
3011573Srgrimes				/* Append the current string */
3021573Srgrimes				for (lm = ls; (pl < pm); *lm++ = *pl++)
3031573Srgrimes					continue;
3048870Srgrimes				/*
3051573Srgrimes				 * Append the rest of the pattern after the
3061573Srgrimes				 * closing brace
3071573Srgrimes				 */
3081573Srgrimes				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
3091573Srgrimes					continue;
3101573Srgrimes
3111573Srgrimes				/* Expand the current pattern */
3121573Srgrimes#ifdef DEBUG
3131573Srgrimes				qprintf("globexp2:", patbuf);
3141573Srgrimes#endif
3151573Srgrimes				*rv = globexp1(patbuf, pglob);
3161573Srgrimes
3171573Srgrimes				/* move after the comma, to the next string */
3181573Srgrimes				pl = pm + 1;
3191573Srgrimes			}
3201573Srgrimes			break;
3211573Srgrimes
3221573Srgrimes		default:
3231573Srgrimes			break;
3241573Srgrimes		}
3251573Srgrimes	*rv = 0;
3261573Srgrimes	return 0;
3271573Srgrimes}
3281573Srgrimes
3291573Srgrimes
3301573Srgrimes
3311573Srgrimes/*
3321573Srgrimes * expand tilde from the passwd file.
3331573Srgrimes */
3341573Srgrimesstatic const Char *
3351573Srgrimesglobtilde(pattern, patbuf, pglob)
3361573Srgrimes	const Char *pattern;
3371573Srgrimes	Char *patbuf;
3381573Srgrimes	glob_t *pglob;
3391573Srgrimes{
3401573Srgrimes	struct passwd *pwd;
3411573Srgrimes	char *h;
3421573Srgrimes	const Char *p;
3431573Srgrimes	Char *b;
3441573Srgrimes
3451573Srgrimes	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
3461573Srgrimes		return pattern;
3471573Srgrimes
3481573Srgrimes	/* Copy up to the end of the string or / */
3498870Srgrimes	for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH;
3501573Srgrimes	     *h++ = *p++)
3511573Srgrimes		continue;
3521573Srgrimes
3531573Srgrimes	*h = EOS;
3541573Srgrimes
3551573Srgrimes	if (((char *) patbuf)[0] == EOS) {
3568870Srgrimes		/*
3578870Srgrimes		 * handle a plain ~ or ~/ by expanding $HOME
3581573Srgrimes		 * first and then trying the password file
3591573Srgrimes		 */
3601573Srgrimes		if ((h = getenv("HOME")) == NULL) {
3611573Srgrimes			if ((pwd = getpwuid(getuid())) == NULL)
3621573Srgrimes				return pattern;
3631573Srgrimes			else
3641573Srgrimes				h = pwd->pw_dir;
3651573Srgrimes		}
3661573Srgrimes	}
3671573Srgrimes	else {
3681573Srgrimes		/*
3691573Srgrimes		 * Expand a ~user
3701573Srgrimes		 */
3711573Srgrimes		if ((pwd = getpwnam((char*) patbuf)) == NULL)
3721573Srgrimes			return pattern;
3731573Srgrimes		else
3741573Srgrimes			h = pwd->pw_dir;
3751573Srgrimes	}
3761573Srgrimes
3771573Srgrimes	/* Copy the home directory */
3781573Srgrimes	for (b = patbuf; *h; *b++ = *h++)
3791573Srgrimes		continue;
3808870Srgrimes
3811573Srgrimes	/* Append the rest of the pattern */
3821573Srgrimes	while ((*b++ = *p++) != EOS)
3831573Srgrimes		continue;
3841573Srgrimes
3851573Srgrimes	return patbuf;
3861573Srgrimes}
3871573Srgrimes
3888870Srgrimes
3891573Srgrimes/*
3901573Srgrimes * The main glob() routine: compiles the pattern (optionally processing
3911573Srgrimes * quotes), calls glob1() to do the real pattern matching, and finally
3921573Srgrimes * sorts the list (unless unsorted operation is requested).  Returns 0
3931573Srgrimes * if things went well, nonzero if errors occurred.  It is not an error
3941573Srgrimes * to find no matches.
3951573Srgrimes */
3961573Srgrimesstatic int
3971573Srgrimesglob0(pattern, pglob)
3981573Srgrimes	const Char *pattern;
3991573Srgrimes	glob_t *pglob;
4001573Srgrimes{
4011573Srgrimes	const Char *qpatnext;
4021573Srgrimes	int c, err, oldpathc;
4031573Srgrimes	Char *bufnext, patbuf[MAXPATHLEN+1];
4041573Srgrimes
4051573Srgrimes	qpatnext = globtilde(pattern, patbuf, pglob);
4061573Srgrimes	oldpathc = pglob->gl_pathc;
4071573Srgrimes	bufnext = patbuf;
4081573Srgrimes
4091573Srgrimes	/* We don't need to check for buffer overflow any more. */
4101573Srgrimes	while ((c = *qpatnext++) != EOS) {
4111573Srgrimes		switch (c) {
4121573Srgrimes		case LBRACKET:
4131573Srgrimes			c = *qpatnext;
4141573Srgrimes			if (c == NOT)
4151573Srgrimes				++qpatnext;
4161573Srgrimes			if (*qpatnext == EOS ||
4171573Srgrimes			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
4181573Srgrimes				*bufnext++ = LBRACKET;
4191573Srgrimes				if (c == NOT)
4201573Srgrimes					--qpatnext;
4211573Srgrimes				break;
4221573Srgrimes			}
4231573Srgrimes			*bufnext++ = M_SET;
4241573Srgrimes			if (c == NOT)
4251573Srgrimes				*bufnext++ = M_NOT;
4261573Srgrimes			c = *qpatnext++;
4271573Srgrimes			do {
4281573Srgrimes				*bufnext++ = CHAR(c);
4291573Srgrimes				if (*qpatnext == RANGE &&
4301573Srgrimes				    (c = qpatnext[1]) != RBRACKET) {
4311573Srgrimes					*bufnext++ = M_RNG;
4321573Srgrimes					*bufnext++ = CHAR(c);
4331573Srgrimes					qpatnext += 2;
4341573Srgrimes				}
4351573Srgrimes			} while ((c = *qpatnext++) != RBRACKET);
4361573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4371573Srgrimes			*bufnext++ = M_END;
4381573Srgrimes			break;
4391573Srgrimes		case QUESTION:
4401573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4411573Srgrimes			*bufnext++ = M_ONE;
4421573Srgrimes			break;
4431573Srgrimes		case STAR:
4441573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4458870Srgrimes			/* collapse adjacent stars to one,
4461573Srgrimes			 * to avoid exponential behavior
4471573Srgrimes			 */
4481573Srgrimes			if (bufnext == patbuf || bufnext[-1] != M_ALL)
4491573Srgrimes			    *bufnext++ = M_ALL;
4501573Srgrimes			break;
4511573Srgrimes		default:
4521573Srgrimes			*bufnext++ = CHAR(c);
4531573Srgrimes			break;
4541573Srgrimes		}
4551573Srgrimes	}
4561573Srgrimes	*bufnext = EOS;
4571573Srgrimes#ifdef DEBUG
4581573Srgrimes	qprintf("glob0:", patbuf);
4591573Srgrimes#endif
4601573Srgrimes
4611573Srgrimes	if ((err = glob1(patbuf, pglob)) != 0)
4621573Srgrimes		return(err);
4631573Srgrimes
4641573Srgrimes	/*
4658870Srgrimes	 * If there was no match we are going to append the pattern
4661573Srgrimes	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
4671573Srgrimes	 * and the pattern did not contain any magic characters
4681573Srgrimes	 * GLOB_NOMAGIC is there just for compatibility with csh.
4691573Srgrimes	 */
4708870Srgrimes	if (pglob->gl_pathc == oldpathc &&
4718870Srgrimes	    ((pglob->gl_flags & GLOB_NOCHECK) ||
4721573Srgrimes	      ((pglob->gl_flags & GLOB_NOMAGIC) &&
4731573Srgrimes	       !(pglob->gl_flags & GLOB_MAGCHAR))))
4741573Srgrimes		return(globextend(pattern, pglob));
4758870Srgrimes	else if (!(pglob->gl_flags & GLOB_NOSORT))
4761573Srgrimes		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
4771573Srgrimes		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
4781573Srgrimes	return(0);
4791573Srgrimes}
4801573Srgrimes
4811573Srgrimesstatic int
4821573Srgrimescompare(p, q)
4831573Srgrimes	const void *p, *q;
4841573Srgrimes{
4851573Srgrimes	return(strcmp(*(char **)p, *(char **)q));
4861573Srgrimes}
4871573Srgrimes
4881573Srgrimesstatic int
4891573Srgrimesglob1(pattern, pglob)
4901573Srgrimes	Char *pattern;
4911573Srgrimes	glob_t *pglob;
4921573Srgrimes{
4931573Srgrimes	Char pathbuf[MAXPATHLEN+1];
4941573Srgrimes
4951573Srgrimes	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
4961573Srgrimes	if (*pattern == EOS)
4971573Srgrimes		return(0);
4981573Srgrimes	return(glob2(pathbuf, pathbuf, pattern, pglob));
4991573Srgrimes}
5001573Srgrimes
5011573Srgrimes/*
5021573Srgrimes * The functions glob2 and glob3 are mutually recursive; there is one level
5031573Srgrimes * of recursion for each segment in the pattern that contains one or more
5041573Srgrimes * meta characters.
5051573Srgrimes */
5061573Srgrimesstatic int
5071573Srgrimesglob2(pathbuf, pathend, pattern, pglob)
5081573Srgrimes	Char *pathbuf, *pathend, *pattern;
5091573Srgrimes	glob_t *pglob;
5101573Srgrimes{
5111573Srgrimes	struct stat sb;
5121573Srgrimes	Char *p, *q;
5131573Srgrimes	int anymeta;
5141573Srgrimes
5151573Srgrimes	/*
5161573Srgrimes	 * Loop over pattern segments until end of pattern or until
5171573Srgrimes	 * segment with meta character found.
5181573Srgrimes	 */
5191573Srgrimes	for (anymeta = 0;;) {
5201573Srgrimes		if (*pattern == EOS) {		/* End of pattern? */
5211573Srgrimes			*pathend = EOS;
5221573Srgrimes			if (g_lstat(pathbuf, &sb, pglob))
5231573Srgrimes				return(0);
5248870Srgrimes
5251573Srgrimes			if (((pglob->gl_flags & GLOB_MARK) &&
5261573Srgrimes			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
5271573Srgrimes			    || (S_ISLNK(sb.st_mode) &&
5281573Srgrimes			    (g_stat(pathbuf, &sb, pglob) == 0) &&
5291573Srgrimes			    S_ISDIR(sb.st_mode)))) {
5301573Srgrimes				*pathend++ = SEP;
5311573Srgrimes				*pathend = EOS;
5321573Srgrimes			}
5331573Srgrimes			++pglob->gl_matchc;
5341573Srgrimes			return(globextend(pathbuf, pglob));
5351573Srgrimes		}
5361573Srgrimes
5371573Srgrimes		/* Find end of next segment, copy tentatively to pathend. */
5381573Srgrimes		q = pathend;
5391573Srgrimes		p = pattern;
5401573Srgrimes		while (*p != EOS && *p != SEP) {
5411573Srgrimes			if (ismeta(*p))
5421573Srgrimes				anymeta = 1;
5431573Srgrimes			*q++ = *p++;
5441573Srgrimes		}
5451573Srgrimes
5461573Srgrimes		if (!anymeta) {		/* No expansion, do next segment. */
5471573Srgrimes			pathend = q;
5481573Srgrimes			pattern = p;
5491573Srgrimes			while (*pattern == SEP)
5501573Srgrimes				*pathend++ = *pattern++;
5511573Srgrimes		} else			/* Need expansion, recurse. */
5521573Srgrimes			return(glob3(pathbuf, pathend, pattern, p, pglob));
5531573Srgrimes	}
5541573Srgrimes	/* NOTREACHED */
5551573Srgrimes}
5561573Srgrimes
5571573Srgrimesstatic int
5581573Srgrimesglob3(pathbuf, pathend, pattern, restpattern, pglob)
5591573Srgrimes	Char *pathbuf, *pathend, *pattern, *restpattern;
5601573Srgrimes	glob_t *pglob;
5611573Srgrimes{
5621573Srgrimes	register struct dirent *dp;
5631573Srgrimes	DIR *dirp;
5641573Srgrimes	int err;
5651573Srgrimes	char buf[MAXPATHLEN];
5661573Srgrimes
5671573Srgrimes	/*
5681573Srgrimes	 * The readdirfunc declaration can't be prototyped, because it is
5691573Srgrimes	 * assigned, below, to two functions which are prototyped in glob.h
5701573Srgrimes	 * and dirent.h as taking pointers to differently typed opaque
5711573Srgrimes	 * structures.
5721573Srgrimes	 */
5731573Srgrimes	struct dirent *(*readdirfunc)();
5741573Srgrimes
5751573Srgrimes	*pathend = EOS;
5761573Srgrimes	errno = 0;
5778870Srgrimes
5781573Srgrimes	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
5791573Srgrimes		/* TODO: don't call for ENOENT or ENOTDIR? */
5801573Srgrimes		if (pglob->gl_errfunc) {
5811573Srgrimes			g_Ctoc(pathbuf, buf);
5821573Srgrimes			if (pglob->gl_errfunc(buf, errno) ||
5831573Srgrimes			    pglob->gl_flags & GLOB_ERR)
5841573Srgrimes				return (GLOB_ABEND);
5851573Srgrimes		}
5861573Srgrimes		return(0);
5871573Srgrimes	}
5881573Srgrimes
5891573Srgrimes	err = 0;
5901573Srgrimes
5911573Srgrimes	/* Search directory for matching names. */
5921573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
5931573Srgrimes		readdirfunc = pglob->gl_readdir;
5941573Srgrimes	else
5951573Srgrimes		readdirfunc = readdir;
5961573Srgrimes	while ((dp = (*readdirfunc)(dirp))) {
5971573Srgrimes		register u_char *sc;
5981573Srgrimes		register Char *dc;
5991573Srgrimes
6001573Srgrimes		/* Initial DOT must be matched literally. */
6011573Srgrimes		if (dp->d_name[0] == DOT && *pattern != DOT)
6021573Srgrimes			continue;
6038870Srgrimes		for (sc = (u_char *) dp->d_name, dc = pathend;
6041573Srgrimes		     (*dc++ = *sc++) != EOS;)
6051573Srgrimes			continue;
6061573Srgrimes		if (!match(pathend, pattern, restpattern)) {
6071573Srgrimes			*pathend = EOS;
6081573Srgrimes			continue;
6091573Srgrimes		}
6101573Srgrimes		err = glob2(pathbuf, --dc, restpattern, pglob);
6111573Srgrimes		if (err)
6121573Srgrimes			break;
6131573Srgrimes	}
6141573Srgrimes
6151573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6161573Srgrimes		(*pglob->gl_closedir)(dirp);
6171573Srgrimes	else
6181573Srgrimes		closedir(dirp);
6191573Srgrimes	return(err);
6201573Srgrimes}
6211573Srgrimes
6221573Srgrimes
6231573Srgrimes/*
6241573Srgrimes * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
6251573Srgrimes * add the new item, and update gl_pathc.
6261573Srgrimes *
6271573Srgrimes * This assumes the BSD realloc, which only copies the block when its size
6281573Srgrimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
6291573Srgrimes * behavior.
6301573Srgrimes *
6311573Srgrimes * Return 0 if new item added, error code if memory couldn't be allocated.
6321573Srgrimes *
6331573Srgrimes * Invariant of the glob_t structure:
6341573Srgrimes *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
6351573Srgrimes *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
6361573Srgrimes */
6371573Srgrimesstatic int
6381573Srgrimesglobextend(path, pglob)
6391573Srgrimes	const Char *path;
6401573Srgrimes	glob_t *pglob;
6411573Srgrimes{
6421573Srgrimes	register char **pathv;
6431573Srgrimes	register int i;
6441573Srgrimes	u_int newsize;
6451573Srgrimes	char *copy;
6461573Srgrimes	const Char *p;
6471573Srgrimes
6481573Srgrimes	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
6498870Srgrimes	pathv = pglob->gl_pathv ?
6501573Srgrimes		    realloc((char *)pglob->gl_pathv, newsize) :
6511573Srgrimes		    malloc(newsize);
6521573Srgrimes	if (pathv == NULL)
6531573Srgrimes		return(GLOB_NOSPACE);
6541573Srgrimes
6551573Srgrimes	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
6561573Srgrimes		/* first time around -- clear initial gl_offs items */
6571573Srgrimes		pathv += pglob->gl_offs;
6581573Srgrimes		for (i = pglob->gl_offs; --i >= 0; )
6591573Srgrimes			*--pathv = NULL;
6601573Srgrimes	}
6611573Srgrimes	pglob->gl_pathv = pathv;
6621573Srgrimes
6631573Srgrimes	for (p = path; *p++;)
6641573Srgrimes		continue;
6651573Srgrimes	if ((copy = malloc(p - path)) != NULL) {
6661573Srgrimes		g_Ctoc(path, copy);
6671573Srgrimes		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
6681573Srgrimes	}
6691573Srgrimes	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
6701573Srgrimes	return(copy == NULL ? GLOB_NOSPACE : 0);
6711573Srgrimes}
6721573Srgrimes
6731573Srgrimes/*
6741573Srgrimes * pattern matching function for filenames.  Each occurrence of the *
6751573Srgrimes * pattern causes a recursion level.
6761573Srgrimes */
6771573Srgrimesstatic int
6781573Srgrimesmatch(name, pat, patend)
6791573Srgrimes	register Char *name, *pat, *patend;
6801573Srgrimes{
6811573Srgrimes	int ok, negate_range;
6821573Srgrimes	Char c, k;
6831573Srgrimes
6841573Srgrimes	while (pat < patend) {
6851573Srgrimes		c = *pat++;
6861573Srgrimes		switch (c & M_MASK) {
6871573Srgrimes		case M_ALL:
6881573Srgrimes			if (pat == patend)
6891573Srgrimes				return(1);
6908870Srgrimes			do
6911573Srgrimes			    if (match(name, pat, patend))
6921573Srgrimes				    return(1);
6931573Srgrimes			while (*name++ != EOS);
6941573Srgrimes			return(0);
6951573Srgrimes		case M_ONE:
6961573Srgrimes			if (*name++ == EOS)
6971573Srgrimes				return(0);
6981573Srgrimes			break;
6991573Srgrimes		case M_SET:
7001573Srgrimes			ok = 0;
7011573Srgrimes			if ((k = *name++) == EOS)
7021573Srgrimes				return(0);
7031573Srgrimes			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
7041573Srgrimes				++pat;
7051573Srgrimes			while (((c = *pat++) & M_MASK) != M_END)
7061573Srgrimes				if ((*pat & M_MASK) == M_RNG) {
70719276Sache					if (   __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
70819276Sache					    && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
70917528Sache					   )
7101573Srgrimes						ok = 1;
7111573Srgrimes					pat += 2;
7121573Srgrimes				} else if (c == k)
7131573Srgrimes					ok = 1;
7141573Srgrimes			if (ok == negate_range)
7151573Srgrimes				return(0);
7161573Srgrimes			break;
7171573Srgrimes		default:
7181573Srgrimes			if (*name++ != c)
7191573Srgrimes				return(0);
7201573Srgrimes			break;
7211573Srgrimes		}
7221573Srgrimes	}
7231573Srgrimes	return(*name == EOS);
7241573Srgrimes}
7251573Srgrimes
7261573Srgrimes/* Free allocated data belonging to a glob_t structure. */
7271573Srgrimesvoid
7281573Srgrimesglobfree(pglob)
7291573Srgrimes	glob_t *pglob;
7301573Srgrimes{
7311573Srgrimes	register int i;
7321573Srgrimes	register char **pp;
7331573Srgrimes
7341573Srgrimes	if (pglob->gl_pathv != NULL) {
7351573Srgrimes		pp = pglob->gl_pathv + pglob->gl_offs;
7361573Srgrimes		for (i = pglob->gl_pathc; i--; ++pp)
7371573Srgrimes			if (*pp)
7381573Srgrimes				free(*pp);
7391573Srgrimes		free(pglob->gl_pathv);
7401573Srgrimes	}
7411573Srgrimes}
7421573Srgrimes
7431573Srgrimesstatic DIR *
7441573Srgrimesg_opendir(str, pglob)
7451573Srgrimes	register Char *str;
7461573Srgrimes	glob_t *pglob;
7471573Srgrimes{
7481573Srgrimes	char buf[MAXPATHLEN];
7491573Srgrimes
7501573Srgrimes	if (!*str)
7511573Srgrimes		strcpy(buf, ".");
7521573Srgrimes	else
7531573Srgrimes		g_Ctoc(str, buf);
7541573Srgrimes
7551573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7561573Srgrimes		return((*pglob->gl_opendir)(buf));
7571573Srgrimes
7581573Srgrimes	return(opendir(buf));
7591573Srgrimes}
7601573Srgrimes
7611573Srgrimesstatic int
7621573Srgrimesg_lstat(fn, sb, pglob)
7631573Srgrimes	register Char *fn;
7641573Srgrimes	struct stat *sb;
7651573Srgrimes	glob_t *pglob;
7661573Srgrimes{
7671573Srgrimes	char buf[MAXPATHLEN];
7681573Srgrimes
7691573Srgrimes	g_Ctoc(fn, buf);
7701573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7711573Srgrimes		return((*pglob->gl_lstat)(buf, sb));
7721573Srgrimes	return(lstat(buf, sb));
7731573Srgrimes}
7741573Srgrimes
7751573Srgrimesstatic int
7761573Srgrimesg_stat(fn, sb, pglob)
7771573Srgrimes	register Char *fn;
7781573Srgrimes	struct stat *sb;
7791573Srgrimes	glob_t *pglob;
7801573Srgrimes{
7811573Srgrimes	char buf[MAXPATHLEN];
7821573Srgrimes
7831573Srgrimes	g_Ctoc(fn, buf);
7841573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7851573Srgrimes		return((*pglob->gl_stat)(buf, sb));
7861573Srgrimes	return(stat(buf, sb));
7871573Srgrimes}
7881573Srgrimes
7891573Srgrimesstatic Char *
7901573Srgrimesg_strchr(str, ch)
7911573Srgrimes	Char *str;
7921573Srgrimes	int ch;
7931573Srgrimes{
7941573Srgrimes	do {
7951573Srgrimes		if (*str == ch)
7961573Srgrimes			return (str);
7971573Srgrimes	} while (*str++);
7981573Srgrimes	return (NULL);
7991573Srgrimes}
8001573Srgrimes
8011573Srgrimes#ifdef notdef
8021573Srgrimesstatic Char *
8031573Srgrimesg_strcat(dst, src)
8041573Srgrimes	Char *dst;
8051573Srgrimes	const Char* src;
8061573Srgrimes{
8071573Srgrimes	Char *sdst = dst;
8081573Srgrimes
8091573Srgrimes	while (*dst++)
8101573Srgrimes		continue;
8111573Srgrimes	--dst;
8121573Srgrimes	while((*dst++ = *src++) != EOS)
8131573Srgrimes	    continue;
8141573Srgrimes
8151573Srgrimes	return (sdst);
8161573Srgrimes}
8171573Srgrimes#endif
8181573Srgrimes
8191573Srgrimesstatic void
8201573Srgrimesg_Ctoc(str, buf)
8211573Srgrimes	register const Char *str;
8221573Srgrimes	char *buf;
8231573Srgrimes{
8241573Srgrimes	register char *dc;
8251573Srgrimes
8261573Srgrimes	for (dc = buf; (*dc++ = *str++) != EOS;)
8271573Srgrimes		continue;
8281573Srgrimes}
8291573Srgrimes
8301573Srgrimes#ifdef DEBUG
8318870Srgrimesstatic void
8321573Srgrimesqprintf(str, s)
8331573Srgrimes	const char *str;
8341573Srgrimes	register Char *s;
8351573Srgrimes{
8361573Srgrimes	register Char *p;
8371573Srgrimes
8381573Srgrimes	(void)printf("%s:\n", str);
8391573Srgrimes	for (p = s; *p; p++)
8401573Srgrimes		(void)printf("%c", CHAR(*p));
8411573Srgrimes	(void)printf("\n");
8421573Srgrimes	for (p = s; *p; p++)
8431573Srgrimes		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
8441573Srgrimes	(void)printf("\n");
8451573Srgrimes	for (p = s; *p; p++)
8461573Srgrimes		(void)printf("%c", ismeta(*p) ? '_' : ' ');
8471573Srgrimes	(void)printf("\n");
8481573Srgrimes}
8491573Srgrimes#endif
850