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