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