]> andersk Git - openssh.git/blame - openbsd-compat/glob.c
- markus@cvs.openbsd.org 2001/04/04 00:06:54
[openssh.git] / openbsd-compat / glob.c
CommitLineData
84ceda19 1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Guido van Rossum.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include "includes.h"
38#include <ctype.h>
39
9887f269 40long
41get_arg_max()
42{
43#ifdef ARG_MAX
44 return(ARG_MAX);
45#elif defined(HAVE_SYSCONF) && defined(_SC_ARG_MAX)
46 return(sysconf(_SC_ARG_MAX));
47#else
48 return(256); /* XXX: arbitrary */
49#endif
50}
51
40849fdb 52#if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
53 !defined(GLOB_HAS_GL_MATCHC)
84ceda19 54
55#if defined(LIBC_SCCS) && !defined(lint)
56#if 0
57static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
58#else
b7a2a476 59static char rcsid[] = "$OpenBSD: glob.c,v 1.14 2001/03/28 20:54:19 millert Exp $";
84ceda19 60#endif
61#endif /* LIBC_SCCS and not lint */
62
63/*
64 * glob(3) -- a superset of the one defined in POSIX 1003.2.
65 *
66 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
67 *
68 * Optional extra services, controlled by flags not defined by POSIX:
69 *
70 * GLOB_QUOTE:
71 * Escaping convention: \ inhibits any special meaning the following
72 * character might have (except \ at end of string is retained).
73 * GLOB_MAGCHAR:
74 * Set in gl_flags if pattern contained a globbing character.
75 * GLOB_NOMAGIC:
76 * Same as GLOB_NOCHECK, but it will only append pattern if it did
77 * not contain any magic characters. [Used in csh style globbing]
78 * GLOB_ALTDIRFUNC:
79 * Use alternately specified directory access functions.
80 * GLOB_TILDE:
81 * expand ~user/foo to the /home/dir/of/user/foo
82 * GLOB_BRACE:
83 * expand {1,2}{a,b} to 1a 1b 2a 2b
84 * gl_matchc:
85 * Number of matches in the current invocation of glob.
86 */
87
88
89#define DOLLAR '$'
90#define DOT '.'
91#define EOS '\0'
92#define LBRACKET '['
93#define NOT '!'
94#define QUESTION '?'
95#define QUOTE '\\'
96#define RANGE '-'
97#define RBRACKET ']'
98#define SEP '/'
99#define STAR '*'
100#define TILDE '~'
101#define UNDERSCORE '_'
102#define LBRACE '{'
103#define RBRACE '}'
104#define SLASH '/'
105#define COMMA ','
106
107#ifndef DEBUG
108
109#define M_QUOTE 0x8000
110#define M_PROTECT 0x4000
111#define M_MASK 0xffff
112#define M_ASCII 0x00ff
113
114typedef u_short Char;
115
116#else
117
118#define M_QUOTE 0x80
119#define M_PROTECT 0x40
120#define M_MASK 0xff
121#define M_ASCII 0x7f
122
123typedef char Char;
124
125#endif
126
127
128#define CHAR(c) ((Char)((c)&M_ASCII))
129#define META(c) ((Char)((c)|M_QUOTE))
130#define M_ALL META('*')
131#define M_END META(']')
132#define M_NOT META('!')
133#define M_ONE META('?')
134#define M_RNG META('-')
135#define M_SET META('[')
136#define ismeta(c) (((c)&M_QUOTE) != 0)
137
138
139static int compare __P((const void *, const void *));
8d0cc79b 140static int g_Ctoc __P((const Char *, char *, char *));
84ceda19 141static int g_lstat __P((Char *, struct stat *, glob_t *));
142static DIR *g_opendir __P((Char *, glob_t *));
143static Char *g_strchr __P((Char *, int));
84ceda19 144static int g_stat __P((Char *, struct stat *, glob_t *));
145static int glob0 __P((const Char *, glob_t *));
b7a2a476 146static int glob1 __P((Char *, Char *, glob_t *, size_t *));
147static int glob2 __P((Char *, Char *, Char *, Char *, Char *, Char *,
148 glob_t *, size_t *));
149static int glob3 __P((Char *, Char *, Char *, Char *, Char *, Char *,
150 Char *, Char *, glob_t *, size_t *));
8d539493 151static int globextend __P((const Char *, glob_t *, size_t *));
152static const Char *
153 globtilde __P((const Char *, Char *, size_t, glob_t *));
84ceda19 154static int globexp1 __P((const Char *, glob_t *));
155static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
156static int match __P((Char *, Char *, Char *));
157#ifdef DEBUG
158static void qprintf __P((const char *, Char *));
159#endif
160
161int
162glob(pattern, flags, errfunc, pglob)
163 const char *pattern;
164 int flags, (*errfunc) __P((const char *, int));
165 glob_t *pglob;
166{
167 const u_char *patnext;
168 int c;
b7a2a476 169 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
84ceda19 170
171 patnext = (u_char *) pattern;
172 if (!(flags & GLOB_APPEND)) {
173 pglob->gl_pathc = 0;
174 pglob->gl_pathv = NULL;
175 if (!(flags & GLOB_DOOFFS))
176 pglob->gl_offs = 0;
177 }
178 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
179 pglob->gl_errfunc = errfunc;
180 pglob->gl_matchc = 0;
181
182 bufnext = patbuf;
b7a2a476 183 bufend = bufnext + MAXPATHLEN - 1;
84ceda19 184 if (flags & GLOB_NOESCAPE)
8d0cc79b 185 while (bufnext < bufend && (c = *patnext++) != EOS)
186 *bufnext++ = c;
84ceda19 187 else {
188 /* Protect the quoted characters. */
189 while (bufnext < bufend && (c = *patnext++) != EOS)
190 if (c == QUOTE) {
191 if ((c = *patnext++) == EOS) {
192 c = QUOTE;
193 --patnext;
194 }
195 *bufnext++ = c | M_PROTECT;
196 }
197 else
198 *bufnext++ = c;
199 }
200 *bufnext = EOS;
201
202 if (flags & GLOB_BRACE)
203 return globexp1(patbuf, pglob);
204 else
205 return glob0(patbuf, pglob);
206}
207
208/*
209 * Expand recursively a glob {} pattern. When there is no more expansion
210 * invoke the standard globbing routine to glob the rest of the magic
211 * characters
212 */
b7a2a476 213static int
214globexp1(pattern, pglob)
84ceda19 215 const Char *pattern;
216 glob_t *pglob;
217{
218 const Char* ptr = pattern;
219 int rv;
220
221 /* Protect a single {}, for find(1), like csh */
222 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
223 return glob0(pattern, pglob);
224
225 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
226 if (!globexp2(ptr, pattern, pglob, &rv))
227 return rv;
228
229 return glob0(pattern, pglob);
230}
231
232
233/*
234 * Recursive brace globbing helper. Tries to expand a single brace.
235 * If it succeeds then it invokes globexp1 with the new pattern.
236 * If it fails then it tries to glob the rest of the pattern and returns.
237 */
b7a2a476 238static int
239globexp2(ptr, pattern, pglob, rv)
84ceda19 240 const Char *ptr, *pattern;
241 glob_t *pglob;
242 int *rv;
243{
244 int i;
245 Char *lm, *ls;
246 const Char *pe, *pm, *pl;
b7a2a476 247 Char patbuf[MAXPATHLEN];
84ceda19 248
249 /* copy part up to the brace */
250 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
251 continue;
b7a2a476 252 *lm = EOS;
84ceda19 253 ls = lm;
254
255 /* Find the balanced brace */
256 for (i = 0, pe = ++ptr; *pe; pe++)
257 if (*pe == LBRACKET) {
258 /* Ignore everything between [] */
259 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
260 continue;
261 if (*pe == EOS) {
262 /*
263 * We could not find a matching RBRACKET.
264 * Ignore and just look for RBRACE
265 */
266 pe = pm;
267 }
268 }
269 else if (*pe == LBRACE)
270 i++;
271 else if (*pe == RBRACE) {
272 if (i == 0)
273 break;
274 i--;
275 }
276
277 /* Non matching braces; just glob the pattern */
278 if (i != 0 || *pe == EOS) {
279 *rv = glob0(patbuf, pglob);
280 return 0;
281 }
282
8d0cc79b 283 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
84ceda19 284 switch (*pm) {
285 case LBRACKET:
286 /* Ignore everything between [] */
287 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
288 continue;
289 if (*pm == EOS) {
290 /*
291 * We could not find a matching RBRACKET.
292 * Ignore and just look for RBRACE
293 */
294 pm = pl;
295 }
296 break;
297
298 case LBRACE:
299 i++;
300 break;
301
302 case RBRACE:
303 if (i) {
8d0cc79b 304 i--;
305 break;
84ceda19 306 }
307 /* FALLTHROUGH */
308 case COMMA:
309 if (i && *pm == COMMA)
310 break;
311 else {
312 /* Append the current string */
313 for (lm = ls; (pl < pm); *lm++ = *pl++)
314 continue;
315 /*
316 * Append the rest of the pattern after the
317 * closing brace
318 */
319 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
320 continue;
321
322 /* Expand the current pattern */
323#ifdef DEBUG
324 qprintf("globexp2:", patbuf);
325#endif
326 *rv = globexp1(patbuf, pglob);
327
328 /* move after the comma, to the next string */
329 pl = pm + 1;
330 }
331 break;
332
333 default:
334 break;
335 }
8d0cc79b 336 }
84ceda19 337 *rv = 0;
338 return 0;
339}
340
341
342
343/*
344 * expand tilde from the passwd file.
345 */
346static const Char *
347globtilde(pattern, patbuf, patbuf_len, pglob)
348 const Char *pattern;
349 Char *patbuf;
350 size_t patbuf_len;
351 glob_t *pglob;
352{
353 struct passwd *pwd;
354 char *h;
355 const Char *p;
356 Char *b, *eb;
357
358 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
359 return pattern;
360
361 /* Copy up to the end of the string or / */
362 eb = &patbuf[patbuf_len - 1];
363 for (p = pattern + 1, h = (char *) patbuf;
364 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
365 continue;
366
367 *h = EOS;
368
369 if (((char *) patbuf)[0] == EOS) {
370 /*
371 * handle a plain ~ or ~/ by expanding $HOME
372 * first and then trying the password file
373 */
374#if 0
375 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
376#endif
377 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
378 if ((pwd = getpwuid(getuid())) == NULL)
379 return pattern;
380 else
381 h = pwd->pw_dir;
382 }
383 }
384 else {
385 /*
386 * Expand a ~user
387 */
388 if ((pwd = getpwnam((char*) patbuf)) == NULL)
389 return pattern;
390 else
391 h = pwd->pw_dir;
392 }
393
394 /* Copy the home directory */
395 for (b = patbuf; b < eb && *h; *b++ = *h++)
396 continue;
397
398 /* Append the rest of the pattern */
399 while (b < eb && (*b++ = *p++) != EOS)
400 continue;
401 *b = EOS;
402
403 return patbuf;
404}
405
406
407/*
408 * The main glob() routine: compiles the pattern (optionally processing
409 * quotes), calls glob1() to do the real pattern matching, and finally
410 * sorts the list (unless unsorted operation is requested). Returns 0
411 * if things went well, nonzero if errors occurred. It is not an error
412 * to find no matches.
413 */
414static int
415glob0(pattern, pglob)
416 const Char *pattern;
417 glob_t *pglob;
418{
419 const Char *qpatnext;
420 int c, err, oldpathc;
b7a2a476 421 Char *bufnext, patbuf[MAXPATHLEN];
8d539493 422 size_t limit = 0;
84ceda19 423
b7a2a476 424 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
84ceda19 425 oldpathc = pglob->gl_pathc;
426 bufnext = patbuf;
427
428 /* We don't need to check for buffer overflow any more. */
429 while ((c = *qpatnext++) != EOS) {
430 switch (c) {
431 case LBRACKET:
432 c = *qpatnext;
433 if (c == NOT)
434 ++qpatnext;
435 if (*qpatnext == EOS ||
436 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
437 *bufnext++ = LBRACKET;
438 if (c == NOT)
439 --qpatnext;
440 break;
441 }
442 *bufnext++ = M_SET;
443 if (c == NOT)
444 *bufnext++ = M_NOT;
445 c = *qpatnext++;
446 do {
447 *bufnext++ = CHAR(c);
448 if (*qpatnext == RANGE &&
449 (c = qpatnext[1]) != RBRACKET) {
450 *bufnext++ = M_RNG;
451 *bufnext++ = CHAR(c);
452 qpatnext += 2;
453 }
454 } while ((c = *qpatnext++) != RBRACKET);
455 pglob->gl_flags |= GLOB_MAGCHAR;
456 *bufnext++ = M_END;
457 break;
458 case QUESTION:
459 pglob->gl_flags |= GLOB_MAGCHAR;
460 *bufnext++ = M_ONE;
461 break;
462 case STAR:
463 pglob->gl_flags |= GLOB_MAGCHAR;
464 /* collapse adjacent stars to one,
465 * to avoid exponential behavior
466 */
467 if (bufnext == patbuf || bufnext[-1] != M_ALL)
8d0cc79b 468 *bufnext++ = M_ALL;
84ceda19 469 break;
470 default:
471 *bufnext++ = CHAR(c);
472 break;
473 }
474 }
475 *bufnext = EOS;
476#ifdef DEBUG
477 qprintf("glob0:", patbuf);
478#endif
479
b7a2a476 480 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
84ceda19 481 return(err);
482
483 /*
484 * If there was no match we are going to append the pattern
485 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
486 * and the pattern did not contain any magic characters
487 * GLOB_NOMAGIC is there just for compatibility with csh.
488 */
489 if (pglob->gl_pathc == oldpathc) {
490 if ((pglob->gl_flags & GLOB_NOCHECK) ||
491 ((pglob->gl_flags & GLOB_NOMAGIC) &&
492 !(pglob->gl_flags & GLOB_MAGCHAR)))
8d539493 493 return(globextend(pattern, pglob, &limit));
84ceda19 494 else
495 return(GLOB_NOMATCH);
496 }
497 if (!(pglob->gl_flags & GLOB_NOSORT))
498 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
499 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
500 return(0);
501}
502
503static int
504compare(p, q)
505 const void *p, *q;
506{
507 return(strcmp(*(char **)p, *(char **)q));
508}
509
510static int
b7a2a476 511glob1(pattern, pattern_last, pglob, limitp)
512 Char *pattern, *pattern_last;
84ceda19 513 glob_t *pglob;
8d539493 514 size_t *limitp;
84ceda19 515{
b7a2a476 516 Char pathbuf[MAXPATHLEN];
84ceda19 517
518 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
519 if (*pattern == EOS)
520 return(0);
b7a2a476 521 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
522 pathbuf, pathbuf+MAXPATHLEN-1,
523 pattern, pattern_last, pglob, limitp));
84ceda19 524}
525
526/*
527 * The functions glob2 and glob3 are mutually recursive; there is one level
528 * of recursion for each segment in the pattern that contains one or more
529 * meta characters.
530 */
531static int
b7a2a476 532glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
533 pattern_last, pglob, limitp)
534 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
535 Char *pattern, *pattern_last;
84ceda19 536 glob_t *pglob;
8d539493 537 size_t *limitp;
84ceda19 538{
539 struct stat sb;
540 Char *p, *q;
541 int anymeta;
542
543 /*
544 * Loop over pattern segments until end of pattern or until
545 * segment with meta character found.
546 */
547 for (anymeta = 0;;) {
548 if (*pattern == EOS) { /* End of pattern? */
549 *pathend = EOS;
550 if (g_lstat(pathbuf, &sb, pglob))
551 return(0);
552
553 if (((pglob->gl_flags & GLOB_MARK) &&
8d0cc79b 554 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
555 (S_ISLNK(sb.st_mode) &&
84ceda19 556 (g_stat(pathbuf, &sb, pglob) == 0) &&
557 S_ISDIR(sb.st_mode)))) {
b7a2a476 558 if (pathend+1 > pathend_last)
559 return (1);
84ceda19 560 *pathend++ = SEP;
561 *pathend = EOS;
562 }
563 ++pglob->gl_matchc;
8d539493 564 return(globextend(pathbuf, pglob, limitp));
84ceda19 565 }
566
567 /* Find end of next segment, copy tentatively to pathend. */
568 q = pathend;
569 p = pattern;
570 while (*p != EOS && *p != SEP) {
571 if (ismeta(*p))
572 anymeta = 1;
b7a2a476 573 if (q+1 > pathend_last)
574 return (1);
84ceda19 575 *q++ = *p++;
576 }
577
578 if (!anymeta) { /* No expansion, do next segment. */
579 pathend = q;
580 pattern = p;
b7a2a476 581 while (*pattern == SEP) {
582 if (pathend+1 > pathend_last)
583 return (1);
84ceda19 584 *pathend++ = *pattern++;
b7a2a476 585 }
8d0cc79b 586 } else
587 /* Need expansion, recurse. */
b7a2a476 588 return(glob3(pathbuf, pathbuf_last, pathend,
589 pathend_last, pattern, pattern_last,
590 p, pattern_last, pglob, limitp));
84ceda19 591 }
592 /* NOTREACHED */
593}
594
595static int
b7a2a476 596glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
597 restpattern, restpattern_last, pglob, limitp)
598 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
599 Char *pattern, *pattern_last, *restpattern, *restpattern_last;
84ceda19 600 glob_t *pglob;
8d539493 601 size_t *limitp;
84ceda19 602{
603 register struct dirent *dp;
604 DIR *dirp;
605 int err;
606 char buf[MAXPATHLEN];
607
608 /*
609 * The readdirfunc declaration can't be prototyped, because it is
610 * assigned, below, to two functions which are prototyped in glob.h
611 * and dirent.h as taking pointers to differently typed opaque
612 * structures.
613 */
614 struct dirent *(*readdirfunc)();
615
b7a2a476 616 if (pathend > pathend_last)
617 return (1);
84ceda19 618 *pathend = EOS;
619 errno = 0;
620
621 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
622 /* TODO: don't call for ENOENT or ENOTDIR? */
623 if (pglob->gl_errfunc) {
8d0cc79b 624 if (g_Ctoc(pathbuf, buf, buf+sizeof(buf)))
625 return(GLOB_ABORTED);
84ceda19 626 if (pglob->gl_errfunc(buf, errno) ||
627 pglob->gl_flags & GLOB_ERR)
8d0cc79b 628 return(GLOB_ABORTED);
84ceda19 629 }
630 return(0);
631 }
632
633 err = 0;
634
635 /* Search directory for matching names. */
636 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
637 readdirfunc = pglob->gl_readdir;
638 else
639 readdirfunc = readdir;
640 while ((dp = (*readdirfunc)(dirp))) {
641 register u_char *sc;
642 register Char *dc;
643
644 /* Initial DOT must be matched literally. */
645 if (dp->d_name[0] == DOT && *pattern != DOT)
646 continue;
b7a2a476 647 dc = pathend;
648 sc = (u_char *) dp->d_name;
649 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
650 ;
651 if (dc >= pathend_last) {
652 *dc = EOS;
653 err = 1;
654 break;
655 }
656
84ceda19 657 if (!match(pathend, pattern, restpattern)) {
658 *pathend = EOS;
659 continue;
660 }
b7a2a476 661 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
662 restpattern, restpattern_last, pglob, limitp);
84ceda19 663 if (err)
664 break;
665 }
666
667 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
668 (*pglob->gl_closedir)(dirp);
669 else
670 closedir(dirp);
671 return(err);
672}
673
674
675/*
676 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
677 * add the new item, and update gl_pathc.
678 *
679 * This assumes the BSD realloc, which only copies the block when its size
680 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
681 * behavior.
682 *
683 * Return 0 if new item added, error code if memory couldn't be allocated.
684 *
685 * Invariant of the glob_t structure:
686 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
687 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
688 */
689static int
8d539493 690globextend(path, pglob, limitp)
84ceda19 691 const Char *path;
692 glob_t *pglob;
8d539493 693 size_t *limitp;
84ceda19 694{
695 register char **pathv;
696 register int i;
8d539493 697 u_int newsize, len;
84ceda19 698 char *copy;
699 const Char *p;
700
701 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
8d539493 702 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
703 malloc(newsize);
84ceda19 704 if (pathv == NULL) {
01022caf 705 if (pglob->gl_pathv) {
84ceda19 706 free(pglob->gl_pathv);
01022caf 707 pglob->gl_pathv = NULL;
708 }
84ceda19 709 return(GLOB_NOSPACE);
710 }
711
712 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
713 /* first time around -- clear initial gl_offs items */
714 pathv += pglob->gl_offs;
715 for (i = pglob->gl_offs; --i >= 0; )
716 *--pathv = NULL;
717 }
718 pglob->gl_pathv = pathv;
719
720 for (p = path; *p++;)
721 continue;
8d539493 722 len = (size_t)(p - path);
723 *limitp += len;
724 if ((copy = malloc(len)) != NULL) {
8d0cc79b 725 if (g_Ctoc(path, copy, copy+len+1)) {
726 free(copy);
727 return(GLOB_NOSPACE);
728 }
84ceda19 729 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
730 }
731 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
8d539493 732
733 if ((pglob->gl_flags & GLOB_LIMIT) &&
9887f269 734 newsize + *limitp >= (u_int) get_arg_max()) {
8d539493 735 errno = 0;
736 return(GLOB_NOSPACE);
737 }
738
84ceda19 739 return(copy == NULL ? GLOB_NOSPACE : 0);
740}
741
742
743/*
744 * pattern matching function for filenames. Each occurrence of the *
745 * pattern causes a recursion level.
746 */
747static int
748match(name, pat, patend)
749 register Char *name, *pat, *patend;
750{
751 int ok, negate_range;
752 Char c, k;
753
754 while (pat < patend) {
755 c = *pat++;
756 switch (c & M_MASK) {
757 case M_ALL:
758 if (pat == patend)
759 return(1);
760 do
761 if (match(name, pat, patend))
762 return(1);
8d0cc79b 763 while (*name++ != EOS)
764 ;
84ceda19 765 return(0);
766 case M_ONE:
767 if (*name++ == EOS)
768 return(0);
769 break;
770 case M_SET:
771 ok = 0;
772 if ((k = *name++) == EOS)
773 return(0);
774 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
775 ++pat;
776 while (((c = *pat++) & M_MASK) != M_END)
777 if ((*pat & M_MASK) == M_RNG) {
778 if (c <= k && k <= pat[1])
779 ok = 1;
780 pat += 2;
781 } else if (c == k)
782 ok = 1;
783 if (ok == negate_range)
784 return(0);
785 break;
786 default:
787 if (*name++ != c)
788 return(0);
789 break;
790 }
791 }
792 return(*name == EOS);
793}
794
795/* Free allocated data belonging to a glob_t structure. */
796void
797globfree(pglob)
798 glob_t *pglob;
799{
800 register int i;
801 register char **pp;
802
803 if (pglob->gl_pathv != NULL) {
804 pp = pglob->gl_pathv + pglob->gl_offs;
805 for (i = pglob->gl_pathc; i--; ++pp)
806 if (*pp)
807 free(*pp);
808 free(pglob->gl_pathv);
01022caf 809 pglob->gl_pathv = NULL;
84ceda19 810 }
811}
812
813static DIR *
814g_opendir(str, pglob)
815 register Char *str;
816 glob_t *pglob;
817{
818 char buf[MAXPATHLEN];
819
820 if (!*str)
821 strcpy(buf, ".");
8d0cc79b 822 else {
823 if (g_Ctoc(str, buf, buf+sizeof(buf)))
824 return(NULL);
825 }
84ceda19 826
827 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
828 return((*pglob->gl_opendir)(buf));
829
830 return(opendir(buf));
831}
832
833static int
834g_lstat(fn, sb, pglob)
835 register Char *fn;
836 struct stat *sb;
837 glob_t *pglob;
838{
839 char buf[MAXPATHLEN];
840
8d0cc79b 841 if (g_Ctoc(fn, buf, buf+sizeof(buf)))
842 return(-1);
84ceda19 843 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
844 return((*pglob->gl_lstat)(buf, sb));
845 return(lstat(buf, sb));
846}
847
848static int
849g_stat(fn, sb, pglob)
850 register Char *fn;
851 struct stat *sb;
852 glob_t *pglob;
853{
854 char buf[MAXPATHLEN];
855
8d0cc79b 856 if (g_Ctoc(fn, buf, buf+sizeof(buf)))
857 return(-1);
84ceda19 858 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
859 return((*pglob->gl_stat)(buf, sb));
860 return(stat(buf, sb));
861}
862
863static Char *
864g_strchr(str, ch)
865 Char *str;
866 int ch;
867{
868 do {
869 if (*str == ch)
870 return (str);
871 } while (*str++);
872 return (NULL);
873}
874
8d0cc79b 875static int
876g_Ctoc(str, buf, ebuf)
84ceda19 877 register const Char *str;
8d0cc79b 878 char *buf, *ebuf;
84ceda19 879{
880 register char *dc;
881
8d0cc79b 882 for (dc = buf; dc < ebuf && (*dc++ = *str++) != EOS;)
84ceda19 883 continue;
8d0cc79b 884 if (dc >= ebuf)
885 return (1);
886 return (0);
84ceda19 887}
888
889#ifdef DEBUG
890static void
891qprintf(str, s)
892 const char *str;
893 register Char *s;
894{
895 register Char *p;
896
897 (void)printf("%s:\n", str);
898 for (p = s; *p; p++)
899 (void)printf("%c", CHAR(*p));
900 (void)printf("\n");
901 for (p = s; *p; p++)
902 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
903 (void)printf("\n");
904 for (p = s; *p; p++)
905 (void)printf("%c", ismeta(*p) ? '_' : ' ');
906 (void)printf("\n");
907}
908#endif
909
40849fdb 910#endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
911 !defined(GLOB_HAS_GL_MATCHC) */
84ceda19 912
This page took 0.382634 seconds and 5 git commands to generate.