18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1989, 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes * Guido van Rossum.
958f0484fSRodney W. Grimes *
103c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation
115b5fa75aSEd Maste *
123c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall
133c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation.
143c87aa1dSDavid Chisnall *
1558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1758f0484fSRodney W. Grimes * are met:
1858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
2058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
2158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
2258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
23fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2558f0484fSRodney W. Grimes * without specific prior written permission.
2658f0484fSRodney W. Grimes *
2758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3758f0484fSRodney W. Grimes * SUCH DAMAGE.
3858f0484fSRodney W. Grimes */
3958f0484fSRodney W. Grimes
4058f0484fSRodney W. Grimes /*
4158f0484fSRodney W. Grimes * glob(3) -- a superset of the one defined in POSIX 1003.2.
4258f0484fSRodney W. Grimes *
4358f0484fSRodney W. Grimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
4458f0484fSRodney W. Grimes *
4558f0484fSRodney W. Grimes * Optional extra services, controlled by flags not defined by POSIX:
4658f0484fSRodney W. Grimes *
4758f0484fSRodney W. Grimes * GLOB_QUOTE:
4858f0484fSRodney W. Grimes * Escaping convention: \ inhibits any special meaning the following
4958f0484fSRodney W. Grimes * character might have (except \ at end of string is retained).
5058f0484fSRodney W. Grimes * GLOB_MAGCHAR:
5158f0484fSRodney W. Grimes * Set in gl_flags if pattern contained a globbing character.
5258f0484fSRodney W. Grimes * GLOB_NOMAGIC:
5358f0484fSRodney W. Grimes * Same as GLOB_NOCHECK, but it will only append pattern if it did
5458f0484fSRodney W. Grimes * not contain any magic characters. [Used in csh style globbing]
5558f0484fSRodney W. Grimes * GLOB_ALTDIRFUNC:
5658f0484fSRodney W. Grimes * Use alternately specified directory access functions.
5758f0484fSRodney W. Grimes * GLOB_TILDE:
5858f0484fSRodney W. Grimes * expand ~user/foo to the /home/dir/of/user/foo
5958f0484fSRodney W. Grimes * GLOB_BRACE:
6058f0484fSRodney W. Grimes * expand {1,2}{a,b} to 1a 1b 2a 2b
6158f0484fSRodney W. Grimes * gl_matchc:
6258f0484fSRodney W. Grimes * Number of matches in the current invocation of glob.
6358f0484fSRodney W. Grimes */
6458f0484fSRodney W. Grimes
65e9346e01STim J. Robbins /*
66e9346e01STim J. Robbins * Some notes on multibyte character support:
67e9346e01STim J. Robbins * 1. Patterns with illegal byte sequences match nothing - even if
68e9346e01STim J. Robbins * GLOB_NOCHECK is specified.
69e9346e01STim J. Robbins * 2. Illegal byte sequences in filenames are handled by treating them as
70aa3d69a6SAndrey A. Chernov * single-byte characters with a values of such bytes of the sequence
71e9346e01STim J. Robbins * cast to wchar_t.
72e9346e01STim J. Robbins * 3. State-dependent encodings are not currently supported.
73e9346e01STim J. Robbins */
74e9346e01STim J. Robbins
7558f0484fSRodney W. Grimes #include <sys/param.h>
7658f0484fSRodney W. Grimes #include <sys/stat.h>
7758f0484fSRodney W. Grimes
7858f0484fSRodney W. Grimes #include <ctype.h>
7958f0484fSRodney W. Grimes #include <dirent.h>
8058f0484fSRodney W. Grimes #include <errno.h>
8158f0484fSRodney W. Grimes #include <glob.h>
82e9346e01STim J. Robbins #include <limits.h>
8358f0484fSRodney W. Grimes #include <pwd.h>
84e9346e01STim J. Robbins #include <stdint.h>
8558f0484fSRodney W. Grimes #include <stdio.h>
8658f0484fSRodney W. Grimes #include <stdlib.h>
8758f0484fSRodney W. Grimes #include <string.h>
8858f0484fSRodney W. Grimes #include <unistd.h>
89e9346e01STim J. Robbins #include <wchar.h>
9058f0484fSRodney W. Grimes
911daad8f5SAndrey A. Chernov #include "collate.h"
921daad8f5SAndrey A. Chernov
93daab0b01SMarcel Moolenaar /*
94daab0b01SMarcel Moolenaar * glob(3) expansion limits. Stop the expansion if any of these limits
95daab0b01SMarcel Moolenaar * is reached. This caps the runtime in the face of DoS attacks. See
96daab0b01SMarcel Moolenaar * also CVE-2010-2632
97daab0b01SMarcel Moolenaar */
98daab0b01SMarcel Moolenaar #define GLOB_LIMIT_BRACE 128 /* number of brace calls */
99daab0b01SMarcel Moolenaar #define GLOB_LIMIT_PATH 65536 /* number of path elements */
100daab0b01SMarcel Moolenaar #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */
101daab0b01SMarcel Moolenaar #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */
102daab0b01SMarcel Moolenaar #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */
103daab0b01SMarcel Moolenaar
104daab0b01SMarcel Moolenaar struct glob_limit {
105daab0b01SMarcel Moolenaar size_t l_brace_cnt;
106daab0b01SMarcel Moolenaar size_t l_path_lim;
107daab0b01SMarcel Moolenaar size_t l_readdir_cnt;
108daab0b01SMarcel Moolenaar size_t l_stat_cnt;
109daab0b01SMarcel Moolenaar size_t l_string_cnt;
110daab0b01SMarcel Moolenaar };
111daab0b01SMarcel Moolenaar
112aa3d69a6SAndrey A. Chernov #define DOT L'.'
113aa3d69a6SAndrey A. Chernov #define EOS L'\0'
114aa3d69a6SAndrey A. Chernov #define LBRACKET L'['
115aa3d69a6SAndrey A. Chernov #define NOT L'!'
116aa3d69a6SAndrey A. Chernov #define QUESTION L'?'
117aa3d69a6SAndrey A. Chernov #define QUOTE L'\\'
118aa3d69a6SAndrey A. Chernov #define RANGE L'-'
119aa3d69a6SAndrey A. Chernov #define RBRACKET L']'
120aa3d69a6SAndrey A. Chernov #define SEP L'/'
121aa3d69a6SAndrey A. Chernov #define STAR L'*'
122aa3d69a6SAndrey A. Chernov #define TILDE L'~'
123aa3d69a6SAndrey A. Chernov #define LBRACE L'{'
124aa3d69a6SAndrey A. Chernov #define RBRACE L'}'
125aa3d69a6SAndrey A. Chernov #define COMMA L','
12658f0484fSRodney W. Grimes
127e9346e01STim J. Robbins #define M_QUOTE 0x8000000000ULL
128e9346e01STim J. Robbins #define M_PROTECT 0x4000000000ULL
129e9346e01STim J. Robbins #define M_MASK 0xffffffffffULL
130e9346e01STim J. Robbins #define M_CHAR 0x00ffffffffULL
13158f0484fSRodney W. Grimes
132e9346e01STim J. Robbins typedef uint_fast64_t Char;
13358f0484fSRodney W. Grimes
134e9346e01STim J. Robbins #define CHAR(c) ((Char)((c)&M_CHAR))
13558f0484fSRodney W. Grimes #define META(c) ((Char)((c)|M_QUOTE))
136aed721ecSAndrey A. Chernov #define UNPROT(c) ((c) & ~M_PROTECT)
137aa3d69a6SAndrey A. Chernov #define M_ALL META(L'*')
138aa3d69a6SAndrey A. Chernov #define M_END META(L']')
139aa3d69a6SAndrey A. Chernov #define M_NOT META(L'!')
140aa3d69a6SAndrey A. Chernov #define M_ONE META(L'?')
141aa3d69a6SAndrey A. Chernov #define M_RNG META(L'-')
142aa3d69a6SAndrey A. Chernov #define M_SET META(L'[')
14358f0484fSRodney W. Grimes #define ismeta(c) (((c)&M_QUOTE) != 0)
14409264d74SAndrey A. Chernov #ifdef DEBUG
145aed721ecSAndrey A. Chernov #define isprot(c) (((c)&M_PROTECT) != 0)
14609264d74SAndrey A. Chernov #endif
14758f0484fSRodney W. Grimes
148b231cb39SDavid E. O'Brien static int compare(const void *, const void *);
14909264d74SAndrey A. Chernov static int g_Ctoc(const Char *, char *, size_t);
150b231cb39SDavid E. O'Brien static int g_lstat(Char *, struct stat *, glob_t *);
151b231cb39SDavid E. O'Brien static DIR *g_opendir(Char *, glob_t *);
15234a08754SMike Makonnen static const Char *g_strchr(const Char *, wchar_t);
15358f0484fSRodney W. Grimes #ifdef notdef
154b231cb39SDavid E. O'Brien static Char *g_strcat(Char *, const Char *);
15558f0484fSRodney W. Grimes #endif
156b231cb39SDavid E. O'Brien static int g_stat(Char *, struct stat *, glob_t *);
15709264d74SAndrey A. Chernov static int glob0(const Char *, glob_t *, struct glob_limit *,
15809264d74SAndrey A. Chernov const char *);
159daab0b01SMarcel Moolenaar static int glob1(Char *, glob_t *, struct glob_limit *);
160daab0b01SMarcel Moolenaar static int glob2(Char *, Char *, Char *, Char *, glob_t *,
161daab0b01SMarcel Moolenaar struct glob_limit *);
162daab0b01SMarcel Moolenaar static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
163daab0b01SMarcel Moolenaar struct glob_limit *);
16409264d74SAndrey A. Chernov static int globextend(const Char *, glob_t *, struct glob_limit *,
16509264d74SAndrey A. Chernov const char *);
166487dbd92SPeter Wemm static const Char *
167b231cb39SDavid E. O'Brien globtilde(const Char *, Char *, size_t, glob_t *);
16809264d74SAndrey A. Chernov static int globexp0(const Char *, glob_t *, struct glob_limit *,
16909264d74SAndrey A. Chernov const char *);
170daab0b01SMarcel Moolenaar static int globexp1(const Char *, glob_t *, struct glob_limit *);
171bd7a9850SAndrey A. Chernov static int globexp2(const Char *, const Char *, glob_t *,
172daab0b01SMarcel Moolenaar struct glob_limit *);
17309264d74SAndrey A. Chernov static int globfinal(glob_t *, struct glob_limit *, size_t,
17409264d74SAndrey A. Chernov const char *);
175b231cb39SDavid E. O'Brien static int match(Char *, Char *, Char *);
17620e37fa8SAndrey A. Chernov static int err_nomatch(glob_t *, struct glob_limit *, const char *);
17720e37fa8SAndrey A. Chernov static int err_aborted(glob_t *, int, char *);
17858f0484fSRodney W. Grimes #ifdef DEBUG
179b231cb39SDavid E. O'Brien static void qprintf(const char *, Char *);
18058f0484fSRodney W. Grimes #endif
18158f0484fSRodney W. Grimes
18258f0484fSRodney W. Grimes int
glob(const char * __restrict pattern,int flags,int (* errfunc)(const char *,int),glob_t * __restrict pglob)1830d6d372cSEitan Adler glob(const char * __restrict pattern, int flags,
1840d6d372cSEitan Adler int (*errfunc)(const char *, int), glob_t * __restrict pglob)
18558f0484fSRodney W. Grimes {
186daab0b01SMarcel Moolenaar struct glob_limit limit = { 0, 0, 0, 0, 0 };
1871cec70adSXin LI const char *patnext;
188e9346e01STim J. Robbins Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
189e9346e01STim J. Robbins mbstate_t mbs;
190e9346e01STim J. Robbins wchar_t wc;
191e9346e01STim J. Robbins size_t clen;
192aed721ecSAndrey A. Chernov int too_long;
19358f0484fSRodney W. Grimes
1941cec70adSXin LI patnext = pattern;
19558f0484fSRodney W. Grimes if (!(flags & GLOB_APPEND)) {
19658f0484fSRodney W. Grimes pglob->gl_pathc = 0;
19758f0484fSRodney W. Grimes pglob->gl_pathv = NULL;
19858f0484fSRodney W. Grimes if (!(flags & GLOB_DOOFFS))
19958f0484fSRodney W. Grimes pglob->gl_offs = 0;
20058f0484fSRodney W. Grimes }
20175dc5f1aSMike Heffner if (flags & GLOB_LIMIT) {
202daab0b01SMarcel Moolenaar limit.l_path_lim = pglob->gl_matchc;
203daab0b01SMarcel Moolenaar if (limit.l_path_lim == 0)
204daab0b01SMarcel Moolenaar limit.l_path_lim = GLOB_LIMIT_PATH;
205daab0b01SMarcel Moolenaar }
20658f0484fSRodney W. Grimes pglob->gl_flags = flags & ~GLOB_MAGCHAR;
20758f0484fSRodney W. Grimes pglob->gl_errfunc = errfunc;
20858f0484fSRodney W. Grimes pglob->gl_matchc = 0;
20958f0484fSRodney W. Grimes
21058f0484fSRodney W. Grimes bufnext = patbuf;
211487dbd92SPeter Wemm bufend = bufnext + MAXPATHLEN - 1;
212aed721ecSAndrey A. Chernov too_long = 1;
213e9346e01STim J. Robbins if (flags & GLOB_NOESCAPE) {
214e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs));
215aed721ecSAndrey A. Chernov while (bufnext <= bufend) {
216e9346e01STim J. Robbins clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
217e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2)
21820e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern));
219aed721ecSAndrey A. Chernov else if (clen == 0) {
220aed721ecSAndrey A. Chernov too_long = 0;
221e9346e01STim J. Robbins break;
222aed721ecSAndrey A. Chernov }
223e9346e01STim J. Robbins *bufnext++ = wc;
224e9346e01STim J. Robbins patnext += clen;
225e9346e01STim J. Robbins }
226e9346e01STim J. Robbins } else {
22758f0484fSRodney W. Grimes /* Protect the quoted characters. */
228e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs));
229aed721ecSAndrey A. Chernov while (bufnext <= bufend) {
230aa3d69a6SAndrey A. Chernov if (*patnext == '\\') {
231aa3d69a6SAndrey A. Chernov if (*++patnext == '\0') {
232aed721ecSAndrey A. Chernov *bufnext++ = QUOTE;
233e9346e01STim J. Robbins continue;
23458f0484fSRodney W. Grimes }
235e9346e01STim J. Robbins prot = M_PROTECT;
236e9346e01STim J. Robbins } else
237e9346e01STim J. Robbins prot = 0;
238e9346e01STim J. Robbins clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
239e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2)
24020e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern));
241aed721ecSAndrey A. Chernov else if (clen == 0) {
242aed721ecSAndrey A. Chernov too_long = 0;
243e9346e01STim J. Robbins break;
244aed721ecSAndrey A. Chernov }
245aed721ecSAndrey A. Chernov *bufnext++ = wc | prot;
246e9346e01STim J. Robbins patnext += clen;
24758f0484fSRodney W. Grimes }
24858f0484fSRodney W. Grimes }
249aed721ecSAndrey A. Chernov if (too_long)
25020e37fa8SAndrey A. Chernov return (err_nomatch(pglob, &limit, pattern));
25158f0484fSRodney W. Grimes *bufnext = EOS;
25258f0484fSRodney W. Grimes
25358f0484fSRodney W. Grimes if (flags & GLOB_BRACE)
25409264d74SAndrey A. Chernov return (globexp0(patbuf, pglob, &limit, pattern));
25558f0484fSRodney W. Grimes else
25609264d74SAndrey A. Chernov return (glob0(patbuf, pglob, &limit, pattern));
257bd7a9850SAndrey A. Chernov }
258bd7a9850SAndrey A. Chernov
259bd7a9850SAndrey A. Chernov static int
globexp0(const Char * pattern,glob_t * pglob,struct glob_limit * limit,const char * origpat)26009264d74SAndrey A. Chernov globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
26109264d74SAndrey A. Chernov const char *origpat) {
262bd7a9850SAndrey A. Chernov int rv;
263bd7a9850SAndrey A. Chernov size_t oldpathc;
264bd7a9850SAndrey A. Chernov
265bd7a9850SAndrey A. Chernov /* Protect a single {}, for find(1), like csh */
266bd7a9850SAndrey A. Chernov if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) {
267bd7a9850SAndrey A. Chernov if ((pglob->gl_flags & GLOB_LIMIT) &&
268bd7a9850SAndrey A. Chernov limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
269869eb80cSAndrey A. Chernov errno = E2BIG;
270bd7a9850SAndrey A. Chernov return (GLOB_NOSPACE);
271bd7a9850SAndrey A. Chernov }
27209264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, origpat));
273bd7a9850SAndrey A. Chernov }
274bd7a9850SAndrey A. Chernov
275bd7a9850SAndrey A. Chernov oldpathc = pglob->gl_pathc;
276bd7a9850SAndrey A. Chernov
277bd7a9850SAndrey A. Chernov if ((rv = globexp1(pattern, pglob, limit)) != 0)
278bd7a9850SAndrey A. Chernov return rv;
27909264d74SAndrey A. Chernov
28009264d74SAndrey A. Chernov return (globfinal(pglob, limit, oldpathc, origpat));
28158f0484fSRodney W. Grimes }
28258f0484fSRodney W. Grimes
28358f0484fSRodney W. Grimes /*
28458f0484fSRodney W. Grimes * Expand recursively a glob {} pattern. When there is no more expansion
28558f0484fSRodney W. Grimes * invoke the standard globbing routine to glob the rest of the magic
28658f0484fSRodney W. Grimes * characters
28758f0484fSRodney W. Grimes */
288487dbd92SPeter Wemm static int
globexp1(const Char * pattern,glob_t * pglob,struct glob_limit * limit)289daab0b01SMarcel Moolenaar globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
29058f0484fSRodney W. Grimes {
291bd7a9850SAndrey A. Chernov const Char* ptr;
29258f0484fSRodney W. Grimes
293bd7a9850SAndrey A. Chernov if ((ptr = g_strchr(pattern, LBRACE)) != NULL) {
294daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) &&
295daab0b01SMarcel Moolenaar limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
296869eb80cSAndrey A. Chernov errno = E2BIG;
297daab0b01SMarcel Moolenaar return (GLOB_NOSPACE);
298daab0b01SMarcel Moolenaar }
299bd7a9850SAndrey A. Chernov return (globexp2(ptr, pattern, pglob, limit));
300bd7a9850SAndrey A. Chernov }
301daab0b01SMarcel Moolenaar
30209264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, NULL));
30358f0484fSRodney W. Grimes }
30458f0484fSRodney W. Grimes
30558f0484fSRodney W. Grimes
30658f0484fSRodney W. Grimes /*
30758f0484fSRodney W. Grimes * Recursive brace globbing helper. Tries to expand a single brace.
30858f0484fSRodney W. Grimes * If it succeeds then it invokes globexp1 with the new pattern.
30958f0484fSRodney W. Grimes * If it fails then it tries to glob the rest of the pattern and returns.
31058f0484fSRodney W. Grimes */
311487dbd92SPeter Wemm static int
globexp2(const Char * ptr,const Char * pattern,glob_t * pglob,struct glob_limit * limit)312bd7a9850SAndrey A. Chernov globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
313daab0b01SMarcel Moolenaar struct glob_limit *limit)
31458f0484fSRodney W. Grimes {
315bd7a9850SAndrey A. Chernov int i, rv;
31658f0484fSRodney W. Grimes Char *lm, *ls;
317369316a8SAndrey A. Chernov const Char *pe, *pm, *pm1, *pl;
318487dbd92SPeter Wemm Char patbuf[MAXPATHLEN];
31958f0484fSRodney W. Grimes
32058f0484fSRodney W. Grimes /* copy part up to the brace */
32158f0484fSRodney W. Grimes for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
32258f0484fSRodney W. Grimes continue;
323487dbd92SPeter Wemm *lm = EOS;
32458f0484fSRodney W. Grimes ls = lm;
32558f0484fSRodney W. Grimes
32658f0484fSRodney W. Grimes /* Find the balanced brace */
327bd7a9850SAndrey A. Chernov for (i = 0, pe = ++ptr; *pe != EOS; pe++)
32858f0484fSRodney W. Grimes if (*pe == LBRACKET) {
32958f0484fSRodney W. Grimes /* Ignore everything between [] */
33058f0484fSRodney W. Grimes for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
33158f0484fSRodney W. Grimes continue;
33258f0484fSRodney W. Grimes if (*pe == EOS) {
33358f0484fSRodney W. Grimes /*
33458f0484fSRodney W. Grimes * We could not find a matching RBRACKET.
33558f0484fSRodney W. Grimes * Ignore and just look for RBRACE
33658f0484fSRodney W. Grimes */
33758f0484fSRodney W. Grimes pe = pm;
33858f0484fSRodney W. Grimes }
33958f0484fSRodney W. Grimes }
34058f0484fSRodney W. Grimes else if (*pe == LBRACE)
34158f0484fSRodney W. Grimes i++;
34258f0484fSRodney W. Grimes else if (*pe == RBRACE) {
34358f0484fSRodney W. Grimes if (i == 0)
34458f0484fSRodney W. Grimes break;
34558f0484fSRodney W. Grimes i--;
34658f0484fSRodney W. Grimes }
34758f0484fSRodney W. Grimes
34858f0484fSRodney W. Grimes /* Non matching braces; just glob the pattern */
349bd7a9850SAndrey A. Chernov if (i != 0 || *pe == EOS)
35009264d74SAndrey A. Chernov return (glob0(pattern, pglob, limit, NULL));
35158f0484fSRodney W. Grimes
35258f0484fSRodney W. Grimes for (i = 0, pl = pm = ptr; pm <= pe; pm++)
35358f0484fSRodney W. Grimes switch (*pm) {
35458f0484fSRodney W. Grimes case LBRACKET:
35558f0484fSRodney W. Grimes /* Ignore everything between [] */
356369316a8SAndrey A. Chernov for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
35758f0484fSRodney W. Grimes continue;
35858f0484fSRodney W. Grimes if (*pm == EOS) {
35958f0484fSRodney W. Grimes /*
36058f0484fSRodney W. Grimes * We could not find a matching RBRACKET.
36158f0484fSRodney W. Grimes * Ignore and just look for RBRACE
36258f0484fSRodney W. Grimes */
363369316a8SAndrey A. Chernov pm = pm1;
36458f0484fSRodney W. Grimes }
36558f0484fSRodney W. Grimes break;
36658f0484fSRodney W. Grimes
36758f0484fSRodney W. Grimes case LBRACE:
36858f0484fSRodney W. Grimes i++;
36958f0484fSRodney W. Grimes break;
37058f0484fSRodney W. Grimes
37158f0484fSRodney W. Grimes case RBRACE:
37258f0484fSRodney W. Grimes if (i) {
37358f0484fSRodney W. Grimes i--;
37458f0484fSRodney W. Grimes break;
37558f0484fSRodney W. Grimes }
37658f0484fSRodney W. Grimes /* FALLTHROUGH */
37758f0484fSRodney W. Grimes case COMMA:
37858f0484fSRodney W. Grimes if (i && *pm == COMMA)
37958f0484fSRodney W. Grimes break;
38058f0484fSRodney W. Grimes else {
38158f0484fSRodney W. Grimes /* Append the current string */
38258f0484fSRodney W. Grimes for (lm = ls; (pl < pm); *lm++ = *pl++)
38358f0484fSRodney W. Grimes continue;
38458f0484fSRodney W. Grimes /*
38558f0484fSRodney W. Grimes * Append the rest of the pattern after the
38658f0484fSRodney W. Grimes * closing brace
38758f0484fSRodney W. Grimes */
38858f0484fSRodney W. Grimes for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
38958f0484fSRodney W. Grimes continue;
39058f0484fSRodney W. Grimes
39158f0484fSRodney W. Grimes /* Expand the current pattern */
39258f0484fSRodney W. Grimes #ifdef DEBUG
39358f0484fSRodney W. Grimes qprintf("globexp2:", patbuf);
39458f0484fSRodney W. Grimes #endif
395bd7a9850SAndrey A. Chernov rv = globexp1(patbuf, pglob, limit);
396bd7a9850SAndrey A. Chernov if (rv)
397bd7a9850SAndrey A. Chernov return (rv);
39858f0484fSRodney W. Grimes
39958f0484fSRodney W. Grimes /* move after the comma, to the next string */
40058f0484fSRodney W. Grimes pl = pm + 1;
40158f0484fSRodney W. Grimes }
40258f0484fSRodney W. Grimes break;
40358f0484fSRodney W. Grimes
40458f0484fSRodney W. Grimes default:
40558f0484fSRodney W. Grimes break;
40658f0484fSRodney W. Grimes }
40785529174SEitan Adler return (0);
40858f0484fSRodney W. Grimes }
40958f0484fSRodney W. Grimes
41058f0484fSRodney W. Grimes
41158f0484fSRodney W. Grimes
41258f0484fSRodney W. Grimes /*
41358f0484fSRodney W. Grimes * expand tilde from the passwd file.
41458f0484fSRodney W. Grimes */
41558f0484fSRodney W. Grimes static const Char *
globtilde(const Char * pattern,Char * patbuf,size_t patbuf_len,glob_t * pglob)4161cec70adSXin LI globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
41758f0484fSRodney W. Grimes {
41858f0484fSRodney W. Grimes struct passwd *pwd;
419aa3d69a6SAndrey A. Chernov char *h, *sc;
42058f0484fSRodney W. Grimes const Char *p;
42162f187a4SWarner Losh Char *b, *eb;
422aa3d69a6SAndrey A. Chernov wchar_t wc;
423aa3d69a6SAndrey A. Chernov wchar_t wbuf[MAXPATHLEN];
424aa3d69a6SAndrey A. Chernov wchar_t *wbufend, *dc;
425aa3d69a6SAndrey A. Chernov size_t clen;
426aa3d69a6SAndrey A. Chernov mbstate_t mbs;
427aa3d69a6SAndrey A. Chernov int too_long;
42858f0484fSRodney W. Grimes
42958f0484fSRodney W. Grimes if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
43085529174SEitan Adler return (pattern);
43158f0484fSRodney W. Grimes
43262f187a4SWarner Losh /*
43362f187a4SWarner Losh * Copy up to the end of the string or /
43462f187a4SWarner Losh */
43562f187a4SWarner Losh eb = &patbuf[patbuf_len - 1];
436aa3d69a6SAndrey A. Chernov for (p = pattern + 1, b = patbuf;
4377455a07aSAndrey A. Chernov b < eb && *p != EOS && UNPROT(*p) != SEP; *b++ = *p++)
43858f0484fSRodney W. Grimes continue;
43958f0484fSRodney W. Grimes
4407455a07aSAndrey A. Chernov if (*p != EOS && UNPROT(*p) != SEP)
441f4d4982eSAndrey A. Chernov return (NULL);
44258f0484fSRodney W. Grimes
443aa3d69a6SAndrey A. Chernov *b = EOS;
444aa3d69a6SAndrey A. Chernov h = NULL;
445aa3d69a6SAndrey A. Chernov
446aa3d69a6SAndrey A. Chernov if (patbuf[0] == EOS) {
44758f0484fSRodney W. Grimes /*
4483fa69daeSWarner Losh * handle a plain ~ or ~/ by expanding $HOME first (iff
4493fa69daeSWarner Losh * we're not running setuid or setgid) and then trying
4503fa69daeSWarner Losh * the password file
45158f0484fSRodney W. Grimes */
452*68ca8363SMark Johnston if ((h = secure_getenv("HOME")) == NULL) {
453eb8eee5aSAndrey A. Chernov if (((h = getlogin()) != NULL &&
454eb8eee5aSAndrey A. Chernov (pwd = getpwnam(h)) != NULL) ||
455eb8eee5aSAndrey A. Chernov (pwd = getpwuid(getuid())) != NULL)
45658f0484fSRodney W. Grimes h = pwd->pw_dir;
457eb8eee5aSAndrey A. Chernov else
45885529174SEitan Adler return (pattern);
45958f0484fSRodney W. Grimes }
46058f0484fSRodney W. Grimes }
46158f0484fSRodney W. Grimes else {
46258f0484fSRodney W. Grimes /*
46358f0484fSRodney W. Grimes * Expand a ~user
46458f0484fSRodney W. Grimes */
46509264d74SAndrey A. Chernov if (g_Ctoc(patbuf, (char *)wbuf, sizeof(wbuf)))
466f4d4982eSAndrey A. Chernov return (NULL);
467f4d4982eSAndrey A. Chernov if ((pwd = getpwnam((char *)wbuf)) == NULL)
46885529174SEitan Adler return (pattern);
46958f0484fSRodney W. Grimes else
47058f0484fSRodney W. Grimes h = pwd->pw_dir;
47158f0484fSRodney W. Grimes }
47258f0484fSRodney W. Grimes
47358f0484fSRodney W. Grimes /* Copy the home directory */
474aa3d69a6SAndrey A. Chernov dc = wbuf;
475aa3d69a6SAndrey A. Chernov sc = h;
476aa3d69a6SAndrey A. Chernov wbufend = wbuf + MAXPATHLEN - 1;
477aa3d69a6SAndrey A. Chernov too_long = 1;
478aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs));
479aa3d69a6SAndrey A. Chernov while (dc <= wbufend) {
480aa3d69a6SAndrey A. Chernov clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
481aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1 || clen == (size_t)-2) {
482aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */
483aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc;
484aa3d69a6SAndrey A. Chernov clen = 1;
485aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs));
486aa3d69a6SAndrey A. Chernov }
487aa3d69a6SAndrey A. Chernov if ((*dc++ = wc) == EOS) {
488aa3d69a6SAndrey A. Chernov too_long = 0;
489aa3d69a6SAndrey A. Chernov break;
490aa3d69a6SAndrey A. Chernov }
491aa3d69a6SAndrey A. Chernov sc += clen;
492aa3d69a6SAndrey A. Chernov }
493aa3d69a6SAndrey A. Chernov if (too_long)
494f4d4982eSAndrey A. Chernov return (NULL);
495aa3d69a6SAndrey A. Chernov
496aa3d69a6SAndrey A. Chernov dc = wbuf;
497aed721ecSAndrey A. Chernov for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT)
498aed721ecSAndrey A. Chernov continue;
499aa3d69a6SAndrey A. Chernov if (*dc != EOS)
500f4d4982eSAndrey A. Chernov return (NULL);
50158f0484fSRodney W. Grimes
50258f0484fSRodney W. Grimes /* Append the rest of the pattern */
503aa3d69a6SAndrey A. Chernov if (*p != EOS) {
504aa3d69a6SAndrey A. Chernov too_long = 1;
505aa3d69a6SAndrey A. Chernov while (b <= eb) {
506aa3d69a6SAndrey A. Chernov if ((*b++ = *p++) == EOS) {
507aa3d69a6SAndrey A. Chernov too_long = 0;
508aa3d69a6SAndrey A. Chernov break;
509aa3d69a6SAndrey A. Chernov }
510aa3d69a6SAndrey A. Chernov }
511aa3d69a6SAndrey A. Chernov if (too_long)
512f4d4982eSAndrey A. Chernov return (NULL);
513aa3d69a6SAndrey A. Chernov } else
51462f187a4SWarner Losh *b = EOS;
51558f0484fSRodney W. Grimes
51685529174SEitan Adler return (patbuf);
51758f0484fSRodney W. Grimes }
51858f0484fSRodney W. Grimes
51958f0484fSRodney W. Grimes
52058f0484fSRodney W. Grimes /*
52158f0484fSRodney W. Grimes * The main glob() routine: compiles the pattern (optionally processing
52258f0484fSRodney W. Grimes * quotes), calls glob1() to do the real pattern matching, and finally
52358f0484fSRodney W. Grimes * sorts the list (unless unsorted operation is requested). Returns 0
5244a59c3abSMike Heffner * if things went well, nonzero if errors occurred.
52558f0484fSRodney W. Grimes */
52658f0484fSRodney W. Grimes static int
glob0(const Char * pattern,glob_t * pglob,struct glob_limit * limit,const char * origpat)527bd7a9850SAndrey A. Chernov glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit,
52809264d74SAndrey A. Chernov const char *origpat) {
52958f0484fSRodney W. Grimes const Char *qpatnext;
5305b3fab81SGordon Tetlow int err;
5314b767fa6SAndrey A. Chernov size_t oldpathc;
5325b3fab81SGordon Tetlow Char *bufnext, c, patbuf[MAXPATHLEN];
53358f0484fSRodney W. Grimes
534487dbd92SPeter Wemm qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
535f4d4982eSAndrey A. Chernov if (qpatnext == NULL) {
536869eb80cSAndrey A. Chernov errno = E2BIG;
537f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE);
538f4d4982eSAndrey A. Chernov }
53958f0484fSRodney W. Grimes oldpathc = pglob->gl_pathc;
54058f0484fSRodney W. Grimes bufnext = patbuf;
54158f0484fSRodney W. Grimes
54258f0484fSRodney W. Grimes /* We don't need to check for buffer overflow any more. */
54358f0484fSRodney W. Grimes while ((c = *qpatnext++) != EOS) {
54458f0484fSRodney W. Grimes switch (c) {
54558f0484fSRodney W. Grimes case LBRACKET:
54658f0484fSRodney W. Grimes c = *qpatnext;
54758f0484fSRodney W. Grimes if (c == NOT)
54858f0484fSRodney W. Grimes ++qpatnext;
54958f0484fSRodney W. Grimes if (*qpatnext == EOS ||
55034a08754SMike Makonnen g_strchr(qpatnext+1, RBRACKET) == NULL) {
55158f0484fSRodney W. Grimes *bufnext++ = LBRACKET;
55258f0484fSRodney W. Grimes if (c == NOT)
55358f0484fSRodney W. Grimes --qpatnext;
55458f0484fSRodney W. Grimes break;
55558f0484fSRodney W. Grimes }
55658f0484fSRodney W. Grimes *bufnext++ = M_SET;
55758f0484fSRodney W. Grimes if (c == NOT)
55858f0484fSRodney W. Grimes *bufnext++ = M_NOT;
55958f0484fSRodney W. Grimes c = *qpatnext++;
56058f0484fSRodney W. Grimes do {
56158f0484fSRodney W. Grimes *bufnext++ = CHAR(c);
56258f0484fSRodney W. Grimes if (*qpatnext == RANGE &&
56358f0484fSRodney W. Grimes (c = qpatnext[1]) != RBRACKET) {
56458f0484fSRodney W. Grimes *bufnext++ = M_RNG;
56558f0484fSRodney W. Grimes *bufnext++ = CHAR(c);
56658f0484fSRodney W. Grimes qpatnext += 2;
56758f0484fSRodney W. Grimes }
56858f0484fSRodney W. Grimes } while ((c = *qpatnext++) != RBRACKET);
56958f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR;
57058f0484fSRodney W. Grimes *bufnext++ = M_END;
57158f0484fSRodney W. Grimes break;
57258f0484fSRodney W. Grimes case QUESTION:
57358f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR;
57458f0484fSRodney W. Grimes *bufnext++ = M_ONE;
57558f0484fSRodney W. Grimes break;
57658f0484fSRodney W. Grimes case STAR:
57758f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR;
57858f0484fSRodney W. Grimes /* collapse adjacent stars to one,
5798f932310SJilles Tjoelker * to ensure "**" at the end continues to match the
5808f932310SJilles Tjoelker * empty string
58158f0484fSRodney W. Grimes */
58258f0484fSRodney W. Grimes if (bufnext == patbuf || bufnext[-1] != M_ALL)
58358f0484fSRodney W. Grimes *bufnext++ = M_ALL;
58458f0484fSRodney W. Grimes break;
58558f0484fSRodney W. Grimes default:
58658f0484fSRodney W. Grimes *bufnext++ = CHAR(c);
58758f0484fSRodney W. Grimes break;
58858f0484fSRodney W. Grimes }
58958f0484fSRodney W. Grimes }
59058f0484fSRodney W. Grimes *bufnext = EOS;
59158f0484fSRodney W. Grimes #ifdef DEBUG
59258f0484fSRodney W. Grimes qprintf("glob0:", patbuf);
59358f0484fSRodney W. Grimes #endif
59458f0484fSRodney W. Grimes
595bae8632fSJonathan Lemon if ((err = glob1(patbuf, pglob, limit)) != 0)
59658f0484fSRodney W. Grimes return(err);
59758f0484fSRodney W. Grimes
59809264d74SAndrey A. Chernov if (origpat != NULL)
59909264d74SAndrey A. Chernov return (globfinal(pglob, limit, oldpathc, origpat));
60009264d74SAndrey A. Chernov
60109264d74SAndrey A. Chernov return (0);
60209264d74SAndrey A. Chernov }
60309264d74SAndrey A. Chernov
60409264d74SAndrey A. Chernov static int
globfinal(glob_t * pglob,struct glob_limit * limit,size_t oldpathc,const char * origpat)60509264d74SAndrey A. Chernov globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc,
60609264d74SAndrey A. Chernov const char *origpat) {
60720e37fa8SAndrey A. Chernov if (pglob->gl_pathc == oldpathc)
60820e37fa8SAndrey A. Chernov return (err_nomatch(pglob, limit, origpat));
60920e37fa8SAndrey A. Chernov
6104a59c3abSMike Heffner if (!(pglob->gl_flags & GLOB_NOSORT))
61158f0484fSRodney W. Grimes qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
61258f0484fSRodney W. Grimes pglob->gl_pathc - oldpathc, sizeof(char *), compare);
61309264d74SAndrey A. Chernov
61458f0484fSRodney W. Grimes return (0);
61558f0484fSRodney W. Grimes }
61658f0484fSRodney W. Grimes
61758f0484fSRodney W. Grimes static int
compare(const void * p,const void * q)6181cec70adSXin LI compare(const void *p, const void *q)
61958f0484fSRodney W. Grimes {
620aa3d69a6SAndrey A. Chernov return (strcoll(*(char **)p, *(char **)q));
62158f0484fSRodney W. Grimes }
62258f0484fSRodney W. Grimes
62358f0484fSRodney W. Grimes static int
glob1(Char * pattern,glob_t * pglob,struct glob_limit * limit)624daab0b01SMarcel Moolenaar glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
62558f0484fSRodney W. Grimes {
626487dbd92SPeter Wemm Char pathbuf[MAXPATHLEN];
62758f0484fSRodney W. Grimes
62858f0484fSRodney W. Grimes /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
62958f0484fSRodney W. Grimes if (*pattern == EOS)
63058f0484fSRodney W. Grimes return (0);
631487dbd92SPeter Wemm return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
632487dbd92SPeter Wemm pattern, pglob, limit));
63358f0484fSRodney W. Grimes }
63458f0484fSRodney W. Grimes
63558f0484fSRodney W. Grimes /*
63658f0484fSRodney W. Grimes * The functions glob2 and glob3 are mutually recursive; there is one level
63758f0484fSRodney W. Grimes * of recursion for each segment in the pattern that contains one or more
63858f0484fSRodney W. Grimes * meta characters.
63958f0484fSRodney W. Grimes */
64058f0484fSRodney W. Grimes static int
glob2(Char * pathbuf,Char * pathend,Char * pathend_last,Char * pattern,glob_t * pglob,struct glob_limit * limit)6411cec70adSXin LI glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
642daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit)
64358f0484fSRodney W. Grimes {
64458f0484fSRodney W. Grimes struct stat sb;
64558f0484fSRodney W. Grimes Char *p, *q;
64658f0484fSRodney W. Grimes int anymeta;
64758f0484fSRodney W. Grimes
64858f0484fSRodney W. Grimes /*
64958f0484fSRodney W. Grimes * Loop over pattern segments until end of pattern or until
65058f0484fSRodney W. Grimes * segment with meta character found.
65158f0484fSRodney W. Grimes */
65258f0484fSRodney W. Grimes for (anymeta = 0;;) {
65358f0484fSRodney W. Grimes if (*pattern == EOS) { /* End of pattern? */
65458f0484fSRodney W. Grimes *pathend = EOS;
65558f0484fSRodney W. Grimes if (g_lstat(pathbuf, &sb, pglob))
65658f0484fSRodney W. Grimes return (0);
65758f0484fSRodney W. Grimes
658daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) &&
659daab0b01SMarcel Moolenaar limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
660869eb80cSAndrey A. Chernov errno = E2BIG;
661daab0b01SMarcel Moolenaar return (GLOB_NOSPACE);
662daab0b01SMarcel Moolenaar }
663aed721ecSAndrey A. Chernov if ((pglob->gl_flags & GLOB_MARK) &&
664aed721ecSAndrey A. Chernov UNPROT(pathend[-1]) != SEP &&
665aed721ecSAndrey A. Chernov (S_ISDIR(sb.st_mode) ||
666aed721ecSAndrey A. Chernov (S_ISLNK(sb.st_mode) &&
667aed721ecSAndrey A. Chernov g_stat(pathbuf, &sb, pglob) == 0 &&
66858f0484fSRodney W. Grimes S_ISDIR(sb.st_mode)))) {
669f4d4982eSAndrey A. Chernov if (pathend + 1 > pathend_last) {
670869eb80cSAndrey A. Chernov errno = E2BIG;
671f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE);
672f4d4982eSAndrey A. Chernov }
67358f0484fSRodney W. Grimes *pathend++ = SEP;
67458f0484fSRodney W. Grimes *pathend = EOS;
67558f0484fSRodney W. Grimes }
67658f0484fSRodney W. Grimes ++pglob->gl_matchc;
67709264d74SAndrey A. Chernov return (globextend(pathbuf, pglob, limit, NULL));
67858f0484fSRodney W. Grimes }
67958f0484fSRodney W. Grimes
68058f0484fSRodney W. Grimes /* Find end of next segment, copy tentatively to pathend. */
68158f0484fSRodney W. Grimes q = pathend;
68258f0484fSRodney W. Grimes p = pattern;
683aed721ecSAndrey A. Chernov while (*p != EOS && UNPROT(*p) != SEP) {
68458f0484fSRodney W. Grimes if (ismeta(*p))
68558f0484fSRodney W. Grimes anymeta = 1;
686f4d4982eSAndrey A. Chernov if (q + 1 > pathend_last) {
687869eb80cSAndrey A. Chernov errno = E2BIG;
688f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE);
689f4d4982eSAndrey A. Chernov }
69058f0484fSRodney W. Grimes *q++ = *p++;
69158f0484fSRodney W. Grimes }
69258f0484fSRodney W. Grimes
69358f0484fSRodney W. Grimes if (!anymeta) { /* No expansion, do next segment. */
69458f0484fSRodney W. Grimes pathend = q;
69558f0484fSRodney W. Grimes pattern = p;
696aed721ecSAndrey A. Chernov while (UNPROT(*pattern) == SEP) {
697f4d4982eSAndrey A. Chernov if (pathend + 1 > pathend_last) {
698869eb80cSAndrey A. Chernov errno = E2BIG;
699f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE);
700f4d4982eSAndrey A. Chernov }
70158f0484fSRodney W. Grimes *pathend++ = *pattern++;
702487dbd92SPeter Wemm }
70358f0484fSRodney W. Grimes } else /* Need expansion, recurse. */
70485529174SEitan Adler return (glob3(pathbuf, pathend, pathend_last, pattern,
70585529174SEitan Adler p, pglob, limit));
70658f0484fSRodney W. Grimes }
70758f0484fSRodney W. Grimes /* NOTREACHED */
70858f0484fSRodney W. Grimes }
70958f0484fSRodney W. Grimes
71058f0484fSRodney W. Grimes static int
glob3(Char * pathbuf,Char * pathend,Char * pathend_last,Char * pattern,Char * restpattern,glob_t * pglob,struct glob_limit * limit)7111cec70adSXin LI glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
7121cec70adSXin LI Char *pattern, Char *restpattern,
713daab0b01SMarcel Moolenaar glob_t *pglob, struct glob_limit *limit)
71458f0484fSRodney W. Grimes {
715b231cb39SDavid E. O'Brien struct dirent *dp;
71658f0484fSRodney W. Grimes DIR *dirp;
717869eb80cSAndrey A. Chernov int err, too_long, saverrno, saverrno2;
718e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1];
71958f0484fSRodney W. Grimes
7203e2981e8SCraig Rodrigues struct dirent *(*readdirfunc)(DIR *);
72158f0484fSRodney W. Grimes
722aed721ecSAndrey A. Chernov if (pathend > pathend_last) {
723869eb80cSAndrey A. Chernov errno = E2BIG;
724f4d4982eSAndrey A. Chernov return (GLOB_NOSPACE);
725aed721ecSAndrey A. Chernov }
726f4d4982eSAndrey A. Chernov *pathend = EOS;
727aed721ecSAndrey A. Chernov if (pglob->gl_errfunc != NULL &&
72809264d74SAndrey A. Chernov g_Ctoc(pathbuf, buf, sizeof(buf))) {
729869eb80cSAndrey A. Chernov errno = E2BIG;
730aed721ecSAndrey A. Chernov return (GLOB_NOSPACE);
731aed721ecSAndrey A. Chernov }
73258f0484fSRodney W. Grimes
733869eb80cSAndrey A. Chernov saverrno = errno;
734aed721ecSAndrey A. Chernov errno = 0;
73558f0484fSRodney W. Grimes if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
736196d61a9SAndrey A. Chernov if (errno == ENOENT || errno == ENOTDIR)
737196d61a9SAndrey A. Chernov return (0);
73820e37fa8SAndrey A. Chernov err = err_aborted(pglob, errno, buf);
739869eb80cSAndrey A. Chernov if (errno == 0)
740869eb80cSAndrey A. Chernov errno = saverrno;
74120e37fa8SAndrey A. Chernov return (err);
74258f0484fSRodney W. Grimes }
74358f0484fSRodney W. Grimes
74458f0484fSRodney W. Grimes err = 0;
74558f0484fSRodney W. Grimes
7463e2981e8SCraig Rodrigues /* pglob->gl_readdir takes a void *, fix this manually */
74758f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC)
7483e2981e8SCraig Rodrigues readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
74958f0484fSRodney W. Grimes else
75058f0484fSRodney W. Grimes readdirfunc = readdir;
7513e2981e8SCraig Rodrigues
752aed721ecSAndrey A. Chernov errno = 0;
7533e2981e8SCraig Rodrigues /* Search directory for matching names. */
7543e2981e8SCraig Rodrigues while ((dp = (*readdirfunc)(dirp)) != NULL) {
7551cec70adSXin LI char *sc;
756b231cb39SDavid E. O'Brien Char *dc;
757e9346e01STim J. Robbins wchar_t wc;
758e9346e01STim J. Robbins size_t clen;
759e9346e01STim J. Robbins mbstate_t mbs;
76058f0484fSRodney W. Grimes
761daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) &&
762daab0b01SMarcel Moolenaar limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
763869eb80cSAndrey A. Chernov errno = E2BIG;
764f4d4982eSAndrey A. Chernov err = GLOB_NOSPACE;
765daab0b01SMarcel Moolenaar break;
766daab0b01SMarcel Moolenaar }
767daab0b01SMarcel Moolenaar
76858f0484fSRodney W. Grimes /* Initial DOT must be matched literally. */
769e04d8562SAndrey A. Chernov if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) {
770e04d8562SAndrey A. Chernov errno = 0;
77158f0484fSRodney W. Grimes continue;
772e04d8562SAndrey A. Chernov }
773e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs));
774487dbd92SPeter Wemm dc = pathend;
7751cec70adSXin LI sc = dp->d_name;
776aed721ecSAndrey A. Chernov too_long = 1;
777aed721ecSAndrey A. Chernov while (dc <= pathend_last) {
778e9346e01STim J. Robbins clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
779e9346e01STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) {
780aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */
781aa3d69a6SAndrey A. Chernov wc = (unsigned char)*sc;
782e9346e01STim J. Robbins clen = 1;
783e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs));
784e9346e01STim J. Robbins }
785aed721ecSAndrey A. Chernov if ((*dc++ = wc) == EOS) {
786aed721ecSAndrey A. Chernov too_long = 0;
787e9346e01STim J. Robbins break;
788aed721ecSAndrey A. Chernov }
789e9346e01STim J. Robbins sc += clen;
790e9346e01STim J. Robbins }
79120e37fa8SAndrey A. Chernov if (too_long && (err = err_aborted(pglob, ENAMETOOLONG,
79220e37fa8SAndrey A. Chernov buf))) {
79315cb7866SAndrey A. Chernov errno = ENAMETOOLONG;
79415cb7866SAndrey A. Chernov break;
79515cb7866SAndrey A. Chernov }
796aed721ecSAndrey A. Chernov if (too_long || !match(pathend, pattern, restpattern)) {
79758f0484fSRodney W. Grimes *pathend = EOS;
798e04d8562SAndrey A. Chernov errno = 0;
79958f0484fSRodney W. Grimes continue;
80058f0484fSRodney W. Grimes }
801000b8f83SAndrey A. Chernov if (errno == 0)
802000b8f83SAndrey A. Chernov errno = saverrno;
803487dbd92SPeter Wemm err = glob2(pathbuf, --dc, pathend_last, restpattern,
804487dbd92SPeter Wemm pglob, limit);
80558f0484fSRodney W. Grimes if (err)
80658f0484fSRodney W. Grimes break;
807aed721ecSAndrey A. Chernov errno = 0;
80858f0484fSRodney W. Grimes }
80958f0484fSRodney W. Grimes
810869eb80cSAndrey A. Chernov saverrno2 = errno;
81158f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC)
81258f0484fSRodney W. Grimes (*pglob->gl_closedir)(dirp);
81358f0484fSRodney W. Grimes else
81458f0484fSRodney W. Grimes closedir(dirp);
815869eb80cSAndrey A. Chernov errno = saverrno2;
816aed721ecSAndrey A. Chernov
817aed721ecSAndrey A. Chernov if (err)
81858f0484fSRodney W. Grimes return (err);
819aed721ecSAndrey A. Chernov
82020e37fa8SAndrey A. Chernov if (dp == NULL && errno != 0 &&
82120e37fa8SAndrey A. Chernov (err = err_aborted(pglob, errno, buf)))
82220e37fa8SAndrey A. Chernov return (err);
823aed721ecSAndrey A. Chernov
824869eb80cSAndrey A. Chernov if (errno == 0)
825869eb80cSAndrey A. Chernov errno = saverrno;
826aed721ecSAndrey A. Chernov return (0);
82758f0484fSRodney W. Grimes }
82858f0484fSRodney W. Grimes
82958f0484fSRodney W. Grimes
83058f0484fSRodney W. Grimes /*
831b4c19408SEd Maste * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
83258f0484fSRodney W. Grimes * add the new item, and update gl_pathc.
83358f0484fSRodney W. Grimes *
83458f0484fSRodney W. Grimes * This assumes the BSD realloc, which only copies the block when its size
83558f0484fSRodney W. Grimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
83658f0484fSRodney W. Grimes * behavior.
83758f0484fSRodney W. Grimes *
83858f0484fSRodney W. Grimes * Return 0 if new item added, error code if memory couldn't be allocated.
83958f0484fSRodney W. Grimes *
84058f0484fSRodney W. Grimes * Invariant of the glob_t structure:
84158f0484fSRodney W. Grimes * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
84258f0484fSRodney W. Grimes * gl_pathv points to (gl_offs + gl_pathc + 1) items.
84358f0484fSRodney W. Grimes */
84458f0484fSRodney W. Grimes static int
globextend(const Char * path,glob_t * pglob,struct glob_limit * limit,const char * origpat)845aed721ecSAndrey A. Chernov globextend(const Char *path, glob_t *pglob, struct glob_limit *limit,
84609264d74SAndrey A. Chernov const char *origpat)
84758f0484fSRodney W. Grimes {
848b231cb39SDavid E. O'Brien char **pathv;
8499f36610fSPedro F. Giffuni size_t i, newn, len;
85058f0484fSRodney W. Grimes char *copy;
85158f0484fSRodney W. Grimes const Char *p;
85258f0484fSRodney W. Grimes
853daab0b01SMarcel Moolenaar if ((pglob->gl_flags & GLOB_LIMIT) &&
854daab0b01SMarcel Moolenaar pglob->gl_matchc > limit->l_path_lim) {
855869eb80cSAndrey A. Chernov errno = E2BIG;
85675dc5f1aSMike Heffner return (GLOB_NOSPACE);
85775dc5f1aSMike Heffner }
858813c96dbSJonathan Lemon
8599f36610fSPedro F. Giffuni newn = 2 + pglob->gl_pathc + pglob->gl_offs;
8609f36610fSPedro F. Giffuni /* reallocarray(NULL, newn, size) is equivalent to malloc(newn*size). */
8619f36610fSPedro F. Giffuni pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
862b628fac5SMarcel Moolenaar if (pathv == NULL)
86358f0484fSRodney W. Grimes return (GLOB_NOSPACE);
86458f0484fSRodney W. Grimes
86558f0484fSRodney W. Grimes if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
86658f0484fSRodney W. Grimes /* first time around -- clear initial gl_offs items */
86758f0484fSRodney W. Grimes pathv += pglob->gl_offs;
8684b767fa6SAndrey A. Chernov for (i = pglob->gl_offs + 1; --i > 0; )
86958f0484fSRodney W. Grimes *--pathv = NULL;
87058f0484fSRodney W. Grimes }
87158f0484fSRodney W. Grimes pglob->gl_pathv = pathv;
87258f0484fSRodney W. Grimes
87309264d74SAndrey A. Chernov if (origpat != NULL)
87409264d74SAndrey A. Chernov copy = strdup(origpat);
87509264d74SAndrey A. Chernov else {
876aed721ecSAndrey A. Chernov for (p = path; *p++ != EOS;)
87758f0484fSRodney W. Grimes continue;
878e9346e01STim J. Robbins len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
879bd7a9850SAndrey A. Chernov if ((copy = malloc(len)) != NULL) {
88009264d74SAndrey A. Chernov if (g_Ctoc(path, copy, len)) {
881bd7a9850SAndrey A. Chernov free(copy);
882869eb80cSAndrey A. Chernov errno = E2BIG;
883daab0b01SMarcel Moolenaar return (GLOB_NOSPACE);
884daab0b01SMarcel Moolenaar }
88509264d74SAndrey A. Chernov }
88609264d74SAndrey A. Chernov }
88709264d74SAndrey A. Chernov if (copy != NULL) {
888bd7a9850SAndrey A. Chernov limit->l_string_cnt += strlen(copy) + 1;
889bd7a9850SAndrey A. Chernov if ((pglob->gl_flags & GLOB_LIMIT) &&
890bd7a9850SAndrey A. Chernov limit->l_string_cnt >= GLOB_LIMIT_STRING) {
89176e2bc01SPeter Wemm free(copy);
892869eb80cSAndrey A. Chernov errno = E2BIG;
89376e2bc01SPeter Wemm return (GLOB_NOSPACE);
89476e2bc01SPeter Wemm }
89558f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
89658f0484fSRodney W. Grimes }
89758f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
89858f0484fSRodney W. Grimes return (copy == NULL ? GLOB_NOSPACE : 0);
89958f0484fSRodney W. Grimes }
90058f0484fSRodney W. Grimes
90158f0484fSRodney W. Grimes /*
902241eb37eSConrad Meyer * pattern matching function for filenames.
90358f0484fSRodney W. Grimes */
90458f0484fSRodney W. Grimes static int
match(Char * name,Char * pat,Char * patend)9051cec70adSXin LI match(Char *name, Char *pat, Char *patend)
90658f0484fSRodney W. Grimes {
90758f0484fSRodney W. Grimes int ok, negate_range;
908241eb37eSConrad Meyer Char c, k, *nextp, *nextn;
9091daad8f5SAndrey A. Chernov struct xlocale_collate *table =
9101daad8f5SAndrey A. Chernov (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
91158f0484fSRodney W. Grimes
912241eb37eSConrad Meyer nextn = NULL;
913241eb37eSConrad Meyer nextp = NULL;
914241eb37eSConrad Meyer
915241eb37eSConrad Meyer while (1) {
91658f0484fSRodney W. Grimes while (pat < patend) {
91758f0484fSRodney W. Grimes c = *pat++;
91858f0484fSRodney W. Grimes switch (c & M_MASK) {
91958f0484fSRodney W. Grimes case M_ALL:
92058f0484fSRodney W. Grimes if (pat == patend)
92158f0484fSRodney W. Grimes return (1);
922241eb37eSConrad Meyer if (*name == EOS)
92358f0484fSRodney W. Grimes return (0);
924241eb37eSConrad Meyer nextn = name + 1;
925241eb37eSConrad Meyer nextp = pat - 1;
926241eb37eSConrad Meyer break;
92758f0484fSRodney W. Grimes case M_ONE:
92858f0484fSRodney W. Grimes if (*name++ == EOS)
929241eb37eSConrad Meyer goto fail;
93058f0484fSRodney W. Grimes break;
93158f0484fSRodney W. Grimes case M_SET:
93258f0484fSRodney W. Grimes ok = 0;
93358f0484fSRodney W. Grimes if ((k = *name++) == EOS)
934241eb37eSConrad Meyer goto fail;
9351365421fSConrad Meyer negate_range = ((*pat & M_MASK) == M_NOT);
9361365421fSConrad Meyer if (negate_range != 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)
952241eb37eSConrad Meyer goto fail;
95358f0484fSRodney W. Grimes break;
95458f0484fSRodney W. Grimes default:
95558f0484fSRodney W. Grimes if (*name++ != c)
956241eb37eSConrad Meyer goto fail;
95758f0484fSRodney W. Grimes break;
95858f0484fSRodney W. Grimes }
95958f0484fSRodney W. Grimes }
960241eb37eSConrad Meyer if (*name == EOS)
961241eb37eSConrad Meyer return (1);
962241eb37eSConrad Meyer
963241eb37eSConrad Meyer fail:
964241eb37eSConrad Meyer if (nextn == NULL)
965241eb37eSConrad Meyer break;
966241eb37eSConrad Meyer pat = nextp;
967241eb37eSConrad Meyer name = nextn;
968241eb37eSConrad Meyer }
969241eb37eSConrad Meyer return (0);
97058f0484fSRodney W. Grimes }
97158f0484fSRodney W. Grimes
97258f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */
97358f0484fSRodney W. Grimes void
globfree(glob_t * pglob)9741cec70adSXin LI globfree(glob_t *pglob)
97558f0484fSRodney W. Grimes {
9764b767fa6SAndrey A. Chernov size_t i;
977b231cb39SDavid E. O'Brien char **pp;
97858f0484fSRodney W. Grimes
97958f0484fSRodney W. Grimes if (pglob->gl_pathv != NULL) {
98058f0484fSRodney W. Grimes pp = pglob->gl_pathv + pglob->gl_offs;
98158f0484fSRodney W. Grimes for (i = pglob->gl_pathc; i--; ++pp)
98258f0484fSRodney W. Grimes if (*pp)
98358f0484fSRodney W. Grimes free(*pp);
98458f0484fSRodney W. Grimes free(pglob->gl_pathv);
98576e2bc01SPeter Wemm pglob->gl_pathv = NULL;
98658f0484fSRodney W. Grimes }
98758f0484fSRodney W. Grimes }
98858f0484fSRodney W. Grimes
98958f0484fSRodney W. Grimes static DIR *
g_opendir(Char * str,glob_t * pglob)9901cec70adSXin LI g_opendir(Char *str, glob_t *pglob)
99158f0484fSRodney W. Grimes {
992e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1];
99358f0484fSRodney W. Grimes
994aa3d69a6SAndrey A. Chernov if (*str == EOS)
99558f0484fSRodney W. Grimes strcpy(buf, ".");
99676e2bc01SPeter Wemm else {
99709264d74SAndrey A. Chernov if (g_Ctoc(str, buf, sizeof(buf))) {
998196d61a9SAndrey A. Chernov errno = ENAMETOOLONG;
99976e2bc01SPeter Wemm return (NULL);
100076e2bc01SPeter Wemm }
1001196d61a9SAndrey A. Chernov }
100258f0484fSRodney W. Grimes
100358f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC)
100458f0484fSRodney W. Grimes return ((*pglob->gl_opendir)(buf));
100558f0484fSRodney W. Grimes
100658f0484fSRodney W. Grimes return (opendir(buf));
100758f0484fSRodney W. Grimes }
100858f0484fSRodney W. Grimes
100958f0484fSRodney W. Grimes static int
g_lstat(Char * fn,struct stat * sb,glob_t * pglob)10101cec70adSXin LI g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
101158f0484fSRodney W. Grimes {
1012e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1];
101358f0484fSRodney W. Grimes
101409264d74SAndrey A. Chernov if (g_Ctoc(fn, buf, sizeof(buf))) {
101576e2bc01SPeter Wemm errno = ENAMETOOLONG;
101676e2bc01SPeter Wemm return (-1);
101776e2bc01SPeter Wemm }
101858f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC)
101958f0484fSRodney W. Grimes return((*pglob->gl_lstat)(buf, sb));
102058f0484fSRodney W. Grimes return (lstat(buf, sb));
102158f0484fSRodney W. Grimes }
102258f0484fSRodney W. Grimes
102358f0484fSRodney W. Grimes static int
g_stat(Char * fn,struct stat * sb,glob_t * pglob)10241cec70adSXin LI g_stat(Char *fn, struct stat *sb, glob_t *pglob)
102558f0484fSRodney W. Grimes {
1026e9c01372SAndrey A. Chernov char buf[MAXPATHLEN + MB_LEN_MAX - 1];
102758f0484fSRodney W. Grimes
102809264d74SAndrey A. Chernov if (g_Ctoc(fn, buf, sizeof(buf))) {
102976e2bc01SPeter Wemm errno = ENAMETOOLONG;
103076e2bc01SPeter Wemm return (-1);
103176e2bc01SPeter Wemm }
103258f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC)
103358f0484fSRodney W. Grimes return ((*pglob->gl_stat)(buf, sb));
103458f0484fSRodney W. Grimes return (stat(buf, sb));
103558f0484fSRodney W. Grimes }
103658f0484fSRodney W. Grimes
103734a08754SMike Makonnen static const Char *
g_strchr(const Char * str,wchar_t ch)103834a08754SMike Makonnen g_strchr(const Char *str, wchar_t ch)
103958f0484fSRodney W. Grimes {
10401cec70adSXin LI
104158f0484fSRodney W. Grimes do {
104258f0484fSRodney W. Grimes if (*str == ch)
104358f0484fSRodney W. Grimes return (str);
104458f0484fSRodney W. Grimes } while (*str++);
104558f0484fSRodney W. Grimes return (NULL);
104658f0484fSRodney W. Grimes }
104758f0484fSRodney W. Grimes
104876e2bc01SPeter Wemm static int
g_Ctoc(const Char * str,char * buf,size_t len)104909264d74SAndrey A. Chernov g_Ctoc(const Char *str, char *buf, size_t len)
105058f0484fSRodney W. Grimes {
1051e9346e01STim J. Robbins mbstate_t mbs;
1052e9346e01STim J. Robbins size_t clen;
105358f0484fSRodney W. Grimes
1054e9346e01STim J. Robbins memset(&mbs, 0, sizeof(mbs));
1055e9346e01STim J. Robbins while (len >= MB_CUR_MAX) {
105609264d74SAndrey A. Chernov clen = wcrtomb(buf, CHAR(*str), &mbs);
1057aa3d69a6SAndrey A. Chernov if (clen == (size_t)-1) {
1058aa3d69a6SAndrey A. Chernov /* XXX See initial comment #2. */
105909264d74SAndrey A. Chernov *buf = (char)CHAR(*str);
1060aa3d69a6SAndrey A. Chernov clen = 1;
1061aa3d69a6SAndrey A. Chernov memset(&mbs, 0, sizeof(mbs));
1062aa3d69a6SAndrey A. Chernov }
106309264d74SAndrey A. Chernov if (CHAR(*str) == EOS)
106476e2bc01SPeter Wemm return (0);
106509264d74SAndrey A. Chernov str++;
1066e9346e01STim J. Robbins buf += clen;
1067e9346e01STim J. Robbins len -= clen;
106858f0484fSRodney W. Grimes }
106927d52f69SPeter Wemm return (1);
107027d52f69SPeter Wemm }
107158f0484fSRodney W. Grimes
107220e37fa8SAndrey A. Chernov static int
err_nomatch(glob_t * pglob,struct glob_limit * limit,const char * origpat)107320e37fa8SAndrey A. Chernov err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) {
107420e37fa8SAndrey A. Chernov /*
107520e37fa8SAndrey A. Chernov * If there was no match we are going to append the origpat
107620e37fa8SAndrey A. Chernov * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
107720e37fa8SAndrey A. Chernov * and the origpat did not contain any magic characters
107820e37fa8SAndrey A. Chernov * GLOB_NOMAGIC is there just for compatibility with csh.
107920e37fa8SAndrey A. Chernov */
108020e37fa8SAndrey A. Chernov if ((pglob->gl_flags & GLOB_NOCHECK) ||
108120e37fa8SAndrey A. Chernov ((pglob->gl_flags & GLOB_NOMAGIC) &&
108220e37fa8SAndrey A. Chernov !(pglob->gl_flags & GLOB_MAGCHAR)))
108320e37fa8SAndrey A. Chernov return (globextend(NULL, pglob, limit, origpat));
108420e37fa8SAndrey A. Chernov return (GLOB_NOMATCH);
108520e37fa8SAndrey A. Chernov }
108620e37fa8SAndrey A. Chernov
108720e37fa8SAndrey A. Chernov static int
err_aborted(glob_t * pglob,int err,char * buf)108820e37fa8SAndrey A. Chernov err_aborted(glob_t *pglob, int err, char *buf) {
108920e37fa8SAndrey A. Chernov if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) ||
109020e37fa8SAndrey A. Chernov (pglob->gl_flags & GLOB_ERR))
109120e37fa8SAndrey A. Chernov return (GLOB_ABORTED);
109220e37fa8SAndrey A. Chernov return (0);
109320e37fa8SAndrey A. Chernov }
109420e37fa8SAndrey A. Chernov
109558f0484fSRodney W. Grimes #ifdef DEBUG
109658f0484fSRodney W. Grimes static void
qprintf(const char * str,Char * s)10971cec70adSXin LI qprintf(const char *str, Char *s)
109858f0484fSRodney W. Grimes {
1099b231cb39SDavid E. O'Brien Char *p;
110058f0484fSRodney W. Grimes
1101bd7a9850SAndrey A. Chernov (void)printf("%s\n", str);
1102bd7a9850SAndrey A. Chernov if (s != NULL) {
1103bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++)
1104bd7a9850SAndrey A. Chernov (void)printf("%c", (char)CHAR(*p));
110558f0484fSRodney W. Grimes (void)printf("\n");
1106bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++)
1107bd7a9850SAndrey A. Chernov (void)printf("%c", (isprot(*p) ? '\\' : ' '));
110858f0484fSRodney W. Grimes (void)printf("\n");
1109bd7a9850SAndrey A. Chernov for (p = s; *p != EOS; p++)
1110bd7a9850SAndrey A. Chernov (void)printf("%c", (ismeta(*p) ? '_' : ' '));
111158f0484fSRodney W. Grimes (void)printf("\n");
111258f0484fSRodney W. Grimes }
1113bd7a9850SAndrey A. Chernov }
111458f0484fSRodney W. Grimes #endif
1115