glob.c revision 178825
159138Smsmith/* 259138Smsmith * Copyright (c) 1989, 1993 359138Smsmith * The Regents of the University of California. All rights reserved. 459138Smsmith * 559138Smsmith * This code is derived from software contributed to Berkeley by 659138Smsmith * Guido van Rossum. 759138Smsmith * 859138Smsmith * Redistribution and use in source and binary forms, with or without 959138Smsmith * modification, are permitted provided that the following conditions 1059138Smsmith * are met: 1159138Smsmith * 1. Redistributions of source code must retain the above copyright 1259138Smsmith * notice, this list of conditions and the following disclaimer. 1359138Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1459138Smsmith * notice, this list of conditions and the following disclaimer in the 1559138Smsmith * documentation and/or other materials provided with the distribution. 1659138Smsmith * 3. Neither the name of the University nor the names of its contributors 1759138Smsmith * may be used to endorse or promote products derived from this software 1859138Smsmith * without specific prior written permission. 1959138Smsmith * 2059138Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2159138Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2259138Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2359138Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2459138Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2559138Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2659138Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2759138Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2859138Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2959138Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3059138Smsmith * SUCH DAMAGE. 3159138Smsmith */ 3259138Smsmith 3359138Smsmith/* 3459138Smsmith * glob(3) -- a superset of the one defined in POSIX 1003.2. 3559138Smsmith * 3659159Smsmith * The [!...] convention to negate a range is supported (SysV, Posix, ksh). 3759159Smsmith * 3859138Smsmith * Optional extra services, controlled by flags not defined by POSIX: 3959138Smsmith * 4059138Smsmith * GLOB_QUOTE: 4159138Smsmith * Escaping convention: \ inhibits any special meaning the following 4259138Smsmith * character might have (except \ at end of string is retained). 4359138Smsmith * GLOB_MAGCHAR: 4459138Smsmith * Set in gl_flags if pattern contained a globbing character. 4559138Smsmith * GLOB_NOMAGIC: 4659138Smsmith * Same as GLOB_NOCHECK, but it will only append pattern if it did 4759138Smsmith * not contain any magic characters. [Used in csh style globbing] 4859138Smsmith * GLOB_ALTDIRFUNC: 4959138Smsmith * Use alternately specified directory access functions. 5059138Smsmith * GLOB_TILDE: 5159138Smsmith * expand ~user/foo to the /home/dir/of/user/foo 5259138Smsmith * GLOB_BRACE: 5359138Smsmith * expand {1,2}{a,b} to 1a 1b 2a 2b 5459138Smsmith * gl_matchc: 5559138Smsmith * Number of matches in the current invocation of glob. 5659138Smsmith */ 5759138Smsmith 5859138Smsmith#ifdef HAVE_CONFIG_H 5959138Smsmith#include <config.h> 6059138Smsmith#endif 6159138Smsmith 6259138Smsmith#ifdef HAVE_SYS_PARAM_H 6359138Smsmith#include <sys/param.h> 6459138Smsmith#endif 6559138Smsmith#ifdef HAVE_SYS_TYPES_H 6659138Smsmith#include <sys/types.h> 6759138Smsmith#endif 6859138Smsmith#ifdef HAVE_SYS_STAT_H 6959138Smsmith#include <sys/stat.h> 7059138Smsmith#endif 7159138Smsmith 7259138Smsmith#include <ctype.h> 7359138Smsmith#ifdef HAVE_DIRENT_H 7459138Smsmith#include <dirent.h> 7559138Smsmith#endif 7659138Smsmith#include <errno.h> 7759138Smsmith#ifdef HAVE_PWD_H 7859138Smsmith#include <pwd.h> 7959138Smsmith#endif 8059138Smsmith#include <stdio.h> 8159138Smsmith#include <stdlib.h> 82182965Ssepotvin#include <string.h> 8359138Smsmith#ifdef HAVE_UNISTD_H 8459138Smsmith#include <unistd.h> 85182965Ssepotvin#endif 8659138Smsmith#ifdef HAVE_LIMITS_H 8759138Smsmith#include <limits.h> 8859138Smsmith#endif 8959138Smsmith 90182965Ssepotvin#include "glob.h" 91182965Ssepotvin#include "roken.h" 9259138Smsmith 93182965Ssepotvin#ifndef ARG_MAX 9459138Smsmith#define ARG_MAX _POSIX_ARG_MAX 9559138Smsmith#endif 9659138Smsmith 9759138Smsmith#define CHAR_DOLLAR '$' 9859138Smsmith#define CHAR_DOT '.' 9959138Smsmith#define CHAR_EOS '\0' 10059138Smsmith#define CHAR_LBRACKET '[' 10159138Smsmith#define CHAR_NOT '!' 10259138Smsmith#define CHAR_QUESTION '?' 10359138Smsmith#define CHAR_QUOTE '\\' 10459138Smsmith#define CHAR_RANGE '-' 10559138Smsmith#define CHAR_RBRACKET ']' 10659138Smsmith#define CHAR_SEP '/' 10759138Smsmith#define CHAR_STAR '*' 10859138Smsmith#define CHAR_TILDE '~' 10959138Smsmith#define CHAR_UNDERSCORE '_' 11059138Smsmith#define CHAR_LBRACE '{' 11159138Smsmith#define CHAR_RBRACE '}' 11259138Smsmith#define CHAR_SLASH '/' 11359138Smsmith#define CHAR_COMMA ',' 11459138Smsmith 11559138Smsmith#ifndef DEBUG 11659138Smsmith 11759138Smsmith#define M_QUOTE 0x8000 11859138Smsmith#define M_PROTECT 0x4000 11959138Smsmith#define M_MASK 0xffff 12059138Smsmith#define M_ASCII 0x00ff 12159138Smsmith 12259138Smsmithtypedef u_short Char; 12359138Smsmith 12459138Smsmith#else 12559138Smsmith 12659138Smsmith#define M_QUOTE 0x80 12759138Smsmith#define M_PROTECT 0x40 12859138Smsmith#define M_MASK 0xff 12959138Smsmith#define M_ASCII 0x7f 13059138Smsmith 13159138Smsmithtypedef char Char; 13259138Smsmith 13359138Smsmith#endif 13459138Smsmith 13559138Smsmith 13659138Smsmith#define CHAR(c) ((Char)((c)&M_ASCII)) 13759138Smsmith#define META(c) ((Char)((c)|M_QUOTE)) 13859138Smsmith#define M_ALL META('*') 13959138Smsmith#define M_END META(']') 14059138Smsmith#define M_NOT META('!') 14159138Smsmith#define M_ONE META('?') 14259138Smsmith#define M_RNG META('-') 14359138Smsmith#define M_SET META('[') 14459138Smsmith#define ismeta(c) (((c)&M_QUOTE) != 0) 14559138Smsmith 14659138Smsmith 14759138Smsmithstatic int compare (const void *, const void *); 14859138Smsmithstatic void g_Ctoc (const Char *, char *); 14959138Smsmithstatic int g_lstat (Char *, struct stat *, glob_t *); 15059138Smsmithstatic DIR *g_opendir (Char *, glob_t *); 15159138Smsmithstatic Char *g_strchr (const Char *, int); 15259138Smsmith#ifdef notdef 15359138Smsmithstatic Char *g_strcat (Char *, const Char *); 15459138Smsmith#endif 15559138Smsmithstatic int g_stat (Char *, struct stat *, glob_t *); 15659138Smsmithstatic int glob0 (const Char *, glob_t *); 15759138Smsmithstatic int glob1 (Char *, glob_t *, size_t *); 15859138Smsmithstatic int glob2 (Char *, Char *, Char *, glob_t *, size_t *); 15959138Smsmithstatic int glob3 (Char *, Char *, Char *, Char *, glob_t *, size_t *); 16059138Smsmithstatic int globextend (const Char *, glob_t *, size_t *); 16159138Smsmithstatic const Char * globtilde (const Char *, Char *, glob_t *); 16259138Smsmithstatic int globexp1 (const Char *, glob_t *); 16359138Smsmithstatic int globexp2 (const Char *, const Char *, glob_t *, int *); 16459138Smsmithstatic int match (Char *, Char *, Char *); 16559138Smsmith#ifdef DEBUG 16659138Smsmithstatic void qprintf (const char *, Char *); 16759138Smsmith#endif 16859138Smsmith 16959138Smsmithint ROKEN_LIB_FUNCTION 17059138Smsmithglob(const char *pattern, 17159138Smsmith int flags, 17259138Smsmith int (*errfunc)(const char *, int), 17359138Smsmith glob_t *pglob) 17459138Smsmith{ 17559138Smsmith const u_char *patnext; 17659138Smsmith int c; 17759138Smsmith Char *bufnext, *bufend, patbuf[MaxPathLen+1]; 17859138Smsmith 17959138Smsmith patnext = (const u_char *) pattern; 18059138Smsmith if (!(flags & GLOB_APPEND)) { 18159138Smsmith pglob->gl_pathc = 0; 18259138Smsmith pglob->gl_pathv = NULL; 18359138Smsmith if (!(flags & GLOB_DOOFFS)) 18459138Smsmith pglob->gl_offs = 0; 18559138Smsmith } 18659138Smsmith pglob->gl_flags = flags & ~GLOB_MAGCHAR; 18759138Smsmith pglob->gl_errfunc = errfunc; 18859138Smsmith pglob->gl_matchc = 0; 18959138Smsmith 19059138Smsmith bufnext = patbuf; 19159138Smsmith bufend = bufnext + MaxPathLen; 19259138Smsmith if (flags & GLOB_QUOTE) { 19359138Smsmith /* Protect the quoted characters. */ 19459138Smsmith while (bufnext < bufend && (c = *patnext++) != CHAR_EOS) 19559138Smsmith if (c == CHAR_QUOTE) { 19659138Smsmith if ((c = *patnext++) == CHAR_EOS) { 19759138Smsmith c = CHAR_QUOTE; 19859138Smsmith --patnext; 19959138Smsmith } 20059138Smsmith *bufnext++ = c | M_PROTECT; 20159138Smsmith } 20259138Smsmith else 20359138Smsmith *bufnext++ = c; 20459138Smsmith } 20559138Smsmith else 20659138Smsmith while (bufnext < bufend && (c = *patnext++) != CHAR_EOS) 20759138Smsmith *bufnext++ = c; 20859138Smsmith *bufnext = CHAR_EOS; 20959138Smsmith 21059138Smsmith if (flags & GLOB_BRACE) 21159138Smsmith return globexp1(patbuf, pglob); 21259138Smsmith else 21359138Smsmith return glob0(patbuf, pglob); 21459138Smsmith} 21559138Smsmith 21659138Smsmith/* 21759138Smsmith * Expand recursively a glob {} pattern. When there is no more expansion 21859138Smsmith * invoke the standard globbing routine to glob the rest of the magic 21959138Smsmith * characters 22059138Smsmith */ 22159138Smsmithstatic int globexp1(const Char *pattern, glob_t *pglob) 22259138Smsmith{ 22359138Smsmith const Char* ptr = pattern; 22459138Smsmith int rv; 22559138Smsmith 22659138Smsmith /* Protect a single {}, for find(1), like csh */ 22759138Smsmith if (pattern[0] == CHAR_LBRACE && pattern[1] == CHAR_RBRACE && pattern[2] == CHAR_EOS) 22859138Smsmith return glob0(pattern, pglob); 22959138Smsmith 23059138Smsmith while ((ptr = (const Char *) g_strchr(ptr, CHAR_LBRACE)) != NULL) 23159138Smsmith if (!globexp2(ptr, pattern, pglob, &rv)) 23259138Smsmith return rv; 23359138Smsmith 23459138Smsmith return glob0(pattern, pglob); 23559138Smsmith} 23659138Smsmith 23759138Smsmith 23859138Smsmith/* 23959138Smsmith * Recursive brace globbing helper. Tries to expand a single brace. 24059138Smsmith * If it succeeds then it invokes globexp1 with the new pattern. 24159138Smsmith * If it fails then it tries to glob the rest of the pattern and returns. 24259138Smsmith */ 24359138Smsmithstatic int globexp2(const Char *ptr, const Char *pattern, 24459138Smsmith glob_t *pglob, int *rv) 24559138Smsmith{ 24659138Smsmith int i; 24759138Smsmith Char *lm, *ls; 24859138Smsmith const Char *pe, *pm, *pl; 24959138Smsmith Char patbuf[MaxPathLen + 1]; 25059138Smsmith 25159138Smsmith /* copy part up to the brace */ 25259138Smsmith for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 25359138Smsmith continue; 25459138Smsmith ls = lm; 25559138Smsmith 256231772Sken /* Find the balanced brace */ 25759138Smsmith for (i = 0, pe = ++ptr; *pe; pe++) 25859138Smsmith if (*pe == CHAR_LBRACKET) { 25959138Smsmith /* Ignore everything between [] */ 26059138Smsmith for (pm = pe++; *pe != CHAR_RBRACKET && *pe != CHAR_EOS; pe++) 26159138Smsmith continue; 26259138Smsmith if (*pe == CHAR_EOS) { 26359138Smsmith /* 26459138Smsmith * We could not find a matching CHAR_RBRACKET. 26559138Smsmith * Ignore and just look for CHAR_RBRACE 26659138Smsmith */ 26759138Smsmith pe = pm; 26859138Smsmith } 26959138Smsmith } 27059138Smsmith else if (*pe == CHAR_LBRACE) 27159138Smsmith i++; 27259138Smsmith else if (*pe == CHAR_RBRACE) { 27359138Smsmith if (i == 0) 27459138Smsmith break; 27559138Smsmith i--; 27659138Smsmith } 27759138Smsmith 27859138Smsmith /* Non matching braces; just glob the pattern */ 27959138Smsmith if (i != 0 || *pe == CHAR_EOS) { 28059138Smsmith *rv = glob0(patbuf, pglob); 28159138Smsmith return 0; 28259138Smsmith } 28359138Smsmith 28459138Smsmith for (i = 0, pl = pm = ptr; pm <= pe; pm++) 28559138Smsmith switch (*pm) { 28659138Smsmith case CHAR_LBRACKET: 28759138Smsmith /* Ignore everything between [] */ 28859138Smsmith for (pl = pm++; *pm != CHAR_RBRACKET && *pm != CHAR_EOS; pm++) 28959138Smsmith continue; 290 if (*pm == CHAR_EOS) { 291 /* 292 * We could not find a matching CHAR_RBRACKET. 293 * Ignore and just look for CHAR_RBRACE 294 */ 295 pm = pl; 296 } 297 break; 298 299 case CHAR_LBRACE: 300 i++; 301 break; 302 303 case CHAR_RBRACE: 304 if (i) { 305 i--; 306 break; 307 } 308 /* FALLTHROUGH */ 309 case CHAR_COMMA: 310 if (i && *pm == CHAR_COMMA) 311 break; 312 else { 313 /* Append the current string */ 314 for (lm = ls; (pl < pm); *lm++ = *pl++) 315 continue; 316 /* 317 * Append the rest of the pattern after the 318 * closing brace 319 */ 320 for (pl = pe + 1; (*lm++ = *pl++) != CHAR_EOS;) 321 continue; 322 323 /* Expand the current pattern */ 324#ifdef DEBUG 325 qprintf("globexp2:", patbuf); 326#endif 327 *rv = globexp1(patbuf, pglob); 328 329 /* move after the comma, to the next string */ 330 pl = pm + 1; 331 } 332 break; 333 334 default: 335 break; 336 } 337 *rv = 0; 338 return 0; 339} 340 341 342 343/* 344 * expand tilde from the passwd file. 345 */ 346static const Char * 347globtilde(const Char *pattern, Char *patbuf, glob_t *pglob) 348{ 349 struct passwd *pwd; 350 char *h; 351 const Char *p; 352 Char *b; 353 354 if (*pattern != CHAR_TILDE || !(pglob->gl_flags & GLOB_TILDE)) 355 return pattern; 356 357 /* Copy up to the end of the string or / */ 358 for (p = pattern + 1, h = (char *) patbuf; *p && *p != CHAR_SLASH; 359 *h++ = *p++) 360 continue; 361 362 *h = CHAR_EOS; 363 364 if (((char *) patbuf)[0] == CHAR_EOS) { 365 /* 366 * handle a plain ~ or ~/ by expanding $HOME 367 * first and then trying the password file 368 */ 369 if ((h = getenv("HOME")) == NULL) { 370 if ((pwd = k_getpwuid(getuid())) == NULL) 371 return pattern; 372 else 373 h = pwd->pw_dir; 374 } 375 } 376 else { 377 /* 378 * Expand a ~user 379 */ 380 if ((pwd = k_getpwnam((char*) patbuf)) == NULL) 381 return pattern; 382 else 383 h = pwd->pw_dir; 384 } 385 386 /* Copy the home directory */ 387 for (b = patbuf; *h; *b++ = *h++) 388 continue; 389 390 /* Append the rest of the pattern */ 391 while ((*b++ = *p++) != CHAR_EOS) 392 continue; 393 394 return patbuf; 395} 396 397 398/* 399 * The main glob() routine: compiles the pattern (optionally processing 400 * quotes), calls glob1() to do the real pattern matching, and finally 401 * sorts the list (unless unsorted operation is requested). Returns 0 402 * if things went well, nonzero if errors occurred. It is not an error 403 * to find no matches. 404 */ 405static int 406glob0(const Char *pattern, glob_t *pglob) 407{ 408 const Char *qpatnext; 409 int c, err, oldpathc; 410 Char *bufnext, patbuf[MaxPathLen+1]; 411 size_t limit = 0; 412 413 qpatnext = globtilde(pattern, patbuf, pglob); 414 oldpathc = pglob->gl_pathc; 415 bufnext = patbuf; 416 417 /* We don't need to check for buffer overflow any more. */ 418 while ((c = *qpatnext++) != CHAR_EOS) { 419 switch (c) { 420 case CHAR_LBRACKET: 421 c = *qpatnext; 422 if (c == CHAR_NOT) 423 ++qpatnext; 424 if (*qpatnext == CHAR_EOS || 425 g_strchr(qpatnext+1, CHAR_RBRACKET) == NULL) { 426 *bufnext++ = CHAR_LBRACKET; 427 if (c == CHAR_NOT) 428 --qpatnext; 429 break; 430 } 431 *bufnext++ = M_SET; 432 if (c == CHAR_NOT) 433 *bufnext++ = M_NOT; 434 c = *qpatnext++; 435 do { 436 *bufnext++ = CHAR(c); 437 if (*qpatnext == CHAR_RANGE && 438 (c = qpatnext[1]) != CHAR_RBRACKET) { 439 *bufnext++ = M_RNG; 440 *bufnext++ = CHAR(c); 441 qpatnext += 2; 442 } 443 } while ((c = *qpatnext++) != CHAR_RBRACKET); 444 pglob->gl_flags |= GLOB_MAGCHAR; 445 *bufnext++ = M_END; 446 break; 447 case CHAR_QUESTION: 448 pglob->gl_flags |= GLOB_MAGCHAR; 449 *bufnext++ = M_ONE; 450 break; 451 case CHAR_STAR: 452 pglob->gl_flags |= GLOB_MAGCHAR; 453 /* collapse adjacent stars to one, 454 * to avoid exponential behavior 455 */ 456 if (bufnext == patbuf || bufnext[-1] != M_ALL) 457 *bufnext++ = M_ALL; 458 break; 459 default: 460 *bufnext++ = CHAR(c); 461 break; 462 } 463 } 464 *bufnext = CHAR_EOS; 465#ifdef DEBUG 466 qprintf("glob0:", patbuf); 467#endif 468 469 if ((err = glob1(patbuf, pglob, &limit)) != 0) 470 return(err); 471 472 /* 473 * If there was no match we are going to append the pattern 474 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 475 * and the pattern did not contain any magic characters 476 * GLOB_NOMAGIC is there just for compatibility with csh. 477 */ 478 if (pglob->gl_pathc == oldpathc && 479 ((pglob->gl_flags & GLOB_NOCHECK) || 480 ((pglob->gl_flags & GLOB_NOMAGIC) && 481 !(pglob->gl_flags & GLOB_MAGCHAR)))) 482 return(globextend(pattern, pglob, &limit)); 483 else if (!(pglob->gl_flags & GLOB_NOSORT)) 484 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 485 pglob->gl_pathc - oldpathc, sizeof(char *), compare); 486 return(0); 487} 488 489static int 490compare(const void *p, const void *q) 491{ 492 return(strcmp(*(char **)p, *(char **)q)); 493} 494 495static int 496glob1(Char *pattern, glob_t *pglob, size_t *limit) 497{ 498 Char pathbuf[MaxPathLen+1]; 499 500 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 501 if (*pattern == CHAR_EOS) 502 return(0); 503 return(glob2(pathbuf, pathbuf, pattern, pglob, limit)); 504} 505 506/* 507 * The functions glob2 and glob3 are mutually recursive; there is one level 508 * of recursion for each segment in the pattern that contains one or more 509 * meta characters. 510 */ 511 512#ifndef S_ISLNK 513#if defined(S_IFLNK) && defined(S_IFMT) 514#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) 515#else 516#define S_ISLNK(mode) 0 517#endif 518#endif 519 520static int 521glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob, 522 size_t *limit) 523{ 524 struct stat sb; 525 Char *p, *q; 526 int anymeta; 527 528 /* 529 * Loop over pattern segments until end of pattern or until 530 * segment with meta character found. 531 */ 532 for (anymeta = 0;;) { 533 if (*pattern == CHAR_EOS) { /* End of pattern? */ 534 *pathend = CHAR_EOS; 535 if (g_lstat(pathbuf, &sb, pglob)) 536 return(0); 537 538 if (((pglob->gl_flags & GLOB_MARK) && 539 pathend[-1] != CHAR_SEP) && (S_ISDIR(sb.st_mode) 540 || (S_ISLNK(sb.st_mode) && 541 (g_stat(pathbuf, &sb, pglob) == 0) && 542 S_ISDIR(sb.st_mode)))) { 543 *pathend++ = CHAR_SEP; 544 *pathend = CHAR_EOS; 545 } 546 ++pglob->gl_matchc; 547 return(globextend(pathbuf, pglob, limit)); 548 } 549 550 /* Find end of next segment, copy tentatively to pathend. */ 551 q = pathend; 552 p = pattern; 553 while (*p != CHAR_EOS && *p != CHAR_SEP) { 554 if (ismeta(*p)) 555 anymeta = 1; 556 *q++ = *p++; 557 } 558 559 if (!anymeta) { /* No expansion, do next segment. */ 560 pathend = q; 561 pattern = p; 562 while (*pattern == CHAR_SEP) 563 *pathend++ = *pattern++; 564 } else /* Need expansion, recurse. */ 565 return(glob3(pathbuf, pathend, pattern, p, pglob, 566 limit)); 567 } 568 /* NOTREACHED */ 569} 570 571static int 572glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, 573 glob_t *pglob, size_t *limit) 574{ 575 struct dirent *dp; 576 DIR *dirp; 577 int err; 578 char buf[MaxPathLen]; 579 580 /* 581 * The readdirfunc declaration can't be prototyped, because it is 582 * assigned, below, to two functions which are prototyped in glob.h 583 * and dirent.h as taking pointers to differently typed opaque 584 * structures. 585 */ 586 struct dirent *(*readdirfunc)(void *); 587 588 *pathend = CHAR_EOS; 589 errno = 0; 590 591 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 592 /* TODO: don't call for ENOENT or ENOTDIR? */ 593 if (pglob->gl_errfunc) { 594 g_Ctoc(pathbuf, buf); 595 if (pglob->gl_errfunc(buf, errno) || 596 pglob->gl_flags & GLOB_ERR) 597 return (GLOB_ABEND); 598 } 599 return(0); 600 } 601 602 err = 0; 603 604 /* Search directory for matching names. */ 605 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 606 readdirfunc = pglob->gl_readdir; 607 else 608 readdirfunc = (struct dirent *(*)(void *))readdir; 609 while ((dp = (*readdirfunc)(dirp))) { 610 u_char *sc; 611 Char *dc; 612 613 /* Initial CHAR_DOT must be matched literally. */ 614 if (dp->d_name[0] == CHAR_DOT && *pattern != CHAR_DOT) 615 continue; 616 for (sc = (u_char *) dp->d_name, dc = pathend; 617 (*dc++ = *sc++) != CHAR_EOS;) 618 continue; 619 if (!match(pathend, pattern, restpattern)) { 620 *pathend = CHAR_EOS; 621 continue; 622 } 623 err = glob2(pathbuf, --dc, restpattern, pglob, limit); 624 if (err) 625 break; 626 } 627 628 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 629 (*pglob->gl_closedir)(dirp); 630 else 631 closedir(dirp); 632 return(err); 633} 634 635 636/* 637 * Extend the gl_pathv member of a glob_t structure to accomodate a new item, 638 * add the new item, and update gl_pathc. 639 * 640 * This assumes the BSD realloc, which only copies the block when its size 641 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 642 * behavior. 643 * 644 * Return 0 if new item added, error code if memory couldn't be allocated. 645 * 646 * Invariant of the glob_t structure: 647 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 648 * gl_pathv points to (gl_offs + gl_pathc + 1) items. 649 */ 650static int 651globextend(const Char *path, glob_t *pglob, size_t *limit) 652{ 653 char **pathv; 654 int i; 655 size_t newsize, len; 656 char *copy; 657 const Char *p; 658 659 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 660 pathv = pglob->gl_pathv ? 661 realloc(pglob->gl_pathv, newsize) : 662 malloc(newsize); 663 if (pathv == NULL) 664 return(GLOB_NOSPACE); 665 666 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 667 /* first time around -- clear initial gl_offs items */ 668 pathv += pglob->gl_offs; 669 for (i = pglob->gl_offs; --i >= 0; ) 670 *--pathv = NULL; 671 } 672 pglob->gl_pathv = pathv; 673 674 for (p = path; *p++;) 675 continue; 676 len = (size_t)(p - path); 677 *limit += len; 678 if ((copy = malloc(len)) != NULL) { 679 g_Ctoc(path, copy); 680 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 681 } 682 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 683 684 if ((pglob->gl_flags & GLOB_LIMIT) && (newsize + *limit) >= ARG_MAX) { 685 errno = 0; 686 return(GLOB_NOSPACE); 687 } 688 689 return(copy == NULL ? GLOB_NOSPACE : 0); 690} 691 692 693/* 694 * pattern matching function for filenames. Each occurrence of the * 695 * pattern causes a recursion level. 696 */ 697static int 698match(Char *name, Char *pat, Char *patend) 699{ 700 int ok, negate_range; 701 Char c, k; 702 703 while (pat < patend) { 704 c = *pat++; 705 switch (c & M_MASK) { 706 case M_ALL: 707 if (pat == patend) 708 return(1); 709 do 710 if (match(name, pat, patend)) 711 return(1); 712 while (*name++ != CHAR_EOS); 713 return(0); 714 case M_ONE: 715 if (*name++ == CHAR_EOS) 716 return(0); 717 break; 718 case M_SET: 719 ok = 0; 720 if ((k = *name++) == CHAR_EOS) 721 return(0); 722 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != CHAR_EOS) 723 ++pat; 724 while (((c = *pat++) & M_MASK) != M_END) 725 if ((*pat & M_MASK) == M_RNG) { 726 if (c <= k && k <= pat[1]) 727 ok = 1; 728 pat += 2; 729 } else if (c == k) 730 ok = 1; 731 if (ok == negate_range) 732 return(0); 733 break; 734 default: 735 if (*name++ != c) 736 return(0); 737 break; 738 } 739 } 740 return(*name == CHAR_EOS); 741} 742 743/* Free allocated data belonging to a glob_t structure. */ 744void ROKEN_LIB_FUNCTION 745globfree(glob_t *pglob) 746{ 747 int i; 748 char **pp; 749 750 if (pglob->gl_pathv != NULL) { 751 pp = pglob->gl_pathv + pglob->gl_offs; 752 for (i = pglob->gl_pathc; i--; ++pp) 753 if (*pp) 754 free(*pp); 755 free(pglob->gl_pathv); 756 pglob->gl_pathv = NULL; 757 } 758} 759 760static DIR * 761g_opendir(Char *str, glob_t *pglob) 762{ 763 char buf[MaxPathLen]; 764 765 if (!*str) 766 strlcpy(buf, ".", sizeof(buf)); 767 else 768 g_Ctoc(str, buf); 769 770 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 771 return((*pglob->gl_opendir)(buf)); 772 773 return(opendir(buf)); 774} 775 776static int 777g_lstat(Char *fn, struct stat *sb, glob_t *pglob) 778{ 779 char buf[MaxPathLen]; 780 781 g_Ctoc(fn, buf); 782 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 783 return((*pglob->gl_lstat)(buf, sb)); 784 return(lstat(buf, sb)); 785} 786 787static int 788g_stat(Char *fn, struct stat *sb, glob_t *pglob) 789{ 790 char buf[MaxPathLen]; 791 792 g_Ctoc(fn, buf); 793 if (pglob->gl_flags & GLOB_ALTDIRFUNC) 794 return((*pglob->gl_stat)(buf, sb)); 795 return(stat(buf, sb)); 796} 797 798static Char * 799g_strchr(const Char *str, int ch) 800{ 801 do { 802 if (*str == ch) 803 return (Char *)str; 804 } while (*str++); 805 return (NULL); 806} 807 808#ifdef notdef 809static Char * 810g_strcat(Char *dst, const Char *src) 811{ 812 Char *sdst = dst; 813 814 while (*dst++) 815 continue; 816 --dst; 817 while((*dst++ = *src++) != CHAR_EOS) 818 continue; 819 820 return (sdst); 821} 822#endif 823 824static void 825g_Ctoc(const Char *str, char *buf) 826{ 827 char *dc; 828 829 for (dc = buf; (*dc++ = *str++) != CHAR_EOS;) 830 continue; 831} 832 833#ifdef DEBUG 834static void 835qprintf(const Char *str, Char *s) 836{ 837 Char *p; 838 839 printf("%s:\n", str); 840 for (p = s; *p; p++) 841 printf("%c", CHAR(*p)); 842 printf("\n"); 843 for (p = s; *p; p++) 844 printf("%c", *p & M_PROTECT ? '"' : ' '); 845 printf("\n"); 846 for (p = s; *p; p++) 847 printf("%c", ismeta(*p) ? '_' : ' '); 848 printf("\n"); 849} 850#endif 851