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