]> andersk Git - openssh.git/blame - openbsd-compat/glob.c
- (djm) Bug #138: Make protocol 1 blowfish work with old OpenSSL.
[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
d60382e0 40static long
41get_arg_max(void)
9887f269 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
e400a640 59static char rcsid[] = "$OpenBSD: glob.c,v 1.16 2001/04/05 18:36:12 deraadt 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 '*'
375c1dee 100#undef TILDE /* Some platforms may already define it */
84ceda19 101#define TILDE '~'
102#define UNDERSCORE '_'
103#define LBRACE '{'
104#define RBRACE '}'
105#define SLASH '/'
106#define COMMA ','
107
108#ifndef DEBUG
109
110#define M_QUOTE 0x8000
111#define M_PROTECT 0x4000
112#define M_MASK 0xffff
113#define M_ASCII 0x00ff
114
115typedef u_short Char;
116
117#else
118
119#define M_QUOTE 0x80
120#define M_PROTECT 0x40
121#define M_MASK 0xff
122#define M_ASCII 0x7f
123
124typedef char Char;
125
126#endif
127
128
129#define CHAR(c) ((Char)((c)&M_ASCII))
130#define META(c) ((Char)((c)|M_QUOTE))
131#define M_ALL META('*')
132#define M_END META(']')
133#define M_NOT META('!')
134#define M_ONE META('?')
135#define M_RNG META('-')
136#define M_SET META('[')
137#define ismeta(c) (((c)&M_QUOTE) != 0)
138
139
140static int compare __P((const void *, const void *));
e400a640 141static int g_Ctoc __P((const Char *, char *, u_int));
84ceda19 142static int g_lstat __P((Char *, struct stat *, glob_t *));
143static DIR *g_opendir __P((Char *, glob_t *));
144static Char *g_strchr __P((Char *, int));
84ceda19 145static int g_stat __P((Char *, struct stat *, glob_t *));
146static int glob0 __P((const Char *, glob_t *));
b7a2a476 147static int glob1 __P((Char *, Char *, glob_t *, size_t *));
148static int glob2 __P((Char *, Char *, Char *, Char *, Char *, Char *,
149 glob_t *, size_t *));
150static int glob3 __P((Char *, Char *, Char *, Char *, Char *, Char *,
151 Char *, Char *, glob_t *, size_t *));
8d539493 152static int globextend __P((const Char *, glob_t *, size_t *));
153static const Char *
154 globtilde __P((const Char *, Char *, size_t, glob_t *));
84ceda19 155static int globexp1 __P((const Char *, glob_t *));
156static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
157static int match __P((Char *, Char *, Char *));
158#ifdef DEBUG
159static void qprintf __P((const char *, Char *));
160#endif
161
162int
163glob(pattern, flags, errfunc, pglob)
164 const char *pattern;
165 int flags, (*errfunc) __P((const char *, int));
166 glob_t *pglob;
167{
168 const u_char *patnext;
169 int c;
b7a2a476 170 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
84ceda19 171
172 patnext = (u_char *) pattern;
173 if (!(flags & GLOB_APPEND)) {
174 pglob->gl_pathc = 0;
175 pglob->gl_pathv = NULL;
176 if (!(flags & GLOB_DOOFFS))
177 pglob->gl_offs = 0;
178 }
179 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
180 pglob->gl_errfunc = errfunc;
181 pglob->gl_matchc = 0;
182
183 bufnext = patbuf;
b7a2a476 184 bufend = bufnext + MAXPATHLEN - 1;
84ceda19 185 if (flags & GLOB_NOESCAPE)
8d0cc79b 186 while (bufnext < bufend && (c = *patnext++) != EOS)
187 *bufnext++ = c;
84ceda19 188 else {
189 /* Protect the quoted characters. */
190 while (bufnext < bufend && (c = *patnext++) != EOS)
191 if (c == QUOTE) {
192 if ((c = *patnext++) == EOS) {
193 c = QUOTE;
194 --patnext;
195 }
196 *bufnext++ = c | M_PROTECT;
e400a640 197 } else
84ceda19 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++)
e400a640 251 ;
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++)
e400a640 260 ;
84ceda19 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 }
e400a640 268 } else if (*pe == LBRACE)
84ceda19 269 i++;
270 else if (*pe == RBRACE) {
271 if (i == 0)
272 break;
273 i--;
274 }
275
276 /* Non matching braces; just glob the pattern */
277 if (i != 0 || *pe == EOS) {
278 *rv = glob0(patbuf, pglob);
279 return 0;
280 }
281
8d0cc79b 282 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
84ceda19 283 switch (*pm) {
284 case LBRACKET:
285 /* Ignore everything between [] */
286 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
e400a640 287 ;
84ceda19 288 if (*pm == EOS) {
289 /*
290 * We could not find a matching RBRACKET.
291 * Ignore and just look for RBRACE
292 */
293 pm = pl;
294 }
295 break;
296
297 case LBRACE:
298 i++;
299 break;
300
301 case RBRACE:
302 if (i) {
8d0cc79b 303 i--;
304 break;
84ceda19 305 }
306 /* FALLTHROUGH */
307 case COMMA:
308 if (i && *pm == COMMA)
309 break;
310 else {
311 /* Append the current string */
312 for (lm = ls; (pl < pm); *lm++ = *pl++)
e400a640 313 ;
314
84ceda19 315 /*
316 * Append the rest of the pattern after the
317 * closing brace
318 */
e400a640 319 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
320 ;
84ceda19 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++)
e400a640 365 ;
84ceda19 366
367 *h = EOS;
368
e400a640 369#if 0
370 if (h == (char *)eb)
371 return what;
372#endif
373
84ceda19 374 if (((char *) patbuf)[0] == EOS) {
375 /*
376 * handle a plain ~ or ~/ by expanding $HOME
377 * first and then trying the password file
378 */
379#if 0
380 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
381#endif
382 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
383 if ((pwd = getpwuid(getuid())) == NULL)
384 return pattern;
385 else
386 h = pwd->pw_dir;
387 }
e400a640 388 } else {
84ceda19 389 /*
390 * Expand a ~user
391 */
392 if ((pwd = getpwnam((char*) patbuf)) == NULL)
393 return pattern;
394 else
395 h = pwd->pw_dir;
396 }
397
398 /* Copy the home directory */
399 for (b = patbuf; b < eb && *h; *b++ = *h++)
e400a640 400 ;
84ceda19 401
402 /* Append the rest of the pattern */
403 while (b < eb && (*b++ = *p++) != EOS)
e400a640 404 ;
84ceda19 405 *b = EOS;
406
407 return patbuf;
408}
409
410
411/*
412 * The main glob() routine: compiles the pattern (optionally processing
413 * quotes), calls glob1() to do the real pattern matching, and finally
414 * sorts the list (unless unsorted operation is requested). Returns 0
415 * if things went well, nonzero if errors occurred. It is not an error
416 * to find no matches.
417 */
418static int
419glob0(pattern, pglob)
420 const Char *pattern;
421 glob_t *pglob;
422{
423 const Char *qpatnext;
424 int c, err, oldpathc;
b7a2a476 425 Char *bufnext, patbuf[MAXPATHLEN];
8d539493 426 size_t limit = 0;
84ceda19 427
b7a2a476 428 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
84ceda19 429 oldpathc = pglob->gl_pathc;
430 bufnext = patbuf;
431
432 /* We don't need to check for buffer overflow any more. */
433 while ((c = *qpatnext++) != EOS) {
434 switch (c) {
435 case LBRACKET:
436 c = *qpatnext;
437 if (c == NOT)
438 ++qpatnext;
439 if (*qpatnext == EOS ||
440 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
441 *bufnext++ = LBRACKET;
442 if (c == NOT)
443 --qpatnext;
444 break;
445 }
446 *bufnext++ = M_SET;
447 if (c == NOT)
448 *bufnext++ = M_NOT;
449 c = *qpatnext++;
450 do {
451 *bufnext++ = CHAR(c);
452 if (*qpatnext == RANGE &&
453 (c = qpatnext[1]) != RBRACKET) {
454 *bufnext++ = M_RNG;
455 *bufnext++ = CHAR(c);
456 qpatnext += 2;
457 }
458 } while ((c = *qpatnext++) != RBRACKET);
459 pglob->gl_flags |= GLOB_MAGCHAR;
460 *bufnext++ = M_END;
461 break;
462 case QUESTION:
463 pglob->gl_flags |= GLOB_MAGCHAR;
464 *bufnext++ = M_ONE;
465 break;
466 case STAR:
467 pglob->gl_flags |= GLOB_MAGCHAR;
468 /* collapse adjacent stars to one,
469 * to avoid exponential behavior
470 */
471 if (bufnext == patbuf || bufnext[-1] != M_ALL)
8d0cc79b 472 *bufnext++ = M_ALL;
84ceda19 473 break;
474 default:
475 *bufnext++ = CHAR(c);
476 break;
477 }
478 }
479 *bufnext = EOS;
480#ifdef DEBUG
481 qprintf("glob0:", patbuf);
482#endif
483
b7a2a476 484 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
84ceda19 485 return(err);
486
487 /*
488 * If there was no match we are going to append the pattern
489 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
490 * and the pattern did not contain any magic characters
491 * GLOB_NOMAGIC is there just for compatibility with csh.
492 */
493 if (pglob->gl_pathc == oldpathc) {
494 if ((pglob->gl_flags & GLOB_NOCHECK) ||
495 ((pglob->gl_flags & GLOB_NOMAGIC) &&
496 !(pglob->gl_flags & GLOB_MAGCHAR)))
8d539493 497 return(globextend(pattern, pglob, &limit));
84ceda19 498 else
499 return(GLOB_NOMATCH);
500 }
501 if (!(pglob->gl_flags & GLOB_NOSORT))
502 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
503 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
504 return(0);
505}
506
507static int
508compare(p, q)
509 const void *p, *q;
510{
511 return(strcmp(*(char **)p, *(char **)q));
512}
513
514static int
b7a2a476 515glob1(pattern, pattern_last, pglob, limitp)
516 Char *pattern, *pattern_last;
84ceda19 517 glob_t *pglob;
8d539493 518 size_t *limitp;
84ceda19 519{
b7a2a476 520 Char pathbuf[MAXPATHLEN];
84ceda19 521
522 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
523 if (*pattern == EOS)
524 return(0);
b7a2a476 525 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
526 pathbuf, pathbuf+MAXPATHLEN-1,
527 pattern, pattern_last, pglob, limitp));
84ceda19 528}
529
530/*
531 * The functions glob2 and glob3 are mutually recursive; there is one level
532 * of recursion for each segment in the pattern that contains one or more
533 * meta characters.
534 */
535static int
b7a2a476 536glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
537 pattern_last, pglob, limitp)
538 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
539 Char *pattern, *pattern_last;
84ceda19 540 glob_t *pglob;
8d539493 541 size_t *limitp;
84ceda19 542{
543 struct stat sb;
544 Char *p, *q;
545 int anymeta;
546
547 /*
548 * Loop over pattern segments until end of pattern or until
549 * segment with meta character found.
550 */
551 for (anymeta = 0;;) {
552 if (*pattern == EOS) { /* End of pattern? */
553 *pathend = EOS;
554 if (g_lstat(pathbuf, &sb, pglob))
555 return(0);
556
557 if (((pglob->gl_flags & GLOB_MARK) &&
8d0cc79b 558 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
559 (S_ISLNK(sb.st_mode) &&
84ceda19 560 (g_stat(pathbuf, &sb, pglob) == 0) &&
561 S_ISDIR(sb.st_mode)))) {
b7a2a476 562 if (pathend+1 > pathend_last)
563 return (1);
84ceda19 564 *pathend++ = SEP;
565 *pathend = EOS;
566 }
567 ++pglob->gl_matchc;
8d539493 568 return(globextend(pathbuf, pglob, limitp));
84ceda19 569 }
570
571 /* Find end of next segment, copy tentatively to pathend. */
572 q = pathend;
573 p = pattern;
574 while (*p != EOS && *p != SEP) {
575 if (ismeta(*p))
576 anymeta = 1;
b7a2a476 577 if (q+1 > pathend_last)
578 return (1);
84ceda19 579 *q++ = *p++;
580 }
581
582 if (!anymeta) { /* No expansion, do next segment. */
583 pathend = q;
584 pattern = p;
b7a2a476 585 while (*pattern == SEP) {
586 if (pathend+1 > pathend_last)
587 return (1);
84ceda19 588 *pathend++ = *pattern++;
b7a2a476 589 }
8d0cc79b 590 } else
591 /* Need expansion, recurse. */
b7a2a476 592 return(glob3(pathbuf, pathbuf_last, pathend,
593 pathend_last, pattern, pattern_last,
594 p, pattern_last, pglob, limitp));
84ceda19 595 }
596 /* NOTREACHED */
597}
598
599static int
b7a2a476 600glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
601 restpattern, restpattern_last, pglob, limitp)
602 Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
603 Char *pattern, *pattern_last, *restpattern, *restpattern_last;
84ceda19 604 glob_t *pglob;
8d539493 605 size_t *limitp;
84ceda19 606{
607 register struct dirent *dp;
608 DIR *dirp;
609 int err;
610 char buf[MAXPATHLEN];
611
612 /*
613 * The readdirfunc declaration can't be prototyped, because it is
614 * assigned, below, to two functions which are prototyped in glob.h
615 * and dirent.h as taking pointers to differently typed opaque
616 * structures.
617 */
618 struct dirent *(*readdirfunc)();
619
b7a2a476 620 if (pathend > pathend_last)
621 return (1);
84ceda19 622 *pathend = EOS;
623 errno = 0;
624
625 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
626 /* TODO: don't call for ENOENT or ENOTDIR? */
627 if (pglob->gl_errfunc) {
e400a640 628 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
8d0cc79b 629 return(GLOB_ABORTED);
84ceda19 630 if (pglob->gl_errfunc(buf, errno) ||
631 pglob->gl_flags & GLOB_ERR)
8d0cc79b 632 return(GLOB_ABORTED);
84ceda19 633 }
634 return(0);
635 }
636
637 err = 0;
638
639 /* Search directory for matching names. */
640 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
641 readdirfunc = pglob->gl_readdir;
642 else
643 readdirfunc = readdir;
644 while ((dp = (*readdirfunc)(dirp))) {
645 register u_char *sc;
646 register Char *dc;
647
648 /* Initial DOT must be matched literally. */
649 if (dp->d_name[0] == DOT && *pattern != DOT)
650 continue;
b7a2a476 651 dc = pathend;
652 sc = (u_char *) dp->d_name;
653 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
654 ;
655 if (dc >= pathend_last) {
656 *dc = EOS;
657 err = 1;
658 break;
659 }
660
84ceda19 661 if (!match(pathend, pattern, restpattern)) {
662 *pathend = EOS;
663 continue;
664 }
b7a2a476 665 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
666 restpattern, restpattern_last, pglob, limitp);
84ceda19 667 if (err)
668 break;
669 }
670
671 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
672 (*pglob->gl_closedir)(dirp);
673 else
674 closedir(dirp);
675 return(err);
676}
677
678
679/*
680 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
681 * add the new item, and update gl_pathc.
682 *
683 * This assumes the BSD realloc, which only copies the block when its size
684 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
685 * behavior.
686 *
687 * Return 0 if new item added, error code if memory couldn't be allocated.
688 *
689 * Invariant of the glob_t structure:
690 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
691 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
692 */
693static int
8d539493 694globextend(path, pglob, limitp)
84ceda19 695 const Char *path;
696 glob_t *pglob;
8d539493 697 size_t *limitp;
84ceda19 698{
699 register char **pathv;
700 register int i;
8d539493 701 u_int newsize, len;
84ceda19 702 char *copy;
703 const Char *p;
704
705 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
8d539493 706 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
707 malloc(newsize);
84ceda19 708 if (pathv == NULL) {
01022caf 709 if (pglob->gl_pathv) {
84ceda19 710 free(pglob->gl_pathv);
01022caf 711 pglob->gl_pathv = NULL;
712 }
84ceda19 713 return(GLOB_NOSPACE);
714 }
715
716 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
717 /* first time around -- clear initial gl_offs items */
718 pathv += pglob->gl_offs;
719 for (i = pglob->gl_offs; --i >= 0; )
720 *--pathv = NULL;
721 }
722 pglob->gl_pathv = pathv;
723
724 for (p = path; *p++;)
e400a640 725 ;
8d539493 726 len = (size_t)(p - path);
727 *limitp += len;
728 if ((copy = malloc(len)) != NULL) {
e400a640 729 if (g_Ctoc(path, copy, len)) {
8d0cc79b 730 free(copy);
731 return(GLOB_NOSPACE);
732 }
84ceda19 733 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
734 }
735 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
8d539493 736
737 if ((pglob->gl_flags & GLOB_LIMIT) &&
9887f269 738 newsize + *limitp >= (u_int) get_arg_max()) {
8d539493 739 errno = 0;
740 return(GLOB_NOSPACE);
741 }
742
84ceda19 743 return(copy == NULL ? GLOB_NOSPACE : 0);
744}
745
746
747/*
748 * pattern matching function for filenames. Each occurrence of the *
749 * pattern causes a recursion level.
750 */
751static int
752match(name, pat, patend)
753 register Char *name, *pat, *patend;
754{
755 int ok, negate_range;
756 Char c, k;
757
758 while (pat < patend) {
759 c = *pat++;
760 switch (c & M_MASK) {
761 case M_ALL:
762 if (pat == patend)
763 return(1);
764 do
765 if (match(name, pat, patend))
766 return(1);
8d0cc79b 767 while (*name++ != EOS)
768 ;
84ceda19 769 return(0);
770 case M_ONE:
771 if (*name++ == EOS)
772 return(0);
773 break;
774 case M_SET:
775 ok = 0;
776 if ((k = *name++) == EOS)
777 return(0);
778 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
779 ++pat;
780 while (((c = *pat++) & M_MASK) != M_END)
781 if ((*pat & M_MASK) == M_RNG) {
782 if (c <= k && k <= pat[1])
783 ok = 1;
784 pat += 2;
785 } else if (c == k)
786 ok = 1;
787 if (ok == negate_range)
788 return(0);
789 break;
790 default:
791 if (*name++ != c)
792 return(0);
793 break;
794 }
795 }
796 return(*name == EOS);
797}
798
799/* Free allocated data belonging to a glob_t structure. */
800void
801globfree(pglob)
802 glob_t *pglob;
803{
804 register int i;
805 register char **pp;
806
807 if (pglob->gl_pathv != NULL) {
808 pp = pglob->gl_pathv + pglob->gl_offs;
809 for (i = pglob->gl_pathc; i--; ++pp)
810 if (*pp)
811 free(*pp);
812 free(pglob->gl_pathv);
01022caf 813 pglob->gl_pathv = NULL;
84ceda19 814 }
815}
816
817static DIR *
818g_opendir(str, pglob)
819 register Char *str;
820 glob_t *pglob;
821{
822 char buf[MAXPATHLEN];
823
824 if (!*str)
825 strcpy(buf, ".");
8d0cc79b 826 else {
e400a640 827 if (g_Ctoc(str, buf, sizeof(buf)))
8d0cc79b 828 return(NULL);
829 }
84ceda19 830
831 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
832 return((*pglob->gl_opendir)(buf));
833
834 return(opendir(buf));
835}
836
837static int
838g_lstat(fn, sb, pglob)
839 register Char *fn;
840 struct stat *sb;
841 glob_t *pglob;
842{
843 char buf[MAXPATHLEN];
844
e400a640 845 if (g_Ctoc(fn, buf, sizeof(buf)))
8d0cc79b 846 return(-1);
84ceda19 847 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
848 return((*pglob->gl_lstat)(buf, sb));
849 return(lstat(buf, sb));
850}
851
852static int
853g_stat(fn, sb, pglob)
854 register Char *fn;
855 struct stat *sb;
856 glob_t *pglob;
857{
858 char buf[MAXPATHLEN];
859
e400a640 860 if (g_Ctoc(fn, buf, sizeof(buf)))
8d0cc79b 861 return(-1);
84ceda19 862 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
863 return((*pglob->gl_stat)(buf, sb));
864 return(stat(buf, sb));
865}
866
867static Char *
868g_strchr(str, ch)
869 Char *str;
870 int ch;
871{
872 do {
873 if (*str == ch)
874 return (str);
875 } while (*str++);
876 return (NULL);
877}
878
8d0cc79b 879static int
e400a640 880g_Ctoc(str, buf, len)
84ceda19 881 register const Char *str;
e400a640 882 char *buf;
883 u_int len;
84ceda19 884{
84ceda19 885
e400a640 886 while (len--) {
887 if ((*buf++ = *str++) == EOS)
888 return (0);
889 }
890 return (1);
84ceda19 891}
892
893#ifdef DEBUG
894static void
895qprintf(str, s)
896 const char *str;
897 register Char *s;
898{
899 register Char *p;
900
901 (void)printf("%s:\n", str);
902 for (p = s; *p; p++)
903 (void)printf("%c", CHAR(*p));
904 (void)printf("\n");
905 for (p = s; *p; p++)
906 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
907 (void)printf("\n");
908 for (p = s; *p; p++)
909 (void)printf("%c", ismeta(*p) ? '_' : ' ');
910 (void)printf("\n");
911}
912#endif
913
40849fdb 914#endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
915 !defined(GLOB_HAS_GL_MATCHC) */
84ceda19 916
This page took 3.580845 seconds and 5 git commands to generate.