xref: /csrg-svn/lib/libc/gen/glob.c (revision 50420)
140087Sbostic /*
240087Sbostic  * Copyright (c) 1989 The Regents of the University of California.
340087Sbostic  * All rights reserved.
440087Sbostic  *
540087Sbostic  * This code is derived from software contributed to Berkeley by
640087Sbostic  * Guido van Rossum.
740087Sbostic  *
842625Sbostic  * %sccs.include.redist.c%
940087Sbostic  */
1040087Sbostic 
1140087Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*50420Sbostic static char sccsid[] = "@(#)glob.c	5.13 (Berkeley) 07/15/91";
1340087Sbostic #endif /* LIBC_SCCS and not lint */
1440087Sbostic 
1540087Sbostic /*
1650050Sbostic  * glob(3) -- a superset of the one defined in POSIX 1003.2.
1740087Sbostic  *
1840087Sbostic  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
1940087Sbostic  *
2040087Sbostic  * Optional extra services, controlled by flags not defined by POSIX:
2147590Sbostic  *
2247590Sbostic  * GLOB_QUOTE:
2347590Sbostic  *	Escaping convention: \ inhibits any special meaning the following
2447590Sbostic  *	character might have (except \ at end of string is retained).
2547590Sbostic  * GLOB_MAGCHAR:
2648540Sbostic  *	Set in gl_flags if pattern contained a globbing character.
27*50420Sbostic  * GLOB_NOMAGIC:
28*50420Sbostic  *	Same as GLOB_NOCHECK, but it will only append pattern if it did
29*50420Sbostic  *	not contain any magic characters.  [Used in csh style globbing]
3047590Sbostic  * gl_matchc:
3147590Sbostic  *	Number of matches in the current invocation of glob.
3240087Sbostic  */
3340087Sbostic 
3440087Sbostic #include <sys/param.h>
3540087Sbostic #include <sys/stat.h>
3640087Sbostic #include <dirent.h>
3740087Sbostic #include <glob.h>
3840087Sbostic #include <ctype.h>
3940087Sbostic #include <errno.h>
4040087Sbostic #include <string.h>
4140087Sbostic #include <stdio.h>
4246597Sdonn #include <stdlib.h>
4340087Sbostic 
4440087Sbostic #define	DOLLAR		'$'
4540087Sbostic #define	DOT		'.'
4640087Sbostic #define	EOS		'\0'
4740087Sbostic #define	LBRACKET	'['
4840087Sbostic #define	NOT		'!'
4940087Sbostic #define	QUESTION	'?'
5040087Sbostic #define	QUOTE		'\\'
5140087Sbostic #define	RANGE		'-'
5240087Sbostic #define	RBRACKET	']'
5340087Sbostic #define	SEP		'/'
5440087Sbostic #define	STAR		'*'
5540087Sbostic #define	TILDE		'~'
5640087Sbostic #define	UNDERSCORE	'_'
5740087Sbostic 
5850049Sbostic #define	M_QUOTE		0x8000
5950049Sbostic #define	M_PROTECT	0x4000
6048540Sbostic #define	M_MASK		0xffff
6150121Sbostic #define	M_ASCII		0x00ff
6248540Sbostic 
6350121Sbostic #define	CHAR(c)		((c)&M_ASCII)
6450049Sbostic #define	META(c)		((c)|M_QUOTE)
6540087Sbostic #define	M_ALL		META('*')
6640087Sbostic #define	M_END		META(']')
6740087Sbostic #define	M_NOT		META('!')
6840087Sbostic #define	M_ONE		META('?')
6940087Sbostic #define	M_RNG		META('-')
7040087Sbostic #define	M_SET		META('[')
7150049Sbostic #define	ismeta(c)	(((c)&M_QUOTE) != 0)
7240087Sbostic 
7350050Sbostic typedef u_short Char;
7450049Sbostic 
7550050Sbostic static int	 compare __P((const void *, const void *));
7650050Sbostic static void	 g_Ctoc __P((Char *, char *));
7750050Sbostic static int	 g_lstat __P((Char *, struct stat *));
7850050Sbostic static DIR	*g_opendir __P((Char *));
7950050Sbostic static Char	*g_strchr __P((Char *, int));
8050050Sbostic static int	 g_stat __P((Char *, struct stat *));
8150050Sbostic static int	 glob1 __P((Char *, glob_t *));
8250050Sbostic static int	 glob2 __P((Char *, Char *, Char *, glob_t *));
8350050Sbostic static int	 glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
8450050Sbostic static int	 globextend __P((Char *, glob_t *));
8550050Sbostic static int	 match __P((Char *, Char *, Char *));
8650049Sbostic #ifdef DEBUG
8750050Sbostic static void	 qprintf __P((Char *));
8850049Sbostic #endif
8950049Sbostic 
9040087Sbostic /*
9140087Sbostic  * The main glob() routine: compiles the pattern (optionally processing
9240087Sbostic  * quotes), calls glob1() to do the real pattern matching, and finally
9340087Sbostic  * sorts the list (unless unsorted operation is requested).  Returns 0
9440087Sbostic  * if things went well, nonzero if errors occurred.  It is not an error
9540087Sbostic  * to find no matches.
9640087Sbostic  */
9740087Sbostic glob(pattern, flags, errfunc, pglob)
9846597Sdonn 	const char *pattern;
9950050Sbostic 	int flags, (*errfunc) __P((char *, int));
10040087Sbostic 	glob_t *pglob;
10140087Sbostic {
10250121Sbostic 	const u_char *compilepat, *patnext;
10350117Sbostic 	int c, err, oldpathc;
10450050Sbostic 	Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
10540087Sbostic 
10650121Sbostic 	patnext = (u_char *) pattern;
10740087Sbostic 	if (!(flags & GLOB_APPEND)) {
10840087Sbostic 		pglob->gl_pathc = 0;
10940087Sbostic 		pglob->gl_pathv = NULL;
11040087Sbostic 		if (!(flags & GLOB_DOOFFS))
11140087Sbostic 			pglob->gl_offs = 0;
11240087Sbostic 	}
11347590Sbostic 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
11440087Sbostic 	pglob->gl_errfunc = errfunc;
11540087Sbostic 	oldpathc = pglob->gl_pathc;
11647590Sbostic 	pglob->gl_matchc = 0;
11740087Sbostic 
11840087Sbostic 	bufnext = patbuf;
11950049Sbostic 	bufend = bufnext + MAXPATHLEN;
12050050Sbostic 	compilebuf = bufnext;
12150050Sbostic 	compilepat = patnext;
12250049Sbostic 	if (flags & GLOB_QUOTE) {
12350050Sbostic 		/* Protect the quoted characters. */
12450049Sbostic 		while (bufnext < bufend && (c = *patnext++) != EOS)
12550049Sbostic 			if (c == QUOTE) {
12650049Sbostic 				if ((c = *patnext++) == EOS) {
12750049Sbostic 					c = QUOTE;
12850049Sbostic 					--patnext;
12950049Sbostic 				}
13050049Sbostic 				*bufnext++ = c | M_PROTECT;
13150049Sbostic 			}
13250049Sbostic 			else
13350049Sbostic 				*bufnext++ = c;
13450049Sbostic 	}
13550049Sbostic 	else
13650049Sbostic 	    while (bufnext < bufend && (c = *patnext++) != EOS)
13750049Sbostic 		    *bufnext++ = c;
13850049Sbostic 	*bufnext = EOS;
13940087Sbostic 
14050049Sbostic 	bufnext = patbuf;
14150049Sbostic 	qpatnext = patbuf;
14250050Sbostic 	/* We don't need to check for buffer overflow any more. */
14350049Sbostic 	while ((c = *qpatnext++) != EOS) {
14440087Sbostic 		switch (c) {
14540087Sbostic 		case LBRACKET:
14650049Sbostic 			c = *qpatnext;
14740087Sbostic 			if (c == NOT)
14850049Sbostic 				++qpatnext;
14950049Sbostic 			if (*qpatnext == EOS ||
15050050Sbostic 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
15140087Sbostic 				*bufnext++ = LBRACKET;
15240087Sbostic 				if (c == NOT)
15350049Sbostic 					--qpatnext;
15440087Sbostic 				break;
15540087Sbostic 			}
15640087Sbostic 			*bufnext++ = M_SET;
15740087Sbostic 			if (c == NOT)
15840087Sbostic 				*bufnext++ = M_NOT;
15950049Sbostic 			c = *qpatnext++;
16040087Sbostic 			do {
16150121Sbostic 				*bufnext++ = CHAR(c);
16250049Sbostic 				if (*qpatnext == RANGE &&
16350049Sbostic 				    (c = qpatnext[1]) != RBRACKET) {
16440087Sbostic 					*bufnext++ = M_RNG;
16550121Sbostic 					*bufnext++ = CHAR(c);
16650049Sbostic 					qpatnext += 2;
16740087Sbostic 				}
16850049Sbostic 			} while ((c = *qpatnext++) != RBRACKET);
169*50420Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
17040087Sbostic 			*bufnext++ = M_END;
17140087Sbostic 			break;
17240087Sbostic 		case QUESTION:
17347590Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
17440087Sbostic 			*bufnext++ = M_ONE;
17540087Sbostic 			break;
17640087Sbostic 		case STAR:
17747590Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
17840087Sbostic 			*bufnext++ = M_ALL;
17940087Sbostic 			break;
18040087Sbostic 		default:
18150121Sbostic 			*bufnext++ = CHAR(c);
18240087Sbostic 			break;
18340087Sbostic 		}
18440087Sbostic 	}
18540087Sbostic 	*bufnext = EOS;
18650049Sbostic #ifdef DEBUG
18750049Sbostic 	qprintf(patbuf);
18850049Sbostic #endif
18940087Sbostic 
19040087Sbostic 	if ((err = glob1(patbuf, pglob)) != 0)
19140087Sbostic 		return(err);
19240087Sbostic 
193*50420Sbostic 	/*
194*50420Sbostic 	 * If there was no match we are going to append the pattern
195*50420Sbostic 	 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
196*50420Sbostic 	 * and the pattern did not contain any magic characters
197*50420Sbostic 	 * GLOB_NOMAGIC is there just for compatibility with csh.
198*50420Sbostic 	 */
199*50420Sbostic 	if (pglob->gl_pathc == oldpathc &&
200*50420Sbostic 	    ((flags & GLOB_NOCHECK) ||
201*50420Sbostic 	     ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
20248540Sbostic 		if (!(flags & GLOB_QUOTE)) {
20350050Sbostic 			Char *dp = compilebuf;
20450121Sbostic 			const u_char *sp = compilepat;
20550121Sbostic 			while (*dp++ = *sp++);
20648540Sbostic 		}
20740087Sbostic 		else {
20840087Sbostic 			/*
20950050Sbostic 			 * Copy pattern, interpreting quotes; this is slightly
21040087Sbostic 			 * different than the interpretation of quotes above
21140087Sbostic 			 * -- which should prevail?
21240087Sbostic 			 */
21340087Sbostic 			while (*compilepat != EOS) {
21440087Sbostic 				if (*compilepat == QUOTE) {
21540087Sbostic 					if (*++compilepat == EOS)
21640087Sbostic 						--compilepat;
21740087Sbostic 				}
21848540Sbostic 				*compilebuf++ = (u_char)*compilepat++;
21940087Sbostic 			}
22040087Sbostic 			*compilebuf = EOS;
22140087Sbostic 		}
22240087Sbostic 		return(globextend(patbuf, pglob));
22348540Sbostic 	} else if (!(flags & GLOB_NOSORT))
22450050Sbostic 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
22550050Sbostic 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
22640087Sbostic 	return(0);
22740087Sbostic }
22840087Sbostic 
22950050Sbostic static int
23050050Sbostic compare(p, q)
23150050Sbostic 	const void *p, *q;
23250050Sbostic {
23350050Sbostic 	return(strcmp(*(char **)p, *(char **)q));
23450050Sbostic }
23550050Sbostic 
23640087Sbostic static
23740087Sbostic glob1(pattern, pglob)
23850050Sbostic 	Char *pattern;
23940087Sbostic 	glob_t *pglob;
24040087Sbostic {
24150050Sbostic 	Char pathbuf[MAXPATHLEN+1];
24240087Sbostic 
24350050Sbostic 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
24440087Sbostic 	if (*pattern == EOS)
24540087Sbostic 		return(0);
24640087Sbostic 	return(glob2(pathbuf, pathbuf, pattern, pglob));
24740087Sbostic }
24840087Sbostic 
24940087Sbostic /*
25050050Sbostic  * The functions glob2 and glob3 are mutually recursive; there is one level
25150050Sbostic  * of recursion for each segment in the pattern that contains one or more
25250050Sbostic  * meta characters.
25340087Sbostic  */
25440087Sbostic static
25540087Sbostic glob2(pathbuf, pathend, pattern, pglob)
25650050Sbostic 	Char *pathbuf, *pathend, *pattern;
25740087Sbostic 	glob_t *pglob;
25840087Sbostic {
25950050Sbostic 	struct stat sb;
26050050Sbostic 	Char *p, *q;
26148540Sbostic 	int anymeta;
26240087Sbostic 
26340087Sbostic 	/*
26450050Sbostic 	 * Loop over pattern segments until end of pattern or until
26540087Sbostic 	 * segment with meta character found.
26640087Sbostic 	 */
26748357Sbostic 	for (anymeta = 0;;) {
26850050Sbostic 		if (*pattern == EOS) {		/* End of pattern? */
26940087Sbostic 			*pathend = EOS;
27050050Sbostic 			if (g_stat(pathbuf, &sb))
27148357Sbostic 				return(0);
27248540Sbostic 
27348540Sbostic 			if (((pglob->gl_flags & GLOB_MARK) &&
27450050Sbostic 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
27550050Sbostic 			    || (S_ISLNK(sb.st_mode) &&
27650050Sbostic 			    (g_stat(pathbuf, &sb) == 0) &&
27750050Sbostic 			    S_ISDIR(sb.st_mode)))) {
27840087Sbostic 				*pathend++ = SEP;
27940087Sbostic 				*pathend = EOS;
28040087Sbostic 			}
28147590Sbostic 			++pglob->gl_matchc;
28240087Sbostic 			return(globextend(pathbuf, pglob));
28340087Sbostic 		}
28440087Sbostic 
28550050Sbostic 		/* Find end of next segment, copy tentatively to pathend. */
28640087Sbostic 		q = pathend;
28740087Sbostic 		p = pattern;
28840087Sbostic 		while (*p != EOS && *p != SEP) {
28940087Sbostic 			if (ismeta(*p))
29040087Sbostic 				anymeta = 1;
29140087Sbostic 			*q++ = *p++;
29240087Sbostic 		}
29340087Sbostic 
29450050Sbostic 		if (!anymeta) {		/* No expansion, do next segment. */
29540087Sbostic 			pathend = q;
29640087Sbostic 			pattern = p;
29740087Sbostic 			while (*pattern == SEP)
29840087Sbostic 				*pathend++ = *pattern++;
29950050Sbostic 		} else			/* Need expansion, recurse. */
30040087Sbostic 			return(glob3(pathbuf, pathend, pattern, p, pglob));
30140087Sbostic 	}
30240087Sbostic 	/* NOTREACHED */
30340087Sbostic }
30440087Sbostic 
30540087Sbostic static
30640087Sbostic glob3(pathbuf, pathend, pattern, restpattern, pglob)
30750050Sbostic 	Char *pathbuf, *pathend, *pattern, *restpattern;
30840087Sbostic 	glob_t *pglob;
30940087Sbostic {
31050050Sbostic 	register struct dirent *dp;
31140087Sbostic 	DIR *dirp;
31240087Sbostic 	int len, err;
31340087Sbostic 
31440087Sbostic 	*pathend = EOS;
31540087Sbostic 	errno = 0;
31648540Sbostic 
31750050Sbostic 	if (!(dirp = g_opendir(pathbuf)))
31850050Sbostic 		/* TODO: don't call for ENOENT or ENOTDIR? */
31940087Sbostic 		if (pglob->gl_errfunc &&
32040087Sbostic 		    (*pglob->gl_errfunc)(pathbuf, errno) ||
32140087Sbostic 		    (pglob->gl_flags & GLOB_ERR))
32240087Sbostic 			return(GLOB_ABEND);
32340087Sbostic 		else
32440087Sbostic 			return(0);
32540087Sbostic 
32640087Sbostic 	err = 0;
32740087Sbostic 
32850050Sbostic 	/* Search directory for matching names. */
32940087Sbostic 	while ((dp = readdir(dirp))) {
33050121Sbostic 		register u_char *sc;
33150050Sbostic 		register Char *dc;
33250050Sbostic 
33350050Sbostic 		/* Initial DOT must be matched literally. */
33440087Sbostic 		if (dp->d_name[0] == DOT && *pattern != DOT)
33540087Sbostic 			continue;
33650121Sbostic 		for (sc = (u_char *) dp->d_name, dc = pathend;
33750121Sbostic 		     *dc++ = *sc++;);
33848540Sbostic 		if (!match(pathend, pattern, restpattern)) {
33948540Sbostic 			*pathend = EOS;
34040087Sbostic 			continue;
34148540Sbostic 		}
34248540Sbostic 		err = glob2(pathbuf, --dc, restpattern, pglob);
34340087Sbostic 		if (err)
34440087Sbostic 			break;
34540087Sbostic 	}
34650050Sbostic 
34750050Sbostic 	/* TODO: check error from readdir? */
34840087Sbostic 	(void)closedir(dirp);
34940087Sbostic 	return(err);
35040087Sbostic }
35140087Sbostic 
35240087Sbostic 
35340087Sbostic /*
35440087Sbostic  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
35540087Sbostic  * add the new item, and update gl_pathc.
35640087Sbostic  *
35740087Sbostic  * This assumes the BSD realloc, which only copies the block when its size
35840087Sbostic  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
35940087Sbostic  * behavior.
36040087Sbostic  *
36140087Sbostic  * Return 0 if new item added, error code if memory couldn't be allocated.
36240087Sbostic  *
36340087Sbostic  * Invariant of the glob_t structure:
36440087Sbostic  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
36550050Sbostic  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
36640087Sbostic  */
36748540Sbostic static int
36840087Sbostic globextend(path, pglob)
36950050Sbostic 	Char *path;
37040087Sbostic 	glob_t *pglob;
37140087Sbostic {
37240087Sbostic 	register char **pathv;
37340087Sbostic 	register int i;
37448540Sbostic 	u_int newsize;
37540087Sbostic 	char *copy;
37650050Sbostic 	Char *p;
37740087Sbostic 
37840087Sbostic 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
37948540Sbostic 	pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
38040087Sbostic 	if (pathv == NULL)
38140087Sbostic 		return(GLOB_NOSPACE);
38240087Sbostic 
38340087Sbostic 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
38440087Sbostic 		/* first time around -- clear initial gl_offs items */
38540087Sbostic 		pathv += pglob->gl_offs;
38640087Sbostic 		for (i = pglob->gl_offs; --i >= 0; )
38740087Sbostic 			*--pathv = NULL;
38840087Sbostic 	}
38940087Sbostic 	pglob->gl_pathv = pathv;
39040087Sbostic 
39148540Sbostic 	for (p = path; *p++;);
39248540Sbostic 	if ((copy = malloc(p - path)) != NULL) {
39350050Sbostic 		g_Ctoc(path, copy);
39440087Sbostic 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
39540087Sbostic 	}
39640087Sbostic 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
39750050Sbostic 	return(copy == NULL ? GLOB_NOSPACE : 0);
39840087Sbostic }
39940087Sbostic 
40040087Sbostic 
40140087Sbostic /*
40240087Sbostic  * pattern matching function for filenames.  Each occurrence of the *
40340087Sbostic  * pattern causes a recursion level.
40440087Sbostic  */
40548540Sbostic static
40640087Sbostic match(name, pat, patend)
40750050Sbostic 	register Char *name, *pat, *patend;
40840087Sbostic {
40948540Sbostic 	int ok, negate_range;
41050050Sbostic 	Char c, k;
41140087Sbostic 
41240087Sbostic 	while (pat < patend) {
41340087Sbostic 		c = *pat++;
41448540Sbostic 		switch (c & M_MASK) {
41540087Sbostic 		case M_ALL:
41640087Sbostic 			if (pat == patend)
41740087Sbostic 				return(1);
41850050Sbostic 			for (; *name != EOS; ++name)
41940087Sbostic 				if (match(name, pat, patend))
42040087Sbostic 					return(1);
42140087Sbostic 			return(0);
42240087Sbostic 		case M_ONE:
42340087Sbostic 			if (*name++ == EOS)
42440087Sbostic 				return(0);
42540087Sbostic 			break;
42640087Sbostic 		case M_SET:
42740087Sbostic 			ok = 0;
42840087Sbostic 			k = *name++;
42948540Sbostic 			if (negate_range = ((*pat & M_MASK) == M_NOT))
43040087Sbostic 				++pat;
43150050Sbostic 			while (((c = *pat++) & M_MASK) != M_END)
43248540Sbostic 				if ((*pat & M_MASK) == M_RNG) {
43340087Sbostic 					if (c <= k && k <= pat[1])
43440087Sbostic 						ok = 1;
43540087Sbostic 					pat += 2;
43650050Sbostic 				} else if (c == k)
43740087Sbostic 					ok = 1;
43840087Sbostic 			if (ok == negate_range)
43940087Sbostic 				return(0);
44040087Sbostic 			break;
44140087Sbostic 		default:
44240087Sbostic 			if (*name++ != c)
44340087Sbostic 				return(0);
44440087Sbostic 			break;
44540087Sbostic 		}
44640087Sbostic 	}
44740087Sbostic 	return(*name == EOS);
44840087Sbostic }
44940087Sbostic 
45050050Sbostic /* Free allocated data belonging to a glob_t structure. */
45140087Sbostic void
45240087Sbostic globfree(pglob)
45340087Sbostic 	glob_t *pglob;
45440087Sbostic {
45540087Sbostic 	register int i;
45640087Sbostic 	register char **pp;
45740087Sbostic 
45840087Sbostic 	if (pglob->gl_pathv != NULL) {
45940087Sbostic 		pp = pglob->gl_pathv + pglob->gl_offs;
46040087Sbostic 		for (i = pglob->gl_pathc; i--; ++pp)
46140087Sbostic 			if (*pp)
46250050Sbostic 				free(*pp);
46350049Sbostic 		free(pglob->gl_pathv);
46440087Sbostic 	}
46540087Sbostic }
46650050Sbostic 
46750050Sbostic static DIR *
46850050Sbostic g_opendir(str)
46950050Sbostic 	register Char *str;
47050050Sbostic {
47150050Sbostic 	char buf[MAXPATHLEN];
47250050Sbostic 
47350050Sbostic 	if (!*str)
47450050Sbostic 		return(opendir("."));
47550050Sbostic 	g_Ctoc(str, buf);
47650050Sbostic 	return(opendir(buf));
47750050Sbostic }
47850050Sbostic 
47950050Sbostic static int
48050050Sbostic g_lstat(fn, sb)
48150050Sbostic 	register Char *fn;
48250050Sbostic 	struct stat *sb;
48350050Sbostic {
48450050Sbostic 	char buf[MAXPATHLEN];
48550050Sbostic 
48650050Sbostic 	g_Ctoc(fn, buf);
48750050Sbostic 	return(lstat(buf, sb));
48850050Sbostic }
48950050Sbostic 
49050050Sbostic static int
49150050Sbostic g_stat(fn, sb)
49250050Sbostic 	register Char *fn;
49350050Sbostic 	struct stat *sb;
49450050Sbostic {
49550050Sbostic 	char buf[MAXPATHLEN];
49650050Sbostic 
49750050Sbostic 	g_Ctoc(fn, buf);
49850050Sbostic 	return(stat(buf, sb));
49950050Sbostic }
50050050Sbostic 
50150050Sbostic static Char *
50250050Sbostic g_strchr(str, ch)
50350050Sbostic 	Char *str;
50450050Sbostic 	int ch;
50550050Sbostic {
50650050Sbostic 	do {
50750050Sbostic 		if (*str == ch)
50850050Sbostic 			return (str);
50950050Sbostic 	} while (*str++);
51050050Sbostic 	return (NULL);
51150050Sbostic }
51250050Sbostic 
51350050Sbostic static void
51450050Sbostic g_Ctoc(str, buf)
51550050Sbostic 	register Char *str;
51650050Sbostic 	char *buf;
51750050Sbostic {
51850050Sbostic 	register char *dc;
51950050Sbostic 
52050050Sbostic 	for (dc = buf; *dc++ = *str++;);
52150050Sbostic }
52250050Sbostic 
52350050Sbostic #ifdef DEBUG
52450050Sbostic static void
52550050Sbostic qprintf(s)
52650050Sbostic 	register Char *s;
52750050Sbostic {
52850050Sbostic 	register Char *p;
52950050Sbostic 
53050050Sbostic 	for (p = s; *p; p++)
53150050Sbostic 		(void)printf("%c", *p & 0xff);
53250050Sbostic 	(void)printf("\n");
53350050Sbostic 	for (p = s; *p; p++)
53450050Sbostic 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
53550050Sbostic 	(void)printf("\n");
53650050Sbostic 	for (p = s; *p; p++)
53750050Sbostic 		(void)printf("%c", *p & M_META ? '_' : ' ');
53850050Sbostic 	(void)printf("\n");
53950050Sbostic }
54050050Sbostic #endif
541