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