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