There is something wrong around this select to ADK_BROKEN
[openadk.git] / config / glob.c
blobb8928b1b64f6c6a8ab31c37cb6cf055fca040d22
1 /* $OpenBSD: glob.c,v 1.25 2005/08/08 08:05:34 espie Exp $ */
2 /*
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Guido van Rossum.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * glob(3) -- a superset of the one defined in POSIX 1003.2.
37 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
39 * Optional extra services, controlled by flags not defined by POSIX:
41 * GLOB_QUOTE:
42 * Escaping convention: \ inhibits any special meaning the following
43 * character might have (except \ at end of string is retained).
44 * GLOB_MAGCHAR:
45 * Set in gl_flags if pattern contained a globbing character.
46 * GLOB_NOMAGIC:
47 * Same as GLOB_NOCHECK, but it will only append pattern if it did
48 * not contain any magic characters. [Used in csh style globbing]
49 * GLOB_ALTDIRFUNC:
50 * Use alternately specified directory access functions.
51 * GLOB_TILDE:
52 * expand ~user/foo to the /home/dir/of/user/foo
53 * GLOB_BRACE:
54 * expand {1,2}{a,b} to 1a 1b 2a 2b
55 * gl_matchc:
56 * Number of matches in the current invocation of glob.
59 #ifdef __APPLE__
60 #include <sys/param.h>
61 #include <sys/stat.h>
63 #include <ctype.h>
64 #include <dirent.h>
65 #include <errno.h>
66 #include <glob.h>
67 #include <pwd.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
73 #define DOLLAR '$'
74 #define DOT '.'
75 #define EOS '\0'
76 #define LBRACKET '['
77 #define NOT '!'
78 #define QUESTION '?'
79 #define QUOTE '\\'
80 #define RANGE '-'
81 #define RBRACKET ']'
82 #define SEP '/'
83 #define STAR '*'
84 #define TILDE '~'
85 #define UNDERSCORE '_'
86 #define LBRACE '{'
87 #define RBRACE '}'
88 #define SLASH '/'
89 #define COMMA ','
91 #ifndef DEBUG
93 #define M_QUOTE 0x8000
94 #define M_PROTECT 0x4000
95 #define M_MASK 0xffff
96 #define M_ASCII 0x00ff
98 typedef u_short Char;
100 #else
102 #define M_QUOTE 0x80
103 #define M_PROTECT 0x40
104 #define M_MASK 0xff
105 #define M_ASCII 0x7f
107 typedef char Char;
109 #endif
112 #define CHAR(c) ((Char)((c)&M_ASCII))
113 #define META(c) ((Char)((c)|M_QUOTE))
114 #define M_ALL META('*')
115 #define M_END META(']')
116 #define M_NOT META('!')
117 #define M_ONE META('?')
118 #define M_RNG META('-')
119 #define M_SET META('[')
120 #define ismeta(c) (((c)&M_QUOTE) != 0)
123 static int compare(const void *, const void *);
124 static int g_Ctoc(const Char *, char *, u_int);
125 static int g_lstat(Char *, struct stat *, glob_t *);
126 static DIR *g_opendir(Char *, glob_t *);
127 static const Char *g_strchr(const Char *, int);
128 static int g_stat(Char *, struct stat *, glob_t *);
129 static int glob0(const Char *, glob_t *);
130 static int glob1(Char *, Char *, glob_t *, size_t *);
131 static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
132 glob_t *, size_t *);
133 static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
134 Char *, Char *, glob_t *, size_t *);
135 static int globextend(const Char *, glob_t *, size_t *);
136 static const Char *
137 globtilde(const Char *, Char *, size_t, glob_t *);
138 static int globexp1(const Char *, glob_t *);
139 static int globexp2(const Char *, const Char *, glob_t *, int *);
140 static int match(Char *, Char *, Char *);
141 #ifdef DEBUG
142 static void qprintf(const char *, Char *);
143 #endif
146 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
147 glob_t *pglob)
149 const u_char *patnext;
150 int c;
151 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
153 patnext = (const u_char *) pattern;
154 if (!(flags & GLOB_APPEND)) {
155 pglob->gl_pathc = 0;
156 pglob->gl_pathv = NULL;
157 if (!(flags & GLOB_DOOFFS))
158 pglob->gl_offs = 0;
160 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
161 pglob->gl_errfunc = errfunc;
162 pglob->gl_matchc = 0;
164 bufnext = patbuf;
165 bufend = bufnext + MAXPATHLEN - 1;
166 if (flags & GLOB_NOESCAPE)
167 while (bufnext < bufend && (c = *patnext++) != EOS)
168 *bufnext++ = c;
169 else {
170 /* Protect the quoted characters. */
171 while (bufnext < bufend && (c = *patnext++) != EOS)
172 if (c == QUOTE) {
173 if ((c = *patnext++) == EOS) {
174 c = QUOTE;
175 --patnext;
177 *bufnext++ = c | M_PROTECT;
178 } else
179 *bufnext++ = c;
181 *bufnext = EOS;
183 if (flags & GLOB_BRACE)
184 return globexp1(patbuf, pglob);
185 else
186 return glob0(patbuf, pglob);
190 * Expand recursively a glob {} pattern. When there is no more expansion
191 * invoke the standard globbing routine to glob the rest of the magic
192 * characters
194 static int
195 globexp1(const Char *pattern, glob_t *pglob)
197 const Char* ptr = pattern;
198 int rv;
200 /* Protect a single {}, for find(1), like csh */
201 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
202 return glob0(pattern, pglob);
204 while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
205 if (!globexp2(ptr, pattern, pglob, &rv))
206 return rv;
208 return glob0(pattern, pglob);
213 * Recursive brace globbing helper. Tries to expand a single brace.
214 * If it succeeds then it invokes globexp1 with the new pattern.
215 * If it fails then it tries to glob the rest of the pattern and returns.
217 static int
218 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv)
220 int i;
221 Char *lm, *ls;
222 const Char *pe, *pm, *pl;
223 Char patbuf[MAXPATHLEN];
225 /* copy part up to the brace */
226 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
228 *lm = EOS;
229 ls = lm;
231 /* Find the balanced brace */
232 for (i = 0, pe = ++ptr; *pe; pe++)
233 if (*pe == LBRACKET) {
234 /* Ignore everything between [] */
235 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
237 if (*pe == EOS) {
239 * We could not find a matching RBRACKET.
240 * Ignore and just look for RBRACE
242 pe = pm;
244 } else if (*pe == LBRACE)
245 i++;
246 else if (*pe == RBRACE) {
247 if (i == 0)
248 break;
249 i--;
252 /* Non matching braces; just glob the pattern */
253 if (i != 0 || *pe == EOS) {
254 *rv = glob0(patbuf, pglob);
255 return 0;
258 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
259 switch (*pm) {
260 case LBRACKET:
261 /* Ignore everything between [] */
262 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
264 if (*pm == EOS) {
266 * We could not find a matching RBRACKET.
267 * Ignore and just look for RBRACE
269 pm = pl;
271 break;
273 case LBRACE:
274 i++;
275 break;
277 case RBRACE:
278 if (i) {
279 i--;
280 break;
282 /* FALLTHROUGH */
283 case COMMA:
284 if (i && *pm == COMMA)
285 break;
286 else {
287 /* Append the current string */
288 for (lm = ls; (pl < pm); *lm++ = *pl++)
292 * Append the rest of the pattern after the
293 * closing brace
295 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
298 /* Expand the current pattern */
299 #ifdef DEBUG
300 qprintf("globexp2:", patbuf);
301 #endif
302 *rv = globexp1(patbuf, pglob);
304 /* move after the comma, to the next string */
305 pl = pm + 1;
307 break;
309 default:
310 break;
313 *rv = 0;
314 return 0;
320 * expand tilde from the passwd file.
322 static const Char *
323 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
325 struct passwd *pwd;
326 char *h;
327 const Char *p;
328 Char *b, *eb;
330 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
331 return pattern;
333 /* Copy up to the end of the string or / */
334 eb = &patbuf[patbuf_len - 1];
335 for (p = pattern + 1, h = (char *) patbuf;
336 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
339 *h = EOS;
341 #if 0
342 if (h == (char *)eb)
343 return what;
344 #endif
346 if (((char *) patbuf)[0] == EOS) {
348 * handle a plain ~ or ~/ by expanding $HOME
349 * first and then trying the password file
351 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
352 if ((pwd = getpwuid(getuid())) == NULL)
353 return pattern;
354 else
355 h = pwd->pw_dir;
357 } else {
359 * Expand a ~user
361 if ((pwd = getpwnam((char*) patbuf)) == NULL)
362 return pattern;
363 else
364 h = pwd->pw_dir;
367 /* Copy the home directory */
368 for (b = patbuf; b < eb && *h; *b++ = *h++)
371 /* Append the rest of the pattern */
372 while (b < eb && (*b++ = *p++) != EOS)
374 *b = EOS;
376 return patbuf;
381 * The main glob() routine: compiles the pattern (optionally processing
382 * quotes), calls glob1() to do the real pattern matching, and finally
383 * sorts the list (unless unsorted operation is requested). Returns 0
384 * if things went well, nonzero if errors occurred. It is not an error
385 * to find no matches.
387 static int
388 glob0(const Char *pattern, glob_t *pglob)
390 const Char *qpatnext;
391 int c, err, oldpathc;
392 Char *bufnext, patbuf[MAXPATHLEN];
393 size_t limit = 0;
395 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
396 oldpathc = pglob->gl_pathc;
397 bufnext = patbuf;
399 /* We don't need to check for buffer overflow any more. */
400 while ((c = *qpatnext++) != EOS) {
401 switch (c) {
402 case LBRACKET:
403 c = *qpatnext;
404 if (c == NOT)
405 ++qpatnext;
406 if (*qpatnext == EOS ||
407 g_strchr(qpatnext+1, RBRACKET) == NULL) {
408 *bufnext++ = LBRACKET;
409 if (c == NOT)
410 --qpatnext;
411 break;
413 *bufnext++ = M_SET;
414 if (c == NOT)
415 *bufnext++ = M_NOT;
416 c = *qpatnext++;
417 do {
418 *bufnext++ = CHAR(c);
419 if (*qpatnext == RANGE &&
420 (c = qpatnext[1]) != RBRACKET) {
421 *bufnext++ = M_RNG;
422 *bufnext++ = CHAR(c);
423 qpatnext += 2;
425 } while ((c = *qpatnext++) != RBRACKET);
426 pglob->gl_flags |= GLOB_MAGCHAR;
427 *bufnext++ = M_END;
428 break;
429 case QUESTION:
430 pglob->gl_flags |= GLOB_MAGCHAR;
431 *bufnext++ = M_ONE;
432 break;
433 case STAR:
434 pglob->gl_flags |= GLOB_MAGCHAR;
435 /* collapse adjacent stars to one,
436 * to avoid exponential behavior
438 if (bufnext == patbuf || bufnext[-1] != M_ALL)
439 *bufnext++ = M_ALL;
440 break;
441 default:
442 *bufnext++ = CHAR(c);
443 break;
446 *bufnext = EOS;
447 #ifdef DEBUG
448 qprintf("glob0:", patbuf);
449 #endif
451 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
452 return(err);
455 * If there was no match we are going to append the pattern
456 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
457 * and the pattern did not contain any magic characters
458 * GLOB_NOMAGIC is there just for compatibility with csh.
460 if (pglob->gl_pathc == oldpathc) {
461 if ((pglob->gl_flags & GLOB_NOCHECK) ||
462 ((pglob->gl_flags & GLOB_NOMAGIC) &&
463 !(pglob->gl_flags & GLOB_MAGCHAR)))
464 return(globextend(pattern, pglob, &limit));
465 else
466 return(GLOB_NOMATCH);
468 if (!(pglob->gl_flags & GLOB_NOSORT))
469 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
470 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
471 return(0);
474 static int
475 compare(const void *p, const void *q)
477 return(strcmp(*(char *const *)p, *(char *const *)q));
480 static int
481 glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
483 Char pathbuf[MAXPATHLEN];
485 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
486 if (*pattern == EOS)
487 return(0);
488 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
489 pathbuf, pathbuf+MAXPATHLEN-1,
490 pattern, pattern_last, pglob, limitp));
494 * The functions glob2 and glob3 are mutually recursive; there is one level
495 * of recursion for each segment in the pattern that contains one or more
496 * meta characters.
498 static int
499 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
500 Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
502 struct stat sb;
503 Char *p, *q;
504 int anymeta;
507 * Loop over pattern segments until end of pattern or until
508 * segment with meta character found.
510 for (anymeta = 0;;) {
511 if (*pattern == EOS) { /* End of pattern? */
512 *pathend = EOS;
513 if (g_lstat(pathbuf, &sb, pglob))
514 return(0);
516 if (((pglob->gl_flags & GLOB_MARK) &&
517 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
518 (S_ISLNK(sb.st_mode) &&
519 (g_stat(pathbuf, &sb, pglob) == 0) &&
520 S_ISDIR(sb.st_mode)))) {
521 if (pathend+1 > pathend_last)
522 return (1);
523 *pathend++ = SEP;
524 *pathend = EOS;
526 ++pglob->gl_matchc;
527 return(globextend(pathbuf, pglob, limitp));
530 /* Find end of next segment, copy tentatively to pathend. */
531 q = pathend;
532 p = pattern;
533 while (*p != EOS && *p != SEP) {
534 if (ismeta(*p))
535 anymeta = 1;
536 if (q+1 > pathend_last)
537 return (1);
538 *q++ = *p++;
541 if (!anymeta) { /* No expansion, do next segment. */
542 pathend = q;
543 pattern = p;
544 while (*pattern == SEP) {
545 if (pathend+1 > pathend_last)
546 return (1);
547 *pathend++ = *pattern++;
549 } else
550 /* Need expansion, recurse. */
551 return(glob3(pathbuf, pathbuf_last, pathend,
552 pathend_last, pattern, pattern_last,
553 p, pattern_last, pglob, limitp));
555 /* NOTREACHED */
558 static int
559 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
560 Char *pattern, Char *pattern_last __attribute__((unused)), Char *restpattern,
561 Char *restpattern_last, glob_t *pglob, size_t *limitp)
563 struct dirent *dp;
564 DIR *dirp;
565 int err;
566 char buf[MAXPATHLEN];
569 * The readdirfunc declaration can't be prototyped, because it is
570 * assigned, below, to two functions which are prototyped in glob.h
571 * and dirent.h as taking pointers to differently typed opaque
572 * structures.
574 struct dirent *(*readdirfunc)(void *);
576 if (pathend > pathend_last)
577 return (1);
578 *pathend = EOS;
579 errno = 0;
581 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
582 /* TODO: don't call for ENOENT or ENOTDIR? */
583 if (pglob->gl_errfunc) {
584 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
585 return(GLOB_ABORTED);
586 if (pglob->gl_errfunc(buf, errno) ||
587 pglob->gl_flags & GLOB_ERR)
588 return(GLOB_ABORTED);
590 return(0);
593 err = 0;
595 /* Search directory for matching names. */
596 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
597 readdirfunc = pglob->gl_readdir;
598 else
599 readdirfunc = (struct dirent *(*)(void *))readdir;
600 while ((dp = (*readdirfunc)(dirp))) {
601 u_char *sc;
602 Char *dc;
604 /* Initial DOT must be matched literally. */
605 if (dp->d_name[0] == DOT && *pattern != DOT)
606 continue;
607 dc = pathend;
608 sc = (u_char *) dp->d_name;
609 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
611 if (dc >= pathend_last) {
612 *dc = EOS;
613 err = 1;
614 break;
617 if (!match(pathend, pattern, restpattern)) {
618 *pathend = EOS;
619 continue;
621 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
622 restpattern, restpattern_last, pglob, limitp);
623 if (err)
624 break;
627 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
628 (*pglob->gl_closedir)(dirp);
629 else
630 closedir(dirp);
631 return(err);
636 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
637 * add the new item, and update gl_pathc.
639 * This assumes the BSD realloc, which only copies the block when its size
640 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
641 * behavior.
643 * Return 0 if new item added, error code if memory couldn't be allocated.
645 * Invariant of the glob_t structure:
646 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
647 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
649 static int
650 globextend(const Char *path, glob_t *pglob, size_t *limitp)
652 char **pathv;
653 int i;
654 u_int newsize, len;
655 char *copy;
656 const Char *p;
658 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
659 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
660 malloc(newsize);
661 if (pathv == NULL) {
662 if (pglob->gl_pathv) {
663 free(pglob->gl_pathv);
664 pglob->gl_pathv = NULL;
666 return(GLOB_NOSPACE);
669 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
670 /* first time around -- clear initial gl_offs items */
671 pathv += pglob->gl_offs;
672 for (i = pglob->gl_offs; --i >= 0; )
673 *--pathv = NULL;
675 pglob->gl_pathv = pathv;
677 for (p = path; *p++;)
679 len = (size_t)(p - path);
680 *limitp += len;
681 if ((copy = malloc(len)) != NULL) {
682 if (g_Ctoc(path, copy, len)) {
683 free(copy);
684 return(GLOB_NOSPACE);
686 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
688 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
690 if ((pglob->gl_flags & GLOB_LIMIT) &&
691 newsize + *limitp >= ARG_MAX) {
692 errno = 0;
693 return(GLOB_NOSPACE);
696 return(copy == NULL ? GLOB_NOSPACE : 0);
701 * pattern matching function for filenames. Each occurrence of the *
702 * pattern causes a recursion level.
704 static int
705 match(Char *name, Char *pat, Char *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);
716 do {
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 globfree(glob_t *pglob)
754 int i;
755 char **pp;
757 if (pglob->gl_pathv != NULL) {
758 pp = pglob->gl_pathv + pglob->gl_offs;
759 for (i = pglob->gl_pathc; i--; ++pp)
760 if (*pp)
761 free(*pp);
762 free(pglob->gl_pathv);
763 pglob->gl_pathv = NULL;
767 static DIR *
768 g_opendir(Char *str, glob_t *pglob)
770 char buf[MAXPATHLEN];
772 if (!*str)
773 strlcpy(buf, ".", sizeof buf);
774 else {
775 if (g_Ctoc(str, buf, sizeof(buf)))
776 return(NULL);
779 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
780 return((*pglob->gl_opendir)(buf));
782 return(opendir(buf));
785 static int
786 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
788 char buf[MAXPATHLEN];
790 if (g_Ctoc(fn, buf, sizeof(buf)))
791 return(-1);
792 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
793 return((*pglob->gl_lstat)(buf, sb));
794 return(lstat(buf, sb));
797 static int
798 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
800 char buf[MAXPATHLEN];
802 if (g_Ctoc(fn, buf, sizeof(buf)))
803 return(-1);
804 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
805 return((*pglob->gl_stat)(buf, sb));
806 return(stat(buf, sb));
809 static const Char *
810 g_strchr(const Char *str, int ch)
812 do {
813 if (*str == ch)
814 return (str);
815 } while (*str++);
816 return (NULL);
819 static int
820 g_Ctoc(const Char *str, char *buf, u_int len)
823 while (len--) {
824 if ((*buf++ = *str++) == EOS)
825 return (0);
827 return (1);
830 #ifdef DEBUG
831 static void
832 qprintf(const char *str, Char *s)
834 Char *p;
836 (void)printf("%s:\n", str);
837 for (p = s; *p; p++)
838 (void)printf("%c", CHAR(*p));
839 (void)printf("\n");
840 for (p = s; *p; p++)
841 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
842 (void)printf("\n");
843 for (p = s; *p; p++)
844 (void)printf("%c", ismeta(*p) ? '_' : ' ');
845 (void)printf("\n");
847 #endif
848 #endif