158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1989, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Guido van Rossum. 758f0484fSRodney W. Grimes * 83c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 93c87aa1dSDavid Chisnall * All rights reserved. 103c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 113c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 123c87aa1dSDavid Chisnall * 1358f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1458f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1558f0484fSRodney W. Grimes * are met: 1658f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1758f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1858f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 2058f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 2158f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2258f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2358f0484fSRodney W. Grimes * without specific prior written permission. 2458f0484fSRodney W. Grimes * 2558f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2658f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2758f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2858f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2958f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3058f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3158f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3258f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3358f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3458f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3558f0484fSRodney W. Grimes * SUCH DAMAGE. 3658f0484fSRodney W. Grimes */ 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; 4058f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 41b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 42b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes /* 4558f0484fSRodney W. Grimes * glob(3) -- a superset of the one defined in POSIX 1003.2. 4658f0484fSRodney W. Grimes * 4758f0484fSRodney W. Grimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh). 4858f0484fSRodney W. Grimes * 4958f0484fSRodney W. Grimes * Optional extra services, controlled by flags not defined by POSIX: 5058f0484fSRodney W. Grimes * 5158f0484fSRodney W. Grimes * GLOB_QUOTE: 5258f0484fSRodney W. Grimes * Escaping convention: \ inhibits any special meaning the following 5358f0484fSRodney W. Grimes * character might have (except \ at end of string is retained). 5458f0484fSRodney W. Grimes * GLOB_MAGCHAR: 5558f0484fSRodney W. Grimes * Set in gl_flags if pattern contained a globbing character. 5658f0484fSRodney W. Grimes * GLOB_NOMAGIC: 5758f0484fSRodney W. Grimes * Same as GLOB_NOCHECK, but it will only append pattern if it did 5858f0484fSRodney W. Grimes * not contain any magic characters. [Used in csh style globbing] 5958f0484fSRodney W. Grimes * GLOB_ALTDIRFUNC: 6058f0484fSRodney W. Grimes * Use alternately specified directory access functions. 6158f0484fSRodney W. Grimes * GLOB_TILDE: 6258f0484fSRodney W. Grimes * expand ~user/foo to the /home/dir/of/user/foo 6358f0484fSRodney W. Grimes * GLOB_BRACE: 6458f0484fSRodney W. Grimes * expand {1,2}{a,b} to 1a 1b 2a 2b 6558f0484fSRodney W. Grimes * gl_matchc: 6658f0484fSRodney W. Grimes * Number of matches in the current invocation of glob. 6758f0484fSRodney W. Grimes */ 6858f0484fSRodney W. Grimes 69e9346e01STim J. Robbins /* 70e9346e01STim J. Robbins * Some notes on multibyte character support: 71e9346e01STim J. Robbins * 1. Patterns with illegal byte sequences match nothing - even if 72e9346e01STim J. Robbins * GLOB_NOCHECK is specified. 73e9346e01STim J. Robbins * 2. Illegal byte sequences in filenames are handled by treating them as 74*aa3d69a6SAndrey A. Chernov * single-byte characters with a values of such bytes of the sequence 75e9346e01STim J. Robbins * cast to wchar_t. 76e9346e01STim J. Robbins * 3. State-dependent encodings are not currently supported. 77e9346e01STim J. Robbins */ 78e9346e01STim J. Robbins 7958f0484fSRodney W. Grimes #include <sys/param.h> 8058f0484fSRodney W. Grimes #include <sys/stat.h> 8158f0484fSRodney W. Grimes 8258f0484fSRodney W. Grimes #include <ctype.h> 8358f0484fSRodney W. Grimes #include <dirent.h> 8458f0484fSRodney W. Grimes #include <errno.h> 8558f0484fSRodney W. Grimes #include <glob.h> 86e9346e01STim J. Robbins #include <limits.h> 8758f0484fSRodney W. Grimes #include <pwd.h> 88e9346e01STim J. Robbins #include <stdint.h> 8958f0484fSRodney W. Grimes #include <stdio.h> 9058f0484fSRodney W. Grimes #include <stdlib.h> 9158f0484fSRodney W. Grimes #include <string.h> 9258f0484fSRodney W. Grimes #include <unistd.h> 93e9346e01STim J. Robbins #include <wchar.h> 9458f0484fSRodney W. Grimes 951daad8f5SAndrey A. Chernov #include "collate.h" 961daad8f5SAndrey A. Chernov 97daab0b01SMarcel Moolenaar /* 98daab0b01SMarcel Moolenaar * glob(3) expansion limits. Stop the expansion if any of these limits 99daab0b01SMarcel Moolenaar * is reached. This caps the runtime in the face of DoS attacks. See 100daab0b01SMarcel Moolenaar * also CVE-2010-2632 101daab0b01SMarcel Moolenaar */ 102daab0b01SMarcel Moolenaar #define GLOB_LIMIT_BRACE 128 /* number of brace calls */ 103daab0b01SMarcel Moolenaar #define GLOB_LIMIT_PATH 65536 /* number of path elements */ 104daab0b01SMarcel Moolenaar #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */ 105daab0b01SMarcel Moolenaar #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */ 106daab0b01SMarcel Moolenaar #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */ 107daab0b01SMarcel Moolenaar 108daab0b01SMarcel Moolenaar struct glob_limit { 109daab0b01SMarcel Moolenaar size_t l_brace_cnt; 110daab0b01SMarcel Moolenaar size_t l_path_lim; 111daab0b01SMarcel Moolenaar size_t l_readdir_cnt; 112daab0b01SMarcel Moolenaar size_t l_stat_cnt; 113daab0b01SMarcel Moolenaar size_t l_string_cnt; 114daab0b01SMarcel Moolenaar }; 115daab0b01SMarcel Moolenaar 116*aa3d69a6SAndrey A. Chernov #define DOT L'.' 117*aa3d69a6SAndrey A. Chernov #define EOS L'\0' 118*aa3d69a6SAndrey A. Chernov #define LBRACKET L'[' 119*aa3d69a6SAndrey A. Chernov #define NOT L'!' 120*aa3d69a6SAndrey A. Chernov #define QUESTION L'?' 121*aa3d69a6SAndrey A. Chernov #define QUOTE L'\\' 122*aa3d69a6SAndrey A. Chernov #define RANGE L'-' 123*aa3d69a6SAndrey A. Chernov #define RBRACKET L']' 124*aa3d69a6SAndrey A. Chernov #define SEP L'/' 125*aa3d69a6SAndrey A. Chernov #define STAR L'*' 126*aa3d69a6SAndrey A. Chernov #define TILDE L'~' 127*aa3d69a6SAndrey A. Chernov #define LBRACE L'{' 128*aa3d69a6SAndrey A. Chernov #define RBRACE L'}' 129*aa3d69a6SAndrey A. Chernov #define SLASH L'/' 130*aa3d69a6SAndrey A. Chernov #define COMMA L',' 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes #ifndef DEBUG 13358f0484fSRodney W. Grimes 134e9346e01STim J. Robbins #define M_QUOTE 0x8000000000ULL 135e9346e01STim J. Robbins #define M_PROTECT 0x4000000000ULL 136e9346e01STim J. Robbins #define M_MASK 0xffffffffffULL 137e9346e01STim J. Robbins #define M_CHAR 0x00ffffffffULL 13858f0484fSRodney W. Grimes 139e9346e01STim J. Robbins typedef uint_fast64_t Char; 14058f0484fSRodney W. Grimes 14158f0484fSRodney W. Grimes #else 14258f0484fSRodney W. Grimes 14358f0484fSRodney W. Grimes #define M_QUOTE 0x80 14458f0484fSRodney W. Grimes #define M_PROTECT 0x40 14558f0484fSRodney W. Grimes #define M_MASK 0xff 146e9346e01STim J. Robbins #define M_CHAR 0x7f 14758f0484fSRodney W. Grimes 14858f0484fSRodney W. Grimes typedef char Char; 14958f0484fSRodney W. Grimes 15058f0484fSRodney W. Grimes #endif 15158f0484fSRodney W. Grimes 15258f0484fSRodney W. Grimes 153e9346e01STim J. Robbins #define CHAR(c) ((Char)((c)&M_CHAR)) 15458f0484fSRodney W. Grimes #define META(c) ((Char)((c)|M_QUOTE)) 155*aa3d69a6SAndrey A. Chernov #define M_ALL META(L'*') 156*aa3d69a6SAndrey A. Chernov #define M_END META(L']') 157*aa3d69a6SAndrey A. Chernov #define M_NOT META(L'!') 158*aa3d69a6SAndrey A. Chernov #define M_ONE META(L'?') 159*aa3d69a6SAndrey A. Chernov #define M_RNG META(L'-') 160*aa3d69a6SAndrey A. Chernov #define M_SET META(L'[') 16158f0484fSRodney W. Grimes #define ismeta(c) (((c)&M_QUOTE) != 0) 16258f0484fSRodney W. Grimes 16358f0484fSRodney W. Grimes 164b231cb39SDavid E. O'Brien static int compare(const void *, const void *); 1654b767fa6SAndrey A. Chernov static int g_Ctoc(const Char *, char *, size_t); 166b231cb39SDavid E. O'Brien static int g_lstat(Char *, struct stat *, glob_t *); 167b231cb39SDavid E. O'Brien static DIR *g_opendir(Char *, glob_t *); 16834a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t); 16958f0484fSRodney W. Grimes #ifdef notdef 170b231cb39SDavid E. O'Brien static Char *g_strcat(Char *, const Char *); 17158f0484fSRodney W. Grimes #endif 172b231cb39SDavid E. O'Brien static int g_stat(Char *, struct stat *, glob_t *); 173daab0b01SMarcel Moolenaar static int glob0(const Char *, glob_t *, struct glob_limit *); 174daab0b01SMarcel Moolenaar static int glob1(Char *, glob_t *, struct glob_limit *); 175daab0b01SMarcel Moolenaar static int glob2(Char *, Char *, Char *, Char *, glob_t *, 176daab0b01SMarcel Moolenaar struct glob_limit *); 177daab0b01SMarcel Moolenaar static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, 178daab0b01SMarcel Moolenaar struct glob_limit *); 179daab0b01SMarcel Moolenaar static int globextend(const Char *, glob_t *, struct glob_limit *); 180487dbd92SPeter Wemm static const Char * 181b231cb39SDavid E. O'Brien globtilde(const Char *, Char *, size_t, glob_t *); 182daab0b01SMarcel Moolenaar static int globexp1(const Char *, glob_t *, struct glob_limit *); 183daab0b01SMarcel Moolenaar static int globexp2(const Char *, const Char *, glob_t *, int *, 184daab0b01SMarcel Moolenaar struct glob_limit *); 185b231cb39SDavid E. O'Brien static int match(Char *, Char *, Char *); 18658f0484fSRodney W. Grimes #ifdef DEBUG 187b231cb39SDavid E. O'Brien static void qprintf(const char *, Char *); 18858f0484fSRodney W. Grimes #endif 18958f0484fSRodney W. Grimes 19058f0484fSRodney W. Grimes int 1910d6d372cSEitan Adler glob(const char * __restrict pattern, int flags, 1920d6d372cSEitan Adler int (*errfunc)(const char *, int), glob_t * __restrict pglob) 19358f0484fSRodney W. Grimes { 194daab0b01SMarcel Moolenaar struct glob_limit limit = { 0, 0, 0, 0, 0 }; 1951cec70adSXin LI const char *patnext; 196e9346e01STim J. Robbins Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot; 197e9346e01STim J. Robbins mbstate_t mbs; 198e9346e01STim J. Robbins wchar_t wc; 199e9346e01STim J. Robbins size_t clen; 20058f0484fSRodney W. Grimes 2011cec70adSXin LI patnext = pattern; 20258f0484fSRodney W. Grimes if (!(flags & GLOB_APPEND)) { 20358f0484fSRodney W. Grimes pglob->gl_pathc = 0; 20458f0484fSRodney W. Grimes pglob->gl_pathv = NULL; 20558f0484fSRodney W. Grimes if (!(flags & GLOB_DOOFFS)) 20658f0484fSRodney W. Grimes pglob->gl_offs = 0; 20758f0484fSRodney W. Grimes } 20875dc5f1aSMike Heffner if (flags & GLOB_LIMIT) { 209daab0b01SMarcel Moolenaar limit.l_path_lim = pglob->gl_matchc; 210daab0b01SMarcel Moolenaar if (limit.l_path_lim == 0) 211daab0b01SMarcel Moolenaar limit.l_path_lim = GLOB_LIMIT_PATH; 212daab0b01SMarcel Moolenaar } 21358f0484fSRodney W. Grimes pglob->gl_flags = flags & ~GLOB_MAGCHAR; 21458f0484fSRodney W. Grimes pglob->gl_errfunc = errfunc; 21558f0484fSRodney W. Grimes pglob->gl_matchc = 0; 21658f0484fSRodney W. Grimes 21758f0484fSRodney W. Grimes bufnext = patbuf; 218487dbd92SPeter Wemm bufend = bufnext + MAXPATHLEN - 1; 219e9346e01STim J. Robbins if (flags & GLOB_NOESCAPE) { 220e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 221e9346e01STim J. Robbins while (bufend - bufnext >= MB_CUR_MAX) { 222e9346e01STim J. Robbins clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs); 223e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) 224e9346e01STim J. Robbins return (GLOB_NOMATCH); 225e9346e01STim J. Robbins else if (clen == 0) 226e9346e01STim J. Robbins break; 227e9346e01STim J. Robbins *bufnext++ = wc; 228e9346e01STim J. Robbins patnext += clen; 229e9346e01STim J. Robbins } 230e9346e01STim J. Robbins } else { 23158f0484fSRodney W. Grimes /* Protect the quoted characters. */ 232e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 233e9346e01STim J. Robbins while (bufend - bufnext >= MB_CUR_MAX) { 234*aa3d69a6SAndrey A. Chernov if (*patnext == '\\') { 235*aa3d69a6SAndrey A. Chernov if (*++patnext == '\0') { 236e9346e01STim J. Robbins *bufnext++ = QUOTE | M_PROTECT; 237e9346e01STim J. Robbins continue; 23858f0484fSRodney W. Grimes } 239e9346e01STim J. Robbins prot = M_PROTECT; 240e9346e01STim J. Robbins } else 241e9346e01STim J. Robbins prot = 0; 242e9346e01STim J. Robbins clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs); 243e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) 244e9346e01STim J. Robbins return (GLOB_NOMATCH); 245e9346e01STim J. Robbins else if (clen == 0) 246e9346e01STim J. Robbins break; 247e9346e01STim J. Robbins *bufnext++ = wc | prot; 248e9346e01STim J. Robbins patnext += clen; 24958f0484fSRodney W. Grimes } 25058f0484fSRodney W. Grimes } 25158f0484fSRodney W. Grimes *bufnext = EOS; 25258f0484fSRodney W. Grimes 25358f0484fSRodney W. Grimes if (flags & GLOB_BRACE) 25485529174SEitan Adler return (globexp1(patbuf, pglob, &limit)); 25558f0484fSRodney W. Grimes else 25685529174SEitan Adler return (glob0(patbuf, pglob, &limit)); 25758f0484fSRodney W. Grimes } 25858f0484fSRodney W. Grimes 25958f0484fSRodney W. Grimes /* 26058f0484fSRodney W. Grimes * Expand recursively a glob {} pattern. When there is no more expansion 26158f0484fSRodney W. Grimes * invoke the standard globbing routine to glob the rest of the magic 26258f0484fSRodney W. Grimes * characters 26358f0484fSRodney W. Grimes */ 264487dbd92SPeter Wemm static int 265daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) 26658f0484fSRodney W. Grimes { 26758f0484fSRodney W. Grimes const Char* ptr = pattern; 26858f0484fSRodney W. Grimes int rv; 26958f0484fSRodney W. Grimes 270daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 271daab0b01SMarcel Moolenaar limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { 272daab0b01SMarcel Moolenaar errno = 0; 273daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 274daab0b01SMarcel Moolenaar } 275daab0b01SMarcel Moolenaar 27658f0484fSRodney W. Grimes /* Protect a single {}, for find(1), like csh */ 27758f0484fSRodney W. Grimes if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) 278bae8632fSJonathan Lemon return glob0(pattern, pglob, limit); 27958f0484fSRodney W. Grimes 28034a08754SMike Makonnen while ((ptr = g_strchr(ptr, LBRACE)) != NULL) 281bae8632fSJonathan Lemon if (!globexp2(ptr, pattern, pglob, &rv, limit)) 28258f0484fSRodney W. Grimes return rv; 28358f0484fSRodney W. Grimes 284bae8632fSJonathan Lemon return glob0(pattern, pglob, limit); 28558f0484fSRodney W. Grimes } 28658f0484fSRodney W. Grimes 28758f0484fSRodney W. Grimes 28858f0484fSRodney W. Grimes /* 28958f0484fSRodney W. Grimes * Recursive brace globbing helper. Tries to expand a single brace. 29058f0484fSRodney W. Grimes * If it succeeds then it invokes globexp1 with the new pattern. 29158f0484fSRodney W. Grimes * If it fails then it tries to glob the rest of the pattern and returns. 29258f0484fSRodney W. Grimes */ 293487dbd92SPeter Wemm static int 294daab0b01SMarcel Moolenaar globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, 295daab0b01SMarcel Moolenaar struct glob_limit *limit) 29658f0484fSRodney W. Grimes { 29758f0484fSRodney W. Grimes int i; 29858f0484fSRodney W. Grimes Char *lm, *ls; 299369316a8SAndrey A. Chernov const Char *pe, *pm, *pm1, *pl; 300487dbd92SPeter Wemm Char patbuf[MAXPATHLEN]; 30158f0484fSRodney W. Grimes 30258f0484fSRodney W. Grimes /* copy part up to the brace */ 30358f0484fSRodney W. Grimes for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 30458f0484fSRodney W. Grimes continue; 305487dbd92SPeter Wemm *lm = EOS; 30658f0484fSRodney W. Grimes ls = lm; 30758f0484fSRodney W. Grimes 30858f0484fSRodney W. Grimes /* Find the balanced brace */ 30958f0484fSRodney W. Grimes for (i = 0, pe = ++ptr; *pe; pe++) 31058f0484fSRodney W. Grimes if (*pe == LBRACKET) { 31158f0484fSRodney W. Grimes /* Ignore everything between [] */ 31258f0484fSRodney W. Grimes for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) 31358f0484fSRodney W. Grimes continue; 31458f0484fSRodney W. Grimes if (*pe == EOS) { 31558f0484fSRodney W. Grimes /* 31658f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 31758f0484fSRodney W. Grimes * Ignore and just look for RBRACE 31858f0484fSRodney W. Grimes */ 31958f0484fSRodney W. Grimes pe = pm; 32058f0484fSRodney W. Grimes } 32158f0484fSRodney W. Grimes } 32258f0484fSRodney W. Grimes else if (*pe == LBRACE) 32358f0484fSRodney W. Grimes i++; 32458f0484fSRodney W. Grimes else if (*pe == RBRACE) { 32558f0484fSRodney W. Grimes if (i == 0) 32658f0484fSRodney W. Grimes break; 32758f0484fSRodney W. Grimes i--; 32858f0484fSRodney W. Grimes } 32958f0484fSRodney W. Grimes 33058f0484fSRodney W. Grimes /* Non matching braces; just glob the pattern */ 33158f0484fSRodney W. Grimes if (i != 0 || *pe == EOS) { 332bae8632fSJonathan Lemon *rv = glob0(patbuf, pglob, limit); 33385529174SEitan Adler return (0); 33458f0484fSRodney W. Grimes } 33558f0484fSRodney W. Grimes 33658f0484fSRodney W. Grimes for (i = 0, pl = pm = ptr; pm <= pe; pm++) 33758f0484fSRodney W. Grimes switch (*pm) { 33858f0484fSRodney W. Grimes case LBRACKET: 33958f0484fSRodney W. Grimes /* Ignore everything between [] */ 340369316a8SAndrey A. Chernov for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++) 34158f0484fSRodney W. Grimes continue; 34258f0484fSRodney W. Grimes if (*pm == EOS) { 34358f0484fSRodney W. Grimes /* 34458f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 34558f0484fSRodney W. Grimes * Ignore and just look for RBRACE 34658f0484fSRodney W. Grimes */ 347369316a8SAndrey A. Chernov pm = pm1; 34858f0484fSRodney W. Grimes } 34958f0484fSRodney W. Grimes break; 35058f0484fSRodney W. Grimes 35158f0484fSRodney W. Grimes case LBRACE: 35258f0484fSRodney W. Grimes i++; 35358f0484fSRodney W. Grimes break; 35458f0484fSRodney W. Grimes 35558f0484fSRodney W. Grimes case RBRACE: 35658f0484fSRodney W. Grimes if (i) { 35758f0484fSRodney W. Grimes i--; 35858f0484fSRodney W. Grimes break; 35958f0484fSRodney W. Grimes } 36058f0484fSRodney W. Grimes /* FALLTHROUGH */ 36158f0484fSRodney W. Grimes case COMMA: 36258f0484fSRodney W. Grimes if (i && *pm == COMMA) 36358f0484fSRodney W. Grimes break; 36458f0484fSRodney W. Grimes else { 36558f0484fSRodney W. Grimes /* Append the current string */ 36658f0484fSRodney W. Grimes for (lm = ls; (pl < pm); *lm++ = *pl++) 36758f0484fSRodney W. Grimes continue; 36858f0484fSRodney W. Grimes /* 36958f0484fSRodney W. Grimes * Append the rest of the pattern after the 37058f0484fSRodney W. Grimes * closing brace 37158f0484fSRodney W. Grimes */ 37258f0484fSRodney W. Grimes for (pl = pe + 1; (*lm++ = *pl++) != EOS;) 37358f0484fSRodney W. Grimes continue; 37458f0484fSRodney W. Grimes 37558f0484fSRodney W. Grimes /* Expand the current pattern */ 37658f0484fSRodney W. Grimes #ifdef DEBUG 37758f0484fSRodney W. Grimes qprintf("globexp2:", patbuf); 37858f0484fSRodney W. Grimes #endif 379bae8632fSJonathan Lemon *rv = globexp1(patbuf, pglob, limit); 38058f0484fSRodney W. Grimes 38158f0484fSRodney W. Grimes /* move after the comma, to the next string */ 38258f0484fSRodney W. Grimes pl = pm + 1; 38358f0484fSRodney W. Grimes } 38458f0484fSRodney W. Grimes break; 38558f0484fSRodney W. Grimes 38658f0484fSRodney W. Grimes default: 38758f0484fSRodney W. Grimes break; 38858f0484fSRodney W. Grimes } 38958f0484fSRodney W. Grimes *rv = 0; 39085529174SEitan Adler return (0); 39158f0484fSRodney W. Grimes } 39258f0484fSRodney W. Grimes 39358f0484fSRodney W. Grimes 39458f0484fSRodney W. Grimes 39558f0484fSRodney W. Grimes /* 39658f0484fSRodney W. Grimes * expand tilde from the passwd file. 39758f0484fSRodney W. Grimes */ 39858f0484fSRodney W. Grimes static const Char * 3991cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) 40058f0484fSRodney W. Grimes { 40158f0484fSRodney W. Grimes struct passwd *pwd; 402*aa3d69a6SAndrey A. Chernov char *h, *sc; 40358f0484fSRodney W. Grimes const Char *p; 40462f187a4SWarner Losh Char *b, *eb; 405*aa3d69a6SAndrey A. Chernov wchar_t wc; 406*aa3d69a6SAndrey A. Chernov wchar_t wbuf[MAXPATHLEN]; 407*aa3d69a6SAndrey A. Chernov wchar_t *wbufend, *dc; 408*aa3d69a6SAndrey A. Chernov size_t clen; 409*aa3d69a6SAndrey A. Chernov mbstate_t mbs; 410*aa3d69a6SAndrey A. Chernov int too_long; 41158f0484fSRodney W. Grimes 41258f0484fSRodney W. Grimes if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) 41385529174SEitan Adler return (pattern); 41458f0484fSRodney W. Grimes 41562f187a4SWarner Losh /* 41662f187a4SWarner Losh * Copy up to the end of the string or / 41762f187a4SWarner Losh */ 41862f187a4SWarner Losh eb = &patbuf[patbuf_len - 1]; 419*aa3d69a6SAndrey A. Chernov for (p = pattern + 1, b = patbuf; 420*aa3d69a6SAndrey A. Chernov b < eb && *p != EOS && *p != SLASH; *b++ = *p++) 42158f0484fSRodney W. Grimes continue; 42258f0484fSRodney W. Grimes 423*aa3d69a6SAndrey A. Chernov if (*p != EOS && *p != SLASH) 424*aa3d69a6SAndrey A. Chernov return (pattern); 42558f0484fSRodney W. Grimes 426*aa3d69a6SAndrey A. Chernov *b = EOS; 427*aa3d69a6SAndrey A. Chernov h = NULL; 428*aa3d69a6SAndrey A. Chernov 429*aa3d69a6SAndrey A. Chernov if (patbuf[0] == EOS) { 43058f0484fSRodney W. Grimes /* 4313fa69daeSWarner Losh * handle a plain ~ or ~/ by expanding $HOME first (iff 4323fa69daeSWarner Losh * we're not running setuid or setgid) and then trying 4333fa69daeSWarner Losh * the password file 43458f0484fSRodney W. Grimes */ 4354539e95aSTim J. Robbins if (issetugid() != 0 || 4369fcbcd02SJohn Birrell (h = getenv("HOME")) == NULL) { 437eb8eee5aSAndrey A. Chernov if (((h = getlogin()) != NULL && 438eb8eee5aSAndrey A. Chernov (pwd = getpwnam(h)) != NULL) || 439eb8eee5aSAndrey A. Chernov (pwd = getpwuid(getuid())) != NULL) 44058f0484fSRodney W. Grimes h = pwd->pw_dir; 441eb8eee5aSAndrey A. Chernov else 44285529174SEitan Adler return (pattern); 44358f0484fSRodney W. Grimes } 44458f0484fSRodney W. Grimes } 44558f0484fSRodney W. Grimes else { 44658f0484fSRodney W. Grimes /* 44758f0484fSRodney W. Grimes * Expand a ~user 44858f0484fSRodney W. Grimes */ 449*aa3d69a6SAndrey A. Chernov if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)) || 450*aa3d69a6SAndrey A. Chernov (pwd = getpwnam((char *)wbuf)) == NULL) 45185529174SEitan Adler return (pattern); 45258f0484fSRodney W. Grimes else 45358f0484fSRodney W. Grimes h = pwd->pw_dir; 45458f0484fSRodney W. Grimes } 45558f0484fSRodney W. Grimes 45658f0484fSRodney W. Grimes /* Copy the home directory */ 457*aa3d69a6SAndrey A. Chernov dc = wbuf; 458*aa3d69a6SAndrey A. Chernov sc = h; 459*aa3d69a6SAndrey A. Chernov wbufend = wbuf + MAXPATHLEN - 1; 460*aa3d69a6SAndrey A. Chernov too_long = 1; 461*aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 462*aa3d69a6SAndrey A. Chernov while (dc <= wbufend) { 463*aa3d69a6SAndrey A. Chernov clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs); 464*aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1 || clen == (size_t)-2) { 465*aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 466*aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc; 467*aa3d69a6SAndrey A. Chernov clen = 1; 468*aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 469*aa3d69a6SAndrey A. Chernov } 470*aa3d69a6SAndrey A. Chernov if ((*dc++ = wc) == EOS) { 471*aa3d69a6SAndrey A. Chernov too_long = 0; 472*aa3d69a6SAndrey A. Chernov break; 473*aa3d69a6SAndrey A. Chernov } 474*aa3d69a6SAndrey A. Chernov sc += clen; 475*aa3d69a6SAndrey A. Chernov } 476*aa3d69a6SAndrey A. Chernov if (too_long) 477*aa3d69a6SAndrey A. Chernov return (pattern); 478*aa3d69a6SAndrey A. Chernov 479*aa3d69a6SAndrey A. Chernov dc = wbuf; 480*aa3d69a6SAndrey A. Chernov for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++) 48158f0484fSRodney W. Grimes continue; 482*aa3d69a6SAndrey A. Chernov if (*dc != EOS) 483*aa3d69a6SAndrey A. Chernov return (pattern); 48458f0484fSRodney W. Grimes 48558f0484fSRodney W. Grimes /* Append the rest of the pattern */ 486*aa3d69a6SAndrey A. Chernov if (*p != EOS) { 487*aa3d69a6SAndrey A. Chernov too_long = 1; 488*aa3d69a6SAndrey A. Chernov while (b <= eb) { 489*aa3d69a6SAndrey A. Chernov if ((*b++ = *p++) == EOS) { 490*aa3d69a6SAndrey A. Chernov too_long = 0; 491*aa3d69a6SAndrey A. Chernov break; 492*aa3d69a6SAndrey A. Chernov } 493*aa3d69a6SAndrey A. Chernov } 494*aa3d69a6SAndrey A. Chernov if (too_long) 495*aa3d69a6SAndrey A. Chernov return (pattern); 496*aa3d69a6SAndrey A. Chernov } else 49762f187a4SWarner Losh *b = EOS; 49858f0484fSRodney W. Grimes 49985529174SEitan Adler return (patbuf); 50058f0484fSRodney W. Grimes } 50158f0484fSRodney W. Grimes 50258f0484fSRodney W. Grimes 50358f0484fSRodney W. Grimes /* 50458f0484fSRodney W. Grimes * The main glob() routine: compiles the pattern (optionally processing 50558f0484fSRodney W. Grimes * quotes), calls glob1() to do the real pattern matching, and finally 50658f0484fSRodney W. Grimes * sorts the list (unless unsorted operation is requested). Returns 0 5074a59c3abSMike Heffner * if things went well, nonzero if errors occurred. 50858f0484fSRodney W. Grimes */ 50958f0484fSRodney W. Grimes static int 510daab0b01SMarcel Moolenaar glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit) 51158f0484fSRodney W. Grimes { 51258f0484fSRodney W. Grimes const Char *qpatnext; 5135b3fab81SGordon Tetlow int err; 5144b767fa6SAndrey A. Chernov size_t oldpathc; 5155b3fab81SGordon Tetlow Char *bufnext, c, patbuf[MAXPATHLEN]; 51658f0484fSRodney W. Grimes 517487dbd92SPeter Wemm qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); 51858f0484fSRodney W. Grimes oldpathc = pglob->gl_pathc; 51958f0484fSRodney W. Grimes bufnext = patbuf; 52058f0484fSRodney W. Grimes 52158f0484fSRodney W. Grimes /* We don't need to check for buffer overflow any more. */ 52258f0484fSRodney W. Grimes while ((c = *qpatnext++) != EOS) { 52358f0484fSRodney W. Grimes switch (c) { 52458f0484fSRodney W. Grimes case LBRACKET: 52558f0484fSRodney W. Grimes c = *qpatnext; 52658f0484fSRodney W. Grimes if (c == NOT) 52758f0484fSRodney W. Grimes ++qpatnext; 52858f0484fSRodney W. Grimes if (*qpatnext == EOS || 52934a08754SMike Makonnen g_strchr(qpatnext+1, RBRACKET) == NULL) { 53058f0484fSRodney W. Grimes *bufnext++ = LBRACKET; 53158f0484fSRodney W. Grimes if (c == NOT) 53258f0484fSRodney W. Grimes --qpatnext; 53358f0484fSRodney W. Grimes break; 53458f0484fSRodney W. Grimes } 53558f0484fSRodney W. Grimes *bufnext++ = M_SET; 53658f0484fSRodney W. Grimes if (c == NOT) 53758f0484fSRodney W. Grimes *bufnext++ = M_NOT; 53858f0484fSRodney W. Grimes c = *qpatnext++; 53958f0484fSRodney W. Grimes do { 54058f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 54158f0484fSRodney W. Grimes if (*qpatnext == RANGE && 54258f0484fSRodney W. Grimes (c = qpatnext[1]) != RBRACKET) { 54358f0484fSRodney W. Grimes *bufnext++ = M_RNG; 54458f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 54558f0484fSRodney W. Grimes qpatnext += 2; 54658f0484fSRodney W. Grimes } 54758f0484fSRodney W. Grimes } while ((c = *qpatnext++) != RBRACKET); 54858f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 54958f0484fSRodney W. Grimes *bufnext++ = M_END; 55058f0484fSRodney W. Grimes break; 55158f0484fSRodney W. Grimes case QUESTION: 55258f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 55358f0484fSRodney W. Grimes *bufnext++ = M_ONE; 55458f0484fSRodney W. Grimes break; 55558f0484fSRodney W. Grimes case STAR: 55658f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 55758f0484fSRodney W. Grimes /* collapse adjacent stars to one, 55858f0484fSRodney W. Grimes * to avoid exponential behavior 55958f0484fSRodney W. Grimes */ 56058f0484fSRodney W. Grimes if (bufnext == patbuf || bufnext[-1] != M_ALL) 56158f0484fSRodney W. Grimes *bufnext++ = M_ALL; 56258f0484fSRodney W. Grimes break; 56358f0484fSRodney W. Grimes default: 56458f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 56558f0484fSRodney W. Grimes break; 56658f0484fSRodney W. Grimes } 56758f0484fSRodney W. Grimes } 56858f0484fSRodney W. Grimes *bufnext = EOS; 56958f0484fSRodney W. Grimes #ifdef DEBUG 57058f0484fSRodney W. Grimes qprintf("glob0:", patbuf); 57158f0484fSRodney W. Grimes #endif 57258f0484fSRodney W. Grimes 573bae8632fSJonathan Lemon if ((err = glob1(patbuf, pglob, limit)) != 0) 57458f0484fSRodney W. Grimes return(err); 57558f0484fSRodney W. Grimes 57658f0484fSRodney W. Grimes /* 57758f0484fSRodney W. Grimes * If there was no match we are going to append the pattern 57858f0484fSRodney W. Grimes * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 57958f0484fSRodney W. Grimes * and the pattern did not contain any magic characters 58058f0484fSRodney W. Grimes * GLOB_NOMAGIC is there just for compatibility with csh. 58158f0484fSRodney W. Grimes */ 5824a59c3abSMike Heffner if (pglob->gl_pathc == oldpathc) { 5834a59c3abSMike Heffner if (((pglob->gl_flags & GLOB_NOCHECK) || 58458f0484fSRodney W. Grimes ((pglob->gl_flags & GLOB_NOMAGIC) && 58558f0484fSRodney W. Grimes !(pglob->gl_flags & GLOB_MAGCHAR)))) 586bae8632fSJonathan Lemon return (globextend(pattern, pglob, limit)); 5874a59c3abSMike Heffner else 5884a59c3abSMike Heffner return (GLOB_NOMATCH); 5894a59c3abSMike Heffner } 5904a59c3abSMike Heffner if (!(pglob->gl_flags & GLOB_NOSORT)) 59158f0484fSRodney W. Grimes qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 59258f0484fSRodney W. Grimes pglob->gl_pathc - oldpathc, sizeof(char *), compare); 59358f0484fSRodney W. Grimes return (0); 59458f0484fSRodney W. Grimes } 59558f0484fSRodney W. Grimes 59658f0484fSRodney W. Grimes static int 5971cec70adSXin LI compare(const void *p, const void *q) 59858f0484fSRodney W. Grimes { 599*aa3d69a6SAndrey A. Chernov return (strcoll(*(char **)p, *(char **)q)); 60058f0484fSRodney W. Grimes } 60158f0484fSRodney W. Grimes 60258f0484fSRodney W. Grimes static int 603daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) 60458f0484fSRodney W. Grimes { 605487dbd92SPeter Wemm Char pathbuf[MAXPATHLEN]; 60658f0484fSRodney W. Grimes 60758f0484fSRodney W. Grimes /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 60858f0484fSRodney W. Grimes if (*pattern == EOS) 60958f0484fSRodney W. Grimes return (0); 610487dbd92SPeter Wemm return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, 611487dbd92SPeter Wemm pattern, pglob, limit)); 61258f0484fSRodney W. Grimes } 61358f0484fSRodney W. Grimes 61458f0484fSRodney W. Grimes /* 61558f0484fSRodney W. Grimes * The functions glob2 and glob3 are mutually recursive; there is one level 61658f0484fSRodney W. Grimes * of recursion for each segment in the pattern that contains one or more 61758f0484fSRodney W. Grimes * meta characters. 61858f0484fSRodney W. Grimes */ 61958f0484fSRodney W. Grimes static int 6201cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, 621daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit) 62258f0484fSRodney W. Grimes { 62358f0484fSRodney W. Grimes struct stat sb; 62458f0484fSRodney W. Grimes Char *p, *q; 62558f0484fSRodney W. Grimes int anymeta; 62658f0484fSRodney W. Grimes 62758f0484fSRodney W. Grimes /* 62858f0484fSRodney W. Grimes * Loop over pattern segments until end of pattern or until 62958f0484fSRodney W. Grimes * segment with meta character found. 63058f0484fSRodney W. Grimes */ 63158f0484fSRodney W. Grimes for (anymeta = 0;;) { 63258f0484fSRodney W. Grimes if (*pattern == EOS) { /* End of pattern? */ 63358f0484fSRodney W. Grimes *pathend = EOS; 63458f0484fSRodney W. Grimes if (g_lstat(pathbuf, &sb, pglob)) 63558f0484fSRodney W. Grimes return (0); 63658f0484fSRodney W. Grimes 637daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 638daab0b01SMarcel Moolenaar limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) { 639daab0b01SMarcel Moolenaar errno = 0; 640daab0b01SMarcel Moolenaar if (pathend + 1 > pathend_last) 641daab0b01SMarcel Moolenaar return (GLOB_ABORTED); 642daab0b01SMarcel Moolenaar *pathend++ = SEP; 643daab0b01SMarcel Moolenaar *pathend = EOS; 644daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 645daab0b01SMarcel Moolenaar } 64658f0484fSRodney W. Grimes if (((pglob->gl_flags & GLOB_MARK) && 64758f0484fSRodney W. Grimes pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 64858f0484fSRodney W. Grimes || (S_ISLNK(sb.st_mode) && 64958f0484fSRodney W. Grimes (g_stat(pathbuf, &sb, pglob) == 0) && 65058f0484fSRodney W. Grimes S_ISDIR(sb.st_mode)))) { 651487dbd92SPeter Wemm if (pathend + 1 > pathend_last) 6524a59c3abSMike Heffner return (GLOB_ABORTED); 65358f0484fSRodney W. Grimes *pathend++ = SEP; 65458f0484fSRodney W. Grimes *pathend = EOS; 65558f0484fSRodney W. Grimes } 65658f0484fSRodney W. Grimes ++pglob->gl_matchc; 657bae8632fSJonathan Lemon return (globextend(pathbuf, pglob, limit)); 65858f0484fSRodney W. Grimes } 65958f0484fSRodney W. Grimes 66058f0484fSRodney W. Grimes /* Find end of next segment, copy tentatively to pathend. */ 66158f0484fSRodney W. Grimes q = pathend; 66258f0484fSRodney W. Grimes p = pattern; 66358f0484fSRodney W. Grimes while (*p != EOS && *p != SEP) { 66458f0484fSRodney W. Grimes if (ismeta(*p)) 66558f0484fSRodney W. Grimes anymeta = 1; 666487dbd92SPeter Wemm if (q + 1 > pathend_last) 6674a59c3abSMike Heffner return (GLOB_ABORTED); 66858f0484fSRodney W. Grimes *q++ = *p++; 66958f0484fSRodney W. Grimes } 67058f0484fSRodney W. Grimes 67158f0484fSRodney W. Grimes if (!anymeta) { /* No expansion, do next segment. */ 67258f0484fSRodney W. Grimes pathend = q; 67358f0484fSRodney W. Grimes pattern = p; 674487dbd92SPeter Wemm while (*pattern == SEP) { 675487dbd92SPeter Wemm if (pathend + 1 > pathend_last) 6764a59c3abSMike Heffner return (GLOB_ABORTED); 67758f0484fSRodney W. Grimes *pathend++ = *pattern++; 678487dbd92SPeter Wemm } 67958f0484fSRodney W. Grimes } else /* Need expansion, recurse. */ 68085529174SEitan Adler return (glob3(pathbuf, pathend, pathend_last, pattern, 68185529174SEitan Adler p, pglob, limit)); 68258f0484fSRodney W. Grimes } 68358f0484fSRodney W. Grimes /* NOTREACHED */ 68458f0484fSRodney W. Grimes } 68558f0484fSRodney W. Grimes 68658f0484fSRodney W. Grimes static int 6871cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last, 6881cec70adSXin LI Char *pattern, Char *restpattern, 689daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit) 69058f0484fSRodney W. Grimes { 691b231cb39SDavid E. O'Brien struct dirent *dp; 69258f0484fSRodney W. Grimes DIR *dirp; 69358f0484fSRodney W. Grimes int err; 69458f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 69558f0484fSRodney W. Grimes 6963e2981e8SCraig Rodrigues struct dirent *(*readdirfunc)(DIR *); 69758f0484fSRodney W. Grimes 698487dbd92SPeter Wemm if (pathend > pathend_last) 6994a59c3abSMike Heffner return (GLOB_ABORTED); 70058f0484fSRodney W. Grimes *pathend = EOS; 70158f0484fSRodney W. Grimes errno = 0; 70258f0484fSRodney W. Grimes 70358f0484fSRodney W. Grimes if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 70458f0484fSRodney W. Grimes /* TODO: don't call for ENOENT or ENOTDIR? */ 70558f0484fSRodney W. Grimes if (pglob->gl_errfunc) { 70627d52f69SPeter Wemm if (g_Ctoc(pathbuf, buf, sizeof(buf))) 7074a59c3abSMike Heffner return (GLOB_ABORTED); 70858f0484fSRodney W. Grimes if (pglob->gl_errfunc(buf, errno) || 70958f0484fSRodney W. Grimes pglob->gl_flags & GLOB_ERR) 7104a59c3abSMike Heffner return (GLOB_ABORTED); 71158f0484fSRodney W. Grimes } 71258f0484fSRodney W. Grimes return (0); 71358f0484fSRodney W. Grimes } 71458f0484fSRodney W. Grimes 71558f0484fSRodney W. Grimes err = 0; 71658f0484fSRodney W. Grimes 7173e2981e8SCraig Rodrigues /* pglob->gl_readdir takes a void *, fix this manually */ 71858f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 7193e2981e8SCraig Rodrigues readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir; 72058f0484fSRodney W. Grimes else 72158f0484fSRodney W. Grimes readdirfunc = readdir; 7223e2981e8SCraig Rodrigues 7233e2981e8SCraig Rodrigues /* Search directory for matching names. */ 7243e2981e8SCraig Rodrigues while ((dp = (*readdirfunc)(dirp)) != NULL) { 7251cec70adSXin LI char *sc; 726b231cb39SDavid E. O'Brien Char *dc; 727e9346e01STim J. Robbins wchar_t wc; 728e9346e01STim J. Robbins size_t clen; 729e9346e01STim J. Robbins mbstate_t mbs; 73058f0484fSRodney W. Grimes 731daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 732daab0b01SMarcel Moolenaar limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) { 733daab0b01SMarcel Moolenaar errno = 0; 734daab0b01SMarcel Moolenaar if (pathend + 1 > pathend_last) 735daab0b01SMarcel Moolenaar err = GLOB_ABORTED; 736daab0b01SMarcel Moolenaar else { 737daab0b01SMarcel Moolenaar *pathend++ = SEP; 738daab0b01SMarcel Moolenaar *pathend = EOS; 739daab0b01SMarcel Moolenaar err = GLOB_NOSPACE; 740daab0b01SMarcel Moolenaar } 741daab0b01SMarcel Moolenaar break; 742daab0b01SMarcel Moolenaar } 743daab0b01SMarcel Moolenaar 74458f0484fSRodney W. Grimes /* Initial DOT must be matched literally. */ 745*aa3d69a6SAndrey A. Chernov if (dp->d_name[0] == '.' && *pattern != DOT) 74658f0484fSRodney W. Grimes continue; 747e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 748487dbd92SPeter Wemm dc = pathend; 7491cec70adSXin LI sc = dp->d_name; 750e9346e01STim J. Robbins while (dc < pathend_last) { 751e9346e01STim J. Robbins clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs); 752e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) { 753*aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 754*aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc; 755e9346e01STim J. Robbins clen = 1; 756e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 757e9346e01STim J. Robbins } 758e9346e01STim J. Robbins if ((*dc++ = wc) == EOS) 759e9346e01STim J. Robbins break; 760e9346e01STim J. Robbins sc += clen; 761e9346e01STim J. Robbins } 76258f0484fSRodney W. Grimes if (!match(pathend, pattern, restpattern)) { 76358f0484fSRodney W. Grimes *pathend = EOS; 76458f0484fSRodney W. Grimes continue; 76558f0484fSRodney W. Grimes } 766487dbd92SPeter Wemm err = glob2(pathbuf, --dc, pathend_last, restpattern, 767487dbd92SPeter Wemm pglob, limit); 76858f0484fSRodney W. Grimes if (err) 76958f0484fSRodney W. Grimes break; 77058f0484fSRodney W. Grimes } 77158f0484fSRodney W. Grimes 77258f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 77358f0484fSRodney W. Grimes (*pglob->gl_closedir)(dirp); 77458f0484fSRodney W. Grimes else 77558f0484fSRodney W. Grimes closedir(dirp); 77658f0484fSRodney W. Grimes return (err); 77758f0484fSRodney W. Grimes } 77858f0484fSRodney W. Grimes 77958f0484fSRodney W. Grimes 78058f0484fSRodney W. Grimes /* 781b4c19408SEd Maste * Extend the gl_pathv member of a glob_t structure to accommodate a new item, 78258f0484fSRodney W. Grimes * add the new item, and update gl_pathc. 78358f0484fSRodney W. Grimes * 78458f0484fSRodney W. Grimes * This assumes the BSD realloc, which only copies the block when its size 78558f0484fSRodney W. Grimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 78658f0484fSRodney W. Grimes * behavior. 78758f0484fSRodney W. Grimes * 78858f0484fSRodney W. Grimes * Return 0 if new item added, error code if memory couldn't be allocated. 78958f0484fSRodney W. Grimes * 79058f0484fSRodney W. Grimes * Invariant of the glob_t structure: 79158f0484fSRodney W. Grimes * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 79258f0484fSRodney W. Grimes * gl_pathv points to (gl_offs + gl_pathc + 1) items. 79358f0484fSRodney W. Grimes */ 79458f0484fSRodney W. Grimes static int 795daab0b01SMarcel Moolenaar globextend(const Char *path, glob_t *pglob, struct glob_limit *limit) 79658f0484fSRodney W. Grimes { 797b231cb39SDavid E. O'Brien char **pathv; 7984b767fa6SAndrey A. Chernov size_t i, newsize, len; 79958f0484fSRodney W. Grimes char *copy; 80058f0484fSRodney W. Grimes const Char *p; 80158f0484fSRodney W. Grimes 802daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 803daab0b01SMarcel Moolenaar pglob->gl_matchc > limit->l_path_lim) { 80475dc5f1aSMike Heffner errno = 0; 80575dc5f1aSMike Heffner return (GLOB_NOSPACE); 80675dc5f1aSMike Heffner } 807813c96dbSJonathan Lemon 80858f0484fSRodney W. Grimes newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 80943cc14e0SMarcel Moolenaar /* realloc(NULL, newsize) is equivalent to malloc(newsize). */ 81043cc14e0SMarcel Moolenaar pathv = realloc((void *)pglob->gl_pathv, newsize); 811b628fac5SMarcel Moolenaar if (pathv == NULL) 81258f0484fSRodney W. Grimes return (GLOB_NOSPACE); 81358f0484fSRodney W. Grimes 81458f0484fSRodney W. Grimes if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 81558f0484fSRodney W. Grimes /* first time around -- clear initial gl_offs items */ 81658f0484fSRodney W. Grimes pathv += pglob->gl_offs; 8174b767fa6SAndrey A. Chernov for (i = pglob->gl_offs + 1; --i > 0; ) 81858f0484fSRodney W. Grimes *--pathv = NULL; 81958f0484fSRodney W. Grimes } 82058f0484fSRodney W. Grimes pglob->gl_pathv = pathv; 82158f0484fSRodney W. Grimes 82258f0484fSRodney W. Grimes for (p = path; *p++;) 82358f0484fSRodney W. Grimes continue; 824e9346e01STim J. Robbins len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */ 825daab0b01SMarcel Moolenaar limit->l_string_cnt += len; 826daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 827daab0b01SMarcel Moolenaar limit->l_string_cnt >= GLOB_LIMIT_STRING) { 828daab0b01SMarcel Moolenaar errno = 0; 829daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 830daab0b01SMarcel Moolenaar } 83127d52f69SPeter Wemm if ((copy = malloc(len)) != NULL) { 83227d52f69SPeter Wemm if (g_Ctoc(path, copy, len)) { 83376e2bc01SPeter Wemm free(copy); 83476e2bc01SPeter Wemm return (GLOB_NOSPACE); 83576e2bc01SPeter Wemm } 83658f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 83758f0484fSRodney W. Grimes } 83858f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 83958f0484fSRodney W. Grimes return (copy == NULL ? GLOB_NOSPACE : 0); 84058f0484fSRodney W. Grimes } 84158f0484fSRodney W. Grimes 84258f0484fSRodney W. Grimes /* 84358f0484fSRodney W. Grimes * pattern matching function for filenames. Each occurrence of the * 84458f0484fSRodney W. Grimes * pattern causes a recursion level. 84558f0484fSRodney W. Grimes */ 84658f0484fSRodney W. Grimes static int 8471cec70adSXin LI match(Char *name, Char *pat, Char *patend) 84858f0484fSRodney W. Grimes { 84958f0484fSRodney W. Grimes int ok, negate_range; 85058f0484fSRodney W. Grimes Char c, k; 8511daad8f5SAndrey A. Chernov struct xlocale_collate *table = 8521daad8f5SAndrey A. Chernov (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 85358f0484fSRodney W. Grimes 85458f0484fSRodney W. Grimes while (pat < patend) { 85558f0484fSRodney W. Grimes c = *pat++; 85658f0484fSRodney W. Grimes switch (c & M_MASK) { 85758f0484fSRodney W. Grimes case M_ALL: 85858f0484fSRodney W. Grimes if (pat == patend) 85958f0484fSRodney W. Grimes return (1); 86058f0484fSRodney W. Grimes do 86158f0484fSRodney W. Grimes if (match(name, pat, patend)) 86258f0484fSRodney W. Grimes return (1); 86358f0484fSRodney W. Grimes while (*name++ != EOS); 86458f0484fSRodney W. Grimes return (0); 86558f0484fSRodney W. Grimes case M_ONE: 86658f0484fSRodney W. Grimes if (*name++ == EOS) 86758f0484fSRodney W. Grimes return (0); 86858f0484fSRodney W. Grimes break; 86958f0484fSRodney W. Grimes case M_SET: 87058f0484fSRodney W. Grimes ok = 0; 87158f0484fSRodney W. Grimes if ((k = *name++) == EOS) 87258f0484fSRodney W. Grimes return (0); 87358f0484fSRodney W. Grimes if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 87458f0484fSRodney W. Grimes ++pat; 87558f0484fSRodney W. Grimes while (((c = *pat++) & M_MASK) != M_END) 87658f0484fSRodney W. Grimes if ((*pat & M_MASK) == M_RNG) { 8771daad8f5SAndrey A. Chernov if (table->__collate_load_error ? 878*aa3d69a6SAndrey A. Chernov CHAR(c) <= CHAR(k) && 879*aa3d69a6SAndrey A. Chernov CHAR(k) <= CHAR(pat[1]) : 880*aa3d69a6SAndrey A. Chernov __wcollate_range_cmp(CHAR(c), 881*aa3d69a6SAndrey A. Chernov CHAR(k)) <= 0 && 882*aa3d69a6SAndrey A. Chernov __wcollate_range_cmp(CHAR(k), 883*aa3d69a6SAndrey A. Chernov CHAR(pat[1])) <= 0) 88458f0484fSRodney W. Grimes ok = 1; 88558f0484fSRodney W. Grimes pat += 2; 88658f0484fSRodney W. Grimes } else if (c == k) 88758f0484fSRodney W. Grimes ok = 1; 88858f0484fSRodney W. Grimes if (ok == negate_range) 88958f0484fSRodney W. Grimes return (0); 89058f0484fSRodney W. Grimes break; 89158f0484fSRodney W. Grimes default: 89258f0484fSRodney W. Grimes if (*name++ != c) 89358f0484fSRodney W. Grimes return (0); 89458f0484fSRodney W. Grimes break; 89558f0484fSRodney W. Grimes } 89658f0484fSRodney W. Grimes } 89758f0484fSRodney W. Grimes return (*name == EOS); 89858f0484fSRodney W. Grimes } 89958f0484fSRodney W. Grimes 90058f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */ 90158f0484fSRodney W. Grimes void 9021cec70adSXin LI globfree(glob_t *pglob) 90358f0484fSRodney W. Grimes { 9044b767fa6SAndrey A. Chernov size_t i; 905b231cb39SDavid E. O'Brien char **pp; 90658f0484fSRodney W. Grimes 90758f0484fSRodney W. Grimes if (pglob->gl_pathv != NULL) { 90858f0484fSRodney W. Grimes pp = pglob->gl_pathv + pglob->gl_offs; 90958f0484fSRodney W. Grimes for (i = pglob->gl_pathc; i--; ++pp) 91058f0484fSRodney W. Grimes if (*pp) 91158f0484fSRodney W. Grimes free(*pp); 91258f0484fSRodney W. Grimes free(pglob->gl_pathv); 91376e2bc01SPeter Wemm pglob->gl_pathv = NULL; 91458f0484fSRodney W. Grimes } 91558f0484fSRodney W. Grimes } 91658f0484fSRodney W. Grimes 91758f0484fSRodney W. Grimes static DIR * 9181cec70adSXin LI g_opendir(Char *str, glob_t *pglob) 91958f0484fSRodney W. Grimes { 92058f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 92158f0484fSRodney W. Grimes 922*aa3d69a6SAndrey A. Chernov if (*str == EOS) 92358f0484fSRodney W. Grimes strcpy(buf, "."); 92476e2bc01SPeter Wemm else { 92527d52f69SPeter Wemm if (g_Ctoc(str, buf, sizeof(buf))) 92676e2bc01SPeter Wemm return (NULL); 92776e2bc01SPeter Wemm } 92858f0484fSRodney W. Grimes 92958f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 93058f0484fSRodney W. Grimes return ((*pglob->gl_opendir)(buf)); 93158f0484fSRodney W. Grimes 93258f0484fSRodney W. Grimes return (opendir(buf)); 93358f0484fSRodney W. Grimes } 93458f0484fSRodney W. Grimes 93558f0484fSRodney W. Grimes static int 9361cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob) 93758f0484fSRodney W. Grimes { 93858f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 93958f0484fSRodney W. Grimes 94027d52f69SPeter Wemm if (g_Ctoc(fn, buf, sizeof(buf))) { 94176e2bc01SPeter Wemm errno = ENAMETOOLONG; 94276e2bc01SPeter Wemm return (-1); 94376e2bc01SPeter Wemm } 94458f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 94558f0484fSRodney W. Grimes return((*pglob->gl_lstat)(buf, sb)); 94658f0484fSRodney W. Grimes return (lstat(buf, sb)); 94758f0484fSRodney W. Grimes } 94858f0484fSRodney W. Grimes 94958f0484fSRodney W. Grimes static int 9501cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob) 95158f0484fSRodney W. Grimes { 95258f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 95358f0484fSRodney W. Grimes 95427d52f69SPeter Wemm if (g_Ctoc(fn, buf, sizeof(buf))) { 95576e2bc01SPeter Wemm errno = ENAMETOOLONG; 95676e2bc01SPeter Wemm return (-1); 95776e2bc01SPeter Wemm } 95858f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 95958f0484fSRodney W. Grimes return ((*pglob->gl_stat)(buf, sb)); 96058f0484fSRodney W. Grimes return (stat(buf, sb)); 96158f0484fSRodney W. Grimes } 96258f0484fSRodney W. Grimes 96334a08754SMike Makonnen static const Char * 96434a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch) 96558f0484fSRodney W. Grimes { 9661cec70adSXin LI 96758f0484fSRodney W. Grimes do { 96858f0484fSRodney W. Grimes if (*str == ch) 96958f0484fSRodney W. Grimes return (str); 97058f0484fSRodney W. Grimes } while (*str++); 97158f0484fSRodney W. Grimes return (NULL); 97258f0484fSRodney W. Grimes } 97358f0484fSRodney W. Grimes 97476e2bc01SPeter Wemm static int 9751cec70adSXin LI g_Ctoc(const Char *str, char *buf, size_t len) 97658f0484fSRodney W. Grimes { 977e9346e01STim J. Robbins mbstate_t mbs; 978e9346e01STim J. Robbins size_t clen; 97958f0484fSRodney W. Grimes 980e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 981e9346e01STim J. Robbins while (len >= MB_CUR_MAX) { 982e9346e01STim J. Robbins clen = wcrtomb(buf, *str, &mbs); 983*aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1) { 984*aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 985*aa3d69a6SAndrey A. Chernov *buf = (char)*str; 986*aa3d69a6SAndrey A. Chernov clen = 1; 987*aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 988*aa3d69a6SAndrey A. Chernov } 989*aa3d69a6SAndrey A. Chernov if (*buf == '\0') 99076e2bc01SPeter Wemm return (0); 991e9346e01STim J. Robbins str++; 992e9346e01STim J. Robbins buf += clen; 993e9346e01STim J. Robbins len -= clen; 99458f0484fSRodney W. Grimes } 99527d52f69SPeter Wemm return (1); 99627d52f69SPeter Wemm } 99758f0484fSRodney W. Grimes 99858f0484fSRodney W. Grimes #ifdef DEBUG 99958f0484fSRodney W. Grimes static void 10001cec70adSXin LI qprintf(const char *str, Char *s) 100158f0484fSRodney W. Grimes { 1002b231cb39SDavid E. O'Brien Char *p; 100358f0484fSRodney W. Grimes 100458f0484fSRodney W. Grimes (void)printf("%s:\n", str); 100558f0484fSRodney W. Grimes for (p = s; *p; p++) 100658f0484fSRodney W. Grimes (void)printf("%c", CHAR(*p)); 100758f0484fSRodney W. Grimes (void)printf("\n"); 100858f0484fSRodney W. Grimes for (p = s; *p; p++) 100958f0484fSRodney W. Grimes (void)printf("%c", *p & M_PROTECT ? '"' : ' '); 101058f0484fSRodney W. Grimes (void)printf("\n"); 101158f0484fSRodney W. Grimes for (p = s; *p; p++) 101258f0484fSRodney W. Grimes (void)printf("%c", ismeta(*p) ? '_' : ' '); 101358f0484fSRodney W. Grimes (void)printf("\n"); 101458f0484fSRodney W. Grimes } 101558f0484fSRodney W. Grimes #endif 1016