17c478bd9Sstevel@tonic-gate /*
2a5229c74SGary Mills * Copyright (c) 1989, 1993
3a5229c74SGary Mills * The Regents of the University of California. All rights reserved.
47c478bd9Sstevel@tonic-gate *
5a5229c74SGary Mills * This code is derived from software contributed to Berkeley by
6a5229c74SGary Mills * Guido van Rossum.
77c478bd9Sstevel@tonic-gate *
8a5229c74SGary Mills * Redistribution and use in source and binary forms, with or without
9a5229c74SGary Mills * modification, are permitted provided that the following conditions
10a5229c74SGary Mills * are met:
11a5229c74SGary Mills * 1. Redistributions of source code must retain the above copyright
12a5229c74SGary Mills * notice, this list of conditions and the following disclaimer.
13a5229c74SGary Mills * 2. Redistributions in binary form must reproduce the above copyright
14a5229c74SGary Mills * notice, this list of conditions and the following disclaimer in the
15a5229c74SGary Mills * documentation and/or other materials provided with the distribution.
16a5229c74SGary Mills * 3. Neither the name of the University nor the names of its contributors
17a5229c74SGary Mills * may be used to endorse or promote products derived from this software
18a5229c74SGary Mills * without specific prior written permission.
197c478bd9Sstevel@tonic-gate *
20a5229c74SGary Mills * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21a5229c74SGary Mills * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a5229c74SGary Mills * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a5229c74SGary Mills * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24a5229c74SGary Mills * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25a5229c74SGary Mills * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26a5229c74SGary Mills * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27a5229c74SGary Mills * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28a5229c74SGary Mills * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29a5229c74SGary Mills * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30a5229c74SGary Mills * SUCH DAMAGE.
317c478bd9Sstevel@tonic-gate */
32e8031f0aSraf
337c478bd9Sstevel@tonic-gate /*
34*79d022daSYuri Pankov * Copyright (c) 2013 Gary Mills
35*79d022daSYuri Pankov */
36*79d022daSYuri Pankov
37*79d022daSYuri Pankov /*
38a5229c74SGary Mills * glob(3) -- a superset of the one defined in POSIX 1003.2.
39a5229c74SGary Mills *
40a5229c74SGary Mills * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
41a5229c74SGary Mills *
42a5229c74SGary Mills * Optional extra services, controlled by flags not defined by POSIX:
43a5229c74SGary Mills *
44a5229c74SGary Mills * GLOB_QUOTE:
45a5229c74SGary Mills * Escaping convention: \ inhibits any special meaning the following
46a5229c74SGary Mills * character might have (except \ at end of string is retained).
47a5229c74SGary Mills * GLOB_MAGCHAR:
48a5229c74SGary Mills * Set in gl_flags if pattern contained a globbing character.
49a5229c74SGary Mills * GLOB_NOMAGIC:
50a5229c74SGary Mills * Same as GLOB_NOCHECK, but it will only append pattern if it did
51a5229c74SGary Mills * not contain any magic characters. [Used in csh style globbing]
52a5229c74SGary Mills * GLOB_ALTDIRFUNC:
53a5229c74SGary Mills * Use alternately specified directory access functions.
54a5229c74SGary Mills * GLOB_TILDE:
55a5229c74SGary Mills * expand ~user/foo to the /home/dir/of/user/foo
56a5229c74SGary Mills * GLOB_BRACE:
57a5229c74SGary Mills * expand {1,2}{a,b} to 1a 1b 2a 2b
58a5229c74SGary Mills * gl_matchc:
59a5229c74SGary Mills * Number of matches in the current invocation of glob.
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate
627257d1b4Sraf #include "lint.h"
63a5229c74SGary Mills
64a5229c74SGary Mills #include <sys/param.h>
65a5229c74SGary Mills #include <sys/stat.h>
66a5229c74SGary Mills
67a5229c74SGary Mills #include <ctype.h>
68a5229c74SGary Mills #include <dirent.h>
69a5229c74SGary Mills #include <errno.h>
70a5229c74SGary Mills #include <glob.h>
717c478bd9Sstevel@tonic-gate #include <limits.h>
72a5229c74SGary Mills #include <pwd.h>
73*79d022daSYuri Pankov #include <stdint.h>
74a5229c74SGary Mills #include <stdio.h>
757c478bd9Sstevel@tonic-gate #include <stdlib.h>
767c478bd9Sstevel@tonic-gate #include <string.h>
77a5229c74SGary Mills #include <unistd.h>
78a5229c74SGary Mills #include <wchar.h>
79a5229c74SGary Mills #include <wctype.h>
807c478bd9Sstevel@tonic-gate
81a5229c74SGary Mills /*
82a5229c74SGary Mills * This is the legacy glob_t prior to illumos enhancement 1097,
83a5229c74SGary Mills * used when old programs call the old libc glob functions.
84a5229c74SGary Mills * (New programs call the _glob_ext, _globfree_ext functions.)
85a5229c74SGary Mills * This struct should be considered "carved in stone".
86a5229c74SGary Mills */
87a5229c74SGary Mills typedef struct old_glob {
88a5229c74SGary Mills size_t gl_pathc; /* Count of paths matched by pattern */
89a5229c74SGary Mills char **gl_pathv; /* List of matched pathnames */
90a5229c74SGary Mills size_t gl_offs; /* # of slots reserved in gl_pathv */
91a5229c74SGary Mills /* following are internal to the implementation */
92a5229c74SGary Mills char **gl_pathp; /* gl_pathv + gl_offs */
93a5229c74SGary Mills int gl_pathn; /* # of elements allocated */
94a5229c74SGary Mills } old_glob_t;
95a5229c74SGary Mills
96a5229c74SGary Mills /*
97a5229c74SGary Mills * For old programs, the external names need to be the old names:
98a5229c74SGary Mills * glob() and globfree() . We've redefined those already to
99a5229c74SGary Mills * _glob_ext() and _globfree_ext() . Now redefine old_glob()
100a5229c74SGary Mills * and old_globfree() to glob() and globfree() .
101a5229c74SGary Mills */
102a5229c74SGary Mills #ifdef __PRAGMA_REDEFINE_EXTNAME
103a5229c74SGary Mills #pragma redefine_extname old_glob glob
104a5229c74SGary Mills #pragma redefine_extname old_globfree globfree
105a5229c74SGary Mills #endif /* __PRAGMA_REDEFINE_EXTNAME */
106a5229c74SGary Mills extern int old_glob(const char *, int, int (*)(const char *, int),
107a5229c74SGary Mills old_glob_t *);
108a5229c74SGary Mills extern void old_globfree(old_glob_t *);
109a5229c74SGary Mills
11033e8313dSRobert Mustacchi /*
11133e8313dSRobert Mustacchi * The various extensions to glob(3C) allow for stat and dirent structures to
11233e8313dSRobert Mustacchi * show up whose size may change in a largefile environment. If libc defines
11333e8313dSRobert Mustacchi * _FILE_OFFSET_BITS to be 64 that is the key to indicate that we're building
11433e8313dSRobert Mustacchi * the LFS version of this file. As such, we rename the public functions here,
11533e8313dSRobert Mustacchi * _glob_ext() and _globfree_ext() to have a 64 suffix. When building the LFS
11633e8313dSRobert Mustacchi * version, we do not include the old versions.
11733e8313dSRobert Mustacchi */
11833e8313dSRobert Mustacchi #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
11933e8313dSRobert Mustacchi #define _glob_ext _glob_ext64
12033e8313dSRobert Mustacchi #define _globfree_ext _globfree_ext64
12133e8313dSRobert Mustacchi #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
12233e8313dSRobert Mustacchi
123a5229c74SGary Mills #define DOLLAR '$'
124a5229c74SGary Mills #define DOT '.'
125a5229c74SGary Mills #define EOS '\0'
126a5229c74SGary Mills #define LBRACKET '['
127a5229c74SGary Mills #define NOT '!'
128a5229c74SGary Mills #define QUESTION '?'
129a5229c74SGary Mills #define QUOTE '\\'
130a5229c74SGary Mills #define RANGE '-'
131a5229c74SGary Mills #define RBRACKET ']'
132a5229c74SGary Mills #define SEP '/'
133a5229c74SGary Mills #define STAR '*'
134a5229c74SGary Mills #define TILDE '~'
135a5229c74SGary Mills #define UNDERSCORE '_'
136a5229c74SGary Mills #define LBRACE '{'
137a5229c74SGary Mills #define RBRACE '}'
138a5229c74SGary Mills #define SLASH '/'
139a5229c74SGary Mills #define COMMA ','
140a5229c74SGary Mills #define COLON ':'
141a5229c74SGary Mills
142a5229c74SGary Mills #define M_QUOTE 0x800000
143a5229c74SGary Mills #define M_PROTECT 0x400000
144a5229c74SGary Mills
145a5229c74SGary Mills typedef struct wcat {
146a5229c74SGary Mills wchar_t w_wc;
147a5229c74SGary Mills uint_t w_at;
148a5229c74SGary Mills } wcat_t;
149a5229c74SGary Mills
150a5229c74SGary Mills #define M_ALL '*' /* Plus M_QUOTE */
151a5229c74SGary Mills #define M_END ']' /* Plus M_QUOTE */
152a5229c74SGary Mills #define M_NOT '!' /* Plus M_QUOTE */
153a5229c74SGary Mills #define M_ONE '?' /* Plus M_QUOTE */
154a5229c74SGary Mills #define M_RNG '-' /* Plus M_QUOTE */
155a5229c74SGary Mills #define M_SET '[' /* Plus M_QUOTE */
156a5229c74SGary Mills #define M_CLASS ':' /* Plus M_QUOTE */
157a5229c74SGary Mills #define ismeta(c) (((c).w_at&M_QUOTE) != 0)
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate #define INITIAL 8 /* initial pathv allocation */
1607c478bd9Sstevel@tonic-gate
161a5229c74SGary Mills #define GLOB_LIMIT_MALLOC 65536
162a5229c74SGary Mills #define GLOB_LIMIT_STAT 2048
163a5229c74SGary Mills #define GLOB_LIMIT_READDIR 16384
164a5229c74SGary Mills
165a5229c74SGary Mills struct glob_lim {
166a5229c74SGary Mills size_t glim_malloc;
167a5229c74SGary Mills size_t glim_stat;
168a5229c74SGary Mills size_t glim_readdir;
169a5229c74SGary Mills };
170a5229c74SGary Mills
171a5229c74SGary Mills struct glob_path_stat {
172a5229c74SGary Mills char *gps_path;
173a5229c74SGary Mills struct stat *gps_stat;
174a5229c74SGary Mills };
175a5229c74SGary Mills
176a5229c74SGary Mills static int compare(const void *, const void *);
177a5229c74SGary Mills static int compare_gps(const void *, const void *);
178a5229c74SGary Mills static int g_Ctoc(const wcat_t *, char *, uint_t);
179a5229c74SGary Mills static int g_lstat(wcat_t *, struct stat *, glob_t *);
180a5229c74SGary Mills static DIR *g_opendir(wcat_t *, glob_t *);
181a5229c74SGary Mills static wcat_t *g_strchr(const wcat_t *, wchar_t);
182a5229c74SGary Mills static int g_stat(wcat_t *, struct stat *, glob_t *);
183a5229c74SGary Mills static int glob0(const wcat_t *, glob_t *, struct glob_lim *,
184a5229c74SGary Mills int (*)(const char *, int));
185a5229c74SGary Mills static int glob1(wcat_t *, wcat_t *, glob_t *, struct glob_lim *,
186a5229c74SGary Mills int (*)(const char *, int));
187a5229c74SGary Mills static int glob2(wcat_t *, wcat_t *, wcat_t *, wcat_t *, wcat_t *,
188a5229c74SGary Mills wcat_t *, glob_t *, struct glob_lim *,
189a5229c74SGary Mills int (*)(const char *, int));
190a5229c74SGary Mills static int glob3(wcat_t *, wcat_t *, wcat_t *, wcat_t *, wcat_t *,
191a5229c74SGary Mills wcat_t *, wcat_t *, glob_t *, struct glob_lim *,
192a5229c74SGary Mills int (*)(const char *, int));
193a5229c74SGary Mills static int globextend(const wcat_t *, glob_t *, struct glob_lim *,
194a5229c74SGary Mills struct stat *);
195a5229c74SGary Mills static
196a5229c74SGary Mills const wcat_t *globtilde(const wcat_t *, wcat_t *, size_t, glob_t *);
197a5229c74SGary Mills static int globexp1(const wcat_t *, glob_t *, struct glob_lim *,
198a5229c74SGary Mills int (*)(const char *, int));
199a5229c74SGary Mills static int globexp2(const wcat_t *, const wcat_t *, glob_t *,
200a5229c74SGary Mills struct glob_lim *, int (*)(const char *, int));
201*79d022daSYuri Pankov static int match(wcat_t *, wcat_t *, wcat_t *);
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /*
204a5229c74SGary Mills * Extended glob() function, selected by #pragma redefine_extname
205a5229c74SGary Mills * in glob.h with the external name _glob_ext() .
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate int
_glob_ext(const char * pattern,int flags,int (* errfunc)(const char *,int),glob_t * pglob)208a5229c74SGary Mills _glob_ext(const char *pattern, int flags, int (*errfunc)(const char *, int),
209a5229c74SGary Mills glob_t *pglob)
2107c478bd9Sstevel@tonic-gate {
211a5229c74SGary Mills const char *patnext;
212a5229c74SGary Mills int n;
213a5229c74SGary Mills size_t patlen;
214a5229c74SGary Mills wchar_t c;
215*79d022daSYuri Pankov wcat_t *bufnext, *bufend, patbuf[PATH_MAX];
216a5229c74SGary Mills struct glob_lim limit = { 0, 0, 0 };
2177c478bd9Sstevel@tonic-gate
218a5229c74SGary Mills patnext = pattern;
2197c478bd9Sstevel@tonic-gate if (!(flags & GLOB_APPEND)) {
220a5229c74SGary Mills pglob->gl_pathc = 0;
221a5229c74SGary Mills pglob->gl_pathn = 0;
222a5229c74SGary Mills pglob->gl_pathv = NULL;
223a5229c74SGary Mills if ((flags & GLOB_KEEPSTAT) != 0)
224a5229c74SGary Mills pglob->gl_statv = NULL;
225a5229c74SGary Mills if (!(flags & GLOB_DOOFFS))
226a5229c74SGary Mills pglob->gl_offs = 0;
227a5229c74SGary Mills }
228a5229c74SGary Mills pglob->gl_flags = flags & ~GLOB_MAGCHAR;
229a5229c74SGary Mills pglob->gl_matchc = 0;
2307c478bd9Sstevel@tonic-gate
231*79d022daSYuri Pankov if ((patlen = strnlen(pattern, PATH_MAX)) == PATH_MAX)
232*79d022daSYuri Pankov return (GLOB_NOMATCH);
233*79d022daSYuri Pankov
234a5229c74SGary Mills if (pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX ||
235a5229c74SGary Mills pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1)
2367c478bd9Sstevel@tonic-gate return (GLOB_NOSPACE);
2377c478bd9Sstevel@tonic-gate
238a5229c74SGary Mills bufnext = patbuf;
239*79d022daSYuri Pankov bufend = bufnext + PATH_MAX - 1;
240a5229c74SGary Mills patlen += 1;
241a5229c74SGary Mills if (flags & GLOB_NOESCAPE) {
242a5229c74SGary Mills while (bufnext < bufend) {
243a5229c74SGary Mills if ((n = mbtowc(&c, patnext, patlen)) > 0) {
244a5229c74SGary Mills patnext += n;
245a5229c74SGary Mills patlen -= n;
246a5229c74SGary Mills bufnext->w_at = 0;
247a5229c74SGary Mills (bufnext++)->w_wc = c;
248a5229c74SGary Mills } else if (n == 0) {
249a5229c74SGary Mills break;
250a5229c74SGary Mills } else {
251a5229c74SGary Mills return (GLOB_NOMATCH);
252a5229c74SGary Mills }
253a5229c74SGary Mills }
254a5229c74SGary Mills } else {
255a5229c74SGary Mills /* Protect the quoted characters. */
256a5229c74SGary Mills while (bufnext < bufend) {
257a5229c74SGary Mills if ((n = mbtowc(&c, patnext, patlen)) > 0) {
258a5229c74SGary Mills patnext += n;
259a5229c74SGary Mills patlen -= n;
260a5229c74SGary Mills if (c == QUOTE) {
261a5229c74SGary Mills n = mbtowc(&c, patnext, patlen);
262a5229c74SGary Mills if (n < 0)
263a5229c74SGary Mills return (GLOB_NOMATCH);
264a5229c74SGary Mills if (n > 0) {
265a5229c74SGary Mills patnext += n;
266a5229c74SGary Mills patlen -= n;
267a5229c74SGary Mills }
268a5229c74SGary Mills if (n == 0)
269a5229c74SGary Mills c = QUOTE;
270a5229c74SGary Mills bufnext->w_at = M_PROTECT;
271a5229c74SGary Mills (bufnext++)->w_wc = c;
272a5229c74SGary Mills } else {
273a5229c74SGary Mills bufnext->w_at = 0;
274a5229c74SGary Mills (bufnext++)->w_wc = c;
275a5229c74SGary Mills }
276a5229c74SGary Mills } else if (n == 0) {
277a5229c74SGary Mills break;
278a5229c74SGary Mills } else {
279a5229c74SGary Mills return (GLOB_NOMATCH);
280a5229c74SGary Mills }
281a5229c74SGary Mills }
282a5229c74SGary Mills }
283a5229c74SGary Mills bufnext->w_at = 0;
284a5229c74SGary Mills bufnext->w_wc = EOS;
285a5229c74SGary Mills
286a5229c74SGary Mills if (flags & GLOB_BRACE)
287a5229c74SGary Mills return (globexp1(patbuf, pglob, &limit, errfunc));
288a5229c74SGary Mills else
289a5229c74SGary Mills return (glob0(patbuf, pglob, &limit, errfunc));
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate /*
293a5229c74SGary Mills * Expand recursively a glob {} pattern. When there is no more expansion
294a5229c74SGary Mills * invoke the standard globbing routine to glob the rest of the magic
295a5229c74SGary Mills * characters
2967c478bd9Sstevel@tonic-gate */
297a5229c74SGary Mills static int
globexp1(const wcat_t * pattern,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))298a5229c74SGary Mills globexp1(const wcat_t *pattern, glob_t *pglob, struct glob_lim *limitp,
299a5229c74SGary Mills int (*errfunc)(const char *, int))
300a5229c74SGary Mills {
301a5229c74SGary Mills const wcat_t *ptr = pattern;
302a5229c74SGary Mills
303a5229c74SGary Mills /* Protect a single {}, for find(1), like csh */
304a5229c74SGary Mills if (pattern[0].w_wc == LBRACE && pattern[1].w_wc == RBRACE &&
305a5229c74SGary Mills pattern[2].w_wc == EOS)
306a5229c74SGary Mills return (glob0(pattern, pglob, limitp, errfunc));
307a5229c74SGary Mills
308a5229c74SGary Mills if ((ptr = (const wcat_t *) g_strchr(ptr, LBRACE)) != NULL)
309a5229c74SGary Mills return (globexp2(ptr, pattern, pglob, limitp, errfunc));
310a5229c74SGary Mills
311a5229c74SGary Mills return (glob0(pattern, pglob, limitp, errfunc));
312a5229c74SGary Mills }
313a5229c74SGary Mills
314a5229c74SGary Mills
315a5229c74SGary Mills /*
316a5229c74SGary Mills * Recursive brace globbing helper. Tries to expand a single brace.
317a5229c74SGary Mills * If it succeeds then it invokes globexp1 with the new pattern.
318a5229c74SGary Mills * If it fails then it tries to glob the rest of the pattern and returns.
319a5229c74SGary Mills */
320a5229c74SGary Mills static int
globexp2(const wcat_t * ptr,const wcat_t * pattern,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))321a5229c74SGary Mills globexp2(const wcat_t *ptr, const wcat_t *pattern, glob_t *pglob,
322a5229c74SGary Mills struct glob_lim *limitp, int (*errfunc)(const char *, int))
323a5229c74SGary Mills {
324a5229c74SGary Mills int i, rv;
325a5229c74SGary Mills wcat_t *lm, *ls;
326a5229c74SGary Mills const wcat_t *pe, *pm, *pl;
327*79d022daSYuri Pankov wcat_t patbuf[PATH_MAX];
328a5229c74SGary Mills
329a5229c74SGary Mills /* copy part up to the brace */
330a5229c74SGary Mills for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
331a5229c74SGary Mills ;
332a5229c74SGary Mills lm->w_at = 0;
333a5229c74SGary Mills lm->w_wc = EOS;
334a5229c74SGary Mills ls = lm;
335a5229c74SGary Mills
336a5229c74SGary Mills /* Find the balanced brace */
337a5229c74SGary Mills for (i = 0, pe = ++ptr; pe->w_wc != EOS; pe++)
338a5229c74SGary Mills if (pe->w_wc == LBRACKET) {
339a5229c74SGary Mills /* Ignore everything between [] */
340a5229c74SGary Mills for (pm = pe++; pe->w_wc != RBRACKET &&
341a5229c74SGary Mills pe->w_wc != EOS; pe++)
342a5229c74SGary Mills ;
343a5229c74SGary Mills if (pe->w_wc == EOS) {
344a5229c74SGary Mills /*
345a5229c74SGary Mills * We could not find a matching RBRACKET.
346a5229c74SGary Mills * Ignore and just look for RBRACE
347a5229c74SGary Mills */
348a5229c74SGary Mills pe = pm;
349a5229c74SGary Mills }
350a5229c74SGary Mills } else if (pe->w_wc == LBRACE) {
351a5229c74SGary Mills i++;
352a5229c74SGary Mills } else if (pe->w_wc == RBRACE) {
353a5229c74SGary Mills if (i == 0)
354a5229c74SGary Mills break;
355a5229c74SGary Mills i--;
356a5229c74SGary Mills }
357a5229c74SGary Mills
358a5229c74SGary Mills /* Non matching braces; just glob the pattern */
359a5229c74SGary Mills if (i != 0 || pe->w_wc == EOS)
360a5229c74SGary Mills return (glob0(patbuf, pglob, limitp, errfunc));
361a5229c74SGary Mills
362a5229c74SGary Mills for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
363a5229c74SGary Mills switch (pm->w_wc) {
364a5229c74SGary Mills case LBRACKET:
365a5229c74SGary Mills /* Ignore everything between [] */
366a5229c74SGary Mills for (pl = pm++; pm->w_wc != RBRACKET && pm->w_wc != EOS;
367a5229c74SGary Mills pm++)
368a5229c74SGary Mills ;
369a5229c74SGary Mills if (pm->w_wc == EOS) {
370a5229c74SGary Mills /*
371a5229c74SGary Mills * We could not find a matching RBRACKET.
372a5229c74SGary Mills * Ignore and just look for RBRACE
373a5229c74SGary Mills */
374a5229c74SGary Mills pm = pl;
375a5229c74SGary Mills }
376a5229c74SGary Mills break;
377a5229c74SGary Mills
378a5229c74SGary Mills case LBRACE:
379a5229c74SGary Mills i++;
380a5229c74SGary Mills break;
381a5229c74SGary Mills
382a5229c74SGary Mills case RBRACE:
383a5229c74SGary Mills if (i) {
384a5229c74SGary Mills i--;
385a5229c74SGary Mills break;
386a5229c74SGary Mills }
387a5229c74SGary Mills /* FALLTHROUGH */
388a5229c74SGary Mills case COMMA:
389a5229c74SGary Mills if (i && pm->w_wc == COMMA)
390a5229c74SGary Mills break;
391a5229c74SGary Mills else {
392a5229c74SGary Mills /* Append the current string */
393a5229c74SGary Mills for (lm = ls; (pl < pm); *lm++ = *pl++)
394a5229c74SGary Mills ;
395a5229c74SGary Mills
396a5229c74SGary Mills /*
397a5229c74SGary Mills * Append the rest of the pattern after the
398a5229c74SGary Mills * closing brace
399a5229c74SGary Mills */
400a5229c74SGary Mills for (pl = pe + 1;
401a5229c74SGary Mills (*lm++ = *pl++).w_wc != EOS; /* */)
402a5229c74SGary Mills ;
403a5229c74SGary Mills
404a5229c74SGary Mills /* Expand the current pattern */
405a5229c74SGary Mills rv = globexp1(patbuf, pglob, limitp, errfunc);
406a5229c74SGary Mills if (rv && rv != GLOB_NOMATCH)
407a5229c74SGary Mills return (rv);
408a5229c74SGary Mills
409a5229c74SGary Mills /* move after the comma, to the next string */
410a5229c74SGary Mills pl = pm + 1;
411a5229c74SGary Mills }
412a5229c74SGary Mills break;
413a5229c74SGary Mills
414a5229c74SGary Mills default:
415a5229c74SGary Mills break;
416a5229c74SGary Mills }
417a5229c74SGary Mills }
418a5229c74SGary Mills return (0);
419a5229c74SGary Mills }
420a5229c74SGary Mills
421a5229c74SGary Mills
422a5229c74SGary Mills
423a5229c74SGary Mills /*
424a5229c74SGary Mills * expand tilde from the passwd file.
425a5229c74SGary Mills */
426a5229c74SGary Mills static const wcat_t *
globtilde(const wcat_t * pattern,wcat_t * patbuf,size_t patbuf_len,glob_t * pglob)427a5229c74SGary Mills globtilde(const wcat_t *pattern, wcat_t *patbuf, size_t patbuf_len,
428a5229c74SGary Mills glob_t *pglob)
429a5229c74SGary Mills {
430a5229c74SGary Mills struct passwd *pwd;
431a5229c74SGary Mills char *h;
432a5229c74SGary Mills const wcat_t *p;
433a5229c74SGary Mills wcat_t *b, *eb, *q;
434a5229c74SGary Mills int n;
435a5229c74SGary Mills size_t lenh;
436a5229c74SGary Mills wchar_t c;
437a5229c74SGary Mills
438a5229c74SGary Mills if (pattern->w_wc != TILDE || !(pglob->gl_flags & GLOB_TILDE))
439a5229c74SGary Mills return (pattern);
440a5229c74SGary Mills
441a5229c74SGary Mills /* Copy up to the end of the string or / */
442a5229c74SGary Mills eb = &patbuf[patbuf_len - 1];
443a5229c74SGary Mills for (p = pattern + 1, q = patbuf;
444a5229c74SGary Mills q < eb && p->w_wc != EOS && p->w_wc != SLASH; *q++ = *p++)
445a5229c74SGary Mills ;
446a5229c74SGary Mills
447a5229c74SGary Mills q->w_at = 0;
448a5229c74SGary Mills q->w_wc = EOS;
449a5229c74SGary Mills
450a5229c74SGary Mills /* What to do if patbuf is full? */
451a5229c74SGary Mills
452a5229c74SGary Mills if (patbuf[0].w_wc == EOS) {
453a5229c74SGary Mills /*
454a5229c74SGary Mills * handle a plain ~ or ~/ by expanding $HOME
455a5229c74SGary Mills * first and then trying the password file
456a5229c74SGary Mills */
457a5229c74SGary Mills if (issetugid() != 0)
458a5229c74SGary Mills return (pattern);
459a5229c74SGary Mills if ((h = getenv("HOME")) == NULL) {
460a5229c74SGary Mills if ((pwd = getpwuid(getuid())) == NULL)
461a5229c74SGary Mills return (pattern);
462a5229c74SGary Mills else
463a5229c74SGary Mills h = pwd->pw_dir;
464a5229c74SGary Mills }
465a5229c74SGary Mills } else {
466a5229c74SGary Mills /*
467a5229c74SGary Mills * Expand a ~user
468a5229c74SGary Mills */
469a5229c74SGary Mills if ((pwd = getpwnam((char *)patbuf)) == NULL)
470a5229c74SGary Mills return (pattern);
471a5229c74SGary Mills else
472a5229c74SGary Mills h = pwd->pw_dir;
473a5229c74SGary Mills }
474a5229c74SGary Mills
475a5229c74SGary Mills /* Copy the home directory */
476a5229c74SGary Mills lenh = strlen(h) + 1;
477a5229c74SGary Mills for (b = patbuf; b < eb && *h != EOS; b++) {
478a5229c74SGary Mills if ((n = mbtowc(&c, h, lenh)) > 0) {
479a5229c74SGary Mills h += n;
480a5229c74SGary Mills lenh -= n;
481a5229c74SGary Mills b->w_at = 0;
482a5229c74SGary Mills b->w_wc = c;
483a5229c74SGary Mills } else if (n < 0) {
484a5229c74SGary Mills return (pattern);
485a5229c74SGary Mills } else {
486a5229c74SGary Mills break;
487a5229c74SGary Mills }
488a5229c74SGary Mills }
489a5229c74SGary Mills
490a5229c74SGary Mills /* Append the rest of the pattern */
491a5229c74SGary Mills while (b < eb && (*b++ = *p++).w_wc != EOS)
492a5229c74SGary Mills ;
493a5229c74SGary Mills b->w_at = 0;
494a5229c74SGary Mills b->w_wc = EOS;
495a5229c74SGary Mills
496a5229c74SGary Mills return (patbuf);
497a5229c74SGary Mills }
498a5229c74SGary Mills
499a5229c74SGary Mills static int
g_charclass(const wcat_t ** patternp,wcat_t ** bufnextp)500a5229c74SGary Mills g_charclass(const wcat_t **patternp, wcat_t **bufnextp)
501a5229c74SGary Mills {
502a5229c74SGary Mills const wcat_t *pattern = *patternp + 1;
503a5229c74SGary Mills wcat_t *bufnext = *bufnextp;
504a5229c74SGary Mills const wcat_t *colon;
505a5229c74SGary Mills char cbuf[MB_LEN_MAX + 32];
506a5229c74SGary Mills wctype_t cc;
507a5229c74SGary Mills size_t len;
508a5229c74SGary Mills
509a5229c74SGary Mills if ((colon = g_strchr(pattern, COLON)) == NULL ||
510a5229c74SGary Mills colon[1].w_wc != RBRACKET)
511a5229c74SGary Mills return (1); /* not a character class */
512a5229c74SGary Mills
513a5229c74SGary Mills len = (size_t)(colon - pattern);
514a5229c74SGary Mills if (len + MB_LEN_MAX + 1 > sizeof (cbuf))
515a5229c74SGary Mills return (-1); /* invalid character class */
516a5229c74SGary Mills {
517a5229c74SGary Mills wchar_t w;
518a5229c74SGary Mills const wcat_t *s1 = pattern;
519a5229c74SGary Mills char *s2 = cbuf;
520a5229c74SGary Mills size_t n = len;
521a5229c74SGary Mills
522a5229c74SGary Mills /* Copy the string. */
523a5229c74SGary Mills while (n > 0) {
524a5229c74SGary Mills w = (s1++)->w_wc;
525a5229c74SGary Mills /* Character class names must be ASCII. */
526a5229c74SGary Mills if (iswascii(w)) {
527a5229c74SGary Mills n--;
528a5229c74SGary Mills *s2++ = w;
529a5229c74SGary Mills } else {
530a5229c74SGary Mills return (-1); /* invalid character class */
531a5229c74SGary Mills }
532a5229c74SGary Mills }
533a5229c74SGary Mills *s2 = EOS;
534a5229c74SGary Mills }
535a5229c74SGary Mills if ((cc = wctype(cbuf)) == 0)
536a5229c74SGary Mills return (-1); /* invalid character class */
537a5229c74SGary Mills bufnext->w_at = M_QUOTE;
538a5229c74SGary Mills (bufnext++)->w_wc = M_CLASS;
539a5229c74SGary Mills bufnext->w_at = 0;
540a5229c74SGary Mills (bufnext++)->w_wc = cc;
541a5229c74SGary Mills *bufnextp = bufnext;
542a5229c74SGary Mills *patternp += len + 3;
543a5229c74SGary Mills
544a5229c74SGary Mills return (0);
545a5229c74SGary Mills }
546a5229c74SGary Mills
547a5229c74SGary Mills /*
548a5229c74SGary Mills * The main glob() routine: compiles the pattern (optionally processing
549a5229c74SGary Mills * quotes), calls glob1() to do the real pattern matching, and finally
550a5229c74SGary Mills * sorts the list (unless unsorted operation is requested). Returns 0
551a5229c74SGary Mills * if things went well, nonzero if errors occurred. It is not an error
552a5229c74SGary Mills * to find no matches.
553a5229c74SGary Mills */
554a5229c74SGary Mills static int
glob0(const wcat_t * pattern,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))555a5229c74SGary Mills glob0(const wcat_t *pattern, glob_t *pglob, struct glob_lim *limitp,
556a5229c74SGary Mills int (*errfunc)(const char *, int))
557a5229c74SGary Mills {
558a5229c74SGary Mills const wcat_t *qpatnext;
559a5229c74SGary Mills int err, oldpathc;
560a5229c74SGary Mills wchar_t c;
561a5229c74SGary Mills int a;
562*79d022daSYuri Pankov wcat_t *bufnext, patbuf[PATH_MAX];
563a5229c74SGary Mills
564*79d022daSYuri Pankov qpatnext = globtilde(pattern, patbuf, PATH_MAX, pglob);
565a5229c74SGary Mills oldpathc = pglob->gl_pathc;
566a5229c74SGary Mills bufnext = patbuf;
567a5229c74SGary Mills
568a5229c74SGary Mills /*
569a5229c74SGary Mills * We don't need to check for buffer overflow any more.
570a5229c74SGary Mills * The pattern has already been copied to an internal buffer.
571a5229c74SGary Mills */
572a5229c74SGary Mills while ((a = qpatnext->w_at), (c = (qpatnext++)->w_wc) != EOS) {
573a5229c74SGary Mills switch (c) {
574a5229c74SGary Mills case LBRACKET:
575a5229c74SGary Mills if (a != 0) {
576a5229c74SGary Mills bufnext->w_at = a;
577a5229c74SGary Mills (bufnext++)->w_wc = c;
578a5229c74SGary Mills break;
579a5229c74SGary Mills }
580a5229c74SGary Mills a = qpatnext->w_at;
581a5229c74SGary Mills c = qpatnext->w_wc;
582a5229c74SGary Mills if (a == 0 && c == NOT)
583a5229c74SGary Mills ++qpatnext;
584a5229c74SGary Mills if (qpatnext->w_wc == EOS ||
585a5229c74SGary Mills g_strchr(qpatnext+1, RBRACKET) == NULL) {
586a5229c74SGary Mills bufnext->w_at = 0;
587a5229c74SGary Mills (bufnext++)->w_wc = LBRACKET;
588a5229c74SGary Mills if (a == 0 && c == NOT)
589a5229c74SGary Mills --qpatnext;
590a5229c74SGary Mills break;
591a5229c74SGary Mills }
592a5229c74SGary Mills bufnext->w_at = M_QUOTE;
593a5229c74SGary Mills (bufnext++)->w_wc = M_SET;
594a5229c74SGary Mills if (a == 0 && c == NOT) {
595a5229c74SGary Mills bufnext->w_at = M_QUOTE;
596a5229c74SGary Mills (bufnext++)->w_wc = M_NOT;
597a5229c74SGary Mills }
598a5229c74SGary Mills a = qpatnext->w_at;
599a5229c74SGary Mills c = (qpatnext++)->w_wc;
600a5229c74SGary Mills do {
601a5229c74SGary Mills if (a == 0 && c == LBRACKET &&
602a5229c74SGary Mills qpatnext->w_wc == COLON) {
603a5229c74SGary Mills do {
604a5229c74SGary Mills err = g_charclass(&qpatnext,
605a5229c74SGary Mills &bufnext);
606a5229c74SGary Mills if (err)
607a5229c74SGary Mills break;
608a5229c74SGary Mills a = qpatnext->w_at;
609a5229c74SGary Mills c = (qpatnext++)->w_wc;
610a5229c74SGary Mills } while (a == 0 && c == LBRACKET &&
611a5229c74SGary Mills qpatnext->w_wc == COLON);
612a5229c74SGary Mills if (err == -1 &&
613a5229c74SGary Mills !(pglob->gl_flags & GLOB_NOCHECK))
614a5229c74SGary Mills return (GLOB_NOMATCH);
615a5229c74SGary Mills if (a == 0 && c == RBRACKET)
616a5229c74SGary Mills break;
617a5229c74SGary Mills }
618a5229c74SGary Mills bufnext->w_at = a;
619a5229c74SGary Mills (bufnext++)->w_wc = c;
620a5229c74SGary Mills if (qpatnext->w_at == 0 &&
621a5229c74SGary Mills qpatnext->w_wc == RANGE) {
622a5229c74SGary Mills a = qpatnext[1].w_at;
623a5229c74SGary Mills c = qpatnext[1].w_wc;
624a5229c74SGary Mills if (qpatnext[1].w_at != 0 ||
625a5229c74SGary Mills qpatnext[1].w_wc != RBRACKET) {
626a5229c74SGary Mills bufnext->w_at = M_QUOTE;
627a5229c74SGary Mills (bufnext++)->w_wc = M_RNG;
628a5229c74SGary Mills bufnext->w_at = a;
629a5229c74SGary Mills (bufnext++)->w_wc = c;
630a5229c74SGary Mills qpatnext += 2;
631a5229c74SGary Mills }
632a5229c74SGary Mills }
633a5229c74SGary Mills a = qpatnext->w_at;
634a5229c74SGary Mills c = (qpatnext++)->w_wc;
635a5229c74SGary Mills } while (a != 0 || c != RBRACKET);
636a5229c74SGary Mills pglob->gl_flags |= GLOB_MAGCHAR;
637a5229c74SGary Mills bufnext->w_at = M_QUOTE;
638a5229c74SGary Mills (bufnext++)->w_wc = M_END;
639a5229c74SGary Mills break;
640a5229c74SGary Mills case QUESTION:
641a5229c74SGary Mills if (a != 0) {
642a5229c74SGary Mills bufnext->w_at = a;
643a5229c74SGary Mills (bufnext++)->w_wc = c;
644a5229c74SGary Mills break;
645a5229c74SGary Mills }
646a5229c74SGary Mills pglob->gl_flags |= GLOB_MAGCHAR;
647a5229c74SGary Mills bufnext->w_at = M_QUOTE;
648a5229c74SGary Mills (bufnext++)->w_wc = M_ONE;
649a5229c74SGary Mills break;
650a5229c74SGary Mills case STAR:
651a5229c74SGary Mills if (a != 0) {
652a5229c74SGary Mills bufnext->w_at = a;
653a5229c74SGary Mills (bufnext++)->w_wc = c;
654a5229c74SGary Mills break;
655a5229c74SGary Mills }
656a5229c74SGary Mills pglob->gl_flags |= GLOB_MAGCHAR;
657a5229c74SGary Mills /*
658a5229c74SGary Mills * collapse adjacent stars to one,
659a5229c74SGary Mills * to avoid exponential behavior
660a5229c74SGary Mills */
661a5229c74SGary Mills if (bufnext == patbuf ||
662a5229c74SGary Mills bufnext[-1].w_at != M_QUOTE ||
663a5229c74SGary Mills bufnext[-1].w_wc != M_ALL) {
664a5229c74SGary Mills bufnext->w_at = M_QUOTE;
665a5229c74SGary Mills (bufnext++)->w_wc = M_ALL;
666a5229c74SGary Mills }
667a5229c74SGary Mills break;
668a5229c74SGary Mills default:
669a5229c74SGary Mills bufnext->w_at = a;
670a5229c74SGary Mills (bufnext++)->w_wc = c;
671a5229c74SGary Mills break;
672a5229c74SGary Mills }
673a5229c74SGary Mills }
674a5229c74SGary Mills bufnext->w_at = 0;
675a5229c74SGary Mills bufnext->w_wc = EOS;
676a5229c74SGary Mills
677*79d022daSYuri Pankov if ((err = glob1(patbuf, patbuf+PATH_MAX-1, pglob, limitp, errfunc))
678a5229c74SGary Mills != 0)
679a5229c74SGary Mills return (err);
680a5229c74SGary Mills
681a5229c74SGary Mills /*
682a5229c74SGary Mills * If there was no match we are going to append the pattern
683a5229c74SGary Mills * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
684a5229c74SGary Mills * and the pattern did not contain any magic characters
685a5229c74SGary Mills * GLOB_NOMAGIC is there just for compatibility with csh.
686a5229c74SGary Mills */
687a5229c74SGary Mills if (pglob->gl_pathc == oldpathc) {
688a5229c74SGary Mills if ((pglob->gl_flags & GLOB_NOCHECK) ||
689a5229c74SGary Mills ((pglob->gl_flags & GLOB_NOMAGIC) &&
690a5229c74SGary Mills !(pglob->gl_flags & GLOB_MAGCHAR)))
691a5229c74SGary Mills return (globextend(pattern, pglob, limitp, NULL));
692a5229c74SGary Mills else
693a5229c74SGary Mills return (GLOB_NOMATCH);
694a5229c74SGary Mills }
695a5229c74SGary Mills if (!(pglob->gl_flags & GLOB_NOSORT)) {
696a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
697a5229c74SGary Mills /* Keep the paths and stat info synced during sort */
698a5229c74SGary Mills struct glob_path_stat *path_stat;
699a5229c74SGary Mills int i;
700a5229c74SGary Mills int n = pglob->gl_pathc - oldpathc;
701a5229c74SGary Mills int o = pglob->gl_offs + oldpathc;
702a5229c74SGary Mills
703a5229c74SGary Mills if ((path_stat = calloc(n, sizeof (*path_stat))) ==
704a5229c74SGary Mills NULL)
705a5229c74SGary Mills return (GLOB_NOSPACE);
706a5229c74SGary Mills for (i = 0; i < n; i++) {
707a5229c74SGary Mills path_stat[i].gps_path = pglob->gl_pathv[o + i];
708a5229c74SGary Mills path_stat[i].gps_stat = pglob->gl_statv[o + i];
709a5229c74SGary Mills }
710a5229c74SGary Mills qsort(path_stat, n, sizeof (*path_stat), compare_gps);
711a5229c74SGary Mills for (i = 0; i < n; i++) {
712a5229c74SGary Mills pglob->gl_pathv[o + i] = path_stat[i].gps_path;
713a5229c74SGary Mills pglob->gl_statv[o + i] = path_stat[i].gps_stat;
714a5229c74SGary Mills }
715a5229c74SGary Mills free(path_stat);
716a5229c74SGary Mills } else {
717a5229c74SGary Mills qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
718a5229c74SGary Mills pglob->gl_pathc - oldpathc, sizeof (char *),
719a5229c74SGary Mills compare);
720a5229c74SGary Mills }
721a5229c74SGary Mills }
722a5229c74SGary Mills return (0);
723a5229c74SGary Mills }
724a5229c74SGary Mills
725a5229c74SGary Mills static int
compare(const void * p,const void * q)726a5229c74SGary Mills compare(const void *p, const void *q)
727a5229c74SGary Mills {
728a5229c74SGary Mills return (strcmp(*(char **)p, *(char **)q));
729a5229c74SGary Mills }
730a5229c74SGary Mills
731a5229c74SGary Mills static int
compare_gps(const void * _p,const void * _q)732a5229c74SGary Mills compare_gps(const void *_p, const void *_q)
733a5229c74SGary Mills {
734a5229c74SGary Mills const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
735a5229c74SGary Mills const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
736a5229c74SGary Mills
737a5229c74SGary Mills return (strcmp(p->gps_path, q->gps_path));
738a5229c74SGary Mills }
739a5229c74SGary Mills
740a5229c74SGary Mills static int
glob1(wcat_t * pattern,wcat_t * pattern_last,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))741a5229c74SGary Mills glob1(wcat_t *pattern, wcat_t *pattern_last, glob_t *pglob,
742a5229c74SGary Mills struct glob_lim *limitp, int (*errfunc)(const char *, int))
743a5229c74SGary Mills {
744*79d022daSYuri Pankov wcat_t pathbuf[PATH_MAX];
745a5229c74SGary Mills
746a5229c74SGary Mills /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
747a5229c74SGary Mills if (pattern->w_wc == EOS)
748a5229c74SGary Mills return (0);
749*79d022daSYuri Pankov return (glob2(pathbuf, pathbuf+PATH_MAX-1,
750*79d022daSYuri Pankov pathbuf, pathbuf+PATH_MAX-1,
751a5229c74SGary Mills pattern, pattern_last, pglob, limitp, errfunc));
752a5229c74SGary Mills }
753a5229c74SGary Mills
754a5229c74SGary Mills /*
755a5229c74SGary Mills * The functions glob2 and glob3 are mutually recursive; there is one level
756a5229c74SGary Mills * of recursion for each segment in the pattern that contains one or more
757a5229c74SGary Mills * meta characters.
758a5229c74SGary Mills */
759a5229c74SGary Mills static int
glob2(wcat_t * pathbuf,wcat_t * pathbuf_last,wcat_t * pathend,wcat_t * pathend_last,wcat_t * pattern,wcat_t * pattern_last,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))760a5229c74SGary Mills glob2(wcat_t *pathbuf, wcat_t *pathbuf_last, wcat_t *pathend,
761a5229c74SGary Mills wcat_t *pathend_last, wcat_t *pattern, wcat_t *pattern_last,
762a5229c74SGary Mills glob_t *pglob, struct glob_lim *limitp, int (*errfunc)(const char *, int))
763a5229c74SGary Mills {
764a5229c74SGary Mills struct stat sb;
765a5229c74SGary Mills wcat_t *p, *q;
766a5229c74SGary Mills int anymeta;
767a5229c74SGary Mills
768a5229c74SGary Mills /*
769a5229c74SGary Mills * Loop over pattern segments until end of pattern or until
770a5229c74SGary Mills * segment with meta character found.
771a5229c74SGary Mills */
772a5229c74SGary Mills for (anymeta = 0; ; ) {
773a5229c74SGary Mills if (pattern->w_wc == EOS) { /* End of pattern? */
774a5229c74SGary Mills pathend->w_at = 0;
775a5229c74SGary Mills pathend->w_wc = EOS;
776a5229c74SGary Mills
777a5229c74SGary Mills if ((pglob->gl_flags & GLOB_LIMIT) &&
778a5229c74SGary Mills limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
779a5229c74SGary Mills errno = 0;
780a5229c74SGary Mills pathend->w_at = 0;
781a5229c74SGary Mills (pathend++)->w_wc = SEP;
782a5229c74SGary Mills pathend->w_at = 0;
783a5229c74SGary Mills pathend->w_wc = EOS;
784a5229c74SGary Mills return (GLOB_NOSPACE);
785a5229c74SGary Mills }
786a5229c74SGary Mills if (g_lstat(pathbuf, &sb, pglob))
787a5229c74SGary Mills return (0);
788a5229c74SGary Mills
789a5229c74SGary Mills if (((pglob->gl_flags & GLOB_MARK) &&
790a5229c74SGary Mills (pathend[-1].w_at != 0 ||
791a5229c74SGary Mills pathend[-1].w_wc != SEP)) &&
792a5229c74SGary Mills (S_ISDIR(sb.st_mode) ||
793a5229c74SGary Mills (S_ISLNK(sb.st_mode) &&
794a5229c74SGary Mills (g_stat(pathbuf, &sb, pglob) == 0) &&
795a5229c74SGary Mills S_ISDIR(sb.st_mode)))) {
796a5229c74SGary Mills if (pathend+1 > pathend_last)
797a5229c74SGary Mills return (GLOB_NOSPACE);
798a5229c74SGary Mills pathend->w_at = 0;
799a5229c74SGary Mills (pathend++)->w_wc = SEP;
800a5229c74SGary Mills pathend->w_at = 0;
801a5229c74SGary Mills pathend->w_wc = EOS;
802a5229c74SGary Mills }
803a5229c74SGary Mills ++pglob->gl_matchc;
804a5229c74SGary Mills return (globextend(pathbuf, pglob, limitp, &sb));
805a5229c74SGary Mills }
806a5229c74SGary Mills
807a5229c74SGary Mills /* Find end of next segment, copy tentatively to pathend. */
808a5229c74SGary Mills q = pathend;
809a5229c74SGary Mills p = pattern;
810a5229c74SGary Mills while (p->w_wc != EOS && p->w_wc != SEP) {
811a5229c74SGary Mills if (ismeta(*p))
812a5229c74SGary Mills anymeta = 1;
813a5229c74SGary Mills if (q+1 > pathend_last)
814a5229c74SGary Mills return (GLOB_NOSPACE);
815a5229c74SGary Mills *q++ = *p++;
816a5229c74SGary Mills }
817a5229c74SGary Mills
818a5229c74SGary Mills if (!anymeta) { /* No expansion, do next segment. */
819a5229c74SGary Mills pathend = q;
820a5229c74SGary Mills pattern = p;
821a5229c74SGary Mills while (pattern->w_wc == SEP) {
822a5229c74SGary Mills if (pathend+1 > pathend_last)
823a5229c74SGary Mills return (GLOB_NOSPACE);
824a5229c74SGary Mills *pathend++ = *pattern++;
825a5229c74SGary Mills }
826a5229c74SGary Mills } else {
827a5229c74SGary Mills /* Need expansion, recurse. */
828a5229c74SGary Mills return (glob3(pathbuf, pathbuf_last, pathend,
829a5229c74SGary Mills pathend_last, pattern, p, pattern_last,
830a5229c74SGary Mills pglob, limitp, errfunc));
831a5229c74SGary Mills }
832a5229c74SGary Mills }
833a5229c74SGary Mills /* NOTREACHED */
834a5229c74SGary Mills }
835a5229c74SGary Mills
836a5229c74SGary Mills static int
glob3(wcat_t * pathbuf,wcat_t * pathbuf_last,wcat_t * pathend,wcat_t * pathend_last,wcat_t * pattern,wcat_t * restpattern,wcat_t * restpattern_last,glob_t * pglob,struct glob_lim * limitp,int (* errfunc)(const char *,int))837a5229c74SGary Mills glob3(wcat_t *pathbuf, wcat_t *pathbuf_last, wcat_t *pathend,
838a5229c74SGary Mills wcat_t *pathend_last, wcat_t *pattern, wcat_t *restpattern,
839a5229c74SGary Mills wcat_t *restpattern_last, glob_t *pglob, struct glob_lim *limitp,
840a5229c74SGary Mills int (*errfunc)(const char *, int))
841a5229c74SGary Mills {
842a5229c74SGary Mills struct dirent *dp;
843a5229c74SGary Mills DIR *dirp;
844a5229c74SGary Mills int err;
845*79d022daSYuri Pankov char buf[PATH_MAX];
846a5229c74SGary Mills
847a5229c74SGary Mills /*
848a5229c74SGary Mills * The readdirfunc declaration can't be prototyped, because it is
849a5229c74SGary Mills * assigned, below, to two functions which are prototyped in glob.h
850a5229c74SGary Mills * and dirent.h as taking pointers to differently typed opaque
851a5229c74SGary Mills * structures.
852a5229c74SGary Mills */
853a5229c74SGary Mills struct dirent *(*readdirfunc)(void *);
854a5229c74SGary Mills
855a5229c74SGary Mills if (pathend > pathend_last)
856a5229c74SGary Mills return (GLOB_NOSPACE);
857a5229c74SGary Mills pathend->w_at = 0;
858a5229c74SGary Mills pathend->w_wc = EOS;
859a5229c74SGary Mills errno = 0;
860a5229c74SGary Mills
861a5229c74SGary Mills if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
862a5229c74SGary Mills /* TODO: don't call for ENOENT or ENOTDIR? */
863a5229c74SGary Mills if (errfunc) {
864a5229c74SGary Mills if (g_Ctoc(pathbuf, buf, sizeof (buf)))
865a5229c74SGary Mills return (GLOB_ABORTED);
866a5229c74SGary Mills if (errfunc(buf, errno) ||
867a5229c74SGary Mills pglob->gl_flags & GLOB_ERR)
8687c478bd9Sstevel@tonic-gate return (GLOB_ABORTED);
8697c478bd9Sstevel@tonic-gate }
870a5229c74SGary Mills return (0);
871a5229c74SGary Mills }
8727c478bd9Sstevel@tonic-gate
873a5229c74SGary Mills err = 0;
874a5229c74SGary Mills
875a5229c74SGary Mills /* Search directory for matching names. */
876a5229c74SGary Mills if (pglob->gl_flags & GLOB_ALTDIRFUNC)
877a5229c74SGary Mills readdirfunc = pglob->gl_readdir;
8787c478bd9Sstevel@tonic-gate else
879a5229c74SGary Mills readdirfunc = (struct dirent *(*)(void *))readdir;
880a5229c74SGary Mills while ((dp = (*readdirfunc)(dirp))) {
881a5229c74SGary Mills char *sc;
882a5229c74SGary Mills wcat_t *dc;
883a5229c74SGary Mills int n;
884a5229c74SGary Mills int lensc;
885a5229c74SGary Mills wchar_t w;
886a5229c74SGary Mills
887a5229c74SGary Mills if ((pglob->gl_flags & GLOB_LIMIT) &&
888a5229c74SGary Mills limitp->glim_readdir++ >= GLOB_LIMIT_READDIR) {
889a5229c74SGary Mills errno = 0;
890a5229c74SGary Mills pathend->w_at = 0;
891a5229c74SGary Mills (pathend++)->w_wc = SEP;
892a5229c74SGary Mills pathend->w_at = 0;
893a5229c74SGary Mills pathend->w_wc = EOS;
894a5229c74SGary Mills err = GLOB_NOSPACE;
895a5229c74SGary Mills break;
8967c478bd9Sstevel@tonic-gate }
897a5229c74SGary Mills
898a5229c74SGary Mills /* Initial DOT must be matched literally. */
899a5229c74SGary Mills if (dp->d_name[0] == DOT && pattern->w_wc != DOT)
900a5229c74SGary Mills continue;
901a5229c74SGary Mills dc = pathend;
902a5229c74SGary Mills sc = dp->d_name;
903a5229c74SGary Mills lensc = strlen(sc) + 1;
904a5229c74SGary Mills while (dc < pathend_last) {
905a5229c74SGary Mills if ((n = mbtowc(&w, sc, lensc)) <= 0) {
906a5229c74SGary Mills sc += 1;
907a5229c74SGary Mills lensc -= 1;
908a5229c74SGary Mills dc->w_at = 0;
909a5229c74SGary Mills dc->w_wc = EOS;
910a5229c74SGary Mills } else {
911a5229c74SGary Mills sc += n;
912a5229c74SGary Mills lensc -= n;
913a5229c74SGary Mills dc->w_at = 0;
914a5229c74SGary Mills dc->w_wc = w;
915a5229c74SGary Mills }
916a5229c74SGary Mills dc++;
917a5229c74SGary Mills if (n <= 0)
918a5229c74SGary Mills break;
919a5229c74SGary Mills }
920a5229c74SGary Mills if (dc >= pathend_last) {
921a5229c74SGary Mills dc->w_at = 0;
922a5229c74SGary Mills dc->w_wc = EOS;
923a5229c74SGary Mills err = GLOB_NOSPACE;
924a5229c74SGary Mills break;
925a5229c74SGary Mills }
926a5229c74SGary Mills if (n < 0) {
927a5229c74SGary Mills err = GLOB_NOMATCH;
928a5229c74SGary Mills break;
929a5229c74SGary Mills }
930a5229c74SGary Mills
931*79d022daSYuri Pankov if (!match(pathend, pattern, restpattern)) {
932a5229c74SGary Mills pathend->w_at = 0;
933a5229c74SGary Mills pathend->w_wc = EOS;
934a5229c74SGary Mills continue;
935a5229c74SGary Mills }
936a5229c74SGary Mills err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
937a5229c74SGary Mills restpattern, restpattern_last, pglob, limitp,
938a5229c74SGary Mills errfunc);
939a5229c74SGary Mills if (err)
940a5229c74SGary Mills break;
941a5229c74SGary Mills }
942a5229c74SGary Mills
943a5229c74SGary Mills if (pglob->gl_flags & GLOB_ALTDIRFUNC)
944a5229c74SGary Mills (*pglob->gl_closedir)(dirp);
945a5229c74SGary Mills else
946a5229c74SGary Mills (void) closedir(dirp);
947a5229c74SGary Mills return (err);
948a5229c74SGary Mills }
949a5229c74SGary Mills
950a5229c74SGary Mills
951a5229c74SGary Mills /*
952a5229c74SGary Mills * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
953a5229c74SGary Mills * add the new item, and update gl_pathc. Avoids excessive reallocation
954a5229c74SGary Mills * by doubling the number of elements each time. Uses gl_pathn to contain
955a5229c74SGary Mills * the number.
956a5229c74SGary Mills *
957a5229c74SGary Mills * Return 0 if new item added, error code if memory couldn't be allocated.
958a5229c74SGary Mills *
959a5229c74SGary Mills * Invariant of the glob_t structure:
960a5229c74SGary Mills * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
961a5229c74SGary Mills * gl_pathv points to (gl_offs + gl_pathc + 1) items.
962a5229c74SGary Mills */
963a5229c74SGary Mills static int
globextend(const wcat_t * path,glob_t * pglob,struct glob_lim * limitp,struct stat * sb)964a5229c74SGary Mills globextend(const wcat_t *path, glob_t *pglob, struct glob_lim *limitp,
965a5229c74SGary Mills struct stat *sb)
966a5229c74SGary Mills {
967a5229c74SGary Mills char **pathv;
968a5229c74SGary Mills ssize_t i;
969a5229c74SGary Mills size_t allocn, newn, len;
970a5229c74SGary Mills char *copy = NULL;
971a5229c74SGary Mills const wcat_t *p;
972a5229c74SGary Mills struct stat **statv;
973a5229c74SGary Mills char junk[MB_LEN_MAX];
974a5229c74SGary Mills int n;
975a5229c74SGary Mills
976a5229c74SGary Mills allocn = pglob->gl_pathn;
977a5229c74SGary Mills newn = 2 + pglob->gl_pathc + pglob->gl_offs;
978a5229c74SGary Mills
979a5229c74SGary Mills if (newn <= allocn) {
980a5229c74SGary Mills pathv = pglob->gl_pathv;
981a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0)
982a5229c74SGary Mills statv = pglob->gl_statv;
983a5229c74SGary Mills } else {
984a5229c74SGary Mills if (allocn == 0)
985a5229c74SGary Mills allocn = pglob->gl_offs + INITIAL;
986a5229c74SGary Mills allocn *= 2;
987a5229c74SGary Mills if (pglob->gl_offs >= INT_MAX ||
988a5229c74SGary Mills pglob->gl_pathc >= INT_MAX ||
989a5229c74SGary Mills allocn >= INT_MAX ||
990a5229c74SGary Mills SIZE_MAX / sizeof (*pathv) <= allocn ||
991a5229c74SGary Mills SIZE_MAX / sizeof (*statv) <= allocn) {
992a5229c74SGary Mills nospace:
993a5229c74SGary Mills for (i = pglob->gl_offs; i < (ssize_t)(newn - 2);
994a5229c74SGary Mills i++) {
995a5229c74SGary Mills if (pglob->gl_pathv && pglob->gl_pathv[i])
996a5229c74SGary Mills free(pglob->gl_pathv[i]);
997a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
998a5229c74SGary Mills pglob->gl_statv && pglob->gl_statv[i])
999a5229c74SGary Mills free(pglob->gl_statv[i]);
1000a5229c74SGary Mills }
1001a5229c74SGary Mills free(pglob->gl_pathv);
1002a5229c74SGary Mills pglob->gl_pathv = NULL;
1003*79d022daSYuri Pankov if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
1004a5229c74SGary Mills free(pglob->gl_statv);
1005a5229c74SGary Mills pglob->gl_statv = NULL;
1006a5229c74SGary Mills }
1007a5229c74SGary Mills return (GLOB_NOSPACE);
1008a5229c74SGary Mills }
1009a5229c74SGary Mills limitp->glim_malloc += allocn * sizeof (*pathv);
1010*79d022daSYuri Pankov pathv = reallocarray(pglob->gl_pathv, allocn, sizeof (*pathv));
1011a5229c74SGary Mills if (pathv == NULL)
1012a5229c74SGary Mills goto nospace;
1013a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
1014a5229c74SGary Mills limitp->glim_malloc += allocn * sizeof (*statv);
1015*79d022daSYuri Pankov statv = reallocarray(pglob->gl_statv, allocn,
1016*79d022daSYuri Pankov sizeof (*statv));
1017a5229c74SGary Mills if (statv == NULL)
1018a5229c74SGary Mills goto nospace;
1019a5229c74SGary Mills }
1020a5229c74SGary Mills }
1021a5229c74SGary Mills pglob->gl_pathn = allocn;
1022a5229c74SGary Mills
1023a5229c74SGary Mills if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
1024a5229c74SGary Mills /* first time around -- clear initial gl_offs items */
1025a5229c74SGary Mills pathv += pglob->gl_offs;
1026a5229c74SGary Mills for (i = pglob->gl_offs; --i >= 0; )
1027a5229c74SGary Mills *--pathv = NULL;
1028a5229c74SGary Mills }
1029a5229c74SGary Mills pglob->gl_pathv = pathv;
1030a5229c74SGary Mills
1031a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
1032a5229c74SGary Mills if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
1033a5229c74SGary Mills /* first time around -- clear initial gl_offs items */
1034a5229c74SGary Mills statv += pglob->gl_offs;
1035a5229c74SGary Mills for (i = pglob->gl_offs; --i >= 0; )
1036a5229c74SGary Mills *--statv = NULL;
1037a5229c74SGary Mills }
1038a5229c74SGary Mills pglob->gl_statv = statv;
1039a5229c74SGary Mills if (sb == NULL)
1040a5229c74SGary Mills statv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1041a5229c74SGary Mills else {
1042a5229c74SGary Mills limitp->glim_malloc += sizeof (**statv);
1043a5229c74SGary Mills if ((statv[pglob->gl_offs + pglob->gl_pathc] =
1044a5229c74SGary Mills malloc(sizeof (**statv))) == NULL)
1045a5229c74SGary Mills goto copy_error;
1046a5229c74SGary Mills (void) memcpy(statv[pglob->gl_offs + pglob->gl_pathc],
1047a5229c74SGary Mills sb, sizeof (*sb));
1048a5229c74SGary Mills }
1049a5229c74SGary Mills statv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL;
1050a5229c74SGary Mills }
1051a5229c74SGary Mills
1052a5229c74SGary Mills len = MB_LEN_MAX;
1053a5229c74SGary Mills p = path;
1054a5229c74SGary Mills while ((n = wctomb(junk, p->w_wc)) > 0) {
1055a5229c74SGary Mills len += n;
1056a5229c74SGary Mills if ((p++)->w_wc == EOS)
1057a5229c74SGary Mills break;
1058a5229c74SGary Mills }
1059a5229c74SGary Mills if (n < 0)
1060a5229c74SGary Mills return (GLOB_NOMATCH);
1061a5229c74SGary Mills
1062a5229c74SGary Mills limitp->glim_malloc += len;
1063a5229c74SGary Mills if ((copy = malloc(len)) != NULL) {
1064a5229c74SGary Mills if (g_Ctoc(path, copy, len)) {
1065a5229c74SGary Mills free(copy);
1066a5229c74SGary Mills return (GLOB_NOSPACE);
1067a5229c74SGary Mills }
1068a5229c74SGary Mills pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
1069a5229c74SGary Mills }
1070a5229c74SGary Mills pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1071a5229c74SGary Mills
1072a5229c74SGary Mills if ((pglob->gl_flags & GLOB_LIMIT) &&
1073a5229c74SGary Mills limitp->glim_malloc >= GLOB_LIMIT_MALLOC) {
1074a5229c74SGary Mills errno = 0;
1075a5229c74SGary Mills return (GLOB_NOSPACE);
1076a5229c74SGary Mills }
1077a5229c74SGary Mills copy_error:
1078a5229c74SGary Mills return (copy == NULL ? GLOB_NOSPACE : 0);
1079a5229c74SGary Mills }
1080a5229c74SGary Mills
1081a5229c74SGary Mills
1082a5229c74SGary Mills /*
1083a5229c74SGary Mills * pattern matching function for filenames. Each occurrence of the *
1084*79d022daSYuri Pankov * pattern causes an iteration.
1085*79d022daSYuri Pankov *
1086*79d022daSYuri Pankov * Note, this function differs from the original as per the discussion
1087*79d022daSYuri Pankov * here: https://research.swtch.com/glob
1088*79d022daSYuri Pankov *
1089*79d022daSYuri Pankov * Basically we removed the recursion and made it use the algorithm
1090*79d022daSYuri Pankov * from Russ Cox to not go quadratic on cases like a file called
1091*79d022daSYuri Pankov * ("a" x 100) . "x" matched against a pattern like "a*a*a*a*a*a*a*y".
1092a5229c74SGary Mills */
1093a5229c74SGary Mills static int
match(wcat_t * name,wcat_t * pat,wcat_t * patend)1094*79d022daSYuri Pankov match(wcat_t *name, wcat_t *pat, wcat_t *patend)
1095a5229c74SGary Mills {
1096a5229c74SGary Mills int ok, negate_range;
1097a5229c74SGary Mills wcat_t c, k;
1098*79d022daSYuri Pankov wcat_t *nextp = NULL;
1099*79d022daSYuri Pankov wcat_t *nextn = NULL;
1100a5229c74SGary Mills
1101*79d022daSYuri Pankov loop:
1102a5229c74SGary Mills while (pat < patend) {
1103a5229c74SGary Mills c = *pat++;
1104a5229c74SGary Mills switch (c.w_wc) {
1105a5229c74SGary Mills case M_ALL:
1106a5229c74SGary Mills if (c.w_at != M_QUOTE) {
1107a5229c74SGary Mills k = *name++;
1108a5229c74SGary Mills if (k.w_at != c.w_at || k.w_wc != c.w_wc)
1109a5229c74SGary Mills return (0);
1110a5229c74SGary Mills break;
1111a5229c74SGary Mills }
1112a5229c74SGary Mills while (pat < patend && pat->w_at == M_QUOTE &&
1113a5229c74SGary Mills pat->w_wc == M_ALL)
1114a5229c74SGary Mills pat++; /* eat consecutive '*' */
1115a5229c74SGary Mills if (pat == patend)
1116a5229c74SGary Mills return (1);
1117*79d022daSYuri Pankov if (name->w_wc == EOS)
1118a5229c74SGary Mills return (0);
1119*79d022daSYuri Pankov nextn = name + 1;
1120*79d022daSYuri Pankov nextp = pat - 1;
1121*79d022daSYuri Pankov break;
1122a5229c74SGary Mills case M_ONE:
1123a5229c74SGary Mills if (c.w_at != M_QUOTE) {
1124a5229c74SGary Mills k = *name++;
1125a5229c74SGary Mills if (k.w_at != c.w_at || k.w_wc != c.w_wc)
1126*79d022daSYuri Pankov goto fail;
1127a5229c74SGary Mills break;
1128a5229c74SGary Mills }
1129a5229c74SGary Mills if ((name++)->w_wc == EOS)
1130*79d022daSYuri Pankov goto fail;
1131a5229c74SGary Mills break;
1132a5229c74SGary Mills case M_SET:
1133a5229c74SGary Mills if (c.w_at != M_QUOTE) {
1134a5229c74SGary Mills k = *name++;
1135a5229c74SGary Mills if (k.w_at != c.w_at || k.w_wc != c.w_wc)
1136*79d022daSYuri Pankov goto fail;
1137a5229c74SGary Mills break;
1138a5229c74SGary Mills }
1139a5229c74SGary Mills ok = 0;
1140a5229c74SGary Mills if ((k = *name++).w_wc == EOS)
1141*79d022daSYuri Pankov goto fail;
1142a5229c74SGary Mills if ((negate_range = (pat->w_at == M_QUOTE &&
1143a5229c74SGary Mills pat->w_wc == M_NOT)) != 0)
1144a5229c74SGary Mills ++pat;
1145a5229c74SGary Mills while (((c = *pat++).w_at != M_QUOTE) ||
1146a5229c74SGary Mills c.w_wc != M_END) {
1147a5229c74SGary Mills if (c.w_at == M_QUOTE && c.w_wc == M_CLASS) {
1148a5229c74SGary Mills wcat_t cc;
1149a5229c74SGary Mills
1150a5229c74SGary Mills cc.w_at = pat->w_at;
1151a5229c74SGary Mills cc.w_wc = pat->w_wc;
1152a5229c74SGary Mills if (iswctype(k.w_wc, cc.w_wc))
1153a5229c74SGary Mills ok = 1;
1154a5229c74SGary Mills ++pat;
1155a5229c74SGary Mills }
1156a5229c74SGary Mills if (pat->w_at == M_QUOTE &&
1157a5229c74SGary Mills pat->w_wc == M_RNG) {
1158a5229c74SGary Mills if (c.w_wc <= k.w_wc &&
1159a5229c74SGary Mills k.w_wc <= pat[1].w_wc)
1160a5229c74SGary Mills ok = 1;
1161a5229c74SGary Mills pat += 2;
1162a5229c74SGary Mills } else if (c.w_wc == k.w_wc)
1163a5229c74SGary Mills ok = 1;
1164a5229c74SGary Mills }
1165a5229c74SGary Mills if (ok == negate_range)
1166*79d022daSYuri Pankov goto fail;
1167a5229c74SGary Mills break;
1168a5229c74SGary Mills default:
1169a5229c74SGary Mills k = *name++;
1170a5229c74SGary Mills if (k.w_at != c.w_at || k.w_wc != c.w_wc)
1171*79d022daSYuri Pankov goto fail;
1172a5229c74SGary Mills break;
1173a5229c74SGary Mills }
1174a5229c74SGary Mills }
1175*79d022daSYuri Pankov if (name->w_wc == EOS)
1176*79d022daSYuri Pankov return (1);
1177*79d022daSYuri Pankov
1178*79d022daSYuri Pankov fail:
1179*79d022daSYuri Pankov if (nextn) {
1180*79d022daSYuri Pankov pat = nextp;
1181*79d022daSYuri Pankov name = nextn;
1182*79d022daSYuri Pankov goto loop;
1183*79d022daSYuri Pankov }
1184*79d022daSYuri Pankov return (0);
1185a5229c74SGary Mills }
1186a5229c74SGary Mills
1187a5229c74SGary Mills /*
1188a5229c74SGary Mills * Extended globfree() function, selected by #pragma redefine_extname
1189a5229c74SGary Mills * in glob.h with the external name _globfree_ext() .
1190a5229c74SGary Mills */
1191a5229c74SGary Mills void
_globfree_ext(glob_t * pglob)1192a5229c74SGary Mills _globfree_ext(glob_t *pglob)
1193a5229c74SGary Mills {
1194a5229c74SGary Mills int i;
1195a5229c74SGary Mills char **pp;
1196a5229c74SGary Mills
1197a5229c74SGary Mills if (pglob->gl_pathv != NULL) {
1198a5229c74SGary Mills pp = pglob->gl_pathv + pglob->gl_offs;
1199a5229c74SGary Mills for (i = pglob->gl_pathc; i--; ++pp)
1200a5229c74SGary Mills free(*pp);
1201a5229c74SGary Mills free(pglob->gl_pathv);
1202a5229c74SGary Mills pglob->gl_pathv = NULL;
1203a5229c74SGary Mills }
1204a5229c74SGary Mills if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
1205a5229c74SGary Mills pglob->gl_statv != NULL) {
1206a5229c74SGary Mills for (i = 0; i < pglob->gl_pathc; i++) {
1207a5229c74SGary Mills free(pglob->gl_statv[i]);
1208a5229c74SGary Mills }
1209a5229c74SGary Mills free(pglob->gl_statv);
1210a5229c74SGary Mills pglob->gl_statv = NULL;
1211a5229c74SGary Mills }
1212a5229c74SGary Mills }
1213a5229c74SGary Mills
1214a5229c74SGary Mills static DIR *
g_opendir(wcat_t * str,glob_t * pglob)1215a5229c74SGary Mills g_opendir(wcat_t *str, glob_t *pglob)
1216a5229c74SGary Mills {
1217*79d022daSYuri Pankov char buf[PATH_MAX];
1218a5229c74SGary Mills
1219a5229c74SGary Mills if (str->w_wc == EOS)
1220a5229c74SGary Mills (void) strlcpy(buf, ".", sizeof (buf));
1221a5229c74SGary Mills else {
1222a5229c74SGary Mills if (g_Ctoc(str, buf, sizeof (buf)))
1223a5229c74SGary Mills return (NULL);
1224a5229c74SGary Mills }
1225a5229c74SGary Mills
1226a5229c74SGary Mills if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1227a5229c74SGary Mills return ((*pglob->gl_opendir)(buf));
1228a5229c74SGary Mills
1229a5229c74SGary Mills return (opendir(buf));
1230a5229c74SGary Mills }
1231a5229c74SGary Mills
1232a5229c74SGary Mills static int
g_lstat(wcat_t * fn,struct stat * sb,glob_t * pglob)1233a5229c74SGary Mills g_lstat(wcat_t *fn, struct stat *sb, glob_t *pglob)
1234a5229c74SGary Mills {
1235*79d022daSYuri Pankov char buf[PATH_MAX];
1236a5229c74SGary Mills
1237a5229c74SGary Mills if (g_Ctoc(fn, buf, sizeof (buf)))
1238a5229c74SGary Mills return (-1);
1239a5229c74SGary Mills if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1240a5229c74SGary Mills return ((*pglob->gl_lstat)(buf, sb));
1241a5229c74SGary Mills return (lstat(buf, sb));
1242a5229c74SGary Mills }
1243a5229c74SGary Mills
1244a5229c74SGary Mills static int
g_stat(wcat_t * fn,struct stat * sb,glob_t * pglob)1245a5229c74SGary Mills g_stat(wcat_t *fn, struct stat *sb, glob_t *pglob)
1246a5229c74SGary Mills {
1247*79d022daSYuri Pankov char buf[PATH_MAX];
1248a5229c74SGary Mills
1249a5229c74SGary Mills if (g_Ctoc(fn, buf, sizeof (buf)))
1250a5229c74SGary Mills return (-1);
1251a5229c74SGary Mills if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1252a5229c74SGary Mills return ((*pglob->gl_stat)(buf, sb));
1253a5229c74SGary Mills return (stat(buf, sb));
1254a5229c74SGary Mills }
1255a5229c74SGary Mills
1256a5229c74SGary Mills static wcat_t *
g_strchr(const wcat_t * str,wchar_t ch)1257a5229c74SGary Mills g_strchr(const wcat_t *str, wchar_t ch)
1258a5229c74SGary Mills {
1259a5229c74SGary Mills do {
1260a5229c74SGary Mills if (str->w_at == 0 && str->w_wc == ch)
1261a5229c74SGary Mills return ((wcat_t *)str);
1262a5229c74SGary Mills } while ((str++)->w_wc != EOS);
1263a5229c74SGary Mills return (NULL);
1264a5229c74SGary Mills }
1265a5229c74SGary Mills
1266a5229c74SGary Mills static int
g_Ctoc(const wcat_t * str,char * buf,uint_t len)1267a5229c74SGary Mills g_Ctoc(const wcat_t *str, char *buf, uint_t len)
1268a5229c74SGary Mills {
1269a5229c74SGary Mills int n;
1270a5229c74SGary Mills wchar_t w;
1271a5229c74SGary Mills
1272a5229c74SGary Mills while (len >= MB_LEN_MAX) {
1273a5229c74SGary Mills w = (str++)->w_wc;
1274a5229c74SGary Mills if ((n = wctomb(buf, w)) > 0) {
1275a5229c74SGary Mills len -= n;
1276a5229c74SGary Mills buf += n;
1277a5229c74SGary Mills }
1278a5229c74SGary Mills if (n < 0)
1279a5229c74SGary Mills break;
1280a5229c74SGary Mills if (w == EOS)
1281a5229c74SGary Mills return (0);
1282a5229c74SGary Mills }
1283a5229c74SGary Mills return (1);
1284a5229c74SGary Mills }
1285a5229c74SGary Mills
128633e8313dSRobert Mustacchi #if defined(_LP64) || _FILE_OFFSET_BITS != 64
128733e8313dSRobert Mustacchi
1288a5229c74SGary Mills /* glob() function with legacy glob structure */
1289a5229c74SGary Mills int
old_glob(const char * pattern,int flags,int (* errfunc)(const char *,int),old_glob_t * pglob)1290a5229c74SGary Mills old_glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
1291a5229c74SGary Mills old_glob_t *pglob)
1292a5229c74SGary Mills {
1293a5229c74SGary Mills
1294a5229c74SGary Mills glob_t gl;
1295a5229c74SGary Mills int rv;
1296a5229c74SGary Mills
1297a5229c74SGary Mills flags &= GLOB_POSIX;
1298a5229c74SGary Mills
1299a5229c74SGary Mills (void) memset(&gl, 0, sizeof (gl));
1300a5229c74SGary Mills
1301a5229c74SGary Mills /*
1302a5229c74SGary Mills * Copy all the members, old to new. There's
1303a5229c74SGary Mills * really no point in micro-optimizing the copying.
1304a5229c74SGary Mills * Other members are set to zero.
1305a5229c74SGary Mills */
1306a5229c74SGary Mills gl.gl_pathc = pglob->gl_pathc;
1307a5229c74SGary Mills gl.gl_pathv = pglob->gl_pathv;
1308a5229c74SGary Mills gl.gl_offs = pglob->gl_offs;
1309a5229c74SGary Mills gl.gl_pathp = pglob->gl_pathp;
1310a5229c74SGary Mills gl.gl_pathn = pglob->gl_pathn;
1311a5229c74SGary Mills
1312a5229c74SGary Mills rv = _glob_ext(pattern, flags, errfunc, &gl);
1313a5229c74SGary Mills
1314a5229c74SGary Mills /*
1315a5229c74SGary Mills * Copy all the members, new to old. There's
1316a5229c74SGary Mills * really no point in micro-optimizing the copying.
1317a5229c74SGary Mills */
1318a5229c74SGary Mills pglob->gl_pathc = gl.gl_pathc;
1319a5229c74SGary Mills pglob->gl_pathv = gl.gl_pathv;
1320a5229c74SGary Mills pglob->gl_offs = gl.gl_offs;
1321a5229c74SGary Mills pglob->gl_pathp = gl.gl_pathp;
1322a5229c74SGary Mills pglob->gl_pathn = gl.gl_pathn;
13237c478bd9Sstevel@tonic-gate
13247c478bd9Sstevel@tonic-gate return (rv);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate
1327a5229c74SGary Mills /* globfree() function with legacy glob structure */
1328a5229c74SGary Mills void
old_globfree(old_glob_t * pglob)1329a5229c74SGary Mills old_globfree(old_glob_t *pglob)
1330a5229c74SGary Mills {
1331a5229c74SGary Mills glob_t gl;
1332a5229c74SGary Mills
1333a5229c74SGary Mills (void) memset(&gl, 0, sizeof (gl));
13347c478bd9Sstevel@tonic-gate
13357c478bd9Sstevel@tonic-gate /*
1336a5229c74SGary Mills * Copy all the members, old to new. There's
1337a5229c74SGary Mills * really no point in micro-optimizing the copying.
1338a5229c74SGary Mills * Other members are set to zero.
13397c478bd9Sstevel@tonic-gate */
1340a5229c74SGary Mills gl.gl_pathc = pglob->gl_pathc;
1341a5229c74SGary Mills gl.gl_pathv = pglob->gl_pathv;
1342a5229c74SGary Mills gl.gl_offs = pglob->gl_offs;
1343a5229c74SGary Mills gl.gl_pathp = pglob->gl_pathp;
1344a5229c74SGary Mills gl.gl_pathn = pglob->gl_pathn;
13457c478bd9Sstevel@tonic-gate
1346a5229c74SGary Mills _globfree_ext(&gl);
13477c478bd9Sstevel@tonic-gate
13487c478bd9Sstevel@tonic-gate /*
1349a5229c74SGary Mills * Copy all the members, new to old. There's
1350a5229c74SGary Mills * really no point in micro-optimizing the copying.
13517c478bd9Sstevel@tonic-gate */
1352a5229c74SGary Mills pglob->gl_pathc = gl.gl_pathc;
1353a5229c74SGary Mills pglob->gl_pathv = gl.gl_pathv;
1354a5229c74SGary Mills pglob->gl_offs = gl.gl_offs;
1355a5229c74SGary Mills pglob->gl_pathp = gl.gl_pathp;
1356a5229c74SGary Mills pglob->gl_pathn = gl.gl_pathn;
1357a5229c74SGary Mills
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate
136033e8313dSRobert Mustacchi #endif /* _LP64 || _FILE_OFFSET_BITS != 64 */
1361