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_NOESCAPE) 186 while (bufnext < bufend && (c = *patnext++) != EOS) 187 *bufnext++ = c; 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; 197 } 198 else 199 *bufnext++ = c; 200 } 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 (issetugid() != 0 || 379 (h = getenv("HOME")) == NULL) { 380 if (((h = getlogin()) != NULL && 381 (pwd = getpwnam(h)) != NULL) || 382 (pwd = getpwuid(getuid())) != NULL) 383 h = pwd->pw_dir; 384 else 385 return pattern; 386 } 387 } 388 else { 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++) 400 continue; 401 402 /* Append the rest of the pattern */ 403 while (b < eb && (*b++ = *p++) != EOS) 404 continue; 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. 416 */ 417 static int 418 glob0(pattern, pglob, limit) 419 const Char *pattern; 420 glob_t *pglob; 421 int *limit; 422 { 423 const Char *qpatnext; 424 int c, err, oldpathc; 425 Char *bufnext, patbuf[MAXPATHLEN]; 426 427 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); 428 oldpathc = pglob->gl_pathc; 429 bufnext = patbuf; 430 431 /* We don't need to check for buffer overflow any more. */ 432 while ((c = *qpatnext++) != EOS) { 433 switch (c) { 434 case LBRACKET: 435 c = *qpatnext; 436 if (c == NOT) 437 ++qpatnext; 438 if (*qpatnext == EOS || 439 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { 440 *bufnext++ = LBRACKET; 441 if (c == NOT) 442 --qpatnext; 443 break; 444 } 445 *bufnext++ = M_SET; 446 if (c == NOT) 447 *bufnext++ = M_NOT; 448 c = *qpatnext++; 449 do { 450 *bufnext++ = CHAR(c); 451 if (*qpatnext == RANGE && 452 (c = qpatnext[1]) != RBRACKET) { 453 *bufnext++ = M_RNG; 454 *bufnext++ = CHAR(c); 455 qpatnext += 2; 456 } 457 } while ((c = *qpatnext++) != RBRACKET); 458 pglob->gl_flags |= GLOB_MAGCHAR; 459 *bufnext++ = M_END; 460 break; 461 case QUESTION: 462 pglob->gl_flags |= GLOB_MAGCHAR; 463 *bufnext++ = M_ONE; 464 break; 465 case STAR: 466 pglob->gl_flags |= GLOB_MAGCHAR; 467 /* collapse adjacent stars to one, 468 * to avoid exponential behavior 469 */ 470 if (bufnext == patbuf || bufnext[-1] != M_ALL) 471 *bufnext++ = M_ALL; 472 break; 473 default: 474 *bufnext++ = CHAR(c); 475 break; 476 } 477 } 478 *bufnext = EOS; 479 #ifdef DEBUG 480 qprintf("glob0:", patbuf); 481 #endif 482 483 if ((err = glob1(patbuf, pglob, limit)) != 0) 484 return(err); 485 486 /* 487 * If there was no match we are going to append the pattern 488 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 489 * and the pattern did not contain any magic characters 490 * GLOB_NOMAGIC is there just for compatibility with csh. 491 */ 492 if (pglob->gl_pathc == oldpathc) { 493 if (((pglob->gl_flags & GLOB_NOCHECK) || 494 ((pglob->gl_flags & GLOB_NOMAGIC) && 495 !(pglob->gl_flags & GLOB_MAGCHAR)))) 496 return(globextend(pattern, pglob, limit)); 497 else 498 return(GLOB_NOMATCH); 499 } 500 if (!(pglob->gl_flags & GLOB_NOSORT)) 501 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 502 pglob->gl_pathc - oldpathc, sizeof(char *), compare); 503 return(0); 504 } 505 506 static int 507 compare(p, q) 508 const void *p, *q; 509 { 510 return(strcmp(*(char **)p, *(char **)q)); 511 } 512 513 static int 514 glob1(pattern, pglob, limit) 515 Char *pattern; 516 glob_t *pglob; 517 int *limit; 518 { 519 Char pathbuf[MAXPATHLEN]; 520 521 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 522 if (*pattern == EOS) 523 return(0); 524 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, 525 pattern, pglob, limit)); 526 } 527 528 /* 529 * The functions glob2 and glob3 are mutually recursive; there is one level 530 * of recursion for each segment in the pattern that contains one or more 531 * meta characters. 532 */ 533 static int 534 glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit) 535 Char *pathbuf, *pathend, *pathend_last, *pattern; 536 glob_t *pglob; 537 int *limit; 538 { 539 struct stat sb; 540 Char *p, *q; 541 int anymeta; 542 543 /* 544 * Loop over pattern segments until end of pattern or until 545 * segment with meta character found. 546 */ 547 for (anymeta = 0;;) { 548 if (*pattern == EOS) { /* End of pattern? */ 549 *pathend = EOS; 550 if (g_lstat(pathbuf, &sb, pglob)) 551 return(0); 552 553 if (((pglob->gl_flags & GLOB_MARK) && 554 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 555 || (S_ISLNK(sb.st_mode) && 556 (g_stat(pathbuf, &sb, pglob) == 0) && 557 S_ISDIR(sb.st_mode)))) { 558 if (pathend + 1 > pathend_last) 559 return (GLOB_ABORTED); 560 *pathend++ = SEP; 561 *pathend = EOS; 562 } 563 ++pglob->gl_matchc; 564 return(globextend(pathbuf, pglob, limit)); 565 } 566 567 /* Find end of next segment, copy tentatively to pathend. */ 568 q = pathend; 569 p = pattern; 570 while (*p != EOS && *p != SEP) { 571 if (ismeta(*p)) 572 anymeta = 1; 573 if (q + 1 > pathend_last) 574 return (GLOB_ABORTED); 575 *q++ = *p++; 576 } 577 578 if (!anymeta) { /* No expansion, do next segment. */ 579 pathend = q; 580 pattern = p; 581 while (*pattern == SEP) { 582 if (pathend + 1 > pathend_last) 583 return (GLOB_ABORTED); 584 *pathend++ = *pattern++; 585 } 586 } else /* Need expansion, recurse. */ 587 return(glob3(pathbuf, pathend, pathend_last, pattern, p, 588 pglob, limit)); 589 } 590 /* NOTREACHED */ 591 } 592 593 static int 594 glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit) 595 Char *pathbuf, *pathend, *pathend_last, *pattern, *restpattern; 596 glob_t *pglob; 597 int *limit; 598 { 599 struct dirent *dp; 600 DIR *dirp; 601 int err; 602 char buf[MAXPATHLEN]; 603 604 /* 605 * The readdirfunc declaration can't be prototyped, because it is 606 * assigned, below, to two functions which are prototyped in glob.h 607 * and dirent.h as taking pointers to differently typed opaque 608 * structures. 609 */ 610 struct dirent *(*readdirfunc)(); 611 612 if (pathend > pathend_last) 613 return (GLOB_ABORTED); 614 *pathend = EOS; 615 errno = 0; 616 617 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 618 /* TODO: don't call for ENOENT or ENOTDIR? */ 619 if (pglob->gl_errfunc) { 620 if (g_Ctoc(pathbuf, buf, sizeof(buf))) 621 return (GLOB_ABORTED); 622 if (pglob->gl_errfunc(buf, errno) || 623 pglob->gl_flags & GLOB_ERR) 624 return (GLOB_ABORTED); 625 } 626 return(0); 627 } 628 629 err = 0; 630 631 /* Search directory for matching names. */ 632 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 633 readdirfunc = pglob->gl_readdir; 634 else 635 readdirfunc = readdir; 636 while ((dp = (*readdirfunc)(dirp))) { 637 u_char *sc; 638 Char *dc; 639 640 /* Initial DOT must be matched literally. */ 641 if (dp->d_name[0] == DOT && *pattern != DOT) 642 continue; 643 dc = pathend; 644 sc = (u_char *) dp->d_name; 645 while (dc < pathend_last && (*dc++ = *sc++) != EOS) 646 ; 647 if (!match(pathend, pattern, restpattern)) { 648 *pathend = EOS; 649 continue; 650 } 651 err = glob2(pathbuf, --dc, pathend_last, restpattern, 652 pglob, limit); 653 if (err) 654 break; 655 } 656 657 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 658 (*pglob->gl_closedir)(dirp); 659 else 660 closedir(dirp); 661 return(err); 662 } 663 664 665 /* 666 * Extend the gl_pathv member of a glob_t structure to accomodate a new item, 667 * add the new item, and update gl_pathc. 668 * 669 * This assumes the BSD realloc, which only copies the block when its size 670 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 671 * behavior. 672 * 673 * Return 0 if new item added, error code if memory couldn't be allocated. 674 * 675 * Invariant of the glob_t structure: 676 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 677 * gl_pathv points to (gl_offs + gl_pathc + 1) items. 678 */ 679 static int 680 globextend(path, pglob, limit) 681 const Char *path; 682 glob_t *pglob; 683 int *limit; 684 { 685 char **pathv; 686 int i; 687 u_int newsize, len; 688 char *copy; 689 const Char *p; 690 691 if (*limit && pglob->gl_pathc > *limit) { 692 errno = 0; 693 return (GLOB_NOSPACE); 694 } 695 696 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 697 pathv = pglob->gl_pathv ? 698 realloc((char *)pglob->gl_pathv, newsize) : 699 malloc(newsize); 700 if (pathv == NULL) { 701 if (pglob->gl_pathv) { 702 free(pglob->gl_pathv); 703 pglob->gl_pathv = NULL; 704 } 705 return(GLOB_NOSPACE); 706 } 707 708 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 709 /* first time around -- clear initial gl_offs items */ 710 pathv += pglob->gl_offs; 711 for (i = pglob->gl_offs; --i >= 0; ) 712 *--pathv = NULL; 713 } 714 pglob->gl_pathv = pathv; 715 716 for (p = path; *p++;) 717 continue; 718 len = (size_t)(p - path); 719 if ((copy = malloc(len)) != NULL) { 720 if (g_Ctoc(path, copy, len)) { 721 free(copy); 722 return (GLOB_NOSPACE); 723 } 724 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 725 } 726 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 727 return(copy == NULL ? GLOB_NOSPACE : 0); 728 } 729 730 /* 731 * pattern matching function for filenames. Each occurrence of the * 732 * pattern causes a recursion level. 733 */ 734 static int 735 match(name, pat, patend) 736 Char *name, *pat, *patend; 737 { 738 int ok, negate_range; 739 Char c, k; 740 741 while (pat < patend) { 742 c = *pat++; 743 switch (c & M_MASK) { 744 case M_ALL: 745 if (pat == patend) 746 return(1); 747 do 748 if (match(name, pat, patend)) 749 return(1); 750 while (*name++ != EOS); 751 return(0); 752 case M_ONE: 753 if (*name++ == EOS) 754 return(0); 755 break; 756 case M_SET: 757 ok = 0; 758 if ((k = *name++) == EOS) 759 return(0); 760 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 761 ++pat; 762 while (((c = *pat++) & M_MASK) != M_END) 763 if ((*pat & M_MASK) == M_RNG) { 764 if (__collate_load_error ? 765 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) : 766 __collate_range_cmp(CHAR(c), CHAR(k)) <= 0 767 && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0 768 ) 769 ok = 1; 770 pat += 2; 771 } else if (c == k) 772 ok = 1; 773 if (ok == negate_range) 774 return(0); 775 break; 776 default: 777 if (*name++ != c) 778 return(0); 779 break; 780 } 781 } 782 return(*name == EOS); 783 } 784 785 /* Free allocated data belonging to a glob_t structure. */ 786 void 787 globfree(pglob) 788 glob_t *pglob; 789 { 790 int i; 791 char **pp; 792 793 if (pglob->gl_pathv != NULL) { 794 pp = pglob->gl_pathv + pglob->gl_offs; 795 for (i = pglob->gl_pathc; i--; ++pp) 796 if (*pp) 797 free(*pp); 798 free(pglob->gl_pathv); 799 pglob->gl_pathv = NULL; 800 } 801 } 802 803 static DIR * 804 g_opendir(str, pglob) 805 Char *str; 806 glob_t *pglob; 807 { 808 char buf[MAXPATHLEN]; 809 810 if (!*str) 811 strcpy(buf, "."); 812 else { 813 if (g_Ctoc(str, buf, sizeof(buf))) 814 return (NULL); 815 } 816 817 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 818 return((*pglob->gl_opendir)(buf)); 819 820 return(opendir(buf)); 821 } 822 823 static int 824 g_lstat(fn, sb, pglob) 825 Char *fn; 826 struct stat *sb; 827 glob_t *pglob; 828 { 829 char buf[MAXPATHLEN]; 830 831 if (g_Ctoc(fn, buf, sizeof(buf))) { 832 errno = ENAMETOOLONG; 833 return (-1); 834 } 835 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 836 return((*pglob->gl_lstat)(buf, sb)); 837 return(lstat(buf, sb)); 838 } 839 840 static int 841 g_stat(fn, sb, pglob) 842 Char *fn; 843 struct stat *sb; 844 glob_t *pglob; 845 { 846 char buf[MAXPATHLEN]; 847 848 if (g_Ctoc(fn, buf, sizeof(buf))) { 849 errno = ENAMETOOLONG; 850 return (-1); 851 } 852 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 853 return((*pglob->gl_stat)(buf, sb)); 854 return(stat(buf, sb)); 855 } 856 857 static Char * 858 g_strchr(str, ch) 859 Char *str; 860 int ch; 861 { 862 do { 863 if (*str == ch) 864 return (str); 865 } while (*str++); 866 return (NULL); 867 } 868 869 static int 870 g_Ctoc(str, buf, len) 871 const Char *str; 872 char *buf; 873 u_int len; 874 { 875 876 while (len--) { 877 if ((*buf++ = *str++) == '\0') 878 return (0); 879 } 880 return (1); 881 } 882 883 #ifdef DEBUG 884 static void 885 qprintf(str, s) 886 const char *str; 887 Char *s; 888 { 889 Char *p; 890 891 (void)printf("%s:\n", str); 892 for (p = s; *p; p++) 893 (void)printf("%c", CHAR(*p)); 894 (void)printf("\n"); 895 for (p = s; *p; p++) 896 (void)printf("%c", *p & M_PROTECT ? '"' : ' '); 897 (void)printf("\n"); 898 for (p = s; *p; p++) 899 (void)printf("%c", ismeta(*p) ? '_' : ' '); 900 (void)printf("\n"); 901 } 902 #endif 903