Home | History | Annotate | Line # | Download | only in gen
glob.c revision 1.9
      1  1.9      fvdl /*	$NetBSD: glob.c,v 1.9 1997/10/22 00:55:26 fvdl Exp $	*/
      2  1.5       cgd 
      3  1.1       cgd /*
      4  1.4       cgd  * Copyright (c) 1989, 1993
      5  1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * This code is derived from software contributed to Berkeley by
      8  1.1       cgd  * Guido van Rossum.
      9  1.1       cgd  *
     10  1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1       cgd  * modification, are permitted provided that the following conditions
     12  1.1       cgd  * are met:
     13  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1       cgd  *    must display the following acknowledgement:
     20  1.1       cgd  *	This product includes software developed by the University of
     21  1.1       cgd  *	California, Berkeley and its contributors.
     22  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1       cgd  *    may be used to endorse or promote products derived from this software
     24  1.1       cgd  *    without specific prior written permission.
     25  1.1       cgd  *
     26  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1       cgd  * SUCH DAMAGE.
     37  1.1       cgd  */
     38  1.1       cgd 
     39  1.6  christos #include <sys/cdefs.h>
     40  1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     41  1.5       cgd #if 0
     42  1.5       cgd static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
     43  1.5       cgd #else
     44  1.8      fvdl __RCSID("$NetBSD: glob.c,v 1.9 1997/10/22 00:55:26 fvdl Exp $");
     45  1.5       cgd #endif
     46  1.1       cgd #endif /* LIBC_SCCS and not lint */
     47  1.1       cgd 
     48  1.1       cgd /*
     49  1.1       cgd  * glob(3) -- a superset of the one defined in POSIX 1003.2.
     50  1.1       cgd  *
     51  1.1       cgd  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
     52  1.1       cgd  *
     53  1.1       cgd  * Optional extra services, controlled by flags not defined by POSIX:
     54  1.1       cgd  *
     55  1.1       cgd  * GLOB_QUOTE:
     56  1.1       cgd  *	Escaping convention: \ inhibits any special meaning the following
     57  1.1       cgd  *	character might have (except \ at end of string is retained).
     58  1.1       cgd  * GLOB_MAGCHAR:
     59  1.1       cgd  *	Set in gl_flags if pattern contained a globbing character.
     60  1.2   mycroft  * GLOB_NOMAGIC:
     61  1.2   mycroft  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
     62  1.2   mycroft  *	not contain any magic characters.  [Used in csh style globbing]
     63  1.2   mycroft  * GLOB_ALTDIRFUNC:
     64  1.2   mycroft  *	Use alternately specified directory access functions.
     65  1.4       cgd  * GLOB_TILDE:
     66  1.4       cgd  *	expand ~user/foo to the /home/dir/of/user/foo
     67  1.4       cgd  * GLOB_BRACE:
     68  1.4       cgd  *	expand {1,2}{a,b} to 1a 1b 2a 2b
     69  1.1       cgd  * gl_matchc:
     70  1.1       cgd  *	Number of matches in the current invocation of glob.
     71  1.1       cgd  */
     72  1.9      fvdl #define __LIBC12_SOURCE__
     73  1.1       cgd 
     74  1.7       jtc #include "namespace.h"
     75  1.1       cgd #include <sys/param.h>
     76  1.1       cgd #include <sys/stat.h>
     77  1.4       cgd #include <ctype.h>
     78  1.1       cgd #include <dirent.h>
     79  1.4       cgd #include <errno.h>
     80  1.1       cgd #include <glob.h>
     81  1.4       cgd #include <pwd.h>
     82  1.1       cgd #include <stdio.h>
     83  1.1       cgd #include <stdlib.h>
     84  1.4       cgd #include <string.h>
     85  1.4       cgd #include <unistd.h>
     86  1.7       jtc 
     87  1.7       jtc #ifdef __weak_alias
     88  1.7       jtc __weak_alias(glob,_glob);
     89  1.7       jtc __weak_alias(globfree,_globfree);
     90  1.7       jtc #endif
     91  1.1       cgd 
     92  1.1       cgd #define	DOLLAR		'$'
     93  1.1       cgd #define	DOT		'.'
     94  1.1       cgd #define	EOS		'\0'
     95  1.1       cgd #define	LBRACKET	'['
     96  1.1       cgd #define	NOT		'!'
     97  1.1       cgd #define	QUESTION	'?'
     98  1.1       cgd #define	QUOTE		'\\'
     99  1.1       cgd #define	RANGE		'-'
    100  1.1       cgd #define	RBRACKET	']'
    101  1.1       cgd #define	SEP		'/'
    102  1.1       cgd #define	STAR		'*'
    103  1.1       cgd #define	TILDE		'~'
    104  1.1       cgd #define	UNDERSCORE	'_'
    105  1.4       cgd #define	LBRACE		'{'
    106  1.4       cgd #define	RBRACE		'}'
    107  1.4       cgd #define	SLASH		'/'
    108  1.4       cgd #define	COMMA		','
    109  1.4       cgd 
    110  1.4       cgd #ifndef DEBUG
    111  1.1       cgd 
    112  1.1       cgd #define	M_QUOTE		0x8000
    113  1.1       cgd #define	M_PROTECT	0x4000
    114  1.1       cgd #define	M_MASK		0xffff
    115  1.1       cgd #define	M_ASCII		0x00ff
    116  1.1       cgd 
    117  1.4       cgd typedef u_short Char;
    118  1.4       cgd 
    119  1.4       cgd #else
    120  1.4       cgd 
    121  1.4       cgd #define	M_QUOTE		0x80
    122  1.4       cgd #define	M_PROTECT	0x40
    123  1.4       cgd #define	M_MASK		0xff
    124  1.4       cgd #define	M_ASCII		0x7f
    125  1.4       cgd 
    126  1.4       cgd typedef char Char;
    127  1.4       cgd 
    128  1.4       cgd #endif
    129  1.4       cgd 
    130  1.4       cgd 
    131  1.4       cgd #define	CHAR(c)		((Char)((c)&M_ASCII))
    132  1.4       cgd #define	META(c)		((Char)((c)|M_QUOTE))
    133  1.1       cgd #define	M_ALL		META('*')
    134  1.1       cgd #define	M_END		META(']')
    135  1.1       cgd #define	M_NOT		META('!')
    136  1.1       cgd #define	M_ONE		META('?')
    137  1.1       cgd #define	M_RNG		META('-')
    138  1.1       cgd #define	M_SET		META('[')
    139  1.1       cgd #define	ismeta(c)	(((c)&M_QUOTE) != 0)
    140  1.1       cgd 
    141  1.1       cgd 
    142  1.1       cgd static int	 compare __P((const void *, const void *));
    143  1.4       cgd static void	 g_Ctoc __P((const Char *, char *));
    144  1.9      fvdl static int	 g_lstat __P((Char *, struct stat12 *, glob_t *));
    145  1.9      fvdl static DIR	*g_opendir __P((Char *, glob_t *));
    146  1.1       cgd static Char	*g_strchr __P((Char *, int));
    147  1.4       cgd #ifdef notdef
    148  1.4       cgd static Char	*g_strcat __P((Char *, const Char *));
    149  1.4       cgd #endif
    150  1.9      fvdl static int	 g_stat __P((Char *, struct stat12 *, glob_t *));
    151  1.9      fvdl static int	 glob0 __P((const Char *, glob_t *));
    152  1.9      fvdl static int	 glob1 __P((Char *, glob_t *));
    153  1.9      fvdl static int	 glob2 __P((Char *, Char *, Char *, glob_t *));
    154  1.9      fvdl static int	 glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
    155  1.9      fvdl static int	 globextend __P((const Char *, glob_t *));
    156  1.9      fvdl static const Char *	 globtilde __P((const Char *, Char *, glob_t *));
    157  1.9      fvdl static int	 globexp1 __P((const Char *, glob_t *));
    158  1.9      fvdl static int	 globexp2 __P((const Char *, const Char *, glob_t *, int *));
    159  1.1       cgd static int	 match __P((Char *, Char *, Char *));
    160  1.1       cgd #ifdef DEBUG
    161  1.4       cgd static void	 qprintf __P((const char *, Char *));
    162  1.1       cgd #endif
    163  1.1       cgd 
    164  1.4       cgd int
    165  1.1       cgd glob(pattern, flags, errfunc, pglob)
    166  1.1       cgd 	const char *pattern;
    167  1.4       cgd 	int flags, (*errfunc) __P((const char *, int));
    168  1.9      fvdl 	glob_t *pglob;
    169  1.1       cgd {
    170  1.4       cgd 	const u_char *patnext;
    171  1.4       cgd 	int c;
    172  1.4       cgd 	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
    173  1.1       cgd 
    174  1.1       cgd 	patnext = (u_char *) pattern;
    175  1.1       cgd 	if (!(flags & GLOB_APPEND)) {
    176  1.1       cgd 		pglob->gl_pathc = 0;
    177  1.1       cgd 		pglob->gl_pathv = NULL;
    178  1.1       cgd 		if (!(flags & GLOB_DOOFFS))
    179  1.1       cgd 			pglob->gl_offs = 0;
    180  1.1       cgd 	}
    181  1.1       cgd 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
    182  1.1       cgd 	pglob->gl_errfunc = errfunc;
    183  1.1       cgd 	pglob->gl_matchc = 0;
    184  1.1       cgd 
    185  1.1       cgd 	bufnext = patbuf;
    186  1.1       cgd 	bufend = bufnext + MAXPATHLEN;
    187  1.1       cgd 	if (flags & GLOB_QUOTE) {
    188  1.1       cgd 		/* Protect the quoted characters. */
    189  1.1       cgd 		while (bufnext < bufend && (c = *patnext++) != EOS)
    190  1.1       cgd 			if (c == QUOTE) {
    191  1.1       cgd 				if ((c = *patnext++) == EOS) {
    192  1.1       cgd 					c = QUOTE;
    193  1.1       cgd 					--patnext;
    194  1.1       cgd 				}
    195  1.1       cgd 				*bufnext++ = c | M_PROTECT;
    196  1.1       cgd 			}
    197  1.1       cgd 			else
    198  1.1       cgd 				*bufnext++ = c;
    199  1.1       cgd 	}
    200  1.1       cgd 	else
    201  1.1       cgd 	    while (bufnext < bufend && (c = *patnext++) != EOS)
    202  1.1       cgd 		    *bufnext++ = c;
    203  1.1       cgd 	*bufnext = EOS;
    204  1.1       cgd 
    205  1.4       cgd 	if (flags & GLOB_BRACE)
    206  1.4       cgd 	    return globexp1(patbuf, pglob);
    207  1.4       cgd 	else
    208  1.4       cgd 	    return glob0(patbuf, pglob);
    209  1.4       cgd }
    210  1.4       cgd 
    211  1.4       cgd /*
    212  1.4       cgd  * Expand recursively a glob {} pattern. When there is no more expansion
    213  1.4       cgd  * invoke the standard globbing routine to glob the rest of the magic
    214  1.4       cgd  * characters
    215  1.4       cgd  */
    216  1.4       cgd static int globexp1(pattern, pglob)
    217  1.4       cgd 	const Char *pattern;
    218  1.9      fvdl 	glob_t *pglob;
    219  1.4       cgd {
    220  1.4       cgd 	const Char* ptr = pattern;
    221  1.4       cgd 	int rv;
    222  1.4       cgd 
    223  1.4       cgd 	/* Protect a single {}, for find(1), like csh */
    224  1.4       cgd 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
    225  1.4       cgd 		return glob0(pattern, pglob);
    226  1.4       cgd 
    227  1.4       cgd 	while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
    228  1.4       cgd 		if (!globexp2(ptr, pattern, pglob, &rv))
    229  1.4       cgd 			return rv;
    230  1.4       cgd 
    231  1.4       cgd 	return glob0(pattern, pglob);
    232  1.4       cgd }
    233  1.4       cgd 
    234  1.4       cgd 
    235  1.4       cgd /*
    236  1.4       cgd  * Recursive brace globbing helper. Tries to expand a single brace.
    237  1.4       cgd  * If it succeeds then it invokes globexp1 with the new pattern.
    238  1.4       cgd  * If it fails then it tries to glob the rest of the pattern and returns.
    239  1.4       cgd  */
    240  1.4       cgd static int globexp2(ptr, pattern, pglob, rv)
    241  1.4       cgd 	const Char *ptr, *pattern;
    242  1.9      fvdl 	glob_t *pglob;
    243  1.4       cgd 	int *rv;
    244  1.4       cgd {
    245  1.4       cgd 	int     i;
    246  1.4       cgd 	Char   *lm, *ls;
    247  1.4       cgd 	const Char *pe, *pm, *pl;
    248  1.4       cgd 	Char    patbuf[MAXPATHLEN + 1];
    249  1.4       cgd 
    250  1.4       cgd 	/* copy part up to the brace */
    251  1.4       cgd 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
    252  1.4       cgd 		continue;
    253  1.4       cgd 	ls = lm;
    254  1.4       cgd 
    255  1.4       cgd 	/* Find the balanced brace */
    256  1.4       cgd 	for (i = 0, pe = ++ptr; *pe; pe++)
    257  1.4       cgd 		if (*pe == LBRACKET) {
    258  1.4       cgd 			/* Ignore everything between [] */
    259  1.4       cgd 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
    260  1.4       cgd 				continue;
    261  1.4       cgd 			if (*pe == EOS) {
    262  1.4       cgd 				/*
    263  1.4       cgd 				 * We could not find a matching RBRACKET.
    264  1.4       cgd 				 * Ignore and just look for RBRACE
    265  1.4       cgd 				 */
    266  1.4       cgd 				pe = pm;
    267  1.4       cgd 			}
    268  1.4       cgd 		}
    269  1.4       cgd 		else if (*pe == LBRACE)
    270  1.4       cgd 			i++;
    271  1.4       cgd 		else if (*pe == RBRACE) {
    272  1.4       cgd 			if (i == 0)
    273  1.4       cgd 				break;
    274  1.4       cgd 			i--;
    275  1.4       cgd 		}
    276  1.4       cgd 
    277  1.4       cgd 	/* Non matching braces; just glob the pattern */
    278  1.4       cgd 	if (i != 0 || *pe == EOS) {
    279  1.4       cgd 		*rv = glob0(patbuf, pglob);
    280  1.4       cgd 		return 0;
    281  1.4       cgd 	}
    282  1.4       cgd 
    283  1.4       cgd 	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
    284  1.4       cgd 		switch (*pm) {
    285  1.4       cgd 		case LBRACKET:
    286  1.4       cgd 			/* Ignore everything between [] */
    287  1.4       cgd 			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
    288  1.4       cgd 				continue;
    289  1.4       cgd 			if (*pm == EOS) {
    290  1.4       cgd 				/*
    291  1.4       cgd 				 * We could not find a matching RBRACKET.
    292  1.4       cgd 				 * Ignore and just look for RBRACE
    293  1.4       cgd 				 */
    294  1.4       cgd 				pm = pl;
    295  1.4       cgd 			}
    296  1.4       cgd 			break;
    297  1.4       cgd 
    298  1.4       cgd 		case LBRACE:
    299  1.4       cgd 			i++;
    300  1.4       cgd 			break;
    301  1.4       cgd 
    302  1.4       cgd 		case RBRACE:
    303  1.4       cgd 			if (i) {
    304  1.4       cgd 			    i--;
    305  1.4       cgd 			    break;
    306  1.4       cgd 			}
    307  1.4       cgd 			/* FALLTHROUGH */
    308  1.4       cgd 		case COMMA:
    309  1.4       cgd 			if (i && *pm == COMMA)
    310  1.4       cgd 				break;
    311  1.4       cgd 			else {
    312  1.4       cgd 				/* Append the current string */
    313  1.4       cgd 				for (lm = ls; (pl < pm); *lm++ = *pl++)
    314  1.4       cgd 					continue;
    315  1.4       cgd 				/*
    316  1.4       cgd 				 * Append the rest of the pattern after the
    317  1.4       cgd 				 * closing brace
    318  1.4       cgd 				 */
    319  1.4       cgd 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
    320  1.4       cgd 					continue;
    321  1.4       cgd 
    322  1.4       cgd 				/* Expand the current pattern */
    323  1.4       cgd #ifdef DEBUG
    324  1.4       cgd 				qprintf("globexp2:", patbuf);
    325  1.4       cgd #endif
    326  1.4       cgd 				*rv = globexp1(patbuf, pglob);
    327  1.4       cgd 
    328  1.4       cgd 				/* move after the comma, to the next string */
    329  1.4       cgd 				pl = pm + 1;
    330  1.4       cgd 			}
    331  1.4       cgd 			break;
    332  1.4       cgd 
    333  1.4       cgd 		default:
    334  1.4       cgd 			break;
    335  1.4       cgd 		}
    336  1.4       cgd 	*rv = 0;
    337  1.4       cgd 	return 0;
    338  1.4       cgd }
    339  1.4       cgd 
    340  1.4       cgd 
    341  1.4       cgd 
    342  1.4       cgd /*
    343  1.4       cgd  * expand tilde from the passwd file.
    344  1.4       cgd  */
    345  1.4       cgd static const Char *
    346  1.4       cgd globtilde(pattern, patbuf, pglob)
    347  1.4       cgd 	const Char *pattern;
    348  1.4       cgd 	Char *patbuf;
    349  1.9      fvdl 	glob_t *pglob;
    350  1.4       cgd {
    351  1.4       cgd 	struct passwd *pwd;
    352  1.4       cgd 	char *h;
    353  1.4       cgd 	const Char *p;
    354  1.4       cgd 	Char *b;
    355  1.4       cgd 
    356  1.4       cgd 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
    357  1.4       cgd 		return pattern;
    358  1.4       cgd 
    359  1.4       cgd 	/* Copy up to the end of the string or / */
    360  1.4       cgd 	for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH;
    361  1.4       cgd 	     *h++ = *p++)
    362  1.4       cgd 		continue;
    363  1.4       cgd 
    364  1.4       cgd 	*h = EOS;
    365  1.4       cgd 
    366  1.4       cgd 	if (((char *) patbuf)[0] == EOS) {
    367  1.4       cgd 		/*
    368  1.4       cgd 		 * handle a plain ~ or ~/ by expanding $HOME
    369  1.4       cgd 		 * first and then trying the password file
    370  1.4       cgd 		 */
    371  1.4       cgd 		if ((h = getenv("HOME")) == NULL) {
    372  1.4       cgd 			if ((pwd = getpwuid(getuid())) == NULL)
    373  1.4       cgd 				return pattern;
    374  1.4       cgd 			else
    375  1.4       cgd 				h = pwd->pw_dir;
    376  1.4       cgd 		}
    377  1.4       cgd 	}
    378  1.4       cgd 	else {
    379  1.4       cgd 		/*
    380  1.4       cgd 		 * Expand a ~user
    381  1.4       cgd 		 */
    382  1.4       cgd 		if ((pwd = getpwnam((char*) patbuf)) == NULL)
    383  1.4       cgd 			return pattern;
    384  1.4       cgd 		else
    385  1.4       cgd 			h = pwd->pw_dir;
    386  1.4       cgd 	}
    387  1.4       cgd 
    388  1.4       cgd 	/* Copy the home directory */
    389  1.4       cgd 	for (b = patbuf; *h; *b++ = *h++)
    390  1.4       cgd 		continue;
    391  1.4       cgd 
    392  1.4       cgd 	/* Append the rest of the pattern */
    393  1.4       cgd 	while ((*b++ = *p++) != EOS)
    394  1.4       cgd 		continue;
    395  1.4       cgd 
    396  1.4       cgd 	return patbuf;
    397  1.4       cgd }
    398  1.4       cgd 
    399  1.4       cgd 
    400  1.4       cgd /*
    401  1.4       cgd  * The main glob() routine: compiles the pattern (optionally processing
    402  1.4       cgd  * quotes), calls glob1() to do the real pattern matching, and finally
    403  1.4       cgd  * sorts the list (unless unsorted operation is requested).  Returns 0
    404  1.4       cgd  * if things went well, nonzero if errors occurred.  It is not an error
    405  1.4       cgd  * to find no matches.
    406  1.4       cgd  */
    407  1.4       cgd static int
    408  1.4       cgd glob0(pattern, pglob)
    409  1.4       cgd 	const Char *pattern;
    410  1.9      fvdl 	glob_t *pglob;
    411  1.4       cgd {
    412  1.4       cgd 	const Char *qpatnext;
    413  1.4       cgd 	int c, err, oldpathc;
    414  1.4       cgd 	Char *bufnext, patbuf[MAXPATHLEN+1];
    415  1.4       cgd 
    416  1.4       cgd 	qpatnext = globtilde(pattern, patbuf, pglob);
    417  1.4       cgd 	oldpathc = pglob->gl_pathc;
    418  1.1       cgd 	bufnext = patbuf;
    419  1.4       cgd 
    420  1.1       cgd 	/* We don't need to check for buffer overflow any more. */
    421  1.1       cgd 	while ((c = *qpatnext++) != EOS) {
    422  1.1       cgd 		switch (c) {
    423  1.1       cgd 		case LBRACKET:
    424  1.1       cgd 			c = *qpatnext;
    425  1.1       cgd 			if (c == NOT)
    426  1.1       cgd 				++qpatnext;
    427  1.1       cgd 			if (*qpatnext == EOS ||
    428  1.4       cgd 			    g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
    429  1.1       cgd 				*bufnext++ = LBRACKET;
    430  1.1       cgd 				if (c == NOT)
    431  1.1       cgd 					--qpatnext;
    432  1.1       cgd 				break;
    433  1.1       cgd 			}
    434  1.1       cgd 			*bufnext++ = M_SET;
    435  1.1       cgd 			if (c == NOT)
    436  1.1       cgd 				*bufnext++ = M_NOT;
    437  1.1       cgd 			c = *qpatnext++;
    438  1.1       cgd 			do {
    439  1.1       cgd 				*bufnext++ = CHAR(c);
    440  1.1       cgd 				if (*qpatnext == RANGE &&
    441  1.1       cgd 				    (c = qpatnext[1]) != RBRACKET) {
    442  1.1       cgd 					*bufnext++ = M_RNG;
    443  1.1       cgd 					*bufnext++ = CHAR(c);
    444  1.1       cgd 					qpatnext += 2;
    445  1.1       cgd 				}
    446  1.1       cgd 			} while ((c = *qpatnext++) != RBRACKET);
    447  1.2   mycroft 			pglob->gl_flags |= GLOB_MAGCHAR;
    448  1.1       cgd 			*bufnext++ = M_END;
    449  1.1       cgd 			break;
    450  1.1       cgd 		case QUESTION:
    451  1.1       cgd 			pglob->gl_flags |= GLOB_MAGCHAR;
    452  1.1       cgd 			*bufnext++ = M_ONE;
    453  1.1       cgd 			break;
    454  1.1       cgd 		case STAR:
    455  1.1       cgd 			pglob->gl_flags |= GLOB_MAGCHAR;
    456  1.2   mycroft 			/* collapse adjacent stars to one,
    457  1.2   mycroft 			 * to avoid exponential behavior
    458  1.2   mycroft 			 */
    459  1.2   mycroft 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
    460  1.2   mycroft 			    *bufnext++ = M_ALL;
    461  1.1       cgd 			break;
    462  1.1       cgd 		default:
    463  1.1       cgd 			*bufnext++ = CHAR(c);
    464  1.1       cgd 			break;
    465  1.1       cgd 		}
    466  1.1       cgd 	}
    467  1.1       cgd 	*bufnext = EOS;
    468  1.1       cgd #ifdef DEBUG
    469  1.4       cgd 	qprintf("glob0:", patbuf);
    470  1.1       cgd #endif
    471  1.1       cgd 
    472  1.1       cgd 	if ((err = glob1(patbuf, pglob)) != 0)
    473  1.1       cgd 		return(err);
    474  1.1       cgd 
    475  1.2   mycroft 	/*
    476  1.2   mycroft 	 * If there was no match we are going to append the pattern
    477  1.2   mycroft 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
    478  1.2   mycroft 	 * and the pattern did not contain any magic characters
    479  1.2   mycroft 	 * GLOB_NOMAGIC is there just for compatibility with csh.
    480  1.2   mycroft 	 */
    481  1.2   mycroft 	if (pglob->gl_pathc == oldpathc &&
    482  1.4       cgd 	    ((pglob->gl_flags & GLOB_NOCHECK) ||
    483  1.4       cgd 	      ((pglob->gl_flags & GLOB_NOMAGIC) &&
    484  1.4       cgd 	       !(pglob->gl_flags & GLOB_MAGCHAR))))
    485  1.4       cgd 		return(globextend(pattern, pglob));
    486  1.4       cgd 	else if (!(pglob->gl_flags & GLOB_NOSORT))
    487  1.1       cgd 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
    488  1.1       cgd 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
    489  1.1       cgd 	return(0);
    490  1.1       cgd }
    491  1.1       cgd 
    492  1.1       cgd static int
    493  1.1       cgd compare(p, q)
    494  1.1       cgd 	const void *p, *q;
    495  1.1       cgd {
    496  1.1       cgd 	return(strcmp(*(char **)p, *(char **)q));
    497  1.1       cgd }
    498  1.1       cgd 
    499  1.4       cgd static int
    500  1.1       cgd glob1(pattern, pglob)
    501  1.1       cgd 	Char *pattern;
    502  1.9      fvdl 	glob_t *pglob;
    503  1.1       cgd {
    504  1.1       cgd 	Char pathbuf[MAXPATHLEN+1];
    505  1.1       cgd 
    506  1.1       cgd 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
    507  1.1       cgd 	if (*pattern == EOS)
    508  1.1       cgd 		return(0);
    509  1.1       cgd 	return(glob2(pathbuf, pathbuf, pattern, pglob));
    510  1.1       cgd }
    511  1.1       cgd 
    512  1.1       cgd /*
    513  1.1       cgd  * The functions glob2 and glob3 are mutually recursive; there is one level
    514  1.1       cgd  * of recursion for each segment in the pattern that contains one or more
    515  1.1       cgd  * meta characters.
    516  1.1       cgd  */
    517  1.4       cgd static int
    518  1.1       cgd glob2(pathbuf, pathend, pattern, pglob)
    519  1.1       cgd 	Char *pathbuf, *pathend, *pattern;
    520  1.9      fvdl 	glob_t *pglob;
    521  1.1       cgd {
    522  1.8      fvdl 	struct stat12 sb;
    523  1.1       cgd 	Char *p, *q;
    524  1.1       cgd 	int anymeta;
    525  1.1       cgd 
    526  1.1       cgd 	/*
    527  1.1       cgd 	 * Loop over pattern segments until end of pattern or until
    528  1.1       cgd 	 * segment with meta character found.
    529  1.1       cgd 	 */
    530  1.1       cgd 	for (anymeta = 0;;) {
    531  1.1       cgd 		if (*pattern == EOS) {		/* End of pattern? */
    532  1.1       cgd 			*pathend = EOS;
    533  1.2   mycroft 			if (g_lstat(pathbuf, &sb, pglob))
    534  1.1       cgd 				return(0);
    535  1.1       cgd 
    536  1.1       cgd 			if (((pglob->gl_flags & GLOB_MARK) &&
    537  1.1       cgd 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
    538  1.1       cgd 			    || (S_ISLNK(sb.st_mode) &&
    539  1.2   mycroft 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
    540  1.1       cgd 			    S_ISDIR(sb.st_mode)))) {
    541  1.1       cgd 				*pathend++ = SEP;
    542  1.1       cgd 				*pathend = EOS;
    543  1.1       cgd 			}
    544  1.1       cgd 			++pglob->gl_matchc;
    545  1.1       cgd 			return(globextend(pathbuf, pglob));
    546  1.1       cgd 		}
    547  1.1       cgd 
    548  1.1       cgd 		/* Find end of next segment, copy tentatively to pathend. */
    549  1.1       cgd 		q = pathend;
    550  1.1       cgd 		p = pattern;
    551  1.1       cgd 		while (*p != EOS && *p != SEP) {
    552  1.1       cgd 			if (ismeta(*p))
    553  1.1       cgd 				anymeta = 1;
    554  1.1       cgd 			*q++ = *p++;
    555  1.1       cgd 		}
    556  1.1       cgd 
    557  1.1       cgd 		if (!anymeta) {		/* No expansion, do next segment. */
    558  1.1       cgd 			pathend = q;
    559  1.1       cgd 			pattern = p;
    560  1.1       cgd 			while (*pattern == SEP)
    561  1.1       cgd 				*pathend++ = *pattern++;
    562  1.1       cgd 		} else			/* Need expansion, recurse. */
    563  1.1       cgd 			return(glob3(pathbuf, pathend, pattern, p, pglob));
    564  1.1       cgd 	}
    565  1.1       cgd 	/* NOTREACHED */
    566  1.1       cgd }
    567  1.1       cgd 
    568  1.4       cgd static int
    569  1.1       cgd glob3(pathbuf, pathend, pattern, restpattern, pglob)
    570  1.1       cgd 	Char *pathbuf, *pathend, *pattern, *restpattern;
    571  1.9      fvdl 	glob_t *pglob;
    572  1.1       cgd {
    573  1.1       cgd 	register struct dirent *dp;
    574  1.1       cgd 	DIR *dirp;
    575  1.4       cgd 	int err;
    576  1.2   mycroft 	char buf[MAXPATHLEN];
    577  1.1       cgd 
    578  1.4       cgd 	/*
    579  1.4       cgd 	 * The readdirfunc declaration can't be prototyped, because it is
    580  1.4       cgd 	 * assigned, below, to two functions which are prototyped in glob.h
    581  1.4       cgd 	 * and dirent.h as taking pointers to differently typed opaque
    582  1.4       cgd 	 * structures.
    583  1.4       cgd 	 */
    584  1.6  christos 	struct dirent *(*readdirfunc) __P((void *));
    585  1.4       cgd 
    586  1.1       cgd 	*pathend = EOS;
    587  1.1       cgd 	errno = 0;
    588  1.1       cgd 
    589  1.2   mycroft 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
    590  1.1       cgd 		/* TODO: don't call for ENOENT or ENOTDIR? */
    591  1.2   mycroft 		if (pglob->gl_errfunc) {
    592  1.2   mycroft 			g_Ctoc(pathbuf, buf);
    593  1.2   mycroft 			if (pglob->gl_errfunc(buf, errno) ||
    594  1.2   mycroft 			    pglob->gl_flags & GLOB_ERR)
    595  1.2   mycroft 				return (GLOB_ABEND);
    596  1.2   mycroft 		}
    597  1.2   mycroft 		return(0);
    598  1.2   mycroft 	}
    599  1.1       cgd 
    600  1.1       cgd 	err = 0;
    601  1.1       cgd 
    602  1.1       cgd 	/* Search directory for matching names. */
    603  1.2   mycroft 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    604  1.2   mycroft 		readdirfunc = pglob->gl_readdir;
    605  1.2   mycroft 	else
    606  1.6  christos 		readdirfunc = (struct dirent *(*)__P((void *))) readdir;
    607  1.2   mycroft 	while ((dp = (*readdirfunc)(dirp))) {
    608  1.1       cgd 		register u_char *sc;
    609  1.1       cgd 		register Char *dc;
    610  1.1       cgd 
    611  1.1       cgd 		/* Initial DOT must be matched literally. */
    612  1.1       cgd 		if (dp->d_name[0] == DOT && *pattern != DOT)
    613  1.1       cgd 			continue;
    614  1.1       cgd 		for (sc = (u_char *) dp->d_name, dc = pathend;
    615  1.4       cgd 		     (*dc++ = *sc++) != EOS;)
    616  1.4       cgd 			continue;
    617  1.1       cgd 		if (!match(pathend, pattern, restpattern)) {
    618  1.1       cgd 			*pathend = EOS;
    619  1.1       cgd 			continue;
    620  1.1       cgd 		}
    621  1.1       cgd 		err = glob2(pathbuf, --dc, restpattern, pglob);
    622  1.1       cgd 		if (err)
    623  1.1       cgd 			break;
    624  1.1       cgd 	}
    625  1.1       cgd 
    626  1.2   mycroft 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    627  1.2   mycroft 		(*pglob->gl_closedir)(dirp);
    628  1.2   mycroft 	else
    629  1.2   mycroft 		closedir(dirp);
    630  1.1       cgd 	return(err);
    631  1.1       cgd }
    632  1.1       cgd 
    633  1.1       cgd 
    634  1.1       cgd /*
    635  1.9      fvdl  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
    636  1.1       cgd  * add the new item, and update gl_pathc.
    637  1.1       cgd  *
    638  1.1       cgd  * This assumes the BSD realloc, which only copies the block when its size
    639  1.1       cgd  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
    640  1.1       cgd  * behavior.
    641  1.1       cgd  *
    642  1.1       cgd  * Return 0 if new item added, error code if memory couldn't be allocated.
    643  1.1       cgd  *
    644  1.9      fvdl  * Invariant of the glob_t structure:
    645  1.1       cgd  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
    646  1.1       cgd  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
    647  1.1       cgd  */
    648  1.1       cgd static int
    649  1.1       cgd globextend(path, pglob)
    650  1.4       cgd 	const Char *path;
    651  1.9      fvdl 	glob_t *pglob;
    652  1.1       cgd {
    653  1.1       cgd 	register char **pathv;
    654  1.1       cgd 	register int i;
    655  1.1       cgd 	u_int newsize;
    656  1.1       cgd 	char *copy;
    657  1.4       cgd 	const Char *p;
    658  1.1       cgd 
    659  1.1       cgd 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
    660  1.4       cgd 	pathv = pglob->gl_pathv ?
    661  1.4       cgd 		    realloc((char *)pglob->gl_pathv, newsize) :
    662  1.4       cgd 		    malloc(newsize);
    663  1.1       cgd 	if (pathv == NULL)
    664  1.1       cgd 		return(GLOB_NOSPACE);
    665  1.1       cgd 
    666  1.1       cgd 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
    667  1.1       cgd 		/* first time around -- clear initial gl_offs items */
    668  1.1       cgd 		pathv += pglob->gl_offs;
    669  1.1       cgd 		for (i = pglob->gl_offs; --i >= 0; )
    670  1.1       cgd 			*--pathv = NULL;
    671  1.1       cgd 	}
    672  1.1       cgd 	pglob->gl_pathv = pathv;
    673  1.1       cgd 
    674  1.4       cgd 	for (p = path; *p++;)
    675  1.4       cgd 		continue;
    676  1.1       cgd 	if ((copy = malloc(p - path)) != NULL) {
    677  1.1       cgd 		g_Ctoc(path, copy);
    678  1.1       cgd 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
    679  1.1       cgd 	}
    680  1.1       cgd 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
    681  1.1       cgd 	return(copy == NULL ? GLOB_NOSPACE : 0);
    682  1.1       cgd }
    683  1.1       cgd 
    684  1.1       cgd 
    685  1.1       cgd /*
    686  1.1       cgd  * pattern matching function for filenames.  Each occurrence of the *
    687  1.1       cgd  * pattern causes a recursion level.
    688  1.1       cgd  */
    689  1.4       cgd static int
    690  1.1       cgd match(name, pat, patend)
    691  1.1       cgd 	register Char *name, *pat, *patend;
    692  1.1       cgd {
    693  1.1       cgd 	int ok, negate_range;
    694  1.1       cgd 	Char c, k;
    695  1.1       cgd 
    696  1.1       cgd 	while (pat < patend) {
    697  1.1       cgd 		c = *pat++;
    698  1.1       cgd 		switch (c & M_MASK) {
    699  1.1       cgd 		case M_ALL:
    700  1.1       cgd 			if (pat == patend)
    701  1.1       cgd 				return(1);
    702  1.2   mycroft 			do
    703  1.2   mycroft 			    if (match(name, pat, patend))
    704  1.2   mycroft 				    return(1);
    705  1.2   mycroft 			while (*name++ != EOS);
    706  1.1       cgd 			return(0);
    707  1.1       cgd 		case M_ONE:
    708  1.1       cgd 			if (*name++ == EOS)
    709  1.1       cgd 				return(0);
    710  1.1       cgd 			break;
    711  1.1       cgd 		case M_SET:
    712  1.1       cgd 			ok = 0;
    713  1.2   mycroft 			if ((k = *name++) == EOS)
    714  1.2   mycroft 				return(0);
    715  1.4       cgd 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
    716  1.1       cgd 				++pat;
    717  1.1       cgd 			while (((c = *pat++) & M_MASK) != M_END)
    718  1.1       cgd 				if ((*pat & M_MASK) == M_RNG) {
    719  1.1       cgd 					if (c <= k && k <= pat[1])
    720  1.1       cgd 						ok = 1;
    721  1.1       cgd 					pat += 2;
    722  1.1       cgd 				} else if (c == k)
    723  1.1       cgd 					ok = 1;
    724  1.1       cgd 			if (ok == negate_range)
    725  1.1       cgd 				return(0);
    726  1.1       cgd 			break;
    727  1.1       cgd 		default:
    728  1.1       cgd 			if (*name++ != c)
    729  1.1       cgd 				return(0);
    730  1.1       cgd 			break;
    731  1.1       cgd 		}
    732  1.1       cgd 	}
    733  1.1       cgd 	return(*name == EOS);
    734  1.1       cgd }
    735  1.1       cgd 
    736  1.9      fvdl /* Free allocated data belonging to a glob_t structure. */
    737  1.1       cgd void
    738  1.1       cgd globfree(pglob)
    739  1.9      fvdl 	glob_t *pglob;
    740  1.1       cgd {
    741  1.1       cgd 	register int i;
    742  1.1       cgd 	register char **pp;
    743  1.1       cgd 
    744  1.1       cgd 	if (pglob->gl_pathv != NULL) {
    745  1.1       cgd 		pp = pglob->gl_pathv + pglob->gl_offs;
    746  1.1       cgd 		for (i = pglob->gl_pathc; i--; ++pp)
    747  1.1       cgd 			if (*pp)
    748  1.1       cgd 				free(*pp);
    749  1.1       cgd 		free(pglob->gl_pathv);
    750  1.1       cgd 	}
    751  1.1       cgd }
    752  1.1       cgd 
    753  1.1       cgd static DIR *
    754  1.2   mycroft g_opendir(str, pglob)
    755  1.1       cgd 	register Char *str;
    756  1.9      fvdl 	glob_t *pglob;
    757  1.1       cgd {
    758  1.1       cgd 	char buf[MAXPATHLEN];
    759  1.1       cgd 
    760  1.1       cgd 	if (!*str)
    761  1.2   mycroft 		strcpy(buf, ".");
    762  1.2   mycroft 	else
    763  1.2   mycroft 		g_Ctoc(str, buf);
    764  1.4       cgd 
    765  1.2   mycroft 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    766  1.2   mycroft 		return((*pglob->gl_opendir)(buf));
    767  1.4       cgd 
    768  1.1       cgd 	return(opendir(buf));
    769  1.1       cgd }
    770  1.1       cgd 
    771  1.1       cgd static int
    772  1.2   mycroft g_lstat(fn, sb, pglob)
    773  1.1       cgd 	register Char *fn;
    774  1.8      fvdl 	struct stat12 *sb;
    775  1.9      fvdl 	glob_t *pglob;
    776  1.1       cgd {
    777  1.1       cgd 	char buf[MAXPATHLEN];
    778  1.1       cgd 
    779  1.1       cgd 	g_Ctoc(fn, buf);
    780  1.2   mycroft 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    781  1.2   mycroft 		return((*pglob->gl_lstat)(buf, sb));
    782  1.1       cgd 	return(lstat(buf, sb));
    783  1.1       cgd }
    784  1.1       cgd 
    785  1.1       cgd static int
    786  1.2   mycroft g_stat(fn, sb, pglob)
    787  1.1       cgd 	register Char *fn;
    788  1.8      fvdl 	struct stat12 *sb;
    789  1.9      fvdl 	glob_t *pglob;
    790  1.1       cgd {
    791  1.1       cgd 	char buf[MAXPATHLEN];
    792  1.1       cgd 
    793  1.1       cgd 	g_Ctoc(fn, buf);
    794  1.2   mycroft 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    795  1.2   mycroft 		return((*pglob->gl_stat)(buf, sb));
    796  1.1       cgd 	return(stat(buf, sb));
    797  1.1       cgd }
    798  1.1       cgd 
    799  1.1       cgd static Char *
    800  1.1       cgd g_strchr(str, ch)
    801  1.1       cgd 	Char *str;
    802  1.1       cgd 	int ch;
    803  1.1       cgd {
    804  1.1       cgd 	do {
    805  1.1       cgd 		if (*str == ch)
    806  1.1       cgd 			return (str);
    807  1.1       cgd 	} while (*str++);
    808  1.1       cgd 	return (NULL);
    809  1.1       cgd }
    810  1.1       cgd 
    811  1.4       cgd #ifdef notdef
    812  1.4       cgd static Char *
    813  1.4       cgd g_strcat(dst, src)
    814  1.4       cgd 	Char *dst;
    815  1.4       cgd 	const Char* src;
    816  1.4       cgd {
    817  1.4       cgd 	Char *sdst = dst;
    818  1.4       cgd 
    819  1.4       cgd 	while (*dst++)
    820  1.4       cgd 		continue;
    821  1.4       cgd 	--dst;
    822  1.4       cgd 	while((*dst++ = *src++) != EOS)
    823  1.4       cgd 	    continue;
    824  1.4       cgd 
    825  1.4       cgd 	return (sdst);
    826  1.4       cgd }
    827  1.4       cgd #endif
    828  1.4       cgd 
    829  1.1       cgd static void
    830  1.1       cgd g_Ctoc(str, buf)
    831  1.4       cgd 	register const Char *str;
    832  1.1       cgd 	char *buf;
    833  1.1       cgd {
    834  1.1       cgd 	register char *dc;
    835  1.1       cgd 
    836  1.4       cgd 	for (dc = buf; (*dc++ = *str++) != EOS;)
    837  1.4       cgd 		continue;
    838  1.1       cgd }
    839  1.1       cgd 
    840  1.1       cgd #ifdef DEBUG
    841  1.1       cgd static void
    842  1.4       cgd qprintf(str, s)
    843  1.4       cgd 	const char *str;
    844  1.1       cgd 	register Char *s;
    845  1.1       cgd {
    846  1.1       cgd 	register Char *p;
    847  1.1       cgd 
    848  1.4       cgd 	(void)printf("%s:\n", str);
    849  1.1       cgd 	for (p = s; *p; p++)
    850  1.2   mycroft 		(void)printf("%c", CHAR(*p));
    851  1.1       cgd 	(void)printf("\n");
    852  1.1       cgd 	for (p = s; *p; p++)
    853  1.1       cgd 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
    854  1.1       cgd 	(void)printf("\n");
    855  1.1       cgd 	for (p = s; *p; p++)
    856  1.2   mycroft 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
    857  1.1       cgd 	(void)printf("\n");
    858  1.1       cgd }
    859  1.1       cgd #endif
    860