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