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