2 * SPDX-License-Identifier: BSD-3-Clause
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 * Copyright (c) 2011 The FreeBSD Foundation
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
41 __SCCSID("@(#)glob.c 8.3 (Berkeley) 10/13/93");
43 * glob(3) -- a superset of the one defined in POSIX 1003.2.
45 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
47 * Optional extra services, controlled by flags not defined by POSIX:
50 * Escaping convention: \ inhibits any special meaning the following
51 * character might have (except \ at end of string is retained).
53 * Set in gl_flags if pattern contained a globbing character.
55 * Same as GLOB_NOCHECK, but it will only append pattern if it did
56 * not contain any magic characters. [Used in csh style globbing]
58 * Use alternately specified directory access functions.
60 * expand ~user/foo to the /home/dir/of/user/foo
62 * expand {1,2}{a,b} to 1a 1b 2a 2b
64 * Number of matches in the current invocation of glob.
68 * Some notes on multibyte character support:
69 * 1. Patterns with illegal byte sequences match nothing - even if
70 * GLOB_NOCHECK is specified.
71 * 2. Illegal byte sequences in filenames are handled by treating them as
72 * single-byte characters with a values of such bytes of the sequence
74 * 3. State-dependent encodings are not currently supported.
77 #include <sys/param.h>
96 * glob(3) expansion limits. Stop the expansion if any of these limits
97 * is reached. This caps the runtime in the face of DoS attacks. See
100 #define GLOB_LIMIT_BRACE 128 /* number of brace calls */
101 #define GLOB_LIMIT_PATH 65536 /* number of path elements */
102 #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */
103 #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */
104 #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */
109 size_t l_readdir_cnt;
116 #define LBRACKET L'['
118 #define QUESTION L'?'
121 #define RBRACKET L']'
129 #define M_QUOTE 0x8000000000ULL
130 #define M_PROTECT 0x4000000000ULL
131 #define M_MASK 0xffffffffffULL
132 #define M_CHAR 0x00ffffffffULL
134 typedef uint_fast64_t Char;
136 #define CHAR(c) ((Char)((c)&M_CHAR))
137 #define META(c) ((Char)((c)|M_QUOTE))
138 #define UNPROT(c) ((c) & ~M_PROTECT)
139 #define M_ALL META(L'*')
140 #define M_END META(L']')
141 #define M_NOT META(L'!')
142 #define M_ONE META(L'?')
143 #define M_RNG META(L'-')
144 #define M_SET META(L'[')
145 #define ismeta(c) (((c)&M_QUOTE) != 0)
147 #define isprot(c) (((c)&M_PROTECT) != 0)
150 static int compare(const void *, const void *);
151 static int g_Ctoc(const Char *, char *, size_t);
152 static int g_lstat(Char *, struct stat *, glob_t *);
153 static DIR *g_opendir(Char *, glob_t *);
154 static const Char *g_strchr(const Char *, wchar_t);
156 static Char *g_strcat(Char *, const Char *);
158 static int g_stat(Char *, struct stat *, glob_t *);
159 static int glob0(const Char *, glob_t *, struct glob_limit *,
161 static int glob1(Char *, glob_t *, struct glob_limit *);
162 static int glob2(Char *, Char *, Char *, Char *, glob_t *,
163 struct glob_limit *);
164 static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
165 struct glob_limit *);
166 static int globextend(const Char *, glob_t *, struct glob_limit *,
169 globtilde(const Char *, Char *, size_t, glob_t *);
170 static int globexp0(const Char *, glob_t *, struct glob_limit *,
172 static int globexp1(const Char *, glob_t *, struct glob_limit *);
173 static int globexp2(const Char *, const Char *, glob_t *,
174 struct glob_limit *);
175 static int globfinal(glob_t *, struct glob_limit *, size_t,
177 static int match(Char *, Char *, Char *);
178 static int err_nomatch(glob_t *, struct glob_limit *, const char *);
179 static int err_aborted(glob_t *, int, char *);
181 static void qprintf(const char *, Char *);
185 glob(const char * __restrict pattern, int flags,
186 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
188 struct glob_limit limit = { 0, 0, 0, 0, 0 };
190 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
197 if (!(flags & GLOB_APPEND)) {
199 pglob->gl_pathv = NULL;
200 if (!(flags & GLOB_DOOFFS))
203 if (flags & GLOB_LIMIT) {
204 limit.l_path_lim = pglob->gl_matchc;
205 if (limit.l_path_lim == 0)
206 limit.l_path_lim = GLOB_LIMIT_PATH;
208 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
209 pglob->gl_errfunc = errfunc;
210 pglob->gl_matchc = 0;
213 bufend = bufnext + MAXPATHLEN - 1;
215 if (flags & GLOB_NOESCAPE) {
216 memset(&mbs, 0, sizeof(mbs));
217 while (bufnext <= bufend) {
218 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
219 if (clen == (size_t)-1 || clen == (size_t)-2)
220 return (err_nomatch(pglob, &limit, pattern));
221 else if (clen == 0) {
229 /* Protect the quoted characters. */
230 memset(&mbs, 0, sizeof(mbs));
231 while (bufnext <= bufend) {
232 if (*patnext == '\\') {
233 if (*++patnext == '\0') {
240 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
241 if (clen == (size_t)-1 || clen == (size_t)-2)
242 return (err_nomatch(pglob, &limit, pattern));
243 else if (clen == 0) {
247 *bufnext++ = wc | prot;
252 return (err_nomatch(pglob, &limit, pattern));
255 if (flags & GLOB_BRACE)
256 return (globexp0(patbuf, pglob, &limit, pattern));
258 return (glob0(patbuf, pglob, &limit, pattern));
262 globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
263 const char *origpat) {
267 /* Protect a single {}, for find(1), like csh */
268 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
269 if ((pglob->gl_flags & GLOB_LIMIT) &&
270 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
272 return (GLOB_NOSPACE);
274 return (glob0(pattern, pglob, limit, origpat));
277 oldpathc = pglob->gl_pathc;
279 if ((rv = globexp1(pattern, pglob, limit)) != 0)
282 return (globfinal(pglob, limit, oldpathc, origpat));
286 * Expand recursively a glob {} pattern. When there is no more expansion
287 * invoke the standard globbing routine to glob the rest of the magic
291 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
295 if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
296 if ((pglob->gl_flags & GLOB_LIMIT) &&
297 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
299 return (GLOB_NOSPACE);
301 return (globexp2(ptr, pattern, pglob, limit));
304 return (glob0(pattern, pglob, limit, NULL));
309 * Recursive brace globbing helper. Tries to expand a single brace.
310 * If it succeeds then it invokes globexp1 with the new pattern.
311 * If it fails then it tries to glob the rest of the pattern and returns.
314 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
315 struct glob_limit *limit)
319 const Char *pe, *pm, *pm1, *pl;
320 Char patbuf[MAXPATHLEN];
322 /* copy part up to the brace */
323 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
328 /* Find the balanced brace */
329 for (i = 0, pe = ++ptr; *pe != EOS; pe++)
330 if (*pe == LBRACKET) {
331 /* Ignore everything between [] */
332 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
336 * We could not find a matching RBRACKET.
337 * Ignore and just look for RBRACE
342 else if (*pe == LBRACE)
344 else if (*pe == RBRACE) {
350 /* Non matching braces; just glob the pattern */
351 if (i != 0 || *pe == EOS)
352 return (glob0(pattern, pglob, limit, NULL));
354 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
357 /* Ignore everything between [] */
358 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
362 * We could not find a matching RBRACKET.
363 * Ignore and just look for RBRACE
380 if (i && *pm == COMMA)
383 /* Append the current string */
384 for (lm = ls; (pl < pm); *lm++ = *pl++)
387 * Append the rest of the pattern after the
390 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
393 /* Expand the current pattern */
395 qprintf("globexp2:", patbuf);
397 rv = globexp1(patbuf, pglob, limit);
401 /* move after the comma, to the next string */
415 * expand tilde from the passwd file.
418 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
425 wchar_t wbuf[MAXPATHLEN];
426 wchar_t *wbufend, *dc;
431 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
435 * Copy up to the end of the string or /
437 eb = &patbuf[patbuf_len - 1];
438 for (p = pattern + 1, b = patbuf;
439 b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
442 if (*p != EOS && UNPROT(*p) != SEP)
448 if (patbuf[0] == EOS) {
450 * handle a plain ~ or ~/ by expanding $HOME first (iff
451 * we're not running setuid or setgid) and then trying
454 if ((h = secure_getenv("HOME")) == NULL) {
455 if (((h = getlogin()) != NULL &&
456 (pwd = getpwnam(h)) != NULL) ||
457 (pwd = getpwuid(getuid())) != NULL)
467 if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
469 if ((pwd = getpwnam((char *)wbuf)) == NULL)
475 /* Copy the home directory */
478 wbufend = wbuf + MAXPATHLEN - 1;
480 memset(&mbs, 0, sizeof(mbs));
481 while (dc <= wbufend) {
482 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
483 if (clen == (size_t)-1 || clen == (size_t)-2) {
484 /* XXX See initial comment #2. */
485 wc = (unsigned char)*sc;
487 memset(&mbs, 0, sizeof(mbs));
489 if ((*dc++ = wc) == EOS) {
499 for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
504 /* Append the rest of the pattern */
508 if ((*b++ = *p++) == EOS) {
523 * The main glob() routine: compiles the pattern (optionally processing
524 * quotes), calls glob1() to do the real pattern matching, and finally
525 * sorts the list (unless unsorted operation is requested). Returns 0
526 * if things went well, nonzero if errors occurred.
529 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
530 const char *origpat) {
531 const Char *qpatnext;
534 Char *bufnext, c, patbuf[MAXPATHLEN];
536 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
537 if (qpatnext == NULL) {
539 return (GLOB_NOSPACE);
541 oldpathc = pglob->gl_pathc;
544 /* We don't need to check for buffer overflow any more. */
545 while ((c = *qpatnext++) != EOS) {
551 if (*qpatnext == EOS ||
552 g_strchr(qpatnext+1, RBRACKET) == NULL) {
553 *bufnext++ = LBRACKET;
563 *bufnext++ = CHAR(c);
564 if (*qpatnext == RANGE &&
565 (c = qpatnext[1]) != RBRACKET) {
567 *bufnext++ = CHAR(c);
570 } while ((c = *qpatnext++) != RBRACKET);
571 pglob->gl_flags |= GLOB_MAGCHAR;
575 pglob->gl_flags |= GLOB_MAGCHAR;
579 pglob->gl_flags |= GLOB_MAGCHAR;
580 /* collapse adjacent stars to one,
581 * to ensure "**" at the end continues to match the
584 if (bufnext == patbuf || bufnext[-1] != M_ALL)
588 *bufnext++ = CHAR(c);
594 qprintf("glob0:", patbuf);
597 if ((err = glob1(patbuf, pglob, limit)) != 0)
601 return (globfinal(pglob, limit, oldpathc, origpat));
607 globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc,
608 const char *origpat) {
609 if (pglob->gl_pathc == oldpathc)
610 return (err_nomatch(pglob, limit, origpat));
612 if (!(pglob->gl_flags & GLOB_NOSORT))
613 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
614 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
620 compare(const void *p, const void *q)
622 return (strcoll(*(char **)p, *(char **)q));
626 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
628 Char pathbuf[MAXPATHLEN];
630 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
633 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
634 pattern, pglob, limit));
638 * The functions glob2 and glob3 are mutually recursive; there is one level
639 * of recursion for each segment in the pattern that contains one or more
643 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
644 glob_t *pglob, struct glob_limit *limit)
651 * Loop over pattern segments until end of pattern or until
652 * segment with meta character found.
654 for (anymeta = 0;;) {
655 if (*pattern == EOS) { /* End of pattern? */
657 if (g_lstat(pathbuf, &sb, pglob))
660 if ((pglob->gl_flags & GLOB_LIMIT) &&
661 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
663 return (GLOB_NOSPACE);
665 if ((pglob->gl_flags & GLOB_MARK) &&
666 UNPROT(pathend[-1]) != SEP &&
667 (S_ISDIR(sb.st_mode) ||
668 (S_ISLNK(sb.st_mode) &&
669 g_stat(pathbuf, &sb, pglob) == 0 &&
670 S_ISDIR(sb.st_mode)))) {
671 if (pathend + 1 > pathend_last) {
673 return (GLOB_NOSPACE);
679 return (globextend(pathbuf, pglob, limit, NULL));
682 /* Find end of next segment, copy tentatively to pathend. */
685 while (*p != EOS && UNPROT(*p) != SEP) {
688 if (q + 1 > pathend_last) {
690 return (GLOB_NOSPACE);
695 if (!anymeta) { /* No expansion, do next segment. */
698 while (UNPROT(*pattern) == SEP) {
699 if (pathend + 1 > pathend_last) {
701 return (GLOB_NOSPACE);
703 *pathend++ = *pattern++;
705 } else /* Need expansion, recurse. */
706 return (glob3(pathbuf, pathend, pathend_last, pattern,
713 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
714 Char *pattern, Char *restpattern,
715 glob_t *pglob, struct glob_limit *limit)
719 int err, too_long, saverrno, saverrno2;
720 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
722 struct dirent *(*readdirfunc)(DIR *);
724 if (pathend > pathend_last) {
726 return (GLOB_NOSPACE);
729 if (pglob->gl_errfunc != NULL &&
730 g_Ctoc(pathbuf, buf, sizeof(buf))) {
732 return (GLOB_NOSPACE);
737 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
738 if (errno == ENOENT || errno == ENOTDIR)
740 err = err_aborted(pglob, errno, buf);
748 /* pglob->gl_readdir takes a void *, fix this manually */
749 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
750 readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
752 readdirfunc = readdir;
755 /* Search directory for matching names. */
756 while ((dp = (*readdirfunc)(dirp)) != NULL) {
763 if ((pglob->gl_flags & GLOB_LIMIT) &&
764 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
770 /* Initial DOT must be matched literally. */
771 if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
775 memset(&mbs, 0, sizeof(mbs));
779 while (dc <= pathend_last) {
780 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
781 if (clen == (size_t)-1 || clen == (size_t)-2) {
782 /* XXX See initial comment #2. */
783 wc = (unsigned char)*sc;
785 memset(&mbs, 0, sizeof(mbs));
787 if ((*dc++ = wc) == EOS) {
793 if (too_long && (err = err_aborted(pglob, ENAMETOOLONG,
795 errno = ENAMETOOLONG;
798 if (too_long || !match(pathend, pattern, restpattern)) {
805 err = glob2(pathbuf, --dc, pathend_last, restpattern,
813 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
814 (*pglob->gl_closedir)(dirp);
822 if (dp == NULL && errno != 0 &&
823 (err = err_aborted(pglob, errno, buf)))
833 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
834 * add the new item, and update gl_pathc.
836 * This assumes the BSD realloc, which only copies the block when its size
837 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
840 * Return 0 if new item added, error code if memory couldn't be allocated.
842 * Invariant of the glob_t structure:
843 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
844 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
847 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
855 if ((pglob->gl_flags & GLOB_LIMIT) &&
856 pglob->gl_matchc > limit->l_path_lim) {
858 return (GLOB_NOSPACE);
861 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
862 /* reallocarray(NULL, newn, size) is equivalent to malloc(newn*size). */
863 pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
865 return (GLOB_NOSPACE);
867 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
868 /* first time around -- clear initial gl_offs items */
869 pathv += pglob->gl_offs;
870 for (i = pglob->gl_offs + 1; --i > 0; )
873 pglob->gl_pathv = pathv;
876 copy = strdup(origpat);
878 for (p = path; *p++ != EOS;)
880 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
881 if ((copy = malloc(len)) != NULL) {
882 if (g_Ctoc(path, copy, len)) {
885 return (GLOB_NOSPACE);
890 limit->l_string_cnt += strlen(copy) + 1;
891 if ((pglob->gl_flags & GLOB_LIMIT) &&
892 limit->l_string_cnt >= GLOB_LIMIT_STRING) {
895 return (GLOB_NOSPACE);
897 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
899 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
900 return (copy == NULL ? GLOB_NOSPACE : 0);
904 * pattern matching function for filenames.
907 match(Char *name, Char *pat, Char *patend)
909 int ok, negate_range;
910 Char c, k, *nextp, *nextn;
911 struct xlocale_collate *table =
912 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
918 while (pat < patend) {
920 switch (c & M_MASK) {
935 if ((k = *name++) == EOS)
937 negate_range = ((*pat & M_MASK) == M_NOT);
938 if (negate_range != 0)
940 while (((c = *pat++) & M_MASK) != M_END)
941 if ((*pat & M_MASK) == M_RNG) {
942 if (table->__collate_load_error ?
943 CHAR(c) <= CHAR(k) &&
944 CHAR(k) <= CHAR(pat[1]) :
945 __wcollate_range_cmp(CHAR(c),
947 __wcollate_range_cmp(CHAR(k),
953 if (ok == negate_range)
974 /* Free allocated data belonging to a glob_t structure. */
976 globfree(glob_t *pglob)
981 if (pglob->gl_pathv != NULL) {
982 pp = pglob->gl_pathv + pglob->gl_offs;
983 for (i = pglob->gl_pathc; i--; ++pp)
986 free(pglob->gl_pathv);
987 pglob->gl_pathv = NULL;
992 g_opendir(Char *str, glob_t *pglob)
994 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
999 if (g_Ctoc(str, buf, sizeof(buf))) {
1000 errno = ENAMETOOLONG;
1005 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1006 return ((*pglob->gl_opendir)(buf));
1008 return (opendir(buf));
1012 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1014 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1016 if (g_Ctoc(fn, buf, sizeof(buf))) {
1017 errno = ENAMETOOLONG;
1020 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1021 return((*pglob->gl_lstat)(buf, sb));
1022 return (lstat(buf, sb));
1026 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1028 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1030 if (g_Ctoc(fn, buf, sizeof(buf))) {
1031 errno = ENAMETOOLONG;
1034 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1035 return ((*pglob->gl_stat)(buf, sb));
1036 return (stat(buf, sb));
1040 g_strchr(const Char *str, wchar_t ch)
1051 g_Ctoc(const Char *str, char *buf, size_t len)
1056 memset(&mbs, 0, sizeof(mbs));
1057 while (len >= MB_CUR_MAX) {
1058 clen = wcrtomb(buf, CHAR(*str), &mbs);
1059 if (clen == (size_t)-1) {
1060 /* XXX See initial comment #2. */
1061 *buf = (char)CHAR(*str);
1063 memset(&mbs, 0, sizeof(mbs));
1065 if (CHAR(*str) == EOS)
1075 err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) {
1077 * If there was no match we are going to append the origpat
1078 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
1079 * and the origpat did not contain any magic characters
1080 * GLOB_NOMAGIC is there just for compatibility with csh.
1082 if ((pglob->gl_flags & GLOB_NOCHECK) ||
1083 ((pglob->gl_flags & GLOB_NOMAGIC) &&
1084 !(pglob->gl_flags & GLOB_MAGCHAR)))
1085 return (globextend(NULL, pglob, limit, origpat));
1086 return (GLOB_NOMATCH);
1090 err_aborted(glob_t *pglob, int err, char *buf) {
1091 if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) ||
1092 (pglob->gl_flags & GLOB_ERR))
1093 return (GLOB_ABORTED);
1099 qprintf(const char *str, Char *s)
1103 (void)printf("%s\n", str);
1105 for (p = s; *p != EOS; p++)
1106 (void)printf("%c", (char)CHAR(*p));
1108 for (p = s; *p != EOS; p++)
1109 (void)printf("%c", (isprot(*p) ? '\\' : ' '));
1111 for (p = s; *p != EOS; p++)
1112 (void)printf("%c", (ismeta(*p) ? '_' : ' '));