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
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.28 2010/05/12 17:44:00 gordon Exp $");
44 * glob(3) -- a superset of the one defined in POSIX 1003.2.
46 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
48 * Optional extra services, controlled by flags not defined by POSIX:
51 * Escaping convention: \ inhibits any special meaning the following
52 * character might have (except \ at end of string is retained).
54 * Set in gl_flags if pattern contained a globbing character.
56 * Same as GLOB_NOCHECK, but it will only append pattern if it did
57 * not contain any magic characters. [Used in csh style globbing]
59 * Use alternately specified directory access functions.
61 * expand ~user/foo to the /home/dir/of/user/foo
63 * expand {1,2}{a,b} to 1a 1b 2a 2b
65 * Number of matches in the current invocation of glob.
69 * Some notes on multibyte character support:
70 * 1. Patterns with illegal byte sequences match nothing - even if
71 * GLOB_NOCHECK is specified.
72 * 2. Illegal byte sequences in filenames are handled by treating them as
73 * single-byte characters with a value of the first byte of the sequence
75 * 3. State-dependent encodings are not currently supported.
78 #include <sys/param.h>
98 #define Cchar(c) (ignore_case_with_glob ? towlower (c) : (c))
102 #define MAXPATHLEN 8192
118 #define UNDERSCORE '_'
126 #define M_QUOTE 0x40000000U
127 #define M_PROTECT 0x20000000U
128 #define M_MASK 0x70ffffffU
129 #define M_COLL_MASK 0x700000ffU
130 #define M_CHAR 0x00ffffffU
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 M_NAMED META(':')
155 #define M_EQUIV META('=')
156 #define M_COLL(_ccnt) META('.' | ((_ccnt) << 8))
157 #define M_COLL_P(_c) (((_c) & M_COLL_MASK) == META('.'))
158 #define M_COLL_CNT(_c) (((_c) & ~M_COLL_MASK) >> 8)
159 #define ismeta(c) (((c)&M_QUOTE) != 0)
161 static int compare(const void *, const void *);
162 static int g_Ctoc(const Char
*, char *, size_t);
163 static int g_lstat(Char
*, struct stat
*, glob_t
*);
164 static DIR *g_opendir(Char
*, glob_t
*);
165 static const Char
*g_strchr(const Char
*, wint_t);
167 static Char
*g_strcat(Char
*, const Char
*);
169 static int g_stat(Char
*, struct stat
*, glob_t
*);
170 static int glob0(const Char
*, glob_t
*, size_t *);
171 static int glob1(Char
*, glob_t
*, size_t *);
172 static int glob2(Char
*, Char
*, Char
*, Char
*, glob_t
*, size_t *);
173 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*, glob_t
*, size_t *);
174 static int globextend(const Char
*, glob_t
*, size_t *);
176 globtilde(const Char
*, Char
*, size_t, glob_t
*);
177 static int globexp1(const Char
*, glob_t
*, size_t *);
178 static int globexp2(const Char
*, const Char
*, glob_t
*, int *, size_t *);
179 static int match(Char
*, Char
*, Char
*);
181 static void qprintf(const char *, Char
*);
184 /* Return value is either EOS, COLON, DOT, EQUALS, or LBRACKET if no class
187 check_classes_expr(const Char
*&cptr
, wint_t *classbuf
= NULL
,
188 size_t classbufsize
= 0)
190 const Char
*ctype
= NULL
;
192 if (*cptr
== LBRACKET
&&
193 (cptr
[1] == COLON
|| cptr
[1] == DOT
|| cptr
[1] == EQUALS
)) {
195 while (*++cptr
!= EOS
&&
196 (*cptr
!= *ctype
|| cptr
[1] != RBRACKET
))
201 const Char
*class_p
= ctype
+ 1;
202 size_t clen
= cptr
- class_p
;
204 if (clen
< classbufsize
)
205 *wcipncpy (classbuf
, class_p
, clen
) = '\0';
209 cptr
++; /* Advance cptr to closing RBRACKET of class expr */
211 return ctype
? *ctype
: LBRACKET
;
215 glob(const char *__restrict pattern
, int flags
, int (*errfunc
)(const char *, int), glob_t
*__restrict pglob
)
219 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
], prot
;
225 if (!(flags
& GLOB_APPEND
)) {
227 pglob
->gl_pathv
= NULL
;
228 if (!(flags
& GLOB_DOOFFS
))
231 if (flags
& GLOB_LIMIT
) {
232 limit
= pglob
->gl_matchc
;
237 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
238 pglob
->gl_errfunc
= errfunc
;
239 pglob
->gl_matchc
= 0;
242 bufend
= bufnext
+ MAXPATHLEN
- 1;
243 if (flags
& GLOB_NOESCAPE
) {
244 memset(&mbs
, 0, sizeof(mbs
));
245 while (bufend
- bufnext
>= MB_CUR_MAX
) {
246 clen
= mbrtowi(&wc
, patnext
, MB_LEN_MAX
, &mbs
);
247 if (clen
== (size_t)-1 || clen
== (size_t)-2)
248 return (GLOB_NOMATCH
);
255 /* Protect the quoted characters. */
256 memset(&mbs
, 0, sizeof(mbs
));
257 while (bufend
- bufnext
>= MB_CUR_MAX
) {
258 if (*patnext
== QUOTE
) {
259 if (*++patnext
== EOS
) {
260 *bufnext
++ = QUOTE
| M_PROTECT
;
266 clen
= mbrtowi(&wc
, patnext
, MB_LEN_MAX
, &mbs
);
267 if (clen
== (size_t)-1 || clen
== (size_t)-2)
268 return (GLOB_NOMATCH
);
271 *bufnext
++ = wc
| prot
;
277 if (flags
& GLOB_BRACE
)
278 return globexp1(patbuf
, pglob
, &limit
);
280 return glob0(patbuf
, pglob
, &limit
);
284 * Expand recursively a glob {} pattern. When there is no more expansion
285 * invoke the standard globbing routine to glob the rest of the magic
289 globexp1(const Char
*pattern
, glob_t
*pglob
, size_t *limit
)
291 const Char
* ptr
= pattern
;
294 /* Protect a single {}, for find(1), like csh */
295 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
296 return glob0(pattern
, pglob
, limit
);
298 while ((ptr
= g_strchr(ptr
, LBRACE
)) != NULL
)
299 if (!globexp2(ptr
, pattern
, pglob
, &rv
, limit
))
302 return glob0(pattern
, pglob
, limit
);
307 * Recursive brace globbing helper. Tries to expand a single brace.
308 * If it succeeds then it invokes globexp1 with the new pattern.
309 * If it fails then it tries to glob the rest of the pattern and returns.
312 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
, int *rv
, size_t *limit
)
316 const Char
*pe
, *pm
, *pm1
, *pl
;
317 Char patbuf
[MAXPATHLEN
];
319 /* copy part up to the brace */
320 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
325 /* Find the balanced brace */
326 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
327 if (*pe
== LBRACKET
) {
328 /* Ignore everything between [] */
329 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++) {
330 if (check_classes_expr (pe
) == EOS
)
335 * We could not find a matching RBRACKET.
336 * Ignore and just look for RBRACE
341 else if (*pe
== LBRACE
)
343 else if (*pe
== RBRACE
) {
349 /* Non matching braces; just glob the pattern */
350 if (i
!= 0 || *pe
== EOS
) {
351 *rv
= glob0(patbuf
, pglob
, limit
);
355 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
358 /* Ignore everything between [] */
359 for (pm1
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++) {
360 if (check_classes_expr (pm
) == EOS
)
365 * We could not find a matching RBRACKET.
366 * Ignore and just look for RBRACE
383 if (i
&& *pm
== COMMA
)
386 /* Append the current string */
387 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
390 * Append the rest of the pattern after the
393 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
396 /* Expand the current pattern */
398 qprintf("globexp2:", patbuf
);
400 *rv
= globexp1(patbuf
, pglob
, limit
);
402 /* 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 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
431 * Copy up to the end of the string or /
433 eb
= &patbuf
[patbuf_len
- 1];
434 for (p
= pattern
+ 1, h
= (char *) patbuf
;
435 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
440 if (((char *) patbuf
)[0] == EOS
) {
442 * handle a plain ~ or ~/ by expanding $HOME first (iff
443 * we're not running setuid or setgid) and then trying
446 if (issetugid() != 0 ||
447 (h
= getenv("HOME")) == NULL
) {
448 if (((h
= getlogin()) != NULL
&&
449 (pwd
= getpwnam(h
)) != NULL
) ||
450 (pwd
= getpwuid(getuid())) != NULL
)
460 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
466 /* Copy the home directory */
467 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
470 /* Append the rest of the pattern */
471 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
479 * The main glob() routine: compiles the pattern (optionally processing
480 * quotes), calls glob1() to do the real pattern matching, and finally
481 * sorts the list (unless unsorted operation is requested). Returns 0
482 * if things went well, nonzero if errors occurred.
485 glob0(const Char
*pattern
, glob_t
*pglob
, size_t *limit
)
487 const Char
*qpatnext
, *qpatrbsrch
;
490 Char
*bufnext
, c
, patbuf
[MAXPATHLEN
];
492 qpatnext
= globtilde(pattern
, patbuf
, MAXPATHLEN
, pglob
);
493 oldpathc
= pglob
->gl_pathc
;
496 /* We don't need to check for buffer overflow any more. */
497 while ((c
= *qpatnext
++) != EOS
) {
503 for (qpatrbsrch
= qpatnext
;
504 *qpatrbsrch
!= RBRACKET
&& *qpatrbsrch
!= EOS
;
506 if (check_classes_expr (qpatrbsrch
) == EOS
)
509 if (*qpatrbsrch
== EOS
) {
510 *bufnext
++ = LBRACKET
;
523 ctype
= check_classes_expr(--qpatnext
, wclass
,
526 if (ctype
== COLON
) {
530 /* No worries, char classes are
532 wcitoascii (cclass
, wclass
);
533 if ((type
= wctype (cclass
))) {
534 *bufnext
++ = M_NAMED
;
535 *bufnext
++ = CHAR (type
);
539 if (ctype
== EQUALS
) {
540 if (wclass
[0] && !wclass
[1]) {
541 *bufnext
++ = M_EQUIV
;
542 *bufnext
++ = CHAR (wclass
[0]);
547 is_unicode_coll_elem (wclass
)) {
549 M_COLL (wcilen (wclass
));
550 wint_t *wcp
= wclass
;
551 while ((*bufnext
++ = *wcp
++))
553 --bufnext
; /* drop NUL */
555 *bufnext
++ = CHAR(c
);
556 if (*qpatnext
== RANGE
&&
557 (c
= qpatnext
[1]) != RBRACKET
) {
560 ctype
= check_classes_expr(++qpatnext
,
563 is_unicode_coll_elem (wclass
)) {
565 M_COLL (wcilen (wclass
));
566 wint_t *wcp
= wclass
;
567 while ((*bufnext
++ = *wcp
++))
569 --bufnext
; /* drop NUL */
571 *bufnext
++ = CHAR(c
);
574 } while ((c
= *qpatnext
++) != RBRACKET
);
575 pglob
->gl_flags
|= GLOB_MAGCHAR
;
579 pglob
->gl_flags
|= GLOB_MAGCHAR
;
583 pglob
->gl_flags
|= GLOB_MAGCHAR
;
584 /* collapse adjacent stars to one,
585 * to avoid exponential behavior
587 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
591 *bufnext
++ = CHAR(c
);
597 qprintf("glob0:", patbuf
);
600 if ((err
= glob1(patbuf
, pglob
, limit
)) != 0)
604 * If there was no match we are going to append the pattern
605 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
606 * and the pattern did not contain any magic characters
607 * GLOB_NOMAGIC is there just for compatibility with csh.
609 if (pglob
->gl_pathc
== oldpathc
) {
610 if (((pglob
->gl_flags
& GLOB_NOCHECK
) ||
611 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
612 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
613 return(globextend(pattern
, pglob
, limit
));
615 return(GLOB_NOMATCH
);
617 if (!(pglob
->gl_flags
& GLOB_NOSORT
))
618 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
619 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
624 compare(const void *p
, const void *q
)
626 return(strcoll(*(char **)p
, *(char **)q
));
630 glob1(Char
*pattern
, glob_t
*pglob
, size_t *limit
)
632 Char pathbuf
[MAXPATHLEN
];
634 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
637 return(glob2(pathbuf
, pathbuf
, pathbuf
+ MAXPATHLEN
- 1,
638 pattern
, pglob
, limit
));
642 * The functions glob2 and glob3 are mutually recursive; there is one level
643 * of recursion for each segment in the pattern that contains one or more
647 glob2(Char
*pathbuf
, Char
*pathend
, Char
*pathend_last
, Char
*pattern
,
648 glob_t
*pglob
, size_t *limit
)
655 * Loop over pattern segments until end of pattern or until
656 * segment with meta character found.
658 for (anymeta
= 0;;) {
659 if (*pattern
== EOS
) { /* End of pattern? */
661 if (g_lstat(pathbuf
, &sb
, pglob
))
664 if (((pglob
->gl_flags
& GLOB_MARK
) &&
665 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
666 || (S_ISLNK(sb
.st_mode
) &&
667 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
668 S_ISDIR(sb
.st_mode
)))) {
669 if (pathend
+ 1 > pathend_last
)
670 return (GLOB_ABORTED
);
675 return(globextend(pathbuf
, pglob
, limit
));
678 /* Find end of next segment, copy tentatively to pathend. */
681 while (*p
!= EOS
&& *p
!= SEP
) {
684 if (q
+ 1 > pathend_last
)
685 return (GLOB_ABORTED
);
689 if (!anymeta
) { /* No expansion, do next segment. */
692 while (*pattern
== SEP
) {
693 if (pathend
+ 1 > pathend_last
)
694 return (GLOB_ABORTED
);
695 *pathend
++ = *pattern
++;
697 } else /* Need expansion, recurse. */
698 return(glob3(pathbuf
, pathend
, pathend_last
, pattern
, p
,
705 glob3(Char
*pathbuf
, Char
*pathend
, Char
*pathend_last
,
706 Char
*pattern
, Char
*restpattern
,
707 glob_t
*pglob
, size_t *limit
)
712 char buf
[MAXPATHLEN
];
715 * The readdirfunc declaration can't be prototyped, because it is
716 * assigned, below, to two functions which are prototyped in glob.h
717 * and dirent.h as taking pointers to differently typed opaque
719 * CYGWIN: Needs prototype and subsequently wild casting to avoid
722 struct dirent
*(*readdirfunc
)(void *);
724 if (pathend
> pathend_last
)
725 return (GLOB_ABORTED
);
729 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
730 /* TODO: don't call for ENOENT or ENOTDIR? */
731 if (pglob
->gl_errfunc
) {
732 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
733 return (GLOB_ABORTED
);
734 if (pglob
->gl_errfunc(buf
, errno
) ||
735 pglob
->gl_flags
& GLOB_ERR
)
736 return (GLOB_ABORTED
);
743 /* Search directory for matching names. */
744 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
745 readdirfunc
= pglob
->gl_readdir
;
747 readdirfunc
= (dirent
*(*)(void*)) readdir
;
748 while ((dp
= (*readdirfunc
)(dirp
))) {
755 /* Initial DOT must be matched literally. */
756 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
758 memset(&mbs
, 0, sizeof(mbs
));
761 while (dc
< pathend_last
) {
762 clen
= mbrtowi(&wc
, sc
, MB_LEN_MAX
, &mbs
);
763 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
766 memset(&mbs
, 0, sizeof(mbs
));
768 if ((*dc
++ = wc
) == EOS
)
772 if (!match(pathend
, pattern
, restpattern
)) {
776 err
= glob2(pathbuf
, --dc
, pathend_last
, restpattern
,
782 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
783 (*pglob
->gl_closedir
)(dirp
);
791 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
792 * add the new item, and update gl_pathc.
794 * This assumes the BSD realloc, which only copies the block when its size
795 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
798 * Return 0 if new item added, error code if memory couldn't be allocated.
800 * Invariant of the glob_t structure:
801 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
802 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
805 globextend(const Char
*path
, glob_t
*pglob
, size_t *limit
)
808 size_t i
, newsize
, len
;
812 if (*limit
&& pglob
->gl_pathc
> *limit
) {
814 return (GLOB_NOSPACE
);
817 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
818 pathv
= pglob
->gl_pathv
?
819 (char **) realloc((char *)pglob
->gl_pathv
, newsize
) :
820 (char **) malloc(newsize
);
822 if (pglob
->gl_pathv
) {
823 free(pglob
->gl_pathv
);
824 pglob
->gl_pathv
= NULL
;
826 return(GLOB_NOSPACE
);
829 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
830 /* first time around -- clear initial gl_offs items */
831 pathv
+= pglob
->gl_offs
;
832 for (i
= pglob
->gl_offs
+ 1; --i
> 0; )
835 pglob
->gl_pathv
= pathv
;
837 for (p
= path
; *p
++;)
839 len
= MB_CUR_MAX
* (size_t)(p
- path
); /* XXX overallocation */
840 if ((copy
= (char *) malloc(len
)) != NULL
) {
841 if (g_Ctoc(path
, copy
, len
)) {
843 return (GLOB_NOSPACE
);
845 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
847 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
848 return(copy
== NULL
? GLOB_NOSPACE
: 0);
852 * pattern matching function for filenames. Each occurrence of the *
853 * pattern causes a recursion level.
856 match(Char
*name
, Char
*pat
, Char
*patend
)
858 int ok
, negate_range
;
862 while (pat
< patend
) {
864 switch (*c
& M_MASK
) {
869 if (match(name
, pat
, patend
))
871 while (*name
++ != EOS
);
879 if (*(k
= name
) == EOS
)
881 k_len
= next_unicode_char (k
);
883 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
885 while ((*(c
= pat
++) & M_MASK
) != M_END
) {
886 size_t len1
= 1, len2
= 1;
888 if ((*c
& M_MASK
) == M_NAMED
) {
889 if (iswctype (*k
, *pat
++))
893 if ((*c
& M_MASK
) == M_EQUIV
) {
894 if (is_unicode_equiv (*k
, *pat
++))
899 len1
= M_COLL_CNT(*c
);
903 if ((*pat
& M_MASK
) == M_RNG
) {
904 if (M_COLL_P(pat
[1]))
905 len2
= M_COLL_CNT(*++pat
);
907 if ((!__get_current_collate_locale ()->win_locale
[0]) ?
909 if (__collate_load_error
?
911 *c
<= *k
&& *k
<= pat
[1] :
912 __wscollate_range_cmp(c
, k
, len1
, k_len
) <= 0
913 && __wscollate_range_cmp(k
, pat
+ 1, k_len
, len2
) <= 0
917 } else if (len1
== k_len
&&
918 wcincmp (c
, k
, len1
) == 0)
921 if (ok
== negate_range
)
925 if (Cchar(*name
++) != Cchar(*c
))
930 return(*name
== EOS
);
933 /* Free allocated data belonging to a glob_t structure. */
935 globfree(glob_t
*pglob
)
940 if (pglob
->gl_pathv
!= NULL
) {
941 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
942 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
945 free(pglob
->gl_pathv
);
946 pglob
->gl_pathv
= NULL
;
951 g_opendir(Char
*str
, glob_t
*pglob
)
953 char buf
[MAXPATHLEN
];
958 if (g_Ctoc(str
, buf
, sizeof(buf
)))
962 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
963 return (DIR *) ((*pglob
->gl_opendir
)((const char *) buf
));
965 return(opendir(buf
));
968 #define CYGWIN_gl_stat(sfptr) ((*pglob->sfptr) (buf, sb))
971 g_lstat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
973 char buf
[MAXPATHLEN
];
975 if (g_Ctoc(fn
, buf
, sizeof(buf
))) {
976 errno
= ENAMETOOLONG
;
979 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
980 return CYGWIN_gl_stat (gl_lstat
);
981 return(lstat(buf
, sb
));
985 g_stat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
987 char buf
[MAXPATHLEN
];
989 if (g_Ctoc(fn
, buf
, sizeof(buf
))) {
990 errno
= ENAMETOOLONG
;
993 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
994 return CYGWIN_gl_stat (gl_stat
);
995 return(stat(buf
, sb
));
999 g_strchr(const Char
*str
, wint_t ch
)
1010 g_Ctoc(const Char
*str
, char *buf
, size_t len
)
1015 memset(&mbs
, 0, sizeof(mbs
));
1016 while (len
>= (size_t) MB_CUR_MAX
) {
1017 clen
= wirtomb(buf
, CHAR (*str
), &mbs
);
1018 if (clen
== (size_t)-1)
1031 qprintf(const char *str
, Char
*s
)
1035 (void)printf("%s:\n", str
);
1036 for (p
= s
; *p
; p
++)
1037 (void)printf("%c", CHAR(*p
));
1039 for (p
= s
; *p
; p
++)
1040 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
1042 for (p
= s
; *p
; p
++)
1043 (void)printf("%c", ismeta(*p
) ? '_' : ' ');