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