glob.c revision 74918
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.
3574307Sjlemon *
3674307Sjlemon * $FreeBSD: head/lib/libc/gen/glob.c 74918 2001-03-28 09:53:16Z peter $
371573Srgrimes */
381573Srgrimes
391573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
401573Srgrimesstatic char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
411573Srgrimes#endif /* LIBC_SCCS and not lint */
421573Srgrimes
431573Srgrimes/*
441573Srgrimes * glob(3) -- a superset of the one defined in POSIX 1003.2.
451573Srgrimes *
461573Srgrimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
471573Srgrimes *
481573Srgrimes * Optional extra services, controlled by flags not defined by POSIX:
491573Srgrimes *
501573Srgrimes * GLOB_QUOTE:
511573Srgrimes *	Escaping convention: \ inhibits any special meaning the following
521573Srgrimes *	character might have (except \ at end of string is retained).
531573Srgrimes * GLOB_MAGCHAR:
541573Srgrimes *	Set in gl_flags if pattern contained a globbing character.
551573Srgrimes * GLOB_NOMAGIC:
561573Srgrimes *	Same as GLOB_NOCHECK, but it will only append pattern if it did
571573Srgrimes *	not contain any magic characters.  [Used in csh style globbing]
581573Srgrimes * GLOB_ALTDIRFUNC:
591573Srgrimes *	Use alternately specified directory access functions.
601573Srgrimes * GLOB_TILDE:
611573Srgrimes *	expand ~user/foo to the /home/dir/of/user/foo
621573Srgrimes * GLOB_BRACE:
638870Srgrimes *	expand {1,2}{a,b} to 1a 1b 2a 2b
641573Srgrimes * gl_matchc:
651573Srgrimes *	Number of matches in the current invocation of glob.
661573Srgrimes */
671573Srgrimes
681573Srgrimes#include <sys/param.h>
691573Srgrimes#include <sys/stat.h>
701573Srgrimes
711573Srgrimes#include <ctype.h>
721573Srgrimes#include <dirent.h>
731573Srgrimes#include <errno.h>
741573Srgrimes#include <glob.h>
751573Srgrimes#include <pwd.h>
761573Srgrimes#include <stdio.h>
771573Srgrimes#include <stdlib.h>
781573Srgrimes#include <string.h>
791573Srgrimes#include <unistd.h>
801573Srgrimes
8119276Sache#include "collate.h"
8219276Sache
831573Srgrimes#define	DOLLAR		'$'
841573Srgrimes#define	DOT		'.'
851573Srgrimes#define	EOS		'\0'
861573Srgrimes#define	LBRACKET	'['
871573Srgrimes#define	NOT		'!'
881573Srgrimes#define	QUESTION	'?'
891573Srgrimes#define	QUOTE		'\\'
901573Srgrimes#define	RANGE		'-'
911573Srgrimes#define	RBRACKET	']'
921573Srgrimes#define	SEP		'/'
931573Srgrimes#define	STAR		'*'
941573Srgrimes#define	TILDE		'~'
951573Srgrimes#define	UNDERSCORE	'_'
961573Srgrimes#define	LBRACE		'{'
971573Srgrimes#define	RBRACE		'}'
981573Srgrimes#define	SLASH		'/'
991573Srgrimes#define	COMMA		','
1001573Srgrimes
1011573Srgrimes#ifndef DEBUG
1021573Srgrimes
1031573Srgrimes#define	M_QUOTE		0x8000
1041573Srgrimes#define	M_PROTECT	0x4000
1051573Srgrimes#define	M_MASK		0xffff
1061573Srgrimes#define	M_ASCII		0x00ff
1071573Srgrimes
1081573Srgrimestypedef u_short Char;
1091573Srgrimes
1101573Srgrimes#else
1111573Srgrimes
1121573Srgrimes#define	M_QUOTE		0x80
1131573Srgrimes#define	M_PROTECT	0x40
1141573Srgrimes#define	M_MASK		0xff
1151573Srgrimes#define	M_ASCII		0x7f
1161573Srgrimes
1171573Srgrimestypedef char Char;
1181573Srgrimes
1191573Srgrimes#endif
1201573Srgrimes
1211573Srgrimes
1221573Srgrimes#define	CHAR(c)		((Char)((c)&M_ASCII))
1231573Srgrimes#define	META(c)		((Char)((c)|M_QUOTE))
1241573Srgrimes#define	M_ALL		META('*')
1251573Srgrimes#define	M_END		META(']')
1261573Srgrimes#define	M_NOT		META('!')
1271573Srgrimes#define	M_ONE		META('?')
1281573Srgrimes#define	M_RNG		META('-')
1291573Srgrimes#define	M_SET		META('[')
1301573Srgrimes#define	ismeta(c)	(((c)&M_QUOTE) != 0)
1311573Srgrimes
1321573Srgrimes
1331573Srgrimesstatic int	 compare __P((const void *, const void *));
13474918Speterstatic int	 g_Ctoc __P((const Char *, char *, char *));
1351573Srgrimesstatic int	 g_lstat __P((Char *, struct stat *, glob_t *));
1361573Srgrimesstatic DIR	*g_opendir __P((Char *, glob_t *));
1371573Srgrimesstatic Char	*g_strchr __P((Char *, int));
1381573Srgrimes#ifdef notdef
1391573Srgrimesstatic Char	*g_strcat __P((Char *, const Char *));
1401573Srgrimes#endif
1411573Srgrimesstatic int	 g_stat __P((Char *, struct stat *, glob_t *));
14274469Sjlemonstatic int	 glob0 __P((const Char *, glob_t *, int *));
14374469Sjlemonstatic int	 glob1 __P((Char *, glob_t *, int *));
14474469Sjlemonstatic int	 glob2 __P((Char *, Char *, Char *, glob_t *, int *));
14574469Sjlemonstatic int	 glob3 __P((Char *, Char *, Char *, Char *, glob_t *, int *));
14674469Sjlemonstatic int	 globextend __P((const Char *, glob_t *, int *));
14724158Simpstatic const Char *	 globtilde __P((const Char *, Char *, size_t, glob_t *));
14874469Sjlemonstatic int	 globexp1 __P((const Char *, glob_t *, int *));
14974469Sjlemonstatic int	 globexp2 __P((const Char *, const Char *, glob_t *, int *, int *));
1501573Srgrimesstatic int	 match __P((Char *, Char *, Char *));
1511573Srgrimes#ifdef DEBUG
1521573Srgrimesstatic void	 qprintf __P((const char *, Char *));
1531573Srgrimes#endif
1541573Srgrimes
1551573Srgrimesint
1561573Srgrimesglob(pattern, flags, errfunc, pglob)
1571573Srgrimes	const char *pattern;
1581573Srgrimes	int flags, (*errfunc) __P((const char *, int));
1591573Srgrimes	glob_t *pglob;
1601573Srgrimes{
1611573Srgrimes	const u_char *patnext;
16274469Sjlemon	int c, limit;
1631573Srgrimes	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
1641573Srgrimes
1651573Srgrimes	patnext = (u_char *) pattern;
1661573Srgrimes	if (!(flags & GLOB_APPEND)) {
1671573Srgrimes		pglob->gl_pathc = 0;
1681573Srgrimes		pglob->gl_pathv = NULL;
1691573Srgrimes		if (!(flags & GLOB_DOOFFS))
1701573Srgrimes			pglob->gl_offs = 0;
1711573Srgrimes	}
17274469Sjlemon	if (flags & GLOB_MAXPATH)
17374469Sjlemon		limit = pglob->gl_matchc;
17474469Sjlemon	else
17574469Sjlemon		limit = 0;
1761573Srgrimes	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
1771573Srgrimes	pglob->gl_errfunc = errfunc;
1781573Srgrimes	pglob->gl_matchc = 0;
1791573Srgrimes
1801573Srgrimes	bufnext = patbuf;
1811573Srgrimes	bufend = bufnext + MAXPATHLEN;
1821573Srgrimes	if (flags & GLOB_QUOTE) {
1831573Srgrimes		/* Protect the quoted characters. */
1848870Srgrimes		while (bufnext < bufend && (c = *patnext++) != EOS)
1851573Srgrimes			if (c == QUOTE) {
1861573Srgrimes				if ((c = *patnext++) == EOS) {
1871573Srgrimes					c = QUOTE;
1881573Srgrimes					--patnext;
1891573Srgrimes				}
1901573Srgrimes				*bufnext++ = c | M_PROTECT;
1911573Srgrimes			}
1921573Srgrimes			else
1931573Srgrimes				*bufnext++ = c;
1941573Srgrimes	}
1958870Srgrimes	else
1968870Srgrimes	    while (bufnext < bufend && (c = *patnext++) != EOS)
1971573Srgrimes		    *bufnext++ = c;
1981573Srgrimes	*bufnext = EOS;
1991573Srgrimes
2001573Srgrimes	if (flags & GLOB_BRACE)
20174469Sjlemon	    return globexp1(patbuf, pglob, &limit);
2021573Srgrimes	else
20374469Sjlemon	    return glob0(patbuf, pglob, &limit);
2041573Srgrimes}
2051573Srgrimes
2061573Srgrimes/*
2071573Srgrimes * Expand recursively a glob {} pattern. When there is no more expansion
2081573Srgrimes * invoke the standard globbing routine to glob the rest of the magic
2091573Srgrimes * characters
2101573Srgrimes */
21174469Sjlemonstatic int globexp1(pattern, pglob, limit)
2121573Srgrimes	const Char *pattern;
2131573Srgrimes	glob_t *pglob;
21474469Sjlemon	int *limit;
2151573Srgrimes{
2161573Srgrimes	const Char* ptr = pattern;
2171573Srgrimes	int rv;
2181573Srgrimes
2191573Srgrimes	/* Protect a single {}, for find(1), like csh */
2201573Srgrimes	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
22174469Sjlemon		return glob0(pattern, pglob, limit);
2221573Srgrimes
2231573Srgrimes	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
22474469Sjlemon		if (!globexp2(ptr, pattern, pglob, &rv, limit))
2251573Srgrimes			return rv;
2261573Srgrimes
22774469Sjlemon	return glob0(pattern, pglob, limit);
2281573Srgrimes}
2291573Srgrimes
2301573Srgrimes
2311573Srgrimes/*
2321573Srgrimes * Recursive brace globbing helper. Tries to expand a single brace.
2331573Srgrimes * If it succeeds then it invokes globexp1 with the new pattern.
2341573Srgrimes * If it fails then it tries to glob the rest of the pattern and returns.
2351573Srgrimes */
23674469Sjlemonstatic int globexp2(ptr, pattern, pglob, rv, limit)
2371573Srgrimes	const Char *ptr, *pattern;
2381573Srgrimes	glob_t *pglob;
23974469Sjlemon	int *rv, *limit;
2401573Srgrimes{
2411573Srgrimes	int     i;
2421573Srgrimes	Char   *lm, *ls;
2431573Srgrimes	const Char *pe, *pm, *pl;
2441573Srgrimes	Char    patbuf[MAXPATHLEN + 1];
2451573Srgrimes
2461573Srgrimes	/* copy part up to the brace */
2471573Srgrimes	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
2481573Srgrimes		continue;
2491573Srgrimes	ls = lm;
2501573Srgrimes
2511573Srgrimes	/* Find the balanced brace */
2521573Srgrimes	for (i = 0, pe = ++ptr; *pe; pe++)
2531573Srgrimes		if (*pe == LBRACKET) {
2541573Srgrimes			/* Ignore everything between [] */
2551573Srgrimes			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
2561573Srgrimes				continue;
2571573Srgrimes			if (*pe == EOS) {
2588870Srgrimes				/*
2591573Srgrimes				 * We could not find a matching RBRACKET.
2601573Srgrimes				 * Ignore and just look for RBRACE
2611573Srgrimes				 */
2621573Srgrimes				pe = pm;
2631573Srgrimes			}
2641573Srgrimes		}
2651573Srgrimes		else if (*pe == LBRACE)
2661573Srgrimes			i++;
2671573Srgrimes		else if (*pe == RBRACE) {
2681573Srgrimes			if (i == 0)
2691573Srgrimes				break;
2701573Srgrimes			i--;
2711573Srgrimes		}
2721573Srgrimes
2731573Srgrimes	/* Non matching braces; just glob the pattern */
2741573Srgrimes	if (i != 0 || *pe == EOS) {
27574469Sjlemon		*rv = glob0(patbuf, pglob, limit);
2761573Srgrimes		return 0;
2771573Srgrimes	}
2781573Srgrimes
2791573Srgrimes	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
2801573Srgrimes		switch (*pm) {
2811573Srgrimes		case LBRACKET:
2821573Srgrimes			/* Ignore everything between [] */
2831573Srgrimes			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
2841573Srgrimes				continue;
2851573Srgrimes			if (*pm == EOS) {
2868870Srgrimes				/*
2871573Srgrimes				 * We could not find a matching RBRACKET.
2881573Srgrimes				 * Ignore and just look for RBRACE
2891573Srgrimes				 */
2901573Srgrimes				pm = pl;
2911573Srgrimes			}
2921573Srgrimes			break;
2931573Srgrimes
2941573Srgrimes		case LBRACE:
2951573Srgrimes			i++;
2961573Srgrimes			break;
2971573Srgrimes
2981573Srgrimes		case RBRACE:
2991573Srgrimes			if (i) {
3001573Srgrimes			    i--;
3011573Srgrimes			    break;
3021573Srgrimes			}
3031573Srgrimes			/* FALLTHROUGH */
3041573Srgrimes		case COMMA:
3051573Srgrimes			if (i && *pm == COMMA)
3061573Srgrimes				break;
3071573Srgrimes			else {
3081573Srgrimes				/* Append the current string */
3091573Srgrimes				for (lm = ls; (pl < pm); *lm++ = *pl++)
3101573Srgrimes					continue;
3118870Srgrimes				/*
3121573Srgrimes				 * Append the rest of the pattern after the
3131573Srgrimes				 * closing brace
3141573Srgrimes				 */
3151573Srgrimes				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
3161573Srgrimes					continue;
3171573Srgrimes
3181573Srgrimes				/* Expand the current pattern */
3191573Srgrimes#ifdef DEBUG
3201573Srgrimes				qprintf("globexp2:", patbuf);
3211573Srgrimes#endif
32274469Sjlemon				*rv = globexp1(patbuf, pglob, limit);
3231573Srgrimes
3241573Srgrimes				/* move after the comma, to the next string */
3251573Srgrimes				pl = pm + 1;
3261573Srgrimes			}
3271573Srgrimes			break;
3281573Srgrimes
3291573Srgrimes		default:
3301573Srgrimes			break;
3311573Srgrimes		}
3321573Srgrimes	*rv = 0;
3331573Srgrimes	return 0;
3341573Srgrimes}
3351573Srgrimes
3361573Srgrimes
3371573Srgrimes
3381573Srgrimes/*
3391573Srgrimes * expand tilde from the passwd file.
3401573Srgrimes */
3411573Srgrimesstatic const Char *
34224158Simpglobtilde(pattern, patbuf, patbuf_len, pglob)
3431573Srgrimes	const Char *pattern;
3441573Srgrimes	Char *patbuf;
34524158Simp	size_t patbuf_len;
3461573Srgrimes	glob_t *pglob;
3471573Srgrimes{
3481573Srgrimes	struct passwd *pwd;
3491573Srgrimes	char *h;
3501573Srgrimes	const Char *p;
35124158Simp	Char *b, *eb;
3521573Srgrimes
3531573Srgrimes	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
3541573Srgrimes		return pattern;
3551573Srgrimes
35624158Simp	/*
35724158Simp	 * Copy up to the end of the string or /
35824158Simp	 */
35924158Simp	eb = &patbuf[patbuf_len - 1];
36024158Simp	for (p = pattern + 1, h = (char *) patbuf;
36124158Simp	    h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
3621573Srgrimes		continue;
3631573Srgrimes
3641573Srgrimes	*h = EOS;
3651573Srgrimes
3661573Srgrimes	if (((char *) patbuf)[0] == EOS) {
3678870Srgrimes		/*
36828820Simp		 * handle a plain ~ or ~/ by expanding $HOME first (iff
36928820Simp		 * we're not running setuid or setgid) and then trying
37028820Simp		 * the password file
3711573Srgrimes		 */
37233664Sjb		if (
37333664Sjb#ifndef	__NETBSD_SYSCALLS
37433664Sjb		    issetugid() != 0 ||
37533664Sjb#endif
37633664Sjb		    (h = getenv("HOME")) == NULL) {
37728836Sache			if (((h = getlogin()) != NULL &&
37828836Sache			     (pwd = getpwnam(h)) != NULL) ||
37928836Sache			    (pwd = getpwuid(getuid())) != NULL)
38028836Sache				h = pwd->pw_dir;
38128836Sache			else
3821573Srgrimes				return pattern;
3831573Srgrimes		}
3841573Srgrimes	}
3851573Srgrimes	else {
3861573Srgrimes		/*
3871573Srgrimes		 * Expand a ~user
3881573Srgrimes		 */
3891573Srgrimes		if ((pwd = getpwnam((char*) patbuf)) == NULL)
3901573Srgrimes			return pattern;
3911573Srgrimes		else
3921573Srgrimes			h = pwd->pw_dir;
3931573Srgrimes	}
3941573Srgrimes
3951573Srgrimes	/* Copy the home directory */
39624158Simp	for (b = patbuf; b < eb && *h; *b++ = *h++)
3971573Srgrimes		continue;
3988870Srgrimes
3991573Srgrimes	/* Append the rest of the pattern */
40024158Simp	while (b < eb && (*b++ = *p++) != EOS)
4011573Srgrimes		continue;
40224158Simp	*b = EOS;
4031573Srgrimes
4041573Srgrimes	return patbuf;
4051573Srgrimes}
4061573Srgrimes
4078870Srgrimes
4081573Srgrimes/*
4091573Srgrimes * The main glob() routine: compiles the pattern (optionally processing
4101573Srgrimes * quotes), calls glob1() to do the real pattern matching, and finally
4111573Srgrimes * sorts the list (unless unsorted operation is requested).  Returns 0
4121573Srgrimes * if things went well, nonzero if errors occurred.  It is not an error
4131573Srgrimes * to find no matches.
4141573Srgrimes */
4151573Srgrimesstatic int
41674469Sjlemonglob0(pattern, pglob, limit)
4171573Srgrimes	const Char *pattern;
4181573Srgrimes	glob_t *pglob;
41974469Sjlemon	int *limit;
4201573Srgrimes{
4211573Srgrimes	const Char *qpatnext;
4221573Srgrimes	int c, err, oldpathc;
4231573Srgrimes	Char *bufnext, patbuf[MAXPATHLEN+1];
4241573Srgrimes
42524158Simp	qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char),
42624158Simp	    pglob);
4271573Srgrimes	oldpathc = pglob->gl_pathc;
4281573Srgrimes	bufnext = patbuf;
4291573Srgrimes
4301573Srgrimes	/* We don't need to check for buffer overflow any more. */
4311573Srgrimes	while ((c = *qpatnext++) != EOS) {
4321573Srgrimes		switch (c) {
4331573Srgrimes		case LBRACKET:
4341573Srgrimes			c = *qpatnext;
4351573Srgrimes			if (c == NOT)
4361573Srgrimes				++qpatnext;
4371573Srgrimes			if (*qpatnext == EOS ||
4381573Srgrimes			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
4391573Srgrimes				*bufnext++ = LBRACKET;
4401573Srgrimes				if (c == NOT)
4411573Srgrimes					--qpatnext;
4421573Srgrimes				break;
4431573Srgrimes			}
4441573Srgrimes			*bufnext++ = M_SET;
4451573Srgrimes			if (c == NOT)
4461573Srgrimes				*bufnext++ = M_NOT;
4471573Srgrimes			c = *qpatnext++;
4481573Srgrimes			do {
4491573Srgrimes				*bufnext++ = CHAR(c);
4501573Srgrimes				if (*qpatnext == RANGE &&
4511573Srgrimes				    (c = qpatnext[1]) != RBRACKET) {
4521573Srgrimes					*bufnext++ = M_RNG;
4531573Srgrimes					*bufnext++ = CHAR(c);
4541573Srgrimes					qpatnext += 2;
4551573Srgrimes				}
4561573Srgrimes			} while ((c = *qpatnext++) != RBRACKET);
4571573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4581573Srgrimes			*bufnext++ = M_END;
4591573Srgrimes			break;
4601573Srgrimes		case QUESTION:
4611573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4621573Srgrimes			*bufnext++ = M_ONE;
4631573Srgrimes			break;
4641573Srgrimes		case STAR:
4651573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4668870Srgrimes			/* collapse adjacent stars to one,
4671573Srgrimes			 * to avoid exponential behavior
4681573Srgrimes			 */
4691573Srgrimes			if (bufnext == patbuf || bufnext[-1] != M_ALL)
4701573Srgrimes			    *bufnext++ = M_ALL;
4711573Srgrimes			break;
4721573Srgrimes		default:
4731573Srgrimes			*bufnext++ = CHAR(c);
4741573Srgrimes			break;
4751573Srgrimes		}
4761573Srgrimes	}
4771573Srgrimes	*bufnext = EOS;
4781573Srgrimes#ifdef DEBUG
4791573Srgrimes	qprintf("glob0:", patbuf);
4801573Srgrimes#endif
4811573Srgrimes
48274469Sjlemon	if ((err = glob1(patbuf, pglob, limit)) != 0)
4831573Srgrimes		return(err);
4841573Srgrimes
4851573Srgrimes	/*
4868870Srgrimes	 * If there was no match we are going to append the pattern
4871573Srgrimes	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
4881573Srgrimes	 * and the pattern did not contain any magic characters
4891573Srgrimes	 * GLOB_NOMAGIC is there just for compatibility with csh.
4901573Srgrimes	 */
4918870Srgrimes	if (pglob->gl_pathc == oldpathc &&
4928870Srgrimes	    ((pglob->gl_flags & GLOB_NOCHECK) ||
4931573Srgrimes	      ((pglob->gl_flags & GLOB_NOMAGIC) &&
4941573Srgrimes	       !(pglob->gl_flags & GLOB_MAGCHAR))))
49574469Sjlemon		return(globextend(pattern, pglob, limit));
4968870Srgrimes	else if (!(pglob->gl_flags & GLOB_NOSORT))
4971573Srgrimes		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
4981573Srgrimes		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
4991573Srgrimes	return(0);
5001573Srgrimes}
5011573Srgrimes
5021573Srgrimesstatic int
5031573Srgrimescompare(p, q)
5041573Srgrimes	const void *p, *q;
5051573Srgrimes{
5061573Srgrimes	return(strcmp(*(char **)p, *(char **)q));
5071573Srgrimes}
5081573Srgrimes
5091573Srgrimesstatic int
51074469Sjlemonglob1(pattern, pglob, limit)
5111573Srgrimes	Char *pattern;
5121573Srgrimes	glob_t *pglob;
51374469Sjlemon	int *limit;
5141573Srgrimes{
5151573Srgrimes	Char pathbuf[MAXPATHLEN+1];
5161573Srgrimes
5171573Srgrimes	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
5181573Srgrimes	if (*pattern == EOS)
5191573Srgrimes		return(0);
52074469Sjlemon	return(glob2(pathbuf, pathbuf, pattern, pglob, limit));
5211573Srgrimes}
5221573Srgrimes
5231573Srgrimes/*
5241573Srgrimes * The functions glob2 and glob3 are mutually recursive; there is one level
5251573Srgrimes * of recursion for each segment in the pattern that contains one or more
5261573Srgrimes * meta characters.
5271573Srgrimes */
5281573Srgrimesstatic int
52974469Sjlemonglob2(pathbuf, pathend, pattern, pglob, limit)
5301573Srgrimes	Char *pathbuf, *pathend, *pattern;
5311573Srgrimes	glob_t *pglob;
53274469Sjlemon	int *limit;
5331573Srgrimes{
5341573Srgrimes	struct stat sb;
5351573Srgrimes	Char *p, *q;
5361573Srgrimes	int anymeta;
5371573Srgrimes
5381573Srgrimes	/*
5391573Srgrimes	 * Loop over pattern segments until end of pattern or until
5401573Srgrimes	 * segment with meta character found.
5411573Srgrimes	 */
5421573Srgrimes	for (anymeta = 0;;) {
5431573Srgrimes		if (*pattern == EOS) {		/* End of pattern? */
5441573Srgrimes			*pathend = EOS;
5451573Srgrimes			if (g_lstat(pathbuf, &sb, pglob))
5461573Srgrimes				return(0);
5478870Srgrimes
5481573Srgrimes			if (((pglob->gl_flags & GLOB_MARK) &&
5491573Srgrimes			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
5501573Srgrimes			    || (S_ISLNK(sb.st_mode) &&
5511573Srgrimes			    (g_stat(pathbuf, &sb, pglob) == 0) &&
5521573Srgrimes			    S_ISDIR(sb.st_mode)))) {
5531573Srgrimes				*pathend++ = SEP;
5541573Srgrimes				*pathend = EOS;
5551573Srgrimes			}
5561573Srgrimes			++pglob->gl_matchc;
55774469Sjlemon			return(globextend(pathbuf, pglob, limit));
5581573Srgrimes		}
5591573Srgrimes
5601573Srgrimes		/* Find end of next segment, copy tentatively to pathend. */
5611573Srgrimes		q = pathend;
5621573Srgrimes		p = pattern;
5631573Srgrimes		while (*p != EOS && *p != SEP) {
5641573Srgrimes			if (ismeta(*p))
5651573Srgrimes				anymeta = 1;
5661573Srgrimes			*q++ = *p++;
5671573Srgrimes		}
5681573Srgrimes
5691573Srgrimes		if (!anymeta) {		/* No expansion, do next segment. */
5701573Srgrimes			pathend = q;
5711573Srgrimes			pattern = p;
5721573Srgrimes			while (*pattern == SEP)
5731573Srgrimes				*pathend++ = *pattern++;
5741573Srgrimes		} else			/* Need expansion, recurse. */
57574469Sjlemon			return(glob3(pathbuf, pathend, pattern, p, pglob,
57674469Sjlemon			    limit));
5771573Srgrimes	}
5781573Srgrimes	/* NOTREACHED */
5791573Srgrimes}
5801573Srgrimes
5811573Srgrimesstatic int
58274469Sjlemonglob3(pathbuf, pathend, pattern, restpattern, pglob, limit)
5831573Srgrimes	Char *pathbuf, *pathend, *pattern, *restpattern;
5841573Srgrimes	glob_t *pglob;
58574469Sjlemon	int *limit;
5861573Srgrimes{
5871573Srgrimes	register struct dirent *dp;
5881573Srgrimes	DIR *dirp;
5891573Srgrimes	int err;
5901573Srgrimes	char buf[MAXPATHLEN];
5911573Srgrimes
5921573Srgrimes	/*
5931573Srgrimes	 * The readdirfunc declaration can't be prototyped, because it is
5941573Srgrimes	 * assigned, below, to two functions which are prototyped in glob.h
5951573Srgrimes	 * and dirent.h as taking pointers to differently typed opaque
5961573Srgrimes	 * structures.
5971573Srgrimes	 */
5981573Srgrimes	struct dirent *(*readdirfunc)();
5991573Srgrimes
6001573Srgrimes	*pathend = EOS;
6011573Srgrimes	errno = 0;
6028870Srgrimes
6031573Srgrimes	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
6041573Srgrimes		/* TODO: don't call for ENOENT or ENOTDIR? */
6051573Srgrimes		if (pglob->gl_errfunc) {
60674918Speter			if (g_Ctoc(pathbuf, buf, buf + sizeof(buf)))
60774918Speter				return (GLOB_ABEND);
6081573Srgrimes			if (pglob->gl_errfunc(buf, errno) ||
6091573Srgrimes			    pglob->gl_flags & GLOB_ERR)
6101573Srgrimes				return (GLOB_ABEND);
6111573Srgrimes		}
6121573Srgrimes		return(0);
6131573Srgrimes	}
6141573Srgrimes
6151573Srgrimes	err = 0;
6161573Srgrimes
6171573Srgrimes	/* Search directory for matching names. */
6181573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6191573Srgrimes		readdirfunc = pglob->gl_readdir;
6201573Srgrimes	else
6211573Srgrimes		readdirfunc = readdir;
6221573Srgrimes	while ((dp = (*readdirfunc)(dirp))) {
6231573Srgrimes		register u_char *sc;
6241573Srgrimes		register Char *dc;
6251573Srgrimes
6261573Srgrimes		/* Initial DOT must be matched literally. */
6271573Srgrimes		if (dp->d_name[0] == DOT && *pattern != DOT)
6281573Srgrimes			continue;
6298870Srgrimes		for (sc = (u_char *) dp->d_name, dc = pathend;
6301573Srgrimes		     (*dc++ = *sc++) != EOS;)
6311573Srgrimes			continue;
6321573Srgrimes		if (!match(pathend, pattern, restpattern)) {
6331573Srgrimes			*pathend = EOS;
6341573Srgrimes			continue;
6351573Srgrimes		}
63674469Sjlemon		err = glob2(pathbuf, --dc, restpattern, pglob, limit);
6371573Srgrimes		if (err)
6381573Srgrimes			break;
6391573Srgrimes	}
6401573Srgrimes
6411573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6421573Srgrimes		(*pglob->gl_closedir)(dirp);
6431573Srgrimes	else
6441573Srgrimes		closedir(dirp);
6451573Srgrimes	return(err);
6461573Srgrimes}
6471573Srgrimes
6481573Srgrimes
6491573Srgrimes/*
6501573Srgrimes * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
6511573Srgrimes * add the new item, and update gl_pathc.
6521573Srgrimes *
6531573Srgrimes * This assumes the BSD realloc, which only copies the block when its size
6541573Srgrimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
6551573Srgrimes * behavior.
6561573Srgrimes *
6571573Srgrimes * Return 0 if new item added, error code if memory couldn't be allocated.
6581573Srgrimes *
6591573Srgrimes * Invariant of the glob_t structure:
6601573Srgrimes *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
6611573Srgrimes *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
6621573Srgrimes */
6631573Srgrimesstatic int
66474469Sjlemonglobextend(path, pglob, limit)
6651573Srgrimes	const Char *path;
6661573Srgrimes	glob_t *pglob;
66774469Sjlemon	int *limit;
6681573Srgrimes{
6691573Srgrimes	register char **pathv;
6701573Srgrimes	register int i;
67174918Speter	u_int newsize, len;
6721573Srgrimes	char *copy;
6731573Srgrimes	const Char *p;
6741573Srgrimes
67574469Sjlemon	if (*limit && pglob->gl_pathc > *limit)
67674469Sjlemon		return (GLOB_LIMIT);
67774307Sjlemon
6781573Srgrimes	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
6798870Srgrimes	pathv = pglob->gl_pathv ?
6801573Srgrimes		    realloc((char *)pglob->gl_pathv, newsize) :
6811573Srgrimes		    malloc(newsize);
68274918Speter	if (pathv == NULL) {
68374918Speter		if (pglob->gl_pathv) {
68474918Speter			free(pglob->gl_pathv);
68574918Speter			pglob->gl_pathv = NULL;
68674918Speter		}
6871573Srgrimes		return(GLOB_NOSPACE);
68874918Speter	}
6891573Srgrimes
6901573Srgrimes	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
6911573Srgrimes		/* first time around -- clear initial gl_offs items */
6921573Srgrimes		pathv += pglob->gl_offs;
6931573Srgrimes		for (i = pglob->gl_offs; --i >= 0; )
6941573Srgrimes			*--pathv = NULL;
6951573Srgrimes	}
6961573Srgrimes	pglob->gl_pathv = pathv;
6971573Srgrimes
6981573Srgrimes	for (p = path; *p++;)
6991573Srgrimes		continue;
70074918Speter	len = (size_t)(p - path);
70174918Speter	if ((copy = malloc(len + 1)) != NULL) {
70274918Speter		if (g_Ctoc(path, copy, copy + len + 1)) {
70374918Speter			free(copy);
70474918Speter			return (GLOB_NOSPACE);
70574918Speter		}
7061573Srgrimes		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
7071573Srgrimes	}
7081573Srgrimes	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
7091573Srgrimes	return(copy == NULL ? GLOB_NOSPACE : 0);
7101573Srgrimes}
7111573Srgrimes
7121573Srgrimes/*
7131573Srgrimes * pattern matching function for filenames.  Each occurrence of the *
7141573Srgrimes * pattern causes a recursion level.
7151573Srgrimes */
7161573Srgrimesstatic int
7171573Srgrimesmatch(name, pat, patend)
7181573Srgrimes	register Char *name, *pat, *patend;
7191573Srgrimes{
7201573Srgrimes	int ok, negate_range;
7211573Srgrimes	Char c, k;
7221573Srgrimes
7231573Srgrimes	while (pat < patend) {
7241573Srgrimes		c = *pat++;
7251573Srgrimes		switch (c & M_MASK) {
7261573Srgrimes		case M_ALL:
7271573Srgrimes			if (pat == patend)
7281573Srgrimes				return(1);
7298870Srgrimes			do
7301573Srgrimes			    if (match(name, pat, patend))
7311573Srgrimes				    return(1);
7321573Srgrimes			while (*name++ != EOS);
7331573Srgrimes			return(0);
7341573Srgrimes		case M_ONE:
7351573Srgrimes			if (*name++ == EOS)
7361573Srgrimes				return(0);
7371573Srgrimes			break;
7381573Srgrimes		case M_SET:
7391573Srgrimes			ok = 0;
7401573Srgrimes			if ((k = *name++) == EOS)
7411573Srgrimes				return(0);
7421573Srgrimes			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
7431573Srgrimes				++pat;
7441573Srgrimes			while (((c = *pat++) & M_MASK) != M_END)
7451573Srgrimes				if ((*pat & M_MASK) == M_RNG) {
74624633Sache					if (__collate_load_error ?
74724633Sache					    CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
74824633Sache					       __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
74919276Sache					    && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
75017528Sache					   )
7511573Srgrimes						ok = 1;
7521573Srgrimes					pat += 2;
7531573Srgrimes				} else if (c == k)
7541573Srgrimes					ok = 1;
7551573Srgrimes			if (ok == negate_range)
7561573Srgrimes				return(0);
7571573Srgrimes			break;
7581573Srgrimes		default:
7591573Srgrimes			if (*name++ != c)
7601573Srgrimes				return(0);
7611573Srgrimes			break;
7621573Srgrimes		}
7631573Srgrimes	}
7641573Srgrimes	return(*name == EOS);
7651573Srgrimes}
7661573Srgrimes
7671573Srgrimes/* Free allocated data belonging to a glob_t structure. */
7681573Srgrimesvoid
7691573Srgrimesglobfree(pglob)
7701573Srgrimes	glob_t *pglob;
7711573Srgrimes{
7721573Srgrimes	register int i;
7731573Srgrimes	register char **pp;
7741573Srgrimes
7751573Srgrimes	if (pglob->gl_pathv != NULL) {
7761573Srgrimes		pp = pglob->gl_pathv + pglob->gl_offs;
7771573Srgrimes		for (i = pglob->gl_pathc; i--; ++pp)
7781573Srgrimes			if (*pp)
7791573Srgrimes				free(*pp);
7801573Srgrimes		free(pglob->gl_pathv);
78174918Speter		pglob->gl_pathv = NULL;
7821573Srgrimes	}
7831573Srgrimes}
7841573Srgrimes
7851573Srgrimesstatic DIR *
7861573Srgrimesg_opendir(str, pglob)
7871573Srgrimes	register Char *str;
7881573Srgrimes	glob_t *pglob;
7891573Srgrimes{
7901573Srgrimes	char buf[MAXPATHLEN];
7911573Srgrimes
7921573Srgrimes	if (!*str)
7931573Srgrimes		strcpy(buf, ".");
79474918Speter	else {
79574918Speter		if (g_Ctoc(str, buf, buf + sizeof(buf)))
79674918Speter			return (NULL);
79774918Speter	}
7981573Srgrimes
7991573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8001573Srgrimes		return((*pglob->gl_opendir)(buf));
8011573Srgrimes
8021573Srgrimes	return(opendir(buf));
8031573Srgrimes}
8041573Srgrimes
8051573Srgrimesstatic int
8061573Srgrimesg_lstat(fn, sb, pglob)
8071573Srgrimes	register Char *fn;
8081573Srgrimes	struct stat *sb;
8091573Srgrimes	glob_t *pglob;
8101573Srgrimes{
8111573Srgrimes	char buf[MAXPATHLEN];
8121573Srgrimes
81374918Speter	if (g_Ctoc(fn, buf, buf + sizeof(buf))) {
81474918Speter		errno = ENAMETOOLONG;
81574918Speter		return (-1);
81674918Speter	}
8171573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8181573Srgrimes		return((*pglob->gl_lstat)(buf, sb));
8191573Srgrimes	return(lstat(buf, sb));
8201573Srgrimes}
8211573Srgrimes
8221573Srgrimesstatic int
8231573Srgrimesg_stat(fn, sb, pglob)
8241573Srgrimes	register Char *fn;
8251573Srgrimes	struct stat *sb;
8261573Srgrimes	glob_t *pglob;
8271573Srgrimes{
8281573Srgrimes	char buf[MAXPATHLEN];
8291573Srgrimes
83074918Speter	if (g_Ctoc(fn, buf, buf + sizeof(buf))) {
83174918Speter		errno = ENAMETOOLONG;
83274918Speter		return (-1);
83374918Speter	}
8341573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8351573Srgrimes		return((*pglob->gl_stat)(buf, sb));
8361573Srgrimes	return(stat(buf, sb));
8371573Srgrimes}
8381573Srgrimes
8391573Srgrimesstatic Char *
8401573Srgrimesg_strchr(str, ch)
8411573Srgrimes	Char *str;
8421573Srgrimes	int ch;
8431573Srgrimes{
8441573Srgrimes	do {
8451573Srgrimes		if (*str == ch)
8461573Srgrimes			return (str);
8471573Srgrimes	} while (*str++);
8481573Srgrimes	return (NULL);
8491573Srgrimes}
8501573Srgrimes
8511573Srgrimes#ifdef notdef
8521573Srgrimesstatic Char *
8531573Srgrimesg_strcat(dst, src)
8541573Srgrimes	Char *dst;
8551573Srgrimes	const Char* src;
8561573Srgrimes{
8571573Srgrimes	Char *sdst = dst;
8581573Srgrimes
8591573Srgrimes	while (*dst++)
8601573Srgrimes		continue;
8611573Srgrimes	--dst;
8621573Srgrimes	while((*dst++ = *src++) != EOS)
8631573Srgrimes	    continue;
8641573Srgrimes
8651573Srgrimes	return (sdst);
8661573Srgrimes}
8671573Srgrimes#endif
8681573Srgrimes
86974918Speterstatic int
87074918Speterg_Ctoc(str, buf, ebuf)
8711573Srgrimes	register const Char *str;
87274918Speter	char *buf, *ebuf;
8731573Srgrimes{
8741573Srgrimes	register char *dc;
8751573Srgrimes
87674918Speter	for (dc = buf; dc < ebuf && (*dc++ = *str++) != EOS;)
8771573Srgrimes		continue;
87874918Speter	if (dc >= ebuf)
87974918Speter		return (1);
88074918Speter	return (0);
8811573Srgrimes}
8821573Srgrimes
8831573Srgrimes#ifdef DEBUG
8848870Srgrimesstatic void
8851573Srgrimesqprintf(str, s)
8861573Srgrimes	const char *str;
8871573Srgrimes	register Char *s;
8881573Srgrimes{
8891573Srgrimes	register Char *p;
8901573Srgrimes
8911573Srgrimes	(void)printf("%s:\n", str);
8921573Srgrimes	for (p = s; *p; p++)
8931573Srgrimes		(void)printf("%c", CHAR(*p));
8941573Srgrimes	(void)printf("\n");
8951573Srgrimes	for (p = s; *p; p++)
8961573Srgrimes		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
8971573Srgrimes	(void)printf("\n");
8981573Srgrimes	for (p = s; *p; p++)
8991573Srgrimes		(void)printf("%c", ismeta(*p) ? '_' : ' ');
9001573Srgrimes	(void)printf("\n");
9011573Srgrimes}
9021573Srgrimes#endif
903