1 /* $NetBSD: __glob13.c,v 1.1.2.1 1997/10/22 06:41:27 thorpej 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 * glob(3) -- a superset of the one defined in POSIX 1003.2.
44 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
46 * Optional extra services, controlled by flags not defined by POSIX:
49 * Escaping convention: \ inhibits any special meaning the following
50 * character might have (except \ at end of string is retained).
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 * Number of matches in the current invocation of glob.
68 #include <sys/param.h>
84 #ifdef __LIBC12_SOURCE__
85 __weak_alias(glob
,_glob
);
86 __weak_alias(globfree
,_globfree
);
88 #error "XXX THESE ARE NOT RIGHT!"
89 __weak_alias(__glob13
,___glob13
);
90 __weak_alias(__globfree13
,___globfree13
);
91 #endif /* __LIBC12_SOURCE__ */
92 #endif /* __weak_alias */
94 #ifdef __LIBC12_SOURCE__
97 #if defined (__INSIDE_CYGWIN__)
116 #define UNDERSCORE '_'
124 #define M_QUOTE 0x8000
125 #define M_PROTECT 0x4000
126 #define M_MASK 0xffff
127 #define M_ASCII 0x00ff
129 typedef u_short Char
;
134 #define M_PROTECT 0x40
143 #define CHAR(c) ((Char)((c)&M_ASCII))
144 #define META(c) ((Char)((c)|M_QUOTE))
145 #define M_ALL META('*')
146 #define M_END META(']')
147 #define M_NOT META('!')
148 #define M_ONE META('?')
149 #define M_RNG META('-')
150 #define M_SET META('[')
151 #define ismeta(c) (((c)&M_QUOTE) != 0)
154 static int compare
__P((const void *, const void *));
155 static void g_Ctoc
__P((const Char
*, char *));
156 static int g_lstat
__P((Char
*, struct STAT
*, glob_t
*));
157 static DIR *g_opendir
__P((Char
*, glob_t
*));
158 static Char
*g_strchr
__P((Char
*, int));
160 static Char
*g_strcat
__P((Char
*, const Char
*));
162 static int g_stat
__P((Char
*, struct STAT
*, glob_t
*));
163 static int glob0
__P((const Char
*, glob_t
*));
164 static int glob1
__P((Char
*, glob_t
*));
165 static int glob2
__P((Char
*, Char
*, Char
*, glob_t
*));
166 static int glob3
__P((Char
*, Char
*, Char
*, Char
*, glob_t
*));
167 static int globextend
__P((const Char
*, glob_t
*));
168 static const Char
* globtilde
__P((const Char
*, Char
*, glob_t
*));
169 static int globexp1
__P((const Char
*, glob_t
*));
170 static int globexp2
__P((const Char
*, const Char
*, glob_t
*, int *));
171 static int match
__P((Char
*, Char
*, Char
*));
173 static void qprintf
__P((const char *, Char
*));
177 #define MAXPATHLEN 16384
179 extern BOOL ignore_case_with_glob
;
182 glob(pattern
, flags
, errfunc
, pglob
)
184 int flags
, (*errfunc
) __P((const char *, int));
187 const u_char
*patnext
;
189 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
191 patnext
= (u_char
*) pattern
;
192 if (!(flags
& GLOB_APPEND
)) {
194 pglob
->gl_pathv
= NULL
;
195 if (!(flags
& GLOB_DOOFFS
))
198 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
199 pglob
->gl_errfunc
= errfunc
;
200 pglob
->gl_matchc
= 0;
203 bufend
= bufnext
+ MAXPATHLEN
;
204 if (flags
& GLOB_QUOTE
) {
205 /* Protect the quoted characters. */
206 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
208 if ((c
= *patnext
++) == EOS
) {
212 *bufnext
++ = c
| M_PROTECT
;
218 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
222 if (flags
& GLOB_BRACE
)
223 return globexp1(patbuf
, pglob
);
225 return glob0(patbuf
, pglob
);
229 * Expand recursively a glob {} pattern. When there is no more expansion
230 * invoke the standard globbing routine to glob the rest of the magic
233 static int globexp1(pattern
, pglob
)
237 const Char
* ptr
= pattern
;
240 /* Protect a single {}, for find(1), like csh */
241 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
242 return glob0(pattern
, pglob
);
244 while ((ptr
= (const Char
*) g_strchr((Char
*) ptr
, LBRACE
)) != NULL
)
245 if (!globexp2(ptr
, pattern
, pglob
, &rv
))
248 return glob0(pattern
, pglob
);
253 * Recursive brace globbing helper. Tries to expand a single brace.
254 * If it succeeds then it invokes globexp1 with the new pattern.
255 * If it fails then it tries to glob the rest of the pattern and returns.
257 static int globexp2(ptr
, pattern
, pglob
, rv
)
258 const Char
*ptr
, *pattern
;
264 const Char
*pe
, *pm
, *pl
;
265 Char patbuf
[MAXPATHLEN
+ 1];
267 /* copy part up to the brace */
268 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
272 /* Find the balanced brace */
273 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
274 if (*pe
== LBRACKET
) {
275 /* Ignore everything between [] */
276 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
280 * We could not find a matching RBRACKET.
281 * Ignore and just look for RBRACE
286 else if (*pe
== LBRACE
)
288 else if (*pe
== RBRACE
) {
294 /* Non matching braces; just glob the pattern */
295 if (i
!= 0 || *pe
== EOS
) {
296 *rv
= glob0(patbuf
, pglob
);
300 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
303 /* Ignore everything between [] */
304 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
308 * We could not find a matching RBRACKET.
309 * Ignore and just look for RBRACE
326 if (i
&& *pm
== COMMA
)
329 /* Append the current string */
330 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
333 * Append the rest of the pattern after the
336 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
339 /* Expand the current pattern */
341 qprintf("globexp2:", patbuf
);
343 *rv
= globexp1(patbuf
, pglob
);
345 /* move after the comma, to the next string */
360 * expand tilde from the passwd file.
363 globtilde(pattern
, patbuf
, pglob
)
373 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
376 /* Copy up to the end of the string or / */
377 for (p
= pattern
+ 1, h
= (char *) patbuf
; *p
&& *p
!= SLASH
;
383 if (((char *) patbuf
)[0] == EOS
) {
385 * handle a plain ~ or ~/ by expanding $HOME
386 * first and then trying the password file
388 if ((h
= getenv("HOME")) == NULL
) {
389 if ((pwd
= getpwuid(getuid())) == NULL
)
399 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
405 /* Copy the home directory */
406 for (b
= patbuf
; *h
; *b
++ = *h
++)
409 /* Append the rest of the pattern */
410 while ((*b
++ = *p
++) != EOS
)
418 * The main glob() routine: compiles the pattern (optionally processing
419 * quotes), calls glob1() to do the real pattern matching, and finally
420 * sorts the list (unless unsorted operation is requested). Returns 0
421 * if things went well, nonzero if errors occurred. It is not an error
422 * to find no matches.
425 glob0(pattern
, pglob
)
429 const Char
*qpatnext
;
430 int c
, err
, oldpathc
;
431 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
433 qpatnext
= globtilde(pattern
, patbuf
, pglob
);
434 oldpathc
= pglob
->gl_pathc
;
437 /* We don't need to check for buffer overflow any more. */
438 while ((c
= *qpatnext
++) != EOS
) {
444 if (*qpatnext
== EOS
||
445 g_strchr((Char
*) qpatnext
+1, RBRACKET
) == NULL
) {
446 *bufnext
++ = LBRACKET
;
456 *bufnext
++ = CHAR(c
);
457 if (*qpatnext
== RANGE
&&
458 (c
= qpatnext
[1]) != RBRACKET
) {
460 *bufnext
++ = CHAR(c
);
463 } while ((c
= *qpatnext
++) != RBRACKET
);
464 pglob
->gl_flags
|= GLOB_MAGCHAR
;
468 pglob
->gl_flags
|= GLOB_MAGCHAR
;
472 pglob
->gl_flags
|= GLOB_MAGCHAR
;
473 /* collapse adjacent stars to one,
474 * to avoid exponential behavior
476 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
480 *bufnext
++ = CHAR(c
);
486 qprintf("glob0:", patbuf
);
489 if ((err
= glob1(patbuf
, pglob
)) != 0)
493 * If there was no match we are going to append the pattern
494 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
495 * and the pattern did not contain any magic characters
496 * GLOB_NOMAGIC is there just for compatibility with csh.
498 if (pglob
->gl_pathc
== oldpathc
&&
499 ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
500 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
501 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
502 return(globextend(pattern
, pglob
));
503 else if (!(pglob
->gl_flags
& GLOB_NOSORT
))
504 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
505 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
513 return(strcmp(*(char **)p
, *(char **)q
));
517 glob1(pattern
, pglob
)
521 Char pathbuf
[MAXPATHLEN
+1];
523 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
526 return(glob2(pathbuf
, pathbuf
, pattern
, pglob
));
530 * The functions glob2 and glob3 are mutually recursive; there is one level
531 * of recursion for each segment in the pattern that contains one or more
535 glob2(pathbuf
, pathend
, pattern
, pglob
)
536 Char
*pathbuf
, *pathend
, *pattern
;
544 * Loop over pattern segments until end of pattern or until
545 * segment with meta character found.
547 for (anymeta
= 0;;) {
548 if (*pattern
== EOS
) { /* End of pattern? */
550 if (g_lstat(pathbuf
, &sb
, pglob
))
553 if (((pglob
->gl_flags
& GLOB_MARK
) &&
554 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
555 || (S_ISLNK(sb
.st_mode
) &&
556 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
557 S_ISDIR(sb
.st_mode
)))) {
562 return(globextend(pathbuf
, pglob
));
565 /* Find end of next segment, copy tentatively to pathend. */
568 while (*p
!= EOS
&& *p
!= SEP
) {
574 if (!anymeta
) { /* No expansion, do next segment. */
577 while (*pattern
== SEP
)
578 *pathend
++ = *pattern
++;
579 } else /* Need expansion, recurse. */
580 return(glob3(pathbuf
, pathend
, pattern
, p
, pglob
));
586 glob3(pathbuf
, pathend
, pattern
, restpattern
, pglob
)
587 Char
*pathbuf
, *pathend
, *pattern
, *restpattern
;
590 register struct dirent
*dp
;
593 char buf
[MAXPATHLEN
];
596 * The readdirfunc declaration can't be prototyped, because it is
597 * assigned, below, to two functions which are prototyped in glob.h
598 * and dirent.h as taking pointers to differently typed opaque
601 struct dirent
*(*readdirfunc
) __P((void *));
606 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
607 /* TODO: don't call for ENOENT or ENOTDIR? */
608 if (pglob
->gl_errfunc
) {
609 g_Ctoc(pathbuf
, buf
);
610 if (pglob
->gl_errfunc(buf
, errno
) ||
611 pglob
->gl_flags
& GLOB_ERR
)
619 /* Search directory for matching names. */
620 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
621 readdirfunc
= pglob
->gl_readdir
;
623 readdirfunc
= (struct dirent
*(*)__P((void *))) readdir
;
624 while ((dp
= (*readdirfunc
)(dirp
))) {
628 /* Initial DOT must be matched literally. */
629 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
631 for (sc
= (u_char
*) dp
->d_name
, dc
= pathend
;
632 (*dc
++ = *sc
++) != EOS
;)
634 if (!match(pathend
, pattern
, restpattern
)) {
638 err
= glob2(pathbuf
, --dc
, restpattern
, pglob
);
643 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
644 (*pglob
->gl_closedir
)(dirp
);
652 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
653 * add the new item, and update gl_pathc.
655 * This assumes the BSD realloc, which only copies the block when its size
656 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
659 * Return 0 if new item added, error code if memory couldn't be allocated.
661 * Invariant of the glob_t structure:
662 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
663 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
666 globextend(path
, pglob
)
670 register char **pathv
;
676 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
677 pathv
= pglob
->gl_pathv
?
678 realloc((char *)pglob
->gl_pathv
, newsize
) :
681 return(GLOB_NOSPACE
);
683 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
684 /* first time around -- clear initial gl_offs items */
685 pathv
+= pglob
->gl_offs
;
686 for (i
= pglob
->gl_offs
; --i
>= 0;)
689 pglob
->gl_pathv
= pathv
;
691 for (p
= path
; *p
++;)
693 if ((copy
= malloc(p
- path
)) != NULL
) {
695 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
697 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
698 return(copy
== NULL
? GLOB_NOSPACE
: 0);
703 * pattern matching function for filenames. Each occurrence of the *
704 * pattern causes a recursion level.
707 match(name
, pat
, patend
)
708 register Char
*name
, *pat
, *patend
;
710 int ok
, negate_range
;
713 while (pat
< patend
) {
715 switch (c
& M_MASK
) {
720 if (match(name
, pat
, patend
))
722 while (*name
++ != EOS
);
730 if ((k
= *name
++) == EOS
)
732 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
734 if (ignore_case_with_glob
)
736 while (((c
= *pat
++) & M_MASK
) != M_END
)
737 if ((*pat
& M_MASK
) == M_RNG
) {
738 if (tolower(c
) <= tolower(k
) && tolower(k
) <= tolower(pat
[1]))
741 } else if (tolower(c
) == tolower(k
))
746 while (((c
= *pat
++) & M_MASK
) != M_END
)
747 if ((*pat
& M_MASK
) == M_RNG
) {
748 if (c
<= k
&& k
<= pat
[1])
754 if (ok
== negate_range
)
758 if (ignore_case_with_glob
)
760 if (tolower(*name
) != tolower(c
))
772 return(*name
== EOS
);
775 /* Free allocated data belonging to a glob_t structure. */
783 if (pglob
->gl_pathv
!= NULL
) {
784 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
785 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
788 free(pglob
->gl_pathv
);
793 g_opendir(str
, pglob
)
797 char buf
[MAXPATHLEN
];
804 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
805 return((*pglob
->gl_opendir
)(buf
));
807 return(opendir(buf
));
811 stat32_to_STAT (struct __stat32
*src
, struct STAT
*dst
)
813 dst
->st_dev
= src
->st_dev
;
814 dst
->st_ino
= src
->st_ino
;
815 dst
->st_mode
= src
->st_mode
;
816 dst
->st_nlink
= src
->st_nlink
;
817 dst
->st_uid
= src
->st_uid
;
818 dst
->st_gid
= src
->st_gid
;
819 dst
->st_rdev
= src
->st_rdev
;
820 dst
->st_size
= src
->st_size
;
821 dst
->st_atim
= src
->st_atim
;
822 dst
->st_mtim
= src
->st_mtim
;
823 dst
->st_ctim
= src
->st_ctim
;
824 dst
->st_blksize
= src
->st_blksize
;
825 dst
->st_blocks
= src
->st_blocks
;
829 g_lstat(fn
, sb
, pglob
)
834 /* FIXME: This only works as long as the application uses the old
835 struct stat with 32 bit off_t types!!!
837 As soon as we switch over to 64 bit, we have to decide by
838 the applications API minor version number, whether to use
839 a pointer to a __stat64 or a _stat32 struct to the
840 pglob->gl_lstat function. */
841 #ifdef __CYGWIN_USE_BIG_TYPES__
842 #error FIXME check apps API minor and use correct struct stat
844 char buf
[MAXPATHLEN
];
847 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
) {
851 if (!(ret
= (*pglob
->gl_lstat
)(buf
, &lsb
)))
852 stat32_to_STAT (&lsb
, sb
);
855 #ifdef __INSIDE_CYGWIN__
856 return(lstat64(buf
, sb
));
858 return(lstat(buf
, sb
));
863 g_stat(fn
, sb
, pglob
)
868 /* FIXME: This only works as long as the application uses the old
869 struct stat with 32 bit off_t types!!!
871 As soon as we switch over to 64 bit, we have to decide by
872 the applications API minor version number, whether to use
873 a pointer to a __stat64 or a _stat32 struct to the
874 pglob->gl_stat function. */
875 #ifdef __CYGWIN_USE_BIG_TYPES__
876 #error FIXME check apps API minor and use correct struct stat
878 char buf
[MAXPATHLEN
];
881 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
) {
885 if (!(ret
= (*pglob
->gl_stat
)(buf
, &lsb
)))
886 stat32_to_STAT (&lsb
, sb
);
889 #ifdef __INSIDE_CYGWIN__
890 return(stat64(buf
, sb
));
892 return(stat(buf
, sb
));
919 while((*dst
++ = *src
++) != EOS
)
928 register const Char
*str
;
933 for (dc
= buf
; (*dc
++ = *str
++) != EOS
;)
945 (void)printf("%s:\n", str
);
947 (void)printf("%c", CHAR(*p
));
950 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
953 (void)printf("%c", ismeta(*p
) ? '_' : ' ');