1 /* $OpenBSD: glob.c,v 1.35 2011/01/12 01:53:14 djm Exp $ */
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* OPENBSD ORIGINAL: lib/libc/gen/glob.c */
37 * glob(3) -- a superset of the one defined in POSIX 1003.2.
39 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
41 * Optional extra services, controlled by flags not defined by POSIX:
44 * Escaping convention: \ inhibits any special meaning the following
45 * character might have (except \ at end of string is retained).
47 * Set in gl_flags if pattern contained a globbing character.
49 * Same as GLOB_NOCHECK, but it will only append pattern if it did
50 * not contain any magic characters. [Used in csh style globbing]
52 * Use alternately specified directory access functions.
54 * expand ~user/foo to the /home/dir/of/user/foo
56 * expand {1,2}{a,b} to 1a 1b 2a 2b
58 * Number of matches in the current invocation of glob.
63 #include <sys/types.h>
74 #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
75 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) || \
76 !defined(HAVE_DECL_GLOB_NOMATCH) || HAVE_DECL_GLOB_NOMATCH == 0 || \
79 #include "charclass.h"
93 #define UNDERSCORE '_'
101 #define M_QUOTE 0x8000
102 #define M_PROTECT 0x4000
103 #define M_MASK 0xffff
104 #define M_ASCII 0x00ff
106 typedef u_short Char
;
111 #define M_PROTECT 0x40
120 #define CHAR(c) ((Char)((c)&M_ASCII))
121 #define META(c) ((Char)((c)|M_QUOTE))
122 #define M_ALL META('*')
123 #define M_END META(']')
124 #define M_NOT META('!')
125 #define M_ONE META('?')
126 #define M_RNG META('-')
127 #define M_SET META('[')
128 #define M_CLASS META(':')
129 #define ismeta(c) (((c)&M_QUOTE) != 0)
131 #define GLOB_LIMIT_MALLOC 65536
132 #define GLOB_LIMIT_STAT 128
133 #define GLOB_LIMIT_READDIR 16384
141 static int compare(const void *, const void *);
142 static int g_Ctoc(const Char
*, char *, u_int
);
143 static int g_lstat(Char
*, struct stat
*, glob_t
*);
144 static DIR *g_opendir(Char
*, glob_t
*);
145 static Char
*g_strchr(const Char
*, int);
146 static int g_strncmp(const Char
*, const char *, size_t);
147 static int g_stat(Char
*, struct stat
*, glob_t
*);
148 static int glob0(const Char
*, glob_t
*, struct glob_lim
*);
149 static int glob1(Char
*, Char
*, glob_t
*, struct glob_lim
*);
150 static int glob2(Char
*, Char
*, Char
*, Char
*, Char
*, Char
*,
151 glob_t
*, struct glob_lim
*);
152 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*,
153 Char
*, Char
*, glob_t
*, struct glob_lim
*);
154 static int globextend(const Char
*, glob_t
*, struct glob_lim
*,
157 globtilde(const Char
*, Char
*, size_t, glob_t
*);
158 static int globexp1(const Char
*, glob_t
*, struct glob_lim
*);
159 static int globexp2(const Char
*, const Char
*, glob_t
*,
161 static int match(Char
*, Char
*, Char
*);
163 static void qprintf(const char *, Char
*);
167 glob(const char *pattern
, int flags
, int (*errfunc
)(const char *, int),
170 const u_char
*patnext
;
172 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
];
173 struct glob_lim limit
= { 0, 0, 0 };
175 patnext
= (u_char
*) pattern
;
176 if (!(flags
& GLOB_APPEND
)) {
178 pglob
->gl_pathv
= NULL
;
179 pglob
->gl_statv
= NULL
;
180 if (!(flags
& GLOB_DOOFFS
))
183 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
184 pglob
->gl_errfunc
= errfunc
;
185 pglob
->gl_matchc
= 0;
187 if (pglob
->gl_offs
< 0 || pglob
->gl_pathc
< 0 ||
188 pglob
->gl_offs
>= INT_MAX
|| pglob
->gl_pathc
>= INT_MAX
||
189 pglob
->gl_pathc
>= INT_MAX
- pglob
->gl_offs
- 1)
193 bufend
= bufnext
+ MAXPATHLEN
- 1;
194 if (flags
& GLOB_NOESCAPE
)
195 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
198 /* Protect the quoted characters. */
199 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
201 if ((c
= *patnext
++) == EOS
) {
205 *bufnext
++ = c
| M_PROTECT
;
211 if (flags
& GLOB_BRACE
)
212 return globexp1(patbuf
, pglob
, &limit
);
214 return glob0(patbuf
, pglob
, &limit
);
218 * Expand recursively a glob {} pattern. When there is no more expansion
219 * invoke the standard globbing routine to glob the rest of the magic
223 globexp1(const Char
*pattern
, glob_t
*pglob
, struct glob_lim
*limitp
)
225 const Char
* ptr
= pattern
;
227 /* Protect a single {}, for find(1), like csh */
228 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
229 return glob0(pattern
, pglob
, limitp
);
231 if ((ptr
= (const Char
*) g_strchr(ptr
, LBRACE
)) != NULL
)
232 return globexp2(ptr
, pattern
, pglob
, limitp
);
234 return glob0(pattern
, pglob
, limitp
);
239 * Recursive brace globbing helper. Tries to expand a single brace.
240 * If it succeeds then it invokes globexp1 with the new pattern.
241 * If it fails then it tries to glob the rest of the pattern and returns.
244 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
,
245 struct glob_lim
*limitp
)
249 const Char
*pe
, *pm
, *pl
;
250 Char patbuf
[MAXPATHLEN
];
252 /* copy part up to the brace */
253 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
258 /* Find the balanced brace */
259 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
260 if (*pe
== LBRACKET
) {
261 /* Ignore everything between [] */
262 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
266 * We could not find a matching RBRACKET.
267 * Ignore and just look for RBRACE
271 } else if (*pe
== LBRACE
)
273 else if (*pe
== RBRACE
) {
279 /* Non matching braces; just glob the pattern */
280 if (i
!= 0 || *pe
== EOS
)
281 return glob0(patbuf
, pglob
, limitp
);
283 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++) {
286 /* Ignore everything between [] */
287 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
291 * We could not find a matching RBRACKET.
292 * Ignore and just look for RBRACE
309 if (i
&& *pm
== COMMA
)
312 /* Append the current string */
313 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
317 * Append the rest of the pattern after the
320 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
; )
323 /* Expand the current pattern */
325 qprintf("globexp2:", patbuf
);
327 rv
= globexp1(patbuf
, pglob
, limitp
);
328 if (rv
&& rv
!= GLOB_NOMATCH
)
331 /* move after the comma, to the next string */
346 * expand tilde from the passwd file.
349 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patbuf_len
, glob_t
*pglob
)
356 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
359 /* Copy up to the end of the string or / */
360 eb
= &patbuf
[patbuf_len
- 1];
361 for (p
= pattern
+ 1, h
= (char *) patbuf
;
362 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
372 if (((char *) patbuf
)[0] == EOS
) {
374 * handle a plain ~ or ~/ by expanding $HOME
375 * first and then trying the password file
378 if (issetugid() != 0 || (h
= getenv("HOME")) == NULL
) {
380 if ((getuid() != geteuid()) || (h
= getenv("HOME")) == NULL
) {
381 if ((pwd
= getpwuid(getuid())) == NULL
)
390 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
396 /* Copy the home directory */
397 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
400 /* Append the rest of the pattern */
401 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
409 g_strncmp(const Char
*s1
, const char *s2
, size_t n
)
414 rv
= *(Char
*)s1
- *(const unsigned char *)s2
++;
424 g_charclass(const Char
**patternp
, Char
**bufnextp
)
426 const Char
*pattern
= *patternp
+ 1;
427 Char
*bufnext
= *bufnextp
;
432 if ((colon
= g_strchr(pattern
, ':')) == NULL
|| colon
[1] != ']')
433 return 1; /* not a character class */
435 len
= (size_t)(colon
- pattern
);
436 for (cc
= cclasses
; cc
->name
!= NULL
; cc
++) {
437 if (!g_strncmp(pattern
, cc
->name
, len
) && cc
->name
[len
] == '\0')
440 if (cc
->name
== NULL
)
441 return -1; /* invalid character class */
442 *bufnext
++ = M_CLASS
;
443 *bufnext
++ = (Char
)(cc
- &cclasses
[0]);
445 *patternp
+= len
+ 3;
451 * The main glob() routine: compiles the pattern (optionally processing
452 * quotes), calls glob1() to do the real pattern matching, and finally
453 * sorts the list (unless unsorted operation is requested). Returns 0
454 * if things went well, nonzero if errors occurred. It is not an error
455 * to find no matches.
458 glob0(const Char
*pattern
, glob_t
*pglob
, struct glob_lim
*limitp
)
460 const Char
*qpatnext
;
461 int c
, err
, oldpathc
;
462 Char
*bufnext
, patbuf
[MAXPATHLEN
];
464 qpatnext
= globtilde(pattern
, patbuf
, MAXPATHLEN
, pglob
);
465 oldpathc
= pglob
->gl_pathc
;
468 /* We don't need to check for buffer overflow any more. */
469 while ((c
= *qpatnext
++) != EOS
) {
475 if (*qpatnext
== EOS
||
476 g_strchr(qpatnext
+1, RBRACKET
) == NULL
) {
477 *bufnext
++ = LBRACKET
;
487 if (c
== LBRACKET
&& *qpatnext
== ':') {
489 err
= g_charclass(&qpatnext
,
494 } while (c
== LBRACKET
&& *qpatnext
== ':');
496 !(pglob
->gl_flags
& GLOB_NOCHECK
))
501 *bufnext
++ = CHAR(c
);
502 if (*qpatnext
== RANGE
&&
503 (c
= qpatnext
[1]) != RBRACKET
) {
505 *bufnext
++ = CHAR(c
);
508 } while ((c
= *qpatnext
++) != RBRACKET
);
509 pglob
->gl_flags
|= GLOB_MAGCHAR
;
513 pglob
->gl_flags
|= GLOB_MAGCHAR
;
517 pglob
->gl_flags
|= GLOB_MAGCHAR
;
518 /* collapse adjacent stars to one,
519 * to avoid exponential behavior
521 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
525 *bufnext
++ = CHAR(c
);
531 qprintf("glob0:", patbuf
);
534 if ((err
= glob1(patbuf
, patbuf
+MAXPATHLEN
-1, pglob
, limitp
)) != 0)
538 * If there was no match we are going to append the pattern
539 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
540 * and the pattern did not contain any magic characters
541 * GLOB_NOMAGIC is there just for compatibility with csh.
543 if (pglob
->gl_pathc
== oldpathc
) {
544 if ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
545 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
546 !(pglob
->gl_flags
& GLOB_MAGCHAR
)))
547 return(globextend(pattern
, pglob
, limitp
, NULL
));
549 return(GLOB_NOMATCH
);
551 if (!(pglob
->gl_flags
& GLOB_NOSORT
))
552 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
553 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
558 compare(const void *p
, const void *q
)
560 return(strcmp(*(char **)p
, *(char **)q
));
564 glob1(Char
*pattern
, Char
*pattern_last
, glob_t
*pglob
, struct glob_lim
*limitp
)
566 Char pathbuf
[MAXPATHLEN
];
568 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
571 return(glob2(pathbuf
, pathbuf
+MAXPATHLEN
-1,
572 pathbuf
, pathbuf
+MAXPATHLEN
-1,
573 pattern
, pattern_last
, pglob
, limitp
));
577 * The functions glob2 and glob3 are mutually recursive; there is one level
578 * of recursion for each segment in the pattern that contains one or more
582 glob2(Char
*pathbuf
, Char
*pathbuf_last
, Char
*pathend
, Char
*pathend_last
,
583 Char
*pattern
, Char
*pattern_last
, glob_t
*pglob
, struct glob_lim
*limitp
)
590 * Loop over pattern segments until end of pattern or until
591 * segment with meta character found.
593 for (anymeta
= 0;;) {
594 if (*pattern
== EOS
) { /* End of pattern? */
596 if (g_lstat(pathbuf
, &sb
, pglob
))
599 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
600 limitp
->glim_stat
++ >= GLOB_LIMIT_STAT
) {
604 return(GLOB_NOSPACE
);
607 if (((pglob
->gl_flags
& GLOB_MARK
) &&
608 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
) ||
609 (S_ISLNK(sb
.st_mode
) &&
610 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
611 S_ISDIR(sb
.st_mode
)))) {
612 if (pathend
+1 > pathend_last
)
618 return(globextend(pathbuf
, pglob
, limitp
, &sb
));
621 /* Find end of next segment, copy tentatively to pathend. */
624 while (*p
!= EOS
&& *p
!= SEP
) {
627 if (q
+1 > pathend_last
)
632 if (!anymeta
) { /* No expansion, do next segment. */
635 while (*pattern
== SEP
) {
636 if (pathend
+1 > pathend_last
)
638 *pathend
++ = *pattern
++;
641 /* Need expansion, recurse. */
642 return(glob3(pathbuf
, pathbuf_last
, pathend
,
643 pathend_last
, pattern
, p
, pattern_last
,
650 glob3(Char
*pathbuf
, Char
*pathbuf_last
, Char
*pathend
, Char
*pathend_last
,
651 Char
*pattern
, Char
*restpattern
, Char
*restpattern_last
, glob_t
*pglob
,
652 struct glob_lim
*limitp
)
657 char buf
[MAXPATHLEN
];
660 * The readdirfunc declaration can't be prototyped, because it is
661 * assigned, below, to two functions which are prototyped in glob.h
662 * and dirent.h as taking pointers to differently typed opaque
665 struct dirent
*(*readdirfunc
)(void *);
667 if (pathend
> pathend_last
)
672 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
673 /* TODO: don't call for ENOENT or ENOTDIR? */
674 if (pglob
->gl_errfunc
) {
675 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
676 return(GLOB_ABORTED
);
677 if (pglob
->gl_errfunc(buf
, errno
) ||
678 pglob
->gl_flags
& GLOB_ERR
)
679 return(GLOB_ABORTED
);
686 /* Search directory for matching names. */
687 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
688 readdirfunc
= pglob
->gl_readdir
;
690 readdirfunc
= (struct dirent
*(*)(void *))readdir
;
691 while ((dp
= (*readdirfunc
)(dirp
))) {
695 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
696 limitp
->glim_readdir
++ >= GLOB_LIMIT_READDIR
) {
700 return(GLOB_NOSPACE
);
703 /* Initial DOT must be matched literally. */
704 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
707 sc
= (u_char
*) dp
->d_name
;
708 while (dc
< pathend_last
&& (*dc
++ = *sc
++) != EOS
)
710 if (dc
>= pathend_last
) {
716 if (!match(pathend
, pattern
, restpattern
)) {
720 err
= glob2(pathbuf
, pathbuf_last
, --dc
, pathend_last
,
721 restpattern
, restpattern_last
, pglob
, limitp
);
726 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
727 (*pglob
->gl_closedir
)(dirp
);
735 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
736 * add the new item, and update gl_pathc.
738 * This assumes the BSD realloc, which only copies the block when its size
739 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
742 * Return 0 if new item added, error code if memory couldn't be allocated.
744 * Invariant of the glob_t structure:
745 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
746 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
749 globextend(const Char
*path
, glob_t
*pglob
, struct glob_lim
*limitp
,
759 newn
= 2 + pglob
->gl_pathc
+ pglob
->gl_offs
;
760 if (pglob
->gl_offs
>= INT_MAX
||
761 pglob
->gl_pathc
>= INT_MAX
||
763 SIZE_MAX
/ sizeof(*pathv
) <= newn
||
764 SIZE_MAX
/ sizeof(*statv
) <= newn
) {
766 for (i
= pglob
->gl_offs
; i
< (ssize_t
)(newn
- 2); i
++) {
767 if (pglob
->gl_pathv
&& pglob
->gl_pathv
[i
])
768 free(pglob
->gl_pathv
[i
]);
769 if ((pglob
->gl_flags
& GLOB_KEEPSTAT
) != 0 &&
770 pglob
->gl_pathv
&& pglob
->gl_pathv
[i
])
771 free(pglob
->gl_statv
[i
]);
773 if (pglob
->gl_pathv
) {
774 free(pglob
->gl_pathv
);
775 pglob
->gl_pathv
= NULL
;
777 if (pglob
->gl_statv
) {
778 free(pglob
->gl_statv
);
779 pglob
->gl_statv
= NULL
;
781 return(GLOB_NOSPACE
);
784 pathv
= realloc(pglob
->gl_pathv
, newn
* sizeof(*pathv
));
787 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
788 /* first time around -- clear initial gl_offs items */
789 pathv
+= pglob
->gl_offs
;
790 for (i
= pglob
->gl_offs
; --i
>= 0; )
793 pglob
->gl_pathv
= pathv
;
795 if ((pglob
->gl_flags
& GLOB_KEEPSTAT
) != 0) {
796 statv
= realloc(pglob
->gl_statv
, newn
* sizeof(*statv
));
799 if (pglob
->gl_statv
== NULL
&& pglob
->gl_offs
> 0) {
800 /* first time around -- clear initial gl_offs items */
801 statv
+= pglob
->gl_offs
;
802 for (i
= pglob
->gl_offs
; --i
>= 0; )
805 pglob
->gl_statv
= statv
;
807 statv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
809 limitp
->glim_malloc
+= sizeof(**statv
);
810 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
811 limitp
->glim_malloc
>= GLOB_LIMIT_MALLOC
) {
813 return(GLOB_NOSPACE
);
815 if ((statv
[pglob
->gl_offs
+ pglob
->gl_pathc
] =
816 malloc(sizeof(**statv
))) == NULL
)
818 memcpy(statv
[pglob
->gl_offs
+ pglob
->gl_pathc
], sb
,
821 statv
[pglob
->gl_offs
+ pglob
->gl_pathc
+ 1] = NULL
;
824 for (p
= path
; *p
++;)
826 len
= (size_t)(p
- path
);
827 limitp
->glim_malloc
+= len
;
828 if ((copy
= malloc(len
)) != NULL
) {
829 if (g_Ctoc(path
, copy
, len
)) {
831 return(GLOB_NOSPACE
);
833 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
835 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
837 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
838 (newn
* sizeof(*pathv
)) + limitp
->glim_malloc
>
841 return(GLOB_NOSPACE
);
844 return(copy
== NULL
? GLOB_NOSPACE
: 0);
849 * pattern matching function for filenames. Each occurrence of the *
850 * pattern causes a recursion level.
853 match(Char
*name
, Char
*pat
, Char
*patend
)
855 int ok
, negate_range
;
858 while (pat
< patend
) {
860 switch (c
& M_MASK
) {
865 if (match(name
, pat
, patend
))
867 } while (*name
++ != EOS
);
875 if ((k
= *name
++) == EOS
)
877 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
879 while (((c
= *pat
++) & M_MASK
) != M_END
) {
880 if ((c
& M_MASK
) == M_CLASS
) {
881 Char idx
= *pat
& M_MASK
;
882 if (idx
< NCCLASSES
&&
883 cclasses
[idx
].isctype(k
))
887 if ((*pat
& M_MASK
) == M_RNG
) {
888 if (c
<= k
&& k
<= pat
[1])
894 if (ok
== negate_range
)
903 return(*name
== EOS
);
906 /* Free allocated data belonging to a glob_t structure. */
908 globfree(glob_t
*pglob
)
913 if (pglob
->gl_pathv
!= NULL
) {
914 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
915 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
918 free(pglob
->gl_pathv
);
919 pglob
->gl_pathv
= NULL
;
921 if (pglob
->gl_statv
!= NULL
) {
922 for (i
= 0; i
< pglob
->gl_pathc
; i
++) {
923 if (pglob
->gl_statv
[i
] != NULL
)
924 free(pglob
->gl_statv
[i
]);
926 free(pglob
->gl_statv
);
927 pglob
->gl_statv
= NULL
;
932 g_opendir(Char
*str
, glob_t
*pglob
)
934 char buf
[MAXPATHLEN
];
937 strlcpy(buf
, ".", sizeof buf
);
939 if (g_Ctoc(str
, buf
, sizeof(buf
)))
943 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
944 return((*pglob
->gl_opendir
)(buf
));
946 return(opendir(buf
));
950 g_lstat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
952 char buf
[MAXPATHLEN
];
954 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
956 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
957 return((*pglob
->gl_lstat
)(buf
, sb
));
958 return(lstat(buf
, sb
));
962 g_stat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
964 char buf
[MAXPATHLEN
];
966 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
968 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
969 return((*pglob
->gl_stat
)(buf
, sb
));
970 return(stat(buf
, sb
));
974 g_strchr(const Char
*str
, int ch
)
978 return ((Char
*)str
);
984 g_Ctoc(const Char
*str
, char *buf
, u_int len
)
988 if ((*buf
++ = *str
++) == EOS
)
996 qprintf(const char *str
, Char
*s
)
1000 (void)printf("%s:\n", str
);
1001 for (p
= s
; *p
; p
++)
1002 (void)printf("%c", CHAR(*p
));
1004 for (p
= s
; *p
; p
++)
1005 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
1007 for (p
= s
; *p
; p
++)
1008 (void)printf("%c", ismeta(*p
) ? '_' : ' ');
1013 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
1014 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */