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