]>
git.saurik.com Git - apple/libc.git/blob - gen/glob-fbsd.c
cc804960a316d7afd88bf87e153c9fb33df3a08a
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.28 2010/05/12 17:44:00 gordon Exp $");
39 #include "xlocale_private.h"
42 * glob(3) -- a superset of the one defined in POSIX 1003.2.
44 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
46 * Optional extra services, controlled by flags not defined by POSIX:
49 * Escaping convention: \ inhibits any special meaning the following
50 * character might have (except \ at end of string is retained).
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 * Number of matches in the current invocation of glob.
67 * Some notes on multibyte character support:
68 * 1. Patterns with illegal byte sequences match nothing - even if
69 * GLOB_NOCHECK is specified.
70 * 2. Illegal byte sequences in filenames are handled by treating them as
71 * single-byte characters with a value of the first byte of the sequence
73 * 3. State-dependent encodings are not currently supported.
76 #include <sys/param.h>
94 #define GLOB_LIMIT_STRING 65536 /* number of readdirs */
95 #define GLOB_LIMIT_STAT 128 /* number of stat system calls */
96 #define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */
97 #define GLOB_LIMIT_PATH 1024 /* number of path elements */
98 #define GLOB_LIMIT_BRACE 128 /* Number of brace calls */
119 #define UNDERSCORE '_'
127 #define M_QUOTE 0x8000000000ULL
128 #define M_PROTECT 0x4000000000ULL
129 #define M_MASK 0xffffffffffULL
130 #define M_CHAR 0x00ffffffffULL
132 typedef uint_fast64_t Char
;
137 #define M_PROTECT 0x40
146 #define CHAR(c) ((Char)((c)&M_CHAR))
147 #define META(c) ((Char)((c)|M_QUOTE))
148 #define M_ALL META('*')
149 #define M_END META(']')
150 #define M_NOT META('!')
151 #define M_ONE META('?')
152 #define M_RNG META('-')
153 #define M_SET META('[')
154 #define ismeta(c) (((c)&M_QUOTE) != 0)
157 #define compare __gl_compare
158 #define g_Ctoc __gl_g_Ctoc
159 #define g_strchr __gl_g_strchr
160 #define globextend __gl_globextend
161 #define globtilde __gl_globtilde
162 #define match __gl_match
163 __private_extern__
int compare(const void *, const void *);
164 __private_extern__
int g_Ctoc(const Char
*, char *, size_t, locale_t
);
165 __private_extern__
const Char
*g_strchr(const Char
*, wchar_t);
166 __private_extern__
int globextend(const Char
*, glob_t
*, struct glob_limit
*, locale_t
);
167 __private_extern__
const Char
*
168 globtilde(const Char
*, Char
*, size_t, glob_t
*);
169 __private_extern__
int match(Char
*, Char
*, Char
*, locale_t
);
172 static int g_lstat(Char
*, struct stat
*, glob_t
*, locale_t
);
173 static DIR *g_opendir(Char
*, glob_t
*, locale_t
);
175 static Char
*g_strcat(Char
*, const Char
*);
177 static int g_stat(Char
*, struct stat
*, glob_t
*, locale_t
);
178 static int glob0(const Char
*, glob_t
*, struct glob_limit
*, locale_t
);
179 static int glob1(Char
*, glob_t
*, struct glob_limit
*, locale_t
);
180 static int glob2(Char
*, Char
*, Char
*, Char
*, glob_t
*, struct glob_limit
*, locale_t
);
181 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*, glob_t
*, struct glob_limit
*, locale_t
);
182 static int globexp1(const Char
*, glob_t
*, struct glob_limit
*, locale_t
);
183 static int globexp2(const Char
*, const Char
*, glob_t
*, int *, struct glob_limit
*, locale_t
);
185 static void qprintf(const char *, Char
*);
189 __glob(const char *pattern
, glob_t
*pglob
)
192 struct glob_limit limit
= { 0, 0, 0, 0 };
193 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
], prot
;
197 locale_t loc
= __current_locale();
198 int mb_cur_max
= MB_CUR_MAX_L(loc
);
201 if (!(pglob
->gl_flags
& GLOB_APPEND
)) {
203 pglob
->gl_pathv
= NULL
;
204 if (!(pglob
->gl_flags
& GLOB_DOOFFS
))
207 pglob
->gl_matchc
= 0;
210 bufend
= bufnext
+ MAXPATHLEN
- 1;
211 if (pglob
->gl_flags
& GLOB_NOESCAPE
) {
212 memset(&mbs
, 0, sizeof(mbs
));
213 while (bufend
- bufnext
>= mb_cur_max
) {
214 clen
= mbrtowc_l(&wc
, patnext
, MB_LEN_MAX
, &mbs
, loc
);
215 if (clen
== (size_t)-1 || clen
== (size_t)-2)
216 return (GLOB_NOMATCH
);
223 /* Protect the quoted characters. */
224 memset(&mbs
, 0, sizeof(mbs
));
225 while (bufend
- bufnext
>= mb_cur_max
) {
226 if (*patnext
== QUOTE
) {
227 if (*++patnext
== EOS
) {
228 *bufnext
++ = QUOTE
| M_PROTECT
;
234 clen
= mbrtowc_l(&wc
, patnext
, MB_LEN_MAX
, &mbs
, loc
);
235 if (clen
== (size_t)-1 || clen
== (size_t)-2)
236 return (GLOB_NOMATCH
);
239 *bufnext
++ = wc
| prot
;
245 if (pglob
->gl_flags
& GLOB_BRACE
)
246 return globexp1(patbuf
, pglob
, &limit
, loc
);
248 return glob0(patbuf
, pglob
, &limit
, loc
);
252 glob(const char *pattern
, int flags
, int (*errfunc
)(const char *, int), glob_t
*pglob
)
255 pglob
->gl_flags
= flags
& ~(GLOB_MAGCHAR
| _GLOB_ERR_BLOCK
);
256 #else /* !__BLOCKS__ */
257 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
258 #endif /* __BLOCKS__ */
259 pglob
->gl_errfunc
= errfunc
;
260 return __glob(pattern
, pglob
);
265 glob_b(const char *pattern
, int flags
, int (^errblk
)(const char *, int), glob_t
*pglob
)
267 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
268 pglob
->gl_flags
|= _GLOB_ERR_BLOCK
;
269 pglob
->gl_errblk
= errblk
;
270 return __glob(pattern
, pglob
);
272 #endif /* __BLOCKS__ */
275 * Expand recursively a glob {} pattern. When there is no more expansion
276 * invoke the standard globbing routine to glob the rest of the magic
280 globexp1(const Char
*pattern
, glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
282 const Char
* ptr
= pattern
;
285 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
286 limit
->l_brace
++ >= GLOB_LIMIT_BRACE
) {
291 /* Protect a single {}, for find(1), like csh */
292 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
293 return glob0(pattern
, pglob
, limit
, loc
);
295 while ((ptr
= g_strchr(ptr
, LBRACE
)) != NULL
)
296 if (!globexp2(ptr
, pattern
, pglob
, &rv
, limit
, loc
))
299 return glob0(pattern
, pglob
, limit
, loc
);
304 * Recursive brace globbing helper. Tries to expand a single brace.
305 * If it succeeds then it invokes globexp1 with the new pattern.
306 * If it fails then it tries to glob the rest of the pattern and returns.
309 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
, int *rv
, struct glob_limit
*limit
, locale_t loc
)
313 const Char
*pe
, *pm
, *pm1
, *pl
;
314 Char patbuf
[MAXPATHLEN
];
316 /* copy part up to the brace */
317 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
322 /* Find the balanced brace */
323 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
324 if (*pe
== LBRACKET
) {
325 /* Ignore everything between [] */
326 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
330 * We could not find a matching RBRACKET.
331 * Ignore and just look for RBRACE
336 else if (*pe
== LBRACE
)
338 else if (*pe
== RBRACE
) {
344 /* Non matching braces; just glob the pattern */
345 if (i
!= 0 || *pe
== EOS
) {
346 *rv
= glob0(patbuf
, pglob
, limit
, loc
);
350 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
353 /* Ignore everything between [] */
354 for (pm1
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
358 * We could not find a matching RBRACKET.
359 * Ignore and just look for RBRACE
376 if (i
&& *pm
== COMMA
)
379 /* Append the current string */
380 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
383 * Append the rest of the pattern after the
386 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
389 /* Expand the current pattern */
391 qprintf("globexp2:", patbuf
);
393 *rv
= globexp1(patbuf
, pglob
, limit
, loc
);
395 /* move after the comma, to the next string */
409 #ifndef BUILDING_VARIANT
411 * expand tilde from the passwd file.
413 __private_extern__
const Char
*
414 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patbuf_len
, glob_t
*pglob
)
421 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
425 * Copy up to the end of the string or /
427 eb
= &patbuf
[patbuf_len
- 1];
428 for (p
= pattern
+ 1, h
= (char *) patbuf
;
429 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
434 if (((char *) patbuf
)[0] == EOS
) {
436 * handle a plain ~ or ~/ by expanding $HOME first (iff
437 * we're not running setuid or setgid) and then trying
440 if (issetugid() != 0 ||
441 (h
= getenv("HOME")) == NULL
) {
442 if (((h
= getlogin()) != NULL
&&
443 (pwd
= getpwnam(h
)) != NULL
) ||
444 (pwd
= getpwuid(getuid())) != NULL
)
454 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
460 /* Copy the home directory */
461 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
464 /* Append the rest of the pattern */
465 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
471 #endif /* BUILDING_VARIANT */
475 * The main glob() routine: compiles the pattern (optionally processing
476 * quotes), calls glob1() to do the real pattern matching, and finally
477 * sorts the list (unless unsorted operation is requested). Returns 0
478 * if things went well, nonzero if errors occurred.
481 glob0(const Char
*pattern
, glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
483 const Char
*qpatnext
;
486 Char
*bufnext
, c
, patbuf
[MAXPATHLEN
];
488 qpatnext
= globtilde(pattern
, patbuf
, MAXPATHLEN
, pglob
);
489 oldpathc
= pglob
->gl_pathc
;
492 /* We don't need to check for buffer overflow any more. */
493 while ((c
= *qpatnext
++) != EOS
) {
495 *bufnext
++ = CHAR(c
);
503 if (*qpatnext
== EOS
||
504 g_strchr(qpatnext
+1, RBRACKET
) == NULL
) {
505 *bufnext
++ = LBRACKET
;
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
, pglob
, limit
, loc
)) != 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
, limit
, loc
));
563 return(GLOB_NOMATCH
);
565 if (!(pglob
->gl_flags
& GLOB_NOSORT
))
566 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
567 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
571 #ifndef BUILDING_VARIANT
572 __private_extern__
int
573 compare(const void *p
, const void *q
)
575 return(strcoll(*(char **)p
, *(char **)q
));
577 #endif /* BUILDING_VARIANT */
580 glob1(Char
*pattern
, glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
582 Char pathbuf
[MAXPATHLEN
];
584 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
587 return(glob2(pathbuf
, pathbuf
, pathbuf
+ MAXPATHLEN
- 1,
588 pattern
, pglob
, limit
, loc
));
592 * The functions glob2 and glob3 are mutually recursive; there is one level
593 * of recursion for each segment in the pattern that contains one or more
597 glob2(Char
*pathbuf
, Char
*pathend
, Char
*pathend_last
, Char
*pattern
,
598 glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
605 * Loop over pattern segments until end of pattern or until
606 * segment with meta character found.
608 for (anymeta
= 0;;) {
609 if (*pattern
== EOS
) { /* End of pattern? */
611 if (g_lstat(pathbuf
, &sb
, pglob
, loc
))
614 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
615 limit
->l_stat
++ >= GLOB_LIMIT_STAT
) {
621 if (((pglob
->gl_flags
& GLOB_MARK
) &&
622 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
623 || (S_ISLNK(sb
.st_mode
) &&
624 (g_stat(pathbuf
, &sb
, pglob
, loc
) == 0) &&
625 S_ISDIR(sb
.st_mode
)))) {
626 if (pathend
+ 1 > pathend_last
)
627 return (GLOB_ABORTED
);
632 return(globextend(pathbuf
, pglob
, limit
, loc
));
635 /* Find end of next segment, copy tentatively to pathend. */
638 while (*p
!= EOS
&& *p
!= SEP
) {
641 if (q
+ 1 > pathend_last
)
642 return (GLOB_ABORTED
);
646 if (!anymeta
) { /* No expansion, do next segment. */
649 while (*pattern
== SEP
) {
650 if (pathend
+ 1 > pathend_last
)
651 return (GLOB_ABORTED
);
652 *pathend
++ = *pattern
++;
654 } else /* Need expansion, recurse. */
655 return(glob3(pathbuf
, pathend
, pathend_last
, pattern
, p
,
662 glob3(Char
*pathbuf
, Char
*pathend
, Char
*pathend_last
,
663 Char
*pattern
, Char
*restpattern
,
664 glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
669 char buf
[MAXPATHLEN
];
672 * The readdirfunc declaration can't be prototyped, because it is
673 * assigned, below, to two functions which are prototyped in glob.h
674 * and dirent.h as taking pointers to differently typed opaque
677 struct dirent
*(*readdirfunc
)();
679 if (pathend
> pathend_last
)
680 return (GLOB_ABORTED
);
684 if ((dirp
= g_opendir(pathbuf
, pglob
, loc
)) == NULL
) {
685 /* TODO: don't call for ENOENT or ENOTDIR? */
686 if (pglob
->gl_errfunc
) {
687 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
), loc
))
688 return (GLOB_ABORTED
);
690 if (pglob
->gl_flags
& _GLOB_ERR_BLOCK
) {
691 if (pglob
->gl_errblk(buf
, errno
))
692 return (GLOB_ABORTED
);
694 #endif /* __BLOCKS__ */
695 if (pglob
->gl_errfunc(buf
, errno
))
696 return (GLOB_ABORTED
);
698 if (pglob
->gl_flags
& GLOB_ERR
)
699 return (GLOB_ABORTED
);
705 /* Search directory for matching names. */
706 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
707 readdirfunc
= pglob
->gl_readdir
;
709 readdirfunc
= readdir
;
710 while ((dp
= (*readdirfunc
)(dirp
))) {
717 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
718 limit
->l_readdir
++ >= GLOB_LIMIT_READDIR
) {
725 /* Initial DOT must be matched literally. */
726 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
728 memset(&mbs
, 0, sizeof(mbs
));
731 while (dc
< pathend_last
) {
732 clen
= mbrtowc_l(&wc
, sc
, MB_LEN_MAX
, &mbs
, loc
);
733 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
736 memset(&mbs
, 0, sizeof(mbs
));
738 if ((*dc
++ = wc
) == EOS
)
742 if (!match(pathend
, pattern
, restpattern
, loc
)) {
746 err
= glob2(pathbuf
, --dc
, pathend_last
, restpattern
,
752 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
753 (*pglob
->gl_closedir
)(dirp
);
760 #ifndef BUILDING_VARIANT
762 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
763 * add the new item, and update gl_pathc.
765 * This assumes the BSD realloc, which only copies the block when its size
766 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
769 * Return 0 if new item added, error code if memory couldn't be allocated.
771 * Invariant of the glob_t structure:
772 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
773 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
775 __private_extern__
int
776 globextend(const Char
*path
, glob_t
*pglob
, struct glob_limit
*limit
, locale_t loc
)
779 size_t i
, newsize
, len
;
783 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
784 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
785 newsize
> GLOB_LIMIT_PATH
* sizeof(*pathv
))
787 pathv
= pglob
->gl_pathv
?
788 realloc((char *)pglob
->gl_pathv
, newsize
) :
791 if (pglob
->gl_pathv
) {
792 free(pglob
->gl_pathv
);
793 pglob
->gl_pathv
= NULL
;
795 return(GLOB_NOSPACE
);
798 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
799 /* first time around -- clear initial gl_offs items */
800 pathv
+= pglob
->gl_offs
;
801 for (i
= pglob
->gl_offs
+ 1; --i
> 0; )
804 pglob
->gl_pathv
= pathv
;
806 for (p
= path
; *p
++;)
808 len
= MB_CUR_MAX_L(loc
) * (size_t)(p
- path
); /* XXX overallocation */
809 limit
->l_string
+= len
;
810 if ((copy
= malloc(len
)) != NULL
) {
811 if (g_Ctoc(path
, copy
, len
, loc
)) {
813 return (GLOB_NOSPACE
);
815 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
817 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
819 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
820 (newsize
+ limit
->l_string
) >= GLOB_LIMIT_STRING
)
823 return(copy
== NULL
? GLOB_NOSPACE
: 0);
830 * pattern matching function for filenames. Each occurrence of the *
831 * pattern causes a recursion level.
833 __private_extern__
int
834 match(Char
*name
, Char
*pat
, Char
*patend
, locale_t loc
)
836 int ok
, negate_range
;
839 while (pat
< patend
) {
841 switch (c
& M_MASK
) {
846 if (match(name
, pat
, patend
, loc
))
848 while (*name
++ != EOS
);
856 if ((k
= *name
++) == EOS
)
858 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
860 while (((c
= *pat
++) & M_MASK
) != M_END
)
861 if ((*pat
& M_MASK
) == M_RNG
) {
862 if (loc
->__collate_load_error
?
863 CHAR(c
) <= CHAR(k
) && CHAR(k
) <= CHAR(pat
[1]) :
864 __collate_range_cmp(CHAR(c
), CHAR(k
), loc
) <= 0
865 && __collate_range_cmp(CHAR(k
), CHAR(pat
[1]), loc
) <= 0
871 if (ok
== negate_range
)
880 return(*name
== EOS
);
883 /* Free allocated data belonging to a glob_t structure. */
885 globfree(glob_t
*pglob
)
890 if (pglob
->gl_pathv
!= NULL
) {
891 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
892 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
895 free(pglob
->gl_pathv
);
896 pglob
->gl_pathv
= NULL
;
899 #endif /* !BUILDING_VARIANT */
902 g_opendir(Char
*str
, glob_t
*pglob
, locale_t loc
)
904 char buf
[MAXPATHLEN
];
909 if (g_Ctoc(str
, buf
, sizeof(buf
), loc
))
913 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
914 return((*pglob
->gl_opendir
)(buf
));
916 return(opendir(buf
));
920 g_lstat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
, locale_t loc
)
922 char buf
[MAXPATHLEN
];
924 if (g_Ctoc(fn
, buf
, sizeof(buf
), loc
)) {
925 errno
= ENAMETOOLONG
;
928 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
929 return((*pglob
->gl_lstat
)(buf
, sb
));
930 return(lstat(buf
, sb
));
934 g_stat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
, locale_t loc
)
936 char buf
[MAXPATHLEN
];
938 if (g_Ctoc(fn
, buf
, sizeof(buf
), loc
)) {
939 errno
= ENAMETOOLONG
;
942 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
943 return((*pglob
->gl_stat
)(buf
, sb
));
944 return(stat(buf
, sb
));
947 #ifndef BUILDING_VARIANT
948 __private_extern__
const Char
*
949 g_strchr(const Char
*str
, wchar_t ch
)
959 __private_extern__
int
960 g_Ctoc(const Char
*str
, char *buf
, size_t len
, locale_t loc
)
964 int mb_cur_max
= MB_CUR_MAX_L(loc
);
966 memset(&mbs
, 0, sizeof(mbs
));
967 while (len
>= mb_cur_max
) {
968 clen
= wcrtomb_l(buf
, *str
, &mbs
, loc
);
969 if (clen
== (size_t)-1)
982 qprintf(const char *str
, Char
*s
)
986 (void)printf("%s:\n", str
);
988 (void)printf("%c", CHAR(*p
));
991 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
994 (void)printf("%c", ismeta(*p
) ? '_' : ' ');
998 #endif /* !BUILDING_VARIANT */