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 74aa3d69a6SAndrey 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 116aa3d69a6SAndrey A. Chernov #define DOT L'.' 117aa3d69a6SAndrey A. Chernov #define EOS L'\0' 118aa3d69a6SAndrey A. Chernov #define LBRACKET L'[' 119aa3d69a6SAndrey A. Chernov #define NOT L'!' 120aa3d69a6SAndrey A. Chernov #define QUESTION L'?' 121aa3d69a6SAndrey A. Chernov #define QUOTE L'\\' 122aa3d69a6SAndrey A. Chernov #define RANGE L'-' 123aa3d69a6SAndrey A. Chernov #define RBRACKET L']' 124aa3d69a6SAndrey A. Chernov #define SEP L'/' 125aa3d69a6SAndrey A. Chernov #define STAR L'*' 126aa3d69a6SAndrey A. Chernov #define TILDE L'~' 127aa3d69a6SAndrey A. Chernov #define LBRACE L'{' 128aa3d69a6SAndrey A. Chernov #define RBRACE L'}' 129aa3d69a6SAndrey A. Chernov #define COMMA L',' 13058f0484fSRodney W. Grimes 131e9346e01STim J. Robbins #define M_QUOTE 0x8000000000ULL 132e9346e01STim J. Robbins #define M_PROTECT 0x4000000000ULL 133e9346e01STim J. Robbins #define M_MASK 0xffffffffffULL 134e9346e01STim J. Robbins #define M_CHAR 0x00ffffffffULL 13558f0484fSRodney W. Grimes 136e9346e01STim J. Robbins typedef uint_fast64_t Char; 13758f0484fSRodney W. Grimes 138e9346e01STim J. Robbins #define CHAR(c) ((Char)((c)&M_CHAR)) 13958f0484fSRodney W. Grimes #define META(c) ((Char)((c)|M_QUOTE)) 140aed721ecSAndrey A. Chernov #define UNPROT(c) ((c) & ~M_PROTECT) 141aa3d69a6SAndrey A. Chernov #define M_ALL META(L'*') 142aa3d69a6SAndrey A. Chernov #define M_END META(L']') 143aa3d69a6SAndrey A. Chernov #define M_NOT META(L'!') 144aa3d69a6SAndrey A. Chernov #define M_ONE META(L'?') 145aa3d69a6SAndrey A. Chernov #define M_RNG META(L'-') 146aa3d69a6SAndrey A. Chernov #define M_SET META(L'[') 14758f0484fSRodney W. Grimes #define ismeta(c) (((c)&M_QUOTE) != 0) 14809264d74SAndrey A. Chernov #ifdef DEBUG 149aed721ecSAndrey A. Chernov #define isprot(c) (((c)&M_PROTECT) != 0) 15009264d74SAndrey A. Chernov #endif 15158f0484fSRodney W. Grimes 152b231cb39SDavid E. O'Brien static int compare(const void *, const void *); 15309264d74SAndrey A. Chernov static int g_Ctoc(const Char *, char *, size_t); 154b231cb39SDavid E. O'Brien static int g_lstat(Char *, struct stat *, glob_t *); 155b231cb39SDavid E. O'Brien static DIR *g_opendir(Char *, glob_t *); 15634a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t); 15758f0484fSRodney W. Grimes #ifdef notdef 158b231cb39SDavid E. O'Brien static Char *g_strcat(Char *, const Char *); 15958f0484fSRodney W. Grimes #endif 160b231cb39SDavid E. O'Brien static int g_stat(Char *, struct stat *, glob_t *); 16109264d74SAndrey A. Chernov static int glob0(const Char *, glob_t *, struct glob_limit *, 16209264d74SAndrey A. Chernov const char *); 163daab0b01SMarcel Moolenaar static int glob1(Char *, glob_t *, struct glob_limit *); 164daab0b01SMarcel Moolenaar static int glob2(Char *, Char *, Char *, Char *, glob_t *, 165daab0b01SMarcel Moolenaar struct glob_limit *); 166daab0b01SMarcel Moolenaar static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, 167daab0b01SMarcel Moolenaar struct glob_limit *); 16809264d74SAndrey A. Chernov static int globextend(const Char *, glob_t *, struct glob_limit *, 16909264d74SAndrey A. Chernov const char *); 170487dbd92SPeter Wemm static const Char * 171b231cb39SDavid E. O'Brien globtilde(const Char *, Char *, size_t, glob_t *); 17209264d74SAndrey A. Chernov static int globexp0(const Char *, glob_t *, struct glob_limit *, 17309264d74SAndrey A. Chernov const char *); 174daab0b01SMarcel Moolenaar static int globexp1(const Char *, glob_t *, struct glob_limit *); 175bd7a9850SAndrey A. Chernov static int globexp2(const Char *, const Char *, glob_t *, 176daab0b01SMarcel Moolenaar struct glob_limit *); 17709264d74SAndrey A. Chernov static int globfinal(glob_t *, struct glob_limit *, size_t, 17809264d74SAndrey A. Chernov const char *); 179b231cb39SDavid E. O'Brien static int match(Char *, Char *, Char *); 180*20e37fa8SAndrey A. Chernov static int err_nomatch(glob_t *, struct glob_limit *, const char *); 181*20e37fa8SAndrey A. Chernov static int err_aborted(glob_t *, int, char *); 18258f0484fSRodney W. Grimes #ifdef DEBUG 183b231cb39SDavid E. O'Brien static void qprintf(const char *, Char *); 18458f0484fSRodney W. Grimes #endif 18558f0484fSRodney W. Grimes 18658f0484fSRodney W. Grimes int 1870d6d372cSEitan Adler glob(const char * __restrict pattern, int flags, 1880d6d372cSEitan Adler int (*errfunc)(const char *, int), glob_t * __restrict pglob) 18958f0484fSRodney W. Grimes { 190daab0b01SMarcel Moolenaar struct glob_limit limit = { 0, 0, 0, 0, 0 }; 1911cec70adSXin LI const char *patnext; 192e9346e01STim J. Robbins Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot; 193e9346e01STim J. Robbins mbstate_t mbs; 194e9346e01STim J. Robbins wchar_t wc; 195e9346e01STim J. Robbins size_t clen; 196aed721ecSAndrey A. Chernov int too_long; 19758f0484fSRodney W. Grimes 1981cec70adSXin LI patnext = pattern; 19958f0484fSRodney W. Grimes if (!(flags & GLOB_APPEND)) { 20058f0484fSRodney W. Grimes pglob->gl_pathc = 0; 20158f0484fSRodney W. Grimes pglob->gl_pathv = NULL; 20258f0484fSRodney W. Grimes if (!(flags & GLOB_DOOFFS)) 20358f0484fSRodney W. Grimes pglob->gl_offs = 0; 20458f0484fSRodney W. Grimes } 20575dc5f1aSMike Heffner if (flags & GLOB_LIMIT) { 206daab0b01SMarcel Moolenaar limit.l_path_lim = pglob->gl_matchc; 207daab0b01SMarcel Moolenaar if (limit.l_path_lim == 0) 208daab0b01SMarcel Moolenaar limit.l_path_lim = GLOB_LIMIT_PATH; 209daab0b01SMarcel Moolenaar } 21058f0484fSRodney W. Grimes pglob->gl_flags = flags & ~GLOB_MAGCHAR; 21158f0484fSRodney W. Grimes pglob->gl_errfunc = errfunc; 21258f0484fSRodney W. Grimes pglob->gl_matchc = 0; 21358f0484fSRodney W. Grimes 21458f0484fSRodney W. Grimes bufnext = patbuf; 215487dbd92SPeter Wemm bufend = bufnext + MAXPATHLEN - 1; 216aed721ecSAndrey A. Chernov too_long = 1; 217e9346e01STim J. Robbins if (flags & GLOB_NOESCAPE) { 218e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 219aed721ecSAndrey A. Chernov while (bufnext <= bufend) { 220e9346e01STim J. Robbins clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs); 221e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) 222*20e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern)); 223aed721ecSAndrey A. Chernov else if (clen == 0) { 224aed721ecSAndrey A. Chernov too_long = 0; 225e9346e01STim J. Robbins break; 226aed721ecSAndrey A. Chernov } 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)); 233aed721ecSAndrey A. Chernov while (bufnext <= bufend) { 234aa3d69a6SAndrey A. Chernov if (*patnext == '\\') { 235aa3d69a6SAndrey A. Chernov if (*++patnext == '\0') { 236aed721ecSAndrey A. Chernov *bufnext++ = QUOTE; 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) 244*20e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern)); 245aed721ecSAndrey A. Chernov else if (clen == 0) { 246aed721ecSAndrey A. Chernov too_long = 0; 247e9346e01STim J. Robbins break; 248aed721ecSAndrey A. Chernov } 249aed721ecSAndrey A. Chernov *bufnext++ = wc | prot; 250e9346e01STim J. Robbins patnext += clen; 25158f0484fSRodney W. Grimes } 25258f0484fSRodney W. Grimes } 253aed721ecSAndrey A. Chernov if (too_long) 254*20e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern)); 25558f0484fSRodney W. Grimes *bufnext = EOS; 25658f0484fSRodney W. Grimes 25758f0484fSRodney W. Grimes if (flags & GLOB_BRACE) 25809264d74SAndrey A. Chernov return (globexp0(patbuf, pglob, &limit, pattern)); 25958f0484fSRodney W. Grimes else 26009264d74SAndrey A. Chernov return (glob0(patbuf, pglob, &limit, pattern)); 261bd7a9850SAndrey A. Chernov } 262bd7a9850SAndrey A. Chernov 263bd7a9850SAndrey A. Chernov static int 26409264d74SAndrey A. Chernov globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, 26509264d74SAndrey A. Chernov const char *origpat) { 266bd7a9850SAndrey A. Chernov int rv; 267bd7a9850SAndrey A. Chernov size_t oldpathc; 268bd7a9850SAndrey A. Chernov 269bd7a9850SAndrey A. Chernov /* Protect a single {}, for find(1), like csh */ 270bd7a9850SAndrey A. Chernov if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) { 271bd7a9850SAndrey A. Chernov if ((pglob->gl_flags & GLOB_LIMIT) && 272bd7a9850SAndrey A. Chernov limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { 273869eb80cSAndrey A. Chernov errno = E2BIG; 274bd7a9850SAndrey A. Chernov return (GLOB_NOSPACE); 275bd7a9850SAndrey A. Chernov } 27609264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, origpat)); 277bd7a9850SAndrey A. Chernov } 278bd7a9850SAndrey A. Chernov 279bd7a9850SAndrey A. Chernov oldpathc = pglob->gl_pathc; 280bd7a9850SAndrey A. Chernov 281bd7a9850SAndrey A. Chernov if ((rv = globexp1(pattern, pglob, limit)) != 0) 282bd7a9850SAndrey A. Chernov return rv; 28309264d74SAndrey A. Chernov 28409264d74SAndrey A. Chernov return (globfinal(pglob, limit, oldpathc, origpat)); 28558f0484fSRodney W. Grimes } 28658f0484fSRodney W. Grimes 28758f0484fSRodney W. Grimes /* 28858f0484fSRodney W. Grimes * Expand recursively a glob {} pattern. When there is no more expansion 28958f0484fSRodney W. Grimes * invoke the standard globbing routine to glob the rest of the magic 29058f0484fSRodney W. Grimes * characters 29158f0484fSRodney W. Grimes */ 292487dbd92SPeter Wemm static int 293daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) 29458f0484fSRodney W. Grimes { 295bd7a9850SAndrey A. Chernov const Char* ptr; 29658f0484fSRodney W. Grimes 297bd7a9850SAndrey A. Chernov if ((ptr = g_strchr(pattern, LBRACE)) != NULL) { 298daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 299daab0b01SMarcel Moolenaar limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { 300869eb80cSAndrey A. Chernov errno = E2BIG; 301daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 302daab0b01SMarcel Moolenaar } 303bd7a9850SAndrey A. Chernov return (globexp2(ptr, pattern, pglob, limit)); 304bd7a9850SAndrey A. Chernov } 305daab0b01SMarcel Moolenaar 30609264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, NULL)); 30758f0484fSRodney W. Grimes } 30858f0484fSRodney W. Grimes 30958f0484fSRodney W. Grimes 31058f0484fSRodney W. Grimes /* 31158f0484fSRodney W. Grimes * Recursive brace globbing helper. Tries to expand a single brace. 31258f0484fSRodney W. Grimes * If it succeeds then it invokes globexp1 with the new pattern. 31358f0484fSRodney W. Grimes * If it fails then it tries to glob the rest of the pattern and returns. 31458f0484fSRodney W. Grimes */ 315487dbd92SPeter Wemm static int 316bd7a9850SAndrey A. Chernov globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, 317daab0b01SMarcel Moolenaar struct glob_limit *limit) 31858f0484fSRodney W. Grimes { 319bd7a9850SAndrey A. Chernov int i, rv; 32058f0484fSRodney W. Grimes Char *lm, *ls; 321369316a8SAndrey A. Chernov const Char *pe, *pm, *pm1, *pl; 322487dbd92SPeter Wemm Char patbuf[MAXPATHLEN]; 32358f0484fSRodney W. Grimes 32458f0484fSRodney W. Grimes /* copy part up to the brace */ 32558f0484fSRodney W. Grimes for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 32658f0484fSRodney W. Grimes continue; 327487dbd92SPeter Wemm *lm = EOS; 32858f0484fSRodney W. Grimes ls = lm; 32958f0484fSRodney W. Grimes 33058f0484fSRodney W. Grimes /* Find the balanced brace */ 331bd7a9850SAndrey A. Chernov for (i = 0, pe = ++ptr; *pe != EOS; pe++) 33258f0484fSRodney W. Grimes if (*pe == LBRACKET) { 33358f0484fSRodney W. Grimes /* Ignore everything between [] */ 33458f0484fSRodney W. Grimes for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) 33558f0484fSRodney W. Grimes continue; 33658f0484fSRodney W. Grimes if (*pe == EOS) { 33758f0484fSRodney W. Grimes /* 33858f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 33958f0484fSRodney W. Grimes * Ignore and just look for RBRACE 34058f0484fSRodney W. Grimes */ 34158f0484fSRodney W. Grimes pe = pm; 34258f0484fSRodney W. Grimes } 34358f0484fSRodney W. Grimes } 34458f0484fSRodney W. Grimes else if (*pe == LBRACE) 34558f0484fSRodney W. Grimes i++; 34658f0484fSRodney W. Grimes else if (*pe == RBRACE) { 34758f0484fSRodney W. Grimes if (i == 0) 34858f0484fSRodney W. Grimes break; 34958f0484fSRodney W. Grimes i--; 35058f0484fSRodney W. Grimes } 35158f0484fSRodney W. Grimes 35258f0484fSRodney W. Grimes /* Non matching braces; just glob the pattern */ 353bd7a9850SAndrey A. Chernov if (i != 0 || *pe == EOS) 35409264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, NULL)); 35558f0484fSRodney W. Grimes 35658f0484fSRodney W. Grimes for (i = 0, pl = pm = ptr; pm <= pe; pm++) 35758f0484fSRodney W. Grimes switch (*pm) { 35858f0484fSRodney W. Grimes case LBRACKET: 35958f0484fSRodney W. Grimes /* Ignore everything between [] */ 360369316a8SAndrey A. Chernov for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++) 36158f0484fSRodney W. Grimes continue; 36258f0484fSRodney W. Grimes if (*pm == EOS) { 36358f0484fSRodney W. Grimes /* 36458f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 36558f0484fSRodney W. Grimes * Ignore and just look for RBRACE 36658f0484fSRodney W. Grimes */ 367369316a8SAndrey A. Chernov pm = pm1; 36858f0484fSRodney W. Grimes } 36958f0484fSRodney W. Grimes break; 37058f0484fSRodney W. Grimes 37158f0484fSRodney W. Grimes case LBRACE: 37258f0484fSRodney W. Grimes i++; 37358f0484fSRodney W. Grimes break; 37458f0484fSRodney W. Grimes 37558f0484fSRodney W. Grimes case RBRACE: 37658f0484fSRodney W. Grimes if (i) { 37758f0484fSRodney W. Grimes i--; 37858f0484fSRodney W. Grimes break; 37958f0484fSRodney W. Grimes } 38058f0484fSRodney W. Grimes /* FALLTHROUGH */ 38158f0484fSRodney W. Grimes case COMMA: 38258f0484fSRodney W. Grimes if (i && *pm == COMMA) 38358f0484fSRodney W. Grimes break; 38458f0484fSRodney W. Grimes else { 38558f0484fSRodney W. Grimes /* Append the current string */ 38658f0484fSRodney W. Grimes for (lm = ls; (pl < pm); *lm++ = *pl++) 38758f0484fSRodney W. Grimes continue; 38858f0484fSRodney W. Grimes /* 38958f0484fSRodney W. Grimes * Append the rest of the pattern after the 39058f0484fSRodney W. Grimes * closing brace 39158f0484fSRodney W. Grimes */ 39258f0484fSRodney W. Grimes for (pl = pe + 1; (*lm++ = *pl++) != EOS;) 39358f0484fSRodney W. Grimes continue; 39458f0484fSRodney W. Grimes 39558f0484fSRodney W. Grimes /* Expand the current pattern */ 39658f0484fSRodney W. Grimes #ifdef DEBUG 39758f0484fSRodney W. Grimes qprintf("globexp2:", patbuf); 39858f0484fSRodney W. Grimes #endif 399bd7a9850SAndrey A. Chernov rv = globexp1(patbuf, pglob, limit); 400bd7a9850SAndrey A. Chernov if (rv) 401bd7a9850SAndrey A. Chernov return (rv); 40258f0484fSRodney W. Grimes 40358f0484fSRodney W. Grimes /* move after the comma, to the next string */ 40458f0484fSRodney W. Grimes pl = pm + 1; 40558f0484fSRodney W. Grimes } 40658f0484fSRodney W. Grimes break; 40758f0484fSRodney W. Grimes 40858f0484fSRodney W. Grimes default: 40958f0484fSRodney W. Grimes break; 41058f0484fSRodney W. Grimes } 41185529174SEitan Adler return (0); 41258f0484fSRodney W. Grimes } 41358f0484fSRodney W. Grimes 41458f0484fSRodney W. Grimes 41558f0484fSRodney W. Grimes 41658f0484fSRodney W. Grimes /* 41758f0484fSRodney W. Grimes * expand tilde from the passwd file. 41858f0484fSRodney W. Grimes */ 41958f0484fSRodney W. Grimes static const Char * 4201cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) 42158f0484fSRodney W. Grimes { 42258f0484fSRodney W. Grimes struct passwd *pwd; 423aa3d69a6SAndrey A. Chernov char *h, *sc; 42458f0484fSRodney W. Grimes const Char *p; 42562f187a4SWarner Losh Char *b, *eb; 426aa3d69a6SAndrey A. Chernov wchar_t wc; 427aa3d69a6SAndrey A. Chernov wchar_t wbuf[MAXPATHLEN]; 428aa3d69a6SAndrey A. Chernov wchar_t *wbufend, *dc; 429aa3d69a6SAndrey A. Chernov size_t clen; 430aa3d69a6SAndrey A. Chernov mbstate_t mbs; 431aa3d69a6SAndrey A. Chernov int too_long; 43258f0484fSRodney W. Grimes 43358f0484fSRodney W. Grimes if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) 43485529174SEitan Adler return (pattern); 43558f0484fSRodney W. Grimes 43662f187a4SWarner Losh /* 43762f187a4SWarner Losh * Copy up to the end of the string or / 43862f187a4SWarner Losh */ 43962f187a4SWarner Losh eb = &patbuf[patbuf_len - 1]; 440aa3d69a6SAndrey A. Chernov for (p = pattern + 1, b = patbuf; 4417455a07aSAndrey A. Chernov b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++) 44258f0484fSRodney W. Grimes continue; 44358f0484fSRodney W. Grimes 4447455a07aSAndrey A. Chernov if (*p != EOS && UNPROT(*p) != SEP) 445f4d4982eSAndrey A. Chernov return (NULL); 44658f0484fSRodney W. Grimes 447aa3d69a6SAndrey A. Chernov *b = EOS; 448aa3d69a6SAndrey A. Chernov h = NULL; 449aa3d69a6SAndrey A. Chernov 450aa3d69a6SAndrey A. Chernov if (patbuf[0] == EOS) { 45158f0484fSRodney W. Grimes /* 4523fa69daeSWarner Losh * handle a plain ~ or ~/ by expanding $HOME first (iff 4533fa69daeSWarner Losh * we're not running setuid or setgid) and then trying 4543fa69daeSWarner Losh * the password file 45558f0484fSRodney W. Grimes */ 4564539e95aSTim J. Robbins if (issetugid() != 0 || 4579fcbcd02SJohn Birrell (h = getenv("HOME")) == NULL) { 458eb8eee5aSAndrey A. Chernov if (((h = getlogin()) != NULL && 459eb8eee5aSAndrey A. Chernov (pwd = getpwnam(h)) != NULL) || 460eb8eee5aSAndrey A. Chernov (pwd = getpwuid(getuid())) != NULL) 46158f0484fSRodney W. Grimes h = pwd->pw_dir; 462eb8eee5aSAndrey A. Chernov else 46385529174SEitan Adler return (pattern); 46458f0484fSRodney W. Grimes } 46558f0484fSRodney W. Grimes } 46658f0484fSRodney W. Grimes else { 46758f0484fSRodney W. Grimes /* 46858f0484fSRodney W. Grimes * Expand a ~user 46958f0484fSRodney W. Grimes */ 47009264d74SAndrey A. Chernov if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf))) 471f4d4982eSAndrey A. Chernov return (NULL); 472f4d4982eSAndrey A. Chernov if ((pwd = getpwnam((char *)wbuf)) == NULL) 47385529174SEitan Adler return (pattern); 47458f0484fSRodney W. Grimes else 47558f0484fSRodney W. Grimes h = pwd->pw_dir; 47658f0484fSRodney W. Grimes } 47758f0484fSRodney W. Grimes 47858f0484fSRodney W. Grimes /* Copy the home directory */ 479aa3d69a6SAndrey A. Chernov dc = wbuf; 480aa3d69a6SAndrey A. Chernov sc = h; 481aa3d69a6SAndrey A. Chernov wbufend = wbuf + MAXPATHLEN - 1; 482aa3d69a6SAndrey A. Chernov too_long = 1; 483aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 484aa3d69a6SAndrey A. Chernov while (dc <= wbufend) { 485aa3d69a6SAndrey A. Chernov clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs); 486aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1 || clen == (size_t)-2) { 487aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 488aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc; 489aa3d69a6SAndrey A. Chernov clen = 1; 490aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 491aa3d69a6SAndrey A. Chernov } 492aa3d69a6SAndrey A. Chernov if ((*dc++ = wc) == EOS) { 493aa3d69a6SAndrey A. Chernov too_long = 0; 494aa3d69a6SAndrey A. Chernov break; 495aa3d69a6SAndrey A. Chernov } 496aa3d69a6SAndrey A. Chernov sc += clen; 497aa3d69a6SAndrey A. Chernov } 498aa3d69a6SAndrey A. Chernov if (too_long) 499f4d4982eSAndrey A. Chernov return (NULL); 500aa3d69a6SAndrey A. Chernov 501aa3d69a6SAndrey A. Chernov dc = wbuf; 502aed721ecSAndrey A. Chernov for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT) 503aed721ecSAndrey A. Chernov continue; 504aa3d69a6SAndrey A. Chernov if (*dc != EOS) 505f4d4982eSAndrey A. Chernov return (NULL); 50658f0484fSRodney W. Grimes 50758f0484fSRodney W. Grimes /* Append the rest of the pattern */ 508aa3d69a6SAndrey A. Chernov if (*p != EOS) { 509aa3d69a6SAndrey A. Chernov too_long = 1; 510aa3d69a6SAndrey A. Chernov while (b <= eb) { 511aa3d69a6SAndrey A. Chernov if ((*b++ = *p++) == EOS) { 512aa3d69a6SAndrey A. Chernov too_long = 0; 513aa3d69a6SAndrey A. Chernov break; 514aa3d69a6SAndrey A. Chernov } 515aa3d69a6SAndrey A. Chernov } 516aa3d69a6SAndrey A. Chernov if (too_long) 517f4d4982eSAndrey A. Chernov return (NULL); 518aa3d69a6SAndrey A. Chernov } else 51962f187a4SWarner Losh *b = EOS; 52058f0484fSRodney W. Grimes 52185529174SEitan Adler return (patbuf); 52258f0484fSRodney W. Grimes } 52358f0484fSRodney W. Grimes 52458f0484fSRodney W. Grimes 52558f0484fSRodney W. Grimes /* 52658f0484fSRodney W. Grimes * The main glob() routine: compiles the pattern (optionally processing 52758f0484fSRodney W. Grimes * quotes), calls glob1() to do the real pattern matching, and finally 52858f0484fSRodney W. Grimes * sorts the list (unless unsorted operation is requested). Returns 0 5294a59c3abSMike Heffner * if things went well, nonzero if errors occurred. 53058f0484fSRodney W. Grimes */ 53158f0484fSRodney W. Grimes static int 532bd7a9850SAndrey A. Chernov glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, 53309264d74SAndrey A. Chernov const char *origpat) { 53458f0484fSRodney W. Grimes const Char *qpatnext; 5355b3fab81SGordon Tetlow int err; 5364b767fa6SAndrey A. Chernov size_t oldpathc; 5375b3fab81SGordon Tetlow Char *bufnext, c, patbuf[MAXPATHLEN]; 53858f0484fSRodney W. Grimes 539487dbd92SPeter Wemm qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); 540f4d4982eSAndrey A. Chernov if (qpatnext == NULL) { 541869eb80cSAndrey A. Chernov errno = E2BIG; 542f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE); 543f4d4982eSAndrey A. Chernov } 54458f0484fSRodney W. Grimes oldpathc = pglob->gl_pathc; 54558f0484fSRodney W. Grimes bufnext = patbuf; 54658f0484fSRodney W. Grimes 54758f0484fSRodney W. Grimes /* We don't need to check for buffer overflow any more. */ 54858f0484fSRodney W. Grimes while ((c = *qpatnext++) != EOS) { 54958f0484fSRodney W. Grimes switch (c) { 55058f0484fSRodney W. Grimes case LBRACKET: 55158f0484fSRodney W. Grimes c = *qpatnext; 55258f0484fSRodney W. Grimes if (c == NOT) 55358f0484fSRodney W. Grimes ++qpatnext; 55458f0484fSRodney W. Grimes if (*qpatnext == EOS || 55534a08754SMike Makonnen g_strchr(qpatnext+1, RBRACKET) == NULL) { 55658f0484fSRodney W. Grimes *bufnext++ = LBRACKET; 55758f0484fSRodney W. Grimes if (c == NOT) 55858f0484fSRodney W. Grimes --qpatnext; 55958f0484fSRodney W. Grimes break; 56058f0484fSRodney W. Grimes } 56158f0484fSRodney W. Grimes *bufnext++ = M_SET; 56258f0484fSRodney W. Grimes if (c == NOT) 56358f0484fSRodney W. Grimes *bufnext++ = M_NOT; 56458f0484fSRodney W. Grimes c = *qpatnext++; 56558f0484fSRodney W. Grimes do { 56658f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 56758f0484fSRodney W. Grimes if (*qpatnext == RANGE && 56858f0484fSRodney W. Grimes (c = qpatnext[1]) != RBRACKET) { 56958f0484fSRodney W. Grimes *bufnext++ = M_RNG; 57058f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 57158f0484fSRodney W. Grimes qpatnext += 2; 57258f0484fSRodney W. Grimes } 57358f0484fSRodney W. Grimes } while ((c = *qpatnext++) != RBRACKET); 57458f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 57558f0484fSRodney W. Grimes *bufnext++ = M_END; 57658f0484fSRodney W. Grimes break; 57758f0484fSRodney W. Grimes case QUESTION: 57858f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 57958f0484fSRodney W. Grimes *bufnext++ = M_ONE; 58058f0484fSRodney W. Grimes break; 58158f0484fSRodney W. Grimes case STAR: 58258f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 58358f0484fSRodney W. Grimes /* collapse adjacent stars to one, 58458f0484fSRodney W. Grimes * to avoid exponential behavior 58558f0484fSRodney W. Grimes */ 58658f0484fSRodney W. Grimes if (bufnext == patbuf || bufnext[-1] != M_ALL) 58758f0484fSRodney W. Grimes *bufnext++ = M_ALL; 58858f0484fSRodney W. Grimes break; 58958f0484fSRodney W. Grimes default: 59058f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 59158f0484fSRodney W. Grimes break; 59258f0484fSRodney W. Grimes } 59358f0484fSRodney W. Grimes } 59458f0484fSRodney W. Grimes *bufnext = EOS; 59558f0484fSRodney W. Grimes #ifdef DEBUG 59658f0484fSRodney W. Grimes qprintf("glob0:", patbuf); 59758f0484fSRodney W. Grimes #endif 59858f0484fSRodney W. Grimes 599bae8632fSJonathan Lemon if ((err = glob1(patbuf, pglob, limit)) != 0) 60058f0484fSRodney W. Grimes return(err); 60158f0484fSRodney W. Grimes 60209264d74SAndrey A. Chernov if (origpat != NULL) 60309264d74SAndrey A. Chernov return (globfinal(pglob, limit, oldpathc, origpat)); 60409264d74SAndrey A. Chernov 60509264d74SAndrey A. Chernov return (0); 60609264d74SAndrey A. Chernov } 60709264d74SAndrey A. Chernov 60809264d74SAndrey A. Chernov static int 60909264d74SAndrey A. Chernov globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc, 61009264d74SAndrey A. Chernov const char *origpat) { 611*20e37fa8SAndrey A. Chernov if (pglob->gl_pathc == oldpathc) 612*20e37fa8SAndrey A. Chernov return (err_nomatch(pglob, limit, origpat)); 613*20e37fa8SAndrey A. Chernov 6144a59c3abSMike Heffner if (!(pglob->gl_flags & GLOB_NOSORT)) 61558f0484fSRodney W. Grimes qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 61658f0484fSRodney W. Grimes pglob->gl_pathc - oldpathc, sizeof(char *), compare); 61709264d74SAndrey A. Chernov 61858f0484fSRodney W. Grimes return (0); 61958f0484fSRodney W. Grimes } 62058f0484fSRodney W. Grimes 62158f0484fSRodney W. Grimes static int 6221cec70adSXin LI compare(const void *p, const void *q) 62358f0484fSRodney W. Grimes { 624aa3d69a6SAndrey A. Chernov return (strcoll(*(char **)p, *(char **)q)); 62558f0484fSRodney W. Grimes } 62658f0484fSRodney W. Grimes 62758f0484fSRodney W. Grimes static int 628daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) 62958f0484fSRodney W. Grimes { 630487dbd92SPeter Wemm Char pathbuf[MAXPATHLEN]; 63158f0484fSRodney W. Grimes 63258f0484fSRodney W. Grimes /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 63358f0484fSRodney W. Grimes if (*pattern == EOS) 63458f0484fSRodney W. Grimes return (0); 635487dbd92SPeter Wemm return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, 636487dbd92SPeter Wemm pattern, pglob, limit)); 63758f0484fSRodney W. Grimes } 63858f0484fSRodney W. Grimes 63958f0484fSRodney W. Grimes /* 64058f0484fSRodney W. Grimes * The functions glob2 and glob3 are mutually recursive; there is one level 64158f0484fSRodney W. Grimes * of recursion for each segment in the pattern that contains one or more 64258f0484fSRodney W. Grimes * meta characters. 64358f0484fSRodney W. Grimes */ 64458f0484fSRodney W. Grimes static int 6451cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, 646daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit) 64758f0484fSRodney W. Grimes { 64858f0484fSRodney W. Grimes struct stat sb; 64958f0484fSRodney W. Grimes Char *p, *q; 65058f0484fSRodney W. Grimes int anymeta; 65158f0484fSRodney W. Grimes 65258f0484fSRodney W. Grimes /* 65358f0484fSRodney W. Grimes * Loop over pattern segments until end of pattern or until 65458f0484fSRodney W. Grimes * segment with meta character found. 65558f0484fSRodney W. Grimes */ 65658f0484fSRodney W. Grimes for (anymeta = 0;;) { 65758f0484fSRodney W. Grimes if (*pattern == EOS) { /* End of pattern? */ 65858f0484fSRodney W. Grimes *pathend = EOS; 65958f0484fSRodney W. Grimes if (g_lstat(pathbuf, &sb, pglob)) 66058f0484fSRodney W. Grimes return (0); 66158f0484fSRodney W. Grimes 662daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 663daab0b01SMarcel Moolenaar limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) { 664869eb80cSAndrey A. Chernov errno = E2BIG; 665daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 666daab0b01SMarcel Moolenaar } 667aed721ecSAndrey A. Chernov if ((pglob->gl_flags & GLOB_MARK) && 668aed721ecSAndrey A. Chernov UNPROT(pathend[-1]) != SEP && 669aed721ecSAndrey A. Chernov (S_ISDIR(sb.st_mode) || 670aed721ecSAndrey A. Chernov (S_ISLNK(sb.st_mode) && 671aed721ecSAndrey A. Chernov g_stat(pathbuf, &sb, pglob) == 0 && 67258f0484fSRodney W. Grimes S_ISDIR(sb.st_mode)))) { 673f4d4982eSAndrey A. Chernov if (pathend + 1 > pathend_last) { 674869eb80cSAndrey A. Chernov errno = E2BIG; 675f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE); 676f4d4982eSAndrey A. Chernov } 67758f0484fSRodney W. Grimes *pathend++ = SEP; 67858f0484fSRodney W. Grimes *pathend = EOS; 67958f0484fSRodney W. Grimes } 68058f0484fSRodney W. Grimes ++pglob->gl_matchc; 68109264d74SAndrey A. Chernov return (globextend(pathbuf, pglob, limit, NULL)); 68258f0484fSRodney W. Grimes } 68358f0484fSRodney W. Grimes 68458f0484fSRodney W. Grimes /* Find end of next segment, copy tentatively to pathend. */ 68558f0484fSRodney W. Grimes q = pathend; 68658f0484fSRodney W. Grimes p = pattern; 687aed721ecSAndrey A. Chernov while (*p != EOS && UNPROT(*p) != SEP) { 68858f0484fSRodney W. Grimes if (ismeta(*p)) 68958f0484fSRodney W. Grimes anymeta = 1; 690f4d4982eSAndrey A. Chernov if (q + 1 > pathend_last) { 691869eb80cSAndrey A. Chernov errno = E2BIG; 692f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE); 693f4d4982eSAndrey A. Chernov } 69458f0484fSRodney W. Grimes *q++ = *p++; 69558f0484fSRodney W. Grimes } 69658f0484fSRodney W. Grimes 69758f0484fSRodney W. Grimes if (!anymeta) { /* No expansion, do next segment. */ 69858f0484fSRodney W. Grimes pathend = q; 69958f0484fSRodney W. Grimes pattern = p; 700aed721ecSAndrey A. Chernov while (UNPROT(*pattern) == SEP) { 701f4d4982eSAndrey A. Chernov if (pathend + 1 > pathend_last) { 702869eb80cSAndrey A. Chernov errno = E2BIG; 703f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE); 704f4d4982eSAndrey A. Chernov } 70558f0484fSRodney W. Grimes *pathend++ = *pattern++; 706487dbd92SPeter Wemm } 70758f0484fSRodney W. Grimes } else /* Need expansion, recurse. */ 70885529174SEitan Adler return (glob3(pathbuf, pathend, pathend_last, pattern, 70985529174SEitan Adler p, pglob, limit)); 71058f0484fSRodney W. Grimes } 71158f0484fSRodney W. Grimes /* NOTREACHED */ 71258f0484fSRodney W. Grimes } 71358f0484fSRodney W. Grimes 71458f0484fSRodney W. Grimes static int 7151cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last, 7161cec70adSXin LI Char *pattern, Char *restpattern, 717daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit) 71858f0484fSRodney W. Grimes { 719b231cb39SDavid E. O'Brien struct dirent *dp; 72058f0484fSRodney W. Grimes DIR *dirp; 721869eb80cSAndrey A. Chernov int err, too_long, saverrno, saverrno2; 722e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1]; 72358f0484fSRodney W. Grimes 7243e2981e8SCraig Rodrigues struct dirent *(*readdirfunc)(DIR *); 72558f0484fSRodney W. Grimes 726aed721ecSAndrey A. Chernov if (pathend > pathend_last) { 727869eb80cSAndrey A. Chernov errno = E2BIG; 728f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE); 729aed721ecSAndrey A. Chernov } 730f4d4982eSAndrey A. Chernov *pathend = EOS; 731aed721ecSAndrey A. Chernov if (pglob->gl_errfunc != NULL && 73209264d74SAndrey A. Chernov g_Ctoc(pathbuf, buf, sizeof(buf))) { 733869eb80cSAndrey A. Chernov errno = E2BIG; 734aed721ecSAndrey A. Chernov return (GLOB_NOSPACE); 735aed721ecSAndrey A. Chernov } 73658f0484fSRodney W. Grimes 737869eb80cSAndrey A. Chernov saverrno = errno; 738aed721ecSAndrey A. Chernov errno = 0; 73958f0484fSRodney W. Grimes if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 740196d61a9SAndrey A. Chernov if (errno == ENOENT || errno == ENOTDIR) 741196d61a9SAndrey A. Chernov return (0); 742*20e37fa8SAndrey A. Chernov err = err_aborted(pglob, errno, buf); 743869eb80cSAndrey A. Chernov if (errno == 0) 744869eb80cSAndrey A. Chernov errno = saverrno; 745*20e37fa8SAndrey A. Chernov return (err); 74658f0484fSRodney W. Grimes } 74758f0484fSRodney W. Grimes 74858f0484fSRodney W. Grimes err = 0; 74958f0484fSRodney W. Grimes 7503e2981e8SCraig Rodrigues /* pglob->gl_readdir takes a void *, fix this manually */ 75158f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 7523e2981e8SCraig Rodrigues readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir; 75358f0484fSRodney W. Grimes else 75458f0484fSRodney W. Grimes readdirfunc = readdir; 7553e2981e8SCraig Rodrigues 756aed721ecSAndrey A. Chernov errno = 0; 7573e2981e8SCraig Rodrigues /* Search directory for matching names. */ 7583e2981e8SCraig Rodrigues while ((dp = (*readdirfunc)(dirp)) != NULL) { 7591cec70adSXin LI char *sc; 760b231cb39SDavid E. O'Brien Char *dc; 761e9346e01STim J. Robbins wchar_t wc; 762e9346e01STim J. Robbins size_t clen; 763e9346e01STim J. Robbins mbstate_t mbs; 76458f0484fSRodney W. Grimes 765daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 766daab0b01SMarcel Moolenaar limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) { 767869eb80cSAndrey A. Chernov errno = E2BIG; 768f4d4982eSAndrey A. Chernov err = GLOB_NOSPACE; 769daab0b01SMarcel Moolenaar break; 770daab0b01SMarcel Moolenaar } 771daab0b01SMarcel Moolenaar 77258f0484fSRodney W. Grimes /* Initial DOT must be matched literally. */ 773e04d8562SAndrey A. Chernov if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) { 774e04d8562SAndrey A. Chernov errno = 0; 77558f0484fSRodney W. Grimes continue; 776e04d8562SAndrey A. Chernov } 777e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 778487dbd92SPeter Wemm dc = pathend; 7791cec70adSXin LI sc = dp->d_name; 780aed721ecSAndrey A. Chernov too_long = 1; 781aed721ecSAndrey A. Chernov while (dc <= pathend_last) { 782e9346e01STim J. Robbins clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs); 783e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) { 784aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 785aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc; 786e9346e01STim J. Robbins clen = 1; 787e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 788e9346e01STim J. Robbins } 789aed721ecSAndrey A. Chernov if ((*dc++ = wc) == EOS) { 790aed721ecSAndrey A. Chernov too_long = 0; 791e9346e01STim J. Robbins break; 792aed721ecSAndrey A. Chernov } 793e9346e01STim J. Robbins sc += clen; 794e9346e01STim J. Robbins } 795*20e37fa8SAndrey A. Chernov if (too_long && (err = err_aborted(pglob, ENAMETOOLONG, 796*20e37fa8SAndrey A. Chernov buf))) { 79715cb7866SAndrey A. Chernov errno = ENAMETOOLONG; 79815cb7866SAndrey A. Chernov break; 79915cb7866SAndrey A. Chernov } 800aed721ecSAndrey A. Chernov if (too_long || !match(pathend, pattern, restpattern)) { 80158f0484fSRodney W. Grimes *pathend = EOS; 802e04d8562SAndrey A. Chernov errno = 0; 80358f0484fSRodney W. Grimes continue; 80458f0484fSRodney W. Grimes } 805000b8f83SAndrey A. Chernov if (errno == 0) 806000b8f83SAndrey A. Chernov errno = saverrno; 807487dbd92SPeter Wemm err = glob2(pathbuf, --dc, pathend_last, restpattern, 808487dbd92SPeter Wemm pglob, limit); 80958f0484fSRodney W. Grimes if (err) 81058f0484fSRodney W. Grimes break; 811aed721ecSAndrey A. Chernov errno = 0; 81258f0484fSRodney W. Grimes } 81358f0484fSRodney W. Grimes 814869eb80cSAndrey A. Chernov saverrno2 = errno; 81558f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 81658f0484fSRodney W. Grimes (*pglob->gl_closedir)(dirp); 81758f0484fSRodney W. Grimes else 81858f0484fSRodney W. Grimes closedir(dirp); 819869eb80cSAndrey A. Chernov errno = saverrno2; 820aed721ecSAndrey A. Chernov 821aed721ecSAndrey A. Chernov if (err) 82258f0484fSRodney W. Grimes return (err); 823aed721ecSAndrey A. Chernov 824*20e37fa8SAndrey A. Chernov if (dp == NULL && errno != 0 && 825*20e37fa8SAndrey A. Chernov (err = err_aborted(pglob, errno, buf))) 826*20e37fa8SAndrey A. Chernov return (err); 827aed721ecSAndrey A. Chernov 828869eb80cSAndrey A. Chernov if (errno == 0) 829869eb80cSAndrey A. Chernov errno = saverrno; 830aed721ecSAndrey A. Chernov return (0); 83158f0484fSRodney W. Grimes } 83258f0484fSRodney W. Grimes 83358f0484fSRodney W. Grimes 83458f0484fSRodney W. Grimes /* 835b4c19408SEd Maste * Extend the gl_pathv member of a glob_t structure to accommodate a new item, 83658f0484fSRodney W. Grimes * add the new item, and update gl_pathc. 83758f0484fSRodney W. Grimes * 83858f0484fSRodney W. Grimes * This assumes the BSD realloc, which only copies the block when its size 83958f0484fSRodney W. Grimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 84058f0484fSRodney W. Grimes * behavior. 84158f0484fSRodney W. Grimes * 84258f0484fSRodney W. Grimes * Return 0 if new item added, error code if memory couldn't be allocated. 84358f0484fSRodney W. Grimes * 84458f0484fSRodney W. Grimes * Invariant of the glob_t structure: 84558f0484fSRodney W. Grimes * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 84658f0484fSRodney W. Grimes * gl_pathv points to (gl_offs + gl_pathc + 1) items. 84758f0484fSRodney W. Grimes */ 84858f0484fSRodney W. Grimes static int 849aed721ecSAndrey A. Chernov globextend(const Char *path, glob_t *pglob, struct glob_limit *limit, 85009264d74SAndrey A. Chernov const char *origpat) 85158f0484fSRodney W. Grimes { 852b231cb39SDavid E. O'Brien char **pathv; 8534b767fa6SAndrey A. Chernov size_t i, newsize, len; 85458f0484fSRodney W. Grimes char *copy; 85558f0484fSRodney W. Grimes const Char *p; 85658f0484fSRodney W. Grimes 857daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) && 858daab0b01SMarcel Moolenaar pglob->gl_matchc > limit->l_path_lim) { 859869eb80cSAndrey A. Chernov errno = E2BIG; 86075dc5f1aSMike Heffner return (GLOB_NOSPACE); 86175dc5f1aSMike Heffner } 862813c96dbSJonathan Lemon 86358f0484fSRodney W. Grimes newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 86443cc14e0SMarcel Moolenaar /* realloc(NULL, newsize) is equivalent to malloc(newsize). */ 86543cc14e0SMarcel Moolenaar pathv = realloc((void *)pglob->gl_pathv, newsize); 866b628fac5SMarcel Moolenaar if (pathv == NULL) 86758f0484fSRodney W. Grimes return (GLOB_NOSPACE); 86858f0484fSRodney W. Grimes 86958f0484fSRodney W. Grimes if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 87058f0484fSRodney W. Grimes /* first time around -- clear initial gl_offs items */ 87158f0484fSRodney W. Grimes pathv += pglob->gl_offs; 8724b767fa6SAndrey A. Chernov for (i = pglob->gl_offs + 1; --i > 0; ) 87358f0484fSRodney W. Grimes *--pathv = NULL; 87458f0484fSRodney W. Grimes } 87558f0484fSRodney W. Grimes pglob->gl_pathv = pathv; 87658f0484fSRodney W. Grimes 87709264d74SAndrey A. Chernov if (origpat != NULL) 87809264d74SAndrey A. Chernov copy = strdup(origpat); 87909264d74SAndrey A. Chernov else { 880aed721ecSAndrey A. Chernov for (p = path; *p++ != EOS;) 88158f0484fSRodney W. Grimes continue; 882e9346e01STim J. Robbins len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */ 883bd7a9850SAndrey A. Chernov if ((copy = malloc(len)) != NULL) { 88409264d74SAndrey A. Chernov if (g_Ctoc(path, copy, len)) { 885bd7a9850SAndrey A. Chernov free(copy); 886869eb80cSAndrey A. Chernov errno = E2BIG; 887daab0b01SMarcel Moolenaar return (GLOB_NOSPACE); 888daab0b01SMarcel Moolenaar } 88909264d74SAndrey A. Chernov } 89009264d74SAndrey A. Chernov } 89109264d74SAndrey A. Chernov if (copy != NULL) { 892bd7a9850SAndrey A. Chernov limit->l_string_cnt += strlen(copy) + 1; 893bd7a9850SAndrey A. Chernov if ((pglob->gl_flags & GLOB_LIMIT) && 894bd7a9850SAndrey A. Chernov limit->l_string_cnt >= GLOB_LIMIT_STRING) { 89576e2bc01SPeter Wemm free(copy); 896869eb80cSAndrey A. Chernov errno = E2BIG; 89776e2bc01SPeter Wemm return (GLOB_NOSPACE); 89876e2bc01SPeter Wemm } 89958f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 90058f0484fSRodney W. Grimes } 90158f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 90258f0484fSRodney W. Grimes return (copy == NULL ? GLOB_NOSPACE : 0); 90358f0484fSRodney W. Grimes } 90458f0484fSRodney W. Grimes 90558f0484fSRodney W. Grimes /* 90658f0484fSRodney W. Grimes * pattern matching function for filenames. Each occurrence of the * 90758f0484fSRodney W. Grimes * pattern causes a recursion level. 90858f0484fSRodney W. Grimes */ 90958f0484fSRodney W. Grimes static int 9101cec70adSXin LI match(Char *name, Char *pat, Char *patend) 91158f0484fSRodney W. Grimes { 91258f0484fSRodney W. Grimes int ok, negate_range; 91358f0484fSRodney W. Grimes Char c, k; 9141daad8f5SAndrey A. Chernov struct xlocale_collate *table = 9151daad8f5SAndrey A. Chernov (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 91658f0484fSRodney W. Grimes 91758f0484fSRodney W. Grimes while (pat < patend) { 91858f0484fSRodney W. Grimes c = *pat++; 91958f0484fSRodney W. Grimes switch (c & M_MASK) { 92058f0484fSRodney W. Grimes case M_ALL: 92158f0484fSRodney W. Grimes if (pat == patend) 92258f0484fSRodney W. Grimes return (1); 92358f0484fSRodney W. Grimes do 92458f0484fSRodney W. Grimes if (match(name, pat, patend)) 92558f0484fSRodney W. Grimes return (1); 92658f0484fSRodney W. Grimes while (*name++ != EOS); 92758f0484fSRodney W. Grimes return (0); 92858f0484fSRodney W. Grimes case M_ONE: 92958f0484fSRodney W. Grimes if (*name++ == EOS) 93058f0484fSRodney W. Grimes return (0); 93158f0484fSRodney W. Grimes break; 93258f0484fSRodney W. Grimes case M_SET: 93358f0484fSRodney W. Grimes ok = 0; 93458f0484fSRodney W. Grimes if ((k = *name++) == EOS) 93558f0484fSRodney W. Grimes return (0); 936eef722c3SAndrey A. Chernov if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0) 93758f0484fSRodney W. Grimes ++pat; 93858f0484fSRodney W. Grimes while (((c = *pat++) & M_MASK) != M_END) 93958f0484fSRodney W. Grimes if ((*pat & M_MASK) == M_RNG) { 9401daad8f5SAndrey A. Chernov if (table->__collate_load_error ? 941aa3d69a6SAndrey A. Chernov CHAR(c) <= CHAR(k) && 942aa3d69a6SAndrey A. Chernov CHAR(k) <= CHAR(pat[1]) : 943aa3d69a6SAndrey A. Chernov __wcollate_range_cmp(CHAR(c), 944aa3d69a6SAndrey A. Chernov CHAR(k)) <= 0 && 945aa3d69a6SAndrey A. Chernov __wcollate_range_cmp(CHAR(k), 946aa3d69a6SAndrey A. Chernov CHAR(pat[1])) <= 0) 94758f0484fSRodney W. Grimes ok = 1; 94858f0484fSRodney W. Grimes pat += 2; 94958f0484fSRodney W. Grimes } else if (c == k) 95058f0484fSRodney W. Grimes ok = 1; 95158f0484fSRodney W. Grimes if (ok == negate_range) 95258f0484fSRodney W. Grimes return (0); 95358f0484fSRodney W. Grimes break; 95458f0484fSRodney W. Grimes default: 95558f0484fSRodney W. Grimes if (*name++ != c) 95658f0484fSRodney W. Grimes return (0); 95758f0484fSRodney W. Grimes break; 95858f0484fSRodney W. Grimes } 95958f0484fSRodney W. Grimes } 96058f0484fSRodney W. Grimes return (*name == EOS); 96158f0484fSRodney W. Grimes } 96258f0484fSRodney W. Grimes 96358f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */ 96458f0484fSRodney W. Grimes void 9651cec70adSXin LI globfree(glob_t *pglob) 96658f0484fSRodney W. Grimes { 9674b767fa6SAndrey A. Chernov size_t i; 968b231cb39SDavid E. O'Brien char **pp; 96958f0484fSRodney W. Grimes 97058f0484fSRodney W. Grimes if (pglob->gl_pathv != NULL) { 97158f0484fSRodney W. Grimes pp = pglob->gl_pathv + pglob->gl_offs; 97258f0484fSRodney W. Grimes for (i = pglob->gl_pathc; i--; ++pp) 97358f0484fSRodney W. Grimes if (*pp) 97458f0484fSRodney W. Grimes free(*pp); 97558f0484fSRodney W. Grimes free(pglob->gl_pathv); 97676e2bc01SPeter Wemm pglob->gl_pathv = NULL; 97758f0484fSRodney W. Grimes } 97858f0484fSRodney W. Grimes } 97958f0484fSRodney W. Grimes 98058f0484fSRodney W. Grimes static DIR * 9811cec70adSXin LI g_opendir(Char *str, glob_t *pglob) 98258f0484fSRodney W. Grimes { 983e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1]; 98458f0484fSRodney W. Grimes 985aa3d69a6SAndrey A. Chernov if (*str == EOS) 98658f0484fSRodney W. Grimes strcpy(buf, "."); 98776e2bc01SPeter Wemm else { 98809264d74SAndrey A. Chernov if (g_Ctoc(str, buf, sizeof(buf))) { 989196d61a9SAndrey A. Chernov errno = ENAMETOOLONG; 99076e2bc01SPeter Wemm return (NULL); 99176e2bc01SPeter Wemm } 992196d61a9SAndrey A. Chernov } 99358f0484fSRodney W. Grimes 99458f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 99558f0484fSRodney W. Grimes return ((*pglob->gl_opendir)(buf)); 99658f0484fSRodney W. Grimes 99758f0484fSRodney W. Grimes return (opendir(buf)); 99858f0484fSRodney W. Grimes } 99958f0484fSRodney W. Grimes 100058f0484fSRodney W. Grimes static int 10011cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob) 100258f0484fSRodney W. Grimes { 1003e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1]; 100458f0484fSRodney W. Grimes 100509264d74SAndrey A. Chernov if (g_Ctoc(fn, buf, sizeof(buf))) { 100676e2bc01SPeter Wemm errno = ENAMETOOLONG; 100776e2bc01SPeter Wemm return (-1); 100876e2bc01SPeter Wemm } 100958f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 101058f0484fSRodney W. Grimes return((*pglob->gl_lstat)(buf, sb)); 101158f0484fSRodney W. Grimes return (lstat(buf, sb)); 101258f0484fSRodney W. Grimes } 101358f0484fSRodney W. Grimes 101458f0484fSRodney W. Grimes static int 10151cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob) 101658f0484fSRodney W. Grimes { 1017e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1]; 101858f0484fSRodney W. Grimes 101909264d74SAndrey A. Chernov if (g_Ctoc(fn, buf, sizeof(buf))) { 102076e2bc01SPeter Wemm errno = ENAMETOOLONG; 102176e2bc01SPeter Wemm return (-1); 102276e2bc01SPeter Wemm } 102358f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 102458f0484fSRodney W. Grimes return ((*pglob->gl_stat)(buf, sb)); 102558f0484fSRodney W. Grimes return (stat(buf, sb)); 102658f0484fSRodney W. Grimes } 102758f0484fSRodney W. Grimes 102834a08754SMike Makonnen static const Char * 102934a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch) 103058f0484fSRodney W. Grimes { 10311cec70adSXin LI 103258f0484fSRodney W. Grimes do { 103358f0484fSRodney W. Grimes if (*str == ch) 103458f0484fSRodney W. Grimes return (str); 103558f0484fSRodney W. Grimes } while (*str++); 103658f0484fSRodney W. Grimes return (NULL); 103758f0484fSRodney W. Grimes } 103858f0484fSRodney W. Grimes 103976e2bc01SPeter Wemm static int 104009264d74SAndrey A. Chernov g_Ctoc(const Char *str, char *buf, size_t len) 104158f0484fSRodney W. Grimes { 1042e9346e01STim J. Robbins mbstate_t mbs; 1043e9346e01STim J. Robbins size_t clen; 104458f0484fSRodney W. Grimes 1045e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1046e9346e01STim J. Robbins while (len >= MB_CUR_MAX) { 104709264d74SAndrey A. Chernov clen = wcrtomb(buf, CHAR(*str), &mbs); 1048aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1) { 1049aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */ 105009264d74SAndrey A. Chernov *buf = (char)CHAR(*str); 1051aa3d69a6SAndrey A. Chernov clen = 1; 1052aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs)); 1053aa3d69a6SAndrey A. Chernov } 105409264d74SAndrey A. Chernov if (CHAR(*str) == EOS) 105576e2bc01SPeter Wemm return (0); 105609264d74SAndrey A. Chernov str++; 1057e9346e01STim J. Robbins buf += clen; 1058e9346e01STim J. Robbins len -= clen; 105958f0484fSRodney W. Grimes } 106027d52f69SPeter Wemm return (1); 106127d52f69SPeter Wemm } 106258f0484fSRodney W. Grimes 1063*20e37fa8SAndrey A. Chernov static int 1064*20e37fa8SAndrey A. Chernov err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) { 1065*20e37fa8SAndrey A. Chernov /* 1066*20e37fa8SAndrey A. Chernov * If there was no match we are going to append the origpat 1067*20e37fa8SAndrey A. Chernov * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 1068*20e37fa8SAndrey A. Chernov * and the origpat did not contain any magic characters 1069*20e37fa8SAndrey A. Chernov * GLOB_NOMAGIC is there just for compatibility with csh. 1070*20e37fa8SAndrey A. Chernov */ 1071*20e37fa8SAndrey A. Chernov if ((pglob->gl_flags & GLOB_NOCHECK) || 1072*20e37fa8SAndrey A. Chernov ((pglob->gl_flags & GLOB_NOMAGIC) && 1073*20e37fa8SAndrey A. Chernov !(pglob->gl_flags & GLOB_MAGCHAR))) 1074*20e37fa8SAndrey A. Chernov return (globextend(NULL, pglob, limit, origpat)); 1075*20e37fa8SAndrey A. Chernov return (GLOB_NOMATCH); 1076*20e37fa8SAndrey A. Chernov } 1077*20e37fa8SAndrey A. Chernov 1078*20e37fa8SAndrey A. Chernov static int 1079*20e37fa8SAndrey A. Chernov err_aborted(glob_t *pglob, int err, char *buf) { 1080*20e37fa8SAndrey A. Chernov if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) || 1081*20e37fa8SAndrey A. Chernov (pglob->gl_flags & GLOB_ERR)) 1082*20e37fa8SAndrey A. Chernov return (GLOB_ABORTED); 1083*20e37fa8SAndrey A. Chernov return (0); 1084*20e37fa8SAndrey A. Chernov } 1085*20e37fa8SAndrey A. Chernov 108658f0484fSRodney W. Grimes #ifdef DEBUG 108758f0484fSRodney W. Grimes static void 10881cec70adSXin LI qprintf(const char *str, Char *s) 108958f0484fSRodney W. Grimes { 1090b231cb39SDavid E. O'Brien Char *p; 109158f0484fSRodney W. Grimes 1092bd7a9850SAndrey A. Chernov (void)printf("%s\n", str); 1093bd7a9850SAndrey A. Chernov if (s != NULL) { 1094bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++) 1095bd7a9850SAndrey A. Chernov (void)printf("%c", (char)CHAR(*p)); 109658f0484fSRodney W. Grimes (void)printf("\n"); 1097bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++) 1098bd7a9850SAndrey A. Chernov (void)printf("%c", (isprot(*p) ? '\\' : ' ')); 109958f0484fSRodney W. Grimes (void)printf("\n"); 1100bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++) 1101bd7a9850SAndrey A. Chernov (void)printf("%c", (ismeta(*p) ? '_' : ' ')); 110258f0484fSRodney W. Grimes (void)printf("\n"); 110358f0484fSRodney W. Grimes } 1104bd7a9850SAndrey A. Chernov } 110558f0484fSRodney W. Grimes #endif 1106