glob.c revision 33664
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 *));
14524158Simpstatic const Char *	 globtilde __P((const Char *, Char *, size_t, 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 *
33524158Simpglobtilde(pattern, patbuf, patbuf_len, pglob)
3361573Srgrimes	const Char *pattern;
3371573Srgrimes	Char *patbuf;
33824158Simp	size_t patbuf_len;
3391573Srgrimes	glob_t *pglob;
3401573Srgrimes{
3411573Srgrimes	struct passwd *pwd;
3421573Srgrimes	char *h;
3431573Srgrimes	const Char *p;
34424158Simp	Char *b, *eb;
3451573Srgrimes
3461573Srgrimes	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
3471573Srgrimes		return pattern;
3481573Srgrimes
34924158Simp	/*
35024158Simp	 * Copy up to the end of the string or /
35124158Simp	 */
35224158Simp	eb = &patbuf[patbuf_len - 1];
35324158Simp	for (p = pattern + 1, h = (char *) patbuf;
35424158Simp	    h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
3551573Srgrimes		continue;
3561573Srgrimes
3571573Srgrimes	*h = EOS;
3581573Srgrimes
3591573Srgrimes	if (((char *) patbuf)[0] == EOS) {
3608870Srgrimes		/*
36128820Simp		 * handle a plain ~ or ~/ by expanding $HOME first (iff
36228820Simp		 * we're not running setuid or setgid) and then trying
36328820Simp		 * the password file
3641573Srgrimes		 */
36533664Sjb		if (
36633664Sjb#ifndef	__NETBSD_SYSCALLS
36733664Sjb		    issetugid() != 0 ||
36833664Sjb#endif
36933664Sjb		    (h = getenv("HOME")) == NULL) {
37028836Sache			if (((h = getlogin()) != NULL &&
37128836Sache			     (pwd = getpwnam(h)) != NULL) ||
37228836Sache			    (pwd = getpwuid(getuid())) != NULL)
37328836Sache				h = pwd->pw_dir;
37428836Sache			else
3751573Srgrimes				return pattern;
3761573Srgrimes		}
3771573Srgrimes	}
3781573Srgrimes	else {
3791573Srgrimes		/*
3801573Srgrimes		 * Expand a ~user
3811573Srgrimes		 */
3821573Srgrimes		if ((pwd = getpwnam((char*) patbuf)) == NULL)
3831573Srgrimes			return pattern;
3841573Srgrimes		else
3851573Srgrimes			h = pwd->pw_dir;
3861573Srgrimes	}
3871573Srgrimes
3881573Srgrimes	/* Copy the home directory */
38924158Simp	for (b = patbuf; b < eb && *h; *b++ = *h++)
3901573Srgrimes		continue;
3918870Srgrimes
3921573Srgrimes	/* Append the rest of the pattern */
39324158Simp	while (b < eb && (*b++ = *p++) != EOS)
3941573Srgrimes		continue;
39524158Simp	*b = EOS;
3961573Srgrimes
3971573Srgrimes	return patbuf;
3981573Srgrimes}
3991573Srgrimes
4008870Srgrimes
4011573Srgrimes/*
4021573Srgrimes * The main glob() routine: compiles the pattern (optionally processing
4031573Srgrimes * quotes), calls glob1() to do the real pattern matching, and finally
4041573Srgrimes * sorts the list (unless unsorted operation is requested).  Returns 0
4051573Srgrimes * if things went well, nonzero if errors occurred.  It is not an error
4061573Srgrimes * to find no matches.
4071573Srgrimes */
4081573Srgrimesstatic int
4091573Srgrimesglob0(pattern, pglob)
4101573Srgrimes	const Char *pattern;
4111573Srgrimes	glob_t *pglob;
4121573Srgrimes{
4131573Srgrimes	const Char *qpatnext;
4141573Srgrimes	int c, err, oldpathc;
4151573Srgrimes	Char *bufnext, patbuf[MAXPATHLEN+1];
4161573Srgrimes
41724158Simp	qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char),
41824158Simp	    pglob);
4191573Srgrimes	oldpathc = pglob->gl_pathc;
4201573Srgrimes	bufnext = patbuf;
4211573Srgrimes
4221573Srgrimes	/* We don't need to check for buffer overflow any more. */
4231573Srgrimes	while ((c = *qpatnext++) != EOS) {
4241573Srgrimes		switch (c) {
4251573Srgrimes		case LBRACKET:
4261573Srgrimes			c = *qpatnext;
4271573Srgrimes			if (c == NOT)
4281573Srgrimes				++qpatnext;
4291573Srgrimes			if (*qpatnext == EOS ||
4301573Srgrimes			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
4311573Srgrimes				*bufnext++ = LBRACKET;
4321573Srgrimes				if (c == NOT)
4331573Srgrimes					--qpatnext;
4341573Srgrimes				break;
4351573Srgrimes			}
4361573Srgrimes			*bufnext++ = M_SET;
4371573Srgrimes			if (c == NOT)
4381573Srgrimes				*bufnext++ = M_NOT;
4391573Srgrimes			c = *qpatnext++;
4401573Srgrimes			do {
4411573Srgrimes				*bufnext++ = CHAR(c);
4421573Srgrimes				if (*qpatnext == RANGE &&
4431573Srgrimes				    (c = qpatnext[1]) != RBRACKET) {
4441573Srgrimes					*bufnext++ = M_RNG;
4451573Srgrimes					*bufnext++ = CHAR(c);
4461573Srgrimes					qpatnext += 2;
4471573Srgrimes				}
4481573Srgrimes			} while ((c = *qpatnext++) != RBRACKET);
4491573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4501573Srgrimes			*bufnext++ = M_END;
4511573Srgrimes			break;
4521573Srgrimes		case QUESTION:
4531573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4541573Srgrimes			*bufnext++ = M_ONE;
4551573Srgrimes			break;
4561573Srgrimes		case STAR:
4571573Srgrimes			pglob->gl_flags |= GLOB_MAGCHAR;
4588870Srgrimes			/* collapse adjacent stars to one,
4591573Srgrimes			 * to avoid exponential behavior
4601573Srgrimes			 */
4611573Srgrimes			if (bufnext == patbuf || bufnext[-1] != M_ALL)
4621573Srgrimes			    *bufnext++ = M_ALL;
4631573Srgrimes			break;
4641573Srgrimes		default:
4651573Srgrimes			*bufnext++ = CHAR(c);
4661573Srgrimes			break;
4671573Srgrimes		}
4681573Srgrimes	}
4691573Srgrimes	*bufnext = EOS;
4701573Srgrimes#ifdef DEBUG
4711573Srgrimes	qprintf("glob0:", patbuf);
4721573Srgrimes#endif
4731573Srgrimes
4741573Srgrimes	if ((err = glob1(patbuf, pglob)) != 0)
4751573Srgrimes		return(err);
4761573Srgrimes
4771573Srgrimes	/*
4788870Srgrimes	 * If there was no match we are going to append the pattern
4791573Srgrimes	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
4801573Srgrimes	 * and the pattern did not contain any magic characters
4811573Srgrimes	 * GLOB_NOMAGIC is there just for compatibility with csh.
4821573Srgrimes	 */
4838870Srgrimes	if (pglob->gl_pathc == oldpathc &&
4848870Srgrimes	    ((pglob->gl_flags & GLOB_NOCHECK) ||
4851573Srgrimes	      ((pglob->gl_flags & GLOB_NOMAGIC) &&
4861573Srgrimes	       !(pglob->gl_flags & GLOB_MAGCHAR))))
4871573Srgrimes		return(globextend(pattern, pglob));
4888870Srgrimes	else if (!(pglob->gl_flags & GLOB_NOSORT))
4891573Srgrimes		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
4901573Srgrimes		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
4911573Srgrimes	return(0);
4921573Srgrimes}
4931573Srgrimes
4941573Srgrimesstatic int
4951573Srgrimescompare(p, q)
4961573Srgrimes	const void *p, *q;
4971573Srgrimes{
4981573Srgrimes	return(strcmp(*(char **)p, *(char **)q));
4991573Srgrimes}
5001573Srgrimes
5011573Srgrimesstatic int
5021573Srgrimesglob1(pattern, pglob)
5031573Srgrimes	Char *pattern;
5041573Srgrimes	glob_t *pglob;
5051573Srgrimes{
5061573Srgrimes	Char pathbuf[MAXPATHLEN+1];
5071573Srgrimes
5081573Srgrimes	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
5091573Srgrimes	if (*pattern == EOS)
5101573Srgrimes		return(0);
5111573Srgrimes	return(glob2(pathbuf, pathbuf, pattern, pglob));
5121573Srgrimes}
5131573Srgrimes
5141573Srgrimes/*
5151573Srgrimes * The functions glob2 and glob3 are mutually recursive; there is one level
5161573Srgrimes * of recursion for each segment in the pattern that contains one or more
5171573Srgrimes * meta characters.
5181573Srgrimes */
5191573Srgrimesstatic int
5201573Srgrimesglob2(pathbuf, pathend, pattern, pglob)
5211573Srgrimes	Char *pathbuf, *pathend, *pattern;
5221573Srgrimes	glob_t *pglob;
5231573Srgrimes{
5241573Srgrimes	struct stat sb;
5251573Srgrimes	Char *p, *q;
5261573Srgrimes	int anymeta;
5271573Srgrimes
5281573Srgrimes	/*
5291573Srgrimes	 * Loop over pattern segments until end of pattern or until
5301573Srgrimes	 * segment with meta character found.
5311573Srgrimes	 */
5321573Srgrimes	for (anymeta = 0;;) {
5331573Srgrimes		if (*pattern == EOS) {		/* End of pattern? */
5341573Srgrimes			*pathend = EOS;
5351573Srgrimes			if (g_lstat(pathbuf, &sb, pglob))
5361573Srgrimes				return(0);
5378870Srgrimes
5381573Srgrimes			if (((pglob->gl_flags & GLOB_MARK) &&
5391573Srgrimes			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
5401573Srgrimes			    || (S_ISLNK(sb.st_mode) &&
5411573Srgrimes			    (g_stat(pathbuf, &sb, pglob) == 0) &&
5421573Srgrimes			    S_ISDIR(sb.st_mode)))) {
5431573Srgrimes				*pathend++ = SEP;
5441573Srgrimes				*pathend = EOS;
5451573Srgrimes			}
5461573Srgrimes			++pglob->gl_matchc;
5471573Srgrimes			return(globextend(pathbuf, pglob));
5481573Srgrimes		}
5491573Srgrimes
5501573Srgrimes		/* Find end of next segment, copy tentatively to pathend. */
5511573Srgrimes		q = pathend;
5521573Srgrimes		p = pattern;
5531573Srgrimes		while (*p != EOS && *p != SEP) {
5541573Srgrimes			if (ismeta(*p))
5551573Srgrimes				anymeta = 1;
5561573Srgrimes			*q++ = *p++;
5571573Srgrimes		}
5581573Srgrimes
5591573Srgrimes		if (!anymeta) {		/* No expansion, do next segment. */
5601573Srgrimes			pathend = q;
5611573Srgrimes			pattern = p;
5621573Srgrimes			while (*pattern == SEP)
5631573Srgrimes				*pathend++ = *pattern++;
5641573Srgrimes		} else			/* Need expansion, recurse. */
5651573Srgrimes			return(glob3(pathbuf, pathend, pattern, p, pglob));
5661573Srgrimes	}
5671573Srgrimes	/* NOTREACHED */
5681573Srgrimes}
5691573Srgrimes
5701573Srgrimesstatic int
5711573Srgrimesglob3(pathbuf, pathend, pattern, restpattern, pglob)
5721573Srgrimes	Char *pathbuf, *pathend, *pattern, *restpattern;
5731573Srgrimes	glob_t *pglob;
5741573Srgrimes{
5751573Srgrimes	register struct dirent *dp;
5761573Srgrimes	DIR *dirp;
5771573Srgrimes	int err;
5781573Srgrimes	char buf[MAXPATHLEN];
5791573Srgrimes
5801573Srgrimes	/*
5811573Srgrimes	 * The readdirfunc declaration can't be prototyped, because it is
5821573Srgrimes	 * assigned, below, to two functions which are prototyped in glob.h
5831573Srgrimes	 * and dirent.h as taking pointers to differently typed opaque
5841573Srgrimes	 * structures.
5851573Srgrimes	 */
5861573Srgrimes	struct dirent *(*readdirfunc)();
5871573Srgrimes
5881573Srgrimes	*pathend = EOS;
5891573Srgrimes	errno = 0;
5908870Srgrimes
5911573Srgrimes	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
5921573Srgrimes		/* TODO: don't call for ENOENT or ENOTDIR? */
5931573Srgrimes		if (pglob->gl_errfunc) {
5941573Srgrimes			g_Ctoc(pathbuf, buf);
5951573Srgrimes			if (pglob->gl_errfunc(buf, errno) ||
5961573Srgrimes			    pglob->gl_flags & GLOB_ERR)
5971573Srgrimes				return (GLOB_ABEND);
5981573Srgrimes		}
5991573Srgrimes		return(0);
6001573Srgrimes	}
6011573Srgrimes
6021573Srgrimes	err = 0;
6031573Srgrimes
6041573Srgrimes	/* Search directory for matching names. */
6051573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6061573Srgrimes		readdirfunc = pglob->gl_readdir;
6071573Srgrimes	else
6081573Srgrimes		readdirfunc = readdir;
6091573Srgrimes	while ((dp = (*readdirfunc)(dirp))) {
6101573Srgrimes		register u_char *sc;
6111573Srgrimes		register Char *dc;
6121573Srgrimes
6131573Srgrimes		/* Initial DOT must be matched literally. */
6141573Srgrimes		if (dp->d_name[0] == DOT && *pattern != DOT)
6151573Srgrimes			continue;
6168870Srgrimes		for (sc = (u_char *) dp->d_name, dc = pathend;
6171573Srgrimes		     (*dc++ = *sc++) != EOS;)
6181573Srgrimes			continue;
6191573Srgrimes		if (!match(pathend, pattern, restpattern)) {
6201573Srgrimes			*pathend = EOS;
6211573Srgrimes			continue;
6221573Srgrimes		}
6231573Srgrimes		err = glob2(pathbuf, --dc, restpattern, pglob);
6241573Srgrimes		if (err)
6251573Srgrimes			break;
6261573Srgrimes	}
6271573Srgrimes
6281573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
6291573Srgrimes		(*pglob->gl_closedir)(dirp);
6301573Srgrimes	else
6311573Srgrimes		closedir(dirp);
6321573Srgrimes	return(err);
6331573Srgrimes}
6341573Srgrimes
6351573Srgrimes
6361573Srgrimes/*
6371573Srgrimes * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
6381573Srgrimes * add the new item, and update gl_pathc.
6391573Srgrimes *
6401573Srgrimes * This assumes the BSD realloc, which only copies the block when its size
6411573Srgrimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
6421573Srgrimes * behavior.
6431573Srgrimes *
6441573Srgrimes * Return 0 if new item added, error code if memory couldn't be allocated.
6451573Srgrimes *
6461573Srgrimes * Invariant of the glob_t structure:
6471573Srgrimes *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
6481573Srgrimes *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
6491573Srgrimes */
6501573Srgrimesstatic int
6511573Srgrimesglobextend(path, pglob)
6521573Srgrimes	const Char *path;
6531573Srgrimes	glob_t *pglob;
6541573Srgrimes{
6551573Srgrimes	register char **pathv;
6561573Srgrimes	register int i;
6571573Srgrimes	u_int newsize;
6581573Srgrimes	char *copy;
6591573Srgrimes	const Char *p;
6601573Srgrimes
6611573Srgrimes	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
6628870Srgrimes	pathv = pglob->gl_pathv ?
6631573Srgrimes		    realloc((char *)pglob->gl_pathv, newsize) :
6641573Srgrimes		    malloc(newsize);
6651573Srgrimes	if (pathv == NULL)
6661573Srgrimes		return(GLOB_NOSPACE);
6671573Srgrimes
6681573Srgrimes	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
6691573Srgrimes		/* first time around -- clear initial gl_offs items */
6701573Srgrimes		pathv += pglob->gl_offs;
6711573Srgrimes		for (i = pglob->gl_offs; --i >= 0; )
6721573Srgrimes			*--pathv = NULL;
6731573Srgrimes	}
6741573Srgrimes	pglob->gl_pathv = pathv;
6751573Srgrimes
6761573Srgrimes	for (p = path; *p++;)
6771573Srgrimes		continue;
6781573Srgrimes	if ((copy = malloc(p - path)) != NULL) {
6791573Srgrimes		g_Ctoc(path, copy);
6801573Srgrimes		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
6811573Srgrimes	}
6821573Srgrimes	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
6831573Srgrimes	return(copy == NULL ? GLOB_NOSPACE : 0);
6841573Srgrimes}
6851573Srgrimes
6861573Srgrimes/*
6871573Srgrimes * pattern matching function for filenames.  Each occurrence of the *
6881573Srgrimes * pattern causes a recursion level.
6891573Srgrimes */
6901573Srgrimesstatic int
6911573Srgrimesmatch(name, pat, patend)
6921573Srgrimes	register Char *name, *pat, *patend;
6931573Srgrimes{
6941573Srgrimes	int ok, negate_range;
6951573Srgrimes	Char c, k;
6961573Srgrimes
6971573Srgrimes	while (pat < patend) {
6981573Srgrimes		c = *pat++;
6991573Srgrimes		switch (c & M_MASK) {
7001573Srgrimes		case M_ALL:
7011573Srgrimes			if (pat == patend)
7021573Srgrimes				return(1);
7038870Srgrimes			do
7041573Srgrimes			    if (match(name, pat, patend))
7051573Srgrimes				    return(1);
7061573Srgrimes			while (*name++ != EOS);
7071573Srgrimes			return(0);
7081573Srgrimes		case M_ONE:
7091573Srgrimes			if (*name++ == EOS)
7101573Srgrimes				return(0);
7111573Srgrimes			break;
7121573Srgrimes		case M_SET:
7131573Srgrimes			ok = 0;
7141573Srgrimes			if ((k = *name++) == EOS)
7151573Srgrimes				return(0);
7161573Srgrimes			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
7171573Srgrimes				++pat;
7181573Srgrimes			while (((c = *pat++) & M_MASK) != M_END)
7191573Srgrimes				if ((*pat & M_MASK) == M_RNG) {
72024633Sache					if (__collate_load_error ?
72124633Sache					    CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
72224633Sache					       __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
72319276Sache					    && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
72417528Sache					   )
7251573Srgrimes						ok = 1;
7261573Srgrimes					pat += 2;
7271573Srgrimes				} else if (c == k)
7281573Srgrimes					ok = 1;
7291573Srgrimes			if (ok == negate_range)
7301573Srgrimes				return(0);
7311573Srgrimes			break;
7321573Srgrimes		default:
7331573Srgrimes			if (*name++ != c)
7341573Srgrimes				return(0);
7351573Srgrimes			break;
7361573Srgrimes		}
7371573Srgrimes	}
7381573Srgrimes	return(*name == EOS);
7391573Srgrimes}
7401573Srgrimes
7411573Srgrimes/* Free allocated data belonging to a glob_t structure. */
7421573Srgrimesvoid
7431573Srgrimesglobfree(pglob)
7441573Srgrimes	glob_t *pglob;
7451573Srgrimes{
7461573Srgrimes	register int i;
7471573Srgrimes	register char **pp;
7481573Srgrimes
7491573Srgrimes	if (pglob->gl_pathv != NULL) {
7501573Srgrimes		pp = pglob->gl_pathv + pglob->gl_offs;
7511573Srgrimes		for (i = pglob->gl_pathc; i--; ++pp)
7521573Srgrimes			if (*pp)
7531573Srgrimes				free(*pp);
7541573Srgrimes		free(pglob->gl_pathv);
7551573Srgrimes	}
7561573Srgrimes}
7571573Srgrimes
7581573Srgrimesstatic DIR *
7591573Srgrimesg_opendir(str, pglob)
7601573Srgrimes	register Char *str;
7611573Srgrimes	glob_t *pglob;
7621573Srgrimes{
7631573Srgrimes	char buf[MAXPATHLEN];
7641573Srgrimes
7651573Srgrimes	if (!*str)
7661573Srgrimes		strcpy(buf, ".");
7671573Srgrimes	else
7681573Srgrimes		g_Ctoc(str, buf);
7691573Srgrimes
7701573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7711573Srgrimes		return((*pglob->gl_opendir)(buf));
7721573Srgrimes
7731573Srgrimes	return(opendir(buf));
7741573Srgrimes}
7751573Srgrimes
7761573Srgrimesstatic int
7771573Srgrimesg_lstat(fn, sb, pglob)
7781573Srgrimes	register Char *fn;
7791573Srgrimes	struct stat *sb;
7801573Srgrimes	glob_t *pglob;
7811573Srgrimes{
7821573Srgrimes	char buf[MAXPATHLEN];
7831573Srgrimes
7841573Srgrimes	g_Ctoc(fn, buf);
7851573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7861573Srgrimes		return((*pglob->gl_lstat)(buf, sb));
7871573Srgrimes	return(lstat(buf, sb));
7881573Srgrimes}
7891573Srgrimes
7901573Srgrimesstatic int
7911573Srgrimesg_stat(fn, sb, pglob)
7921573Srgrimes	register Char *fn;
7931573Srgrimes	struct stat *sb;
7941573Srgrimes	glob_t *pglob;
7951573Srgrimes{
7961573Srgrimes	char buf[MAXPATHLEN];
7971573Srgrimes
7981573Srgrimes	g_Ctoc(fn, buf);
7991573Srgrimes	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
8001573Srgrimes		return((*pglob->gl_stat)(buf, sb));
8011573Srgrimes	return(stat(buf, sb));
8021573Srgrimes}
8031573Srgrimes
8041573Srgrimesstatic Char *
8051573Srgrimesg_strchr(str, ch)
8061573Srgrimes	Char *str;
8071573Srgrimes	int ch;
8081573Srgrimes{
8091573Srgrimes	do {
8101573Srgrimes		if (*str == ch)
8111573Srgrimes			return (str);
8121573Srgrimes	} while (*str++);
8131573Srgrimes	return (NULL);
8141573Srgrimes}
8151573Srgrimes
8161573Srgrimes#ifdef notdef
8171573Srgrimesstatic Char *
8181573Srgrimesg_strcat(dst, src)
8191573Srgrimes	Char *dst;
8201573Srgrimes	const Char* src;
8211573Srgrimes{
8221573Srgrimes	Char *sdst = dst;
8231573Srgrimes
8241573Srgrimes	while (*dst++)
8251573Srgrimes		continue;
8261573Srgrimes	--dst;
8271573Srgrimes	while((*dst++ = *src++) != EOS)
8281573Srgrimes	    continue;
8291573Srgrimes
8301573Srgrimes	return (sdst);
8311573Srgrimes}
8321573Srgrimes#endif
8331573Srgrimes
8341573Srgrimesstatic void
8351573Srgrimesg_Ctoc(str, buf)
8361573Srgrimes	register const Char *str;
8371573Srgrimes	char *buf;
8381573Srgrimes{
8391573Srgrimes	register char *dc;
8401573Srgrimes
8411573Srgrimes	for (dc = buf; (*dc++ = *str++) != EOS;)
8421573Srgrimes		continue;
8431573Srgrimes}
8441573Srgrimes
8451573Srgrimes#ifdef DEBUG
8468870Srgrimesstatic void
8471573Srgrimesqprintf(str, s)
8481573Srgrimes	const char *str;
8491573Srgrimes	register Char *s;
8501573Srgrimes{
8511573Srgrimes	register Char *p;
8521573Srgrimes
8531573Srgrimes	(void)printf("%s:\n", str);
8541573Srgrimes	for (p = s; *p; p++)
8551573Srgrimes		(void)printf("%c", CHAR(*p));
8561573Srgrimes	(void)printf("\n");
8571573Srgrimes	for (p = s; *p; p++)
8581573Srgrimes		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
8591573Srgrimes	(void)printf("\n");
8601573Srgrimes	for (p = s; *p; p++)
8611573Srgrimes		(void)printf("%c", ismeta(*p) ? '_' : ' ');
8621573Srgrimes	(void)printf("\n");
8631573Srgrimes}
8641573Srgrimes#endif
865