| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
2 |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#include "config.h" |
| 67 |
#include "compat/include/compatglob.h" |
| 68 |
|
| 69 |
#include <sys/types.h> |
| 70 |
|
| 71 |
#ifndef HAVE_GLOB |
| 72 |
|
| 73 |
#include <sys/param.h> |
| 74 |
#include <sys/stat.h> |
| 75 |
|
| 76 |
#include <dirent.h> |
| 77 |
#include <errno.h> |
| 78 |
#include <pwd.h> |
| 79 |
#include <stdio.h> |
| 80 |
#include <stdlib.h> |
| 81 |
#include <string.h> |
| 82 |
#include <unistd.h> |
| 83 |
#include <stdint.h> |
| 84 |
|
| 85 |
#define DOLLAR '$' |
| 86 |
#define DOT '.' |
| 87 |
#define EOS '\0' |
| 88 |
#define LBRACKET '[' |
| 89 |
#define NOT '!' |
| 90 |
#define QUESTION '?' |
| 91 |
#define QUOTE '\\' |
| 92 |
#define RANGE '-' |
| 93 |
#define RBRACKET ']' |
| 94 |
#define SEP '/' |
| 95 |
#define STAR '*' |
| 96 |
#define TILDE '~' |
| 97 |
#define UNDERSCORE '_' |
| 98 |
#define LBRACE '{' |
| 99 |
#define RBRACE '}' |
| 100 |
#define SLASH '/' |
| 101 |
#define COMMA ',' |
| 102 |
|
| 103 |
#ifndef DEBUG |
| 104 |
|
| 105 |
#define M_QUOTE 0x8000 |
| 106 |
#define M_PROTECT 0x4000 |
| 107 |
#define M_MASK 0xffff |
| 108 |
#define M_ASCII 0x00ff |
| 109 |
|
| 110 |
typedef u_short Char; |
| 111 |
|
| 112 |
#else |
| 113 |
|
| 114 |
#define M_QUOTE 0x80 |
| 115 |
#define M_PROTECT 0x40 |
| 116 |
#define M_MASK 0xff |
| 117 |
#define M_ASCII 0x7f |
| 118 |
|
| 119 |
typedef char Char; |
| 120 |
|
| 121 |
#endif |
| 122 |
|
| 123 |
|
| 124 |
#define CHAR(c) ((Char)((c)&M_ASCII)) |
| 125 |
#define META(c) ((Char)((c)|M_QUOTE)) |
| 126 |
#define M_ALL META('*') |
| 127 |
#define M_END META(']') |
| 128 |
#define M_NOT META('!') |
| 129 |
#define M_ONE META('?') |
| 130 |
#define M_RNG META('-') |
| 131 |
#define M_SET META('[') |
| 132 |
#define ismeta(c) (((c)&M_QUOTE) != 0) |
| 133 |
|
| 134 |
|
| 135 |
static int compare (const void *, const void *); |
| 136 |
static void g_Ctoc (const Char *, char *); |
| 137 |
static int g_lstat (Char *, struct stat *, glob_t *); |
| 138 |
static DIR *g_opendir (Char *, glob_t *); |
| 139 |
static Char *g_strchr (Char *, int); |
| 140 |
#ifdef notdef |
| 141 |
static Char *g_strcat (Char *, const Char *); |
| 142 |
#endif |
| 143 |
static int g_stat (Char *, struct stat *, glob_t *); |
| 144 |
static int glob0 (const Char *, glob_t *); |
| 145 |
static int glob1 (Char *, glob_t *); |
| 146 |
static int glob2 (Char *, Char *, Char *, glob_t *); |
| 147 |
static int glob3 (Char *, Char *, Char *, Char *, glob_t *); |
| 148 |
static int globextend (const Char *, glob_t *); |
| 149 |
static const Char * globtilde (const Char *, Char *, size_t, glob_t *); |
| 150 |
static int globexp1 (const Char *, glob_t *); |
| 151 |
static int globexp2 (const Char *, const Char *, glob_t *, int *); |
| 152 |
static int match (Char *, Char *, Char *); |
| 153 |
#ifdef DEBUG |
| 154 |
static void qprintf (const char *, Char *); |
| 155 |
#endif |
| 156 |
|
| 157 |
int |
| 158 |
glob(const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob) |
| 159 |
{ |
| 160 |
const u_char *patnext; |
| 161 |
int c; |
| 162 |
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; |
| 163 |
|
| 164 |
patnext = (const u_char *) pattern; |
| 165 |
if (!(flags & GLOB_APPEND)) { |
| 166 |
pglob->gl_pathc = 0; |
| 167 |
pglob->gl_pathv = NULL; |
| 168 |
if (!(flags & GLOB_DOOFFS)) |
| 169 |
pglob->gl_offs = 0; |
| 170 |
} |
| 171 |
pglob->gl_flags = flags & ~GLOB_MAGCHAR; |
| 172 |
pglob->gl_errfunc = errfunc; |
| 173 |
pglob->gl_matchc = 0; |
| 174 |
|
| 175 |
bufnext = patbuf; |
| 176 |
bufend = bufnext + MAXPATHLEN; |
| 177 |
if (flags & GLOB_QUOTE) { |
| 178 |
|
| 179 |
while (bufnext < bufend && (c = *patnext++) != EOS) |
| 180 |
if (c == QUOTE) { |
| 181 |
if ((c = *patnext++) == EOS) { |
| 182 |
c = QUOTE; |
| 183 |
--patnext; |
| 184 |
} |
| 185 |
*bufnext++ = c | M_PROTECT; |
| 186 |
} |
| 187 |
else |
| 188 |
*bufnext++ = c; |
| 189 |
} |
| 190 |
else |
| 191 |
while (bufnext < bufend && (c = *patnext++) != EOS) |
| 192 |
*bufnext++ = c; |
| 193 |
*bufnext = EOS; |
| 194 |
|
| 195 |
if (flags & GLOB_BRACE) |
| 196 |
return globexp1(patbuf, pglob); |
| 197 |
else |
| 198 |
return glob0(patbuf, pglob); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static int globexp1(const Char *pattern, glob_t *pglob) |
| 207 |
{ |
| 208 |
const Char* ptr = pattern; |
| 209 |
int rv; |
| 210 |
|
| 211 |
|
| 212 |
if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) |
| 213 |
return glob0(pattern, pglob); |
| 214 |
|
| 215 |
while ((ptr = (const Char *) g_strchr((Char *)(intptr_t) ptr, LBRACE)) != NULL) |
| 216 |
if (!globexp2(ptr, pattern, pglob, &rv)) |
| 217 |
return rv; |
| 218 |
|
| 219 |
return glob0(pattern, pglob); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv) |
| 229 |
{ |
| 230 |
int i; |
| 231 |
Char *lm, *ls; |
| 232 |
const Char *pe, *pm, *pl; |
| 233 |
Char patbuf[MAXPATHLEN + 1]; |
| 234 |
|
| 235 |
|
| 236 |
for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) |
| 237 |
continue; |
| 238 |
ls = lm; |
| 239 |
|
| 240 |
|
| 241 |
for (i = 0, pe = ++ptr; *pe; pe++) |
| 242 |
if (*pe == LBRACKET) { |
| 243 |
|
| 244 |
for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) |
| 245 |
continue; |
| 246 |
if (*pe == EOS) { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
pe = pm; |
| 252 |
} |
| 253 |
} |
| 254 |
else if (*pe == LBRACE) |
| 255 |
i++; |
| 256 |
else if (*pe == RBRACE) { |
| 257 |
if (i == 0) |
| 258 |
break; |
| 259 |
i--; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
if (i != 0 || *pe == EOS) { |
| 264 |
*rv = glob0(patbuf, pglob); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
for (i = 0, pl = pm = ptr; pm <= pe; pm++) |
| 269 |
switch (*pm) { |
| 270 |
case LBRACKET: |
| 271 |
|
| 272 |
for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) |
| 273 |
continue; |
| 274 |
if (*pm == EOS) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
pm = pl; |
| 280 |
} |
| 281 |
break; |
| 282 |
|
| 283 |
case LBRACE: |
| 284 |
i++; |
| 285 |
break; |
| 286 |
|
| 287 |
case RBRACE: |
| 288 |
if (i) { |
| 289 |
i--; |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
case COMMA: |
| 294 |
if (i && *pm == COMMA) |
| 295 |
break; |
| 296 |
else { |
| 297 |
|
| 298 |
for (lm = ls; (pl < pm); *lm++ = *pl++) |
| 299 |
continue; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
for (pl = pe + 1; (*lm++ = *pl++) != EOS;) |
| 305 |
continue; |
| 306 |
|
| 307 |
|
| 308 |
#ifdef DEBUG |
| 309 |
qprintf("globexp2:", patbuf); |
| 310 |
#endif |
| 311 |
*rv = globexp1(patbuf, pglob); |
| 312 |
|
| 313 |
|
| 314 |
pl = pm + 1; |
| 315 |
} |
| 316 |
break; |
| 317 |
|
| 318 |
default: |
| 319 |
break; |
| 320 |
} |
| 321 |
*rv = 0; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static const Char * |
| 331 |
globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) |
| 332 |
{ |
| 333 |
struct passwd *pwd; |
| 334 |
char *h; |
| 335 |
const Char *p; |
| 336 |
Char *b, *eb; |
| 337 |
|
| 338 |
if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) |
| 339 |
return pattern; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
eb = &patbuf[patbuf_len - 1]; |
| 345 |
for (p = pattern + 1, h = (char *) patbuf; |
| 346 |
h < (char *)eb && *p && *p != SLASH; *h++ = *p++) |
| 347 |
continue; |
| 348 |
|
| 349 |
*h = EOS; |
| 350 |
|
| 351 |
if (((char *) patbuf)[0] == EOS) { |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if ( |
| 358 |
(h = getenv("HOME")) == NULL) { |
| 359 |
if (((h = getlogin()) != NULL && |
| 360 |
(pwd = getpwnam(h)) != NULL) || |
| 361 |
(pwd = getpwuid(getuid())) != NULL) |
| 362 |
h = pwd->pw_dir; |
| 363 |
else |
| 364 |
return pattern; |
| 365 |
} |
| 366 |
} |
| 367 |
else { |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
if ((pwd = getpwnam((char*) patbuf)) == NULL) |
| 372 |
return pattern; |
| 373 |
else |
| 374 |
h = pwd->pw_dir; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
for (b = patbuf; b < eb && *h; *b++ = *h++) |
| 379 |
continue; |
| 380 |
|
| 381 |
|
| 382 |
while (b < eb && (*b++ = *p++) != EOS) |
| 383 |
continue; |
| 384 |
*b = EOS; |
| 385 |
|
| 386 |
return patbuf; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
static int |
| 398 |
glob0(const Char *pattern, glob_t *pglob) |
| 399 |
{ |
| 400 |
const Char *qpatnext; |
| 401 |
int c, err, oldpathc; |
| 402 |
Char *bufnext, patbuf[MAXPATHLEN+1]; |
| 403 |
|
| 404 |
qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), |
| 405 |
pglob); |
| 406 |
oldpathc = pglob->gl_pathc; |
| 407 |
bufnext = patbuf; |
| 408 |
|
| 409 |
|
| 410 |
while ((c = *qpatnext++) != EOS) { |
| 411 |
switch (c) { |
| 412 |
case LBRACKET: |
| 413 |
c = *qpatnext; |
| 414 |
if (c == NOT) |
| 415 |
++qpatnext; |
| 416 |
if (*qpatnext == EOS || |
| 417 |
g_strchr((Char *)(uintptr_t)qpatnext+1, RBRACKET) == NULL) { |
| 418 |
*bufnext++ = LBRACKET; |
| 419 |
if (c == NOT) |
| 420 |
--qpatnext; |
| 421 |
break; |
| 422 |
} |
| 423 |
*bufnext++ = M_SET; |
| 424 |
if (c == NOT) |
| 425 |
*bufnext++ = M_NOT; |
| 426 |
c = *qpatnext++; |
| 427 |
do { |
| 428 |
*bufnext++ = CHAR(c); |
| 429 |
if (*qpatnext == RANGE && |
| 430 |
(c = qpatnext[1]) != RBRACKET) { |
| 431 |
*bufnext++ = M_RNG; |
| 432 |
*bufnext++ = CHAR(c); |
| 433 |
qpatnext += 2; |
| 434 |
} |
| 435 |
} while ((c = *qpatnext++) != RBRACKET); |
| 436 |
pglob->gl_flags |= GLOB_MAGCHAR; |
| 437 |
*bufnext++ = M_END; |
| 438 |
break; |
| 439 |
case QUESTION: |
| 440 |
pglob->gl_flags |= GLOB_MAGCHAR; |
| 441 |
*bufnext++ = M_ONE; |
| 442 |
break; |
| 443 |
case STAR: |
| 444 |
pglob->gl_flags |= GLOB_MAGCHAR; |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if (bufnext == patbuf || bufnext[-1] != M_ALL) |
| 449 |
*bufnext++ = M_ALL; |
| 450 |
break; |
| 451 |
default: |
| 452 |
*bufnext++ = CHAR(c); |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
*bufnext = EOS; |
| 457 |
#ifdef DEBUG |
| 458 |
qprintf("glob0:", patbuf); |
| 459 |
#endif |
| 460 |
|
| 461 |
if ((err = glob1(patbuf, pglob)) != 0) |
| 462 |
return(err); |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
if (pglob->gl_pathc == oldpathc && |
| 471 |
((pglob->gl_flags & GLOB_NOCHECK) || |
| 472 |
((pglob->gl_flags & GLOB_NOMAGIC) && |
| 473 |
!(pglob->gl_flags & GLOB_MAGCHAR)))) |
| 474 |
return(globextend(pattern, pglob)); |
| 475 |
else if (!(pglob->gl_flags & GLOB_NOSORT)) |
| 476 |
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, |
| 477 |
pglob->gl_pathc - oldpathc, sizeof(char *), compare); |
| 478 |
return(0); |
| 479 |
} |
| 480 |
|
| 481 |
static int |
| 482 |
compare(const void *p, const void *q) |
| 483 |
{ |
| 484 |
return(strcmp(*(const char *const *)p, *(const char *const *)q)); |
| 485 |
} |
| 486 |
|
| 487 |
static int |
| 488 |
glob1(Char *pattern, glob_t *pglob) |
| 489 |
{ |
| 490 |
Char pathbuf[MAXPATHLEN+1]; |
| 491 |
|
| 492 |
|
| 493 |
if (*pattern == EOS) |
| 494 |
return(0); |
| 495 |
return(glob2(pathbuf, pathbuf, pattern, pglob)); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static int |
| 504 |
glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob) |
| 505 |
{ |
| 506 |
struct stat sb; |
| 507 |
Char *p, *q; |
| 508 |
int anymeta; |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
for (anymeta = 0;;) { |
| 515 |
if (*pattern == EOS) { |
| 516 |
*pathend = EOS; |
| 517 |
if (g_lstat(pathbuf, &sb, pglob)) |
| 518 |
return(0); |
| 519 |
|
| 520 |
if (((pglob->gl_flags & GLOB_MARK) && |
| 521 |
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) |
| 522 |
|| (S_ISLNK(sb.st_mode) && |
| 523 |
(g_stat(pathbuf, &sb, pglob) == 0) && |
| 524 |
S_ISDIR(sb.st_mode)))) { |
| 525 |
*pathend++ = SEP; |
| 526 |
*pathend = EOS; |
| 527 |
} |
| 528 |
++pglob->gl_matchc; |
| 529 |
return(globextend(pathbuf, pglob)); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
q = pathend; |
| 534 |
p = pattern; |
| 535 |
while (*p != EOS && *p != SEP) { |
| 536 |
if (ismeta(*p)) |
| 537 |
anymeta = 1; |
| 538 |
*q++ = *p++; |
| 539 |
} |
| 540 |
|
| 541 |
if (!anymeta) { |
| 542 |
pathend = q; |
| 543 |
pattern = p; |
| 544 |
while (*pattern == SEP) |
| 545 |
*pathend++ = *pattern++; |
| 546 |
} else |
| 547 |
return(glob3(pathbuf, pathend, pattern, p, pglob)); |
| 548 |
} |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
static struct dirent *readdir_indirect(void *argp) { |
| 553 |
return readdir(argp); |
| 554 |
} |
| 555 |
|
| 556 |
static int |
| 557 |
glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob) |
| 558 |
{ |
| 559 |
struct dirent *dp; |
| 560 |
DIR *dirp; |
| 561 |
int err; |
| 562 |
char buf[MAXPATHLEN]; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
struct dirent *(*readdirfunc)(void*); |
| 571 |
|
| 572 |
*pathend = EOS; |
| 573 |
errno = 0; |
| 574 |
|
| 575 |
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
| 576 |
|
| 577 |
if (pglob->gl_errfunc) { |
| 578 |
g_Ctoc(pathbuf, buf); |
| 579 |
if (pglob->gl_errfunc(buf, errno) || |
| 580 |
pglob->gl_flags & GLOB_ERR) |
| 581 |
return (GLOB_ABEND); |
| 582 |
} |
| 583 |
return(0); |
| 584 |
} |
| 585 |
|
| 586 |
err = 0; |
| 587 |
|
| 588 |
|
| 589 |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 590 |
readdirfunc = pglob->gl_readdir; |
| 591 |
else |
| 592 |
readdirfunc = readdir_indirect; |
| 593 |
while ((dp = (*readdirfunc)(dirp))) { |
| 594 |
u_char *sc; |
| 595 |
Char *dc; |
| 596 |
|
| 597 |
|
| 598 |
if (dp->d_name[0] == DOT && *pattern != DOT) |
| 599 |
continue; |
| 600 |
for (sc = (u_char *) dp->d_name, dc = pathend; |
| 601 |
(*dc++ = *sc++) != EOS;) |
| 602 |
continue; |
| 603 |
if (!match(pathend, pattern, restpattern)) { |
| 604 |
*pathend = EOS; |
| 605 |
continue; |
| 606 |
} |
| 607 |
err = glob2(pathbuf, --dc, restpattern, pglob); |
| 608 |
if (err) |
| 609 |
break; |
| 610 |
} |
| 611 |
|
| 612 |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 613 |
(*pglob->gl_closedir)(dirp); |
| 614 |
else |
| 615 |
closedir(dirp); |
| 616 |
return(err); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
static int |
| 635 |
globextend(const Char *path, glob_t *pglob) |
| 636 |
{ |
| 637 |
char **pathv; |
| 638 |
int i; |
| 639 |
u_int newsize; |
| 640 |
char *copy; |
| 641 |
const Char *p; |
| 642 |
|
| 643 |
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
| 644 |
pathv = pglob->gl_pathv ? |
| 645 |
realloc((char *)pglob->gl_pathv, newsize) : |
| 646 |
malloc(newsize); |
| 647 |
if (pathv == NULL) |
| 648 |
return(GLOB_NOSPACE); |
| 649 |
|
| 650 |
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
| 651 |
|
| 652 |
pathv += pglob->gl_offs; |
| 653 |
for (i = pglob->gl_offs; --i >= 0; ) |
| 654 |
*--pathv = NULL; |
| 655 |
} |
| 656 |
pglob->gl_pathv = pathv; |
| 657 |
|
| 658 |
for (p = path; *p++;) |
| 659 |
continue; |
| 660 |
if ((copy = malloc(p - path)) != NULL) { |
| 661 |
g_Ctoc(path, copy); |
| 662 |
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
| 663 |
} |
| 664 |
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
| 665 |
return(copy == NULL ? GLOB_NOSPACE : 0); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
static int |
| 673 |
match(Char *name, Char *pat, Char *patend) |
| 674 |
{ |
| 675 |
int ok, negate_range; |
| 676 |
Char c, k; |
| 677 |
|
| 678 |
while (pat < patend) { |
| 679 |
c = *pat++; |
| 680 |
switch (c & M_MASK) { |
| 681 |
case M_ALL: |
| 682 |
if (pat == patend) |
| 683 |
return(1); |
| 684 |
do |
| 685 |
if (match(name, pat, patend)) |
| 686 |
return(1); |
| 687 |
while (*name++ != EOS); |
| 688 |
return(0); |
| 689 |
case M_ONE: |
| 690 |
if (*name++ == EOS) |
| 691 |
return(0); |
| 692 |
break; |
| 693 |
case M_SET: |
| 694 |
ok = 0; |
| 695 |
if ((k = *name++) == EOS) |
| 696 |
return(0); |
| 697 |
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) |
| 698 |
++pat; |
| 699 |
while (((c = *pat++) & M_MASK) != M_END) |
| 700 |
if ((*pat & M_MASK) == M_RNG) { |
| 701 |
if (c <= k && k <= pat[1]) |
| 702 |
ok = 1; |
| 703 |
pat += 2; |
| 704 |
} else if (c == k) |
| 705 |
ok = 1; |
| 706 |
if (ok == negate_range) |
| 707 |
return(0); |
| 708 |
break; |
| 709 |
default: |
| 710 |
if (*name++ != c) |
| 711 |
return(0); |
| 712 |
break; |
| 713 |
} |
| 714 |
} |
| 715 |
return(*name == EOS); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
void |
| 720 |
globfree(glob_t *pglob) |
| 721 |
{ |
| 722 |
int i; |
| 723 |
char **pp; |
| 724 |
|
| 725 |
if (pglob->gl_pathv != NULL) { |
| 726 |
pp = pglob->gl_pathv + pglob->gl_offs; |
| 727 |
for (i = pglob->gl_pathc; i--; ++pp) |
| 728 |
if (*pp) |
| 729 |
free(*pp); |
| 730 |
free(pglob->gl_pathv); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
static DIR * |
| 735 |
g_opendir(Char *str, glob_t *pglob) |
| 736 |
{ |
| 737 |
char buf[MAXPATHLEN]; |
| 738 |
|
| 739 |
if (!*str) |
| 740 |
strcpy(buf, "."); |
| 741 |
else |
| 742 |
g_Ctoc(str, buf); |
| 743 |
|
| 744 |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 745 |
return((*pglob->gl_opendir)(buf)); |
| 746 |
|
| 747 |
return(opendir(buf)); |
| 748 |
} |
| 749 |
|
| 750 |
static int |
| 751 |
g_lstat(Char *fn, struct stat *sb, glob_t *pglob) |
| 752 |
{ |
| 753 |
char buf[MAXPATHLEN]; |
| 754 |
|
| 755 |
g_Ctoc(fn, buf); |
| 756 |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 757 |
return((*pglob->gl_lstat)(buf, sb)); |
| 758 |
return(lstat(buf, sb)); |
| 759 |
} |
| 760 |
|
| 761 |
static int |
| 762 |
g_stat(Char *fn, struct stat *sb, glob_t *pglob) |
| 763 |
{ |
| 764 |
char buf[MAXPATHLEN]; |
| 765 |
|
| 766 |
g_Ctoc(fn, buf); |
| 767 |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 768 |
return((*pglob->gl_stat)(buf, sb)); |
| 769 |
return(stat(buf, sb)); |
| 770 |
} |
| 771 |
|
| 772 |
static Char * |
| 773 |
g_strchr(Char *str, int ch) |
| 774 |
{ |
| 775 |
do { |
| 776 |
if (*str == ch) |
| 777 |
return (str); |
| 778 |
} while (*str++); |
| 779 |
return (NULL); |
| 780 |
} |
| 781 |
|
| 782 |
#ifdef notdef |
| 783 |
static Char * |
| 784 |
g_strcat(Char *dst, const Char *src) |
| 785 |
{ |
| 786 |
Char *sdst = dst; |
| 787 |
|
| 788 |
while (*dst++) |
| 789 |
continue; |
| 790 |
--dst; |
| 791 |
while((*dst++ = *src++) != EOS) |
| 792 |
continue; |
| 793 |
|
| 794 |
return (sdst); |
| 795 |
} |
| 796 |
#endif |
| 797 |
|
| 798 |
static void |
| 799 |
g_Ctoc(const Char *str, char *buf) |
| 800 |
{ |
| 801 |
char *dc; |
| 802 |
|
| 803 |
for (dc = buf; (*dc++ = *str++) != EOS;) |
| 804 |
continue; |
| 805 |
} |
| 806 |
|
| 807 |
#ifdef DEBUG |
| 808 |
static void |
| 809 |
qprintf(const char *str, Char *s) |
| 810 |
{ |
| 811 |
Char *p; |
| 812 |
|
| 813 |
(void)printf("%s:\n", str); |
| 814 |
for (p = s; *p; p++) |
| 815 |
(void)printf("%c", CHAR(*p)); |
| 816 |
(void)printf("\n"); |
| 817 |
for (p = s; *p; p++) |
| 818 |
(void)printf("%c", *p & M_PROTECT ? '"' : ' '); |
| 819 |
(void)printf("\n"); |
| 820 |
for (p = s; *p; p++) |
| 821 |
(void)printf("%c", ismeta(*p) ? '_' : ' '); |
| 822 |
(void)printf("\n"); |
| 823 |
} |
| 824 |
#endif |
| 825 |
|
| 826 |
#endif |