blob: 7fafc8c402876680a60607d9f1479f8530f51597 [file] [log] [blame]
Damien Miller3db2e4d2003-11-24 13:33:34 +11001/* OPENBSD ORIGINAL: lib/libc/gen/glob.c */
2
Damien Miller3c027682001-03-14 11:39:45 +11003/*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Guido van Rossum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
Damien Miller329638e2003-06-03 12:12:50 +100018 * 3. Neither the name of the University nor the names of its contributors
Damien Miller3c027682001-03-14 11:39:45 +110019 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include "includes.h"
36#include <ctype.h>
37
Damien Miller79b332d2001-06-27 23:36:08 +100038static long
39get_arg_max(void)
Tim Riced9d5ba22001-03-19 20:46:50 -080040{
41#ifdef ARG_MAX
42 return(ARG_MAX);
43#elif defined(HAVE_SYSCONF) && defined(_SC_ARG_MAX)
44 return(sysconf(_SC_ARG_MAX));
45#else
46 return(256); /* XXX: arbitrary */
47#endif
48}
49
Ben Lindstrom45b14db2001-03-17 01:15:38 +000050#if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
51 !defined(GLOB_HAS_GL_MATCHC)
Damien Miller3c027682001-03-14 11:39:45 +110052
53#if defined(LIBC_SCCS) && !defined(lint)
54#if 0
55static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
56#else
Ben Lindstromaf4a6c32003-08-25 01:10:51 +000057static char rcsid[] = "$OpenBSD: glob.c,v 1.22 2003/06/25 21:16:47 deraadt Exp $";
Damien Miller3c027682001-03-14 11:39:45 +110058#endif
59#endif /* LIBC_SCCS and not lint */
60
61/*
62 * glob(3) -- a superset of the one defined in POSIX 1003.2.
63 *
64 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
65 *
66 * Optional extra services, controlled by flags not defined by POSIX:
67 *
68 * GLOB_QUOTE:
69 * Escaping convention: \ inhibits any special meaning the following
70 * character might have (except \ at end of string is retained).
71 * GLOB_MAGCHAR:
72 * Set in gl_flags if pattern contained a globbing character.
73 * GLOB_NOMAGIC:
74 * Same as GLOB_NOCHECK, but it will only append pattern if it did
75 * not contain any magic characters. [Used in csh style globbing]
76 * GLOB_ALTDIRFUNC:
77 * Use alternately specified directory access functions.
78 * GLOB_TILDE:
79 * expand ~user/foo to the /home/dir/of/user/foo
80 * GLOB_BRACE:
81 * expand {1,2}{a,b} to 1a 1b 2a 2b
82 * gl_matchc:
83 * Number of matches in the current invocation of glob.
84 */
85
86
87#define DOLLAR '$'
88#define DOT '.'
89#define EOS '\0'
90#define LBRACKET '['
91#define NOT '!'
92#define QUESTION '?'
93#define QUOTE '\\'
94#define RANGE '-'
95#define RBRACKET ']'
96#define SEP '/'
97#define STAR '*'
Ben Lindstrom604de562002-07-04 18:20:51 +000098#undef TILDE /* Some platforms may already define it */
Damien Miller3c027682001-03-14 11:39:45 +110099#define TILDE '~'
100#define UNDERSCORE '_'
101#define LBRACE '{'
102#define RBRACE '}'
103#define SLASH '/'
104#define COMMA ','
105
106#ifndef DEBUG
107
108#define M_QUOTE 0x8000
109#define M_PROTECT 0x4000
110#define M_MASK 0xffff
111#define M_ASCII 0x00ff
112
113typedef u_short Char;
114
115#else
116
117#define M_QUOTE 0x80
118#define M_PROTECT 0x40
119#define M_MASK 0xff
120#define M_ASCII 0x7f
121
122typedef char Char;
123
124#endif
125
126
127#define CHAR(c) ((Char)((c)&M_ASCII))
128#define META(c) ((Char)((c)|M_QUOTE))
129#define M_ALL META('*')
130#define M_END META(']')
131#define M_NOT META('!')
132#define M_ONE META('?')
133#define M_RNG META('-')
134#define M_SET META('[')
135#define ismeta(c) (((c)&M_QUOTE) != 0)
136
137
Damien Miller71eb0c12002-09-11 10:29:11 +1000138static int compare(const void *, const void *);
139static int g_Ctoc(const Char *, char *, u_int);
140static int g_lstat(Char *, struct stat *, glob_t *);
141static DIR *g_opendir(Char *, glob_t *);
142static Char *g_strchr(Char *, int);
143static int g_stat(Char *, struct stat *, glob_t *);
144static int glob0(const Char *, glob_t *);
145static int glob1(Char *, Char *, glob_t *, size_t *);
146static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
147 glob_t *, size_t *);
148static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
149 Char *, Char *, glob_t *, size_t *);
150static int globextend(const Char *, glob_t *, size_t *);
Ben Lindstroma77d6412001-03-19 18:58:13 +0000151static const Char *
Damien Miller71eb0c12002-09-11 10:29:11 +1000152 globtilde(const Char *, Char *, size_t, glob_t *);
153static int globexp1(const Char *, glob_t *);
154static int globexp2(const Char *, const Char *, glob_t *, int *);
155static int match(Char *, Char *, Char *);
Damien Miller3c027682001-03-14 11:39:45 +1100156#ifdef DEBUG
Damien Miller71eb0c12002-09-11 10:29:11 +1000157static void qprintf(const char *, Char *);
Damien Miller3c027682001-03-14 11:39:45 +1100158#endif
159
160int
161glob(pattern, flags, errfunc, pglob)
162 const char *pattern;
Damien Miller71eb0c12002-09-11 10:29:11 +1000163 int flags, (*errfunc)(const char *, int);
Damien Miller3c027682001-03-14 11:39:45 +1100164 glob_t *pglob;
165{
166 const u_char *patnext;
167 int c;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000168 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100169
170 patnext = (u_char *) pattern;
171 if (!(flags & GLOB_APPEND)) {
172 pglob->gl_pathc = 0;
173 pglob->gl_pathv = NULL;
174 if (!(flags & GLOB_DOOFFS))
175 pglob->gl_offs = 0;
176 }
177 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
178 pglob->gl_errfunc = errfunc;
179 pglob->gl_matchc = 0;
180
181 bufnext = patbuf;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000182 bufend = bufnext + MAXPATHLEN - 1;
Damien Miller3c027682001-03-14 11:39:45 +1100183 if (flags & GLOB_NOESCAPE)
Damien Millerb68af622001-03-28 21:05:26 +1000184 while (bufnext < bufend && (c = *patnext++) != EOS)
185 *bufnext++ = c;
Damien Miller3c027682001-03-14 11:39:45 +1100186 else {
187 /* Protect the quoted characters. */
188 while (bufnext < bufend && (c = *patnext++) != EOS)
189 if (c == QUOTE) {
190 if ((c = *patnext++) == EOS) {
191 c = QUOTE;
192 --patnext;
193 }
194 *bufnext++ = c | M_PROTECT;
Damien Miller6e77a532001-04-14 00:22:33 +1000195 } else
Damien Miller3c027682001-03-14 11:39:45 +1100196 *bufnext++ = c;
197 }
198 *bufnext = EOS;
199
200 if (flags & GLOB_BRACE)
201 return globexp1(patbuf, pglob);
202 else
203 return glob0(patbuf, pglob);
204}
205
206/*
207 * Expand recursively a glob {} pattern. When there is no more expansion
208 * invoke the standard globbing routine to glob the rest of the magic
209 * characters
210 */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000211static int
212globexp1(pattern, pglob)
Damien Miller3c027682001-03-14 11:39:45 +1100213 const Char *pattern;
214 glob_t *pglob;
215{
216 const Char* ptr = pattern;
217 int rv;
218
219 /* Protect a single {}, for find(1), like csh */
220 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
221 return glob0(pattern, pglob);
222
223 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
224 if (!globexp2(ptr, pattern, pglob, &rv))
225 return rv;
226
227 return glob0(pattern, pglob);
228}
229
230
231/*
232 * Recursive brace globbing helper. Tries to expand a single brace.
233 * If it succeeds then it invokes globexp1 with the new pattern.
234 * If it fails then it tries to glob the rest of the pattern and returns.
235 */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000236static int
237globexp2(ptr, pattern, pglob, rv)
Damien Miller3c027682001-03-14 11:39:45 +1100238 const Char *ptr, *pattern;
239 glob_t *pglob;
240 int *rv;
241{
242 int i;
243 Char *lm, *ls;
244 const Char *pe, *pm, *pl;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000245 Char patbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100246
247 /* copy part up to the brace */
248 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
Damien Miller6e77a532001-04-14 00:22:33 +1000249 ;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000250 *lm = EOS;
Damien Miller3c027682001-03-14 11:39:45 +1100251 ls = lm;
252
253 /* Find the balanced brace */
254 for (i = 0, pe = ++ptr; *pe; pe++)
255 if (*pe == LBRACKET) {
256 /* Ignore everything between [] */
257 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
Damien Miller6e77a532001-04-14 00:22:33 +1000258 ;
Damien Miller3c027682001-03-14 11:39:45 +1100259 if (*pe == EOS) {
260 /*
261 * We could not find a matching RBRACKET.
262 * Ignore and just look for RBRACE
263 */
264 pe = pm;
265 }
Damien Miller6e77a532001-04-14 00:22:33 +1000266 } else if (*pe == LBRACE)
Damien Miller3c027682001-03-14 11:39:45 +1100267 i++;
268 else if (*pe == RBRACE) {
269 if (i == 0)
270 break;
271 i--;
272 }
273
274 /* Non matching braces; just glob the pattern */
275 if (i != 0 || *pe == EOS) {
276 *rv = glob0(patbuf, pglob);
277 return 0;
278 }
279
Damien Millerb68af622001-03-28 21:05:26 +1000280 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
Damien Miller3c027682001-03-14 11:39:45 +1100281 switch (*pm) {
282 case LBRACKET:
283 /* Ignore everything between [] */
284 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
Damien Miller6e77a532001-04-14 00:22:33 +1000285 ;
Damien Miller3c027682001-03-14 11:39:45 +1100286 if (*pm == EOS) {
287 /*
288 * We could not find a matching RBRACKET.
289 * Ignore and just look for RBRACE
290 */
291 pm = pl;
292 }
293 break;
294
295 case LBRACE:
296 i++;
297 break;
298
299 case RBRACE:
300 if (i) {
Damien Millerb68af622001-03-28 21:05:26 +1000301 i--;
302 break;
Damien Miller3c027682001-03-14 11:39:45 +1100303 }
304 /* FALLTHROUGH */
305 case COMMA:
306 if (i && *pm == COMMA)
307 break;
308 else {
309 /* Append the current string */
310 for (lm = ls; (pl < pm); *lm++ = *pl++)
Damien Miller6e77a532001-04-14 00:22:33 +1000311 ;
312
Damien Miller3c027682001-03-14 11:39:45 +1100313 /*
314 * Append the rest of the pattern after the
315 * closing brace
316 */
Damien Miller6e77a532001-04-14 00:22:33 +1000317 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
318 ;
Damien Miller3c027682001-03-14 11:39:45 +1100319
320 /* Expand the current pattern */
321#ifdef DEBUG
322 qprintf("globexp2:", patbuf);
323#endif
324 *rv = globexp1(patbuf, pglob);
325
326 /* move after the comma, to the next string */
327 pl = pm + 1;
328 }
329 break;
330
331 default:
332 break;
333 }
Damien Millerb68af622001-03-28 21:05:26 +1000334 }
Damien Miller3c027682001-03-14 11:39:45 +1100335 *rv = 0;
336 return 0;
337}
338
339
340
341/*
342 * expand tilde from the passwd file.
343 */
344static const Char *
345globtilde(pattern, patbuf, patbuf_len, pglob)
346 const Char *pattern;
347 Char *patbuf;
348 size_t patbuf_len;
349 glob_t *pglob;
350{
351 struct passwd *pwd;
352 char *h;
353 const Char *p;
354 Char *b, *eb;
355
356 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
357 return pattern;
358
359 /* Copy up to the end of the string or / */
360 eb = &patbuf[patbuf_len - 1];
361 for (p = pattern + 1, h = (char *) patbuf;
362 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
Damien Miller6e77a532001-04-14 00:22:33 +1000363 ;
Damien Miller3c027682001-03-14 11:39:45 +1100364
365 *h = EOS;
366
Damien Miller6e77a532001-04-14 00:22:33 +1000367#if 0
368 if (h == (char *)eb)
369 return what;
370#endif
371
Damien Miller3c027682001-03-14 11:39:45 +1100372 if (((char *) patbuf)[0] == EOS) {
373 /*
374 * handle a plain ~ or ~/ by expanding $HOME
375 * first and then trying the password file
376 */
377#if 0
378 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
379#endif
380 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
381 if ((pwd = getpwuid(getuid())) == NULL)
382 return pattern;
383 else
384 h = pwd->pw_dir;
385 }
Damien Miller6e77a532001-04-14 00:22:33 +1000386 } else {
Damien Miller3c027682001-03-14 11:39:45 +1100387 /*
388 * Expand a ~user
389 */
390 if ((pwd = getpwnam((char*) patbuf)) == NULL)
391 return pattern;
392 else
393 h = pwd->pw_dir;
394 }
395
396 /* Copy the home directory */
397 for (b = patbuf; b < eb && *h; *b++ = *h++)
Damien Miller6e77a532001-04-14 00:22:33 +1000398 ;
Damien Miller3c027682001-03-14 11:39:45 +1100399
400 /* Append the rest of the pattern */
401 while (b < eb && (*b++ = *p++) != EOS)
Damien Miller6e77a532001-04-14 00:22:33 +1000402 ;
Damien Miller3c027682001-03-14 11:39:45 +1100403 *b = EOS;
404
405 return patbuf;
406}
407
408
409/*
410 * The main glob() routine: compiles the pattern (optionally processing
411 * quotes), calls glob1() to do the real pattern matching, and finally
412 * sorts the list (unless unsorted operation is requested). Returns 0
413 * if things went well, nonzero if errors occurred. It is not an error
414 * to find no matches.
415 */
416static int
417glob0(pattern, pglob)
418 const Char *pattern;
419 glob_t *pglob;
420{
421 const Char *qpatnext;
422 int c, err, oldpathc;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000423 Char *bufnext, patbuf[MAXPATHLEN];
Ben Lindstroma77d6412001-03-19 18:58:13 +0000424 size_t limit = 0;
Damien Miller3c027682001-03-14 11:39:45 +1100425
Damien Millerd8f72ca2001-03-30 10:23:17 +1000426 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
Damien Miller3c027682001-03-14 11:39:45 +1100427 oldpathc = pglob->gl_pathc;
428 bufnext = patbuf;
429
430 /* We don't need to check for buffer overflow any more. */
431 while ((c = *qpatnext++) != EOS) {
432 switch (c) {
433 case LBRACKET:
434 c = *qpatnext;
435 if (c == NOT)
436 ++qpatnext;
437 if (*qpatnext == EOS ||
438 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
439 *bufnext++ = LBRACKET;
440 if (c == NOT)
441 --qpatnext;
442 break;
443 }
444 *bufnext++ = M_SET;
445 if (c == NOT)
446 *bufnext++ = M_NOT;
447 c = *qpatnext++;
448 do {
449 *bufnext++ = CHAR(c);
450 if (*qpatnext == RANGE &&
451 (c = qpatnext[1]) != RBRACKET) {
452 *bufnext++ = M_RNG;
453 *bufnext++ = CHAR(c);
454 qpatnext += 2;
455 }
456 } while ((c = *qpatnext++) != RBRACKET);
457 pglob->gl_flags |= GLOB_MAGCHAR;
458 *bufnext++ = M_END;
459 break;
460 case QUESTION:
461 pglob->gl_flags |= GLOB_MAGCHAR;
462 *bufnext++ = M_ONE;
463 break;
464 case STAR:
465 pglob->gl_flags |= GLOB_MAGCHAR;
466 /* collapse adjacent stars to one,
467 * to avoid exponential behavior
468 */
469 if (bufnext == patbuf || bufnext[-1] != M_ALL)
Damien Millerb68af622001-03-28 21:05:26 +1000470 *bufnext++ = M_ALL;
Damien Miller3c027682001-03-14 11:39:45 +1100471 break;
472 default:
473 *bufnext++ = CHAR(c);
474 break;
475 }
476 }
477 *bufnext = EOS;
478#ifdef DEBUG
479 qprintf("glob0:", patbuf);
480#endif
481
Damien Millerd8f72ca2001-03-30 10:23:17 +1000482 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
Damien Miller3c027682001-03-14 11:39:45 +1100483 return(err);
484
485 /*
486 * If there was no match we are going to append the pattern
487 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
488 * and the pattern did not contain any magic characters
489 * GLOB_NOMAGIC is there just for compatibility with csh.
490 */
491 if (pglob->gl_pathc == oldpathc) {
492 if ((pglob->gl_flags & GLOB_NOCHECK) ||
493 ((pglob->gl_flags & GLOB_NOMAGIC) &&
494 !(pglob->gl_flags & GLOB_MAGCHAR)))
Ben Lindstroma77d6412001-03-19 18:58:13 +0000495 return(globextend(pattern, pglob, &limit));
Damien Miller3c027682001-03-14 11:39:45 +1100496 else
497 return(GLOB_NOMATCH);
498 }
499 if (!(pglob->gl_flags & GLOB_NOSORT))
500 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
501 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
502 return(0);
503}
504
505static int
506compare(p, q)
507 const void *p, *q;
508{
509 return(strcmp(*(char **)p, *(char **)q));
510}
511
512static int
Damien Millerd8f72ca2001-03-30 10:23:17 +1000513glob1(pattern, pattern_last, pglob, limitp)
514 Char *pattern, *pattern_last;
Damien Miller3c027682001-03-14 11:39:45 +1100515 glob_t *pglob;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000516 size_t *limitp;
Damien Miller3c027682001-03-14 11:39:45 +1100517{
Damien Millerd8f72ca2001-03-30 10:23:17 +1000518 Char pathbuf[MAXPATHLEN];
Damien Miller3c027682001-03-14 11:39:45 +1100519
520 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
521 if (*pattern == EOS)
522 return(0);
Damien Millerd8f72ca2001-03-30 10:23:17 +1000523 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
524 pathbuf, pathbuf+MAXPATHLEN-1,
525 pattern, pattern_last, pglob, limitp));
Damien Miller3c027682001-03-14 11:39:45 +1100526}
527
528/*
529 * The functions glob2 and glob3 are mutually recursive; there is one level
530 * of recursion for each segment in the pattern that contains one or more
531 * meta characters.
532 */
533static int
Damien Millerd8f72ca2001-03-30 10:23:17 +1000534glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
535 pattern_last, pglob, limitp)
536 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
537 Char *pattern, *pattern_last;
Damien Miller3c027682001-03-14 11:39:45 +1100538 glob_t *pglob;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000539 size_t *limitp;
Damien Miller3c027682001-03-14 11:39:45 +1100540{
541 struct stat sb;
542 Char *p, *q;
543 int anymeta;
544
545 /*
546 * Loop over pattern segments until end of pattern or until
547 * segment with meta character found.
548 */
549 for (anymeta = 0;;) {
550 if (*pattern == EOS) { /* End of pattern? */
551 *pathend = EOS;
552 if (g_lstat(pathbuf, &sb, pglob))
553 return(0);
554
555 if (((pglob->gl_flags & GLOB_MARK) &&
Damien Millerb68af622001-03-28 21:05:26 +1000556 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
557 (S_ISLNK(sb.st_mode) &&
Damien Miller3c027682001-03-14 11:39:45 +1100558 (g_stat(pathbuf, &sb, pglob) == 0) &&
559 S_ISDIR(sb.st_mode)))) {
Damien Millerd8f72ca2001-03-30 10:23:17 +1000560 if (pathend+1 > pathend_last)
561 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100562 *pathend++ = SEP;
563 *pathend = EOS;
564 }
565 ++pglob->gl_matchc;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000566 return(globextend(pathbuf, pglob, limitp));
Damien Miller3c027682001-03-14 11:39:45 +1100567 }
568
569 /* Find end of next segment, copy tentatively to pathend. */
570 q = pathend;
571 p = pattern;
572 while (*p != EOS && *p != SEP) {
573 if (ismeta(*p))
574 anymeta = 1;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000575 if (q+1 > pathend_last)
576 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100577 *q++ = *p++;
578 }
579
580 if (!anymeta) { /* No expansion, do next segment. */
581 pathend = q;
582 pattern = p;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000583 while (*pattern == SEP) {
584 if (pathend+1 > pathend_last)
585 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100586 *pathend++ = *pattern++;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000587 }
Damien Millerb68af622001-03-28 21:05:26 +1000588 } else
589 /* Need expansion, recurse. */
Damien Millerd8f72ca2001-03-30 10:23:17 +1000590 return(glob3(pathbuf, pathbuf_last, pathend,
591 pathend_last, pattern, pattern_last,
592 p, pattern_last, pglob, limitp));
Damien Miller3c027682001-03-14 11:39:45 +1100593 }
594 /* NOTREACHED */
595}
596
597static int
Damien Millerd8f72ca2001-03-30 10:23:17 +1000598glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
599 restpattern, restpattern_last, pglob, limitp)
600 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
601 Char *pattern, *pattern_last, *restpattern, *restpattern_last;
Damien Miller3c027682001-03-14 11:39:45 +1100602 glob_t *pglob;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000603 size_t *limitp;
Damien Miller3c027682001-03-14 11:39:45 +1100604{
605 register struct dirent *dp;
606 DIR *dirp;
607 int err;
608 char buf[MAXPATHLEN];
609
610 /*
611 * The readdirfunc declaration can't be prototyped, because it is
612 * assigned, below, to two functions which are prototyped in glob.h
613 * and dirent.h as taking pointers to differently typed opaque
614 * structures.
615 */
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000616 struct dirent *(*readdirfunc)(void *);
Damien Miller3c027682001-03-14 11:39:45 +1100617
Damien Millerd8f72ca2001-03-30 10:23:17 +1000618 if (pathend > pathend_last)
619 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100620 *pathend = EOS;
621 errno = 0;
622
623 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
624 /* TODO: don't call for ENOENT or ENOTDIR? */
625 if (pglob->gl_errfunc) {
Damien Miller6e77a532001-04-14 00:22:33 +1000626 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000627 return(GLOB_ABORTED);
Damien Miller3c027682001-03-14 11:39:45 +1100628 if (pglob->gl_errfunc(buf, errno) ||
629 pglob->gl_flags & GLOB_ERR)
Damien Millerb68af622001-03-28 21:05:26 +1000630 return(GLOB_ABORTED);
Damien Miller3c027682001-03-14 11:39:45 +1100631 }
632 return(0);
633 }
634
635 err = 0;
636
637 /* Search directory for matching names. */
638 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
639 readdirfunc = pglob->gl_readdir;
640 else
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000641 readdirfunc = (struct dirent *(*)(void *))readdir;
Damien Miller3c027682001-03-14 11:39:45 +1100642 while ((dp = (*readdirfunc)(dirp))) {
643 register u_char *sc;
644 register Char *dc;
645
646 /* Initial DOT must be matched literally. */
647 if (dp->d_name[0] == DOT && *pattern != DOT)
648 continue;
Damien Millerd8f72ca2001-03-30 10:23:17 +1000649 dc = pathend;
650 sc = (u_char *) dp->d_name;
651 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
652 ;
653 if (dc >= pathend_last) {
654 *dc = EOS;
655 err = 1;
656 break;
657 }
658
Damien Miller3c027682001-03-14 11:39:45 +1100659 if (!match(pathend, pattern, restpattern)) {
660 *pathend = EOS;
661 continue;
662 }
Damien Millerd8f72ca2001-03-30 10:23:17 +1000663 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
664 restpattern, restpattern_last, pglob, limitp);
Damien Miller3c027682001-03-14 11:39:45 +1100665 if (err)
666 break;
667 }
668
669 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
670 (*pglob->gl_closedir)(dirp);
671 else
672 closedir(dirp);
673 return(err);
674}
675
676
677/*
Damien Miller71eb0c12002-09-11 10:29:11 +1000678 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
Damien Miller3c027682001-03-14 11:39:45 +1100679 * add the new item, and update gl_pathc.
680 *
681 * This assumes the BSD realloc, which only copies the block when its size
682 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
683 * behavior.
684 *
685 * Return 0 if new item added, error code if memory couldn't be allocated.
686 *
687 * Invariant of the glob_t structure:
688 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
689 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
690 */
691static int
Ben Lindstroma77d6412001-03-19 18:58:13 +0000692globextend(path, pglob, limitp)
Damien Miller3c027682001-03-14 11:39:45 +1100693 const Char *path;
694 glob_t *pglob;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000695 size_t *limitp;
Damien Miller3c027682001-03-14 11:39:45 +1100696{
697 register char **pathv;
698 register int i;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000699 u_int newsize, len;
Damien Miller3c027682001-03-14 11:39:45 +1100700 char *copy;
701 const Char *p;
702
703 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
Ben Lindstroma77d6412001-03-19 18:58:13 +0000704 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
705 malloc(newsize);
Damien Miller3c027682001-03-14 11:39:45 +1100706 if (pathv == NULL) {
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000707 if (pglob->gl_pathv) {
Damien Miller3c027682001-03-14 11:39:45 +1100708 free(pglob->gl_pathv);
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000709 pglob->gl_pathv = NULL;
710 }
Damien Miller3c027682001-03-14 11:39:45 +1100711 return(GLOB_NOSPACE);
712 }
713
714 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
715 /* first time around -- clear initial gl_offs items */
716 pathv += pglob->gl_offs;
717 for (i = pglob->gl_offs; --i >= 0; )
718 *--pathv = NULL;
719 }
720 pglob->gl_pathv = pathv;
721
722 for (p = path; *p++;)
Damien Miller6e77a532001-04-14 00:22:33 +1000723 ;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000724 len = (size_t)(p - path);
725 *limitp += len;
726 if ((copy = malloc(len)) != NULL) {
Damien Miller6e77a532001-04-14 00:22:33 +1000727 if (g_Ctoc(path, copy, len)) {
Damien Millerb68af622001-03-28 21:05:26 +1000728 free(copy);
729 return(GLOB_NOSPACE);
730 }
Damien Miller3c027682001-03-14 11:39:45 +1100731 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
732 }
733 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
Ben Lindstroma77d6412001-03-19 18:58:13 +0000734
735 if ((pglob->gl_flags & GLOB_LIMIT) &&
Tim Riced9d5ba22001-03-19 20:46:50 -0800736 newsize + *limitp >= (u_int) get_arg_max()) {
Ben Lindstroma77d6412001-03-19 18:58:13 +0000737 errno = 0;
738 return(GLOB_NOSPACE);
739 }
740
Damien Miller3c027682001-03-14 11:39:45 +1100741 return(copy == NULL ? GLOB_NOSPACE : 0);
742}
743
744
745/*
746 * pattern matching function for filenames. Each occurrence of the *
747 * pattern causes a recursion level.
748 */
749static int
750match(name, pat, patend)
751 register Char *name, *pat, *patend;
752{
753 int ok, negate_range;
754 Char c, k;
755
756 while (pat < patend) {
757 c = *pat++;
758 switch (c & M_MASK) {
759 case M_ALL:
760 if (pat == patend)
761 return(1);
762 do
763 if (match(name, pat, patend))
764 return(1);
Damien Millerb68af622001-03-28 21:05:26 +1000765 while (*name++ != EOS)
766 ;
Damien Miller3c027682001-03-14 11:39:45 +1100767 return(0);
768 case M_ONE:
769 if (*name++ == EOS)
770 return(0);
771 break;
772 case M_SET:
773 ok = 0;
774 if ((k = *name++) == EOS)
775 return(0);
776 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
777 ++pat;
778 while (((c = *pat++) & M_MASK) != M_END)
779 if ((*pat & M_MASK) == M_RNG) {
780 if (c <= k && k <= pat[1])
781 ok = 1;
782 pat += 2;
783 } else if (c == k)
784 ok = 1;
785 if (ok == negate_range)
786 return(0);
787 break;
788 default:
789 if (*name++ != c)
790 return(0);
791 break;
792 }
793 }
794 return(*name == EOS);
795}
796
797/* Free allocated data belonging to a glob_t structure. */
798void
799globfree(pglob)
800 glob_t *pglob;
801{
802 register int i;
803 register char **pp;
804
805 if (pglob->gl_pathv != NULL) {
806 pp = pglob->gl_pathv + pglob->gl_offs;
807 for (i = pglob->gl_pathc; i--; ++pp)
808 if (*pp)
809 free(*pp);
810 free(pglob->gl_pathv);
Ben Lindstrom11c78f82001-03-19 19:00:09 +0000811 pglob->gl_pathv = NULL;
Damien Miller3c027682001-03-14 11:39:45 +1100812 }
813}
814
815static DIR *
816g_opendir(str, pglob)
817 register Char *str;
818 glob_t *pglob;
819{
820 char buf[MAXPATHLEN];
821
822 if (!*str)
Damien Miller71eb0c12002-09-11 10:29:11 +1000823 strlcpy(buf, ".", sizeof buf);
Damien Millerb68af622001-03-28 21:05:26 +1000824 else {
Damien Miller6e77a532001-04-14 00:22:33 +1000825 if (g_Ctoc(str, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000826 return(NULL);
827 }
Damien Miller3c027682001-03-14 11:39:45 +1100828
829 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
830 return((*pglob->gl_opendir)(buf));
831
832 return(opendir(buf));
833}
834
835static int
836g_lstat(fn, sb, pglob)
837 register Char *fn;
838 struct stat *sb;
839 glob_t *pglob;
840{
841 char buf[MAXPATHLEN];
842
Damien Miller6e77a532001-04-14 00:22:33 +1000843 if (g_Ctoc(fn, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000844 return(-1);
Damien Miller3c027682001-03-14 11:39:45 +1100845 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
846 return((*pglob->gl_lstat)(buf, sb));
847 return(lstat(buf, sb));
848}
849
850static int
851g_stat(fn, sb, pglob)
852 register Char *fn;
853 struct stat *sb;
854 glob_t *pglob;
855{
856 char buf[MAXPATHLEN];
857
Damien Miller6e77a532001-04-14 00:22:33 +1000858 if (g_Ctoc(fn, buf, sizeof(buf)))
Damien Millerb68af622001-03-28 21:05:26 +1000859 return(-1);
Damien Miller3c027682001-03-14 11:39:45 +1100860 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
861 return((*pglob->gl_stat)(buf, sb));
862 return(stat(buf, sb));
863}
864
865static Char *
866g_strchr(str, ch)
867 Char *str;
868 int ch;
869{
870 do {
871 if (*str == ch)
872 return (str);
873 } while (*str++);
874 return (NULL);
875}
876
Damien Millerb68af622001-03-28 21:05:26 +1000877static int
Damien Miller6e77a532001-04-14 00:22:33 +1000878g_Ctoc(str, buf, len)
Damien Miller3c027682001-03-14 11:39:45 +1100879 register const Char *str;
Damien Miller6e77a532001-04-14 00:22:33 +1000880 char *buf;
881 u_int len;
Damien Miller3c027682001-03-14 11:39:45 +1100882{
Damien Miller3c027682001-03-14 11:39:45 +1100883
Damien Miller6e77a532001-04-14 00:22:33 +1000884 while (len--) {
885 if ((*buf++ = *str++) == EOS)
886 return (0);
887 }
888 return (1);
Damien Miller3c027682001-03-14 11:39:45 +1100889}
890
891#ifdef DEBUG
892static void
893qprintf(str, s)
894 const char *str;
895 register Char *s;
896{
897 register Char *p;
898
899 (void)printf("%s:\n", str);
900 for (p = s; *p; p++)
901 (void)printf("%c", CHAR(*p));
902 (void)printf("\n");
903 for (p = s; *p; p++)
904 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
905 (void)printf("\n");
906 for (p = s; *p; p++)
907 (void)printf("%c", ismeta(*p) ? '_' : ' ');
908 (void)printf("\n");
909}
910#endif
911
Ben Lindstrom45b14db2001-03-17 01:15:38 +0000912#endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
913 !defined(GLOB_HAS_GL_MATCHC) */
Damien Miller3c027682001-03-14 11:39:45 +1100914