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