xref: /csrg-svn/lib/libc/gen/glob.c (revision 50117)
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*50117Sbostic static char sccsid[] = "@(#)glob.c	5.11 (Berkeley) 06/23/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.
2747590Sbostic  * gl_matchc:
2847590Sbostic  *	Number of matches in the current invocation of glob.
2940087Sbostic  */
3040087Sbostic 
3148540Sbostic #include <sys/cdefs.h>
3240087Sbostic #include <sys/param.h>
3340087Sbostic #include <sys/stat.h>
3440087Sbostic #include <dirent.h>
3540087Sbostic #include <glob.h>
3640087Sbostic #include <ctype.h>
3740087Sbostic #include <errno.h>
3840087Sbostic #include <string.h>
3940087Sbostic #include <stdio.h>
4046597Sdonn #include <stdlib.h>
4140087Sbostic 
4240087Sbostic #define	DOLLAR		'$'
4340087Sbostic #define	DOT		'.'
4440087Sbostic #define	EOS		'\0'
4540087Sbostic #define	LBRACKET	'['
4640087Sbostic #define	NOT		'!'
4740087Sbostic #define	QUESTION	'?'
4840087Sbostic #define	QUOTE		'\\'
4940087Sbostic #define	RANGE		'-'
5040087Sbostic #define	RBRACKET	']'
5140087Sbostic #define	SEP		'/'
5240087Sbostic #define	STAR		'*'
5340087Sbostic #define	TILDE		'~'
5440087Sbostic #define	UNDERSCORE	'_'
5540087Sbostic 
5650049Sbostic #define	M_QUOTE		0x8000
5750049Sbostic #define	M_PROTECT	0x4000
5848540Sbostic #define	M_MASK		0xffff
5948540Sbostic 
6050049Sbostic #define	META(c)		((c)|M_QUOTE)
6140087Sbostic #define	M_ALL		META('*')
6240087Sbostic #define	M_END		META(']')
6340087Sbostic #define	M_NOT		META('!')
6440087Sbostic #define	M_ONE		META('?')
6540087Sbostic #define	M_RNG		META('-')
6640087Sbostic #define	M_SET		META('[')
6750049Sbostic #define	ismeta(c)	(((c)&M_QUOTE) != 0)
6840087Sbostic 
6950050Sbostic typedef u_short Char;
7050049Sbostic 
7150050Sbostic static int	 compare __P((const void *, const void *));
7250050Sbostic static void	 g_Ctoc __P((Char *, char *));
7350050Sbostic static int	 g_lstat __P((Char *, struct stat *));
7450050Sbostic static DIR	*g_opendir __P((Char *));
7550050Sbostic static Char	*g_strchr __P((Char *, int));
7650050Sbostic static int	 g_stat __P((Char *, struct stat *));
7750050Sbostic static int	 glob1 __P((Char *, glob_t *));
7850050Sbostic static int	 glob2 __P((Char *, Char *, Char *, glob_t *));
7950050Sbostic static int	 glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
8050050Sbostic static int	 globextend __P((Char *, glob_t *));
8150050Sbostic static int	 match __P((Char *, Char *, Char *));
8250049Sbostic #ifdef DEBUG
8350050Sbostic static void	 qprintf __P((Char *));
8450049Sbostic #endif
8550049Sbostic 
8640087Sbostic /*
8740087Sbostic  * The main glob() routine: compiles the pattern (optionally processing
8840087Sbostic  * quotes), calls glob1() to do the real pattern matching, and finally
8940087Sbostic  * sorts the list (unless unsorted operation is requested).  Returns 0
9040087Sbostic  * if things went well, nonzero if errors occurred.  It is not an error
9140087Sbostic  * to find no matches.
9240087Sbostic  */
9340087Sbostic glob(pattern, flags, errfunc, pglob)
9446597Sdonn 	const char *pattern;
9550050Sbostic 	int flags, (*errfunc) __P((char *, int));
9640087Sbostic 	glob_t *pglob;
9740087Sbostic {
9850050Sbostic 	const char *compilepat, *patnext;
99*50117Sbostic 	int c, err, oldpathc;
10050050Sbostic 	Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
10140087Sbostic 
10240087Sbostic 	patnext = pattern;
10340087Sbostic 	if (!(flags & GLOB_APPEND)) {
10440087Sbostic 		pglob->gl_pathc = 0;
10540087Sbostic 		pglob->gl_pathv = NULL;
10640087Sbostic 		if (!(flags & GLOB_DOOFFS))
10740087Sbostic 			pglob->gl_offs = 0;
10840087Sbostic 	}
10947590Sbostic 	pglob->gl_flags = flags & ~GLOB_MAGCHAR;
11040087Sbostic 	pglob->gl_errfunc = errfunc;
11140087Sbostic 	oldpathc = pglob->gl_pathc;
11247590Sbostic 	pglob->gl_matchc = 0;
11340087Sbostic 
11440087Sbostic 	bufnext = patbuf;
11550049Sbostic 	bufend = bufnext + MAXPATHLEN;
11650050Sbostic 	compilebuf = bufnext;
11750050Sbostic 	compilepat = patnext;
11850049Sbostic 	if (flags & GLOB_QUOTE) {
11950050Sbostic 		/* Protect the quoted characters. */
12050049Sbostic 		while (bufnext < bufend && (c = *patnext++) != EOS)
12150049Sbostic 			if (c == QUOTE) {
12250049Sbostic 				if ((c = *patnext++) == EOS) {
12350049Sbostic 					c = QUOTE;
12450049Sbostic 					--patnext;
12550049Sbostic 				}
12650049Sbostic 				*bufnext++ = c | M_PROTECT;
12750049Sbostic 			}
12850049Sbostic 			else
12950049Sbostic 				*bufnext++ = c;
13050049Sbostic 	}
13150049Sbostic 	else
13250049Sbostic 	    while (bufnext < bufend && (c = *patnext++) != EOS)
13350049Sbostic 		    *bufnext++ = c;
13450049Sbostic 	*bufnext = EOS;
13540087Sbostic 
13650049Sbostic 	bufnext = patbuf;
13750049Sbostic 	qpatnext = patbuf;
13850050Sbostic 	/* We don't need to check for buffer overflow any more. */
13950049Sbostic 	while ((c = *qpatnext++) != EOS) {
14040087Sbostic 		switch (c) {
14140087Sbostic 		case LBRACKET:
14247590Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
14350049Sbostic 			c = *qpatnext;
14440087Sbostic 			if (c == NOT)
14550049Sbostic 				++qpatnext;
14650049Sbostic 			if (*qpatnext == EOS ||
14750050Sbostic 			    g_strchr(qpatnext+1, RBRACKET) == NULL) {
14840087Sbostic 				*bufnext++ = LBRACKET;
14940087Sbostic 				if (c == NOT)
15050049Sbostic 					--qpatnext;
15140087Sbostic 				break;
15240087Sbostic 			}
15340087Sbostic 			*bufnext++ = M_SET;
15440087Sbostic 			if (c == NOT)
15540087Sbostic 				*bufnext++ = M_NOT;
15650049Sbostic 			c = *qpatnext++;
15740087Sbostic 			do {
15840087Sbostic 				*bufnext++ = c;
15950049Sbostic 				if (*qpatnext == RANGE &&
16050049Sbostic 				    (c = qpatnext[1]) != RBRACKET) {
16140087Sbostic 					*bufnext++ = M_RNG;
16240087Sbostic 					*bufnext++ = c;
16350049Sbostic 					qpatnext += 2;
16440087Sbostic 				}
16550049Sbostic 			} while ((c = *qpatnext++) != RBRACKET);
16640087Sbostic 			*bufnext++ = M_END;
16740087Sbostic 			break;
16840087Sbostic 		case QUESTION:
16947590Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
17040087Sbostic 			*bufnext++ = M_ONE;
17140087Sbostic 			break;
17240087Sbostic 		case STAR:
17347590Sbostic 			pglob->gl_flags |= GLOB_MAGCHAR;
17440087Sbostic 			*bufnext++ = M_ALL;
17540087Sbostic 			break;
17640087Sbostic 		default:
17740087Sbostic 			*bufnext++ = c;
17840087Sbostic 			break;
17940087Sbostic 		}
18040087Sbostic 	}
18140087Sbostic 	*bufnext = EOS;
18250049Sbostic #ifdef DEBUG
18350049Sbostic 	qprintf(patbuf);
18450049Sbostic #endif
18540087Sbostic 
18640087Sbostic 	if ((err = glob1(patbuf, pglob)) != 0)
18740087Sbostic 		return(err);
18840087Sbostic 
18940087Sbostic 	if (pglob->gl_pathc == oldpathc && flags & GLOB_NOCHECK) {
19048540Sbostic 		if (!(flags & GLOB_QUOTE)) {
19150050Sbostic 			Char *dp = compilebuf;
19248540Sbostic 			const char *sp = compilepat;
19348540Sbostic 			while (*dp++ = (u_char)*sp++);
19448540Sbostic 		}
19540087Sbostic 		else {
19640087Sbostic 			/*
19750050Sbostic 			 * Copy pattern, interpreting quotes; this is slightly
19840087Sbostic 			 * different than the interpretation of quotes above
19940087Sbostic 			 * -- which should prevail?
20040087Sbostic 			 */
20140087Sbostic 			while (*compilepat != EOS) {
20240087Sbostic 				if (*compilepat == QUOTE) {
20340087Sbostic 					if (*++compilepat == EOS)
20440087Sbostic 						--compilepat;
20540087Sbostic 				}
20648540Sbostic 				*compilebuf++ = (u_char)*compilepat++;
20740087Sbostic 			}
20840087Sbostic 			*compilebuf = EOS;
20940087Sbostic 		}
21040087Sbostic 		return(globextend(patbuf, pglob));
21148540Sbostic 	} else if (!(flags & GLOB_NOSORT))
21250050Sbostic 		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
21350050Sbostic 		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);
21440087Sbostic 	return(0);
21540087Sbostic }
21640087Sbostic 
21750050Sbostic static int
21850050Sbostic compare(p, q)
21950050Sbostic 	const void *p, *q;
22050050Sbostic {
22150050Sbostic 	return(strcmp(*(char **)p, *(char **)q));
22250050Sbostic }
22350050Sbostic 
22440087Sbostic static
22540087Sbostic glob1(pattern, pglob)
22650050Sbostic 	Char *pattern;
22740087Sbostic 	glob_t *pglob;
22840087Sbostic {
22950050Sbostic 	Char pathbuf[MAXPATHLEN+1];
23040087Sbostic 
23150050Sbostic 	/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
23240087Sbostic 	if (*pattern == EOS)
23340087Sbostic 		return(0);
23440087Sbostic 	return(glob2(pathbuf, pathbuf, pattern, pglob));
23540087Sbostic }
23640087Sbostic 
23740087Sbostic /*
23850050Sbostic  * The functions glob2 and glob3 are mutually recursive; there is one level
23950050Sbostic  * of recursion for each segment in the pattern that contains one or more
24050050Sbostic  * meta characters.
24140087Sbostic  */
24240087Sbostic static
24340087Sbostic glob2(pathbuf, pathend, pattern, pglob)
24450050Sbostic 	Char *pathbuf, *pathend, *pattern;
24540087Sbostic 	glob_t *pglob;
24640087Sbostic {
24750050Sbostic 	struct stat sb;
24850050Sbostic 	Char *p, *q;
24948540Sbostic 	int anymeta;
25040087Sbostic 
25140087Sbostic 	/*
25250050Sbostic 	 * Loop over pattern segments until end of pattern or until
25340087Sbostic 	 * segment with meta character found.
25440087Sbostic 	 */
25548357Sbostic 	for (anymeta = 0;;) {
25650050Sbostic 		if (*pattern == EOS) {		/* End of pattern? */
25740087Sbostic 			*pathend = EOS;
25850050Sbostic 			if (g_stat(pathbuf, &sb))
25948357Sbostic 				return(0);
26048540Sbostic 
26148540Sbostic 			if (((pglob->gl_flags & GLOB_MARK) &&
26250050Sbostic 			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
26350050Sbostic 			    || (S_ISLNK(sb.st_mode) &&
26450050Sbostic 			    (g_stat(pathbuf, &sb) == 0) &&
26550050Sbostic 			    S_ISDIR(sb.st_mode)))) {
26640087Sbostic 				*pathend++ = SEP;
26740087Sbostic 				*pathend = EOS;
26840087Sbostic 			}
26947590Sbostic 			++pglob->gl_matchc;
27040087Sbostic 			return(globextend(pathbuf, pglob));
27140087Sbostic 		}
27240087Sbostic 
27350050Sbostic 		/* Find end of next segment, copy tentatively to pathend. */
27440087Sbostic 		q = pathend;
27540087Sbostic 		p = pattern;
27640087Sbostic 		while (*p != EOS && *p != SEP) {
27740087Sbostic 			if (ismeta(*p))
27840087Sbostic 				anymeta = 1;
27940087Sbostic 			*q++ = *p++;
28040087Sbostic 		}
28140087Sbostic 
28250050Sbostic 		if (!anymeta) {		/* No expansion, do next segment. */
28340087Sbostic 			pathend = q;
28440087Sbostic 			pattern = p;
28540087Sbostic 			while (*pattern == SEP)
28640087Sbostic 				*pathend++ = *pattern++;
28750050Sbostic 		} else			/* Need expansion, recurse. */
28840087Sbostic 			return(glob3(pathbuf, pathend, pattern, p, pglob));
28940087Sbostic 	}
29040087Sbostic 	/* NOTREACHED */
29140087Sbostic }
29240087Sbostic 
29340087Sbostic static
29440087Sbostic glob3(pathbuf, pathend, pattern, restpattern, pglob)
29550050Sbostic 	Char *pathbuf, *pathend, *pattern, *restpattern;
29640087Sbostic 	glob_t *pglob;
29740087Sbostic {
29850050Sbostic 	register struct dirent *dp;
29940087Sbostic 	DIR *dirp;
30040087Sbostic 	int len, err;
30140087Sbostic 
30240087Sbostic 	*pathend = EOS;
30340087Sbostic 	errno = 0;
30448540Sbostic 
30550050Sbostic 	if (!(dirp = g_opendir(pathbuf)))
30650050Sbostic 		/* TODO: don't call for ENOENT or ENOTDIR? */
30740087Sbostic 		if (pglob->gl_errfunc &&
30840087Sbostic 		    (*pglob->gl_errfunc)(pathbuf, errno) ||
30940087Sbostic 		    (pglob->gl_flags & GLOB_ERR))
31040087Sbostic 			return(GLOB_ABEND);
31140087Sbostic 		else
31240087Sbostic 			return(0);
31340087Sbostic 
31440087Sbostic 	err = 0;
31540087Sbostic 
31650050Sbostic 	/* Search directory for matching names. */
31740087Sbostic 	while ((dp = readdir(dirp))) {
31848540Sbostic 		register char *sc;
31950050Sbostic 		register Char *dc;
32050050Sbostic 
32150050Sbostic 		/* Initial DOT must be matched literally. */
32240087Sbostic 		if (dp->d_name[0] == DOT && *pattern != DOT)
32340087Sbostic 			continue;
32448540Sbostic 		for (sc = dp->d_name, dc = pathend;
32548540Sbostic 		     *dc++ = (u_char)*sc++;);
32648540Sbostic 		if (!match(pathend, pattern, restpattern)) {
32748540Sbostic 			*pathend = EOS;
32840087Sbostic 			continue;
32948540Sbostic 		}
33048540Sbostic 		err = glob2(pathbuf, --dc, restpattern, pglob);
33140087Sbostic 		if (err)
33240087Sbostic 			break;
33340087Sbostic 	}
33450050Sbostic 
33550050Sbostic 	/* TODO: check error from readdir? */
33640087Sbostic 	(void)closedir(dirp);
33740087Sbostic 	return(err);
33840087Sbostic }
33940087Sbostic 
34040087Sbostic 
34140087Sbostic /*
34240087Sbostic  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
34340087Sbostic  * add the new item, and update gl_pathc.
34440087Sbostic  *
34540087Sbostic  * This assumes the BSD realloc, which only copies the block when its size
34640087Sbostic  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
34740087Sbostic  * behavior.
34840087Sbostic  *
34940087Sbostic  * Return 0 if new item added, error code if memory couldn't be allocated.
35040087Sbostic  *
35140087Sbostic  * Invariant of the glob_t structure:
35240087Sbostic  *	Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
35350050Sbostic  *	gl_pathv points to (gl_offs + gl_pathc + 1) items.
35440087Sbostic  */
35548540Sbostic static int
35640087Sbostic globextend(path, pglob)
35750050Sbostic 	Char *path;
35840087Sbostic 	glob_t *pglob;
35940087Sbostic {
36040087Sbostic 	register char **pathv;
36140087Sbostic 	register int i;
36248540Sbostic 	u_int newsize;
36340087Sbostic 	char *copy;
36450050Sbostic 	Char *p;
36540087Sbostic 
36640087Sbostic 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
36748540Sbostic 	pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
36840087Sbostic 	if (pathv == NULL)
36940087Sbostic 		return(GLOB_NOSPACE);
37040087Sbostic 
37140087Sbostic 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
37240087Sbostic 		/* first time around -- clear initial gl_offs items */
37340087Sbostic 		pathv += pglob->gl_offs;
37440087Sbostic 		for (i = pglob->gl_offs; --i >= 0; )
37540087Sbostic 			*--pathv = NULL;
37640087Sbostic 	}
37740087Sbostic 	pglob->gl_pathv = pathv;
37840087Sbostic 
37948540Sbostic 	for (p = path; *p++;);
38048540Sbostic 	if ((copy = malloc(p - path)) != NULL) {
38150050Sbostic 		g_Ctoc(path, copy);
38240087Sbostic 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
38340087Sbostic 	}
38440087Sbostic 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
38550050Sbostic 	return(copy == NULL ? GLOB_NOSPACE : 0);
38640087Sbostic }
38740087Sbostic 
38840087Sbostic 
38940087Sbostic /*
39040087Sbostic  * pattern matching function for filenames.  Each occurrence of the *
39140087Sbostic  * pattern causes a recursion level.
39240087Sbostic  */
39348540Sbostic static
39440087Sbostic match(name, pat, patend)
39550050Sbostic 	register Char *name, *pat, *patend;
39640087Sbostic {
39748540Sbostic 	int ok, negate_range;
39850050Sbostic 	Char c, k;
39940087Sbostic 
40040087Sbostic 	while (pat < patend) {
40140087Sbostic 		c = *pat++;
40248540Sbostic 		switch (c & M_MASK) {
40340087Sbostic 		case M_ALL:
40440087Sbostic 			if (pat == patend)
40540087Sbostic 				return(1);
40650050Sbostic 			for (; *name != EOS; ++name)
40740087Sbostic 				if (match(name, pat, patend))
40840087Sbostic 					return(1);
40940087Sbostic 			return(0);
41040087Sbostic 		case M_ONE:
41140087Sbostic 			if (*name++ == EOS)
41240087Sbostic 				return(0);
41340087Sbostic 			break;
41440087Sbostic 		case M_SET:
41540087Sbostic 			ok = 0;
41640087Sbostic 			k = *name++;
41748540Sbostic 			if (negate_range = ((*pat & M_MASK) == M_NOT))
41840087Sbostic 				++pat;
41950050Sbostic 			while (((c = *pat++) & M_MASK) != M_END)
42048540Sbostic 				if ((*pat & M_MASK) == M_RNG) {
42140087Sbostic 					if (c <= k && k <= pat[1])
42240087Sbostic 						ok = 1;
42340087Sbostic 					pat += 2;
42450050Sbostic 				} else if (c == k)
42540087Sbostic 					ok = 1;
42640087Sbostic 			if (ok == negate_range)
42740087Sbostic 				return(0);
42840087Sbostic 			break;
42940087Sbostic 		default:
43040087Sbostic 			if (*name++ != c)
43140087Sbostic 				return(0);
43240087Sbostic 			break;
43340087Sbostic 		}
43440087Sbostic 	}
43540087Sbostic 	return(*name == EOS);
43640087Sbostic }
43740087Sbostic 
43850050Sbostic /* Free allocated data belonging to a glob_t structure. */
43940087Sbostic void
44040087Sbostic globfree(pglob)
44140087Sbostic 	glob_t *pglob;
44240087Sbostic {
44340087Sbostic 	register int i;
44440087Sbostic 	register char **pp;
44540087Sbostic 
44640087Sbostic 	if (pglob->gl_pathv != NULL) {
44740087Sbostic 		pp = pglob->gl_pathv + pglob->gl_offs;
44840087Sbostic 		for (i = pglob->gl_pathc; i--; ++pp)
44940087Sbostic 			if (*pp)
45050050Sbostic 				free(*pp);
45150049Sbostic 		free(pglob->gl_pathv);
45240087Sbostic 	}
45340087Sbostic }
45450050Sbostic 
45550050Sbostic static DIR *
45650050Sbostic g_opendir(str)
45750050Sbostic 	register Char *str;
45850050Sbostic {
45950050Sbostic 	char buf[MAXPATHLEN];
46050050Sbostic 
46150050Sbostic 	if (!*str)
46250050Sbostic 		return(opendir("."));
46350050Sbostic 	g_Ctoc(str, buf);
46450050Sbostic 	return(opendir(buf));
46550050Sbostic }
46650050Sbostic 
46750050Sbostic static int
46850050Sbostic g_lstat(fn, sb)
46950050Sbostic 	register Char *fn;
47050050Sbostic 	struct stat *sb;
47150050Sbostic {
47250050Sbostic 	char buf[MAXPATHLEN];
47350050Sbostic 
47450050Sbostic 	g_Ctoc(fn, buf);
47550050Sbostic 	return(lstat(buf, sb));
47650050Sbostic }
47750050Sbostic 
47850050Sbostic static int
47950050Sbostic g_stat(fn, sb)
48050050Sbostic 	register Char *fn;
48150050Sbostic 	struct stat *sb;
48250050Sbostic {
48350050Sbostic 	char buf[MAXPATHLEN];
48450050Sbostic 
48550050Sbostic 	g_Ctoc(fn, buf);
48650050Sbostic 	return(stat(buf, sb));
48750050Sbostic }
48850050Sbostic 
48950050Sbostic static Char *
49050050Sbostic g_strchr(str, ch)
49150050Sbostic 	Char *str;
49250050Sbostic 	int ch;
49350050Sbostic {
49450050Sbostic 	do {
49550050Sbostic 		if (*str == ch)
49650050Sbostic 			return (str);
49750050Sbostic 	} while (*str++);
49850050Sbostic 	return (NULL);
49950050Sbostic }
50050050Sbostic 
50150050Sbostic static void
50250050Sbostic g_Ctoc(str, buf)
50350050Sbostic 	register Char *str;
50450050Sbostic 	char *buf;
50550050Sbostic {
50650050Sbostic 	register char *dc;
50750050Sbostic 
50850050Sbostic 	for (dc = buf; *dc++ = *str++;);
50950050Sbostic }
51050050Sbostic 
51150050Sbostic #ifdef DEBUG
51250050Sbostic static void
51350050Sbostic qprintf(s)
51450050Sbostic 	register Char *s;
51550050Sbostic {
51650050Sbostic 	register Char *p;
51750050Sbostic 
51850050Sbostic 	for (p = s; *p; p++)
51950050Sbostic 		(void)printf("%c", *p & 0xff);
52050050Sbostic 	(void)printf("\n");
52150050Sbostic 	for (p = s; *p; p++)
52250050Sbostic 		(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
52350050Sbostic 	(void)printf("\n");
52450050Sbostic 	for (p = s; *p; p++)
52550050Sbostic 		(void)printf("%c", *p & M_META ? '_' : ' ');
52650050Sbostic 	(void)printf("\n");
52750050Sbostic }
52850050Sbostic #endif
529