blob: 7c97e67f557cc827e19bdc3b20bee7d284f4586e [file] [log] [blame]
Damien Millere128a502011-09-22 21:22:21 +10001/* $OpenBSD: glob.c,v 1.38 2011/09/22 06:27:29 djm Exp $ */
Damien Miller3c027682001-03-14 11:39:45 +11002/*
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Guido van Rossum.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
Damien Miller329638e2003-06-03 12:12:50 +100017 * 3. Neither the name of the University nor the names of its contributors
Damien Miller3c027682001-03-14 11:39:45 +110018 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
Darren Tucker7f24a0e2005-11-10 16:18:56 +110034/* OPENBSD ORIGINAL: lib/libc/gen/glob.c */
35
Damien Miller3c027682001-03-14 11:39:45 +110036/*
37 * glob(3) -- a superset of the one defined in POSIX 1003.2.
38 *
39 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
40 *
41 * Optional extra services, controlled by flags not defined by POSIX:
42 *
43 * GLOB_QUOTE:
44 * Escaping convention: \ inhibits any special meaning the following
45 * character might have (except \ at end of string is retained).
46 * GLOB_MAGCHAR:
47 * Set in gl_flags if pattern contained a globbing character.
48 * GLOB_NOMAGIC:
49 * Same as GLOB_NOCHECK, but it will only append pattern if it did
50 * not contain any magic characters. [Used in csh style globbing]
51 * GLOB_ALTDIRFUNC:
52 * Use alternately specified directory access functions.
53 * GLOB_TILDE:
54 * expand ~user/foo to the /home/dir/of/user/foo
55 * GLOB_BRACE:
56 * expand {1,2}{a,b} to 1a 1b 2a 2b
57 * gl_matchc:
58 * Number of matches in the current invocation of glob.
59 */
60
Damien Millera6e121a2010-10-07 21:39:17 +110061#include "includes.h"
Darren Tucker8db134e2015-10-29 10:48:23 +110062#include "glob.h"
Damien Millera6e121a2010-10-07 21:39:17 +110063
64#include <sys/types.h>
65#include <sys/stat.h>
66
67#include <dirent.h>
68#include <ctype.h>
69#include <errno.h>
Damien Millerc4bf7dd2011-09-22 21:21:48 +100070#include <limits.h>
Damien Millera6e121a2010-10-07 21:39:17 +110071#include <pwd.h>
72#include <stdlib.h>
73#include <string.h>
74#include <unistd.h>
75
76#if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
77 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) || \
78 !defined(HAVE_DECL_GLOB_NOMATCH) || HAVE_DECL_GLOB_NOMATCH == 0 || \
79 defined(BROKEN_GLOB)
80
Damien Millera6e121a2010-10-07 21:39:17 +110081#include "charclass.h"
Damien Miller3c027682001-03-14 11:39:45 +110082
83#define DOLLAR '$'
84#define DOT '.'
85#define EOS '\0'
86#define LBRACKET '['
87#define NOT '!'
88#define QUESTION '?'
89#define QUOTE '\\'
90#define RANGE '-'
91#define RBRACKET ']'
92#define SEP '/'
93#define STAR '*'
94#define TILDE '~'
95#define UNDERSCORE '_'
96#define LBRACE '{'
97#define RBRACE '}'
98#define SLASH '/'
99#define COMMA ','
100
101#ifndef DEBUG
102
103#define M_QUOTE 0x8000
104#define M_PROTECT 0x4000
105#define M_MASK 0xffff
106#define M_ASCII 0x00ff
107
108typedef u_short Char;
109
110#else
111
112#define M_QUOTE 0x80
113#define M_PROTECT 0x40
114#define M_MASK 0xff
115#define M_ASCII 0x7f
116
117typedef char Char;
118
119#endif
120
121
122#define CHAR(c) ((Char)((c)&M_ASCII))
123#define META(c) ((Char)((c)|M_QUOTE))
124#define M_ALL META('*')
125#define M_END META(']')
126#define M_NOT META('!')
127#define M_ONE META('?')
128#define M_RNG META('-')
129#define M_SET META('[')
Damien Millera6e121a2010-10-07 21:39:17 +1100130#define M_CLASS META(':')
Damien Miller3c027682001-03-14 11:39:45 +1100131#define ismeta(c) (((c)&M_QUOTE) != 0)
132
Damien Millerb66e9172011-01-12 13:30:18 +1100133#define GLOB_LIMIT_MALLOC 65536
134#define GLOB_LIMIT_STAT 128
135#define GLOB_LIMIT_READDIR 16384
136
Damien Millere128a502011-09-22 21:22:21 +1000137/* Limit of recursion during matching attempts. */
138#define GLOB_LIMIT_RECUR 64
139
Damien Millerb66e9172011-01-12 13:30:18 +1100140struct glob_lim {
141 size_t glim_malloc;
142 size_t glim_stat;
143 size_t glim_readdir;
144};
Damien Miller3c027682001-03-14 11:39:45 +1100145
Damien Millere128a502011-09-22 21:22:21 +1000146struct glob_path_stat {
147 char *gps_path;
148 struct stat *gps_stat;
149};
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000150
Damien Miller71eb0c12002-09-11 10:29:11 +1000151static int compare(const void *, const void *);
Damien Millere128a502011-09-22 21:22:21 +1000152static int compare_gps(const void *, const void *);
Damien Miller71eb0c12002-09-11 10:29:11 +1000153static int g_Ctoc(const Char *, char *, u_int);
154static int g_lstat(Char *, struct stat *, glob_t *);
155static DIR *g_opendir(Char *, glob_t *);
Damien Millera6e121a2010-10-07 21:39:17 +1100156static Char *g_strchr(const Char *, int);
157static int g_strncmp(const Char *, const char *, size_t);
Damien Miller71eb0c12002-09-11 10:29:11 +1000158static int g_stat(Char *, struct stat *, glob_t *);
Damien Millerb66e9172011-01-12 13:30:18 +1100159static int glob0(const Char *, glob_t *, struct glob_lim *);
160static int glob1(Char *, Char *, glob_t *, struct glob_lim *);
Damien Miller71eb0c12002-09-11 10:29:11 +1000161static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
Damien Millerb66e9172011-01-12 13:30:18 +1100162 glob_t *, struct glob_lim *);
Damien Miller9c51c8d2007-10-26 16:13:39 +1000163static int glob3(Char *, Char *, Char *, Char *, Char *,
Damien Millerb66e9172011-01-12 13:30:18 +1100164 Char *, Char *, glob_t *, struct glob_lim *);
165static int globextend(const Char *, glob_t *, struct glob_lim *,
166 struct stat *);
Ben Lindstroma77d6412001-03-19 18:58:13 +0000167static const Char *
Damien Miller71eb0c12002-09-11 10:29:11 +1000168 globtilde(const Char *, Char *, size_t, glob_t *);
Damien Millerb66e9172011-01-12 13:30:18 +1100169static int globexp1(const Char *, glob_t *, struct glob_lim *);
170static int globexp2(const Char *, const Char *, glob_t *,
171 struct glob_lim *);
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000172static int match(Char *, Char *, Char *, int);
Damien Miller3c027682001-03-14 11:39:45 +1100173#ifdef DEBUG
Damien Miller71eb0c12002-09-11 10:29:11 +1000174static void qprintf(const char *, Char *);
Damien Miller3c027682001-03-14 11:39:45 +1100175#endif
176
177int
Darren Tucker6524d4f2005-11-10 17:02:21 +1100178glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
179 glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +1100180{
181 const u_char *patnext;
182 int c;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000183 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
Damien Millerb66e9172011-01-12 13:30:18 +1100184 struct glob_lim limit = { 0, 0, 0 };
Damien Miller3c027682001-03-14 11:39:45 +1100185
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000186 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
187 return(GLOB_NOMATCH);
188
Damien Miller3c027682001-03-14 11:39:45 +1100189 patnext = (u_char *) pattern;
190 if (!(flags & GLOB_APPEND)) {
191 pglob->gl_pathc = 0;
192 pglob->gl_pathv = NULL;
Damien Millera6e121a2010-10-07 21:39:17 +1100193 pglob->gl_statv = NULL;
Damien Miller3c027682001-03-14 11:39:45 +1100194 if (!(flags & GLOB_DOOFFS))
195 pglob->gl_offs = 0;
196 }
197 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
198 pglob->gl_errfunc = errfunc;
199 pglob->gl_matchc = 0;
200
Damien Miller4927aaf2011-01-12 13:32:03 +1100201 if (pglob->gl_offs < 0 || pglob->gl_pathc < 0 ||
202 pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX ||
203 pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1)
204 return GLOB_NOSPACE;
205
Damien Miller3c027682001-03-14 11:39:45 +1100206 bufnext = patbuf;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000207 bufend = bufnext + MAXPATHLEN - 1;
Damien Miller3c027682001-03-14 11:39:45 +1100208 if (flags & GLOB_NOESCAPE)
Damien Millerb68af622001-03-28 21:05:26 +1000209 while (bufnext < bufend && (c = *patnext++) != EOS)
210 *bufnext++ = c;
Damien Miller3c027682001-03-14 11:39:45 +1100211 else {
212 /* Protect the quoted characters. */
213 while (bufnext < bufend && (c = *patnext++) != EOS)
214 if (c == QUOTE) {
215 if ((c = *patnext++) == EOS) {
216 c = QUOTE;
217 --patnext;
218 }
219 *bufnext++ = c | M_PROTECT;
Damien Miller6e77a532001-04-14 00:22:33 +1000220 } else
Damien Miller3c027682001-03-14 11:39:45 +1100221 *bufnext++ = c;
222 }
223 *bufnext = EOS;
224
225 if (flags & GLOB_BRACE)
Damien Millerb66e9172011-01-12 13:30:18 +1100226 return globexp1(patbuf, pglob, &limit);
Damien Miller3c027682001-03-14 11:39:45 +1100227 else
Damien Millerb66e9172011-01-12 13:30:18 +1100228 return glob0(patbuf, pglob, &limit);
Damien Miller3c027682001-03-14 11:39:45 +1100229}
230
231/*
232 * Expand recursively a glob {} pattern. When there is no more expansion
233 * invoke the standard globbing routine to glob the rest of the magic
234 * characters
235 */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000236static int
Damien Millerb66e9172011-01-12 13:30:18 +1100237globexp1(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100238{
239 const Char* ptr = pattern;
Damien Miller3c027682001-03-14 11:39:45 +1100240
241 /* Protect a single {}, for find(1), like csh */
242 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
Damien Millerb66e9172011-01-12 13:30:18 +1100243 return glob0(pattern, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100244
Damien Millera6e121a2010-10-07 21:39:17 +1100245 if ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
Damien Millerb66e9172011-01-12 13:30:18 +1100246 return globexp2(ptr, pattern, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100247
Damien Millerb66e9172011-01-12 13:30:18 +1100248 return glob0(pattern, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100249}
250
251
252/*
253 * Recursive brace globbing helper. Tries to expand a single brace.
254 * If it succeeds then it invokes globexp1 with the new pattern.
255 * If it fails then it tries to glob the rest of the pattern and returns.
256 */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000257static int
Damien Millerb66e9172011-01-12 13:30:18 +1100258globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
259 struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100260{
Damien Millera6e121a2010-10-07 21:39:17 +1100261 int i, rv;
Damien Miller3c027682001-03-14 11:39:45 +1100262 Char *lm, *ls;
263 const Char *pe, *pm, *pl;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000264 Char patbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100265
266 /* copy part up to the brace */
267 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
Damien Miller6e77a532001-04-14 00:22:33 +1000268 ;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000269 *lm = EOS;
Damien Miller3c027682001-03-14 11:39:45 +1100270 ls = lm;
271
272 /* Find the balanced brace */
273 for (i = 0, pe = ++ptr; *pe; pe++)
274 if (*pe == LBRACKET) {
275 /* Ignore everything between [] */
276 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
Damien Miller6e77a532001-04-14 00:22:33 +1000277 ;
Damien Miller3c027682001-03-14 11:39:45 +1100278 if (*pe == EOS) {
279 /*
280 * We could not find a matching RBRACKET.
281 * Ignore and just look for RBRACE
282 */
283 pe = pm;
284 }
Damien Miller6e77a532001-04-14 00:22:33 +1000285 } else if (*pe == LBRACE)
Damien Miller3c027682001-03-14 11:39:45 +1100286 i++;
287 else if (*pe == RBRACE) {
288 if (i == 0)
289 break;
290 i--;
291 }
292
293 /* Non matching braces; just glob the pattern */
Damien Millera6e121a2010-10-07 21:39:17 +1100294 if (i != 0 || *pe == EOS)
Damien Millerb66e9172011-01-12 13:30:18 +1100295 return glob0(patbuf, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100296
Damien Millerb68af622001-03-28 21:05:26 +1000297 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
Damien Miller3c027682001-03-14 11:39:45 +1100298 switch (*pm) {
299 case LBRACKET:
300 /* Ignore everything between [] */
301 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
Damien Miller6e77a532001-04-14 00:22:33 +1000302 ;
Damien Miller3c027682001-03-14 11:39:45 +1100303 if (*pm == EOS) {
304 /*
305 * We could not find a matching RBRACKET.
306 * Ignore and just look for RBRACE
307 */
308 pm = pl;
309 }
310 break;
311
312 case LBRACE:
313 i++;
314 break;
315
316 case RBRACE:
317 if (i) {
Damien Millerb68af622001-03-28 21:05:26 +1000318 i--;
319 break;
Damien Miller3c027682001-03-14 11:39:45 +1100320 }
321 /* FALLTHROUGH */
322 case COMMA:
323 if (i && *pm == COMMA)
324 break;
325 else {
326 /* Append the current string */
327 for (lm = ls; (pl < pm); *lm++ = *pl++)
Damien Miller6e77a532001-04-14 00:22:33 +1000328 ;
329
Damien Miller3c027682001-03-14 11:39:45 +1100330 /*
331 * Append the rest of the pattern after the
332 * closing brace
333 */
Damien Miller6e77a532001-04-14 00:22:33 +1000334 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
335 ;
Damien Miller3c027682001-03-14 11:39:45 +1100336
337 /* Expand the current pattern */
338#ifdef DEBUG
339 qprintf("globexp2:", patbuf);
340#endif
Damien Millerb66e9172011-01-12 13:30:18 +1100341 rv = globexp1(patbuf, pglob, limitp);
Damien Millera6e121a2010-10-07 21:39:17 +1100342 if (rv && rv != GLOB_NOMATCH)
343 return rv;
Damien Miller3c027682001-03-14 11:39:45 +1100344
345 /* move after the comma, to the next string */
346 pl = pm + 1;
347 }
348 break;
349
350 default:
351 break;
352 }
Damien Millerb68af622001-03-28 21:05:26 +1000353 }
Damien Miller3c027682001-03-14 11:39:45 +1100354 return 0;
355}
356
357
358
359/*
360 * expand tilde from the passwd file.
361 */
362static const Char *
Darren Tucker6524d4f2005-11-10 17:02:21 +1100363globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +1100364{
365 struct passwd *pwd;
366 char *h;
367 const Char *p;
368 Char *b, *eb;
369
370 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
371 return pattern;
372
373 /* Copy up to the end of the string or / */
374 eb = &patbuf[patbuf_len - 1];
375 for (p = pattern + 1, h = (char *) patbuf;
376 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
Damien Miller6e77a532001-04-14 00:22:33 +1000377 ;
Damien Miller3c027682001-03-14 11:39:45 +1100378
379 *h = EOS;
380
Damien Miller6e77a532001-04-14 00:22:33 +1000381#if 0
382 if (h == (char *)eb)
383 return what;
384#endif
385
Damien Miller3c027682001-03-14 11:39:45 +1100386 if (((char *) patbuf)[0] == EOS) {
387 /*
388 * handle a plain ~ or ~/ by expanding $HOME
389 * first and then trying the password file
390 */
391#if 0
392 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
393#endif
394 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
395 if ((pwd = getpwuid(getuid())) == NULL)
396 return pattern;
397 else
398 h = pwd->pw_dir;
399 }
Damien Miller6e77a532001-04-14 00:22:33 +1000400 } else {
Damien Miller3c027682001-03-14 11:39:45 +1100401 /*
402 * Expand a ~user
403 */
404 if ((pwd = getpwnam((char*) patbuf)) == NULL)
405 return pattern;
406 else
407 h = pwd->pw_dir;
408 }
409
410 /* Copy the home directory */
411 for (b = patbuf; b < eb && *h; *b++ = *h++)
Damien Miller6e77a532001-04-14 00:22:33 +1000412 ;
Damien Miller3c027682001-03-14 11:39:45 +1100413
414 /* Append the rest of the pattern */
415 while (b < eb && (*b++ = *p++) != EOS)
Damien Miller6e77a532001-04-14 00:22:33 +1000416 ;
Damien Miller3c027682001-03-14 11:39:45 +1100417 *b = EOS;
418
419 return patbuf;
420}
421
Damien Millera6e121a2010-10-07 21:39:17 +1100422static int
423g_strncmp(const Char *s1, const char *s2, size_t n)
424{
425 int rv = 0;
426
427 while (n--) {
428 rv = *(Char *)s1 - *(const unsigned char *)s2++;
429 if (rv)
430 break;
431 if (*s1++ == '\0')
432 break;
433 }
434 return rv;
435}
436
437static int
438g_charclass(const Char **patternp, Char **bufnextp)
439{
440 const Char *pattern = *patternp + 1;
441 Char *bufnext = *bufnextp;
442 const Char *colon;
443 struct cclass *cc;
444 size_t len;
445
446 if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']')
447 return 1; /* not a character class */
448
449 len = (size_t)(colon - pattern);
450 for (cc = cclasses; cc->name != NULL; cc++) {
451 if (!g_strncmp(pattern, cc->name, len) && cc->name[len] == '\0')
452 break;
453 }
454 if (cc->name == NULL)
455 return -1; /* invalid character class */
456 *bufnext++ = M_CLASS;
457 *bufnext++ = (Char)(cc - &cclasses[0]);
458 *bufnextp = bufnext;
459 *patternp += len + 3;
460
461 return 0;
462}
Damien Miller3c027682001-03-14 11:39:45 +1100463
464/*
465 * The main glob() routine: compiles the pattern (optionally processing
466 * quotes), calls glob1() to do the real pattern matching, and finally
467 * sorts the list (unless unsorted operation is requested). Returns 0
468 * if things went well, nonzero if errors occurred. It is not an error
469 * to find no matches.
470 */
471static int
Damien Millerb66e9172011-01-12 13:30:18 +1100472glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100473{
474 const Char *qpatnext;
475 int c, err, oldpathc;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000476 Char *bufnext, patbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100477
Damien Millerd8f72ca2001-03-30 10:23:17 +1000478 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
Damien Miller3c027682001-03-14 11:39:45 +1100479 oldpathc = pglob->gl_pathc;
480 bufnext = patbuf;
481
482 /* We don't need to check for buffer overflow any more. */
483 while ((c = *qpatnext++) != EOS) {
484 switch (c) {
485 case LBRACKET:
486 c = *qpatnext;
487 if (c == NOT)
488 ++qpatnext;
489 if (*qpatnext == EOS ||
Damien Millera6e121a2010-10-07 21:39:17 +1100490 g_strchr(qpatnext+1, RBRACKET) == NULL) {
Damien Miller3c027682001-03-14 11:39:45 +1100491 *bufnext++ = LBRACKET;
492 if (c == NOT)
493 --qpatnext;
494 break;
495 }
496 *bufnext++ = M_SET;
497 if (c == NOT)
498 *bufnext++ = M_NOT;
499 c = *qpatnext++;
500 do {
Damien Millera6e121a2010-10-07 21:39:17 +1100501 if (c == LBRACKET && *qpatnext == ':') {
502 do {
503 err = g_charclass(&qpatnext,
504 &bufnext);
505 if (err)
506 break;
507 c = *qpatnext++;
508 } while (c == LBRACKET && *qpatnext == ':');
509 if (err == -1 &&
510 !(pglob->gl_flags & GLOB_NOCHECK))
511 return GLOB_NOMATCH;
512 if (c == RBRACKET)
513 break;
514 }
Damien Miller3c027682001-03-14 11:39:45 +1100515 *bufnext++ = CHAR(c);
516 if (*qpatnext == RANGE &&
517 (c = qpatnext[1]) != RBRACKET) {
518 *bufnext++ = M_RNG;
519 *bufnext++ = CHAR(c);
520 qpatnext += 2;
521 }
522 } while ((c = *qpatnext++) != RBRACKET);
523 pglob->gl_flags |= GLOB_MAGCHAR;
524 *bufnext++ = M_END;
525 break;
526 case QUESTION:
527 pglob->gl_flags |= GLOB_MAGCHAR;
528 *bufnext++ = M_ONE;
529 break;
530 case STAR:
531 pglob->gl_flags |= GLOB_MAGCHAR;
532 /* collapse adjacent stars to one,
533 * to avoid exponential behavior
534 */
535 if (bufnext == patbuf || bufnext[-1] != M_ALL)
Damien Millerb68af622001-03-28 21:05:26 +1000536 *bufnext++ = M_ALL;
Damien Miller3c027682001-03-14 11:39:45 +1100537 break;
538 default:
539 *bufnext++ = CHAR(c);
540 break;
541 }
542 }
543 *bufnext = EOS;
544#ifdef DEBUG
545 qprintf("glob0:", patbuf);
546#endif
547
Damien Millerb66e9172011-01-12 13:30:18 +1100548 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limitp)) != 0)
Damien Miller3c027682001-03-14 11:39:45 +1100549 return(err);
550
551 /*
552 * If there was no match we are going to append the pattern
553 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
554 * and the pattern did not contain any magic characters
555 * GLOB_NOMAGIC is there just for compatibility with csh.
556 */
557 if (pglob->gl_pathc == oldpathc) {
558 if ((pglob->gl_flags & GLOB_NOCHECK) ||
559 ((pglob->gl_flags & GLOB_NOMAGIC) &&
560 !(pglob->gl_flags & GLOB_MAGCHAR)))
Damien Millerb66e9172011-01-12 13:30:18 +1100561 return(globextend(pattern, pglob, limitp, NULL));
Damien Miller3c027682001-03-14 11:39:45 +1100562 else
563 return(GLOB_NOMATCH);
564 }
Damien Millere128a502011-09-22 21:22:21 +1000565 if (!(pglob->gl_flags & GLOB_NOSORT)) {
566 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
567 /* Keep the paths and stat info synced during sort */
568 struct glob_path_stat *path_stat;
569 int i;
570 int n = pglob->gl_pathc - oldpathc;
571 int o = pglob->gl_offs + oldpathc;
572
573 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
574 return GLOB_NOSPACE;
575 for (i = 0; i < n; i++) {
576 path_stat[i].gps_path = pglob->gl_pathv[o + i];
577 path_stat[i].gps_stat = pglob->gl_statv[o + i];
578 }
579 qsort(path_stat, n, sizeof(*path_stat), compare_gps);
580 for (i = 0; i < n; i++) {
581 pglob->gl_pathv[o + i] = path_stat[i].gps_path;
582 pglob->gl_statv[o + i] = path_stat[i].gps_stat;
583 }
584 free(path_stat);
585 } else {
586 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
587 pglob->gl_pathc - oldpathc, sizeof(char *),
588 compare);
589 }
590 }
Damien Miller3c027682001-03-14 11:39:45 +1100591 return(0);
592}
593
594static int
Darren Tucker6524d4f2005-11-10 17:02:21 +1100595compare(const void *p, const void *q)
Damien Miller3c027682001-03-14 11:39:45 +1100596{
597 return(strcmp(*(char **)p, *(char **)q));
598}
599
600static int
Damien Millere128a502011-09-22 21:22:21 +1000601compare_gps(const void *_p, const void *_q)
602{
603 const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
604 const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
605
606 return(strcmp(p->gps_path, q->gps_path));
607}
608
609static int
Damien Millerb66e9172011-01-12 13:30:18 +1100610glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100611{
Damien Millerd8f72ca2001-03-30 10:23:17 +1000612 Char pathbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100613
614 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
615 if (*pattern == EOS)
616 return(0);
Damien Millerd8f72ca2001-03-30 10:23:17 +1000617 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
618 pathbuf, pathbuf+MAXPATHLEN-1,
619 pattern, pattern_last, pglob, limitp));
Damien Miller3c027682001-03-14 11:39:45 +1100620}
621
622/*
623 * The functions glob2 and glob3 are mutually recursive; there is one level
624 * of recursion for each segment in the pattern that contains one or more
625 * meta characters.
626 */
627static int
Darren Tucker6524d4f2005-11-10 17:02:21 +1100628glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
Damien Millerb66e9172011-01-12 13:30:18 +1100629 Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100630{
631 struct stat sb;
632 Char *p, *q;
633 int anymeta;
634
635 /*
636 * Loop over pattern segments until end of pattern or until
637 * segment with meta character found.
638 */
639 for (anymeta = 0;;) {
640 if (*pattern == EOS) { /* End of pattern? */
641 *pathend = EOS;
642 if (g_lstat(pathbuf, &sb, pglob))
643 return(0);
644
Damien Millerb66e9172011-01-12 13:30:18 +1100645 if ((pglob->gl_flags & GLOB_LIMIT) &&
646 limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
647 errno = 0;
648 *pathend++ = SEP;
649 *pathend = EOS;
650 return(GLOB_NOSPACE);
651 }
652
Damien Miller3c027682001-03-14 11:39:45 +1100653 if (((pglob->gl_flags & GLOB_MARK) &&
Damien Millerb68af622001-03-28 21:05:26 +1000654 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
655 (S_ISLNK(sb.st_mode) &&
Damien Miller3c027682001-03-14 11:39:45 +1100656 (g_stat(pathbuf, &sb, pglob) == 0) &&
657 S_ISDIR(sb.st_mode)))) {
Damien Millerd8f72ca2001-03-30 10:23:17 +1000658 if (pathend+1 > pathend_last)
659 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100660 *pathend++ = SEP;
661 *pathend = EOS;
662 }
663 ++pglob->gl_matchc;
Damien Millera6e121a2010-10-07 21:39:17 +1100664 return(globextend(pathbuf, pglob, limitp, &sb));
Damien Miller3c027682001-03-14 11:39:45 +1100665 }
666
667 /* Find end of next segment, copy tentatively to pathend. */
668 q = pathend;
669 p = pattern;
670 while (*p != EOS && *p != SEP) {
671 if (ismeta(*p))
672 anymeta = 1;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000673 if (q+1 > pathend_last)
674 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100675 *q++ = *p++;
676 }
677
678 if (!anymeta) { /* No expansion, do next segment. */
679 pathend = q;
680 pattern = p;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000681 while (*pattern == SEP) {
682 if (pathend+1 > pathend_last)
683 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100684 *pathend++ = *pattern++;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000685 }
Damien Millerb68af622001-03-28 21:05:26 +1000686 } else
687 /* Need expansion, recurse. */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000688 return(glob3(pathbuf, pathbuf_last, pathend,
Damien Miller9c51c8d2007-10-26 16:13:39 +1000689 pathend_last, pattern, p, pattern_last,
690 pglob, limitp));
Damien Miller3c027682001-03-14 11:39:45 +1100691 }
692 /* NOTREACHED */
693}
694
695static int
Darren Tucker6524d4f2005-11-10 17:02:21 +1100696glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
Damien Miller9c51c8d2007-10-26 16:13:39 +1000697 Char *pattern, Char *restpattern, Char *restpattern_last, glob_t *pglob,
Damien Millerb66e9172011-01-12 13:30:18 +1100698 struct glob_lim *limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100699{
Darren Tucker6524d4f2005-11-10 17:02:21 +1100700 struct dirent *dp;
Damien Miller3c027682001-03-14 11:39:45 +1100701 DIR *dirp;
702 int err;
703 char buf[MAXPATHLEN];
704
705 /*
706 * The readdirfunc declaration can't be prototyped, because it is
707 * assigned, below, to two functions which are prototyped in glob.h
708 * and dirent.h as taking pointers to differently typed opaque
709 * structures.
710 */
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000711 struct dirent *(*readdirfunc)(void *);
Damien Miller3c027682001-03-14 11:39:45 +1100712
Damien Millerd8f72ca2001-03-30 10:23:17 +1000713 if (pathend > pathend_last)
714 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100715 *pathend = EOS;
716 errno = 0;
717
718 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
719 /* TODO: don't call for ENOENT or ENOTDIR? */
720 if (pglob->gl_errfunc) {
Damien Miller6e77a532001-04-14 00:22:33 +1000721 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000722 return(GLOB_ABORTED);
Damien Miller3c027682001-03-14 11:39:45 +1100723 if (pglob->gl_errfunc(buf, errno) ||
724 pglob->gl_flags & GLOB_ERR)
Damien Millerb68af622001-03-28 21:05:26 +1000725 return(GLOB_ABORTED);
Damien Miller3c027682001-03-14 11:39:45 +1100726 }
727 return(0);
728 }
729
730 err = 0;
731
732 /* Search directory for matching names. */
733 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
734 readdirfunc = pglob->gl_readdir;
735 else
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000736 readdirfunc = (struct dirent *(*)(void *))readdir;
Damien Miller3c027682001-03-14 11:39:45 +1100737 while ((dp = (*readdirfunc)(dirp))) {
Darren Tucker6524d4f2005-11-10 17:02:21 +1100738 u_char *sc;
739 Char *dc;
Damien Miller3c027682001-03-14 11:39:45 +1100740
Damien Millerb66e9172011-01-12 13:30:18 +1100741 if ((pglob->gl_flags & GLOB_LIMIT) &&
742 limitp->glim_readdir++ >= GLOB_LIMIT_READDIR) {
743 errno = 0;
744 *pathend++ = SEP;
745 *pathend = EOS;
Damien Millere01a6272011-09-22 21:20:21 +1000746 err = GLOB_NOSPACE;
747 break;
Damien Millerb66e9172011-01-12 13:30:18 +1100748 }
749
Damien Miller3c027682001-03-14 11:39:45 +1100750 /* Initial DOT must be matched literally. */
751 if (dp->d_name[0] == DOT && *pattern != DOT)
752 continue;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000753 dc = pathend;
754 sc = (u_char *) dp->d_name;
755 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
756 ;
757 if (dc >= pathend_last) {
758 *dc = EOS;
759 err = 1;
760 break;
761 }
762
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000763 if (!match(pathend, pattern, restpattern, GLOB_LIMIT_RECUR)) {
Damien Miller3c027682001-03-14 11:39:45 +1100764 *pathend = EOS;
765 continue;
766 }
Damien Millerd8f72ca2001-03-30 10:23:17 +1000767 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
768 restpattern, restpattern_last, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100769 if (err)
770 break;
771 }
772
773 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
774 (*pglob->gl_closedir)(dirp);
775 else
776 closedir(dirp);
777 return(err);
778}
779
780
781/*
Damien Miller71eb0c12002-09-11 10:29:11 +1000782 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
Damien Miller3c027682001-03-14 11:39:45 +1100783 * add the new item, and update gl_pathc.
784 *
785 * This assumes the BSD realloc, which only copies the block when its size
786 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
787 * behavior.
788 *
789 * Return 0 if new item added, error code if memory couldn't be allocated.
790 *
791 * Invariant of the glob_t structure:
792 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
793 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
794 */
795static int
Damien Millerb66e9172011-01-12 13:30:18 +1100796globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
797 struct stat *sb)
Damien Miller3c027682001-03-14 11:39:45 +1100798{
Darren Tucker6524d4f2005-11-10 17:02:21 +1100799 char **pathv;
Damien Millera6e121a2010-10-07 21:39:17 +1100800 ssize_t i;
801 size_t newn, len;
802 char *copy = NULL;
Damien Miller3c027682001-03-14 11:39:45 +1100803 const Char *p;
Damien Millera6e121a2010-10-07 21:39:17 +1100804 struct stat **statv;
Damien Miller3c027682001-03-14 11:39:45 +1100805
Damien Millera6e121a2010-10-07 21:39:17 +1100806 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
Damien Miller4927aaf2011-01-12 13:32:03 +1100807 if (pglob->gl_offs >= INT_MAX ||
808 pglob->gl_pathc >= INT_MAX ||
809 newn >= INT_MAX ||
810 SIZE_MAX / sizeof(*pathv) <= newn ||
Damien Millera6e121a2010-10-07 21:39:17 +1100811 SIZE_MAX / sizeof(*statv) <= newn) {
812 nospace:
Damien Miller4927aaf2011-01-12 13:32:03 +1100813 for (i = pglob->gl_offs; i < (ssize_t)(newn - 2); i++) {
Damien Millera6e121a2010-10-07 21:39:17 +1100814 if (pglob->gl_pathv && pglob->gl_pathv[i])
815 free(pglob->gl_pathv[i]);
816 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
817 pglob->gl_pathv && pglob->gl_pathv[i])
818 free(pglob->gl_statv[i]);
819 }
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000820 if (pglob->gl_pathv) {
Damien Miller3c027682001-03-14 11:39:45 +1100821 free(pglob->gl_pathv);
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000822 pglob->gl_pathv = NULL;
823 }
Damien Millera6e121a2010-10-07 21:39:17 +1100824 if (pglob->gl_statv) {
825 free(pglob->gl_statv);
826 pglob->gl_statv = NULL;
827 }
Damien Miller3c027682001-03-14 11:39:45 +1100828 return(GLOB_NOSPACE);
829 }
830
Damien Millera6e121a2010-10-07 21:39:17 +1100831 pathv = realloc(pglob->gl_pathv, newn * sizeof(*pathv));
832 if (pathv == NULL)
833 goto nospace;
Damien Miller3c027682001-03-14 11:39:45 +1100834 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
835 /* first time around -- clear initial gl_offs items */
836 pathv += pglob->gl_offs;
837 for (i = pglob->gl_offs; --i >= 0; )
838 *--pathv = NULL;
839 }
840 pglob->gl_pathv = pathv;
841
Damien Millera6e121a2010-10-07 21:39:17 +1100842 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
843 statv = realloc(pglob->gl_statv, newn * sizeof(*statv));
844 if (statv == NULL)
845 goto nospace;
846 if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
847 /* first time around -- clear initial gl_offs items */
848 statv += pglob->gl_offs;
849 for (i = pglob->gl_offs; --i >= 0; )
850 *--statv = NULL;
851 }
852 pglob->gl_statv = statv;
853 if (sb == NULL)
854 statv[pglob->gl_offs + pglob->gl_pathc] = NULL;
855 else {
Damien Millerb66e9172011-01-12 13:30:18 +1100856 limitp->glim_malloc += sizeof(**statv);
857 if ((pglob->gl_flags & GLOB_LIMIT) &&
858 limitp->glim_malloc >= GLOB_LIMIT_MALLOC) {
859 errno = 0;
860 return(GLOB_NOSPACE);
861 }
Damien Millera6e121a2010-10-07 21:39:17 +1100862 if ((statv[pglob->gl_offs + pglob->gl_pathc] =
863 malloc(sizeof(**statv))) == NULL)
864 goto copy_error;
865 memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb,
866 sizeof(*sb));
867 }
868 statv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL;
869 }
870
Damien Miller3c027682001-03-14 11:39:45 +1100871 for (p = path; *p++;)
Damien Miller6e77a532001-04-14 00:22:33 +1000872 ;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000873 len = (size_t)(p - path);
Damien Millerb66e9172011-01-12 13:30:18 +1100874 limitp->glim_malloc += len;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000875 if ((copy = malloc(len)) != NULL) {
Damien Miller6e77a532001-04-14 00:22:33 +1000876 if (g_Ctoc(path, copy, len)) {
Damien Millerb68af622001-03-28 21:05:26 +1000877 free(copy);
878 return(GLOB_NOSPACE);
879 }
Damien Miller3c027682001-03-14 11:39:45 +1100880 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
881 }
882 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000883
884 if ((pglob->gl_flags & GLOB_LIMIT) &&
Damien Millerb66e9172011-01-12 13:30:18 +1100885 (newn * sizeof(*pathv)) + limitp->glim_malloc >
886 GLOB_LIMIT_MALLOC) {
Ben Lindstroma77d6412001-03-19 18:58:13 +0000887 errno = 0;
888 return(GLOB_NOSPACE);
889 }
Damien Millera6e121a2010-10-07 21:39:17 +1100890 copy_error:
Damien Miller3c027682001-03-14 11:39:45 +1100891 return(copy == NULL ? GLOB_NOSPACE : 0);
892}
893
894
895/*
896 * pattern matching function for filenames. Each occurrence of the *
897 * pattern causes a recursion level.
898 */
899static int
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000900match(Char *name, Char *pat, Char *patend, int recur)
Damien Miller3c027682001-03-14 11:39:45 +1100901{
902 int ok, negate_range;
903 Char c, k;
904
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000905 if (recur-- == 0)
906 return(GLOB_NOSPACE);
907
Damien Miller3c027682001-03-14 11:39:45 +1100908 while (pat < patend) {
909 c = *pat++;
910 switch (c & M_MASK) {
911 case M_ALL:
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000912 while (pat < patend && (*pat & M_MASK) == M_ALL)
913 pat++; /* eat consecutive '*' */
Damien Miller3c027682001-03-14 11:39:45 +1100914 if (pat == patend)
915 return(1);
Darren Tucker6524d4f2005-11-10 17:02:21 +1100916 do {
Damien Millerc4bf7dd2011-09-22 21:21:48 +1000917 if (match(name, pat, patend, recur))
Damien Miller3c027682001-03-14 11:39:45 +1100918 return(1);
Darren Tucker6524d4f2005-11-10 17:02:21 +1100919 } while (*name++ != EOS);
Damien Miller3c027682001-03-14 11:39:45 +1100920 return(0);
921 case M_ONE:
922 if (*name++ == EOS)
923 return(0);
924 break;
925 case M_SET:
926 ok = 0;
927 if ((k = *name++) == EOS)
928 return(0);
929 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
930 ++pat;
Damien Millera6e121a2010-10-07 21:39:17 +1100931 while (((c = *pat++) & M_MASK) != M_END) {
932 if ((c & M_MASK) == M_CLASS) {
Damien Miller4927aaf2011-01-12 13:32:03 +1100933 Char idx = *pat & M_MASK;
Damien Millera6e121a2010-10-07 21:39:17 +1100934 if (idx < NCCLASSES &&
935 cclasses[idx].isctype(k))
936 ok = 1;
937 ++pat;
938 }
Damien Miller3c027682001-03-14 11:39:45 +1100939 if ((*pat & M_MASK) == M_RNG) {
940 if (c <= k && k <= pat[1])
941 ok = 1;
942 pat += 2;
943 } else if (c == k)
944 ok = 1;
Damien Millera6e121a2010-10-07 21:39:17 +1100945 }
Damien Miller3c027682001-03-14 11:39:45 +1100946 if (ok == negate_range)
947 return(0);
948 break;
949 default:
950 if (*name++ != c)
951 return(0);
952 break;
953 }
954 }
955 return(*name == EOS);
956}
957
958/* Free allocated data belonging to a glob_t structure. */
959void
Darren Tucker6524d4f2005-11-10 17:02:21 +1100960globfree(glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +1100961{
Darren Tucker6524d4f2005-11-10 17:02:21 +1100962 int i;
963 char **pp;
Damien Miller3c027682001-03-14 11:39:45 +1100964
965 if (pglob->gl_pathv != NULL) {
966 pp = pglob->gl_pathv + pglob->gl_offs;
967 for (i = pglob->gl_pathc; i--; ++pp)
968 if (*pp)
969 free(*pp);
970 free(pglob->gl_pathv);
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000971 pglob->gl_pathv = NULL;
Damien Miller3c027682001-03-14 11:39:45 +1100972 }
Damien Millera6e121a2010-10-07 21:39:17 +1100973 if (pglob->gl_statv != NULL) {
974 for (i = 0; i < pglob->gl_pathc; i++) {
975 if (pglob->gl_statv[i] != NULL)
976 free(pglob->gl_statv[i]);
977 }
978 free(pglob->gl_statv);
979 pglob->gl_statv = NULL;
980 }
Damien Miller3c027682001-03-14 11:39:45 +1100981}
982
983static DIR *
Darren Tucker6524d4f2005-11-10 17:02:21 +1100984g_opendir(Char *str, glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +1100985{
986 char buf[MAXPATHLEN];
987
988 if (!*str)
Damien Miller71eb0c12002-09-11 10:29:11 +1000989 strlcpy(buf, ".", sizeof buf);
Damien Millerb68af622001-03-28 21:05:26 +1000990 else {
Damien Miller6e77a532001-04-14 00:22:33 +1000991 if (g_Ctoc(str, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000992 return(NULL);
993 }
Damien Miller3c027682001-03-14 11:39:45 +1100994
995 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
996 return((*pglob->gl_opendir)(buf));
997
998 return(opendir(buf));
999}
1000
1001static int
Darren Tucker6524d4f2005-11-10 17:02:21 +11001002g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +11001003{
1004 char buf[MAXPATHLEN];
1005
Damien Miller6e77a532001-04-14 00:22:33 +10001006 if (g_Ctoc(fn, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +10001007 return(-1);
Damien Miller3c027682001-03-14 11:39:45 +11001008 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1009 return((*pglob->gl_lstat)(buf, sb));
1010 return(lstat(buf, sb));
1011}
1012
1013static int
Darren Tucker6524d4f2005-11-10 17:02:21 +11001014g_stat(Char *fn, struct stat *sb, glob_t *pglob)
Damien Miller3c027682001-03-14 11:39:45 +11001015{
1016 char buf[MAXPATHLEN];
1017
Damien Miller6e77a532001-04-14 00:22:33 +10001018 if (g_Ctoc(fn, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +10001019 return(-1);
Damien Miller3c027682001-03-14 11:39:45 +11001020 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1021 return((*pglob->gl_stat)(buf, sb));
1022 return(stat(buf, sb));
1023}
1024
1025static Char *
Damien Millera6e121a2010-10-07 21:39:17 +11001026g_strchr(const Char *str, int ch)
Damien Miller3c027682001-03-14 11:39:45 +11001027{
1028 do {
1029 if (*str == ch)
Damien Millera6e121a2010-10-07 21:39:17 +11001030 return ((Char *)str);
Damien Miller3c027682001-03-14 11:39:45 +11001031 } while (*str++);
1032 return (NULL);
1033}
1034
Damien Millerb68af622001-03-28 21:05:26 +10001035static int
Darren Tucker6524d4f2005-11-10 17:02:21 +11001036g_Ctoc(const Char *str, char *buf, u_int len)
Damien Miller3c027682001-03-14 11:39:45 +11001037{
Damien Miller3c027682001-03-14 11:39:45 +11001038
Damien Miller6e77a532001-04-14 00:22:33 +10001039 while (len--) {
1040 if ((*buf++ = *str++) == EOS)
1041 return (0);
1042 }
1043 return (1);
Damien Miller3c027682001-03-14 11:39:45 +11001044}
1045
1046#ifdef DEBUG
1047static void
Darren Tucker6524d4f2005-11-10 17:02:21 +11001048qprintf(const char *str, Char *s)
Damien Miller3c027682001-03-14 11:39:45 +11001049{
Darren Tucker6524d4f2005-11-10 17:02:21 +11001050 Char *p;
Damien Miller3c027682001-03-14 11:39:45 +11001051
1052 (void)printf("%s:\n", str);
1053 for (p = s; *p; p++)
1054 (void)printf("%c", CHAR(*p));
1055 (void)printf("\n");
1056 for (p = s; *p; p++)
1057 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1058 (void)printf("\n");
1059 for (p = s; *p; p++)
1060 (void)printf("%c", ismeta(*p) ? '_' : ' ');
1061 (void)printf("\n");
1062}
1063#endif
1064
Ben Lindstrom45b14db2001-03-17 01:15:38 +00001065#endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
Damien Millera6e121a2010-10-07 21:39:17 +11001066 !defined(GLOB_HAS_GL_MATCHC) || !defined(GLOB_HAS_GL_STATV) */