diff options
Diffstat (limited to 'openbsd-compat/glob.c')
-rw-r--r-- | openbsd-compat/glob.c | 85 |
1 files changed, 56 insertions, 29 deletions
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c index cca819de0..677a8a10c 100644 --- a/openbsd-compat/glob.c +++ b/openbsd-compat/glob.c | |||
@@ -56,7 +56,7 @@ get_arg_max() | |||
56 | #if 0 | 56 | #if 0 |
57 | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; | 57 | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; |
58 | #else | 58 | #else |
59 | static char rcsid[] = "$OpenBSD: glob.c,v 1.13 2001/03/28 08:00:00 deraadt Exp $"; | 59 | static char rcsid[] = "$OpenBSD: glob.c,v 1.14 2001/03/28 20:54:19 millert Exp $"; |
60 | #endif | 60 | #endif |
61 | #endif /* LIBC_SCCS and not lint */ | 61 | #endif /* LIBC_SCCS and not lint */ |
62 | 62 | ||
@@ -143,10 +143,11 @@ static DIR *g_opendir __P((Char *, glob_t *)); | |||
143 | static Char *g_strchr __P((Char *, int)); | 143 | static Char *g_strchr __P((Char *, int)); |
144 | static int g_stat __P((Char *, struct stat *, glob_t *)); | 144 | static int g_stat __P((Char *, struct stat *, glob_t *)); |
145 | static int glob0 __P((const Char *, glob_t *)); | 145 | static int glob0 __P((const Char *, glob_t *)); |
146 | static int glob1 __P((Char *, glob_t *, size_t *)); | 146 | static int glob1 __P((Char *, Char *, glob_t *, size_t *)); |
147 | static int glob2 __P((Char *, Char *, Char *, glob_t *, size_t *)); | 147 | static int glob2 __P((Char *, Char *, Char *, Char *, Char *, Char *, |
148 | static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *, | 148 | glob_t *, size_t *)); |
149 | size_t *)); | 149 | static int glob3 __P((Char *, Char *, Char *, Char *, Char *, Char *, |
150 | Char *, Char *, glob_t *, size_t *)); | ||
150 | static int globextend __P((const Char *, glob_t *, size_t *)); | 151 | static int globextend __P((const Char *, glob_t *, size_t *)); |
151 | static const Char * | 152 | static const Char * |
152 | globtilde __P((const Char *, Char *, size_t, glob_t *)); | 153 | globtilde __P((const Char *, Char *, size_t, glob_t *)); |
@@ -165,7 +166,7 @@ glob(pattern, flags, errfunc, pglob) | |||
165 | { | 166 | { |
166 | const u_char *patnext; | 167 | const u_char *patnext; |
167 | int c; | 168 | int c; |
168 | Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; | 169 | Char *bufnext, *bufend, patbuf[MAXPATHLEN]; |
169 | 170 | ||
170 | patnext = (u_char *) pattern; | 171 | patnext = (u_char *) pattern; |
171 | if (!(flags & GLOB_APPEND)) { | 172 | if (!(flags & GLOB_APPEND)) { |
@@ -179,7 +180,7 @@ glob(pattern, flags, errfunc, pglob) | |||
179 | pglob->gl_matchc = 0; | 180 | pglob->gl_matchc = 0; |
180 | 181 | ||
181 | bufnext = patbuf; | 182 | bufnext = patbuf; |
182 | bufend = bufnext + MAXPATHLEN; | 183 | bufend = bufnext + MAXPATHLEN - 1; |
183 | if (flags & GLOB_NOESCAPE) | 184 | if (flags & GLOB_NOESCAPE) |
184 | while (bufnext < bufend && (c = *patnext++) != EOS) | 185 | while (bufnext < bufend && (c = *patnext++) != EOS) |
185 | *bufnext++ = c; | 186 | *bufnext++ = c; |
@@ -209,7 +210,8 @@ glob(pattern, flags, errfunc, pglob) | |||
209 | * invoke the standard globbing routine to glob the rest of the magic | 210 | * invoke the standard globbing routine to glob the rest of the magic |
210 | * characters | 211 | * characters |
211 | */ | 212 | */ |
212 | static int globexp1(pattern, pglob) | 213 | static int |
214 | globexp1(pattern, pglob) | ||
213 | const Char *pattern; | 215 | const Char *pattern; |
214 | glob_t *pglob; | 216 | glob_t *pglob; |
215 | { | 217 | { |
@@ -233,7 +235,8 @@ static int globexp1(pattern, pglob) | |||
233 | * If it succeeds then it invokes globexp1 with the new pattern. | 235 | * 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. | 236 | * If it fails then it tries to glob the rest of the pattern and returns. |
235 | */ | 237 | */ |
236 | static int globexp2(ptr, pattern, pglob, rv) | 238 | static int |
239 | globexp2(ptr, pattern, pglob, rv) | ||
237 | const Char *ptr, *pattern; | 240 | const Char *ptr, *pattern; |
238 | glob_t *pglob; | 241 | glob_t *pglob; |
239 | int *rv; | 242 | int *rv; |
@@ -241,11 +244,12 @@ static int globexp2(ptr, pattern, pglob, rv) | |||
241 | int i; | 244 | int i; |
242 | Char *lm, *ls; | 245 | Char *lm, *ls; |
243 | const Char *pe, *pm, *pl; | 246 | const Char *pe, *pm, *pl; |
244 | Char patbuf[MAXPATHLEN + 1]; | 247 | Char patbuf[MAXPATHLEN]; |
245 | 248 | ||
246 | /* copy part up to the brace */ | 249 | /* copy part up to the brace */ |
247 | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) | 250 | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) |
248 | continue; | 251 | continue; |
252 | *lm = EOS; | ||
249 | ls = lm; | 253 | ls = lm; |
250 | 254 | ||
251 | /* Find the balanced brace */ | 255 | /* Find the balanced brace */ |
@@ -414,11 +418,10 @@ glob0(pattern, pglob) | |||
414 | { | 418 | { |
415 | const Char *qpatnext; | 419 | const Char *qpatnext; |
416 | int c, err, oldpathc; | 420 | int c, err, oldpathc; |
417 | Char *bufnext, patbuf[MAXPATHLEN+1]; | 421 | Char *bufnext, patbuf[MAXPATHLEN]; |
418 | size_t limit = 0; | 422 | size_t limit = 0; |
419 | 423 | ||
420 | qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), | 424 | qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); |
421 | pglob); | ||
422 | oldpathc = pglob->gl_pathc; | 425 | oldpathc = pglob->gl_pathc; |
423 | bufnext = patbuf; | 426 | bufnext = patbuf; |
424 | 427 | ||
@@ -474,7 +477,7 @@ glob0(pattern, pglob) | |||
474 | qprintf("glob0:", patbuf); | 477 | qprintf("glob0:", patbuf); |
475 | #endif | 478 | #endif |
476 | 479 | ||
477 | if ((err = glob1(patbuf, pglob, &limit)) != 0) | 480 | if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0) |
478 | return(err); | 481 | return(err); |
479 | 482 | ||
480 | /* | 483 | /* |
@@ -505,17 +508,19 @@ compare(p, q) | |||
505 | } | 508 | } |
506 | 509 | ||
507 | static int | 510 | static int |
508 | glob1(pattern, pglob, limitp) | 511 | glob1(pattern, pattern_last, pglob, limitp) |
509 | Char *pattern; | 512 | Char *pattern, *pattern_last; |
510 | glob_t *pglob; | 513 | glob_t *pglob; |
511 | size_t *limitp; | 514 | size_t *limitp; |
512 | { | 515 | { |
513 | Char pathbuf[MAXPATHLEN+1]; | 516 | Char pathbuf[MAXPATHLEN]; |
514 | 517 | ||
515 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ | 518 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ |
516 | if (*pattern == EOS) | 519 | if (*pattern == EOS) |
517 | return(0); | 520 | return(0); |
518 | return(glob2(pathbuf, pathbuf, pattern, pglob, limitp)); | 521 | return(glob2(pathbuf, pathbuf+MAXPATHLEN-1, |
522 | pathbuf, pathbuf+MAXPATHLEN-1, | ||
523 | pattern, pattern_last, pglob, limitp)); | ||
519 | } | 524 | } |
520 | 525 | ||
521 | /* | 526 | /* |
@@ -524,8 +529,10 @@ glob1(pattern, pglob, limitp) | |||
524 | * meta characters. | 529 | * meta characters. |
525 | */ | 530 | */ |
526 | static int | 531 | static int |
527 | glob2(pathbuf, pathend, pattern, pglob, limitp) | 532 | glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern, |
528 | Char *pathbuf, *pathend, *pattern; | 533 | pattern_last, pglob, limitp) |
534 | Char *pathbuf, *pathbuf_last, *pathend, *pathend_last; | ||
535 | Char *pattern, *pattern_last; | ||
529 | glob_t *pglob; | 536 | glob_t *pglob; |
530 | size_t *limitp; | 537 | size_t *limitp; |
531 | { | 538 | { |
@@ -548,6 +555,8 @@ glob2(pathbuf, pathend, pattern, pglob, limitp) | |||
548 | (S_ISLNK(sb.st_mode) && | 555 | (S_ISLNK(sb.st_mode) && |
549 | (g_stat(pathbuf, &sb, pglob) == 0) && | 556 | (g_stat(pathbuf, &sb, pglob) == 0) && |
550 | S_ISDIR(sb.st_mode)))) { | 557 | S_ISDIR(sb.st_mode)))) { |
558 | if (pathend+1 > pathend_last) | ||
559 | return (1); | ||
551 | *pathend++ = SEP; | 560 | *pathend++ = SEP; |
552 | *pathend = EOS; | 561 | *pathend = EOS; |
553 | } | 562 | } |
@@ -561,25 +570,33 @@ glob2(pathbuf, pathend, pattern, pglob, limitp) | |||
561 | while (*p != EOS && *p != SEP) { | 570 | while (*p != EOS && *p != SEP) { |
562 | if (ismeta(*p)) | 571 | if (ismeta(*p)) |
563 | anymeta = 1; | 572 | anymeta = 1; |
573 | if (q+1 > pathend_last) | ||
574 | return (1); | ||
564 | *q++ = *p++; | 575 | *q++ = *p++; |
565 | } | 576 | } |
566 | 577 | ||
567 | if (!anymeta) { /* No expansion, do next segment. */ | 578 | if (!anymeta) { /* No expansion, do next segment. */ |
568 | pathend = q; | 579 | pathend = q; |
569 | pattern = p; | 580 | pattern = p; |
570 | while (*pattern == SEP) | 581 | while (*pattern == SEP) { |
582 | if (pathend+1 > pathend_last) | ||
583 | return (1); | ||
571 | *pathend++ = *pattern++; | 584 | *pathend++ = *pattern++; |
585 | } | ||
572 | } else | 586 | } else |
573 | /* Need expansion, recurse. */ | 587 | /* Need expansion, recurse. */ |
574 | return(glob3(pathbuf, pathend, pattern, p, pglob, | 588 | return(glob3(pathbuf, pathbuf_last, pathend, |
575 | limitp)); | 589 | pathend_last, pattern, pattern_last, |
590 | p, pattern_last, pglob, limitp)); | ||
576 | } | 591 | } |
577 | /* NOTREACHED */ | 592 | /* NOTREACHED */ |
578 | } | 593 | } |
579 | 594 | ||
580 | static int | 595 | static int |
581 | glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp) | 596 | glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last, |
582 | Char *pathbuf, *pathend, *pattern, *restpattern; | 597 | restpattern, restpattern_last, pglob, limitp) |
598 | Char *pathbuf, *pathbuf_last, *pathend, *pathend_last; | ||
599 | Char *pattern, *pattern_last, *restpattern, *restpattern_last; | ||
583 | glob_t *pglob; | 600 | glob_t *pglob; |
584 | size_t *limitp; | 601 | size_t *limitp; |
585 | { | 602 | { |
@@ -596,6 +613,8 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp) | |||
596 | */ | 613 | */ |
597 | struct dirent *(*readdirfunc)(); | 614 | struct dirent *(*readdirfunc)(); |
598 | 615 | ||
616 | if (pathend > pathend_last) | ||
617 | return (1); | ||
599 | *pathend = EOS; | 618 | *pathend = EOS; |
600 | errno = 0; | 619 | errno = 0; |
601 | 620 | ||
@@ -625,14 +644,22 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp) | |||
625 | /* Initial DOT must be matched literally. */ | 644 | /* Initial DOT must be matched literally. */ |
626 | if (dp->d_name[0] == DOT && *pattern != DOT) | 645 | if (dp->d_name[0] == DOT && *pattern != DOT) |
627 | continue; | 646 | continue; |
628 | for (sc = (u_char *) dp->d_name, dc = pathend; | 647 | dc = pathend; |
629 | (*dc++ = *sc++) != EOS;) | 648 | sc = (u_char *) dp->d_name; |
630 | continue; | 649 | while (dc < pathend_last && (*dc++ = *sc++) != EOS) |
650 | ; | ||
651 | if (dc >= pathend_last) { | ||
652 | *dc = EOS; | ||
653 | err = 1; | ||
654 | break; | ||
655 | } | ||
656 | |||
631 | if (!match(pathend, pattern, restpattern)) { | 657 | if (!match(pathend, pattern, restpattern)) { |
632 | *pathend = EOS; | 658 | *pathend = EOS; |
633 | continue; | 659 | continue; |
634 | } | 660 | } |
635 | err = glob2(pathbuf, --dc, restpattern, pglob, limitp); | 661 | err = glob2(pathbuf, pathbuf_last, --dc, pathend_last, |
662 | restpattern, restpattern_last, pglob, limitp); | ||
636 | if (err) | 663 | if (err) |
637 | break; | 664 | break; |
638 | } | 665 | } |