Home | History | Annotate | Line # | Download | only in gen
glob.c revision 1.39
      1  1.39  christos /*	$NetBSD: glob.c,v 1.39 2019/05/29 01:21:33 christos Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*
      4  1.12  christos  * Copyright (c) 1989, 1993
      5  1.12  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.12  christos  *
      7  1.12  christos  * This code is derived from software contributed to Berkeley by
      8  1.12  christos  * Guido van Rossum.
      9  1.12  christos  *
     10  1.12  christos  * Redistribution and use in source and binary forms, with or without
     11  1.12  christos  * modification, are permitted provided that the following conditions
     12  1.12  christos  * are met:
     13  1.12  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.12  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.12  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.12  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.12  christos  *    documentation and/or other materials provided with the distribution.
     18  1.12  christos  * 3. Neither the name of the University nor the names of its contributors
     19  1.12  christos  *    may be used to endorse or promote products derived from this software
     20  1.12  christos  *    without specific prior written permission.
     21  1.12  christos  *
     22  1.12  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.12  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.12  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.12  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.12  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.12  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.12  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.12  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.12  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.12  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.12  christos  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.12  christos #include <sys/cdefs.h>
     36  1.12  christos #if defined(LIBC_SCCS) && !defined(lint)
     37  1.12  christos #if 0
     38  1.12  christos static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
     39  1.12  christos #else
     40  1.39  christos __RCSID("$NetBSD: glob.c,v 1.39 2019/05/29 01:21:33 christos Exp $");
     41  1.12  christos #endif
     42  1.12  christos #endif /* LIBC_SCCS and not lint */
     43   1.1       cgd 
     44  1.12  christos /*
     45  1.12  christos  * glob(3) -- a superset of the one defined in POSIX 1003.2.
     46  1.12  christos  *
     47  1.12  christos  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
     48  1.12  christos  *
     49  1.12  christos  * Optional extra services, controlled by flags not defined by POSIX:
     50  1.12  christos  *
     51  1.12  christos  * GLOB_MAGCHAR:
     52  1.12  christos  *	Set in gl_flags if pattern contained a globbing character.
     53  1.12  christos  * GLOB_NOMAGIC:
     54  1.12  christos  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
     55  1.12  christos  *	not contain any magic characters.  [Used in csh style globbing]
     56  1.12  christos  * GLOB_ALTDIRFUNC:
     57  1.12  christos  *	Use alternately specified directory access functions.
     58  1.12  christos  * GLOB_TILDE:
     59  1.12  christos  *	expand ~user/foo to the /home/dir/of/user/foo
     60  1.12  christos  * GLOB_BRACE:
     61  1.12  christos  *	expand {1,2}{a,b} to 1a 1b 2a 2b
     62  1.19  christos  * GLOB_PERIOD:
     63  1.19  christos  *	allow metacharacters to match leading dots in filenames.
     64  1.22  christos  * GLOB_NO_DOTDIRS:
     65  1.22  christos  *	. and .. are hidden from wildcards, even if GLOB_PERIOD is set.
     66  1.12  christos  * gl_matchc:
     67  1.12  christos  *	Number of matches in the current invocation of glob.
     68  1.12  christos  */
     69  1.12  christos 
     70  1.12  christos #include "namespace.h"
     71  1.12  christos #include <sys/param.h>
     72  1.12  christos #include <sys/stat.h>
     73  1.12  christos 
     74  1.12  christos #include <assert.h>
     75  1.12  christos #include <ctype.h>
     76  1.12  christos #include <dirent.h>
     77  1.12  christos #include <errno.h>
     78  1.12  christos #include <glob.h>
     79  1.12  christos #include <pwd.h>
     80  1.12  christos #include <stdio.h>
     81  1.21  christos #include <stddef.h>
     82  1.12  christos #include <stdlib.h>
     83  1.12  christos #include <string.h>
     84  1.12  christos #include <unistd.h>
     85  1.12  christos 
     86  1.12  christos #ifdef HAVE_NBTOOL_CONFIG_H
     87  1.12  christos #define NO_GETPW_R
     88  1.12  christos #endif
     89  1.12  christos 
     90  1.36     joerg #define	GLOB_LIMIT_STRING	524288	/* number of readdirs */
     91  1.28  christos #define	GLOB_LIMIT_STAT		128	/* number of stat system calls */
     92  1.36     joerg #define	GLOB_LIMIT_READDIR	65536	/* total buffer size of path strings */
     93  1.28  christos #define	GLOB_LIMIT_PATH		1024	/* number of path elements */
     94  1.28  christos #define GLOB_LIMIT_BRACE	128	/* Number of brace calls */
     95  1.28  christos 
     96  1.28  christos struct glob_limit {
     97  1.28  christos 	size_t l_string;
     98  1.28  christos 	size_t l_stat;
     99  1.28  christos 	size_t l_readdir;
    100  1.28  christos 	size_t l_brace;
    101  1.28  christos };
    102  1.23        ad 
    103  1.12  christos /*
    104  1.12  christos  * XXX: For NetBSD 1.4.x compatibility. (kill me l8r)
    105  1.12  christos  */
    106  1.12  christos #ifndef _DIAGASSERT
    107  1.12  christos #define _DIAGASSERT(a)
    108  1.12  christos #endif
    109  1.12  christos 
    110  1.12  christos #define	DOLLAR		'$'
    111  1.12  christos #define	DOT		'.'
    112  1.12  christos #define	EOS		'\0'
    113  1.12  christos #define	LBRACKET	'['
    114  1.12  christos #define	NOT		'!'
    115  1.12  christos #define	QUESTION	'?'
    116  1.12  christos #define	QUOTE		'\\'
    117  1.12  christos #define	RANGE		'-'
    118  1.12  christos #define	RBRACKET	']'
    119  1.12  christos #define	SEP		'/'
    120  1.12  christos #define	STAR		'*'
    121  1.12  christos #define	TILDE		'~'
    122  1.12  christos #define	UNDERSCORE	'_'
    123  1.12  christos #define	LBRACE		'{'
    124  1.12  christos #define	RBRACE		'}'
    125  1.12  christos #define	SLASH		'/'
    126  1.12  christos #define	COMMA		','
    127  1.12  christos 
    128  1.15  christos #ifndef USE_8BIT_CHARS
    129  1.12  christos 
    130  1.12  christos #define	M_QUOTE		0x8000
    131  1.12  christos #define	M_PROTECT	0x4000
    132  1.12  christos #define	M_MASK		0xffff
    133  1.12  christos #define	M_ASCII		0x00ff
    134  1.12  christos 
    135  1.35     lukem typedef unsigned short Char;
    136  1.12  christos 
    137  1.12  christos #else
    138  1.12  christos 
    139  1.15  christos #define	M_QUOTE		(Char)0x80
    140  1.15  christos #define	M_PROTECT	(Char)0x40
    141  1.15  christos #define	M_MASK		(Char)0xff
    142  1.15  christos #define	M_ASCII		(Char)0x7f
    143  1.12  christos 
    144  1.12  christos typedef char Char;
    145  1.12  christos 
    146  1.12  christos #endif
    147  1.12  christos 
    148  1.12  christos 
    149  1.12  christos #define	CHAR(c)		((Char)((c)&M_ASCII))
    150  1.12  christos #define	META(c)		((Char)((c)|M_QUOTE))
    151  1.12  christos #define	M_ALL		META('*')
    152  1.12  christos #define	M_END		META(']')
    153  1.12  christos #define	M_NOT		META('!')
    154  1.12  christos #define	M_ONE		META('?')
    155  1.12  christos #define	M_RNG		META('-')
    156  1.12  christos #define	M_SET		META('[')
    157  1.12  christos #define	ismeta(c)	(((c)&M_QUOTE) != 0)
    158  1.12  christos 
    159  1.12  christos 
    160  1.18  christos static int	 compare(const void *, const void *);
    161  1.18  christos static int	 g_Ctoc(const Char *, char *, size_t);
    162  1.18  christos static int	 g_lstat(Char *, __gl_stat_t  *, glob_t *);
    163  1.18  christos static DIR	*g_opendir(Char *, glob_t *);
    164  1.18  christos static Char	*g_strchr(const Char *, int);
    165  1.18  christos static int	 g_stat(Char *, __gl_stat_t *, glob_t *);
    166  1.33  christos static int	 glob0(const Char *, glob_t *, struct glob_limit *);
    167  1.28  christos static int	 glob1(Char *, glob_t *, struct glob_limit *);
    168  1.27  christos static int	 glob2(Char *, Char *, Char *, const Char *, glob_t *,
    169  1.28  christos     struct glob_limit *);
    170  1.27  christos static int	 glob3(Char *, Char *, Char *, const Char *, const Char *,
    171  1.28  christos     const Char *, glob_t *, struct glob_limit *);
    172  1.28  christos static int	 globextend(const Char *, glob_t *, struct glob_limit *);
    173  1.39  christos static int       globtilde(const Char **, const Char *, Char *, size_t,
    174  1.39  christos     glob_t *);
    175  1.33  christos static int	 globexp1(const Char *, glob_t *, struct glob_limit *);
    176  1.33  christos static int	 globexp2(const Char *, const Char *, glob_t *, int *,
    177  1.28  christos     struct glob_limit *);
    178  1.27  christos static int	 match(const Char *, const Char *, const Char *);
    179  1.12  christos #ifdef DEBUG
    180  1.18  christos static void	 qprintf(const char *, Char *);
    181  1.12  christos #endif
    182  1.12  christos 
    183  1.12  christos int
    184  1.31  christos glob(const char * __restrict pattern, int flags, int (*errfunc)(const char *,
    185  1.31  christos     int), glob_t * __restrict pglob)
    186  1.12  christos {
    187  1.35     lukem 	const unsigned char *patnext;
    188  1.12  christos 	int c;
    189  1.12  christos 	Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
    190  1.28  christos 	struct glob_limit limit = { 0, 0, 0, 0 };
    191  1.12  christos 
    192  1.12  christos 	_DIAGASSERT(pattern != NULL);
    193  1.12  christos 
    194  1.35     lukem 	patnext = (const unsigned char *) pattern;
    195  1.12  christos 	if (!(flags & GLOB_APPEND)) {
    196  1.12  christos 		pglob->gl_pathc = 0;
    197  1.12  christos 		pglob->gl_pathv = NULL;
    198  1.12  christos 		if (!(flags & GLOB_DOOFFS))
    199  1.12  christos 			pglob->gl_offs = 0;
    200  1.12  christos 	}
    201  1.12  christos 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
    202  1.12  christos 	pglob->gl_errfunc = errfunc;
    203  1.12  christos 	pglob->gl_matchc = 0;
    204  1.12  christos 
    205  1.12  christos 	bufnext = patbuf;
    206  1.12  christos 	bufend = bufnext + MAXPATHLEN;
    207  1.12  christos 	if (flags & GLOB_NOESCAPE) {
    208  1.12  christos 		while (bufnext < bufend && (c = *patnext++) != EOS)
    209  1.12  christos 			*bufnext++ = c;
    210  1.12  christos 	} else {
    211  1.12  christos 		/* Protect the quoted characters. */
    212  1.12  christos 		while (bufnext < bufend && (c = *patnext++) != EOS)
    213  1.12  christos 			if (c == QUOTE) {
    214  1.12  christos 				if ((c = *patnext++) == EOS) {
    215  1.12  christos 					c = QUOTE;
    216  1.12  christos 					--patnext;
    217  1.12  christos 				}
    218  1.12  christos 				*bufnext++ = c | M_PROTECT;
    219  1.12  christos 			}
    220  1.12  christos 			else
    221  1.12  christos 				*bufnext++ = c;
    222  1.12  christos 	}
    223  1.12  christos 	*bufnext = EOS;
    224  1.12  christos 
    225  1.12  christos 	if (flags & GLOB_BRACE)
    226  1.33  christos 	    return globexp1(patbuf, pglob, &limit);
    227  1.12  christos 	else
    228  1.33  christos 	    return glob0(patbuf, pglob, &limit);
    229  1.12  christos }
    230  1.12  christos 
    231  1.12  christos /*
    232  1.12  christos  * Expand recursively a glob {} pattern. When there is no more expansion
    233  1.12  christos  * invoke the standard globbing routine to glob the rest of the magic
    234  1.12  christos  * characters
    235  1.12  christos  */
    236  1.12  christos static int
    237  1.33  christos globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
    238  1.12  christos {
    239  1.12  christos 	const Char* ptr = pattern;
    240  1.12  christos 	int rv;
    241  1.12  christos 
    242  1.12  christos 	_DIAGASSERT(pattern != NULL);
    243  1.12  christos 	_DIAGASSERT(pglob != NULL);
    244  1.12  christos 
    245  1.28  christos 	if ((pglob->gl_flags & GLOB_LIMIT) &&
    246  1.28  christos 	    limit->l_brace++ >= GLOB_LIMIT_BRACE) {
    247  1.28  christos 		errno = 0;
    248  1.28  christos 		return GLOB_NOSPACE;
    249  1.28  christos 	}
    250  1.28  christos 
    251  1.12  christos 	/* Protect a single {}, for find(1), like csh */
    252  1.12  christos 	if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
    253  1.33  christos 		return glob0(pattern, pglob, limit);
    254  1.12  christos 
    255  1.12  christos 	while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
    256  1.33  christos 		if (!globexp2(ptr, pattern, pglob, &rv, limit))
    257  1.12  christos 			return rv;
    258  1.12  christos 
    259  1.33  christos 	return glob0(pattern, pglob, limit);
    260  1.12  christos }
    261  1.12  christos 
    262  1.12  christos 
    263  1.12  christos /*
    264  1.12  christos  * Recursive brace globbing helper. Tries to expand a single brace.
    265  1.12  christos  * If it succeeds then it invokes globexp1 with the new pattern.
    266  1.12  christos  * If it fails then it tries to glob the rest of the pattern and returns.
    267  1.12  christos  */
    268  1.12  christos static int
    269  1.33  christos globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
    270  1.28  christos     struct glob_limit *limit)
    271  1.12  christos {
    272  1.12  christos 	int     i;
    273  1.12  christos 	Char   *lm, *ls;
    274  1.12  christos 	const Char *pe, *pm, *pl;
    275  1.12  christos 	Char    patbuf[MAXPATHLEN + 1];
    276  1.12  christos 
    277  1.12  christos 	_DIAGASSERT(ptr != NULL);
    278  1.12  christos 	_DIAGASSERT(pattern != NULL);
    279  1.12  christos 	_DIAGASSERT(pglob != NULL);
    280  1.12  christos 	_DIAGASSERT(rv != NULL);
    281  1.12  christos 
    282  1.12  christos 	/* copy part up to the brace */
    283  1.12  christos 	for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
    284  1.12  christos 		continue;
    285  1.12  christos 	ls = lm;
    286  1.12  christos 
    287  1.12  christos 	/* Find the balanced brace */
    288  1.12  christos 	for (i = 0, pe = ++ptr; *pe; pe++)
    289  1.12  christos 		if (*pe == LBRACKET) {
    290  1.12  christos 			/* Ignore everything between [] */
    291  1.12  christos 			for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
    292  1.12  christos 				continue;
    293  1.12  christos 			if (*pe == EOS) {
    294  1.12  christos 				/*
    295  1.12  christos 				 * We could not find a matching RBRACKET.
    296  1.12  christos 				 * Ignore and just look for RBRACE
    297  1.12  christos 				 */
    298  1.12  christos 				pe = pm;
    299  1.12  christos 			}
    300  1.12  christos 		}
    301  1.12  christos 		else if (*pe == LBRACE)
    302  1.12  christos 			i++;
    303  1.12  christos 		else if (*pe == RBRACE) {
    304  1.12  christos 			if (i == 0)
    305  1.12  christos 				break;
    306  1.12  christos 			i--;
    307  1.12  christos 		}
    308  1.12  christos 
    309  1.12  christos 	/* Non matching braces; just glob the pattern */
    310  1.12  christos 	if (i != 0 || *pe == EOS) {
    311  1.12  christos 		/*
    312  1.12  christos 		 * we use `pattern', not `patbuf' here so that that
    313  1.12  christos 		 * unbalanced braces are passed to the match
    314  1.12  christos 		 */
    315  1.33  christos 		*rv = glob0(pattern, pglob, limit);
    316  1.12  christos 		return 0;
    317  1.12  christos 	}
    318  1.12  christos 
    319  1.12  christos 	for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
    320  1.12  christos 		switch (*pm) {
    321  1.12  christos 		case LBRACKET:
    322  1.12  christos 			/* Ignore everything between [] */
    323  1.12  christos 			for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
    324  1.12  christos 				continue;
    325  1.12  christos 			if (*pm == EOS) {
    326  1.12  christos 				/*
    327  1.12  christos 				 * We could not find a matching RBRACKET.
    328  1.12  christos 				 * Ignore and just look for RBRACE
    329  1.12  christos 				 */
    330  1.12  christos 				pm = pl;
    331  1.12  christos 			}
    332  1.12  christos 			break;
    333  1.12  christos 
    334  1.12  christos 		case LBRACE:
    335  1.12  christos 			i++;
    336  1.12  christos 			break;
    337  1.12  christos 
    338  1.12  christos 		case RBRACE:
    339  1.12  christos 			if (i) {
    340  1.12  christos 				i--;
    341  1.12  christos 				break;
    342  1.12  christos 			}
    343  1.12  christos 			/* FALLTHROUGH */
    344  1.12  christos 		case COMMA:
    345  1.12  christos 			if (i && *pm == COMMA)
    346  1.12  christos 				break;
    347  1.12  christos 			else {
    348  1.12  christos 				/* Append the current string */
    349  1.12  christos 				for (lm = ls; (pl < pm); *lm++ = *pl++)
    350  1.12  christos 					continue;
    351  1.12  christos 				/*
    352  1.12  christos 				 * Append the rest of the pattern after the
    353  1.12  christos 				 * closing brace
    354  1.12  christos 				 */
    355  1.12  christos 				for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
    356  1.12  christos 					continue;
    357  1.12  christos 
    358  1.12  christos 				/* Expand the current pattern */
    359  1.12  christos #ifdef DEBUG
    360  1.27  christos 				qprintf("globexp2", patbuf);
    361  1.12  christos #endif
    362  1.33  christos 				*rv = globexp1(patbuf, pglob, limit);
    363  1.12  christos 
    364  1.12  christos 				/* move after the comma, to the next string */
    365  1.12  christos 				pl = pm + 1;
    366  1.12  christos 			}
    367  1.12  christos 			break;
    368  1.12  christos 
    369  1.12  christos 		default:
    370  1.12  christos 			break;
    371  1.12  christos 		}
    372  1.12  christos 	}
    373  1.12  christos 	*rv = 0;
    374  1.12  christos 	return 0;
    375  1.12  christos }
    376  1.12  christos 
    377  1.12  christos 
    378  1.12  christos 
    379  1.12  christos /*
    380  1.12  christos  * expand tilde from the passwd file.
    381  1.12  christos  */
    382  1.39  christos static int
    383  1.39  christos globtilde(const Char **qpatnext, const Char *pattern, Char *patbuf,
    384  1.39  christos     size_t patsize, glob_t *pglob)
    385  1.12  christos {
    386  1.12  christos 	struct passwd *pwd;
    387  1.12  christos 	const char *h;
    388  1.12  christos 	const Char *p;
    389  1.12  christos 	Char *b;
    390  1.12  christos 	char *d;
    391  1.12  christos 	Char *pend = &patbuf[patsize / sizeof(Char)];
    392  1.12  christos #ifndef NO_GETPW_R
    393  1.12  christos 	struct passwd pwres;
    394  1.12  christos 	char pwbuf[1024];
    395  1.12  christos #endif
    396  1.12  christos 
    397  1.12  christos 	pend--;
    398  1.12  christos 
    399  1.12  christos 	_DIAGASSERT(pattern != NULL);
    400  1.12  christos 	_DIAGASSERT(patbuf != NULL);
    401  1.12  christos 	_DIAGASSERT(pglob != NULL);
    402  1.39  christos 	*qpatnext = pattern;
    403  1.12  christos 
    404  1.12  christos 	if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
    405  1.39  christos 		return 0;
    406  1.12  christos 
    407  1.12  christos 	/* Copy up to the end of the string or / */
    408  1.12  christos 	for (p = pattern + 1, d = (char *)(void *)patbuf;
    409  1.12  christos 	     d < (char *)(void *)pend && *p && *p != SLASH;
    410  1.12  christos 	     *d++ = *p++)
    411  1.12  christos 		continue;
    412  1.12  christos 
    413  1.12  christos 	if (d == (char *)(void *)pend)
    414  1.39  christos 		return GLOB_ABEND;
    415  1.12  christos 
    416  1.12  christos 	*d = EOS;
    417  1.12  christos 	d = (char *)(void *)patbuf;
    418  1.12  christos 
    419  1.12  christos 	if (*d == EOS) {
    420  1.12  christos 		/*
    421  1.12  christos 		 * handle a plain ~ or ~/ by expanding $HOME
    422  1.12  christos 		 * first and then trying the password file
    423  1.12  christos 		 */
    424  1.12  christos 		if ((h = getenv("HOME")) == NULL) {
    425  1.12  christos #ifdef NO_GETPW_R
    426  1.12  christos 			if ((pwd = getpwuid(getuid())) == NULL)
    427  1.12  christos #else
    428  1.12  christos 			if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
    429  1.12  christos 			    &pwd) != 0 || pwd == NULL)
    430  1.12  christos #endif
    431  1.39  christos 				goto nouser;
    432  1.39  christos 			h = pwd->pw_dir;
    433  1.12  christos 		}
    434  1.12  christos 	}
    435  1.12  christos 	else {
    436  1.12  christos 		/*
    437  1.12  christos 		 * Expand a ~user
    438  1.12  christos 		 */
    439  1.12  christos #ifdef NO_GETPW_R
    440  1.12  christos 		if ((pwd = getpwnam(d)) == NULL)
    441  1.12  christos #else
    442  1.12  christos 		if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
    443  1.12  christos 		    pwd == NULL)
    444  1.12  christos #endif
    445  1.39  christos 			goto nouser;
    446  1.39  christos 		h = pwd->pw_dir;
    447  1.12  christos 	}
    448  1.12  christos 
    449  1.12  christos 	/* Copy the home directory */
    450  1.12  christos 	for (b = patbuf; b < pend && *h; *b++ = *h++)
    451  1.12  christos 		continue;
    452  1.12  christos 
    453  1.12  christos 	if (b == pend)
    454  1.39  christos 		return GLOB_ABEND;
    455  1.12  christos 
    456  1.12  christos 	/* Append the rest of the pattern */
    457  1.12  christos 	while (b < pend && (*b++ = *p++) != EOS)
    458  1.12  christos 		continue;
    459  1.12  christos 
    460  1.12  christos 	if (b == pend)
    461  1.39  christos 		return GLOB_ABEND;
    462  1.12  christos 
    463  1.39  christos 	*qpatnext = patbuf;
    464  1.39  christos 	return 0;
    465  1.39  christos nouser:
    466  1.39  christos 	return (pglob->gl_flags & GLOB_TILDE_CHECK) ?  GLOB_NOMATCH : 0;
    467  1.12  christos }
    468  1.12  christos 
    469  1.12  christos 
    470  1.12  christos /*
    471  1.12  christos  * The main glob() routine: compiles the pattern (optionally processing
    472  1.12  christos  * quotes), calls glob1() to do the real pattern matching, and finally
    473  1.12  christos  * sorts the list (unless unsorted operation is requested).  Returns 0
    474  1.12  christos  * if things went well, nonzero if errors occurred.  It is not an error
    475  1.12  christos  * to find no matches.
    476  1.12  christos  */
    477  1.12  christos static int
    478  1.33  christos glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
    479  1.12  christos {
    480  1.12  christos 	const Char *qpatnext;
    481  1.16  christos 	int c, error;
    482  1.16  christos 	__gl_size_t oldpathc;
    483  1.12  christos 	Char *bufnext, patbuf[MAXPATHLEN+1];
    484  1.12  christos 
    485  1.12  christos 	_DIAGASSERT(pattern != NULL);
    486  1.12  christos 	_DIAGASSERT(pglob != NULL);
    487  1.12  christos 
    488  1.39  christos 	if ((error = globtilde(&qpatnext, pattern, patbuf, sizeof(patbuf),
    489  1.39  christos 	    pglob)) != 0)
    490  1.39  christos 		return error;
    491  1.12  christos 	oldpathc = pglob->gl_pathc;
    492  1.12  christos 	bufnext = patbuf;
    493  1.12  christos 
    494  1.12  christos 	/* We don't need to check for buffer overflow any more. */
    495  1.12  christos 	while ((c = *qpatnext++) != EOS) {
    496  1.12  christos 		switch (c) {
    497  1.12  christos 		case LBRACKET:
    498  1.12  christos 			c = *qpatnext;
    499  1.12  christos 			if (c == NOT)
    500  1.12  christos 				++qpatnext;
    501  1.12  christos 			if (*qpatnext == EOS ||
    502  1.12  christos 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
    503  1.12  christos 				*bufnext++ = LBRACKET;
    504  1.12  christos 				if (c == NOT)
    505  1.12  christos 					--qpatnext;
    506  1.12  christos 				break;
    507  1.12  christos 			}
    508  1.12  christos 			*bufnext++ = M_SET;
    509  1.12  christos 			if (c == NOT)
    510  1.12  christos 				*bufnext++ = M_NOT;
    511  1.12  christos 			c = *qpatnext++;
    512  1.12  christos 			do {
    513  1.12  christos 				*bufnext++ = CHAR(c);
    514  1.12  christos 				if (*qpatnext == RANGE &&
    515  1.12  christos 				    (c = qpatnext[1]) != RBRACKET) {
    516  1.12  christos 					*bufnext++ = M_RNG;
    517  1.12  christos 					*bufnext++ = CHAR(c);
    518  1.12  christos 					qpatnext += 2;
    519  1.12  christos 				}
    520  1.12  christos 			} while ((c = *qpatnext++) != RBRACKET);
    521  1.12  christos 			pglob->gl_flags |= GLOB_MAGCHAR;
    522  1.12  christos 			*bufnext++ = M_END;
    523  1.12  christos 			break;
    524  1.12  christos 		case QUESTION:
    525  1.12  christos 			pglob->gl_flags |= GLOB_MAGCHAR;
    526  1.12  christos 			*bufnext++ = M_ONE;
    527  1.12  christos 			break;
    528  1.12  christos 		case STAR:
    529  1.12  christos 			pglob->gl_flags |= GLOB_MAGCHAR;
    530  1.27  christos 			/* collapse adjacent stars to one [or three if globstar]
    531  1.12  christos 			 * to avoid exponential behavior
    532  1.12  christos 			 */
    533  1.27  christos 			if (bufnext == patbuf || bufnext[-1] != M_ALL ||
    534  1.27  christos 			    ((pglob->gl_flags & GLOB_STAR) != 0 &&
    535  1.27  christos 			    (bufnext - 1 == patbuf || bufnext[-2] != M_ALL ||
    536  1.27  christos 			    bufnext - 2 == patbuf || bufnext[-3] != M_ALL)))
    537  1.12  christos 				*bufnext++ = M_ALL;
    538  1.12  christos 			break;
    539  1.12  christos 		default:
    540  1.12  christos 			*bufnext++ = CHAR(c);
    541  1.12  christos 			break;
    542  1.12  christos 		}
    543  1.12  christos 	}
    544  1.12  christos 	*bufnext = EOS;
    545  1.12  christos #ifdef DEBUG
    546  1.27  christos 	qprintf("glob0", patbuf);
    547  1.12  christos #endif
    548  1.12  christos 
    549  1.25  christos 	if ((error = glob1(patbuf, pglob, limit)) != 0)
    550  1.18  christos 		return error;
    551  1.12  christos 
    552  1.12  christos 	if (pglob->gl_pathc == oldpathc) {
    553  1.12  christos 		/*
    554  1.12  christos 		 * If there was no match we are going to append the pattern
    555  1.12  christos 		 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
    556  1.12  christos 		 * specified and the pattern did not contain any magic
    557  1.12  christos 		 * characters GLOB_NOMAGIC is there just for compatibility
    558  1.12  christos 		 * with csh.
    559  1.12  christos 		 */
    560  1.12  christos 		if ((pglob->gl_flags & GLOB_NOCHECK) ||
    561  1.12  christos 		    ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR))
    562  1.12  christos 		     == GLOB_NOMAGIC)) {
    563  1.33  christos 			return globextend(pattern, pglob, limit);
    564  1.12  christos 		} else {
    565  1.18  christos 			return GLOB_NOMATCH;
    566  1.12  christos 		}
    567  1.12  christos 	} else if (!(pglob->gl_flags & GLOB_NOSORT)) {
    568  1.12  christos 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
    569  1.12  christos 		    (size_t)pglob->gl_pathc - oldpathc, sizeof(char *),
    570  1.12  christos 		    compare);
    571  1.12  christos 	}
    572  1.12  christos 
    573  1.18  christos 	return 0;
    574  1.12  christos }
    575  1.12  christos 
    576  1.12  christos static int
    577  1.18  christos compare(const void *p, const void *q)
    578  1.12  christos {
    579  1.12  christos 
    580  1.12  christos 	_DIAGASSERT(p != NULL);
    581  1.12  christos 	_DIAGASSERT(q != NULL);
    582  1.12  christos 
    583  1.18  christos 	return strcoll(*(const char * const *)p, *(const char * const *)q);
    584  1.12  christos }
    585  1.12  christos 
    586  1.12  christos static int
    587  1.28  christos glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
    588  1.12  christos {
    589  1.12  christos 	Char pathbuf[MAXPATHLEN+1];
    590  1.12  christos 
    591  1.12  christos 	_DIAGASSERT(pattern != NULL);
    592  1.12  christos 	_DIAGASSERT(pglob != NULL);
    593  1.12  christos 
    594  1.12  christos 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
    595  1.12  christos 	if (*pattern == EOS)
    596  1.18  christos 		return 0;
    597  1.12  christos 	/*
    598  1.12  christos 	 * we save one character so that we can use ptr >= limit,
    599  1.12  christos 	 * in the general case when we are appending non nul chars only.
    600  1.12  christos 	 */
    601  1.18  christos 	return glob2(pathbuf, pathbuf,
    602  1.18  christos 	    pathbuf + (sizeof(pathbuf) / sizeof(*pathbuf)) - 1, pattern,
    603  1.18  christos 	    pglob, limit);
    604  1.12  christos }
    605  1.12  christos 
    606  1.12  christos /*
    607  1.12  christos  * The functions glob2 and glob3 are mutually recursive; there is one level
    608  1.12  christos  * of recursion for each segment in the pattern that contains one or more
    609  1.12  christos  * meta characters.
    610  1.12  christos  */
    611  1.12  christos static int
    612  1.27  christos glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
    613  1.28  christos     glob_t *pglob, struct glob_limit *limit)
    614  1.12  christos {
    615  1.12  christos 	__gl_stat_t sb;
    616  1.27  christos 	const Char *p;
    617  1.27  christos 	Char *q;
    618  1.12  christos 	int anymeta;
    619  1.12  christos 
    620  1.12  christos 	_DIAGASSERT(pathbuf != NULL);
    621  1.12  christos 	_DIAGASSERT(pathend != NULL);
    622  1.12  christos 	_DIAGASSERT(pattern != NULL);
    623  1.12  christos 	_DIAGASSERT(pglob != NULL);
    624  1.12  christos 
    625  1.27  christos #ifdef DEBUG
    626  1.27  christos 	qprintf("glob2", pathbuf);
    627  1.27  christos #endif
    628  1.12  christos 	/*
    629  1.12  christos 	 * Loop over pattern segments until end of pattern or until
    630  1.12  christos 	 * segment with meta character found.
    631  1.12  christos 	 */
    632  1.12  christos 	for (anymeta = 0;;) {
    633  1.12  christos 		if (*pattern == EOS) {		/* End of pattern? */
    634  1.12  christos 			*pathend = EOS;
    635  1.12  christos 			if (g_lstat(pathbuf, &sb, pglob))
    636  1.18  christos 				return 0;
    637  1.12  christos 
    638  1.26  christos 			if ((pglob->gl_flags & GLOB_LIMIT) &&
    639  1.28  christos 			    limit->l_stat++ >= GLOB_LIMIT_STAT) {
    640  1.26  christos 				errno = 0;
    641  1.26  christos 				*pathend++ = SEP;
    642  1.26  christos 				*pathend = EOS;
    643  1.26  christos 				return GLOB_NOSPACE;
    644  1.26  christos 			}
    645  1.12  christos 			if (((pglob->gl_flags & GLOB_MARK) &&
    646  1.12  christos 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
    647  1.12  christos 			    (S_ISLNK(sb.st_mode) &&
    648  1.12  christos 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
    649  1.12  christos 			    S_ISDIR(sb.st_mode)))) {
    650  1.12  christos 				if (pathend >= pathlim)
    651  1.18  christos 					return GLOB_ABORTED;
    652  1.12  christos 				*pathend++ = SEP;
    653  1.12  christos 				*pathend = EOS;
    654  1.12  christos 			}
    655  1.12  christos 			++pglob->gl_matchc;
    656  1.18  christos 			return globextend(pathbuf, pglob, limit);
    657  1.12  christos 		}
    658  1.12  christos 
    659  1.12  christos 		/* Find end of next segment, copy tentatively to pathend. */
    660  1.12  christos 		q = pathend;
    661  1.12  christos 		p = pattern;
    662  1.12  christos 		while (*p != EOS && *p != SEP) {
    663  1.12  christos 			if (ismeta(*p))
    664  1.12  christos 				anymeta = 1;
    665  1.12  christos 			if (q >= pathlim)
    666  1.12  christos 				return GLOB_ABORTED;
    667  1.12  christos 			*q++ = *p++;
    668  1.12  christos 		}
    669  1.12  christos 
    670  1.34  christos                 if (!anymeta) {
    671  1.12  christos 			pathend = q;
    672  1.12  christos 			pattern = p;
    673  1.12  christos 			while (*pattern == SEP) {
    674  1.12  christos 				if (pathend >= pathlim)
    675  1.12  christos 					return GLOB_ABORTED;
    676  1.12  christos 				*pathend++ = *pattern++;
    677  1.12  christos 			}
    678  1.12  christos 		} else			/* Need expansion, recurse. */
    679  1.18  christos 			return glob3(pathbuf, pathend, pathlim, pattern, p,
    680  1.27  christos 			    pattern, pglob, limit);
    681  1.12  christos 	}
    682  1.12  christos 	/* NOTREACHED */
    683  1.12  christos }
    684  1.12  christos 
    685  1.12  christos static int
    686  1.27  christos glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
    687  1.27  christos     const Char *restpattern, const Char *pglobstar, glob_t *pglob,
    688  1.28  christos     struct glob_limit *limit)
    689  1.12  christos {
    690  1.12  christos 	struct dirent *dp;
    691  1.12  christos 	DIR *dirp;
    692  1.27  christos 	__gl_stat_t sbuf;
    693  1.12  christos 	int error;
    694  1.12  christos 	char buf[MAXPATHLEN];
    695  1.27  christos 	int globstar = 0;
    696  1.27  christos 	int chase_symlinks = 0;
    697  1.27  christos 	const Char *termstar = NULL;
    698  1.12  christos 
    699  1.12  christos 	/*
    700  1.12  christos 	 * The readdirfunc declaration can't be prototyped, because it is
    701  1.12  christos 	 * assigned, below, to two functions which are prototyped in glob.h
    702  1.12  christos 	 * and dirent.h as taking pointers to differently typed opaque
    703  1.12  christos 	 * structures.
    704  1.12  christos 	 */
    705  1.18  christos 	struct dirent *(*readdirfunc)(void *);
    706  1.12  christos 
    707  1.12  christos 	_DIAGASSERT(pathbuf != NULL);
    708  1.12  christos 	_DIAGASSERT(pathend != NULL);
    709  1.12  christos 	_DIAGASSERT(pattern != NULL);
    710  1.12  christos 	_DIAGASSERT(restpattern != NULL);
    711  1.12  christos 	_DIAGASSERT(pglob != NULL);
    712  1.12  christos 
    713  1.12  christos 	*pathend = EOS;
    714  1.12  christos 	errno = 0;
    715  1.12  christos 
    716  1.27  christos 	while (pglobstar < restpattern) {
    717  1.27  christos 		if ((pglobstar[0] & M_MASK) == M_ALL &&
    718  1.27  christos 		    (pglobstar[1] & M_MASK) == M_ALL) {
    719  1.27  christos 			globstar = 1;
    720  1.27  christos 			chase_symlinks = (pglobstar[2] & M_MASK) == M_ALL;
    721  1.27  christos 			termstar = pglobstar + (2 + chase_symlinks);
    722  1.27  christos 			break;
    723  1.27  christos 		}
    724  1.27  christos 		pglobstar++;
    725  1.27  christos 	}
    726  1.27  christos 
    727  1.27  christos 	if (globstar) {
    728  1.27  christos 		error = pglobstar == pattern && termstar == restpattern ?
    729  1.27  christos 		    *restpattern == EOS ?
    730  1.27  christos 		    glob2(pathbuf, pathend, pathlim, restpattern - 1, pglob,
    731  1.27  christos 		    limit) :
    732  1.27  christos 		    glob2(pathbuf, pathend, pathlim, restpattern + 1, pglob,
    733  1.27  christos 		    limit) :
    734  1.27  christos 		    glob3(pathbuf, pathend, pathlim, pattern, restpattern,
    735  1.27  christos 		    termstar, pglob, limit);
    736  1.27  christos 		if (error)
    737  1.27  christos 			return error;
    738  1.27  christos 		*pathend = EOS;
    739  1.27  christos 	}
    740  1.27  christos 
    741  1.27  christos 	if (*pathbuf && (g_lstat(pathbuf, &sbuf, pglob) ||
    742  1.27  christos 	    !S_ISDIR(sbuf.st_mode)
    743  1.27  christos #ifdef S_IFLINK
    744  1.27  christos 	     && ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode))
    745  1.27  christos #endif
    746  1.27  christos 	    ))
    747  1.27  christos 		return 0;
    748  1.27  christos 
    749  1.12  christos 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
    750  1.12  christos 		if (pglob->gl_errfunc) {
    751  1.12  christos 			if (g_Ctoc(pathbuf, buf, sizeof(buf)))
    752  1.18  christos 				return GLOB_ABORTED;
    753  1.12  christos 			if (pglob->gl_errfunc(buf, errno) ||
    754  1.12  christos 			    pglob->gl_flags & GLOB_ERR)
    755  1.18  christos 				return GLOB_ABORTED;
    756  1.12  christos 		}
    757  1.12  christos 		/*
    758  1.12  christos 		 * Posix/XOpen: glob should return when it encounters a
    759  1.12  christos 		 * directory that it cannot open or read
    760  1.12  christos 		 * XXX: Should we ignore ENOTDIR and ENOENT though?
    761  1.12  christos 		 * I think that Posix had in mind EPERM...
    762  1.12  christos 		 */
    763  1.12  christos 		if (pglob->gl_flags & GLOB_ERR)
    764  1.18  christos 			return GLOB_ABORTED;
    765  1.12  christos 
    766  1.18  christos 		return 0;
    767  1.12  christos 	}
    768  1.12  christos 
    769  1.12  christos 	error = 0;
    770  1.12  christos 
    771  1.12  christos 	/* Search directory for matching names. */
    772  1.12  christos 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    773  1.12  christos 		readdirfunc = pglob->gl_readdir;
    774  1.12  christos 	else
    775  1.30  christos 		readdirfunc = (struct dirent *(*)(void *)) readdir;
    776  1.12  christos 	while ((dp = (*readdirfunc)(dirp)) != NULL) {
    777  1.35     lukem 		unsigned char *sc;
    778  1.12  christos 		Char *dc;
    779  1.12  christos 
    780  1.26  christos 		if ((pglob->gl_flags & GLOB_LIMIT) &&
    781  1.28  christos 		    limit->l_readdir++ >= GLOB_LIMIT_READDIR) {
    782  1.26  christos 			errno = 0;
    783  1.26  christos 			*pathend++ = SEP;
    784  1.26  christos 			*pathend = EOS;
    785  1.30  christos 			error = GLOB_NOSPACE;
    786  1.30  christos 			break;
    787  1.26  christos 		}
    788  1.26  christos 
    789  1.19  christos 		/*
    790  1.19  christos 		 * Initial DOT must be matched literally, unless we have
    791  1.19  christos 		 * GLOB_PERIOD set.
    792  1.19  christos 		 */
    793  1.19  christos 		if ((pglob->gl_flags & GLOB_PERIOD) == 0)
    794  1.19  christos 			if (dp->d_name[0] == DOT && *pattern != DOT)
    795  1.19  christos 				continue;
    796  1.12  christos 		/*
    797  1.22  christos 		 * If GLOB_NO_DOTDIRS is set, . and .. vanish.
    798  1.22  christos 		 */
    799  1.22  christos 		if ((pglob->gl_flags & GLOB_NO_DOTDIRS) &&
    800  1.22  christos 		    (dp->d_name[0] == DOT) &&
    801  1.22  christos 		    ((dp->d_name[1] == EOS) ||
    802  1.22  christos 		     ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS))))
    803  1.22  christos 			continue;
    804  1.22  christos 		/*
    805  1.12  christos 		 * The resulting string contains EOS, so we can
    806  1.12  christos 		 * use the pathlim character, if it is the nul
    807  1.12  christos 		 */
    808  1.35     lukem 		for (sc = (unsigned char *) dp->d_name, dc = pathend;
    809  1.12  christos 		     dc <= pathlim && (*dc++ = *sc++) != EOS;)
    810  1.12  christos 			continue;
    811  1.12  christos 
    812  1.12  christos 		/*
    813  1.12  christos 		 * Have we filled the buffer without seeing EOS?
    814  1.12  christos 		 */
    815  1.12  christos 		if (dc > pathlim && *pathlim != EOS) {
    816  1.12  christos 			/*
    817  1.12  christos 			 * Abort when requested by caller, otherwise
    818  1.12  christos 			 * reset pathend back to last SEP and continue
    819  1.12  christos 			 * with next dir entry.
    820  1.12  christos 			 */
    821  1.12  christos 			if (pglob->gl_flags & GLOB_ERR) {
    822  1.12  christos 				error = GLOB_ABORTED;
    823  1.12  christos 				break;
    824  1.12  christos 			}
    825  1.12  christos 			else {
    826  1.12  christos 				*pathend = EOS;
    827  1.12  christos 				continue;
    828  1.12  christos 			}
    829  1.12  christos 		}
    830  1.12  christos 
    831  1.27  christos 		if (globstar) {
    832  1.27  christos #ifdef S_IFLNK
    833  1.27  christos 			if (!chase_symlinks &&
    834  1.27  christos 			    (g_lstat(pathbuf, &sbuf, pglob) ||
    835  1.27  christos 			    S_ISLNK(sbuf.st_mode)))
    836  1.27  christos 				continue;
    837  1.27  christos #endif
    838  1.27  christos 
    839  1.27  christos 			if (!match(pathend, pattern, termstar))
    840  1.27  christos 				continue;
    841  1.27  christos 
    842  1.27  christos 			if (--dc < pathlim - 2)
    843  1.27  christos 				*dc++ = SEP;
    844  1.27  christos 			*dc = EOS;
    845  1.27  christos 			error = glob2(pathbuf, dc, pathlim, pglobstar,
    846  1.27  christos 			    pglob, limit);
    847  1.27  christos 			if (error)
    848  1.27  christos 				break;
    849  1.12  christos 			*pathend = EOS;
    850  1.27  christos 		} else {
    851  1.27  christos 			if (!match(pathend, pattern, restpattern)) {
    852  1.27  christos 				*pathend = EOS;
    853  1.27  christos 				continue;
    854  1.27  christos 			}
    855  1.27  christos 			error = glob2(pathbuf, --dc, pathlim, restpattern,
    856  1.27  christos 			    pglob, limit);
    857  1.27  christos 			if (error)
    858  1.27  christos 				break;
    859  1.12  christos 		}
    860  1.12  christos 	}
    861  1.12  christos 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
    862  1.12  christos 		(*pglob->gl_closedir)(dirp);
    863  1.12  christos 	else
    864  1.12  christos 		closedir(dirp);
    865  1.12  christos 
    866  1.12  christos 	/*
    867  1.12  christos 	 * Again Posix X/Open issue with regards to error handling.
    868  1.12  christos 	 */
    869  1.12  christos 	if ((error || errno) && (pglob->gl_flags & GLOB_ERR))
    870  1.18  christos 		return GLOB_ABORTED;
    871  1.12  christos 
    872  1.18  christos 	return error;
    873  1.12  christos }
    874  1.12  christos 
    875  1.12  christos 
    876  1.12  christos /*
    877  1.17  christos  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
    878  1.12  christos  * add the new item, and update gl_pathc.
    879  1.12  christos  *
    880  1.12  christos  * This assumes the BSD realloc, which only copies the block when its size
    881  1.12  christos  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
    882  1.12  christos  * behavior.
    883  1.12  christos  *
    884  1.12  christos  * Return 0 if new item added, error code if memory couldn't be allocated.
    885  1.12  christos  *
    886  1.12  christos  * Invariant of the glob_t structure:
    887  1.12  christos  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
    888  1.12  christos  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
    889  1.12  christos  */
    890  1.12  christos static int
    891  1.28  christos globextend(const Char *path, glob_t *pglob, struct glob_limit *limit)
    892  1.12  christos {
    893  1.12  christos 	char **pathv;
    894  1.16  christos 	size_t i, newsize, len;
    895  1.12  christos 	char *copy;
    896  1.12  christos 	const Char *p;
    897  1.12  christos 
    898  1.12  christos 	_DIAGASSERT(path != NULL);
    899  1.12  christos 	_DIAGASSERT(pglob != NULL);
    900  1.12  christos 
    901  1.12  christos 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
    902  1.28  christos 	if ((pglob->gl_flags & GLOB_LIMIT) &&
    903  1.28  christos 	    newsize > GLOB_LIMIT_PATH * sizeof(*pathv))
    904  1.28  christos 		goto nospace;
    905  1.12  christos 	pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) :
    906  1.12  christos 	    malloc(newsize);
    907  1.12  christos 	if (pathv == NULL)
    908  1.18  christos 		return GLOB_NOSPACE;
    909  1.12  christos 
    910  1.12  christos 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
    911  1.12  christos 		/* first time around -- clear initial gl_offs items */
    912  1.12  christos 		pathv += pglob->gl_offs;
    913  1.16  christos 		for (i = pglob->gl_offs + 1; --i > 0; )
    914  1.12  christos 			*--pathv = NULL;
    915  1.12  christos 	}
    916  1.12  christos 	pglob->gl_pathv = pathv;
    917  1.12  christos 
    918  1.12  christos 	for (p = path; *p++;)
    919  1.12  christos 		continue;
    920  1.12  christos 	len = (size_t)(p - path);
    921  1.28  christos 	limit->l_string += len;
    922  1.12  christos 	if ((copy = malloc(len)) != NULL) {
    923  1.12  christos 		if (g_Ctoc(path, copy, len)) {
    924  1.12  christos 			free(copy);
    925  1.18  christos 			return GLOB_ABORTED;
    926  1.12  christos 		}
    927  1.12  christos 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
    928  1.12  christos 	}
    929  1.12  christos 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
    930  1.12  christos 
    931  1.26  christos 	if ((pglob->gl_flags & GLOB_LIMIT) &&
    932  1.28  christos 	    (newsize + limit->l_string) >= GLOB_LIMIT_STRING)
    933  1.28  christos 		goto nospace;
    934  1.12  christos 
    935  1.18  christos 	return copy == NULL ? GLOB_NOSPACE : 0;
    936  1.28  christos nospace:
    937  1.28  christos 	errno = 0;
    938  1.28  christos 	return GLOB_NOSPACE;
    939  1.12  christos }
    940  1.12  christos 
    941  1.12  christos 
    942  1.12  christos /*
    943  1.37  christos  * pattern matching function for filenames.
    944  1.12  christos  */
    945  1.12  christos static int
    946  1.27  christos match(const Char *name, const Char *pat, const Char *patend)
    947  1.12  christos {
    948  1.12  christos 	int ok, negate_range;
    949  1.12  christos 	Char c, k;
    950  1.37  christos 	const Char *patNext, *nameNext, *nameStart, *nameEnd;
    951  1.12  christos 
    952  1.12  christos 	_DIAGASSERT(name != NULL);
    953  1.12  christos 	_DIAGASSERT(pat != NULL);
    954  1.12  christos 	_DIAGASSERT(patend != NULL);
    955  1.37  christos 	patNext = pat;
    956  1.37  christos 	nameStart = nameNext = name;
    957  1.37  christos 	nameEnd = NULL;
    958  1.37  christos 
    959  1.37  christos 	while (pat < patend || *name) {
    960  1.37  christos 		c = *pat;
    961  1.37  christos 		if (*name == EOS)
    962  1.37  christos 			nameEnd = name;
    963  1.12  christos 		switch (c & M_MASK) {
    964  1.12  christos 		case M_ALL:
    965  1.38  christos 			while ((pat[1] & M_MASK) == M_ALL) pat++;
    966  1.37  christos 			patNext = pat;
    967  1.37  christos 			nameNext = name + 1;
    968  1.37  christos 			pat++;
    969  1.37  christos 			continue;
    970  1.12  christos 		case M_ONE:
    971  1.37  christos 			if (*name == EOS)
    972  1.37  christos 				break;
    973  1.37  christos 			pat++;
    974  1.37  christos 			name++;
    975  1.37  christos 			continue;
    976  1.12  christos 		case M_SET:
    977  1.12  christos 			ok = 0;
    978  1.37  christos 			if ((k = *name) == EOS)
    979  1.37  christos 				break;
    980  1.37  christos 			pat++;
    981  1.37  christos 			name++;
    982  1.12  christos 			if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
    983  1.12  christos 				++pat;
    984  1.12  christos 			while (((c = *pat++) & M_MASK) != M_END)
    985  1.12  christos 				if ((*pat & M_MASK) == M_RNG) {
    986  1.12  christos 					if (c <= k && k <= pat[1])
    987  1.12  christos 						ok = 1;
    988  1.12  christos 					pat += 2;
    989  1.12  christos 				} else if (c == k)
    990  1.12  christos 					ok = 1;
    991  1.12  christos 			if (ok == negate_range)
    992  1.37  christos 				break;
    993  1.37  christos 			continue;
    994  1.12  christos 		default:
    995  1.37  christos 			if (*name != c)
    996  1.37  christos 				break;
    997  1.37  christos 			pat++;
    998  1.37  christos 			name++;
    999  1.37  christos 			continue;
   1000  1.37  christos 		}
   1001  1.37  christos 		if (nameNext != nameStart
   1002  1.37  christos 		    && (nameEnd == NULL || nameNext <= nameEnd)) {
   1003  1.37  christos 			pat = patNext;
   1004  1.37  christos 			name = nameNext;
   1005  1.37  christos 			continue;
   1006  1.12  christos 		}
   1007  1.37  christos 		return 0;
   1008  1.12  christos 	}
   1009  1.37  christos 	return 1;
   1010  1.12  christos }
   1011  1.12  christos 
   1012  1.12  christos /* Free allocated data belonging to a glob_t structure. */
   1013  1.12  christos void
   1014  1.18  christos globfree(glob_t *pglob)
   1015  1.12  christos {
   1016  1.16  christos 	size_t i;
   1017  1.12  christos 	char **pp;
   1018  1.12  christos 
   1019  1.12  christos 	_DIAGASSERT(pglob != NULL);
   1020  1.12  christos 
   1021  1.12  christos 	if (pglob->gl_pathv != NULL) {
   1022  1.12  christos 		pp = pglob->gl_pathv + pglob->gl_offs;
   1023  1.12  christos 		for (i = pglob->gl_pathc; i--; ++pp)
   1024  1.12  christos 			if (*pp)
   1025  1.12  christos 				free(*pp);
   1026  1.12  christos 		free(pglob->gl_pathv);
   1027  1.12  christos 		pglob->gl_pathv = NULL;
   1028  1.12  christos 		pglob->gl_pathc = 0;
   1029  1.12  christos 	}
   1030  1.12  christos }
   1031  1.12  christos 
   1032  1.24  christos #ifndef __LIBC12_SOURCE__
   1033  1.24  christos int
   1034  1.24  christos glob_pattern_p(const char *pattern, int quote)
   1035  1.24  christos {
   1036  1.24  christos 	int range = 0;
   1037  1.24  christos 
   1038  1.24  christos 	for (; *pattern; pattern++)
   1039  1.24  christos 		switch (*pattern) {
   1040  1.24  christos 		case QUESTION:
   1041  1.24  christos 		case STAR:
   1042  1.24  christos 			return 1;
   1043  1.24  christos 
   1044  1.24  christos 		case QUOTE:
   1045  1.27  christos 			if (quote && pattern[1] != EOS)
   1046  1.24  christos 			      ++pattern;
   1047  1.24  christos 			break;
   1048  1.24  christos 
   1049  1.24  christos 		case LBRACKET:
   1050  1.24  christos 			range = 1;
   1051  1.24  christos 			break;
   1052  1.24  christos 
   1053  1.24  christos 		case RBRACKET:
   1054  1.24  christos 			if (range)
   1055  1.24  christos 			      return 1;
   1056  1.24  christos 			break;
   1057  1.24  christos 		default:
   1058  1.24  christos 			break;
   1059  1.24  christos 		}
   1060  1.24  christos 
   1061  1.24  christos 	  return 0;
   1062  1.24  christos }
   1063  1.24  christos #endif
   1064  1.24  christos 
   1065  1.12  christos static DIR *
   1066  1.18  christos g_opendir(Char *str, glob_t *pglob)
   1067  1.12  christos {
   1068  1.12  christos 	char buf[MAXPATHLEN];
   1069  1.12  christos 
   1070  1.12  christos 	_DIAGASSERT(str != NULL);
   1071  1.12  christos 	_DIAGASSERT(pglob != NULL);
   1072  1.12  christos 
   1073  1.12  christos 	if (!*str)
   1074  1.12  christos 		(void)strlcpy(buf, ".", sizeof(buf));
   1075  1.12  christos 	else {
   1076  1.12  christos 		if (g_Ctoc(str, buf, sizeof(buf)))
   1077  1.12  christos 			return NULL;
   1078  1.12  christos 	}
   1079  1.12  christos 
   1080  1.12  christos 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
   1081  1.18  christos 		return (*pglob->gl_opendir)(buf);
   1082  1.12  christos 
   1083  1.18  christos 	return opendir(buf);
   1084  1.12  christos }
   1085  1.12  christos 
   1086  1.12  christos static int
   1087  1.18  christos g_lstat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
   1088  1.12  christos {
   1089  1.12  christos 	char buf[MAXPATHLEN];
   1090  1.12  christos 
   1091  1.12  christos 	_DIAGASSERT(fn != NULL);
   1092  1.12  christos 	_DIAGASSERT(sb != NULL);
   1093  1.12  christos 	_DIAGASSERT(pglob != NULL);
   1094  1.12  christos 
   1095  1.12  christos 	if (g_Ctoc(fn, buf, sizeof(buf)))
   1096  1.12  christos 		return -1;
   1097  1.12  christos 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
   1098  1.18  christos 		return (*pglob->gl_lstat)(buf, sb);
   1099  1.18  christos 	return lstat(buf, sb);
   1100  1.12  christos }
   1101  1.12  christos 
   1102  1.12  christos static int
   1103  1.18  christos g_stat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
   1104  1.12  christos {
   1105  1.12  christos 	char buf[MAXPATHLEN];
   1106  1.12  christos 
   1107  1.12  christos 	_DIAGASSERT(fn != NULL);
   1108  1.12  christos 	_DIAGASSERT(sb != NULL);
   1109  1.12  christos 	_DIAGASSERT(pglob != NULL);
   1110  1.12  christos 
   1111  1.12  christos 	if (g_Ctoc(fn, buf, sizeof(buf)))
   1112  1.12  christos 		return -1;
   1113  1.12  christos 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
   1114  1.18  christos 		return (*pglob->gl_stat)(buf, sb);
   1115  1.18  christos 	return stat(buf, sb);
   1116  1.12  christos }
   1117  1.12  christos 
   1118  1.12  christos static Char *
   1119  1.18  christos g_strchr(const Char *str, int ch)
   1120  1.12  christos {
   1121  1.12  christos 
   1122  1.12  christos 	_DIAGASSERT(str != NULL);
   1123  1.12  christos 
   1124  1.12  christos 	do {
   1125  1.12  christos 		if (*str == ch)
   1126  1.14  christos 			return __UNCONST(str);
   1127  1.12  christos 	} while (*str++);
   1128  1.12  christos 	return NULL;
   1129  1.12  christos }
   1130  1.12  christos 
   1131  1.12  christos static int
   1132  1.18  christos g_Ctoc(const Char *str, char *buf, size_t len)
   1133  1.12  christos {
   1134  1.12  christos 	char *dc;
   1135  1.12  christos 
   1136  1.12  christos 	_DIAGASSERT(str != NULL);
   1137  1.12  christos 	_DIAGASSERT(buf != NULL);
   1138  1.12  christos 
   1139  1.12  christos 	if (len == 0)
   1140  1.12  christos 		return 1;
   1141  1.12  christos 
   1142  1.12  christos 	for (dc = buf; len && (*dc++ = *str++) != EOS; len--)
   1143  1.12  christos 		continue;
   1144  1.12  christos 
   1145  1.12  christos 	return len == 0;
   1146  1.12  christos }
   1147  1.12  christos 
   1148  1.12  christos #ifdef DEBUG
   1149  1.12  christos static void
   1150  1.18  christos qprintf(const char *str, Char *s)
   1151  1.12  christos {
   1152  1.12  christos 	Char *p;
   1153  1.12  christos 
   1154  1.12  christos 	_DIAGASSERT(str != NULL);
   1155  1.12  christos 	_DIAGASSERT(s != NULL);
   1156  1.12  christos 
   1157  1.12  christos 	(void)printf("%s:\n", str);
   1158  1.12  christos 	for (p = s; *p; p++)
   1159  1.12  christos 		(void)printf("%c", CHAR(*p));
   1160  1.12  christos 	(void)printf("\n");
   1161  1.12  christos 	for (p = s; *p; p++)
   1162  1.12  christos 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
   1163  1.12  christos 	(void)printf("\n");
   1164  1.12  christos 	for (p = s; *p; p++)
   1165  1.12  christos 		(void)printf("%c", ismeta(*p) ? '_' : ' ');
   1166  1.12  christos 	(void)printf("\n");
   1167  1.12  christos }
   1168  1.12  christos #endif
   1169