1 /* $OpenBSD: glob.c,v 1.38 2011/09/22 06:27:29 djm 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.
63 #include <sys/types.h>
75 #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
76 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) || \
77 !defined(HAVE_DECL_GLOB_NOMATCH) || HAVE_DECL_GLOB_NOMATCH == 0 || \
80 #include "charclass.h"
94 #define UNDERSCORE '_'
102 #define M_QUOTE 0x8000
103 #define M_PROTECT 0x4000
104 #define M_MASK 0xffff
105 #define M_ASCII 0x00ff
107 typedef u_short Char;
112 #define M_PROTECT 0x40
121 #define CHAR(c) ((Char)((c)&M_ASCII))
122 #define META(c) ((Char)((c)|M_QUOTE))
123 #define M_ALL META('*')
124 #define M_END META(']')
125 #define M_NOT META('!')
126 #define M_ONE META('?')
127 #define M_RNG META('-')
128 #define M_SET META('[')
129 #define M_CLASS META(':')
130 #define ismeta(c) (((c)&M_QUOTE) != 0)
132 #define GLOB_LIMIT_MALLOC 65536
133 #define GLOB_LIMIT_STAT 128
134 #define GLOB_LIMIT_READDIR 16384
136 /* Limit of recursion during matching attempts. */
137 #define GLOB_LIMIT_RECUR 64
145 struct glob_path_stat {
147 struct stat *gps_stat;
150 static int compare(const void *, const void *);
151 static int compare_gps(const void *, const void *);
152 static int g_Ctoc(const Char *, char *, u_int);
153 static int g_lstat(Char *, struct stat *, glob_t *);
154 static DIR *g_opendir(Char *, glob_t *);
155 static Char *g_strchr(const Char *, int);
156 static int g_strncmp(const Char *, const char *, size_t);
157 static int g_stat(Char *, struct stat *, glob_t *);
158 static int glob0(const Char *, glob_t *, struct glob_lim *);
159 static int glob1(Char *, Char *, glob_t *, struct glob_lim *);
160 static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
161 glob_t *, struct glob_lim *);
162 static int glob3(Char *, Char *, Char *, Char *, Char *,
163 Char *, Char *, glob_t *, struct glob_lim *);
164 static int globextend(const Char *, glob_t *, struct glob_lim *,
167 globtilde(const Char *, Char *, size_t, glob_t *);
168 static int globexp1(const Char *, glob_t *, struct glob_lim *);
169 static int globexp2(const Char *, const Char *, glob_t *,
171 static int match(Char *, Char *, Char *, int);
173 static void qprintf(const char *, Char *);
177 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
180 const u_char *patnext;
182 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
183 struct glob_lim limit = { 0, 0, 0 };
185 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
186 return(GLOB_NOMATCH);
188 patnext = (u_char *) pattern;
189 if (!(flags & GLOB_APPEND)) {
191 pglob->gl_pathv = NULL;
192 pglob->gl_statv = NULL;
193 if (!(flags & GLOB_DOOFFS))
196 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
197 pglob->gl_errfunc = errfunc;
198 pglob->gl_matchc = 0;
200 if (pglob->gl_offs < 0 || pglob->gl_pathc < 0 ||
201 pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX ||
202 pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1)
206 bufend = bufnext + MAXPATHLEN - 1;
207 if (flags & GLOB_NOESCAPE)
208 while (bufnext < bufend && (c = *patnext++) != EOS)
211 /* Protect the quoted characters. */
212 while (bufnext < bufend && (c = *patnext++) != EOS)
214 if ((c = *patnext++) == EOS) {
218 *bufnext++ = c | M_PROTECT;
224 if (flags & GLOB_BRACE)
225 return globexp1(patbuf, pglob, &limit);
227 return glob0(patbuf, pglob, &limit);
231 * Expand recursively a glob {} pattern. When there is no more expansion
232 * invoke the standard globbing routine to glob the rest of the magic
236 globexp1(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
238 const Char* ptr = pattern;
240 /* Protect a single {}, for find(1), like csh */
241 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
242 return glob0(pattern, pglob, limitp);
244 if ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
245 return globexp2(ptr, pattern, pglob, limitp);
247 return glob0(pattern, pglob, limitp);
252 * Recursive brace globbing helper. Tries to expand a single brace.
253 * If it succeeds then it invokes globexp1 with the new pattern.
254 * If it fails then it tries to glob the rest of the pattern and returns.
257 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
258 struct glob_lim *limitp)
262 const Char *pe, *pm, *pl;
263 Char patbuf[MAXPATHLEN];
265 /* copy part up to the brace */
266 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
271 /* Find the balanced brace */
272 for (i = 0, pe = ++ptr; *pe; pe++)
273 if (*pe == LBRACKET) {
274 /* Ignore everything between [] */
275 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
279 * We could not find a matching RBRACKET.
280 * Ignore and just look for RBRACE
284 } else if (*pe == LBRACE)
286 else if (*pe == RBRACE) {
292 /* Non matching braces; just glob the pattern */
293 if (i != 0 || *pe == EOS)
294 return glob0(patbuf, pglob, limitp);
296 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
299 /* Ignore everything between [] */
300 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
304 * We could not find a matching RBRACKET.
305 * Ignore and just look for RBRACE
322 if (i && *pm == COMMA)
325 /* Append the current string */
326 for (lm = ls; (pl < pm); *lm++ = *pl++)
330 * Append the rest of the pattern after the
333 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
336 /* Expand the current pattern */
338 qprintf("globexp2:", patbuf);
340 rv = globexp1(patbuf, pglob, limitp);
341 if (rv && rv != GLOB_NOMATCH)
344 /* move after the comma, to the next string */
359 * expand tilde from the passwd file.
362 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
369 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
372 /* Copy up to the end of the string or / */
373 eb = &patbuf[patbuf_len - 1];
374 for (p = pattern + 1, h = (char *) patbuf;
375 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
385 if (((char *) patbuf)[0] == EOS) {
387 * handle a plain ~ or ~/ by expanding $HOME
388 * first and then trying the password file
391 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
393 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
394 if ((pwd = getpwuid(getuid())) == NULL)
403 if ((pwd = getpwnam((char*) patbuf)) == NULL)
409 /* Copy the home directory */
410 for (b = patbuf; b < eb && *h; *b++ = *h++)
413 /* Append the rest of the pattern */
414 while (b < eb && (*b++ = *p++) != EOS)
422 g_strncmp(const Char *s1, const char *s2, size_t n)
427 rv = *(Char *)s1 - *(const unsigned char *)s2++;
437 g_charclass(const Char **patternp, Char **bufnextp)
439 const Char *pattern = *patternp + 1;
440 Char *bufnext = *bufnextp;
445 if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']')
446 return 1; /* not a character class */
448 len = (size_t)(colon - pattern);
449 for (cc = cclasses; cc->name != NULL; cc++) {
450 if (!g_strncmp(pattern, cc->name, len) && cc->name[len] == '\0')
453 if (cc->name == NULL)
454 return -1; /* invalid character class */
455 *bufnext++ = M_CLASS;
456 *bufnext++ = (Char)(cc - &cclasses[0]);
458 *patternp += len + 3;
464 * The main glob() routine: compiles the pattern (optionally processing
465 * quotes), calls glob1() to do the real pattern matching, and finally
466 * sorts the list (unless unsorted operation is requested). Returns 0
467 * if things went well, nonzero if errors occurred. It is not an error
468 * to find no matches.
471 glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
473 const Char *qpatnext;
474 int c, err, oldpathc;
475 Char *bufnext, patbuf[MAXPATHLEN];
477 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
478 oldpathc = pglob->gl_pathc;
481 /* We don't need to check for buffer overflow any more. */
482 while ((c = *qpatnext++) != EOS) {
488 if (*qpatnext == EOS ||
489 g_strchr(qpatnext+1, RBRACKET) == NULL) {
490 *bufnext++ = LBRACKET;
500 if (c == LBRACKET && *qpatnext == ':') {
502 err = g_charclass(&qpatnext,
507 } while (c == LBRACKET && *qpatnext == ':');
509 !(pglob->gl_flags & GLOB_NOCHECK))
514 *bufnext++ = CHAR(c);
515 if (*qpatnext == RANGE &&
516 (c = qpatnext[1]) != RBRACKET) {
518 *bufnext++ = CHAR(c);
521 } while ((c = *qpatnext++) != RBRACKET);
522 pglob->gl_flags |= GLOB_MAGCHAR;
526 pglob->gl_flags |= GLOB_MAGCHAR;
530 pglob->gl_flags |= GLOB_MAGCHAR;
531 /* collapse adjacent stars to one,
532 * to avoid exponential behavior
534 if (bufnext == patbuf || bufnext[-1] != M_ALL)
538 *bufnext++ = CHAR(c);
544 qprintf("glob0:", patbuf);
547 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limitp)) != 0)
551 * If there was no match we are going to append the pattern
552 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
553 * and the pattern did not contain any magic characters
554 * GLOB_NOMAGIC is there just for compatibility with csh.
556 if (pglob->gl_pathc == oldpathc) {
557 if ((pglob->gl_flags & GLOB_NOCHECK) ||
558 ((pglob->gl_flags & GLOB_NOMAGIC) &&
559 !(pglob->gl_flags & GLOB_MAGCHAR)))
560 return(globextend(pattern, pglob, limitp, NULL));
562 return(GLOB_NOMATCH);
564 if (!(pglob->gl_flags & GLOB_NOSORT)) {
565 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
566 /* Keep the paths and stat info synced during sort */
567 struct glob_path_stat *path_stat;
569 int n = pglob->gl_pathc - oldpathc;
570 int o = pglob->gl_offs + oldpathc;
572 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
574 for (i = 0; i < n; i++) {
575 path_stat[i].gps_path = pglob->gl_pathv[o + i];
576 path_stat[i].gps_stat = pglob->gl_statv[o + i];
578 qsort(path_stat, n, sizeof(*path_stat), compare_gps);
579 for (i = 0; i < n; i++) {
580 pglob->gl_pathv[o + i] = path_stat[i].gps_path;
581 pglob->gl_statv[o + i] = path_stat[i].gps_stat;
585 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
586 pglob->gl_pathc - oldpathc, sizeof(char *),
594 compare(const void *p, const void *q)
596 return(strcmp(*(char **)p, *(char **)q));
600 compare_gps(const void *_p, const void *_q)
602 const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
603 const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
605 return(strcmp(p->gps_path, q->gps_path));
609 glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
611 Char pathbuf[MAXPATHLEN];
613 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
616 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
617 pathbuf, pathbuf+MAXPATHLEN-1,
618 pattern, pattern_last, pglob, limitp));
622 * The functions glob2 and glob3 are mutually recursive; there is one level
623 * of recursion for each segment in the pattern that contains one or more
627 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
628 Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
635 * Loop over pattern segments until end of pattern or until
636 * segment with meta character found.
638 for (anymeta = 0;;) {
639 if (*pattern == EOS) { /* End of pattern? */
641 if (g_lstat(pathbuf, &sb, pglob))
644 if ((pglob->gl_flags & GLOB_LIMIT) &&
645 limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
649 return(GLOB_NOSPACE);
652 if (((pglob->gl_flags & GLOB_MARK) &&
653 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
654 (S_ISLNK(sb.st_mode) &&
655 (g_stat(pathbuf, &sb, pglob) == 0) &&
656 S_ISDIR(sb.st_mode)))) {
657 if (pathend+1 > pathend_last)
663 return(globextend(pathbuf, pglob, limitp, &sb));
666 /* Find end of next segment, copy tentatively to pathend. */
669 while (*p != EOS && *p != SEP) {
672 if (q+1 > pathend_last)
677 if (!anymeta) { /* No expansion, do next segment. */
680 while (*pattern == SEP) {
681 if (pathend+1 > pathend_last)
683 *pathend++ = *pattern++;
686 /* Need expansion, recurse. */
687 return(glob3(pathbuf, pathbuf_last, pathend,
688 pathend_last, pattern, p, pattern_last,
695 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
696 Char *pattern, Char *restpattern, Char *restpattern_last, glob_t *pglob,
697 struct glob_lim *limitp)
702 char buf[MAXPATHLEN];
705 * The readdirfunc declaration can't be prototyped, because it is
706 * assigned, below, to two functions which are prototyped in glob.h
707 * and dirent.h as taking pointers to differently typed opaque
710 struct dirent *(*readdirfunc)(void *);
712 if (pathend > pathend_last)
717 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
718 /* TODO: don't call for ENOENT or ENOTDIR? */
719 if (pglob->gl_errfunc) {
720 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
721 return(GLOB_ABORTED);
722 if (pglob->gl_errfunc(buf, errno) ||
723 pglob->gl_flags & GLOB_ERR)
724 return(GLOB_ABORTED);
731 /* Search directory for matching names. */
732 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
733 readdirfunc = pglob->gl_readdir;
735 readdirfunc = (struct dirent *(*)(void *))readdir;
736 while ((dp = (*readdirfunc)(dirp))) {
740 if ((pglob->gl_flags & GLOB_LIMIT) &&
741 limitp->glim_readdir++ >= GLOB_LIMIT_READDIR) {
749 /* Initial DOT must be matched literally. */
750 if (dp->d_name[0] == DOT && *pattern != DOT)
753 sc = (u_char *) dp->d_name;
754 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
756 if (dc >= pathend_last) {
762 if (!match(pathend, pattern, restpattern, GLOB_LIMIT_RECUR)) {
766 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
767 restpattern, restpattern_last, pglob, limitp);
772 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
773 (*pglob->gl_closedir)(dirp);
781 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
782 * add the new item, and update gl_pathc.
784 * This assumes the BSD realloc, which only copies the block when its size
785 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
788 * Return 0 if new item added, error code if memory couldn't be allocated.
790 * Invariant of the glob_t structure:
791 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
792 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
795 globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
805 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
806 if (pglob->gl_offs >= INT_MAX ||
807 pglob->gl_pathc >= INT_MAX ||
809 SIZE_MAX / sizeof(*pathv) <= newn ||
810 SIZE_MAX / sizeof(*statv) <= newn) {
812 for (i = pglob->gl_offs; i < (ssize_t)(newn - 2); i++) {
813 if (pglob->gl_pathv && pglob->gl_pathv[i])
814 free(pglob->gl_pathv[i]);
815 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
816 pglob->gl_pathv && pglob->gl_pathv[i])
817 free(pglob->gl_statv[i]);
819 if (pglob->gl_pathv) {
820 free(pglob->gl_pathv);
821 pglob->gl_pathv = NULL;
823 if (pglob->gl_statv) {
824 free(pglob->gl_statv);
825 pglob->gl_statv = NULL;
827 return(GLOB_NOSPACE);
830 pathv = realloc(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; )
839 pglob->gl_pathv = pathv;
841 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
842 statv = realloc(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; )
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 a recursion level.
899 match(Char *name, Char *pat, Char *patend, int recur)
901 int ok, negate_range;
905 return(GLOB_NOSPACE);
907 while (pat < patend) {
909 switch (c & M_MASK) {
911 while (pat < patend && (*pat & M_MASK) == M_ALL)
912 pat++; /* eat consecutive '*' */
916 if (match(name, pat, patend, recur))
918 } while (*name++ != EOS);
926 if ((k = *name++) == EOS)
928 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
930 while (((c = *pat++) & M_MASK) != M_END) {
931 if ((c & M_MASK) == M_CLASS) {
932 Char idx = *pat & M_MASK;
933 if (idx < NCCLASSES &&
934 cclasses[idx].isctype(k))
938 if ((*pat & M_MASK) == M_RNG) {
939 if (c <= k && k <= pat[1])
945 if (ok == negate_range)
954 return(*name == EOS);
957 /* Free allocated data belonging to a glob_t structure. */
959 globfree(glob_t *pglob)
964 if (pglob->gl_pathv != NULL) {
965 pp = pglob->gl_pathv + pglob->gl_offs;
966 for (i = pglob->gl_pathc; i--; ++pp)
969 free(pglob->gl_pathv);
970 pglob->gl_pathv = NULL;
972 if (pglob->gl_statv != NULL) {
973 for (i = 0; i < pglob->gl_pathc; i++) {
974 if (pglob->gl_statv[i] != NULL)
975 free(pglob->gl_statv[i]);
977 free(pglob->gl_statv);
978 pglob->gl_statv = NULL;
983 g_opendir(Char *str, glob_t *pglob)
985 char buf[MAXPATHLEN];
988 strlcpy(buf, ".", sizeof buf);
990 if (g_Ctoc(str, buf, sizeof(buf)))
994 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
995 return((*pglob->gl_opendir)(buf));
997 return(opendir(buf));
1001 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1003 char buf[MAXPATHLEN];
1005 if (g_Ctoc(fn, buf, sizeof(buf)))
1007 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1008 return((*pglob->gl_lstat)(buf, sb));
1009 return(lstat(buf, sb));
1013 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1015 char buf[MAXPATHLEN];
1017 if (g_Ctoc(fn, buf, sizeof(buf)))
1019 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1020 return((*pglob->gl_stat)(buf, sb));
1021 return(stat(buf, sb));
1025 g_strchr(const Char *str, int ch)
1029 return ((Char *)str);
1035 g_Ctoc(const Char *str, char *buf, u_int len)
1039 if ((*buf++ = *str++) == EOS)
1047 qprintf(const char *str, Char *s)
1051 (void)printf("%s:\n", str);
1052 for (p = s; *p; p++)
1053 (void)printf("%c", CHAR(*p));
1055 for (p = s; *p; p++)
1056 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1058 for (p = s; *p; p++)
1059 (void)printf("%c", ismeta(*p) ? '_' : ' ');
1064 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
1065 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */