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