]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/PosixLib/Glob/glob.c
2 * glob(3) -- a superset of the one defined in POSIX 1003.2.
4 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
6 * Optional extra services, controlled by flags not defined by POSIX:
9 * Set in gl_flags if pattern contained a globbing character.
11 * Same as GLOB_NOCHECK, but it will only append pattern if it did
12 * not contain any magic characters. [Used in csh style globbing]
14 * Use alternately specified directory access functions.
16 * expand ~user/foo to the /home/dir/of/user/foo
18 * expand {1,2}{a,b} to 1a 1b 2a 2b
20 * allow metacharacters to match leading dots in filenames.
22 * . and .. are hidden from wildcards, even if GLOB_PERIOD is set.
24 * Number of matches in the current invocation of glob.
26 * Copyright (c) 1989, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 glob.c 8.3 (Berkeley) 10/13/93
57 NetBSD: glob.c,v 1.23.4.1 2010/07/19 18:14:08 riz Exp
59 #pragma warning ( disable : 4244 )
60 #pragma warning ( disable : 4018 )
62 #include <LibConfig.h>
64 #include <sys/cdefs.h>
66 #include <sys/param.h>
80 #include <sys/fcntl.h>
83 #ifdef HAVE_NBTOOL_CONFIG_H
87 #define GLOB_LIMIT_MALLOC 65536
88 #define GLOB_LIMIT_STAT 128
89 #define GLOB_LIMIT_READDIR 16384
91 #define GLOB_INDEX_MALLOC 0
92 #define GLOB_INDEX_STAT 1
93 #define GLOB_INDEX_READDIR 2
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)
151 static int compare(const void *, const void *);
152 static int g_Ctoc(const Char
*, char *, size_t);
153 static int g_lstat(Char
*, __gl_stat_t
*, glob_t
*);
154 static DIR *g_opendir(Char
*, glob_t
*);
155 static Char
*g_strchr(const Char
*, int);
156 static int g_stat(Char
*, __gl_stat_t
*, glob_t
*);
157 static int glob0(const Char
*, glob_t
*, size_t *);
158 static int glob1(Char
*, glob_t
*, size_t *);
159 static int glob2(Char
*, Char
*, Char
*, Char
*, glob_t
*, size_t *);
160 static int glob3(Char
*, Char
*, Char
*, Char
*, Char
*, glob_t
*, size_t *);
161 static int globextend(const Char
*, glob_t
*, size_t *);
162 static const Char
*globtilde(const Char
*, Char
*, size_t, glob_t
*);
163 static int globexp1(const Char
*, glob_t
*, size_t *);
164 static int globexp2(const Char
*, const Char
*, glob_t
*, int *, size_t *);
165 static int match(Char
*, Char
*, Char
*);
167 static void qprintf(const char *, Char
*);
174 int (*errfunc
)(const char *, int),
178 const u_char
*patnext
;
180 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
181 /* 0 = malloc(), 1 = stat(), 2 = readdir() */
182 size_t limit
[] = { 0, 0, 0 };
184 _DIAGASSERT(pattern
!= NULL
);
186 patnext
= (const u_char
*) pattern
;
187 if (!(flags
& GLOB_APPEND
)) {
189 pglob
->gl_pathv
= NULL
;
190 if (!(flags
& GLOB_DOOFFS
))
193 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
194 pglob
->gl_errfunc
= errfunc
;
195 pglob
->gl_matchc
= 0;
198 bufend
= bufnext
+ MAXPATHLEN
;
199 if (flags
& GLOB_NOESCAPE
) {
200 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
203 /* Protect the quoted characters. */
204 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
206 if ((c
= *patnext
++) == EOS
) {
210 *bufnext
++ = c
| M_PROTECT
;
217 if (flags
& GLOB_BRACE
)
218 return globexp1(patbuf
, pglob
, limit
);
220 return glob0(patbuf
, pglob
, limit
);
224 * Expand recursively a glob {} pattern. When there is no more expansion
225 * invoke the standard globbing routine to glob the rest of the magic
229 globexp1(const Char
*pattern
, glob_t
*pglob
, size_t *limit
)
231 const Char
* ptr
= pattern
;
234 _DIAGASSERT(pattern
!= NULL
);
235 _DIAGASSERT(pglob
!= NULL
);
237 /* Protect a single {}, for find(1), like csh */
238 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
239 return glob0(pattern
, pglob
, limit
);
241 while ((ptr
= (const Char
*) g_strchr(ptr
, LBRACE
)) != NULL
)
242 if (!globexp2(ptr
, pattern
, pglob
, &rv
, limit
))
245 return glob0(pattern
, pglob
, limit
);
250 * Recursive brace globbing helper. Tries to expand a single brace.
251 * If it succeeds then it invokes globexp1 with the new pattern.
252 * If it fails then it tries to glob the rest of the pattern and returns.
255 globexp2(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
, int *rv
,
260 const Char
*pe
, *pm
, *pl
;
261 Char patbuf
[MAXPATHLEN
+ 1];
263 _DIAGASSERT(ptr
!= NULL
);
264 _DIAGASSERT(pattern
!= NULL
);
265 _DIAGASSERT(pglob
!= NULL
);
266 _DIAGASSERT(rv
!= NULL
);
268 /* copy part up to the brace */
269 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
273 /* Find the balanced brace */
274 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
275 if (*pe
== LBRACKET
) {
276 /* Ignore everything between [] */
277 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
281 * We could not find a matching RBRACKET.
282 * Ignore and just look for RBRACE
287 else if (*pe
== LBRACE
)
289 else if (*pe
== RBRACE
) {
295 /* Non matching braces; just glob the pattern */
296 if (i
!= 0 || *pe
== EOS
) {
298 * we use `pattern', not `patbuf' here so that that
299 * unbalanced braces are passed to the match
301 *rv
= glob0(pattern
, pglob
, limit
);
305 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++) {
308 /* Ignore everything between [] */
309 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
313 * We could not find a matching RBRACKET.
314 * Ignore and just look for RBRACE
331 if (i
&& *pm
== COMMA
)
334 /* Append the current string */
335 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
338 * Append the rest of the pattern after the
341 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
344 /* Expand the current pattern */
346 qprintf("globexp2:", patbuf
);
348 *rv
= globexp1(patbuf
, pglob
, limit
);
350 /* move after the comma, to the next string */
366 * expand tilde from the passwd file.
369 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patsize
, glob_t
*pglob
)
375 Char
*pend
= &patbuf
[patsize
/ sizeof(Char
)];
379 _DIAGASSERT(pattern
!= NULL
);
380 _DIAGASSERT(patbuf
!= NULL
);
381 _DIAGASSERT(pglob
!= NULL
);
383 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
386 /* Copy up to the end of the string or / */
387 for (p
= pattern
+ 1, d
= (char *)(void *)patbuf
;
388 d
< (char *)(void *)pend
&& *p
&& *p
!= SLASH
;
392 if (d
== (char *)(void *)pend
)
396 d
= (char *)(void *)patbuf
;
400 * handle a plain ~ or ~/ by expanding $HOME
401 * first and then trying the password file
403 if ((h
= getenv("HOME")) == NULL
) {
414 /* Copy the home directory */
415 for (b
= patbuf
; b
< pend
&& *h
; *b
++ = *h
++)
421 /* Append the rest of the pattern */
422 while (b
< pend
&& (*b
++ = *p
++) != EOS
)
433 * The main glob() routine: compiles the pattern (optionally processing
434 * quotes), calls glob1() to do the real pattern matching, and finally
435 * sorts the list (unless unsorted operation is requested). Returns 0
436 * if things went well, nonzero if errors occurred. It is not an error
437 * to find no matches.
440 glob0(const Char
*pattern
, glob_t
*pglob
, size_t *limit
)
442 const Char
*qpatnext
;
444 __gl_size_t oldpathc
;
445 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
447 _DIAGASSERT(pattern
!= NULL
);
448 _DIAGASSERT(pglob
!= NULL
);
450 if ((qpatnext
= globtilde(pattern
, patbuf
, sizeof(patbuf
),
453 oldpathc
= pglob
->gl_pathc
;
456 /* We don't need to check for buffer overflow any more. */
457 while ((c
= *qpatnext
++) != EOS
) {
463 if (*qpatnext
== EOS
||
464 g_strchr(qpatnext
+1, RBRACKET
) == NULL
) {
465 *bufnext
++ = LBRACKET
;
475 *bufnext
++ = CHAR(c
);
476 if (*qpatnext
== RANGE
&&
477 (c
= qpatnext
[1]) != RBRACKET
) {
479 *bufnext
++ = CHAR(c
);
482 } while ((c
= *qpatnext
++) != RBRACKET
);
483 pglob
->gl_flags
|= GLOB_MAGCHAR
;
487 pglob
->gl_flags
|= GLOB_MAGCHAR
;
491 pglob
->gl_flags
|= GLOB_MAGCHAR
;
492 /* collapse adjacent stars to one,
493 * to avoid exponential behavior
495 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
499 *bufnext
++ = CHAR(c
);
505 qprintf("glob0:", patbuf
);
508 if ((error
= glob1(patbuf
, pglob
, limit
)) != 0)
511 if (pglob
->gl_pathc
== oldpathc
) {
513 * If there was no match we are going to append the pattern
514 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was
515 * specified and the pattern did not contain any magic
516 * characters GLOB_NOMAGIC is there just for compatibility
519 if ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
520 ((pglob
->gl_flags
& (GLOB_NOMAGIC
|GLOB_MAGCHAR
))
522 return globextend(pattern
, pglob
, limit
);
526 } else if (!(pglob
->gl_flags
& GLOB_NOSORT
)) {
527 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
528 (size_t)pglob
->gl_pathc
- oldpathc
, sizeof(char *),
536 compare(const void *p
, const void *q
)
539 _DIAGASSERT(p
!= NULL
);
540 _DIAGASSERT(q
!= NULL
);
542 return strcoll(*(const char * const *)p
, *(const char * const *)q
);
546 glob1(Char
*pattern
, glob_t
*pglob
, size_t *limit
)
548 Char pathbuf
[MAXPATHLEN
+1];
550 _DIAGASSERT(pattern
!= NULL
);
551 _DIAGASSERT(pglob
!= NULL
);
553 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
557 * we save one character so that we can use ptr >= limit,
558 * in the general case when we are appending non nul chars only.
560 return glob2(pathbuf
, pathbuf
,
561 pathbuf
+ (sizeof(pathbuf
) / sizeof(*pathbuf
)) - 1, pattern
,
566 * The functions glob2 and glob3 are mutually recursive; there is one level
567 * of recursion for each segment in the pattern that contains one or more
571 glob2(Char
*pathbuf
, Char
*pathend
, Char
*pathlim
, Char
*pattern
, glob_t
*pglob
,
580 _DIAGASSERT(pathbuf
!= NULL
);
581 _DIAGASSERT(pathend
!= NULL
);
582 _DIAGASSERT(pattern
!= NULL
);
583 _DIAGASSERT(pglob
!= NULL
);
586 * Loop over pattern segments until end of pattern or until
587 * segment with meta character found.
589 for (anymeta
= 0;;) {
590 if (*pattern
== EOS
) { /* End of pattern? */
592 if (g_lstat(pathbuf
, &sb
, pglob
))
595 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
596 limit
[GLOB_INDEX_STAT
]++ >= GLOB_LIMIT_STAT
) {
602 if (((pglob
->gl_flags
& GLOB_MARK
) &&
603 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
) ||
604 // (S_ISLNK(sb.st_mode) &&
606 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
607 S_ISDIR(sb
.st_mode
)))) {
608 if (pathend
>= pathlim
)
614 return globextend(pathbuf
, pglob
, limit
);
617 /* Find end of next segment, copy tentatively to pathend. */
620 while (*p
!= EOS
&& *p
!= SEP
) {
629 * No expansion, or path ends in slash-dot shash-dot-dot,
632 if (pglob
->gl_flags
& GLOB_PERIOD
) {
633 for (pend
= pathend
; pend
> pathbuf
&& pend
[-1] == '/';
636 diff
= pend
- pathbuf
;
644 ((pglob
->gl_flags
& GLOB_PERIOD
) &&
645 (diff
>= 1 && pend
[-1] == DOT
) &&
646 (diff
>= 2 && (pend
[-2] == SLASH
|| pend
[-2] == DOT
)) &&
647 (diff
< 3 || pend
[-3] == SLASH
))) {
650 while (*pattern
== SEP
) {
651 if (pathend
>= pathlim
)
653 *pathend
++ = *pattern
++;
655 } else /* Need expansion, recurse. */
656 return glob3(pathbuf
, pathend
, pathlim
, pattern
, p
,
663 glob3(Char
*pathbuf
, Char
*pathend
, Char
*pathlim
, Char
*pattern
,
664 Char
*restpattern
, glob_t
*pglob
, size_t *limit
)
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
)(void *);
679 _DIAGASSERT(pathbuf
!= NULL
);
680 _DIAGASSERT(pathend
!= NULL
);
681 _DIAGASSERT(pattern
!= NULL
);
682 _DIAGASSERT(restpattern
!= NULL
);
683 _DIAGASSERT(pglob
!= NULL
);
688 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
689 if (pglob
->gl_errfunc
) {
690 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
692 if (pglob
->gl_errfunc(buf
, errno
) ||
693 pglob
->gl_flags
& GLOB_ERR
)
697 * Posix/XOpen: glob should return when it encounters a
698 * directory that it cannot open or read
699 * XXX: Should we ignore ENOTDIR and ENOENT though?
700 * I think that Posix had in mind EPERM...
702 if (pglob
->gl_flags
& GLOB_ERR
)
710 /* Search directory for matching names. */
711 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
712 readdirfunc
= pglob
->gl_readdir
;
714 readdirfunc
= (struct dirent
*(*)(void *)) readdir
;
715 while ((dp
= (*readdirfunc
)(dirp
)) != NULL
) {
719 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
720 limit
[GLOB_INDEX_READDIR
]++ >= GLOB_LIMIT_READDIR
) {
728 * Initial DOT must be matched literally, unless we have
731 if ((pglob
->gl_flags
& GLOB_PERIOD
) == 0)
732 if (dp
->FileName
[0] == DOT
&& *pattern
!= DOT
)
735 * If GLOB_NO_DOTDIRS is set, . and .. vanish.
737 if ((pglob
->gl_flags
& GLOB_NO_DOTDIRS
) &&
738 (dp
->FileName
[0] == DOT
) &&
739 ((dp
->FileName
[1] == EOS
) ||
740 ((dp
->FileName
[1] == DOT
) && (dp
->FileName
[2] == EOS
))))
743 * The resulting string contains EOS, so we can
744 * use the pathlim character, if it is the nul
746 for (sc
= (u_char
*) dp
->FileName
, dc
= pathend
;
747 dc
<= pathlim
&& (*dc
++ = *sc
++) != EOS
;)
751 * Have we filled the buffer without seeing EOS?
753 if (dc
> pathlim
&& *pathlim
!= EOS
) {
755 * Abort when requested by caller, otherwise
756 * reset pathend back to last SEP and continue
757 * with next dir entry.
759 if (pglob
->gl_flags
& GLOB_ERR
) {
760 error
= GLOB_ABORTED
;
769 if (!match(pathend
, pattern
, restpattern
)) {
773 error
= glob2(pathbuf
, --dc
, pathlim
, restpattern
, pglob
,
779 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
780 (*pglob
->gl_closedir
)(dirp
);
785 * Again Posix X/Open issue with regards to error handling.
787 if ((error
|| errno
) && (pglob
->gl_flags
& GLOB_ERR
))
795 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
796 * add the new item, and update gl_pathc.
798 * This assumes the BSD realloc, which only copies the block when its size
799 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
802 * Return 0 if new item added, error code if memory couldn't be allocated.
804 * Invariant of the glob_t structure:
805 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
806 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
809 globextend(const Char
*path
, glob_t
*pglob
, size_t *limit
)
812 size_t i
, newsize
, len
;
816 _DIAGASSERT(path
!= NULL
);
817 _DIAGASSERT(pglob
!= NULL
);
819 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
820 pathv
= pglob
->gl_pathv
? realloc(pglob
->gl_pathv
, newsize
) :
825 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
826 /* first time around -- clear initial gl_offs items */
827 pathv
+= pglob
->gl_offs
;
828 for (i
= pglob
->gl_offs
+ 1; --i
> 0; )
831 pglob
->gl_pathv
= pathv
;
833 for (p
= path
; *p
++;)
835 len
= (size_t)(p
- path
);
836 limit
[GLOB_INDEX_MALLOC
] += len
;
837 if ((copy
= malloc(len
)) != NULL
) {
838 if (g_Ctoc(path
, copy
, len
)) {
842 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
844 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
846 if ((pglob
->gl_flags
& GLOB_LIMIT
) &&
847 (newsize
+ limit
[GLOB_INDEX_MALLOC
]) >= GLOB_LIMIT_MALLOC
) {
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
;
931 g_opendir(Char
*str
, glob_t
*pglob
)
933 char buf
[MAXPATHLEN
];
935 _DIAGASSERT(str
!= NULL
);
936 _DIAGASSERT(pglob
!= NULL
);
939 (void)strlcpy(buf
, ".", sizeof(buf
));
941 if (g_Ctoc(str
, buf
, sizeof(buf
)))
945 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
946 return (*pglob
->gl_opendir
)(buf
);
952 g_lstat(Char
*fn
, __gl_stat_t
*sb
, glob_t
*pglob
)
954 char buf
[MAXPATHLEN
];
956 _DIAGASSERT(fn
!= NULL
);
957 _DIAGASSERT(sb
!= NULL
);
958 _DIAGASSERT(pglob
!= NULL
);
960 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
962 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
963 return (*pglob
->gl_lstat
)(buf
, sb
);
964 return lstat(buf
, sb
);
968 g_stat(Char
*fn
, __gl_stat_t
*sb
, glob_t
*pglob
)
970 char buf
[MAXPATHLEN
];
972 _DIAGASSERT(fn
!= NULL
);
973 _DIAGASSERT(sb
!= NULL
);
974 _DIAGASSERT(pglob
!= NULL
);
976 if (g_Ctoc(fn
, buf
, sizeof(buf
)))
978 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
979 return (*pglob
->gl_stat
)(buf
, sb
);
980 return stat(buf
, sb
);
984 g_strchr(const Char
*str
, int ch
)
987 _DIAGASSERT(str
!= NULL
);
991 return __UNCONST(str
);
997 g_Ctoc(const Char
*str
, char *buf
, size_t len
)
1001 _DIAGASSERT(str
!= NULL
);
1002 _DIAGASSERT(buf
!= NULL
);
1007 for (dc
= buf
; len
&& (*dc
++ = *str
++) != EOS
; len
--)
1015 qprintf(const char *str
, Char
*s
)
1019 _DIAGASSERT(str
!= NULL
);
1020 _DIAGASSERT(s
!= NULL
);
1022 (void)printf("%s:\n", str
);
1023 for (p
= s
; *p
; p
++)
1024 (void)printf("%c", CHAR(*p
));
1026 for (p
= s
; *p
; p
++)
1027 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
1029 for (p
= s
; *p
; p
++)
1030 (void)printf("%c", ismeta(*p
) ? '_' : ' ');