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