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
11 * All rights reserved.
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 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
42 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * glob(3) -- a superset of the one defined in POSIX 1003.2.
49 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
51 * Optional extra services, controlled by flags not defined by POSIX:
54 * Escaping convention: \ inhibits any special meaning the following
55 * character might have (except \ at end of string is retained).
57 * Set in gl_flags if pattern contained a globbing character.
59 * Same as GLOB_NOCHECK, but it will only append pattern if it did
60 * not contain any magic characters. [Used in csh style globbing]
62 * Use alternately specified directory access functions.
64 * expand ~user/foo to the /home/dir/of/user/foo
66 * expand {1,2}{a,b} to 1a 1b 2a 2b
68 * Number of matches in the current invocation of glob.
72 * Some notes on multibyte character support:
73 * 1. Patterns with illegal byte sequences match nothing - even if
74 * GLOB_NOCHECK is specified.
75 * 2. Illegal byte sequences in filenames are handled by treating them as
76 * single-byte characters with a values of such bytes of the sequence
78 * 3. State-dependent encodings are not currently supported.
81 #include <sys/param.h>
100 * glob(3) expansion limits. Stop the expansion if any of these limits
101 * is reached. This caps the runtime in the face of DoS attacks. See
104 #define GLOB_LIMIT_BRACE 128 /* number of brace calls */
105 #define GLOB_LIMIT_PATH 65536 /* number of path elements */
106 #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */
107 #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */
108 #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */
113 size_t l_readdir_cnt;
120 #define LBRACKET L'['
122 #define QUESTION L'?'
125 #define RBRACKET L']'
133 #define M_QUOTE 0x8000000000ULL
134 #define M_PROTECT 0x4000000000ULL
135 #define M_MASK 0xffffffffffULL
136 #define M_CHAR 0x00ffffffffULL
138 typedef uint_fast64_t Char;
140 #define CHAR(c) ((Char)((c)&M_CHAR))
141 #define META(c) ((Char)((c)|M_QUOTE))
142 #define UNPROT(c) ((c) & ~M_PROTECT)
143 #define M_ALL META(L'*')
144 #define M_END META(L']')
145 #define M_NOT META(L'!')
146 #define M_ONE META(L'?')
147 #define M_RNG META(L'-')
148 #define M_SET META(L'[')
149 #define ismeta(c) (((c)&M_QUOTE) != 0)
151 #define isprot(c) (((c)&M_PROTECT) != 0)
154 static int compare(const void *, const void *);
155 static int g_Ctoc(const Char *, char *, size_t);
156 static int g_lstat(Char *, struct stat *, glob_t *);
157 static DIR *g_opendir(Char *, glob_t *);
158 static const Char *g_strchr(const Char *, wchar_t);
160 static Char *g_strcat(Char *, const Char *);
162 static int g_stat(Char *, struct stat *, glob_t *);
163 static int glob0(const Char *, glob_t *, struct glob_limit *,
165 static int glob1(Char *, glob_t *, struct glob_limit *);
166 static int glob2(Char *, Char *, Char *, Char *, glob_t *,
167 struct glob_limit *);
168 static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
169 struct glob_limit *);
170 static int globextend(const Char *, glob_t *, struct glob_limit *,
173 globtilde(const Char *, Char *, size_t, glob_t *);
174 static int globexp0(const Char *, glob_t *, struct glob_limit *,
176 static int globexp1(const Char *, glob_t *, struct glob_limit *);
177 static int globexp2(const Char *, const Char *, glob_t *,
178 struct glob_limit *);
179 static int globfinal(glob_t *, struct glob_limit *, size_t,
181 static int match(Char *, Char *, Char *);
182 static int err_nomatch(glob_t *, struct glob_limit *, const char *);
183 static int err_aborted(glob_t *, int, char *);
185 static void qprintf(const char *, Char *);
189 glob(const char * __restrict pattern, int flags,
190 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
192 struct glob_limit limit = { 0, 0, 0, 0, 0 };
194 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
201 if (!(flags & GLOB_APPEND)) {
203 pglob->gl_pathv = NULL;
204 if (!(flags & GLOB_DOOFFS))
207 if (flags & GLOB_LIMIT) {
208 limit.l_path_lim = pglob->gl_matchc;
209 if (limit.l_path_lim == 0)
210 limit.l_path_lim = GLOB_LIMIT_PATH;
212 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
213 pglob->gl_errfunc = errfunc;
214 pglob->gl_matchc = 0;
217 bufend = bufnext + MAXPATHLEN - 1;
219 if (flags & GLOB_NOESCAPE) {
220 memset(&mbs, 0, sizeof(mbs));
221 while (bufnext <= bufend) {
222 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
223 if (clen == (size_t)-1 || clen == (size_t)-2)
224 return (err_nomatch(pglob, &limit, pattern));
225 else if (clen == 0) {
233 /* Protect the quoted characters. */
234 memset(&mbs, 0, sizeof(mbs));
235 while (bufnext <= bufend) {
236 if (*patnext == '\\') {
237 if (*++patnext == '\0') {
244 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
245 if (clen == (size_t)-1 || clen == (size_t)-2)
246 return (err_nomatch(pglob, &limit, pattern));
247 else if (clen == 0) {
251 *bufnext++ = wc | prot;
256 return (err_nomatch(pglob, &limit, pattern));
259 if (flags & GLOB_BRACE)
260 return (globexp0(patbuf, pglob, &limit, pattern));
262 return (glob0(patbuf, pglob, &limit, pattern));
266 globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
267 const char *origpat) {
271 /* Protect a single {}, for find(1), like csh */
272 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
273 if ((pglob->gl_flags & GLOB_LIMIT) &&
274 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
276 return (GLOB_NOSPACE);
278 return (glob0(pattern, pglob, limit, origpat));
281 oldpathc = pglob->gl_pathc;
283 if ((rv = globexp1(pattern, pglob, limit)) != 0)
286 return (globfinal(pglob, limit, oldpathc, origpat));
290 * Expand recursively a glob {} pattern. When there is no more expansion
291 * invoke the standard globbing routine to glob the rest of the magic
295 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
299 if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
300 if ((pglob->gl_flags & GLOB_LIMIT) &&
301 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
303 return (GLOB_NOSPACE);
305 return (globexp2(ptr, pattern, pglob, limit));
308 return (glob0(pattern, pglob, limit, NULL));
313 * Recursive brace globbing helper. Tries to expand a single brace.
314 * If it succeeds then it invokes globexp1 with the new pattern.
315 * If it fails then it tries to glob the rest of the pattern and returns.
318 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
319 struct glob_limit *limit)
323 const Char *pe, *pm, *pm1, *pl;
324 Char patbuf[MAXPATHLEN];
326 /* copy part up to the brace */
327 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
332 /* Find the balanced brace */
333 for (i = 0, pe = ++ptr; *pe != EOS; pe++)
334 if (*pe == LBRACKET) {
335 /* Ignore everything between [] */
336 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
340 * We could not find a matching RBRACKET.
341 * Ignore and just look for RBRACE
346 else if (*pe == LBRACE)
348 else if (*pe == RBRACE) {
354 /* Non matching braces; just glob the pattern */
355 if (i != 0 || *pe == EOS)
356 return (glob0(pattern, pglob, limit, NULL));
358 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
361 /* Ignore everything between [] */
362 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
366 * We could not find a matching RBRACKET.
367 * Ignore and just look for RBRACE
384 if (i && *pm == COMMA)
387 /* Append the current string */
388 for (lm = ls; (pl < pm); *lm++ = *pl++)
391 * Append the rest of the pattern after the
394 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
397 /* Expand the current pattern */
399 qprintf("globexp2:", patbuf);
401 rv = globexp1(patbuf, pglob, limit);
405 /* move after the comma, to the next string */
419 * expand tilde from the passwd file.
422 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
429 wchar_t wbuf[MAXPATHLEN];
430 wchar_t *wbufend, *dc;
435 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
439 * Copy up to the end of the string or /
441 eb = &patbuf[patbuf_len - 1];
442 for (p = pattern + 1, b = patbuf;
443 b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
446 if (*p != EOS && UNPROT(*p) != SEP)
452 if (patbuf[0] == EOS) {
454 * handle a plain ~ or ~/ by expanding $HOME first (iff
455 * we're not running setuid or setgid) and then trying
458 if (issetugid() != 0 ||
459 (h = getenv("HOME")) == NULL) {
460 if (((h = getlogin()) != NULL &&
461 (pwd = getpwnam(h)) != NULL) ||
462 (pwd = getpwuid(getuid())) != NULL)
472 if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
474 if ((pwd = getpwnam((char *)wbuf)) == NULL)
480 /* Copy the home directory */
483 wbufend = wbuf + MAXPATHLEN - 1;
485 memset(&mbs, 0, sizeof(mbs));
486 while (dc <= wbufend) {
487 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
488 if (clen == (size_t)-1 || clen == (size_t)-2) {
489 /* XXX See initial comment #2. */
490 wc = (unsigned char)*sc;
492 memset(&mbs, 0, sizeof(mbs));
494 if ((*dc++ = wc) == EOS) {
504 for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
509 /* Append the rest of the pattern */
513 if ((*b++ = *p++) == EOS) {
528 * The main glob() routine: compiles the pattern (optionally processing
529 * quotes), calls glob1() to do the real pattern matching, and finally
530 * sorts the list (unless unsorted operation is requested). Returns 0
531 * if things went well, nonzero if errors occurred.
534 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
535 const char *origpat) {
536 const Char *qpatnext;
539 Char *bufnext, c, patbuf[MAXPATHLEN];
541 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
542 if (qpatnext == NULL) {
544 return (GLOB_NOSPACE);
546 oldpathc = pglob->gl_pathc;
549 /* We don't need to check for buffer overflow any more. */
550 while ((c = *qpatnext++) != EOS) {
556 if (*qpatnext == EOS ||
557 g_strchr(qpatnext+1, RBRACKET) == NULL) {
558 *bufnext++ = LBRACKET;
568 *bufnext++ = CHAR(c);
569 if (*qpatnext == RANGE &&
570 (c = qpatnext[1]) != RBRACKET) {
572 *bufnext++ = CHAR(c);
575 } while ((c = *qpatnext++) != RBRACKET);
576 pglob->gl_flags |= GLOB_MAGCHAR;
580 pglob->gl_flags |= GLOB_MAGCHAR;
584 pglob->gl_flags |= GLOB_MAGCHAR;
585 /* collapse adjacent stars to one,
586 * to ensure "**" at the end continues to match the
589 if (bufnext == patbuf || bufnext[-1] != M_ALL)
593 *bufnext++ = CHAR(c);
599 qprintf("glob0:", patbuf);
602 if ((err = glob1(patbuf, pglob, limit)) != 0)
606 return (globfinal(pglob, limit, oldpathc, origpat));
612 globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc,
613 const char *origpat) {
614 if (pglob->gl_pathc == oldpathc)
615 return (err_nomatch(pglob, limit, origpat));
617 if (!(pglob->gl_flags & GLOB_NOSORT))
618 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
619 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
625 compare(const void *p, const void *q)
627 return (strcoll(*(char **)p, *(char **)q));
631 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
633 Char pathbuf[MAXPATHLEN];
635 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
638 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
639 pattern, pglob, limit));
643 * The functions glob2 and glob3 are mutually recursive; there is one level
644 * of recursion for each segment in the pattern that contains one or more
648 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
649 glob_t *pglob, struct glob_limit *limit)
656 * Loop over pattern segments until end of pattern or until
657 * segment with meta character found.
659 for (anymeta = 0;;) {
660 if (*pattern == EOS) { /* End of pattern? */
662 if (g_lstat(pathbuf, &sb, pglob))
665 if ((pglob->gl_flags & GLOB_LIMIT) &&
666 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
668 return (GLOB_NOSPACE);
670 if ((pglob->gl_flags & GLOB_MARK) &&
671 UNPROT(pathend[-1]) != SEP &&
672 (S_ISDIR(sb.st_mode) ||
673 (S_ISLNK(sb.st_mode) &&
674 g_stat(pathbuf, &sb, pglob) == 0 &&
675 S_ISDIR(sb.st_mode)))) {
676 if (pathend + 1 > pathend_last) {
678 return (GLOB_NOSPACE);
684 return (globextend(pathbuf, pglob, limit, NULL));
687 /* Find end of next segment, copy tentatively to pathend. */
690 while (*p != EOS && UNPROT(*p) != SEP) {
693 if (q + 1 > pathend_last) {
695 return (GLOB_NOSPACE);
700 if (!anymeta) { /* No expansion, do next segment. */
703 while (UNPROT(*pattern) == SEP) {
704 if (pathend + 1 > pathend_last) {
706 return (GLOB_NOSPACE);
708 *pathend++ = *pattern++;
710 } else /* Need expansion, recurse. */
711 return (glob3(pathbuf, pathend, pathend_last, pattern,
718 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
719 Char *pattern, Char *restpattern,
720 glob_t *pglob, struct glob_limit *limit)
724 int err, too_long, saverrno, saverrno2;
725 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
727 struct dirent *(*readdirfunc)(DIR *);
729 if (pathend > pathend_last) {
731 return (GLOB_NOSPACE);
734 if (pglob->gl_errfunc != NULL &&
735 g_Ctoc(pathbuf, buf, sizeof(buf))) {
737 return (GLOB_NOSPACE);
742 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
743 if (errno == ENOENT || errno == ENOTDIR)
745 err = err_aborted(pglob, errno, buf);
753 /* pglob->gl_readdir takes a void *, fix this manually */
754 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
755 readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
757 readdirfunc = readdir;
760 /* Search directory for matching names. */
761 while ((dp = (*readdirfunc)(dirp)) != NULL) {
768 if ((pglob->gl_flags & GLOB_LIMIT) &&
769 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
775 /* Initial DOT must be matched literally. */
776 if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
780 memset(&mbs, 0, sizeof(mbs));
784 while (dc <= pathend_last) {
785 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
786 if (clen == (size_t)-1 || clen == (size_t)-2) {
787 /* XXX See initial comment #2. */
788 wc = (unsigned char)*sc;
790 memset(&mbs, 0, sizeof(mbs));
792 if ((*dc++ = wc) == EOS) {
798 if (too_long && (err = err_aborted(pglob, ENAMETOOLONG,
800 errno = ENAMETOOLONG;
803 if (too_long || !match(pathend, pattern, restpattern)) {
810 err = glob2(pathbuf, --dc, pathend_last, restpattern,
818 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
819 (*pglob->gl_closedir)(dirp);
827 if (dp == NULL && errno != 0 &&
828 (err = err_aborted(pglob, errno, buf)))
838 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
839 * add the new item, and update gl_pathc.
841 * This assumes the BSD realloc, which only copies the block when its size
842 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
845 * Return 0 if new item added, error code if memory couldn't be allocated.
847 * Invariant of the glob_t structure:
848 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
849 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
852 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
860 if ((pglob->gl_flags & GLOB_LIMIT) &&
861 pglob->gl_matchc > limit->l_path_lim) {
863 return (GLOB_NOSPACE);
866 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
867 /* reallocarray(NULL, newn, size) is equivalent to malloc(newn*size). */
868 pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
870 return (GLOB_NOSPACE);
872 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
873 /* first time around -- clear initial gl_offs items */
874 pathv += pglob->gl_offs;
875 for (i = pglob->gl_offs + 1; --i > 0; )
878 pglob->gl_pathv = pathv;
881 copy = strdup(origpat);
883 for (p = path; *p++ != EOS;)
885 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
886 if ((copy = malloc(len)) != NULL) {
887 if (g_Ctoc(path, copy, len)) {
890 return (GLOB_NOSPACE);
895 limit->l_string_cnt += strlen(copy) + 1;
896 if ((pglob->gl_flags & GLOB_LIMIT) &&
897 limit->l_string_cnt >= GLOB_LIMIT_STRING) {
900 return (GLOB_NOSPACE);
902 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
904 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
905 return (copy == NULL ? GLOB_NOSPACE : 0);
909 * pattern matching function for filenames.
912 match(Char *name, Char *pat, Char *patend)
914 int ok, negate_range;
915 Char c, k, *nextp, *nextn;
916 struct xlocale_collate *table =
917 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
923 while (pat < patend) {
925 switch (c & M_MASK) {
940 if ((k = *name++) == EOS)
942 negate_range = ((*pat & M_MASK) == M_NOT);
943 if (negate_range != 0)
945 while (((c = *pat++) & M_MASK) != M_END)
946 if ((*pat & M_MASK) == M_RNG) {
947 if (table->__collate_load_error ?
948 CHAR(c) <= CHAR(k) &&
949 CHAR(k) <= CHAR(pat[1]) :
950 __wcollate_range_cmp(CHAR(c),
952 __wcollate_range_cmp(CHAR(k),
958 if (ok == negate_range)
979 /* Free allocated data belonging to a glob_t structure. */
981 globfree(glob_t *pglob)
986 if (pglob->gl_pathv != NULL) {
987 pp = pglob->gl_pathv + pglob->gl_offs;
988 for (i = pglob->gl_pathc; i--; ++pp)
991 free(pglob->gl_pathv);
992 pglob->gl_pathv = NULL;
997 g_opendir(Char *str, glob_t *pglob)
999 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1004 if (g_Ctoc(str, buf, sizeof(buf))) {
1005 errno = ENAMETOOLONG;
1010 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1011 return ((*pglob->gl_opendir)(buf));
1013 return (opendir(buf));
1017 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1019 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1021 if (g_Ctoc(fn, buf, sizeof(buf))) {
1022 errno = ENAMETOOLONG;
1025 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1026 return((*pglob->gl_lstat)(buf, sb));
1027 return (lstat(buf, sb));
1031 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1033 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1035 if (g_Ctoc(fn, buf, sizeof(buf))) {
1036 errno = ENAMETOOLONG;
1039 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1040 return ((*pglob->gl_stat)(buf, sb));
1041 return (stat(buf, sb));
1045 g_strchr(const Char *str, wchar_t ch)
1056 g_Ctoc(const Char *str, char *buf, size_t len)
1061 memset(&mbs, 0, sizeof(mbs));
1062 while (len >= MB_CUR_MAX) {
1063 clen = wcrtomb(buf, CHAR(*str), &mbs);
1064 if (clen == (size_t)-1) {
1065 /* XXX See initial comment #2. */
1066 *buf = (char)CHAR(*str);
1068 memset(&mbs, 0, sizeof(mbs));
1070 if (CHAR(*str) == EOS)
1080 err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) {
1082 * If there was no match we are going to append the origpat
1083 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
1084 * and the origpat did not contain any magic characters
1085 * GLOB_NOMAGIC is there just for compatibility with csh.
1087 if ((pglob->gl_flags & GLOB_NOCHECK) ||
1088 ((pglob->gl_flags & GLOB_NOMAGIC) &&
1089 !(pglob->gl_flags & GLOB_MAGCHAR)))
1090 return (globextend(NULL, pglob, limit, origpat));
1091 return (GLOB_NOMATCH);
1095 err_aborted(glob_t *pglob, int err, char *buf) {
1096 if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) ||
1097 (pglob->gl_flags & GLOB_ERR))
1098 return (GLOB_ABORTED);
1104 qprintf(const char *str, Char *s)
1108 (void)printf("%s\n", str);
1110 for (p = s; *p != EOS; p++)
1111 (void)printf("%c", (char)CHAR(*p));
1113 for (p = s; *p != EOS; p++)
1114 (void)printf("%c", (isprot(*p) ? '\\' : ' '));
1116 for (p = s; *p != EOS; p++)
1117 (void)printf("%c", (ismeta(*p) ? '_' : ' '));