summaryrefslogtreecommitdiff
path: root/openbsd-compat/glob.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/glob.c')
-rw-r--r--openbsd-compat/glob.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c
index e2fd7c27f..b42cedb79 100644
--- a/openbsd-compat/glob.c
+++ b/openbsd-compat/glob.c
@@ -44,7 +44,7 @@
44#if 0 44#if 0
45static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; 45static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
46#else 46#else
47static char rcsid[] = "$OpenBSD: glob.c,v 1.8 1998/08/14 21:39:30 deraadt Exp $"; 47static char rcsid[] = "$OpenBSD: glob.c,v 1.9 2001/03/18 17:18:58 deraadt Exp $";
48#endif 48#endif
49#endif /* LIBC_SCCS and not lint */ 49#endif /* LIBC_SCCS and not lint */
50 50
@@ -134,11 +134,13 @@ static Char *g_strcat __P((Char *, const Char *));
134#endif 134#endif
135static int g_stat __P((Char *, struct stat *, glob_t *)); 135static int g_stat __P((Char *, struct stat *, glob_t *));
136static int glob0 __P((const Char *, glob_t *)); 136static int glob0 __P((const Char *, glob_t *));
137static int glob1 __P((Char *, glob_t *)); 137static int glob1 __P((Char *, glob_t *, size_t *));
138static int glob2 __P((Char *, Char *, Char *, glob_t *)); 138static int glob2 __P((Char *, Char *, Char *, glob_t *, size_t *));
139static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); 139static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *,
140static int globextend __P((const Char *, glob_t *)); 140 size_t *));
141static const Char * globtilde __P((const Char *, Char *, size_t, glob_t *)); 141static int globextend __P((const Char *, glob_t *, size_t *));
142static const Char *
143 globtilde __P((const Char *, Char *, size_t, glob_t *));
142static int globexp1 __P((const Char *, glob_t *)); 144static int globexp1 __P((const Char *, glob_t *));
143static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); 145static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
144static int match __P((Char *, Char *, Char *)); 146static int match __P((Char *, Char *, Char *));
@@ -403,6 +405,7 @@ glob0(pattern, pglob)
403 const Char *qpatnext; 405 const Char *qpatnext;
404 int c, err, oldpathc; 406 int c, err, oldpathc;
405 Char *bufnext, patbuf[MAXPATHLEN+1]; 407 Char *bufnext, patbuf[MAXPATHLEN+1];
408 size_t limit = 0;
406 409
407 qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), 410 qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char),
408 pglob); 411 pglob);
@@ -461,7 +464,7 @@ glob0(pattern, pglob)
461 qprintf("glob0:", patbuf); 464 qprintf("glob0:", patbuf);
462#endif 465#endif
463 466
464 if ((err = glob1(patbuf, pglob)) != 0) 467 if ((err = glob1(patbuf, pglob, &limit)) != 0)
465 return(err); 468 return(err);
466 469
467 /* 470 /*
@@ -474,7 +477,7 @@ glob0(pattern, pglob)
474 if ((pglob->gl_flags & GLOB_NOCHECK) || 477 if ((pglob->gl_flags & GLOB_NOCHECK) ||
475 ((pglob->gl_flags & GLOB_NOMAGIC) && 478 ((pglob->gl_flags & GLOB_NOMAGIC) &&
476 !(pglob->gl_flags & GLOB_MAGCHAR))) 479 !(pglob->gl_flags & GLOB_MAGCHAR)))
477 return(globextend(pattern, pglob)); 480 return(globextend(pattern, pglob, &limit));
478 else 481 else
479 return(GLOB_NOMATCH); 482 return(GLOB_NOMATCH);
480 } 483 }
@@ -492,16 +495,17 @@ compare(p, q)
492} 495}
493 496
494static int 497static int
495glob1(pattern, pglob) 498glob1(pattern, pglob, limitp)
496 Char *pattern; 499 Char *pattern;
497 glob_t *pglob; 500 glob_t *pglob;
501 size_t *limitp;
498{ 502{
499 Char pathbuf[MAXPATHLEN+1]; 503 Char pathbuf[MAXPATHLEN+1];
500 504
501 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 505 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
502 if (*pattern == EOS) 506 if (*pattern == EOS)
503 return(0); 507 return(0);
504 return(glob2(pathbuf, pathbuf, pattern, pglob)); 508 return(glob2(pathbuf, pathbuf, pattern, pglob, limitp));
505} 509}
506 510
507/* 511/*
@@ -510,9 +514,10 @@ glob1(pattern, pglob)
510 * meta characters. 514 * meta characters.
511 */ 515 */
512static int 516static int
513glob2(pathbuf, pathend, pattern, pglob) 517glob2(pathbuf, pathend, pattern, pglob, limitp)
514 Char *pathbuf, *pathend, *pattern; 518 Char *pathbuf, *pathend, *pattern;
515 glob_t *pglob; 519 glob_t *pglob;
520 size_t *limitp;
516{ 521{
517 struct stat sb; 522 struct stat sb;
518 Char *p, *q; 523 Char *p, *q;
@@ -537,7 +542,7 @@ glob2(pathbuf, pathend, pattern, pglob)
537 *pathend = EOS; 542 *pathend = EOS;
538 } 543 }
539 ++pglob->gl_matchc; 544 ++pglob->gl_matchc;
540 return(globextend(pathbuf, pglob)); 545 return(globextend(pathbuf, pglob, limitp));
541 } 546 }
542 547
543 /* Find end of next segment, copy tentatively to pathend. */ 548 /* Find end of next segment, copy tentatively to pathend. */
@@ -555,15 +560,17 @@ glob2(pathbuf, pathend, pattern, pglob)
555 while (*pattern == SEP) 560 while (*pattern == SEP)
556 *pathend++ = *pattern++; 561 *pathend++ = *pattern++;
557 } else /* Need expansion, recurse. */ 562 } else /* Need expansion, recurse. */
558 return(glob3(pathbuf, pathend, pattern, p, pglob)); 563 return(glob3(pathbuf, pathend, pattern, p, pglob,
564 limitp));
559 } 565 }
560 /* NOTREACHED */ 566 /* NOTREACHED */
561} 567}
562 568
563static int 569static int
564glob3(pathbuf, pathend, pattern, restpattern, pglob) 570glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp)
565 Char *pathbuf, *pathend, *pattern, *restpattern; 571 Char *pathbuf, *pathend, *pattern, *restpattern;
566 glob_t *pglob; 572 glob_t *pglob;
573 size_t *limitp;
567{ 574{
568 register struct dirent *dp; 575 register struct dirent *dp;
569 DIR *dirp; 576 DIR *dirp;
@@ -613,7 +620,7 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob)
613 *pathend = EOS; 620 *pathend = EOS;
614 continue; 621 continue;
615 } 622 }
616 err = glob2(pathbuf, --dc, restpattern, pglob); 623 err = glob2(pathbuf, --dc, restpattern, pglob, limitp);
617 if (err) 624 if (err)
618 break; 625 break;
619 } 626 }
@@ -641,20 +648,20 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob)
641 * gl_pathv points to (gl_offs + gl_pathc + 1) items. 648 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
642 */ 649 */
643static int 650static int
644globextend(path, pglob) 651globextend(path, pglob, limitp)
645 const Char *path; 652 const Char *path;
646 glob_t *pglob; 653 glob_t *pglob;
654 size_t *limitp;
647{ 655{
648 register char **pathv; 656 register char **pathv;
649 register int i; 657 register int i;
650 u_int newsize; 658 u_int newsize, len;
651 char *copy; 659 char *copy;
652 const Char *p; 660 const Char *p;
653 661
654 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 662 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
655 pathv = pglob->gl_pathv ? 663 pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
656 realloc((char *)pglob->gl_pathv, newsize) : 664 malloc(newsize);
657 malloc(newsize);
658 if (pathv == NULL) { 665 if (pathv == NULL) {
659 if (pglob->gl_pathv) 666 if (pglob->gl_pathv)
660 free(pglob->gl_pathv); 667 free(pglob->gl_pathv);
@@ -671,11 +678,20 @@ globextend(path, pglob)
671 678
672 for (p = path; *p++;) 679 for (p = path; *p++;)
673 continue; 680 continue;
674 if ((copy = malloc(p - path)) != NULL) { 681 len = (size_t)(p - path);
682 *limitp += len;
683 if ((copy = malloc(len)) != NULL) {
675 g_Ctoc(path, copy); 684 g_Ctoc(path, copy);
676 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 685 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
677 } 686 }
678 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 687 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
688
689 if ((pglob->gl_flags & GLOB_LIMIT) &&
690 newsize + *limitp >= ARG_MAX) {
691 errno = 0;
692 return(GLOB_NOSPACE);
693 }
694
679 return(copy == NULL ? GLOB_NOSPACE : 0); 695 return(copy == NULL ? GLOB_NOSPACE : 0);
680} 696}
681 697
@@ -818,7 +834,7 @@ g_strcat(dst, src)
818 continue; 834 continue;
819 --dst; 835 --dst;
820 while((*dst++ = *src++) != EOS) 836 while((*dst++ = *src++) != EOS)
821 continue; 837 continue;
822 838
823 return (sdst); 839 return (sdst);
824} 840}