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.
64 #include <sys/types.h>
76 #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
77 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) || \
78 !defined(HAVE_DECL_GLOB_NOMATCH) || HAVE_DECL_GLOB_NOMATCH == 0 || \
81 #include "charclass.h"
95 #define UNDERSCORE '_'
103 #define M_QUOTE 0x8000
104 #define M_PROTECT 0x4000
105 #define M_MASK 0xffff
106 #define M_ASCII 0x00ff
108 typedef u_short Char;
113 #define M_PROTECT 0x40
122 #define CHAR(c) ((Char)((c)&M_ASCII))
123 #define META(c) ((Char)((c)|M_QUOTE))
124 #define M_ALL META('*')
125 #define M_END META(']')
126 #define M_NOT META('!')
127 #define M_ONE META('?')
128 #define M_RNG META('-')
129 #define M_SET META('[')
130 #define M_CLASS META(':')
131 #define ismeta(c) (((c)&M_QUOTE) != 0)
133 #define GLOB_LIMIT_MALLOC 65536
134 #define GLOB_LIMIT_STAT 128
135 #define GLOB_LIMIT_READDIR 16384
137 /* Limit of recursion during matching attempts. */
138 #define GLOB_LIMIT_RECUR 64
146 struct glob_path_stat {
148 struct stat *gps_stat;
151 static int compare(const void *, const void *);
152 static int compare_gps(const void *, const void *);
153 static int g_Ctoc(const Char *, char *, u_int);
154 static int g_lstat(Char *, struct stat *, glob_t *);
155 static DIR *g_opendir(Char *, glob_t *);
156 static Char *g_strchr(const Char *, int);
157 static int g_strncmp(const Char *, const char *, size_t);
158 static int g_stat(Char *, struct stat *, glob_t *);
159 static int glob0(const Char *, glob_t *, struct glob_lim *);
160 static int glob1(Char *, Char *, glob_t *, struct glob_lim *);
161 static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
162 glob_t *, struct glob_lim *);
163 static int glob3(Char *, Char *, Char *, Char *, Char *,
164 Char *, Char *, glob_t *, struct glob_lim *);
165 static int globextend(const Char *, glob_t *, struct glob_lim *,
168 globtilde(const Char *, Char *, size_t, glob_t *);
169 static int globexp1(const Char *, glob_t *, struct glob_lim *);
170 static int globexp2(const Char *, const Char *, glob_t *,
172 static int match(Char *, Char *, Char *, int);
174 static void qprintf(const char *, Char *);
178 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
181 const u_char *patnext;
183 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
184 struct glob_lim limit = { 0, 0, 0 };
186 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
187 return(GLOB_NOMATCH);
189 patnext = (u_char *) pattern;
190 if (!(flags & GLOB_APPEND)) {
192 pglob->gl_pathv = NULL;
193 pglob->gl_statv = NULL;
194 if (!(flags & GLOB_DOOFFS))
197 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
198 pglob->gl_errfunc = errfunc;
199 pglob->gl_matchc = 0;
201 if (pglob->gl_offs < 0 || pglob->gl_pathc < 0 ||
202 pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX ||
203 pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1)
207 bufend = bufnext + MAXPATHLEN - 1;
208 if (flags & GLOB_NOESCAPE)
209 while (bufnext < bufend && (c = *patnext++) != EOS)
212 /* Protect the quoted characters. */
213 while (bufnext < bufend && (c = *patnext++) != EOS)
215 if ((c = *patnext++) == EOS) {
219 *bufnext++ = c | M_PROTECT;
225 if (flags & GLOB_BRACE)
226 return globexp1(patbuf, pglob, &limit);
228 return glob0(patbuf, pglob, &limit);
232 * Expand recursively a glob {} pattern. When there is no more expansion
233 * invoke the standard globbing routine to glob the rest of the magic
237 globexp1(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
239 const Char* ptr = pattern;
241 /* Protect a single {}, for find(1), like csh */
242 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
243 return glob0(pattern, pglob, limitp);
245 if ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
246 return globexp2(ptr, pattern, pglob, limitp);
248 return glob0(pattern, pglob, limitp);
253 * Recursive brace globbing helper. Tries to expand a single brace.
254 * If it succeeds then it invokes globexp1 with the new pattern.
255 * If it fails then it tries to glob the rest of the pattern and returns.
258 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
259 struct glob_lim *limitp)
263 const Char *pe, *pm, *pl;
264 Char patbuf[MAXPATHLEN];
266 /* copy part up to the brace */
267 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
272 /* Find the balanced brace */
273 for (i = 0, pe = ++ptr; *pe; pe++)
274 if (*pe == LBRACKET) {
275 /* Ignore everything between [] */
276 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
280 * We could not find a matching RBRACKET.
281 * Ignore and just look for RBRACE
285 } else if (*pe == LBRACE)
287 else if (*pe == RBRACE) {
293 /* Non matching braces; just glob the pattern */
294 if (i != 0 || *pe == EOS)
295 return glob0(patbuf, pglob, limitp);
297 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
300 /* Ignore everything between [] */
301 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
305 * We could not find a matching RBRACKET.
306 * Ignore and just look for RBRACE
323 if (i && *pm == COMMA)
326 /* Append the current string */
327 for (lm = ls; (pl < pm); *lm++ = *pl++)
331 * Append the rest of the pattern after the
334 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
337 /* Expand the current pattern */
339 qprintf("globexp2:", patbuf);
341 rv = globexp1(patbuf, pglob, limitp);
342 if (rv && rv != GLOB_NOMATCH)
345 /* move after the comma, to the next string */
360 * expand tilde from the passwd file.
363 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
370 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
373 /* Copy up to the end of the string or / */
374 eb = &patbuf[patbuf_len - 1];
375 for (p = pattern + 1, h = (char *) patbuf;
376 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
386 if (((char *) patbuf)[0] == EOS) {
388 * handle a plain ~ or ~/ by expanding $HOME
389 * first and then trying the password file
392 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
394 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
395 if ((pwd = getpwuid(getuid())) == NULL)
404 if ((pwd = getpwnam((char*) patbuf)) == NULL)
410 /* Copy the home directory */
411 for (b = patbuf; b < eb && *h; *b++ = *h++)
414 /* Append the rest of the pattern */
415 while (b < eb && (*b++ = *p++) != EOS)
423 g_strncmp(const Char *s1, const char *s2, size_t n)
428 rv = *(Char *)s1 - *(const unsigned char *)s2++;
438 g_charclass(const Char **patternp, Char **bufnextp)
440 const Char *pattern = *patternp + 1;
441 Char *bufnext = *bufnextp;
446 if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']')
447 return 1; /* not a character class */
449 len = (size_t)(colon - pattern);
450 for (cc = cclasses; cc->name != NULL; cc++) {
451 if (!g_strncmp(pattern, cc->name, len) && cc->name[len] == '\0')
454 if (cc->name == NULL)
455 return -1; /* invalid character class */
456 *bufnext++ = M_CLASS;
457 *bufnext++ = (Char)(cc - &cclasses[0]);
459 *patternp += len + 3;
465 * The main glob() routine: compiles the pattern (optionally processing
466 * quotes), calls glob1() to do the real pattern matching, and finally
467 * sorts the list (unless unsorted operation is requested). Returns 0
468 * if things went well, nonzero if errors occurred. It is not an error
469 * to find no matches.
472 glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
474 const Char *qpatnext;
475 int c, err, oldpathc;
476 Char *bufnext, patbuf[MAXPATHLEN];
478 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
479 oldpathc = pglob->gl_pathc;
482 /* We don't need to check for buffer overflow any more. */
483 while ((c = *qpatnext++) != EOS) {
489 if (*qpatnext == EOS ||
490 g_strchr(qpatnext+1, RBRACKET) == NULL) {
491 *bufnext++ = LBRACKET;
501 if (c == LBRACKET && *qpatnext == ':') {
503 err = g_charclass(&qpatnext,
508 } while (c == LBRACKET && *qpatnext == ':');
510 !(pglob->gl_flags & GLOB_NOCHECK))
515 *bufnext++ = CHAR(c);
516 if (*qpatnext == RANGE &&
517 (c = qpatnext[1]) != RBRACKET) {
519 *bufnext++ = CHAR(c);
522 } while ((c = *qpatnext++) != RBRACKET);
523 pglob->gl_flags |= GLOB_MAGCHAR;
527 pglob->gl_flags |= GLOB_MAGCHAR;
531 pglob->gl_flags |= GLOB_MAGCHAR;
532 /* collapse adjacent stars to one,
533 * to avoid exponential behavior
535 if (bufnext == patbuf || bufnext[-1] != M_ALL)
539 *bufnext++ = CHAR(c);
545 qprintf("glob0:", patbuf);
548 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limitp)) != 0)
552 * If there was no match we are going to append the pattern
553 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
554 * and the pattern did not contain any magic characters
555 * GLOB_NOMAGIC is there just for compatibility with csh.
557 if (pglob->gl_pathc == oldpathc) {
558 if ((pglob->gl_flags & GLOB_NOCHECK) ||
559 ((pglob->gl_flags & GLOB_NOMAGIC) &&
560 !(pglob->gl_flags & GLOB_MAGCHAR)))
561 return(globextend(pattern, pglob, limitp, NULL));
563 return(GLOB_NOMATCH);
565 if (!(pglob->gl_flags & GLOB_NOSORT)) {
566 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
567 /* Keep the paths and stat info synced during sort */
568 struct glob_path_stat *path_stat;
570 int n = pglob->gl_pathc - oldpathc;
571 int o = pglob->gl_offs + oldpathc;
573 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
575 for (i = 0; i < n; i++) {
576 path_stat[i].gps_path = pglob->gl_pathv[o + i];
577 path_stat[i].gps_stat = pglob->gl_statv[o + i];
579 qsort(path_stat, n, sizeof(*path_stat), compare_gps);
580 for (i = 0; i < n; i++) {
581 pglob->gl_pathv[o + i] = path_stat[i].gps_path;
582 pglob->gl_statv[o + i] = path_stat[i].gps_stat;
586 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
587 pglob->gl_pathc - oldpathc, sizeof(char *),
595 compare(const void *p, const void *q)
597 return(strcmp(*(char **)p, *(char **)q));
601 compare_gps(const void *_p, const void *_q)
603 const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
604 const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
606 return(strcmp(p->gps_path, q->gps_path));
610 glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
612 Char pathbuf[MAXPATHLEN];
614 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
617 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
618 pathbuf, pathbuf+MAXPATHLEN-1,
619 pattern, pattern_last, pglob, limitp));
623 * The functions glob2 and glob3 are mutually recursive; there is one level
624 * of recursion for each segment in the pattern that contains one or more
628 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
629 Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
636 * Loop over pattern segments until end of pattern or until
637 * segment with meta character found.
639 for (anymeta = 0;;) {
640 if (*pattern == EOS) { /* End of pattern? */
642 if (g_lstat(pathbuf, &sb, pglob))
645 if ((pglob->gl_flags & GLOB_LIMIT) &&
646 limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
650 return(GLOB_NOSPACE);
653 if (((pglob->gl_flags & GLOB_MARK) &&
654 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
655 (S_ISLNK(sb.st_mode) &&
656 (g_stat(pathbuf, &sb, pglob) == 0) &&
657 S_ISDIR(sb.st_mode)))) {
658 if (pathend+1 > pathend_last)
664 return(globextend(pathbuf, pglob, limitp, &sb));
667 /* Find end of next segment, copy tentatively to pathend. */
670 while (*p != EOS && *p != SEP) {
673 if (q+1 > pathend_last)
678 if (!anymeta) { /* No expansion, do next segment. */
681 while (*pattern == SEP) {
682 if (pathend+1 > pathend_last)
684 *pathend++ = *pattern++;
687 /* Need expansion, recurse. */
688 return(glob3(pathbuf, pathbuf_last, pathend,
689 pathend_last, pattern, p, pattern_last,
696 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
697 Char *pattern, Char *restpattern, Char *restpattern_last, glob_t *pglob,
698 struct glob_lim *limitp)
703 char buf[MAXPATHLEN];
706 * The readdirfunc declaration can't be prototyped, because it is
707 * assigned, below, to two functions which are prototyped in glob.h
708 * and dirent.h as taking pointers to differently typed opaque
711 struct dirent *(*readdirfunc)(void *);
713 if (pathend > pathend_last)
718 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
719 /* TODO: don't call for ENOENT or ENOTDIR? */
720 if (pglob->gl_errfunc) {
721 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
722 return(GLOB_ABORTED);
723 if (pglob->gl_errfunc(buf, errno) ||
724 pglob->gl_flags & GLOB_ERR)
725 return(GLOB_ABORTED);
732 /* Search directory for matching names. */
733 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
734 readdirfunc = pglob->gl_readdir;
736 readdirfunc = (struct dirent *(*)(void *))readdir;
737 while ((dp = (*readdirfunc)(dirp))) {
741 if ((pglob->gl_flags & GLOB_LIMIT) &&
742 limitp->glim_readdir++ >= GLOB_LIMIT_READDIR) {
750 /* Initial DOT must be matched literally. */
751 if (dp->d_name[0] == DOT && *pattern != DOT)
754 sc = (u_char *) dp->d_name;
755 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
757 if (dc >= pathend_last) {
763 if (!match(pathend, pattern, restpattern, GLOB_LIMIT_RECUR)) {
767 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
768 restpattern, restpattern_last, pglob, limitp);
773 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
774 (*pglob->gl_closedir)(dirp);
782 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
783 * add the new item, and update gl_pathc.
785 * This assumes the BSD realloc, which only copies the block when its size
786 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
789 * Return 0 if new item added, error code if memory couldn't be allocated.
791 * Invariant of the glob_t structure:
792 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
793 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
796 globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
806 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
807 if (pglob->gl_offs >= INT_MAX ||
808 pglob->gl_pathc >= INT_MAX ||
810 SIZE_MAX / sizeof(*pathv) <= newn ||
811 SIZE_MAX / sizeof(*statv) <= newn) {
813 for (i = pglob->gl_offs; i < (ssize_t)(newn - 2); i++) {
814 if (pglob->gl_pathv && pglob->gl_pathv[i])
815 free(pglob->gl_pathv[i]);
816 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
817 pglob->gl_pathv && pglob->gl_pathv[i])
818 free(pglob->gl_statv[i]);
820 if (pglob->gl_pathv) {
821 free(pglob->gl_pathv);
822 pglob->gl_pathv = NULL;
824 if (pglob->gl_statv) {
825 free(pglob->gl_statv);
826 pglob->gl_statv = NULL;
828 return(GLOB_NOSPACE);
831 pathv = realloc(pglob->gl_pathv, newn * sizeof(*pathv));
834 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
835 /* first time around -- clear initial gl_offs items */
836 pathv += pglob->gl_offs;
837 for (i = pglob->gl_offs; --i >= 0; )
840 pglob->gl_pathv = pathv;
842 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
843 statv = realloc(pglob->gl_statv, newn * sizeof(*statv));
846 if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
847 /* first time around -- clear initial gl_offs items */
848 statv += pglob->gl_offs;
849 for (i = pglob->gl_offs; --i >= 0; )
852 pglob->gl_statv = statv;
854 statv[pglob->gl_offs + pglob->gl_pathc] = NULL;
856 limitp->glim_malloc += sizeof(**statv);
857 if ((pglob->gl_flags & GLOB_LIMIT) &&
858 limitp->glim_malloc >= GLOB_LIMIT_MALLOC) {
860 return(GLOB_NOSPACE);
862 if ((statv[pglob->gl_offs + pglob->gl_pathc] =
863 malloc(sizeof(**statv))) == NULL)
865 memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb,
868 statv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL;
871 for (p = path; *p++;)
873 len = (size_t)(p - path);
874 limitp->glim_malloc += len;
875 if ((copy = malloc(len)) != NULL) {
876 if (g_Ctoc(path, copy, len)) {
878 return(GLOB_NOSPACE);
880 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
882 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
884 if ((pglob->gl_flags & GLOB_LIMIT) &&
885 (newn * sizeof(*pathv)) + limitp->glim_malloc >
888 return(GLOB_NOSPACE);
891 return(copy == NULL ? GLOB_NOSPACE : 0);
896 * pattern matching function for filenames. Each occurrence of the *
897 * pattern causes a recursion level.
900 match(Char *name, Char *pat, Char *patend, int recur)
902 int ok, negate_range;
906 return(GLOB_NOSPACE);
908 while (pat < patend) {
910 switch (c & M_MASK) {
912 while (pat < patend && (*pat & M_MASK) == M_ALL)
913 pat++; /* eat consecutive '*' */
917 if (match(name, pat, patend, recur))
919 } while (*name++ != EOS);
927 if ((k = *name++) == EOS)
929 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
931 while (((c = *pat++) & M_MASK) != M_END) {
932 if ((c & M_MASK) == M_CLASS) {
933 Char idx = *pat & M_MASK;
934 if (idx < NCCLASSES &&
935 cclasses[idx].isctype(k))
939 if ((*pat & M_MASK) == M_RNG) {
940 if (c <= k && k <= pat[1])
946 if (ok == negate_range)
955 return(*name == EOS);
958 /* Free allocated data belonging to a glob_t structure. */
960 globfree(glob_t *pglob)
965 if (pglob->gl_pathv != NULL) {
966 pp = pglob->gl_pathv + pglob->gl_offs;
967 for (i = pglob->gl_pathc; i--; ++pp)
970 free(pglob->gl_pathv);
971 pglob->gl_pathv = NULL;
973 if (pglob->gl_statv != NULL) {
974 for (i = 0; i < pglob->gl_pathc; i++) {
975 if (pglob->gl_statv[i] != NULL)
976 free(pglob->gl_statv[i]);
978 free(pglob->gl_statv);
979 pglob->gl_statv = NULL;
984 g_opendir(Char *str, glob_t *pglob)
986 char buf[MAXPATHLEN];
989 strlcpy(buf, ".", sizeof buf);
991 if (g_Ctoc(str, buf, sizeof(buf)))
995 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
996 return((*pglob->gl_opendir)(buf));
998 return(opendir(buf));
1002 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1004 char buf[MAXPATHLEN];
1006 if (g_Ctoc(fn, buf, sizeof(buf)))
1008 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1009 return((*pglob->gl_lstat)(buf, sb));
1010 return(lstat(buf, sb));
1014 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1016 char buf[MAXPATHLEN];
1018 if (g_Ctoc(fn, buf, sizeof(buf)))
1020 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1021 return((*pglob->gl_stat)(buf, sb));
1022 return(stat(buf, sb));
1026 g_strchr(const Char *str, int ch)
1030 return ((Char *)str);
1036 g_Ctoc(const Char *str, char *buf, u_int len)
1040 if ((*buf++ = *str++) == EOS)
1048 qprintf(const char *str, Char *s)
1052 (void)printf("%s:\n", str);
1053 for (p = s; *p; p++)
1054 (void)printf("%c", CHAR(*p));
1056 for (p = s; *p; p++)
1057 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1059 for (p = s; *p; p++)
1060 (void)printf("%c", ismeta(*p) ? '_' : ' ');
1065 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
1066 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */