1 /* $NetBSD: glob.c,v 1.35 2013/03/20 23:44:47 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
40 __RCSID("$NetBSD: glob.c,v 1.35 2013/03/20 23:44:47 lukem Exp $");
42 #endif /* LIBC_SCCS and not lint */
45 * glob(3) -- a superset of the one defined in POSIX 1003.2.
47 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
49 * Optional extra services, controlled by flags not defined by POSIX:
52 * Set in gl_flags if pattern contained a globbing character.
54 * Same as GLOB_NOCHECK, but it will only append pattern if it did
55 * not contain any magic characters. [Used in csh style globbing]
57 * Use alternately specified directory access functions.
59 * expand ~user/foo to the /home/dir/of/user/foo
61 * expand {1,2}{a,b} to 1a 1b 2a 2b
63 * allow metacharacters to match leading dots in filenames.
65 * . and .. are hidden from wildcards, even if GLOB_PERIOD is set.
67 * Number of matches in the current invocation of glob.
70 //#include "namespace.h"
71 #include <sys/param.h>
86 #ifdef HAVE_NBTOOL_CONFIG_H
90 #define GLOB_LIMIT_STRING 65536 /* number of readdirs */
91 #define GLOB_LIMIT_STAT 128 /* number of stat system calls */
92 #define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */
93 #define GLOB_LIMIT_PATH 1024 /* number of path elements */
94 #define GLOB_LIMIT_BRACE 128 /* Number of brace calls */
104 * XXX: For NetBSD 1.4.x compatibility. (kill me l8r)
107 #define _DIAGASSERT(a)
122 #define UNDERSCORE '_'
128 #ifndef USE_8BIT_CHARS
130 #define M_QUOTE 0x8000
131 #define M_PROTECT 0x4000
132 #define M_MASK 0xffff
133 #define M_ASCII 0x00ff
135 typedef unsigned short Char;
139 #define M_QUOTE (Char)0x80
140 #define M_PROTECT (Char)0x40
141 #define M_MASK (Char)0xff
142 #define M_ASCII (Char)0x7f
149 #define CHAR(c) ((Char)((c)&M_ASCII))
150 #define META(c) ((Char)((c)|M_QUOTE))
151 #define M_ALL META('*')
152 #define M_END META(']')
153 #define M_NOT META('!')
154 #define M_ONE META('?')
155 #define M_RNG META('-')
156 #define M_SET META('[')
157 #define ismeta(c) (((c)&M_QUOTE) != 0)
160 static int compare(const void *, const void *);
161 static int g_Ctoc(const Char *, char *, size_t);
162 static int g_lstat(Char *, __gl_stat_t *, glob_t *);
163 static DIR *g_opendir(Char *, glob_t *);
164 static Char *g_strchr(const Char *, int);
165 static int g_stat(Char *, __gl_stat_t *, glob_t *);
166 static int glob0(const Char *, glob_t *, struct glob_limit *);
167 static int glob1(Char *, glob_t *, struct glob_limit *);
168 static int glob2(Char *, Char *, Char *, const Char *, glob_t *,
169 struct glob_limit *);
170 static int glob3(Char *, Char *, Char *, const Char *, const Char *,
171 const Char *, glob_t *, struct glob_limit *);
172 static int globextend(const Char *, glob_t *, struct glob_limit *);
173 static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
174 static int globexp1(const Char *, glob_t *, struct glob_limit *);
175 static int globexp2(const Char *, const Char *, glob_t *, int *,
176 struct glob_limit *);
177 static int match(const Char *, const Char *, const Char *);
179 static void qprintf(const char *, Char *);
183 glob(const char * __restrict pattern, int flags, int (*errfunc)(const char *,
184 int), glob_t * __restrict pglob)
186 const unsigned char *patnext;
188 Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
189 struct glob_limit limit = { 0, 0, 0, 0 };
191 _DIAGASSERT(pattern != NULL);
193 patnext = (const unsigned char *) pattern;
194 if (!(flags & GLOB_APPEND)) {
196 pglob->gl_pathv = NULL;
197 if (!(flags & GLOB_DOOFFS))
200 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
201 pglob->gl_errfunc = errfunc;
202 pglob->gl_matchc = 0;
205 bufend = bufnext + MAXPATHLEN;
206 if (flags & GLOB_NOESCAPE) {
207 while (bufnext < bufend && (c = *patnext++) != EOS)
210 /* Protect the quoted characters. */
211 while (bufnext < bufend && (c = *patnext++) != EOS)
213 if ((c = *patnext++) == EOS) {
217 *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_limit *limit)
238 const Char* ptr = pattern;
241 _DIAGASSERT(pattern != NULL);
242 _DIAGASSERT(pglob != NULL);
244 if ((pglob->gl_flags & GLOB_LIMIT) &&
245 limit->l_brace++ >= GLOB_LIMIT_BRACE) {
250 /* Protect a single {}, for find(1), like csh */
251 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
252 return glob0(pattern, pglob, limit);
254 while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
255 if (!globexp2(ptr, pattern, pglob, &rv, limit))
258 return glob0(pattern, pglob, limit);
263 * Recursive brace globbing helper. Tries to expand a single brace.
264 * If it succeeds then it invokes globexp1 with the new pattern.
265 * If it fails then it tries to glob the rest of the pattern and returns.
268 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
269 struct glob_limit *limit)
273 const Char *pe, *pm, *pl;
274 Char patbuf[MAXPATHLEN + 1];
276 _DIAGASSERT(ptr != NULL);
277 _DIAGASSERT(pattern != NULL);
278 _DIAGASSERT(pglob != NULL);
279 _DIAGASSERT(rv != NULL);
281 /* copy part up to the brace */
282 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
286 /* Find the balanced brace */
287 for (i = 0, pe = ++ptr; *pe; pe++)
288 if (*pe == LBRACKET) {
289 /* Ignore everything between [] */
290 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
294 * We could not find a matching RBRACKET.
295 * Ignore and just look for RBRACE
300 else if (*pe == LBRACE)
302 else if (*pe == RBRACE) {
308 /* Non matching braces; just glob the pattern */
309 if (i != 0 || *pe == EOS) {
311 * we use `pattern', not `patbuf' here so that that
312 * unbalanced braces are passed to the match
314 *rv = glob0(pattern, pglob, limit);
318 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
321 /* Ignore everything between [] */
322 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
326 * We could not find a matching RBRACKET.
327 * Ignore and just look for RBRACE
344 if (i && *pm == COMMA)
347 /* Append the current string */
348 for (lm = ls; (pl < pm); *lm++ = *pl++)
351 * Append the rest of the pattern after the
354 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
357 /* Expand the current pattern */
359 qprintf("globexp2", patbuf);
361 *rv = globexp1(patbuf, pglob, limit);
363 /* move after the comma, to the next string */
379 * expand tilde from the passwd file.
382 globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
389 Char *pend = &patbuf[patsize / sizeof(Char)];
397 _DIAGASSERT(pattern != NULL);
398 _DIAGASSERT(patbuf != NULL);
399 _DIAGASSERT(pglob != NULL);
401 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
404 /* Copy up to the end of the string or / */
405 for (p = pattern + 1, d = (char *)(void *)patbuf;
406 d < (char *)(void *)pend && *p && *p != SLASH;
410 if (d == (char *)(void *)pend)
414 d = (char *)(void *)patbuf;
418 * handle a plain ~ or ~/ by expanding $HOME
419 * first and then trying the password file
421 if ((h = getenv("HOME")) == NULL) {
423 if ((pwd = getpwuid(getuid())) == NULL)
425 if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
426 &pwd) != 0 || pwd == NULL)
438 if ((pwd = getpwnam(d)) == NULL)
440 if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
448 /* Copy the home directory */
449 for (b = patbuf; b < pend && *h; *b++ = *h++)
455 /* Append the rest of the pattern */
456 while (b < pend && (*b++ = *p++) != EOS)
467 * The main glob() routine: compiles the pattern (optionally processing
468 * quotes), calls glob1() to do the real pattern matching, and finally
469 * sorts the list (unless unsorted operation is requested). Returns 0
470 * if things went well, nonzero if errors occurred. It is not an error
471 * to find no matches.
474 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
476 const Char *qpatnext;
478 __gl_size_t oldpathc;
479 Char *bufnext, patbuf[MAXPATHLEN+1];
481 _DIAGASSERT(pattern != NULL);
482 _DIAGASSERT(pglob != NULL);
484 if ((qpatnext = globtilde(pattern, patbuf, sizeof(patbuf),
487 oldpathc = pglob->gl_pathc;
490 /* We don't need to check for buffer overflow any more. */
491 while ((c = *qpatnext++) != EOS) {
497 if (*qpatnext == EOS ||
498 g_strchr(qpatnext+1, RBRACKET) == NULL) {
499 *bufnext++ = LBRACKET;
509 *bufnext++ = CHAR(c);
510 if (*qpatnext == RANGE &&
511 (c = qpatnext[1]) != RBRACKET) {
513 *bufnext++ = CHAR(c);
516 } while ((c = *qpatnext++) != RBRACKET);
517 pglob->gl_flags |= GLOB_MAGCHAR;
521 pglob->gl_flags |= GLOB_MAGCHAR;
525 pglob->gl_flags |= GLOB_MAGCHAR;
526 /* collapse adjacent stars to one [or three if globstar]
527 * to avoid exponential behavior
529 if (bufnext == patbuf || bufnext[-1] != M_ALL ||
530 ((pglob->gl_flags & GLOB_STAR) != 0 &&
531 (bufnext - 1 == patbuf || bufnext[-2] != M_ALL ||
532 bufnext - 2 == patbuf || bufnext[-3] != M_ALL)))
536 *bufnext++ = CHAR(c);
542 qprintf("glob0", patbuf);
545 if ((error = glob1(patbuf, pglob, limit)) != 0)
548 if (pglob->gl_pathc == oldpathc) {
550 * If there was no match we are going to append the pattern
551 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
552 * specified and the pattern did not contain any magic
553 * characters GLOB_NOMAGIC is there just for compatibility
556 if ((pglob->gl_flags & GLOB_NOCHECK) ||
557 ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR))
559 return globextend(pattern, pglob, limit);
563 } else if (!(pglob->gl_flags & GLOB_NOSORT)) {
564 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
565 (size_t)pglob->gl_pathc - oldpathc, sizeof(char *),
573 compare(const void *p, const void *q)
576 _DIAGASSERT(p != NULL);
577 _DIAGASSERT(q != NULL);
579 return strcoll(*(const char * const *)p, *(const char * const *)q);
583 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
585 Char pathbuf[MAXPATHLEN+1];
587 _DIAGASSERT(pattern != NULL);
588 _DIAGASSERT(pglob != NULL);
590 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
594 * we save one character so that we can use ptr >= limit,
595 * in the general case when we are appending non nul chars only.
597 return glob2(pathbuf, pathbuf,
598 pathbuf + (sizeof(pathbuf) / sizeof(*pathbuf)) - 1, pattern,
603 * The functions glob2 and glob3 are mutually recursive; there is one level
604 * of recursion for each segment in the pattern that contains one or more
608 glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
609 glob_t *pglob, struct glob_limit *limit)
616 _DIAGASSERT(pathbuf != NULL);
617 _DIAGASSERT(pathend != NULL);
618 _DIAGASSERT(pattern != NULL);
619 _DIAGASSERT(pglob != NULL);
622 qprintf("glob2", pathbuf);
625 * Loop over pattern segments until end of pattern or until
626 * segment with meta character found.
628 for (anymeta = 0;;) {
629 if (*pattern == EOS) { /* End of pattern? */
631 if (g_lstat(pathbuf, &sb, pglob))
634 if ((pglob->gl_flags & GLOB_LIMIT) &&
635 limit->l_stat++ >= GLOB_LIMIT_STAT) {
641 if (((pglob->gl_flags & GLOB_MARK) &&
642 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
643 (S_ISLNK(sb.st_mode) &&
644 (g_stat(pathbuf, &sb, pglob) == 0) &&
645 S_ISDIR(sb.st_mode)))) {
646 if (pathend >= pathlim)
652 return globextend(pathbuf, pglob, limit);
655 /* Find end of next segment, copy tentatively to pathend. */
658 while (*p != EOS && *p != SEP) {
669 while (*pattern == SEP) {
670 if (pathend >= pathlim)
672 *pathend++ = *pattern++;
674 } else /* Need expansion, recurse. */
675 return glob3(pathbuf, pathend, pathlim, pattern, p,
676 pattern, pglob, limit);
682 glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern,
683 const Char *restpattern, const Char *pglobstar, glob_t *pglob,
684 struct glob_limit *limit)
690 char buf[MAXPATHLEN];
692 int chase_symlinks = 0;
693 const Char *termstar = NULL;
696 * The readdirfunc declaration can't be prototyped, because it is
697 * assigned, below, to two functions which are prototyped in glob.h
698 * and dirent.h as taking pointers to differently typed opaque
701 struct dirent *(*readdirfunc)(void *);
703 _DIAGASSERT(pathbuf != NULL);
704 _DIAGASSERT(pathend != NULL);
705 _DIAGASSERT(pattern != NULL);
706 _DIAGASSERT(restpattern != NULL);
707 _DIAGASSERT(pglob != NULL);
712 while (pglobstar < restpattern) {
713 if ((pglobstar[0] & M_MASK) == M_ALL &&
714 (pglobstar[1] & M_MASK) == M_ALL) {
716 chase_symlinks = (pglobstar[2] & M_MASK) == M_ALL;
717 termstar = pglobstar + (2 + chase_symlinks);
724 error = pglobstar == pattern && termstar == restpattern ?
725 *restpattern == EOS ?
726 glob2(pathbuf, pathend, pathlim, restpattern - 1, pglob,
728 glob2(pathbuf, pathend, pathlim, restpattern + 1, pglob,
730 glob3(pathbuf, pathend, pathlim, pattern, restpattern,
731 termstar, pglob, limit);
737 if (*pathbuf && (g_lstat(pathbuf, &sbuf, pglob) ||
738 !S_ISDIR(sbuf.st_mode)
740 && ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode))
745 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
746 if (pglob->gl_errfunc) {
747 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
749 if (pglob->gl_errfunc(buf, errno) ||
750 pglob->gl_flags & GLOB_ERR)
754 * Posix/XOpen: glob should return when it encounters a
755 * directory that it cannot open or read
756 * XXX: Should we ignore ENOTDIR and ENOENT though?
757 * I think that Posix had in mind EPERM...
759 if (pglob->gl_flags & GLOB_ERR)
767 /* Search directory for matching names. */
768 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
769 readdirfunc = pglob->gl_readdir;
771 readdirfunc = (struct dirent *(*)(void *)) readdir;
772 while ((dp = (*readdirfunc)(dirp)) != NULL) {
776 if ((pglob->gl_flags & GLOB_LIMIT) &&
777 limit->l_readdir++ >= GLOB_LIMIT_READDIR) {
781 error = GLOB_NOSPACE;
786 * Initial DOT must be matched literally, unless we have
789 if ((pglob->gl_flags & GLOB_PERIOD) == 0)
790 if (dp->d_name[0] == DOT && *pattern != DOT)
793 * If GLOB_NO_DOTDIRS is set, . and .. vanish.
795 if ((pglob->gl_flags & GLOB_NO_DOTDIRS) &&
796 (dp->d_name[0] == DOT) &&
797 ((dp->d_name[1] == EOS) ||
798 ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS))))
801 * The resulting string contains EOS, so we can
802 * use the pathlim character, if it is the nul
804 for (sc = (unsigned char *) dp->d_name, dc = pathend;
805 dc <= pathlim && (*dc++ = *sc++) != EOS;)
809 * Have we filled the buffer without seeing EOS?
811 if (dc > pathlim && *pathlim != EOS) {
813 * Abort when requested by caller, otherwise
814 * reset pathend back to last SEP and continue
815 * with next dir entry.
817 if (pglob->gl_flags & GLOB_ERR) {
818 error = GLOB_ABORTED;
829 if (!chase_symlinks &&
830 (g_lstat(pathbuf, &sbuf, pglob) ||
831 S_ISLNK(sbuf.st_mode)))
835 if (!match(pathend, pattern, termstar))
838 if (--dc < pathlim - 2)
841 error = glob2(pathbuf, dc, pathlim, pglobstar,
847 if (!match(pathend, pattern, restpattern)) {
851 error = glob2(pathbuf, --dc, pathlim, restpattern,
857 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
858 (*pglob->gl_closedir)(dirp);
863 * Again Posix X/Open issue with regards to error handling.
865 if ((error || errno) && (pglob->gl_flags & GLOB_ERR))
873 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
874 * add the new item, and update gl_pathc.
876 * This assumes the BSD realloc, which only copies the block when its size
877 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
880 * Return 0 if new item added, error code if memory couldn't be allocated.
882 * Invariant of the glob_t structure:
883 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
884 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
887 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit)
890 size_t i, newsize, len;
894 _DIAGASSERT(path != NULL);
895 _DIAGASSERT(pglob != NULL);
897 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
898 if ((pglob->gl_flags & GLOB_LIMIT) &&
899 newsize > GLOB_LIMIT_PATH * sizeof(*pathv))
901 pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) :
906 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
907 /* first time around -- clear initial gl_offs items */
908 pathv += pglob->gl_offs;
909 for (i = pglob->gl_offs + 1; --i > 0; )
912 pglob->gl_pathv = pathv;
914 for (p = path; *p++;)
916 len = (size_t)(p - path);
917 limit->l_string += len;
918 if ((copy = malloc(len)) != NULL) {
919 if (g_Ctoc(path, copy, len)) {
923 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
925 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
927 if ((pglob->gl_flags & GLOB_LIMIT) &&
928 (newsize + limit->l_string) >= GLOB_LIMIT_STRING)
931 return copy == NULL ? GLOB_NOSPACE : 0;
939 * pattern matching function for filenames. Each occurrence of the *
940 * pattern causes a recursion level.
943 match(const Char *name, const Char *pat, const Char *patend)
945 int ok, negate_range;
948 _DIAGASSERT(name != NULL);
949 _DIAGASSERT(pat != NULL);
950 _DIAGASSERT(patend != NULL);
952 while (pat < patend) {
954 switch (c & M_MASK) {
956 while (pat < patend && (*pat & M_MASK) == M_ALL)
957 pat++; /* eat consecutive '*' */
960 for (; !match(name, pat, patend); name++)
970 if ((k = *name++) == EOS)
972 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
974 while (((c = *pat++) & M_MASK) != M_END)
975 if ((*pat & M_MASK) == M_RNG) {
976 if (c <= k && k <= pat[1])
981 if (ok == negate_range)
993 /* Free allocated data belonging to a glob_t structure. */
995 globfree(glob_t *pglob)
1000 _DIAGASSERT(pglob != NULL);
1002 if (pglob->gl_pathv != NULL) {
1003 pp = pglob->gl_pathv + pglob->gl_offs;
1004 for (i = pglob->gl_pathc; i--; ++pp)
1007 free(pglob->gl_pathv);
1008 pglob->gl_pathv = NULL;
1009 pglob->gl_pathc = 0;
1013 #ifndef __LIBC12_SOURCE__
1015 glob_pattern_p(const char *pattern, int quote)
1019 for (; *pattern; pattern++)
1026 if (quote && pattern[1] != EOS)
1047 g_opendir(Char *str, glob_t *pglob)
1049 char buf[MAXPATHLEN];
1051 _DIAGASSERT(str != NULL);
1052 _DIAGASSERT(pglob != NULL);
1055 (void)strlcpy(buf, ".", sizeof(buf));
1057 if (g_Ctoc(str, buf, sizeof(buf)))
1061 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1062 return (*pglob->gl_opendir)(buf);
1064 return opendir(buf);
1068 g_lstat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
1070 char buf[MAXPATHLEN];
1072 _DIAGASSERT(fn != NULL);
1073 _DIAGASSERT(sb != NULL);
1074 _DIAGASSERT(pglob != NULL);
1076 if (g_Ctoc(fn, buf, sizeof(buf)))
1078 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1079 return (*pglob->gl_lstat)(buf, sb);
1080 return lstat(buf, sb);
1084 g_stat(Char *fn, __gl_stat_t *sb, glob_t *pglob)
1086 char buf[MAXPATHLEN];
1088 _DIAGASSERT(fn != NULL);
1089 _DIAGASSERT(sb != NULL);
1090 _DIAGASSERT(pglob != NULL);
1092 if (g_Ctoc(fn, buf, sizeof(buf)))
1094 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1095 return (*pglob->gl_stat)(buf, sb);
1096 return stat(buf, sb);
1100 g_strchr(const Char *str, int ch)
1103 _DIAGASSERT(str != NULL);
1107 return __UNCONST(str);
1113 g_Ctoc(const Char *str, char *buf, size_t len)
1117 _DIAGASSERT(str != NULL);
1118 _DIAGASSERT(buf != NULL);
1123 for (dc = buf; len && (*dc++ = *str++) != EOS; len--)
1131 qprintf(const char *str, Char *s)
1135 _DIAGASSERT(str != NULL);
1136 _DIAGASSERT(s != NULL);
1138 (void)printf("%s:\n", str);
1139 for (p = s; *p; p++)
1140 (void)printf("%c", CHAR(*p));
1142 for (p = s; *p; p++)
1143 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1145 for (p = s; *p; p++)
1146 (void)printf("%c", ismeta(*p) ? '_' : ' ');