2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
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 * Escaping convention: \ inhibits any special meaning the following
53 * character might have (except \ at end of string is retained).
55 * Set in gl_flags if pattern contained a globbing character.
57 * Same as GLOB_NOCHECK, but it will only append pattern if it did
58 * not contain any magic characters. [Used in csh style globbing]
60 * Use alternately specified directory access functions.
62 * expand ~user/foo to the /home/dir/of/user/foo
64 * expand {1,2}{a,b} to 1a 1b 2a 2b
66 * Number of matches in the current invocation of glob.
70 * Some notes on multibyte character support:
71 * 1. Patterns with illegal byte sequences match nothing - even if
72 * GLOB_NOCHECK is specified.
73 * 2. Illegal byte sequences in filenames are handled by treating them as
74 * single-byte characters with a values of such bytes of the sequence
76 * 3. State-dependent encodings are not currently supported.
79 #include <sys/param.h>
98 * glob(3) expansion limits. Stop the expansion if any of these limits
99 * is reached. This caps the runtime in the face of DoS attacks. See
102 #define GLOB_LIMIT_BRACE 128 /* number of brace calls */
103 #define GLOB_LIMIT_PATH 65536 /* number of path elements */
104 #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */
105 #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */
106 #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */
111 size_t l_readdir_cnt;
118 #define LBRACKET L'['
120 #define QUESTION L'?'
123 #define RBRACKET L']'
131 #define M_QUOTE 0x8000000000ULL
132 #define M_PROTECT 0x4000000000ULL
133 #define M_MASK 0xffffffffffULL
134 #define M_CHAR 0x00ffffffffULL
136 typedef uint_fast64_t Char;
138 #define CHAR(c) ((Char)((c)&M_CHAR))
139 #define META(c) ((Char)((c)|M_QUOTE))
140 #define UNPROT(c) ((c) & ~M_PROTECT)
141 #define M_ALL META(L'*')
142 #define M_END META(L']')
143 #define M_NOT META(L'!')
144 #define M_ONE META(L'?')
145 #define M_RNG META(L'-')
146 #define M_SET META(L'[')
147 #define ismeta(c) (((c)&M_QUOTE) != 0)
149 #define isprot(c) (((c)&M_PROTECT) != 0)
152 static int compare(const void *, const void *);
153 static int g_Ctoc(const Char *, char *, size_t);
154 static int g_lstat(Char *, struct stat *, glob_t *);
155 static DIR *g_opendir(Char *, glob_t *);
156 static const Char *g_strchr(const Char *, wchar_t);
158 static Char *g_strcat(Char *, const Char *);
160 static int g_stat(Char *, struct stat *, glob_t *);
161 static int glob0(const Char *, glob_t *, struct glob_limit *,
163 static int glob1(Char *, glob_t *, struct glob_limit *);
164 static int glob2(Char *, Char *, Char *, Char *, glob_t *,
165 struct glob_limit *);
166 static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
167 struct glob_limit *);
168 static int globextend(const Char *, glob_t *, struct glob_limit *,
171 globtilde(const Char *, Char *, size_t, glob_t *);
172 static int globexp0(const Char *, glob_t *, struct glob_limit *,
174 static int globexp1(const Char *, glob_t *, struct glob_limit *);
175 static int globexp2(const Char *, const Char *, glob_t *,
176 struct glob_limit *);
177 static int globfinal(glob_t *, struct glob_limit *, size_t,
179 static int match(Char *, Char *, Char *);
180 static int err_nomatch(glob_t *, struct glob_limit *, const char *);
181 static int err_aborted(glob_t *, int, char *);
183 static void qprintf(const char *, Char *);
187 glob(const char * __restrict pattern, int flags,
188 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
190 struct glob_limit limit = { 0, 0, 0, 0, 0 };
192 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
199 if (!(flags & GLOB_APPEND)) {
201 pglob->gl_pathv = NULL;
202 if (!(flags & GLOB_DOOFFS))
205 if (flags & GLOB_LIMIT) {
206 limit.l_path_lim = pglob->gl_matchc;
207 if (limit.l_path_lim == 0)
208 limit.l_path_lim = GLOB_LIMIT_PATH;
210 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
211 pglob->gl_errfunc = errfunc;
212 pglob->gl_matchc = 0;
215 bufend = bufnext + MAXPATHLEN - 1;
217 if (flags & GLOB_NOESCAPE) {
218 memset(&mbs, 0, sizeof(mbs));
219 while (bufnext <= bufend) {
220 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
221 if (clen == (size_t)-1 || clen == (size_t)-2)
222 return (err_nomatch(pglob, &limit, pattern));
223 else if (clen == 0) {
231 /* Protect the quoted characters. */
232 memset(&mbs, 0, sizeof(mbs));
233 while (bufnext <= bufend) {
234 if (*patnext == '\\') {
235 if (*++patnext == '\0') {
242 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
243 if (clen == (size_t)-1 || clen == (size_t)-2)
244 return (err_nomatch(pglob, &limit, pattern));
245 else if (clen == 0) {
249 *bufnext++ = wc | prot;
254 return (err_nomatch(pglob, &limit, pattern));
257 if (flags & GLOB_BRACE)
258 return (globexp0(patbuf, pglob, &limit, pattern));
260 return (glob0(patbuf, pglob, &limit, pattern));
264 globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
265 const char *origpat) {
269 /* Protect a single {}, for find(1), like csh */
270 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
271 if ((pglob->gl_flags & GLOB_LIMIT) &&
272 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
274 return (GLOB_NOSPACE);
276 return (glob0(pattern, pglob, limit, origpat));
279 oldpathc = pglob->gl_pathc;
281 if ((rv = globexp1(pattern, pglob, limit)) != 0)
284 return (globfinal(pglob, limit, oldpathc, origpat));
288 * Expand recursively a glob {} pattern. When there is no more expansion
289 * invoke the standard globbing routine to glob the rest of the magic
293 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
297 if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
298 if ((pglob->gl_flags & GLOB_LIMIT) &&
299 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
301 return (GLOB_NOSPACE);
303 return (globexp2(ptr, pattern, pglob, limit));
306 return (glob0(pattern, pglob, limit, NULL));
311 * Recursive brace globbing helper. Tries to expand a single brace.
312 * If it succeeds then it invokes globexp1 with the new pattern.
313 * If it fails then it tries to glob the rest of the pattern and returns.
316 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
317 struct glob_limit *limit)
321 const Char *pe, *pm, *pm1, *pl;
322 Char patbuf[MAXPATHLEN];
324 /* copy part up to the brace */
325 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
330 /* Find the balanced brace */
331 for (i = 0, pe = ++ptr; *pe != EOS; pe++)
332 if (*pe == LBRACKET) {
333 /* Ignore everything between [] */
334 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
338 * We could not find a matching RBRACKET.
339 * Ignore and just look for RBRACE
344 else if (*pe == LBRACE)
346 else if (*pe == RBRACE) {
352 /* Non matching braces; just glob the pattern */
353 if (i != 0 || *pe == EOS)
354 return (glob0(pattern, pglob, limit, NULL));
356 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
359 /* Ignore everything between [] */
360 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
364 * We could not find a matching RBRACKET.
365 * Ignore and just look for RBRACE
382 if (i && *pm == COMMA)
385 /* Append the current string */
386 for (lm = ls; (pl < pm); *lm++ = *pl++)
389 * Append the rest of the pattern after the
392 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
395 /* Expand the current pattern */
397 qprintf("globexp2:", patbuf);
399 rv = globexp1(patbuf, pglob, limit);
403 /* move after the comma, to the next string */
417 * expand tilde from the passwd file.
420 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
427 wchar_t wbuf[MAXPATHLEN];
428 wchar_t *wbufend, *dc;
433 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
437 * Copy up to the end of the string or /
439 eb = &patbuf[patbuf_len - 1];
440 for (p = pattern + 1, b = patbuf;
441 b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
444 if (*p != EOS && UNPROT(*p) != SEP)
450 if (patbuf[0] == EOS) {
452 * handle a plain ~ or ~/ by expanding $HOME first (iff
453 * we're not running setuid or setgid) and then trying
456 if (issetugid() != 0 ||
457 (h = getenv("HOME")) == NULL) {
458 if (((h = getlogin()) != NULL &&
459 (pwd = getpwnam(h)) != NULL) ||
460 (pwd = getpwuid(getuid())) != NULL)
470 if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
472 if ((pwd = getpwnam((char *)wbuf)) == NULL)
478 /* Copy the home directory */
481 wbufend = wbuf + MAXPATHLEN - 1;
483 memset(&mbs, 0, sizeof(mbs));
484 while (dc <= wbufend) {
485 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
486 if (clen == (size_t)-1 || clen == (size_t)-2) {
487 /* XXX See initial comment #2. */
488 wc = (unsigned char)*sc;
490 memset(&mbs, 0, sizeof(mbs));
492 if ((*dc++ = wc) == EOS) {
502 for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
507 /* Append the rest of the pattern */
511 if ((*b++ = *p++) == EOS) {
526 * The main glob() routine: compiles the pattern (optionally processing
527 * quotes), calls glob1() to do the real pattern matching, and finally
528 * sorts the list (unless unsorted operation is requested). Returns 0
529 * if things went well, nonzero if errors occurred.
532 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
533 const char *origpat) {
534 const Char *qpatnext;
537 Char *bufnext, c, patbuf[MAXPATHLEN];
539 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
540 if (qpatnext == NULL) {
542 return (GLOB_NOSPACE);
544 oldpathc = pglob->gl_pathc;
547 /* We don't need to check for buffer overflow any more. */
548 while ((c = *qpatnext++) != EOS) {
554 if (*qpatnext == EOS ||
555 g_strchr(qpatnext+1, RBRACKET) == NULL) {
556 *bufnext++ = LBRACKET;
566 *bufnext++ = CHAR(c);
567 if (*qpatnext == RANGE &&
568 (c = qpatnext[1]) != RBRACKET) {
570 *bufnext++ = CHAR(c);
573 } while ((c = *qpatnext++) != RBRACKET);
574 pglob->gl_flags |= GLOB_MAGCHAR;
578 pglob->gl_flags |= GLOB_MAGCHAR;
582 pglob->gl_flags |= GLOB_MAGCHAR;
583 /* collapse adjacent stars to one,
584 * to avoid exponential behavior
586 if (bufnext == patbuf || bufnext[-1] != M_ALL)
590 *bufnext++ = CHAR(c);
596 qprintf("glob0:", patbuf);
599 if ((err = glob1(patbuf, pglob, limit)) != 0)
603 return (globfinal(pglob, limit, oldpathc, origpat));
609 globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc,
610 const char *origpat) {
611 if (pglob->gl_pathc == oldpathc)
612 return (err_nomatch(pglob, limit, origpat));
614 if (!(pglob->gl_flags & GLOB_NOSORT))
615 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
616 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
622 compare(const void *p, const void *q)
624 return (strcoll(*(char **)p, *(char **)q));
628 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
630 Char pathbuf[MAXPATHLEN];
632 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
635 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
636 pattern, pglob, limit));
640 * The functions glob2 and glob3 are mutually recursive; there is one level
641 * of recursion for each segment in the pattern that contains one or more
645 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
646 glob_t *pglob, struct glob_limit *limit)
653 * Loop over pattern segments until end of pattern or until
654 * segment with meta character found.
656 for (anymeta = 0;;) {
657 if (*pattern == EOS) { /* End of pattern? */
659 if (g_lstat(pathbuf, &sb, pglob))
662 if ((pglob->gl_flags & GLOB_LIMIT) &&
663 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
665 return (GLOB_NOSPACE);
667 if ((pglob->gl_flags & GLOB_MARK) &&
668 UNPROT(pathend[-1]) != SEP &&
669 (S_ISDIR(sb.st_mode) ||
670 (S_ISLNK(sb.st_mode) &&
671 g_stat(pathbuf, &sb, pglob) == 0 &&
672 S_ISDIR(sb.st_mode)))) {
673 if (pathend + 1 > pathend_last) {
675 return (GLOB_NOSPACE);
681 return (globextend(pathbuf, pglob, limit, NULL));
684 /* Find end of next segment, copy tentatively to pathend. */
687 while (*p != EOS && UNPROT(*p) != SEP) {
690 if (q + 1 > pathend_last) {
692 return (GLOB_NOSPACE);
697 if (!anymeta) { /* No expansion, do next segment. */
700 while (UNPROT(*pattern) == SEP) {
701 if (pathend + 1 > pathend_last) {
703 return (GLOB_NOSPACE);
705 *pathend++ = *pattern++;
707 } else /* Need expansion, recurse. */
708 return (glob3(pathbuf, pathend, pathend_last, pattern,
715 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
716 Char *pattern, Char *restpattern,
717 glob_t *pglob, struct glob_limit *limit)
721 int err, too_long, saverrno, saverrno2;
722 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
724 struct dirent *(*readdirfunc)(DIR *);
726 if (pathend > pathend_last) {
728 return (GLOB_NOSPACE);
731 if (pglob->gl_errfunc != NULL &&
732 g_Ctoc(pathbuf, buf, sizeof(buf))) {
734 return (GLOB_NOSPACE);
739 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
740 if (errno == ENOENT || errno == ENOTDIR)
742 err = err_aborted(pglob, errno, buf);
750 /* pglob->gl_readdir takes a void *, fix this manually */
751 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
752 readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
754 readdirfunc = readdir;
757 /* Search directory for matching names. */
758 while ((dp = (*readdirfunc)(dirp)) != NULL) {
765 if ((pglob->gl_flags & GLOB_LIMIT) &&
766 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
772 /* Initial DOT must be matched literally. */
773 if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
777 memset(&mbs, 0, sizeof(mbs));
781 while (dc <= pathend_last) {
782 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
783 if (clen == (size_t)-1 || clen == (size_t)-2) {
784 /* XXX See initial comment #2. */
785 wc = (unsigned char)*sc;
787 memset(&mbs, 0, sizeof(mbs));
789 if ((*dc++ = wc) == EOS) {
795 if (too_long && (err = err_aborted(pglob, ENAMETOOLONG,
797 errno = ENAMETOOLONG;
800 if (too_long || !match(pathend, pattern, restpattern)) {
807 err = glob2(pathbuf, --dc, pathend_last, restpattern,
815 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
816 (*pglob->gl_closedir)(dirp);
824 if (dp == NULL && errno != 0 &&
825 (err = err_aborted(pglob, errno, buf)))
835 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
836 * add the new item, and update gl_pathc.
838 * This assumes the BSD realloc, which only copies the block when its size
839 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
842 * Return 0 if new item added, error code if memory couldn't be allocated.
844 * Invariant of the glob_t structure:
845 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
846 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
849 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
853 size_t i, newsize, len;
857 if ((pglob->gl_flags & GLOB_LIMIT) &&
858 pglob->gl_matchc > limit->l_path_lim) {
860 return (GLOB_NOSPACE);
863 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
864 /* realloc(NULL, newsize) is equivalent to malloc(newsize). */
865 pathv = realloc((void *)pglob->gl_pathv, newsize);
867 return (GLOB_NOSPACE);
869 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
870 /* first time around -- clear initial gl_offs items */
871 pathv += pglob->gl_offs;
872 for (i = pglob->gl_offs + 1; --i > 0; )
875 pglob->gl_pathv = pathv;
878 copy = strdup(origpat);
880 for (p = path; *p++ != EOS;)
882 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
883 if ((copy = malloc(len)) != NULL) {
884 if (g_Ctoc(path, copy, len)) {
887 return (GLOB_NOSPACE);
892 limit->l_string_cnt += strlen(copy) + 1;
893 if ((pglob->gl_flags & GLOB_LIMIT) &&
894 limit->l_string_cnt >= GLOB_LIMIT_STRING) {
897 return (GLOB_NOSPACE);
899 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
901 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
902 return (copy == NULL ? GLOB_NOSPACE : 0);
906 * pattern matching function for filenames. Each occurrence of the *
907 * pattern causes a recursion level.
910 match(Char *name, Char *pat, Char *patend)
912 int ok, negate_range;
914 struct xlocale_collate *table =
915 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
917 while (pat < patend) {
919 switch (c & M_MASK) {
924 if (match(name, pat, patend))
926 while (*name++ != EOS);
934 if ((k = *name++) == EOS)
936 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
938 while (((c = *pat++) & M_MASK) != M_END)
939 if ((*pat & M_MASK) == M_RNG) {
940 if (table->__collate_load_error ?
941 CHAR(c) <= CHAR(k) &&
942 CHAR(k) <= CHAR(pat[1]) :
943 __wcollate_range_cmp(CHAR(c),
945 __wcollate_range_cmp(CHAR(k),
951 if (ok == negate_range)
960 return (*name == EOS);
963 /* Free allocated data belonging to a glob_t structure. */
965 globfree(glob_t *pglob)
970 if (pglob->gl_pathv != NULL) {
971 pp = pglob->gl_pathv + pglob->gl_offs;
972 for (i = pglob->gl_pathc; i--; ++pp)
975 free(pglob->gl_pathv);
976 pglob->gl_pathv = NULL;
981 g_opendir(Char *str, glob_t *pglob)
983 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
988 if (g_Ctoc(str, buf, sizeof(buf))) {
989 errno = ENAMETOOLONG;
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 + MB_LEN_MAX - 1];
1005 if (g_Ctoc(fn, buf, sizeof(buf))) {
1006 errno = ENAMETOOLONG;
1009 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1010 return((*pglob->gl_lstat)(buf, sb));
1011 return (lstat(buf, sb));
1015 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1017 char buf[MAXPATHLEN + MB_LEN_MAX - 1];
1019 if (g_Ctoc(fn, buf, sizeof(buf))) {
1020 errno = ENAMETOOLONG;
1023 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1024 return ((*pglob->gl_stat)(buf, sb));
1025 return (stat(buf, sb));
1029 g_strchr(const Char *str, wchar_t ch)
1040 g_Ctoc(const Char *str, char *buf, size_t len)
1045 memset(&mbs, 0, sizeof(mbs));
1046 while (len >= MB_CUR_MAX) {
1047 clen = wcrtomb(buf, CHAR(*str), &mbs);
1048 if (clen == (size_t)-1) {
1049 /* XXX See initial comment #2. */
1050 *buf = (char)CHAR(*str);
1052 memset(&mbs, 0, sizeof(mbs));
1054 if (CHAR(*str) == EOS)
1064 err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) {
1066 * If there was no match we are going to append the origpat
1067 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
1068 * and the origpat did not contain any magic characters
1069 * GLOB_NOMAGIC is there just for compatibility with csh.
1071 if ((pglob->gl_flags & GLOB_NOCHECK) ||
1072 ((pglob->gl_flags & GLOB_NOMAGIC) &&
1073 !(pglob->gl_flags & GLOB_MAGCHAR)))
1074 return (globextend(NULL, pglob, limit, origpat));
1075 return (GLOB_NOMATCH);
1079 err_aborted(glob_t *pglob, int err, char *buf) {
1080 if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) ||
1081 (pglob->gl_flags & GLOB_ERR))
1082 return (GLOB_ABORTED);
1088 qprintf(const char *str, Char *s)
1092 (void)printf("%s\n", str);
1094 for (p = s; *p != EOS; p++)
1095 (void)printf("%c", (char)CHAR(*p));
1097 for (p = s; *p != EOS; p++)
1098 (void)printf("%c", (isprot(*p) ? '\\' : ' '));
1100 for (p = s; *p != EOS; p++)
1101 (void)printf("%c", (ismeta(*p) ? '_' : ' '));