Change th_get_size() macro to return unsigned int
[libtar.git] / compat / glob.c
blob9ee235a35f3b4c168f2153fa5e47fa43db799f05
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Guido van Rossum.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
40 #else
41 static char rcsid[] = "$OpenBSD: glob.c,v 1.8 1998/08/14 21:39:30 deraadt Exp $";
42 #endif
43 #endif /* LIBC_SCCS and not lint */
46 * glob(3) -- a superset of the one defined in POSIX 1003.2.
48 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
50 * Optional extra services, controlled by flags not defined by POSIX:
52 * GLOB_QUOTE:
53 * Escaping convention: \ inhibits any special meaning the following
54 * character might have (except \ at end of string is retained).
55 * GLOB_MAGCHAR:
56 * Set in gl_flags if pattern contained a globbing character.
57 * GLOB_NOMAGIC:
58 * Same as GLOB_NOCHECK, but it will only append pattern if it did
59 * not contain any magic characters. [Used in csh style globbing]
60 * GLOB_ALTDIRFUNC:
61 * Use alternately specified directory access functions.
62 * GLOB_TILDE:
63 * expand ~user/foo to the /home/dir/of/user/foo
64 * GLOB_BRACE:
65 * expand {1,2}{a,b} to 1a 1b 2a 2b
66 * gl_matchc:
67 * Number of matches in the current invocation of glob.
70 #include <config.h>
72 #include <sys/param.h>
73 #include <sys/stat.h>
75 #include <dirent.h>
76 #include <errno.h>
77 #include <pwd.h>
78 #include <stdio.h>
80 #ifdef STDC_HEADERS
81 # include <stdlib.h>
82 # include <string.h>
83 #endif
85 #ifdef HAVE_UNISTD_H
86 # include <unistd.h>
87 #endif
89 #include <compat.h>
92 #define DOLLAR '$'
93 #define DOT '.'
94 #define EOS '\0'
95 #define LBRACKET '['
96 #define NOT '!'
97 #define QUESTION '?'
98 #define QUOTE '\\'
99 #define RANGE '-'
100 #define RBRACKET ']'
101 #define SEP '/'
102 #define STAR '*'
103 #define TILDE '~'
104 #define UNDERSCORE '_'
105 #define LBRACE '{'
106 #define RBRACE '}'
107 #define SLASH '/'
108 #define COMMA ','
110 #ifndef DEBUG
112 #define M_QUOTE 0x8000
113 #define M_PROTECT 0x4000
114 #define M_MASK 0xffff
115 #define M_ASCII 0x00ff
117 typedef u_short Char;
119 #else
121 #define M_QUOTE 0x80
122 #define M_PROTECT 0x40
123 #define M_MASK 0xff
124 #define M_ASCII 0x7f
126 typedef char Char;
128 #endif
131 #define CHAR(c) ((Char)((c)&M_ASCII))
132 #define META(c) ((Char)((c)|M_QUOTE))
133 #define M_ALL META('*')
134 #define M_END META(']')
135 #define M_NOT META('!')
136 #define M_ONE META('?')
137 #define M_RNG META('-')
138 #define M_SET META('[')
139 #define ismeta(c) (((c)&M_QUOTE) != 0)
142 static int compare (const void *, const void *);
143 static void g_Ctoc (const Char *, char *);
144 static int g_lstat (Char *, struct stat *, glob_t *);
145 static DIR *g_opendir (Char *, glob_t *);
146 static Char *g_strchr (Char *, int);
147 #ifdef notdef
148 static Char *g_strcat (Char *, const Char *);
149 #endif
150 static int g_stat (Char *, struct stat *, glob_t *);
151 static int glob0 (const Char *, glob_t *);
152 static int glob1 (Char *, glob_t *);
153 static int glob2 (Char *, Char *, Char *, glob_t *);
154 static int glob3 (Char *, Char *, Char *, Char *, glob_t *);
155 static int globextend (const Char *, glob_t *);
156 static const Char * globtilde (const Char *, Char *, size_t, glob_t *);
157 static int globexp1 (const Char *, glob_t *);
158 static int globexp2 (const Char *, const Char *, glob_t *, int *);
159 static int match (Char *, Char *, Char *);
160 #ifdef DEBUG
161 static void qprintf (const char *, Char *);
162 #endif
165 openbsd_glob(pattern, flags, errfunc, pglob)
166 const char *pattern;
167 int flags, (*errfunc) __P((const char *, int));
168 glob_t *pglob;
170 const u_char *patnext;
171 int c;
172 Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
174 patnext = (u_char *) pattern;
175 if (!(flags & GLOB_APPEND)) {
176 pglob->gl_pathc = 0;
177 pglob->gl_pathv = NULL;
178 if (!(flags & GLOB_DOOFFS))
179 pglob->gl_offs = 0;
181 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
182 pglob->gl_errfunc = errfunc;
183 pglob->gl_matchc = 0;
185 bufnext = patbuf;
186 bufend = bufnext + MAXPATHLEN;
187 if (flags & GLOB_NOESCAPE)
188 while (bufnext < bufend && (c = *patnext++) != EOS)
189 *bufnext++ = c;
190 else {
191 /* Protect the quoted characters. */
192 while (bufnext < bufend && (c = *patnext++) != EOS)
193 if (c == QUOTE) {
194 if ((c = *patnext++) == EOS) {
195 c = QUOTE;
196 --patnext;
198 *bufnext++ = c | M_PROTECT;
200 else
201 *bufnext++ = c;
203 *bufnext = EOS;
205 if (flags & GLOB_BRACE)
206 return globexp1(patbuf, pglob);
207 else
208 return glob0(patbuf, pglob);
212 * Expand recursively a glob {} pattern. When there is no more expansion
213 * invoke the standard globbing routine to glob the rest of the magic
214 * characters
216 static int globexp1(pattern, pglob)
217 const Char *pattern;
218 glob_t *pglob;
220 const Char* ptr = pattern;
221 int rv;
223 /* Protect a single {}, for find(1), like csh */
224 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
225 return glob0(pattern, pglob);
227 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
228 if (!globexp2(ptr, pattern, pglob, &rv))
229 return rv;
231 return glob0(pattern, pglob);
236 * Recursive brace globbing helper. Tries to expand a single brace.
237 * If it succeeds then it invokes globexp1 with the new pattern.
238 * If it fails then it tries to glob the rest of the pattern and returns.
240 static int globexp2(ptr, pattern, pglob, rv)
241 const Char *ptr, *pattern;
242 glob_t *pglob;
243 int *rv;
245 int i;
246 Char *lm, *ls;
247 const Char *pe, *pm, *pl;
248 Char patbuf[MAXPATHLEN + 1];
250 /* copy part up to the brace */
251 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
252 continue;
253 ls = lm;
255 /* Find the balanced brace */
256 for (i = 0, pe = ++ptr; *pe; pe++)
257 if (*pe == LBRACKET) {
258 /* Ignore everything between [] */
259 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
260 continue;
261 if (*pe == EOS) {
263 * We could not find a matching RBRACKET.
264 * Ignore and just look for RBRACE
266 pe = pm;
269 else if (*pe == LBRACE)
270 i++;
271 else if (*pe == RBRACE) {
272 if (i == 0)
273 break;
274 i--;
277 /* Non matching braces; just glob the pattern */
278 if (i != 0 || *pe == EOS) {
279 *rv = glob0(patbuf, pglob);
280 return 0;
283 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
284 switch (*pm) {
285 case LBRACKET:
286 /* Ignore everything between [] */
287 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
288 continue;
289 if (*pm == EOS) {
291 * We could not find a matching RBRACKET.
292 * Ignore and just look for RBRACE
294 pm = pl;
296 break;
298 case LBRACE:
299 i++;
300 break;
302 case RBRACE:
303 if (i) {
304 i--;
305 break;
307 /* FALLTHROUGH */
308 case COMMA:
309 if (i && *pm == COMMA)
310 break;
311 else {
312 /* Append the current string */
313 for (lm = ls; (pl < pm); *lm++ = *pl++)
314 continue;
316 * Append the rest of the pattern after the
317 * closing brace
319 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
320 continue;
322 /* Expand the current pattern */
323 #ifdef DEBUG
324 qprintf("globexp2:", patbuf);
325 #endif
326 *rv = globexp1(patbuf, pglob);
328 /* move after the comma, to the next string */
329 pl = pm + 1;
331 break;
333 default:
334 break;
336 *rv = 0;
337 return 0;
343 * expand tilde from the passwd file.
345 static const Char *
346 globtilde(pattern, patbuf, patbuf_len, pglob)
347 const Char *pattern;
348 Char *patbuf;
349 size_t patbuf_len;
350 glob_t *pglob;
352 struct passwd *pwd;
353 char *h;
354 const Char *p;
355 Char *b, *eb;
357 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
358 return pattern;
360 /* Copy up to the end of the string or / */
361 eb = &patbuf[patbuf_len - 1];
362 for (p = pattern + 1, h = (char *) patbuf;
363 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
364 continue;
366 *h = EOS;
368 if (((char *) patbuf)[0] == EOS) {
370 * handle a plain ~ or ~/ by expanding $HOME
371 * first and then trying the password file
373 #ifdef HAVE_ISSETUGID
374 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
375 #endif
376 if ((pwd = getpwuid(getuid())) == NULL)
377 return pattern;
378 else
379 h = pwd->pw_dir;
380 #ifdef HAVE_ISSETUGID
382 #endif
384 else {
386 * Expand a ~user
388 if ((pwd = getpwnam((char*) patbuf)) == NULL)
389 return pattern;
390 else
391 h = pwd->pw_dir;
394 /* Copy the home directory */
395 for (b = patbuf; b < eb && *h; *b++ = *h++)
396 continue;
398 /* Append the rest of the pattern */
399 while (b < eb && (*b++ = *p++) != EOS)
400 continue;
401 *b = EOS;
403 return patbuf;
408 * The main glob() routine: compiles the pattern (optionally processing
409 * quotes), calls glob1() to do the real pattern matching, and finally
410 * sorts the list (unless unsorted operation is requested). Returns 0
411 * if things went well, nonzero if errors occurred. It is not an error
412 * to find no matches.
414 static int
415 glob0(pattern, pglob)
416 const Char *pattern;
417 glob_t *pglob;
419 const Char *qpatnext;
420 int c, err, oldpathc;
421 Char *bufnext, patbuf[MAXPATHLEN+1];
423 qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char),
424 pglob);
425 oldpathc = pglob->gl_pathc;
426 bufnext = patbuf;
428 /* We don't need to check for buffer overflow any more. */
429 while ((c = *qpatnext++) != EOS) {
430 switch (c) {
431 case LBRACKET:
432 c = *qpatnext;
433 if (c == NOT)
434 ++qpatnext;
435 if (*qpatnext == EOS ||
436 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
437 *bufnext++ = LBRACKET;
438 if (c == NOT)
439 --qpatnext;
440 break;
442 *bufnext++ = M_SET;
443 if (c == NOT)
444 *bufnext++ = M_NOT;
445 c = *qpatnext++;
446 do {
447 *bufnext++ = CHAR(c);
448 if (*qpatnext == RANGE &&
449 (c = qpatnext[1]) != RBRACKET) {
450 *bufnext++ = M_RNG;
451 *bufnext++ = CHAR(c);
452 qpatnext += 2;
454 } while ((c = *qpatnext++) != RBRACKET);
455 pglob->gl_flags |= GLOB_MAGCHAR;
456 *bufnext++ = M_END;
457 break;
458 case QUESTION:
459 pglob->gl_flags |= GLOB_MAGCHAR;
460 *bufnext++ = M_ONE;
461 break;
462 case STAR:
463 pglob->gl_flags |= GLOB_MAGCHAR;
464 /* collapse adjacent stars to one,
465 * to avoid exponential behavior
467 if (bufnext == patbuf || bufnext[-1] != M_ALL)
468 *bufnext++ = M_ALL;
469 break;
470 default:
471 *bufnext++ = CHAR(c);
472 break;
475 *bufnext = EOS;
476 #ifdef DEBUG
477 qprintf("glob0:", patbuf);
478 #endif
480 if ((err = glob1(patbuf, pglob)) != 0)
481 return(err);
484 * If there was no match we are going to append the pattern
485 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
486 * and the pattern did not contain any magic characters
487 * GLOB_NOMAGIC is there just for compatibility with csh.
489 if (pglob->gl_pathc == oldpathc) {
490 if ((pglob->gl_flags & GLOB_NOCHECK) ||
491 ((pglob->gl_flags & GLOB_NOMAGIC) &&
492 !(pglob->gl_flags & GLOB_MAGCHAR)))
493 return(globextend(pattern, pglob));
494 else
495 return(GLOB_NOMATCH);
497 if (!(pglob->gl_flags & GLOB_NOSORT))
498 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
499 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
500 return(0);
503 static int
504 compare(p, q)
505 const void *p, *q;
507 return(strcmp(*(char **)p, *(char **)q));
510 static int
511 glob1(pattern, pglob)
512 Char *pattern;
513 glob_t *pglob;
515 Char pathbuf[MAXPATHLEN+1];
517 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
518 if (*pattern == EOS)
519 return(0);
520 return(glob2(pathbuf, pathbuf, pattern, pglob));
524 * The functions glob2 and glob3 are mutually recursive; there is one level
525 * of recursion for each segment in the pattern that contains one or more
526 * meta characters.
528 static int
529 glob2(pathbuf, pathend, pattern, pglob)
530 Char *pathbuf, *pathend, *pattern;
531 glob_t *pglob;
533 struct stat sb;
534 Char *p, *q;
535 int anymeta;
538 * Loop over pattern segments until end of pattern or until
539 * segment with meta character found.
541 for (anymeta = 0;;) {
542 if (*pattern == EOS) { /* End of pattern? */
543 *pathend = EOS;
544 if (g_lstat(pathbuf, &sb, pglob))
545 return(0);
547 if (((pglob->gl_flags & GLOB_MARK) &&
548 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
549 || (S_ISLNK(sb.st_mode) &&
550 (g_stat(pathbuf, &sb, pglob) == 0) &&
551 S_ISDIR(sb.st_mode)))) {
552 *pathend++ = SEP;
553 *pathend = EOS;
555 ++pglob->gl_matchc;
556 return(globextend(pathbuf, pglob));
559 /* Find end of next segment, copy tentatively to pathend. */
560 q = pathend;
561 p = pattern;
562 while (*p != EOS && *p != SEP) {
563 if (ismeta(*p))
564 anymeta = 1;
565 *q++ = *p++;
568 if (!anymeta) { /* No expansion, do next segment. */
569 pathend = q;
570 pattern = p;
571 while (*pattern == SEP)
572 *pathend++ = *pattern++;
573 } else /* Need expansion, recurse. */
574 return(glob3(pathbuf, pathend, pattern, p, pglob));
576 /* NOTREACHED */
579 static int
580 glob3(pathbuf, pathend, pattern, restpattern, pglob)
581 Char *pathbuf, *pathend, *pattern, *restpattern;
582 glob_t *pglob;
584 register struct dirent *dp;
585 DIR *dirp;
586 int err;
587 char buf[MAXPATHLEN];
590 * The readdirfunc declaration can't be prototyped, because it is
591 * assigned, below, to two functions which are prototyped in glob.h
592 * and dirent.h as taking pointers to differently typed opaque
593 * structures.
595 struct dirent *(*readdirfunc)();
597 *pathend = EOS;
598 errno = 0;
600 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
601 /* TODO: don't call for ENOENT or ENOTDIR? */
602 if (pglob->gl_errfunc) {
603 g_Ctoc(pathbuf, buf);
604 if (pglob->gl_errfunc(buf, errno) ||
605 pglob->gl_flags & GLOB_ERR)
606 return (GLOB_ABORTED);
608 return(0);
611 err = 0;
613 /* Search directory for matching names. */
614 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
615 readdirfunc = pglob->gl_readdir;
616 else
617 readdirfunc = readdir;
618 while ((dp = (*readdirfunc)(dirp))) {
619 register u_char *sc;
620 register Char *dc;
622 /* Initial DOT must be matched literally. */
623 if (dp->d_name[0] == DOT && *pattern != DOT)
624 continue;
625 for (sc = (u_char *) dp->d_name, dc = pathend;
626 (*dc++ = *sc++) != EOS;)
627 continue;
628 if (!match(pathend, pattern, restpattern)) {
629 *pathend = EOS;
630 continue;
632 err = glob2(pathbuf, --dc, restpattern, pglob);
633 if (err)
634 break;
637 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
638 (*pglob->gl_closedir)(dirp);
639 else
640 closedir(dirp);
641 return(err);
646 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
647 * add the new item, and update gl_pathc.
649 * This assumes the BSD realloc, which only copies the block when its size
650 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
651 * behavior.
653 * Return 0 if new item added, error code if memory couldn't be allocated.
655 * Invariant of the glob_t structure:
656 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
657 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
659 static int
660 globextend(path, pglob)
661 const Char *path;
662 glob_t *pglob;
664 register char **pathv;
665 register int i;
666 u_int newsize;
667 char *copy;
668 const Char *p;
670 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
671 pathv = pglob->gl_pathv ?
672 realloc((char *)pglob->gl_pathv, newsize) :
673 malloc(newsize);
674 if (pathv == NULL) {
675 if (pglob->gl_pathv)
676 free(pglob->gl_pathv);
677 return(GLOB_NOSPACE);
680 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
681 /* first time around -- clear initial gl_offs items */
682 pathv += pglob->gl_offs;
683 for (i = pglob->gl_offs; --i >= 0; )
684 *--pathv = NULL;
686 pglob->gl_pathv = pathv;
688 for (p = path; *p++;)
689 continue;
690 if ((copy = malloc(p - path)) != NULL) {
691 g_Ctoc(path, copy);
692 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
694 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
695 return(copy == NULL ? GLOB_NOSPACE : 0);
700 * pattern matching function for filenames. Each occurrence of the *
701 * pattern causes a recursion level.
703 static int
704 match(name, pat, patend)
705 register Char *name, *pat, *patend;
707 int ok, negate_range;
708 Char c, k;
710 while (pat < patend) {
711 c = *pat++;
712 switch (c & M_MASK) {
713 case M_ALL:
714 if (pat == patend)
715 return(1);
717 if (match(name, pat, patend))
718 return(1);
719 while (*name++ != EOS);
720 return(0);
721 case M_ONE:
722 if (*name++ == EOS)
723 return(0);
724 break;
725 case M_SET:
726 ok = 0;
727 if ((k = *name++) == EOS)
728 return(0);
729 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
730 ++pat;
731 while (((c = *pat++) & M_MASK) != M_END)
732 if ((*pat & M_MASK) == M_RNG) {
733 if (c <= k && k <= pat[1])
734 ok = 1;
735 pat += 2;
736 } else if (c == k)
737 ok = 1;
738 if (ok == negate_range)
739 return(0);
740 break;
741 default:
742 if (*name++ != c)
743 return(0);
744 break;
747 return(*name == EOS);
750 /* Free allocated data belonging to a glob_t structure. */
751 void
752 openbsd_globfree(pglob)
753 glob_t *pglob;
755 register int i;
756 register char **pp;
758 if (pglob->gl_pathv != NULL) {
759 pp = pglob->gl_pathv + pglob->gl_offs;
760 for (i = pglob->gl_pathc; i--; ++pp)
761 if (*pp)
762 free(*pp);
763 free(pglob->gl_pathv);
767 static DIR *
768 g_opendir(str, pglob)
769 register Char *str;
770 glob_t *pglob;
772 char buf[MAXPATHLEN];
774 if (!*str)
775 strcpy(buf, ".");
776 else
777 g_Ctoc(str, buf);
779 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
780 return((*pglob->gl_opendir)(buf));
782 return(opendir(buf));
785 static int
786 g_lstat(fn, sb, pglob)
787 register Char *fn;
788 struct stat *sb;
789 glob_t *pglob;
791 char buf[MAXPATHLEN];
793 g_Ctoc(fn, buf);
794 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
795 return((*pglob->gl_lstat)(buf, sb));
796 return(lstat(buf, sb));
799 static int
800 g_stat(fn, sb, pglob)
801 register Char *fn;
802 struct stat *sb;
803 glob_t *pglob;
805 char buf[MAXPATHLEN];
807 g_Ctoc(fn, buf);
808 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
809 return((*pglob->gl_stat)(buf, sb));
810 return(stat(buf, sb));
813 static Char *
814 g_strchr(str, ch)
815 Char *str;
816 int ch;
818 do {
819 if (*str == ch)
820 return (str);
821 } while (*str++);
822 return (NULL);
825 #ifdef notdef
826 static Char *
827 g_strcat(dst, src)
828 Char *dst;
829 const Char* src;
831 Char *sdst = dst;
833 while (*dst++)
834 continue;
835 --dst;
836 while((*dst++ = *src++) != EOS)
837 continue;
839 return (sdst);
841 #endif
843 static void
844 g_Ctoc(str, buf)
845 register const Char *str;
846 char *buf;
848 register char *dc;
850 for (dc = buf; (*dc++ = *str++) != EOS;)
851 continue;
854 #ifdef DEBUG
855 static void
856 qprintf(str, s)
857 const char *str;
858 register Char *s;
860 register Char *p;
862 (void)printf("%s:\n", str);
863 for (p = s; *p; p++)
864 (void)printf("%c", CHAR(*p));
865 (void)printf("\n");
866 for (p = s; *p; p++)
867 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
868 (void)printf("\n");
869 for (p = s; *p; p++)
870 (void)printf("%c", ismeta(*p) ? '_' : ' ');
871 (void)printf("\n");
873 #endif