summaryrefslogtreecommitdiff
path: root/openbsd-compat/glob.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2001-03-28 21:05:26 +1000
committerDamien Miller <djm@mindrot.org>2001-03-28 21:05:26 +1000
commitb68af624a9bf64fcf810a16a35749f4047ebe1ef (patch)
tree0a3467611968e1af5f3ca3d148d78699bd816aab /openbsd-compat/glob.c
parent98344741512e0171567409c5c7208af5cebd4fe1 (diff)
- (djm) Sync openbsd-compat/glob.c
Diffstat (limited to 'openbsd-compat/glob.c')
-rw-r--r--openbsd-compat/glob.c81
1 files changed, 37 insertions, 44 deletions
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c
index 5ed286211..cca819de0 100644
--- a/openbsd-compat/glob.c
+++ b/openbsd-compat/glob.c
@@ -56,7 +56,7 @@ get_arg_max()
56#if 0 56#if 0
57static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; 57static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
58#else 58#else
59static char rcsid[] = "$OpenBSD: glob.c,v 1.10 2001/03/19 13:45:30 millert Exp $"; 59static char rcsid[] = "$OpenBSD: glob.c,v 1.13 2001/03/28 08:00:00 deraadt Exp $";
60#endif 60#endif
61#endif /* LIBC_SCCS and not lint */ 61#endif /* LIBC_SCCS and not lint */
62 62
@@ -137,13 +137,10 @@ typedef char Char;
137 137
138 138
139static int compare __P((const void *, const void *)); 139static int compare __P((const void *, const void *));
140static void g_Ctoc __P((const Char *, char *)); 140static int g_Ctoc __P((const Char *, char *, char *));
141static int g_lstat __P((Char *, struct stat *, glob_t *)); 141static int g_lstat __P((Char *, struct stat *, glob_t *));
142static DIR *g_opendir __P((Char *, glob_t *)); 142static DIR *g_opendir __P((Char *, glob_t *));
143static Char *g_strchr __P((Char *, int)); 143static Char *g_strchr __P((Char *, int));
144#ifdef notdef
145static Char *g_strcat __P((Char *, const Char *));
146#endif
147static int g_stat __P((Char *, struct stat *, glob_t *)); 144static int g_stat __P((Char *, struct stat *, glob_t *));
148static int glob0 __P((const Char *, glob_t *)); 145static int glob0 __P((const Char *, glob_t *));
149static int glob1 __P((Char *, glob_t *, size_t *)); 146static int glob1 __P((Char *, glob_t *, size_t *));
@@ -184,8 +181,8 @@ glob(pattern, flags, errfunc, pglob)
184 bufnext = patbuf; 181 bufnext = patbuf;
185 bufend = bufnext + MAXPATHLEN; 182 bufend = bufnext + MAXPATHLEN;
186 if (flags & GLOB_NOESCAPE) 183 if (flags & GLOB_NOESCAPE)
187 while (bufnext < bufend && (c = *patnext++) != EOS) 184 while (bufnext < bufend && (c = *patnext++) != EOS)
188 *bufnext++ = c; 185 *bufnext++ = c;
189 else { 186 else {
190 /* Protect the quoted characters. */ 187 /* Protect the quoted characters. */
191 while (bufnext < bufend && (c = *patnext++) != EOS) 188 while (bufnext < bufend && (c = *patnext++) != EOS)
@@ -279,7 +276,7 @@ static int globexp2(ptr, pattern, pglob, rv)
279 return 0; 276 return 0;
280 } 277 }
281 278
282 for (i = 0, pl = pm = ptr; pm <= pe; pm++) 279 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
283 switch (*pm) { 280 switch (*pm) {
284 case LBRACKET: 281 case LBRACKET:
285 /* Ignore everything between [] */ 282 /* Ignore everything between [] */
@@ -300,8 +297,8 @@ static int globexp2(ptr, pattern, pglob, rv)
300 297
301 case RBRACE: 298 case RBRACE:
302 if (i) { 299 if (i) {
303 i--; 300 i--;
304 break; 301 break;
305 } 302 }
306 /* FALLTHROUGH */ 303 /* FALLTHROUGH */
307 case COMMA: 304 case COMMA:
@@ -332,6 +329,7 @@ static int globexp2(ptr, pattern, pglob, rv)
332 default: 329 default:
333 break; 330 break;
334 } 331 }
332 }
335 *rv = 0; 333 *rv = 0;
336 return 0; 334 return 0;
337} 335}
@@ -464,7 +462,7 @@ glob0(pattern, pglob)
464 * to avoid exponential behavior 462 * to avoid exponential behavior
465 */ 463 */
466 if (bufnext == patbuf || bufnext[-1] != M_ALL) 464 if (bufnext == patbuf || bufnext[-1] != M_ALL)
467 *bufnext++ = M_ALL; 465 *bufnext++ = M_ALL;
468 break; 466 break;
469 default: 467 default:
470 *bufnext++ = CHAR(c); 468 *bufnext++ = CHAR(c);
@@ -546,8 +544,8 @@ glob2(pathbuf, pathend, pattern, pglob, limitp)
546 return(0); 544 return(0);
547 545
548 if (((pglob->gl_flags & GLOB_MARK) && 546 if (((pglob->gl_flags & GLOB_MARK) &&
549 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 547 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
550 || (S_ISLNK(sb.st_mode) && 548 (S_ISLNK(sb.st_mode) &&
551 (g_stat(pathbuf, &sb, pglob) == 0) && 549 (g_stat(pathbuf, &sb, pglob) == 0) &&
552 S_ISDIR(sb.st_mode)))) { 550 S_ISDIR(sb.st_mode)))) {
553 *pathend++ = SEP; 551 *pathend++ = SEP;
@@ -571,7 +569,8 @@ glob2(pathbuf, pathend, pattern, pglob, limitp)
571 pattern = p; 569 pattern = p;
572 while (*pattern == SEP) 570 while (*pattern == SEP)
573 *pathend++ = *pattern++; 571 *pathend++ = *pattern++;
574 } else /* Need expansion, recurse. */ 572 } else
573 /* Need expansion, recurse. */
575 return(glob3(pathbuf, pathend, pattern, p, pglob, 574 return(glob3(pathbuf, pathend, pattern, p, pglob,
576 limitp)); 575 limitp));
577 } 576 }
@@ -603,10 +602,11 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp)
603 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 602 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
604 /* TODO: don't call for ENOENT or ENOTDIR? */ 603 /* TODO: don't call for ENOENT or ENOTDIR? */
605 if (pglob->gl_errfunc) { 604 if (pglob->gl_errfunc) {
606 g_Ctoc(pathbuf, buf); 605 if (g_Ctoc(pathbuf, buf, buf+sizeof(buf)))
606 return(GLOB_ABORTED);
607 if (pglob->gl_errfunc(buf, errno) || 607 if (pglob->gl_errfunc(buf, errno) ||
608 pglob->gl_flags & GLOB_ERR) 608 pglob->gl_flags & GLOB_ERR)
609 return (GLOB_ABORTED); 609 return(GLOB_ABORTED);
610 } 610 }
611 return(0); 611 return(0);
612 } 612 }
@@ -695,7 +695,10 @@ globextend(path, pglob, limitp)
695 len = (size_t)(p - path); 695 len = (size_t)(p - path);
696 *limitp += len; 696 *limitp += len;
697 if ((copy = malloc(len)) != NULL) { 697 if ((copy = malloc(len)) != NULL) {
698 g_Ctoc(path, copy); 698 if (g_Ctoc(path, copy, copy+len+1)) {
699 free(copy);
700 return(GLOB_NOSPACE);
701 }
699 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 702 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
700 } 703 }
701 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 704 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
@@ -730,7 +733,8 @@ match(name, pat, patend)
730 do 733 do
731 if (match(name, pat, patend)) 734 if (match(name, pat, patend))
732 return(1); 735 return(1);
733 while (*name++ != EOS); 736 while (*name++ != EOS)
737 ;
734 return(0); 738 return(0);
735 case M_ONE: 739 case M_ONE:
736 if (*name++ == EOS) 740 if (*name++ == EOS)
@@ -788,8 +792,10 @@ g_opendir(str, pglob)
788 792
789 if (!*str) 793 if (!*str)
790 strcpy(buf, "."); 794 strcpy(buf, ".");
791 else 795 else {
792 g_Ctoc(str, buf); 796 if (g_Ctoc(str, buf, buf+sizeof(buf)))
797 return(NULL);
798 }
793 799
794 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 800 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
795 return((*pglob->gl_opendir)(buf)); 801 return((*pglob->gl_opendir)(buf));
@@ -805,7 +811,8 @@ g_lstat(fn, sb, pglob)
805{ 811{
806 char buf[MAXPATHLEN]; 812 char buf[MAXPATHLEN];
807 813
808 g_Ctoc(fn, buf); 814 if (g_Ctoc(fn, buf, buf+sizeof(buf)))
815 return(-1);
809 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 816 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
810 return((*pglob->gl_lstat)(buf, sb)); 817 return((*pglob->gl_lstat)(buf, sb));
811 return(lstat(buf, sb)); 818 return(lstat(buf, sb));
@@ -819,7 +826,8 @@ g_stat(fn, sb, pglob)
819{ 826{
820 char buf[MAXPATHLEN]; 827 char buf[MAXPATHLEN];
821 828
822 g_Ctoc(fn, buf); 829 if (g_Ctoc(fn, buf, buf+sizeof(buf)))
830 return(-1);
823 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 831 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
824 return((*pglob->gl_stat)(buf, sb)); 832 return((*pglob->gl_stat)(buf, sb));
825 return(stat(buf, sb)); 833 return(stat(buf, sb));
@@ -837,33 +845,18 @@ g_strchr(str, ch)
837 return (NULL); 845 return (NULL);
838} 846}
839 847
840#ifdef notdef 848static int
841static Char * 849g_Ctoc(str, buf, ebuf)
842g_strcat(dst, src)
843 Char *dst;
844 const Char* src;
845{
846 Char *sdst = dst;
847
848 while (*dst++)
849 continue;
850 --dst;
851 while((*dst++ = *src++) != EOS)
852 continue;
853
854 return (sdst);
855}
856#endif
857
858static void
859g_Ctoc(str, buf)
860 register const Char *str; 850 register const Char *str;
861 char *buf; 851 char *buf, *ebuf;
862{ 852{
863 register char *dc; 853 register char *dc;
864 854
865 for (dc = buf; (*dc++ = *str++) != EOS;) 855 for (dc = buf; dc < ebuf && (*dc++ = *str++) != EOS;)
866 continue; 856 continue;
857 if (dc >= ebuf)
858 return (1);
859 return (0);
867} 860}
868 861
869#ifdef DEBUG 862#ifdef DEBUG