1 /* $OpenBSD: glob.c,v 1.49 2020/04/21 08:25:22 dtucker Exp $ */
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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
34 /* OPENBSD ORIGINAL: lib/libc/gen/glob.c */
37 * glob(3) -- a superset of the one defined in POSIX 1003.2.
39 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
41 * Optional extra services, controlled by flags not defined by POSIX:
44 * Escaping convention: \ inhibits any special meaning the following
45 * character might have (except \ at end of string is retained).
47 * Set in gl_flags if pattern contained a globbing character.
49 * Same as GLOB_NOCHECK, but it will only append pattern if it did
50 * not contain any magic characters. [Used in csh style globbing]
52 * Use alternately specified directory access functions.
54 * expand ~user/foo to the /home/dir/of/user/foo
56 * expand {1,2}{a,b} to 1a 1b 2a 2b
58 * Number of matches in the current invocation of glob.
64 #include <sys/types.h>
79 #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
80 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) || \
81 !defined(HAVE_DECL_GLOB_NOMATCH) || HAVE_DECL_GLOB_NOMATCH == 0 || \
84 #include "charclass.h"
102 #define UNDERSCORE '_'
110 #define M_QUOTE 0x8000
111 #define M_PROTECT 0x4000
112 #define M_MASK 0xffff
113 #define M_ASCII 0x00ff
115 typedef u_short Char;
120 #define M_PROTECT 0x40
129 #define CHAR(c) ((Char)((c)&M_ASCII))
130 #define META(c) ((Char)((c)|M_QUOTE))
131 #define M_ALL META('*')
132 #define M_END META(']')
133 #define M_NOT META('!')
134 #define M_ONE META('?')
135 #define M_RNG META('-')
136 #define M_SET META('[')
137 #define M_CLASS META(':')
138 #define ismeta(c) (((c)&M_QUOTE) != 0)
140 #define GLOB_LIMIT_MALLOC 65536
141 #define GLOB_LIMIT_STAT 2048
142 #define GLOB_LIMIT_READDIR 16384
150 struct glob_path_stat {
152 struct stat *gps_stat;
155 static int compare(const void *, const void *);
156 static int compare_gps(const void *, const void *);
157 static int g_Ctoc(const Char *, char *, size_t);
158 static int g_lstat(Char *, struct stat *, glob_t *);
159 static DIR *g_opendir(Char *, glob_t *);
160 static Char *g_strchr(const Char *, int);
161 static int g_strncmp(const Char *, const char *, size_t);
162 static int g_stat(Char *, struct stat *, glob_t *);
163 static int glob0(const Char *, glob_t *, struct glob_lim *);
164 static int glob1(Char *, Char *, glob_t *, struct glob_lim *);
165 static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
166 glob_t *, struct glob_lim *);
167 static int glob3(Char *, Char *, Char *, Char *, Char *,
168 Char *, Char *, glob_t *, struct glob_lim *);
169 static int globextend(const Char *, glob_t *, struct glob_lim *,
172 globtilde(const Char *, Char *, size_t, glob_t *);
173 static int globexp1(const Char *, glob_t *, struct glob_lim *);
174 static int globexp2(const Char *, const Char *, glob_t *,
176 static int match(Char *, Char *, Char *);
178 static void qprintf(const char *, Char *);
182 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
185 const u_char *patnext;
187 Char *bufnext, *bufend, patbuf[PATH_MAX];
188 struct glob_lim limit = { 0, 0, 0 };
190 patnext = (u_char *) pattern;
191 if (!(flags & GLOB_APPEND)) {
193 pglob->gl_pathv = NULL;
194 pglob->gl_statv = NULL;
195 if (!(flags & GLOB_DOOFFS))
198 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
199 pglob->gl_errfunc = errfunc;
200 pglob->gl_matchc = 0;
202 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
203 return(GLOB_NOMATCH);
205 if (pglob->gl_offs >= SSIZE_MAX || pglob->gl_pathc >= SSIZE_MAX ||
206 pglob->gl_pathc >= SSIZE_MAX - pglob->gl_offs - 1)
210 bufend = bufnext + PATH_MAX - 1;
211 if (flags & GLOB_NOESCAPE)
212 while (bufnext < bufend && (c = *patnext++) != EOS)
215 /* Protect the quoted characters. */
216 while (bufnext < bufend && (c = *patnext++) != EOS)
218 if ((c = *patnext++) == EOS) {
222 *bufnext++ = c | M_PROTECT;
228 if (flags & GLOB_BRACE)
229 return globexp1(patbuf, pglob, &limit);
231 return glob0(patbuf, pglob, &limit);
235 * Expand recursively a glob {} pattern. When there is no more expansion
236 * invoke the standard globbing routine to glob the rest of the magic
240 globexp1(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
242 const Char* ptr = pattern;
244 /* Protect a single {}, for find(1), like csh */
245 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
246 return glob0(pattern, pglob, limitp);
248 if ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
249 return globexp2(ptr, pattern, pglob, limitp);
251 return glob0(pattern, pglob, limitp);
256 * Recursive brace globbing helper. Tries to expand a single brace.
257 * If it succeeds then it invokes globexp1 with the new pattern.
258 * If it fails then it tries to glob the rest of the pattern and returns.
261 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
262 struct glob_lim *limitp)
266 const Char *pe, *pm, *pl;
267 Char patbuf[PATH_MAX];
269 /* copy part up to the brace */
270 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
275 /* Find the balanced brace */
276 for (i = 0, pe = ++ptr; *pe; pe++)
277 if (*pe == LBRACKET) {
278 /* Ignore everything between [] */
279 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
283 * We could not find a matching RBRACKET.
284 * Ignore and just look for RBRACE
288 } else if (*pe == LBRACE)
290 else if (*pe == RBRACE) {
296 /* Non matching braces; just glob the pattern */
297 if (i != 0 || *pe == EOS)
298 return glob0(patbuf, pglob, limitp);
300 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
303 /* Ignore everything between [] */
304 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
308 * We could not find a matching RBRACKET.
309 * Ignore and just look for RBRACE
326 if (i && *pm == COMMA)
329 /* Append the current string */
330 for (lm = ls; (pl < pm); *lm++ = *pl++)
334 * Append the rest of the pattern after the
337 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
340 /* Expand the current pattern */
342 qprintf("globexp2:", patbuf);
344 rv = globexp1(patbuf, pglob, limitp);
345 if (rv && rv != GLOB_NOMATCH)
348 /* move after the comma, to the next string */
363 * expand tilde from the passwd file.
366 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
373 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
376 /* Copy up to the end of the string or / */
377 eb = &patbuf[patbuf_len - 1];
378 for (p = pattern + 1, h = (char *) patbuf;
379 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
389 if (((char *) patbuf)[0] == EOS) {
391 * handle a plain ~ or ~/ by expanding $HOME
392 * first and then trying the password file
395 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
397 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
398 if ((pwd = getpwuid(getuid())) == NULL)
407 if ((pwd = getpwnam((char*) patbuf)) == NULL)
413 /* Copy the home directory */
414 for (b = patbuf; b < eb && *h; *b++ = *h++)
417 /* Append the rest of the pattern */
418 while (b < eb && (*b++ = *p++) != EOS)
426 g_strncmp(const Char *s1, const char *s2, size_t n)
431 rv = *(Char *)s1 - *(const unsigned char *)s2++;
441 g_charclass(const Char **patternp, Char **bufnextp)
443 const Char *pattern = *patternp + 1;
444 Char *bufnext = *bufnextp;
449 if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']')
450 return 1; /* not a character class */
452 len = (size_t)(colon - pattern);
453 for (cc = cclasses; cc->name != NULL; cc++) {
454 if (!g_strncmp(pattern, cc->name, len) && cc->name[len] == '\0')
457 if (cc->name == NULL)
458 return -1; /* invalid character class */
459 *bufnext++ = M_CLASS;
460 *bufnext++ = (Char)(cc - &cclasses[0]);
462 *patternp += len + 3;
468 * The main glob() routine: compiles the pattern (optionally processing
469 * quotes), calls glob1() to do the real pattern matching, and finally
470 * sorts the list (unless unsorted operation is requested). Returns 0
471 * if things went well, nonzero if errors occurred. It is not an error
472 * to find no matches.
475 glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
477 const Char *qpatnext;
480 Char *bufnext, patbuf[PATH_MAX];
482 qpatnext = globtilde(pattern, patbuf, PATH_MAX, pglob);
483 oldpathc = pglob->gl_pathc;
486 /* We don't need to check for buffer overflow any more. */
487 while ((c = *qpatnext++) != EOS) {
493 if (*qpatnext == EOS ||
494 g_strchr(qpatnext+1, RBRACKET) == NULL) {
495 *bufnext++ = LBRACKET;
505 if (c == LBRACKET && *qpatnext == ':') {
507 err = g_charclass(&qpatnext,
512 } while (c == LBRACKET && *qpatnext == ':');
514 !(pglob->gl_flags & GLOB_NOCHECK))
519 *bufnext++ = CHAR(c);
520 if (*qpatnext == RANGE &&
521 (c = qpatnext[1]) != RBRACKET) {
523 *bufnext++ = CHAR(c);
526 } while ((c = *qpatnext++) != RBRACKET);
527 pglob->gl_flags |= GLOB_MAGCHAR;
531 pglob->gl_flags |= GLOB_MAGCHAR;
535 pglob->gl_flags |= GLOB_MAGCHAR;
536 /* collapse adjacent stars to one,
537 * to avoid exponential behavior
539 if (bufnext == patbuf || bufnext[-1] != M_ALL)
543 *bufnext++ = CHAR(c);
549 qprintf("glob0:", patbuf);
552 if ((err = glob1(patbuf, patbuf+PATH_MAX-1, pglob, limitp)) != 0)
556 * If there was no match we are going to append the pattern
557 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
558 * and the pattern did not contain any magic characters
559 * GLOB_NOMAGIC is there just for compatibility with csh.
561 if (pglob->gl_pathc == oldpathc) {
562 if ((pglob->gl_flags & GLOB_NOCHECK) ||
563 ((pglob->gl_flags & GLOB_NOMAGIC) &&
564 !(pglob->gl_flags & GLOB_MAGCHAR)))
565 return(globextend(pattern, pglob, limitp, NULL));
567 return(GLOB_NOMATCH);
569 if (!(pglob->gl_flags & GLOB_NOSORT)) {
570 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
571 /* Keep the paths and stat info synced during sort */
572 struct glob_path_stat *path_stat;
574 size_t n = pglob->gl_pathc - oldpathc;
575 size_t o = pglob->gl_offs + oldpathc;
577 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
579 for (i = 0; i < n; i++) {
580 path_stat[i].gps_path = pglob->gl_pathv[o + i];
581 path_stat[i].gps_stat = pglob->gl_statv[o + i];
583 qsort(path_stat, n, sizeof(*path_stat), compare_gps);
584 for (i = 0; i < n; i++) {
585 pglob->gl_pathv[o + i] = path_stat[i].gps_path;
586 pglob->gl_statv[o + i] = path_stat[i].gps_stat;
590 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
591 pglob->gl_pathc - oldpathc, sizeof(char *),
599 compare(const void *p, const void *q)
601 return(strcmp(*(char **)p, *(char **)q));
605 compare_gps(const void *_p, const void *_q)
607 const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
608 const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
610 return(strcmp(p->gps_path, q->gps_path));
614 glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
616 Char pathbuf[PATH_MAX];
618 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
621 return(glob2(pathbuf, pathbuf+PATH_MAX-1,
622 pathbuf, pathbuf+PATH_MAX-1,
623 pattern, pattern_last, pglob, limitp));
627 * The functions glob2 and glob3 are mutually recursive; there is one level
628 * of recursion for each segment in the pattern that contains one or more
632 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
633 Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
640 * Loop over pattern segments until end of pattern or until
641 * segment with meta character found.
643 for (anymeta = 0;;) {
644 if (*pattern == EOS) { /* End of pattern? */
647 if ((pglob->gl_flags & GLOB_LIMIT) &&
648 limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
652 return(GLOB_NOSPACE);
654 if (g_lstat(pathbuf, &sb, pglob))
657 if (((pglob->gl_flags & GLOB_MARK) &&
658 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
659 (S_ISLNK(sb.st_mode) &&
660 (g_stat(pathbuf, &sb, pglob) == 0) &&
661 S_ISDIR(sb.st_mode)))) {
662 if (pathend+1 > pathend_last)
668 return(globextend(pathbuf, pglob, limitp, &sb));
671 /* Find end of next segment, copy tentatively to pathend. */
674 while (*p != EOS && *p != SEP) {
677 if (q+1 > pathend_last)
682 if (!anymeta) { /* No expansion, do next segment. */
685 while (*pattern == SEP) {
686 if (pathend+1 > pathend_last)
688 *pathend++ = *pattern++;
691 /* Need expansion, recurse. */
692 return(glob3(pathbuf, pathbuf_last, pathend,
693 pathend_last, pattern, p, pattern_last,
700 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
701 Char *pattern, Char *restpattern, Char *restpattern_last, glob_t *pglob,
702 struct glob_lim *limitp)
710 * The readdirfunc declaration can't be prototyped, because it is
711 * assigned, below, to two functions which are prototyped in glob.h
712 * and dirent.h as taking pointers to differently typed opaque
715 struct dirent *(*readdirfunc)(void *);
717 if (pathend > pathend_last)
722 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
723 /* TODO: don't call for ENOENT or ENOTDIR? */
724 if (pglob->gl_errfunc) {
725 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
726 return(GLOB_ABORTED);
727 if (pglob->gl_errfunc(buf, errno) ||
728 pglob->gl_flags & GLOB_ERR)
729 return(GLOB_ABORTED);
736 /* Search directory for matching names. */
737 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
738 readdirfunc = pglob->gl_readdir;
740 readdirfunc = (struct dirent *(*)(void *))readdir;
741 while ((dp = (*readdirfunc)(dirp))) {
745 if ((pglob->gl_flags & GLOB_LIMIT) &&
746 limitp->glim_readdir++ >= GLOB_LIMIT_READDIR) {
754 /* Initial DOT must be matched literally. */
755 if (dp->d_name[0] == DOT && *pattern != DOT)
758 sc = (u_char *) dp->d_name;
759 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
761 if (dc >= pathend_last) {
767 if (!match(pathend, pattern, restpattern)) {
771 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
772 restpattern, restpattern_last, pglob, limitp);
777 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
778 (*pglob->gl_closedir)(dirp);
786 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
787 * add the new item, and update gl_pathc.
789 * This assumes the BSD realloc, which only copies the block when its size
790 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
793 * Return 0 if new item added, error code if memory couldn't be allocated.
795 * Invariant of the glob_t structure:
796 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
797 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
800 globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
809 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
810 if (pglob->gl_offs >= SSIZE_MAX ||
811 pglob->gl_pathc >= SSIZE_MAX ||
813 SIZE_MAX / sizeof(*pathv) <= newn ||
814 SIZE_MAX / sizeof(*statv) <= newn) {
816 for (i = pglob->gl_offs; i < newn - 2; i++) {
817 if (pglob->gl_pathv && pglob->gl_pathv[i])
818 free(pglob->gl_pathv[i]);
819 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
820 pglob->gl_pathv && pglob->gl_pathv[i])
821 free(pglob->gl_statv[i]);
823 free(pglob->gl_pathv);
824 pglob->gl_pathv = NULL;
825 free(pglob->gl_statv);
826 pglob->gl_statv = NULL;
827 return(GLOB_NOSPACE);
830 pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
833 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
834 /* first time around -- clear initial gl_offs items */
835 pathv += pglob->gl_offs;
836 for (i = pglob->gl_offs; i > 0; i--)
839 pglob->gl_pathv = pathv;
841 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
842 statv = reallocarray(pglob->gl_statv, newn, sizeof(*statv));
845 if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
846 /* first time around -- clear initial gl_offs items */
847 statv += pglob->gl_offs;
848 for (i = pglob->gl_offs; i > 0; i--)
851 pglob->gl_statv = statv;
853 statv[pglob->gl_offs + pglob->gl_pathc] = NULL;
855 limitp->glim_malloc += sizeof(**statv);
856 if ((pglob->gl_flags & GLOB_LIMIT) &&
857 limitp->glim_malloc >= GLOB_LIMIT_MALLOC) {
859 return(GLOB_NOSPACE);
861 if ((statv[pglob->gl_offs + pglob->gl_pathc] =
862 malloc(sizeof(**statv))) == NULL)
864 memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb,
867 statv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL;
870 for (p = path; *p++;)
872 len = (size_t)(p - path);
873 limitp->glim_malloc += len;
874 if ((copy = malloc(len)) != NULL) {
875 if (g_Ctoc(path, copy, len)) {
877 return(GLOB_NOSPACE);
879 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
881 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
883 if ((pglob->gl_flags & GLOB_LIMIT) &&
884 (newn * sizeof(*pathv)) + limitp->glim_malloc >
887 return(GLOB_NOSPACE);
890 return(copy == NULL ? GLOB_NOSPACE : 0);
895 * pattern matching function for filenames. Each occurrence of the *
896 * pattern causes an iteration.
898 * Note, this function differs from the original as per the discussion
899 * here: https://research.swtch.com/glob
901 * Basically we removed the recursion and made it use the algorithm
902 * from Russ Cox to not go quadratic on cases like a file called
903 * ("a" x 100) . "x" matched against a pattern like "a*a*a*a*a*a*a*y".
906 match(Char *name, Char *pat, Char *patend)
908 int ok, negate_range;
914 while (pat < patend) {
916 switch (c & M_MASK) {
918 while (pat < patend && (*pat & M_MASK) == M_ALL)
919 pat++; /* eat consecutive '*' */
933 if ((k = *name++) == EOS)
935 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
937 while (((c = *pat++) & M_MASK) != M_END) {
938 if ((c & M_MASK) == M_CLASS) {
939 Char idx = *pat & M_MASK;
940 if (idx < NCCLASSES &&
941 cclasses[idx].isctype(k))
945 if ((*pat & M_MASK) == M_RNG) {
946 if (c <= k && k <= pat[1])
952 if (ok == negate_range)
973 /* Free allocated data belonging to a glob_t structure. */
975 globfree(glob_t *pglob)
980 if (pglob->gl_pathv != NULL) {
981 pp = pglob->gl_pathv + pglob->gl_offs;
982 for (i = pglob->gl_pathc; i--; ++pp)
984 free(pglob->gl_pathv);
985 pglob->gl_pathv = NULL;
987 if (pglob->gl_statv != NULL) {
988 for (i = 0; i < pglob->gl_pathc; i++) {
989 free(pglob->gl_statv[i]);
991 free(pglob->gl_statv);
992 pglob->gl_statv = NULL;
997 g_opendir(Char *str, glob_t *pglob)
1002 strlcpy(buf, ".", sizeof buf);
1004 if (g_Ctoc(str, buf, sizeof(buf)))
1008 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1009 return((*pglob->gl_opendir)(buf));
1011 return(opendir(buf));
1015 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1019 if (g_Ctoc(fn, buf, sizeof(buf)))
1021 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1022 return((*pglob->gl_lstat)(buf, sb));
1023 return(lstat(buf, sb));
1027 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1031 if (g_Ctoc(fn, buf, sizeof(buf)))
1033 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1034 return((*pglob->gl_stat)(buf, sb));
1035 return(stat(buf, sb));
1039 g_strchr(const Char *str, int ch)
1043 return ((Char *)str);
1049 g_Ctoc(const Char *str, char *buf, size_t len)
1053 if ((*buf++ = *str++) == EOS)
1061 qprintf(const char *str, Char *s)
1065 (void)printf("%s:\n", str);
1066 for (p = s; *p; p++)
1067 (void)printf("%c", CHAR(*p));
1069 for (p = s; *p; p++)
1070 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1072 for (p = s; *p; p++)
1073 (void)printf("%c", ismeta(*p) ? '_' : ' ');
1078 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
1079 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */