1 /* $OpenBSD: glob.c,v 1.33 2010/09/26 22:15:39 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 || \
84 #elif defined(HAVE_SYSCONF) && defined(_SC_ARG_MAX)
85 return(sysconf(_SC_ARG_MAX
));
87 return(256); /* XXX: arbitrary */
91 #include "charclass.h"
105 #define UNDERSCORE '_'
113 #define M_QUOTE 0x8000
114 #define M_PROTECT 0x4000
115 #define M_MASK 0xffff
116 #define M_ASCII 0x00ff
118 typedef u_short Char
;
123 #define M_PROTECT 0x40
132 #define CHAR(c) ((Char)((c)&M_ASCII))
133 #define META(c) ((Char)((c)|M_QUOTE))
134 #define M_ALL META('*')
135 #define M_END META(']')
136 #define M_NOT META('!')
137 #define M_ONE META('?')
138 #define M_RNG META('-')
139 #define M_SET META('[')
140 #define M_CLASS META(':')
141 #define ismeta(c) (((c)&M_QUOTE) != 0)
144 static int compare(const void *, const void *);
145 static int g_Ctoc(const Char
*, char *, u_int
);
146 static int g_lstat(Char
*, struct stat
*, glob_t
*);
147 static DIR *g_opendir(Char
*, glob_t
*);
148 static Char
*g_strchr(const Char
*, int);
149 static int g_strncmp(const Char
*, const char *, size_t);
150 static int g_stat(Char
*, struct stat
*, glob_t
*);
151 static int glob0(const Char
*, glob_t
*);
152 static int glob1(Char
*, Char
*, glob_t
*, size_t *);
153 static int glob2(Char
*, Char
*, Char
*, Char
*, Char
*, Char
*,
155 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*,
156 Char
*, Char
*, glob_t
*, size_t *);
157 static int globextend(const Char
*, glob_t
*, size_t *, struct stat
*);
159 globtilde(const Char
*, Char
*, size_t, glob_t
*);
160 static int globexp1(const Char
*, glob_t
*);
161 static int globexp2(const Char
*, const Char
*, glob_t
*);
162 static int match(Char
*, Char
*, Char
*);
164 static void qprintf(const char *, Char
*);
168 glob(const char *pattern
, int flags
, int (*errfunc
)(const char *, int),
171 const u_char
*patnext
;
173 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
];
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;
188 bufend
= bufnext
+ MAXPATHLEN
- 1;
189 if (flags
& GLOB_NOESCAPE
)
190 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
193 /* Protect the quoted characters. */
194 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
196 if ((c
= *patnext
++) == EOS
) {
200 *bufnext
++ = c
| M_PROTECT
;
206 if (flags
& GLOB_BRACE
)
207 return globexp1(patbuf
, pglob
);
209 return glob0(patbuf
, pglob
);
213 * Expand recursively a glob {} pattern. When there is no more expansion
214 * invoke the standard globbing routine to glob the rest of the magic
218 globexp1(const Char
*pattern
, glob_t
*pglob
)
220 const Char
* ptr
= pattern
;
222 /* Protect a single {}, for find(1), like csh */
223 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
224 return glob0(pattern
, pglob
);
226 if ((ptr
= (const Char
*) g_strchr(ptr
, LBRACE
)) != NULL
)
227 return globexp2(ptr
, pattern
, pglob
);
229 return glob0(pattern
, pglob
);
234 * Recursive brace globbing helper. Tries to expand a single brace.
235 * If it succeeds then it invokes globexp1 with the new pattern.
236 * If it fails then it tries to glob the rest of the pattern and returns.
239 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
)
243 const Char
*pe
, *pm
, *pl
;
244 Char patbuf
[MAXPATHLEN
];
246 /* copy part up to the brace */
247 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
252 /* Find the balanced brace */
253 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
254 if (*pe
== LBRACKET
) {
255 /* Ignore everything between [] */
256 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
260 * We could not find a matching RBRACKET.
261 * Ignore and just look for RBRACE
265 } else if (*pe
== LBRACE
)
267 else if (*pe
== RBRACE
) {
273 /* Non matching braces; just glob the pattern */
274 if (i
!= 0 || *pe
== EOS
)
275 return glob0(patbuf
, pglob
);
277 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++) {
280 /* Ignore everything between [] */
281 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
285 * We could not find a matching RBRACKET.
286 * Ignore and just look for RBRACE
303 if (i
&& *pm
== COMMA
)
306 /* Append the current string */
307 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
311 * Append the rest of the pattern after the
314 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
; )
317 /* Expand the current pattern */
319 qprintf("globexp2:", patbuf
);
321 rv
= globexp1(patbuf
, pglob
);
322 if (rv
&& rv
!= GLOB_NOMATCH
)
325 /* move after the comma, to the next string */
340 * expand tilde from the passwd file.
343 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patbuf_len
, glob_t
*pglob
)
350 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
353 /* Copy up to the end of the string or / */
354 eb
= &patbuf
[patbuf_len
- 1];
355 for (p
= pattern
+ 1, h
= (char *) patbuf
;
356 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
366 if (((char *) patbuf
)[0] == EOS
) {
368 * handle a plain ~ or ~/ by expanding $HOME
369 * first and then trying the password file
372 if (issetugid() != 0 || (h
= getenv("HOME")) == NULL
) {
374 if ((getuid() != geteuid()) || (h
= getenv("HOME")) == NULL
) {
375 if ((pwd
= getpwuid(getuid())) == NULL
)
384 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
390 /* Copy the home directory */
391 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
394 /* Append the rest of the pattern */
395 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
403 g_strncmp(const Char
*s1
, const char *s2
, size_t n
)
408 rv
= *(Char
*)s1
- *(const unsigned char *)s2
++;
418 g_charclass(const Char
**patternp
, Char
**bufnextp
)
420 const Char
*pattern
= *patternp
+ 1;
421 Char
*bufnext
= *bufnextp
;
426 if ((colon
= g_strchr(pattern
, ':')) == NULL
|| colon
[1] != ']')
427 return 1; /* not a character class */
429 len
= (size_t)(colon
- pattern
);
430 for (cc
= cclasses
; cc
->name
!= NULL
; cc
++) {
431 if (!g_strncmp(pattern
, cc
->name
, len
) && cc
->name
[len
] == '\0')
434 if (cc
->name
== NULL
)
435 return -1; /* invalid character class */
436 *bufnext
++ = M_CLASS
;
437 *bufnext
++ = (Char
)(cc
- &cclasses
[0]);
439 *patternp
+= len
+ 3;
445 * The main glob() routine: compiles the pattern (optionally processing
446 * quotes), calls glob1() to do the real pattern matching, and finally
447 * sorts the list (unless unsorted operation is requested). Returns 0
448 * if things went well, nonzero if errors occurred. It is not an error
449 * to find no matches.
452 glob0(const Char
*pattern
, glob_t
*pglob
)
454 const Char
*qpatnext
;
455 int c
, err
, oldpathc
;
456 Char
*bufnext
, patbuf
[MAXPATHLEN
];
459 qpatnext
= globtilde(pattern
, patbuf
, MAXPATHLEN
, pglob
);
460 oldpathc
= pglob
->gl_pathc
;
463 /* We don't need to check for buffer overflow any more. */
464 while ((c
= *qpatnext
++) != EOS
) {
470 if (*qpatnext
== EOS
||
471 g_strchr(qpatnext
+1, RBRACKET
) == NULL
) {
472 *bufnext
++ = LBRACKET
;
482 if (c
== LBRACKET
&& *qpatnext
== ':') {
484 err
= g_charclass(&qpatnext
,
489 } while (c
== LBRACKET
&& *qpatnext
== ':');
491 !(pglob
->gl_flags
& GLOB_NOCHECK
))
496 *bufnext
++ = CHAR(c
);
497 if (*qpatnext
== RANGE
&&
498 (c
= qpatnext
[1]) != RBRACKET
) {
500 *bufnext
++ = CHAR(c
);
503 } while ((c
= *qpatnext
++) != RBRACKET
);
504 pglob
->gl_flags
|= GLOB_MAGCHAR
;
508 pglob
->gl_flags
|= GLOB_MAGCHAR
;
512 pglob
->gl_flags
|= GLOB_MAGCHAR
;
513 /* collapse adjacent stars to one,
514 * to avoid exponential behavior
516 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
520 *bufnext
++ = CHAR(c
);
526 qprintf("glob0:", patbuf
);
529 if ((err
= glob1(patbuf
, patbuf
+MAXPATHLEN
-1, pglob
, &limit
)) != 0)
533 * If there was no match we are going to append the pattern
534 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
535 * and the pattern did not contain any magic characters
536 * GLOB_NOMAGIC is there just for compatibility with csh.
538 if (pglob
->gl_pathc
== oldpathc
) {
539 if ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
540 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
541 !(pglob
->gl_flags
& GLOB_MAGCHAR
)))
542 return(globextend(pattern
, pglob
, &limit
, NULL
));
544 return(GLOB_NOMATCH
);
546 if (!(pglob
->gl_flags
& GLOB_NOSORT
))
547 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
548 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
553 compare(const void *p
, const void *q
)
555 return(strcmp(*(char **)p
, *(char **)q
));
559 glob1(Char
*pattern
, Char
*pattern_last
, glob_t
*pglob
, size_t *limitp
)
561 Char pathbuf
[MAXPATHLEN
];
563 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
566 return(glob2(pathbuf
, pathbuf
+MAXPATHLEN
-1,
567 pathbuf
, pathbuf
+MAXPATHLEN
-1,
568 pattern
, pattern_last
, pglob
, limitp
));
572 * The functions glob2 and glob3 are mutually recursive; there is one level
573 * of recursion for each segment in the pattern that contains one or more
577 glob2(Char
*pathbuf
, Char
*pathbuf_last
, Char
*pathend
, Char
*pathend_last
,
578 Char
*pattern
, Char
*pattern_last
, glob_t
*pglob
, size_t *limitp
)
585 * Loop over pattern segments until end of pattern or until
586 * segment with meta character found.
588 for (anymeta
= 0;;) {
589 if (*pattern
== EOS
) { /* End of pattern? */
591 if (g_lstat(pathbuf
, &sb
, pglob
))
594 if (((pglob
->gl_flags
& GLOB_MARK
) &&
595 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
) ||
596 (S_ISLNK(sb
.st_mode
) &&
597 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
598 S_ISDIR(sb
.st_mode
)))) {
599 if (pathend
+1 > pathend_last
)
605 return(globextend(pathbuf
, pglob
, limitp
, &sb
));
608 /* Find end of next segment, copy tentatively to pathend. */
611 while (*p
!= EOS
&& *p
!= SEP
) {
614 if (q
+1 > pathend_last
)
619 if (!anymeta
) { /* No expansion, do next segment. */
622 while (*pattern
== SEP
) {
623 if (pathend
+1 > pathend_last
)
625 *pathend
++ = *pattern
++;
628 /* Need expansion, recurse. */
629 return(glob3(pathbuf
, pathbuf_last
, pathend
,
630 pathend_last
, pattern
, p
, pattern_last
,
637 glob3(Char
*pathbuf
, Char
*pathbuf_last
, Char
*pathend
, Char
*pathend_last
,
638 Char
*pattern
, Char
*restpattern
, Char
*restpattern_last
, glob_t
*pglob
,
644 char buf
[MAXPATHLEN
];
647 * The readdirfunc declaration can't be prototyped, because it is
648 * assigned, below, to two functions which are prototyped in glob.h
649 * and dirent.h as taking pointers to differently typed opaque
652 struct dirent
*(*readdirfunc
)(void *);
654 if (pathend
> pathend_last
)
659 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
660 /* TODO: don't call for ENOENT or ENOTDIR? */
661 if (pglob
->gl_errfunc
) {
662 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
663 return(GLOB_ABORTED
);
664 if (pglob
->gl_errfunc(buf
, errno
) ||
665 pglob
->gl_flags
& GLOB_ERR
)
666 return(GLOB_ABORTED
);
673 /* Search directory for matching names. */
674 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
675 readdirfunc
= pglob
->gl_readdir
;
677 readdirfunc
= (struct dirent
*(*)(void *))readdir
;
678 while ((dp
= (*readdirfunc
)(dirp
))) {
682 /* Initial DOT must be matched literally. */
683 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
686 sc
= (u_char
*) dp
->d_name
;
687 while (dc
< pathend_last
&& (*dc
++ = *sc
++) != EOS
)
689 if (dc
>= pathend_last
) {
695 if (!match(pathend
, pattern
, restpattern
)) {
699 err
= glob2(pathbuf
, pathbuf_last
, --dc
, pathend_last
,
700 restpattern
, restpattern_last
, pglob
, limitp
);
705 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
706 (*pglob
->gl_closedir
)(dirp
);
714 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
715 * add the new item, and update gl_pathc.
717 * This assumes the BSD realloc, which only copies the block when its size
718 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
721 * Return 0 if new item added, error code if memory couldn't be allocated.
723 * Invariant of the glob_t structure:
724 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
725 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
728 globextend(const Char
*path
, glob_t
*pglob
, size_t *limitp
, struct stat
*sb
)
737 newn
= 2 + pglob
->gl_pathc
+ pglob
->gl_offs
;
738 if (SIZE_MAX
/ sizeof(*pathv
) <= newn
||
739 SIZE_MAX
/ sizeof(*statv
) <= newn
) {
741 for (i
= pglob
->gl_offs
; i
< newn
- 2; i
++) {
742 if (pglob
->gl_pathv
&& pglob
->gl_pathv
[i
])
743 free(pglob
->gl_pathv
[i
]);
744 if ((pglob
->gl_flags
& GLOB_KEEPSTAT
) != 0 &&
745 pglob
->gl_pathv
&& pglob
->gl_pathv
[i
])
746 free(pglob
->gl_statv
[i
]);
748 if (pglob
->gl_pathv
) {
749 free(pglob
->gl_pathv
);
750 pglob
->gl_pathv
= NULL
;
752 if (pglob
->gl_statv
) {
753 free(pglob
->gl_statv
);
754 pglob
->gl_statv
= NULL
;
756 return(GLOB_NOSPACE
);
759 pathv
= realloc(pglob
->gl_pathv
, newn
* sizeof(*pathv
));
762 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
763 /* first time around -- clear initial gl_offs items */
764 pathv
+= pglob
->gl_offs
;
765 for (i
= pglob
->gl_offs
; --i
>= 0; )
768 pglob
->gl_pathv
= pathv
;
770 if ((pglob
->gl_flags
& GLOB_KEEPSTAT
) != 0) {
771 statv
= realloc(pglob
->gl_statv
, newn
* sizeof(*statv
));
774 if (pglob
->gl_statv
== NULL
&& pglob
->gl_offs
> 0) {
775 /* first time around -- clear initial gl_offs items */
776 statv
+= pglob
->gl_offs
;
777 for (i
= pglob
->gl_offs
; --i
>= 0; )
780 pglob
->gl_statv
= statv
;
782 statv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
784 if ((statv
[pglob
->gl_offs
+ pglob
->gl_pathc
] =
785 malloc(sizeof(**statv
))) == NULL
)
787 memcpy(statv
[pglob
->gl_offs
+ pglob
->gl_pathc
], sb
,
790 statv
[pglob
->gl_offs
+ pglob
->gl_pathc
+ 1] = NULL
;
793 for (p
= path
; *p
++;)
795 len
= (size_t)(p
- path
);
797 if ((copy
= malloc(len
)) != NULL
) {
798 if (g_Ctoc(path
, copy
, len
)) {
800 return(GLOB_NOSPACE
);
802 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
804 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
806 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
807 (newn
* sizeof(*pathv
)) + *limitp
>= (u_int
) get_arg_max()) {
809 return(GLOB_NOSPACE
);
812 return(copy
== NULL
? GLOB_NOSPACE
: 0);
817 * pattern matching function for filenames. Each occurrence of the *
818 * pattern causes a recursion level.
821 match(Char
*name
, Char
*pat
, Char
*patend
)
823 int ok
, negate_range
;
826 while (pat
< patend
) {
828 switch (c
& M_MASK
) {
833 if (match(name
, pat
, patend
))
835 } while (*name
++ != EOS
);
843 if ((k
= *name
++) == EOS
)
845 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
847 while (((c
= *pat
++) & M_MASK
) != M_END
) {
848 if ((c
& M_MASK
) == M_CLASS
) {
849 int idx
= *pat
& M_MASK
;
850 if (idx
< NCCLASSES
&&
851 cclasses
[idx
].isctype(k
))
855 if ((*pat
& M_MASK
) == M_RNG
) {
856 if (c
<= k
&& k
<= pat
[1])
862 if (ok
== negate_range
)
871 return(*name
== EOS
);
874 /* Free allocated data belonging to a glob_t structure. */
876 globfree(glob_t
*pglob
)
881 if (pglob
->gl_pathv
!= NULL
) {
882 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
883 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
886 free(pglob
->gl_pathv
);
887 pglob
->gl_pathv
= NULL
;
889 if (pglob
->gl_statv
!= NULL
) {
890 for (i
= 0; i
< pglob
->gl_pathc
; i
++) {
891 if (pglob
->gl_statv
[i
] != NULL
)
892 free(pglob
->gl_statv
[i
]);
894 free(pglob
->gl_statv
);
895 pglob
->gl_statv
= NULL
;
900 g_opendir(Char
*str
, glob_t
*pglob
)
902 char buf
[MAXPATHLEN
];
905 strlcpy(buf
, ".", sizeof buf
);
907 if (g_Ctoc(str
, buf
, sizeof(buf
)))
911 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
912 return((*pglob
->gl_opendir
)(buf
));
914 return(opendir(buf
));
918 g_lstat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
920 char buf
[MAXPATHLEN
];
922 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
924 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
925 return((*pglob
->gl_lstat
)(buf
, sb
));
926 return(lstat(buf
, sb
));
930 g_stat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
932 char buf
[MAXPATHLEN
];
934 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
936 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
937 return((*pglob
->gl_stat
)(buf
, sb
));
938 return(stat(buf
, sb
));
942 g_strchr(const Char
*str
, int ch
)
946 return ((Char
*)str
);
952 g_Ctoc(const Char
*str
, char *buf
, u_int len
)
956 if ((*buf
++ = *str
++) == EOS
)
964 qprintf(const char *str
, Char
*s
)
968 (void)printf("%s:\n", str
);
970 (void)printf("%c", CHAR(*p
));
973 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
976 (void)printf("%c", ismeta(*p
) ? '_' : ' ');
981 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
982 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */