1 /* $NetBSD: glob.c,v 1.23 2008/05/26 13:06:38 ad Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
40 __RCSID("$NetBSD: glob.c,v 1.23 2008/05/26 13:06:38 ad Exp $");
42 #endif /* LIBC_SCCS and not lint */
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 * 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 * allow metacharacters to match leading dots in filenames.
65 * . and .. are hidden from wildcards, even if GLOB_PERIOD is set.
67 * Number of matches in the current invocation of glob.
70 #include "namespace.h"
71 #include <sys/param.h>
86 #ifdef HAVE_NBTOOL_CONFIG_H
92 #define ARG_MAX _POSIX_ARG_MAX
96 * XXX: For NetBSD 1.4.x compatibility. (kill me l8r)
99 #define _DIAGASSERT(a)
114 #define UNDERSCORE '_'
120 #ifndef USE_8BIT_CHARS
122 #define M_QUOTE 0x8000
123 #define M_PROTECT 0x4000
124 #define M_MASK 0xffff
125 #define M_ASCII 0x00ff
127 typedef u_short Char
;
131 #define M_QUOTE (Char)0x80
132 #define M_PROTECT (Char)0x40
133 #define M_MASK (Char)0xff
134 #define M_ASCII (Char)0x7f
141 #define CHAR(c) ((Char)((c)&M_ASCII))
142 #define META(c) ((Char)((c)|M_QUOTE))
143 #define M_ALL META('*')
144 #define M_END META(']')
145 #define M_NOT META('!')
146 #define M_ONE META('?')
147 #define M_RNG META('-')
148 #define M_SET META('[')
149 #define ismeta(c) (((c)&M_QUOTE) != 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
*, __gl_stat_t
*, glob_t
*);
155 static DIR *g_opendir(Char
*, glob_t
*);
156 static Char
*g_strchr(const Char
*, int);
157 static int g_stat(Char
*, __gl_stat_t
*, glob_t
*);
158 static int glob0(const Char
*, glob_t
*);
159 static int glob1(Char
*, glob_t
*, size_t *);
160 static int glob2(Char
*, Char
*, Char
*, Char
*, glob_t
*,
162 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*, glob_t
*,
164 static int globextend(const Char
*, glob_t
*, size_t *);
165 static const Char
*globtilde(const Char
*, Char
*, size_t, glob_t
*);
166 static int globexp1(const Char
*, glob_t
*);
167 static int globexp2(const Char
*, const Char
*, glob_t
*, int *);
168 static int match(Char
*, Char
*, Char
*);
170 static void qprintf(const char *, Char
*);
174 glob(const char *pattern
, int flags
, int (*errfunc
)(const char *, int),
177 const u_char
*patnext
;
179 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
181 _DIAGASSERT(pattern
!= NULL
);
183 patnext
= (const u_char
*) pattern
;
184 if (!(flags
& GLOB_APPEND
)) {
186 pglob
->gl_pathv
= NULL
;
187 if (!(flags
& GLOB_DOOFFS
))
190 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
191 pglob
->gl_errfunc
= errfunc
;
192 pglob
->gl_matchc
= 0;
195 bufend
= bufnext
+ MAXPATHLEN
;
196 if (flags
& GLOB_NOESCAPE
) {
197 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
200 /* Protect the quoted characters. */
201 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
203 if ((c
= *patnext
++) == EOS
) {
207 *bufnext
++ = c
| M_PROTECT
;
214 if (flags
& GLOB_BRACE
)
215 return globexp1(patbuf
, pglob
);
217 return glob0(patbuf
, pglob
);
221 * Expand recursively a glob {} pattern. When there is no more expansion
222 * invoke the standard globbing routine to glob the rest of the magic
226 globexp1(const Char
*pattern
, glob_t
*pglob
)
228 const Char
* ptr
= pattern
;
231 _DIAGASSERT(pattern
!= NULL
);
232 _DIAGASSERT(pglob
!= NULL
);
234 /* Protect a single {}, for find(1), like csh */
235 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
236 return glob0(pattern
, pglob
);
238 while ((ptr
= (const Char
*) g_strchr(ptr
, LBRACE
)) != NULL
)
239 if (!globexp2(ptr
, pattern
, pglob
, &rv
))
242 return glob0(pattern
, pglob
);
247 * Recursive brace globbing helper. Tries to expand a single brace.
248 * If it succeeds then it invokes globexp1 with the new pattern.
249 * If it fails then it tries to glob the rest of the pattern and returns.
252 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
, int *rv
)
256 const Char
*pe
, *pm
, *pl
;
257 Char patbuf
[MAXPATHLEN
+ 1];
259 _DIAGASSERT(ptr
!= NULL
);
260 _DIAGASSERT(pattern
!= NULL
);
261 _DIAGASSERT(pglob
!= NULL
);
262 _DIAGASSERT(rv
!= NULL
);
264 /* copy part up to the brace */
265 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
269 /* Find the balanced brace */
270 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
271 if (*pe
== LBRACKET
) {
272 /* Ignore everything between [] */
273 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
277 * We could not find a matching RBRACKET.
278 * Ignore and just look for RBRACE
283 else if (*pe
== LBRACE
)
285 else if (*pe
== RBRACE
) {
291 /* Non matching braces; just glob the pattern */
292 if (i
!= 0 || *pe
== EOS
) {
294 * we use `pattern', not `patbuf' here so that that
295 * unbalanced braces are passed to the match
297 *rv
= glob0(pattern
, pglob
);
301 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++) {
304 /* Ignore everything between [] */
305 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
309 * We could not find a matching RBRACKET.
310 * Ignore and just look for RBRACE
327 if (i
&& *pm
== COMMA
)
330 /* Append the current string */
331 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
334 * Append the rest of the pattern after the
337 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
340 /* Expand the current pattern */
342 qprintf("globexp2:", patbuf
);
344 *rv
= globexp1(patbuf
, pglob
);
346 /* move after the comma, to the next string */
362 * expand tilde from the passwd file.
365 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patsize
, glob_t
*pglob
)
372 Char
*pend
= &patbuf
[patsize
/ sizeof(Char
)];
380 _DIAGASSERT(pattern
!= NULL
);
381 _DIAGASSERT(patbuf
!= NULL
);
382 _DIAGASSERT(pglob
!= NULL
);
384 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
387 /* Copy up to the end of the string or / */
388 for (p
= pattern
+ 1, d
= (char *)(void *)patbuf
;
389 d
< (char *)(void *)pend
&& *p
&& *p
!= SLASH
;
393 if (d
== (char *)(void *)pend
)
397 d
= (char *)(void *)patbuf
;
401 * handle a plain ~ or ~/ by expanding $HOME
402 * first and then trying the password file
404 if ((h
= getenv("HOME")) == NULL
) {
406 if ((pwd
= getpwuid(getuid())) == NULL
)
408 if (getpwuid_r(getuid(), &pwres
, pwbuf
, sizeof(pwbuf
),
409 &pwd
) != 0 || pwd
== NULL
)
421 if ((pwd
= getpwnam(d
)) == NULL
)
423 if (getpwnam_r(d
, &pwres
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0 ||
431 /* Copy the home directory */
432 for (b
= patbuf
; b
< pend
&& *h
; *b
++ = *h
++)
438 /* Append the rest of the pattern */
439 while (b
< pend
&& (*b
++ = *p
++) != EOS
)
450 * The main glob() routine: compiles the pattern (optionally processing
451 * quotes), calls glob1() to do the real pattern matching, and finally
452 * sorts the list (unless unsorted operation is requested). Returns 0
453 * if things went well, nonzero if errors occurred. It is not an error
454 * to find no matches.
457 glob0(const Char
*pattern
, glob_t
*pglob
)
459 const Char
*qpatnext
;
461 __gl_size_t oldpathc
;
462 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
465 _DIAGASSERT(pattern
!= NULL
);
466 _DIAGASSERT(pglob
!= NULL
);
468 if ((qpatnext
= globtilde(pattern
, patbuf
, sizeof(patbuf
),
471 oldpathc
= pglob
->gl_pathc
;
474 /* We don't need to check for buffer overflow any more. */
475 while ((c
= *qpatnext
++) != EOS
) {
481 if (*qpatnext
== EOS
||
482 g_strchr(qpatnext
+1, RBRACKET
) == NULL
) {
483 *bufnext
++ = LBRACKET
;
493 *bufnext
++ = CHAR(c
);
494 if (*qpatnext
== RANGE
&&
495 (c
= qpatnext
[1]) != RBRACKET
) {
497 *bufnext
++ = CHAR(c
);
500 } while ((c
= *qpatnext
++) != RBRACKET
);
501 pglob
->gl_flags
|= GLOB_MAGCHAR
;
505 pglob
->gl_flags
|= GLOB_MAGCHAR
;
509 pglob
->gl_flags
|= GLOB_MAGCHAR
;
510 /* collapse adjacent stars to one,
511 * to avoid exponential behavior
513 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
517 *bufnext
++ = CHAR(c
);
523 qprintf("glob0:", patbuf
);
526 if ((error
= glob1(patbuf
, pglob
, &limit
)) != 0)
529 if (pglob
->gl_pathc
== oldpathc
) {
531 * If there was no match we are going to append the pattern
532 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
533 * specified and the pattern did not contain any magic
534 * characters GLOB_NOMAGIC is there just for compatibility
537 if ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
538 ((pglob
->gl_flags
& (GLOB_NOMAGIC
|GLOB_MAGCHAR
))
540 return globextend(pattern
, pglob
, &limit
);
544 } else if (!(pglob
->gl_flags
& GLOB_NOSORT
)) {
545 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
546 (size_t)pglob
->gl_pathc
- oldpathc
, sizeof(char *),
554 compare(const void *p
, const void *q
)
557 _DIAGASSERT(p
!= NULL
);
558 _DIAGASSERT(q
!= NULL
);
560 return strcoll(*(const char * const *)p
, *(const char * const *)q
);
564 glob1(Char
*pattern
, glob_t
*pglob
, size_t *limit
)
566 Char pathbuf
[MAXPATHLEN
+1];
568 _DIAGASSERT(pattern
!= NULL
);
569 _DIAGASSERT(pglob
!= NULL
);
571 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
575 * we save one character so that we can use ptr >= limit,
576 * in the general case when we are appending non nul chars only.
578 return glob2(pathbuf
, pathbuf
,
579 pathbuf
+ (sizeof(pathbuf
) / sizeof(*pathbuf
)) - 1, pattern
,
584 * The functions glob2 and glob3 are mutually recursive; there is one level
585 * of recursion for each segment in the pattern that contains one or more
589 glob2(Char
*pathbuf
, Char
*pathend
, Char
*pathlim
, Char
*pattern
, glob_t
*pglob
,
598 _DIAGASSERT(pathbuf
!= NULL
);
599 _DIAGASSERT(pathend
!= NULL
);
600 _DIAGASSERT(pattern
!= NULL
);
601 _DIAGASSERT(pglob
!= NULL
);
604 * Loop over pattern segments until end of pattern or until
605 * segment with meta character found.
607 for (anymeta
= 0;;) {
608 if (*pattern
== EOS
) { /* End of pattern? */
610 if (g_lstat(pathbuf
, &sb
, pglob
))
613 if (((pglob
->gl_flags
& GLOB_MARK
) &&
614 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
) ||
615 (S_ISLNK(sb
.st_mode
) &&
616 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
617 S_ISDIR(sb
.st_mode
)))) {
618 if (pathend
>= pathlim
)
624 return globextend(pathbuf
, pglob
, limit
);
627 /* Find end of next segment, copy tentatively to pathend. */
630 while (*p
!= EOS
&& *p
!= SEP
) {
639 * No expansion, or path ends in slash-dot shash-dot-dot,
642 if (pglob
->gl_flags
& GLOB_PERIOD
) {
643 for (pend
= pathend
; pend
> pathbuf
&& pend
[-1] == '/';
646 diff
= pend
- pathbuf
;
654 ((pglob
->gl_flags
& GLOB_PERIOD
) &&
655 (diff
>= 1 && pend
[-1] == DOT
) &&
656 (diff
>= 2 && (pend
[-2] == SLASH
|| pend
[-2] == DOT
)) &&
657 (diff
< 3 || pend
[-3] == SLASH
))) {
660 while (*pattern
== SEP
) {
661 if (pathend
>= pathlim
)
663 *pathend
++ = *pattern
++;
665 } else /* Need expansion, recurse. */
666 return glob3(pathbuf
, pathend
, pathlim
, pattern
, p
,
673 glob3(Char
*pathbuf
, Char
*pathend
, Char
*pathlim
, Char
*pattern
,
674 Char
*restpattern
, glob_t
*pglob
, size_t *limit
)
679 char buf
[MAXPATHLEN
];
682 * The readdirfunc declaration can't be prototyped, because it is
683 * assigned, below, to two functions which are prototyped in glob.h
684 * and dirent.h as taking pointers to differently typed opaque
687 struct dirent
*(*readdirfunc
)(void *);
689 _DIAGASSERT(pathbuf
!= NULL
);
690 _DIAGASSERT(pathend
!= NULL
);
691 _DIAGASSERT(pattern
!= NULL
);
692 _DIAGASSERT(restpattern
!= NULL
);
693 _DIAGASSERT(pglob
!= NULL
);
698 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
699 if (pglob
->gl_errfunc
) {
700 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
702 if (pglob
->gl_errfunc(buf
, errno
) ||
703 pglob
->gl_flags
& GLOB_ERR
)
707 * Posix/XOpen: glob should return when it encounters a
708 * directory that it cannot open or read
709 * XXX: Should we ignore ENOTDIR and ENOENT though?
710 * I think that Posix had in mind EPERM...
712 if (pglob
->gl_flags
& GLOB_ERR
)
720 /* Search directory for matching names. */
721 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
722 readdirfunc
= pglob
->gl_readdir
;
724 readdirfunc
= (struct dirent
*(*)__P((void *))) readdir
;
725 while ((dp
= (*readdirfunc
)(dirp
)) != NULL
) {
730 * Initial DOT must be matched literally, unless we have
733 if ((pglob
->gl_flags
& GLOB_PERIOD
) == 0)
734 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
737 * If GLOB_NO_DOTDIRS is set, . and .. vanish.
739 if ((pglob
->gl_flags
& GLOB_NO_DOTDIRS
) &&
740 (dp
->d_name
[0] == DOT
) &&
741 ((dp
->d_name
[1] == EOS
) ||
742 ((dp
->d_name
[1] == DOT
) && (dp
->d_name
[2] == EOS
))))
745 * The resulting string contains EOS, so we can
746 * use the pathlim character, if it is the nul
748 for (sc
= (u_char
*) dp
->d_name
, dc
= pathend
;
749 dc
<= pathlim
&& (*dc
++ = *sc
++) != EOS
;)
753 * Have we filled the buffer without seeing EOS?
755 if (dc
> pathlim
&& *pathlim
!= EOS
) {
757 * Abort when requested by caller, otherwise
758 * reset pathend back to last SEP and continue
759 * with next dir entry.
761 if (pglob
->gl_flags
& GLOB_ERR
) {
762 error
= GLOB_ABORTED
;
771 if (!match(pathend
, pattern
, restpattern
)) {
775 error
= glob2(pathbuf
, --dc
, pathlim
, restpattern
, pglob
, limit
);
780 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
781 (*pglob
->gl_closedir
)(dirp
);
786 * Again Posix X/Open issue with regards to error handling.
788 if ((error
|| errno
) && (pglob
->gl_flags
& GLOB_ERR
))
796 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
797 * add the new item, and update gl_pathc.
799 * This assumes the BSD realloc, which only copies the block when its size
800 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
803 * Return 0 if new item added, error code if memory couldn't be allocated.
805 * Invariant of the glob_t structure:
806 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
807 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
810 globextend(const Char
*path
, glob_t
*pglob
, size_t *limit
)
813 size_t i
, newsize
, len
;
817 _DIAGASSERT(path
!= NULL
);
818 _DIAGASSERT(pglob
!= NULL
);
820 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
821 pathv
= pglob
->gl_pathv
? realloc(pglob
->gl_pathv
, newsize
) :
826 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
827 /* first time around -- clear initial gl_offs items */
828 pathv
+= pglob
->gl_offs
;
829 for (i
= pglob
->gl_offs
+ 1; --i
> 0; )
832 pglob
->gl_pathv
= pathv
;
834 for (p
= path
; *p
++;)
836 len
= (size_t)(p
- path
);
838 if ((copy
= malloc(len
)) != NULL
) {
839 if (g_Ctoc(path
, copy
, len
)) {
843 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
845 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
847 if ((pglob
->gl_flags
& GLOB_LIMIT
) && (newsize
+ *limit
) >= ARG_MAX
) {
852 return copy
== NULL
? GLOB_NOSPACE
: 0;
857 * pattern matching function for filenames. Each occurrence of the *
858 * pattern causes a recursion level.
861 match(Char
*name
, Char
*pat
, Char
*patend
)
863 int ok
, negate_range
;
866 _DIAGASSERT(name
!= NULL
);
867 _DIAGASSERT(pat
!= NULL
);
868 _DIAGASSERT(patend
!= NULL
);
870 while (pat
< patend
) {
872 switch (c
& M_MASK
) {
877 if (match(name
, pat
, patend
))
879 while (*name
++ != EOS
);
887 if ((k
= *name
++) == EOS
)
889 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
891 while (((c
= *pat
++) & M_MASK
) != M_END
)
892 if ((*pat
& M_MASK
) == M_RNG
) {
893 if (c
<= k
&& k
<= pat
[1])
898 if (ok
== negate_range
)
910 /* Free allocated data belonging to a glob_t structure. */
912 globfree(glob_t
*pglob
)
917 _DIAGASSERT(pglob
!= NULL
);
919 if (pglob
->gl_pathv
!= NULL
) {
920 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
921 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
924 free(pglob
->gl_pathv
);
925 pglob
->gl_pathv
= NULL
;
930 #ifndef __LIBC12_SOURCE__
932 glob_pattern_p(const char *pattern
, int quote
)
936 for (; *pattern
; pattern
++)
943 if (quote
&& pattern
[1] != '\0')
964 g_opendir(Char
*str
, glob_t
*pglob
)
966 char buf
[MAXPATHLEN
];
968 _DIAGASSERT(str
!= NULL
);
969 _DIAGASSERT(pglob
!= NULL
);
972 (void)strlcpy(buf
, ".", sizeof(buf
));
974 if (g_Ctoc(str
, buf
, sizeof(buf
)))
978 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
979 return (*pglob
->gl_opendir
)(buf
);
985 g_lstat(Char
*fn
, __gl_stat_t
*sb
, glob_t
*pglob
)
987 char buf
[MAXPATHLEN
];
989 _DIAGASSERT(fn
!= NULL
);
990 _DIAGASSERT(sb
!= NULL
);
991 _DIAGASSERT(pglob
!= NULL
);
993 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
995 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
996 return (*pglob
->gl_lstat
)(buf
, sb
);
997 return lstat(buf
, sb
);
1001 g_stat(Char
*fn
, __gl_stat_t
*sb
, glob_t
*pglob
)
1003 char buf
[MAXPATHLEN
];
1005 _DIAGASSERT(fn
!= NULL
);
1006 _DIAGASSERT(sb
!= NULL
);
1007 _DIAGASSERT(pglob
!= NULL
);
1009 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
1011 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
1012 return (*pglob
->gl_stat
)(buf
, sb
);
1013 return stat(buf
, sb
);
1017 g_strchr(const Char
*str
, int ch
)
1020 _DIAGASSERT(str
!= NULL
);
1024 return __UNCONST(str
);
1030 g_Ctoc(const Char
*str
, char *buf
, size_t len
)
1034 _DIAGASSERT(str
!= NULL
);
1035 _DIAGASSERT(buf
!= NULL
);
1040 for (dc
= buf
; len
&& (*dc
++ = *str
++) != EOS
; len
--)
1048 qprintf(const char *str
, Char
*s
)
1052 _DIAGASSERT(str
!= NULL
);
1053 _DIAGASSERT(s
!= NULL
);
1055 (void)printf("%s:\n", str
);
1056 for (p
= s
; *p
; p
++)
1057 (void)printf("%c", CHAR(*p
));
1059 for (p
= s
; *p
; p
++)
1060 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
1062 for (p
= s
; *p
; p
++)
1063 (void)printf("%c", ismeta(*p
) ? '_' : ' ');