| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#if defined _AIX && !defined __GNUC__ |
| 21 |
#pragma alloca |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
# include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
#ifndef _GNU_SOURCE |
| 30 |
# define _GNU_SOURCE 1 |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <errno.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include <assert.h> |
| 40 |
|
| 41 |
#include <stdio.h> |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define GLOB_INTERFACE_VERSION 1 |
| 53 |
#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 |
| 54 |
# include <gnu-versions.h> |
| 55 |
# if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION |
| 56 |
# define ELIDE_CODE |
| 57 |
# endif |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifndef ELIDE_CODE |
| 61 |
|
| 62 |
#if defined STDC_HEADERS || defined __GNU_LIBRARY__ |
| 63 |
# include <stddef.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#if defined HAVE_UNISTD_H || defined _LIBC |
| 67 |
# include <unistd.h> |
| 68 |
# ifndef POSIX |
| 69 |
# ifdef _POSIX_VERSION |
| 70 |
# define POSIX |
| 71 |
# endif |
| 72 |
# endif |
| 73 |
#endif |
| 74 |
|
| 75 |
#if !defined _AMIGA && !defined VMS && !defined WINDOWS32 |
| 76 |
# include <pwd.h> |
| 77 |
#endif |
| 78 |
|
| 79 |
#if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS |
| 80 |
extern int errno; |
| 81 |
#endif |
| 82 |
#ifndef __set_errno |
| 83 |
# define __set_errno(val) errno = (val) |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifndef NULL |
| 87 |
# define NULL 0 |
| 88 |
#endif |
| 89 |
|
| 90 |
|
| 91 |
#if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__ |
| 92 |
# include <dirent.h> |
| 93 |
# define NAMLEN(dirent) strlen((dirent)->d_name) |
| 94 |
#else |
| 95 |
# define dirent direct |
| 96 |
# define NAMLEN(dirent) (dirent)->d_namlen |
| 97 |
# ifdef HAVE_SYS_NDIR_H |
| 98 |
# include <sys/ndir.h> |
| 99 |
# endif |
| 100 |
# ifdef HAVE_SYS_DIR_H |
| 101 |
# include <sys/dir.h> |
| 102 |
# endif |
| 103 |
# ifdef HAVE_NDIR_H |
| 104 |
# include <ndir.h> |
| 105 |
# endif |
| 106 |
# ifdef HAVE_VMSDIR_H |
| 107 |
# include "vmsdir.h" |
| 108 |
# endif |
| 109 |
#endif |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#ifdef _D_NAMLEN |
| 114 |
# undef NAMLEN |
| 115 |
# define NAMLEN(d) _D_NAMLEN(d) |
| 116 |
#endif |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#ifdef _DIRENT_HAVE_D_TYPE |
| 121 |
# define HAVE_D_TYPE 1 |
| 122 |
#endif |
| 123 |
|
| 124 |
|
| 125 |
#if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__ |
| 126 |
|
| 127 |
|
| 128 |
# define REAL_DIR_ENTRY(dp) 1 |
| 129 |
#else |
| 130 |
# define REAL_DIR_ENTRY(dp) (dp->d_ino != 0) |
| 131 |
#endif |
| 132 |
|
| 133 |
#if defined STDC_HEADERS || defined __GNU_LIBRARY__ |
| 134 |
# include <stdlib.h> |
| 135 |
# include <string.h> |
| 136 |
# define ANSI_STRING |
| 137 |
#else |
| 138 |
|
| 139 |
extern char *getenv (); |
| 140 |
|
| 141 |
# ifdef HAVE_STRING_H |
| 142 |
# include <string.h> |
| 143 |
# define ANSI_STRING |
| 144 |
# else |
| 145 |
# include <strings.h> |
| 146 |
# endif |
| 147 |
# ifdef HAVE_MEMORY_H |
| 148 |
# include <memory.h> |
| 149 |
# endif |
| 150 |
|
| 151 |
extern char *malloc (), *realloc (); |
| 152 |
extern void free (); |
| 153 |
|
| 154 |
extern void qsort (); |
| 155 |
extern void abort (), exit (); |
| 156 |
|
| 157 |
#endif |
| 158 |
|
| 159 |
#ifndef ANSI_STRING |
| 160 |
|
| 161 |
# ifndef bzero |
| 162 |
extern void bzero (); |
| 163 |
# endif |
| 164 |
# ifndef bcopy |
| 165 |
extern void bcopy (); |
| 166 |
# endif |
| 167 |
|
| 168 |
# define memcpy(d, s, n) bcopy ((s), (d), (n)) |
| 169 |
# define strrchr rindex |
| 170 |
|
| 171 |
# define memset(s, better_be_zero, n) \ |
| 172 |
((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0))) |
| 173 |
#endif |
| 174 |
|
| 175 |
#if !defined HAVE_STRCOLL && !defined _LIBC |
| 176 |
# define strcoll strcmp |
| 177 |
#endif |
| 178 |
|
| 179 |
#if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1 |
| 180 |
# define HAVE_MEMPCPY 1 |
| 181 |
# undef mempcpy |
| 182 |
# define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len) |
| 183 |
#endif |
| 184 |
|
| 185 |
#if !defined __GNU_LIBRARY__ && !defined __DJGPP__ |
| 186 |
# ifdef __GNUC__ |
| 187 |
__inline |
| 188 |
# endif |
| 189 |
# ifndef __SASC |
| 190 |
# ifdef WINDOWS32 |
| 191 |
static void * |
| 192 |
my_realloc (void *p, unsigned int n) |
| 193 |
# else |
| 194 |
static char * |
| 195 |
my_realloc (p, n) |
| 196 |
char *p; |
| 197 |
unsigned int n; |
| 198 |
# endif |
| 199 |
{ |
| 200 |
|
| 201 |
|
| 202 |
if (p == NULL) |
| 203 |
return (char *) malloc (n); |
| 204 |
return (char *) realloc (p, n); |
| 205 |
} |
| 206 |
# define realloc my_realloc |
| 207 |
# endif |
| 208 |
#endif |
| 209 |
|
| 210 |
|
| 211 |
#if !defined __alloca && !defined __GNU_LIBRARY__ |
| 212 |
|
| 213 |
# ifdef __GNUC__ |
| 214 |
# undef alloca |
| 215 |
# define alloca(n) __builtin_alloca (n) |
| 216 |
# else |
| 217 |
# ifdef HAVE_ALLOCA_H |
| 218 |
# include <alloca.h> |
| 219 |
# else |
| 220 |
# ifndef _AIX |
| 221 |
# ifdef WINDOWS32 |
| 222 |
# include <malloc.h> |
| 223 |
# else |
| 224 |
extern char *alloca (); |
| 225 |
# endif |
| 226 |
# endif |
| 227 |
# endif |
| 228 |
# endif |
| 229 |
|
| 230 |
# define __alloca alloca |
| 231 |
|
| 232 |
#endif |
| 233 |
|
| 234 |
#ifndef __GNU_LIBRARY__ |
| 235 |
# define __stat stat |
| 236 |
# ifdef STAT_MACROS_BROKEN |
| 237 |
# undef S_ISDIR |
| 238 |
# endif |
| 239 |
# ifndef S_ISDIR |
| 240 |
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
| 241 |
# endif |
| 242 |
#endif |
| 243 |
|
| 244 |
#ifdef _LIBC |
| 245 |
# undef strdup |
| 246 |
# define strdup(str) __strdup (str) |
| 247 |
# define sysconf(id) __sysconf (id) |
| 248 |
# define closedir(dir) __closedir (dir) |
| 249 |
# define opendir(name) __opendir (name) |
| 250 |
# define readdir(str) __readdir (str) |
| 251 |
# define getpwnam_r(name, bufp, buf, len, res) \ |
| 252 |
__getpwnam_r (name, bufp, buf, len, res) |
| 253 |
# ifndef __stat |
| 254 |
# define __stat(fname, buf) __xstat (_STAT_VER, fname, buf) |
| 255 |
# endif |
| 256 |
#endif |
| 257 |
|
| 258 |
#if !(defined STDC_HEADERS || defined __GNU_LIBRARY__) |
| 259 |
# undef size_t |
| 260 |
# define size_t unsigned int |
| 261 |
#endif |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
#ifndef __GNU_LIBRARY__ |
| 266 |
# undef FNM_PATHNAME |
| 267 |
# undef FNM_NOESCAPE |
| 268 |
# undef FNM_PERIOD |
| 269 |
#endif |
| 270 |
#include <fnmatch.h> |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
#ifndef __GNU_LIBRARY__ |
| 275 |
# undef GLOB_ERR |
| 276 |
# undef GLOB_MARK |
| 277 |
# undef GLOB_NOSORT |
| 278 |
# undef GLOB_DOOFFS |
| 279 |
# undef GLOB_NOCHECK |
| 280 |
# undef GLOB_APPEND |
| 281 |
# undef GLOB_NOESCAPE |
| 282 |
# undef GLOB_PERIOD |
| 283 |
#endif |
| 284 |
#include <glob.h> |
| 285 |
|
| 286 |
#ifdef HAVE_GETLOGIN_R |
| 287 |
extern int getlogin_r __P ((char *, size_t)); |
| 288 |
#else |
| 289 |
extern char *getlogin __P ((void)); |
| 290 |
#endif |
| 291 |
|
| 292 |
static |
| 293 |
#if __GNUC__ - 0 >= 2 |
| 294 |
inline |
| 295 |
#endif |
| 296 |
const char *next_brace_sub __P ((const char *begin)); |
| 297 |
static int glob_in_dir __P ((const char *pattern, const char *directory, |
| 298 |
int flags, |
| 299 |
int (*errfunc) (const char *, int), |
| 300 |
glob_t *pglob)); |
| 301 |
static int prefix_array __P ((const char *prefix, char **array, size_t n)); |
| 302 |
static int collated_compare __P ((const __ptr_t, const __ptr_t)); |
| 303 |
|
| 304 |
#if !defined _LIBC || !defined NO_GLOB_PATTERN_P |
| 305 |
int __glob_pattern_p __P ((const char *pattern, int quote)); |
| 306 |
#endif |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
static |
| 311 |
#if __GNUC__ - 0 >= 2 |
| 312 |
inline |
| 313 |
#endif |
| 314 |
const char * |
| 315 |
next_brace_sub (begin) |
| 316 |
const char *begin; |
| 317 |
{ |
| 318 |
unsigned int depth = 0; |
| 319 |
const char *cp = begin; |
| 320 |
|
| 321 |
while (1) |
| 322 |
{ |
| 323 |
if (depth == 0) |
| 324 |
{ |
| 325 |
if (*cp != ',' && *cp != '}' && *cp != '\0') |
| 326 |
{ |
| 327 |
if (*cp == '{') |
| 328 |
++depth; |
| 329 |
++cp; |
| 330 |
continue; |
| 331 |
} |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
while (*cp != '\0' && (*cp != '}' || depth > 0)) |
| 336 |
{ |
| 337 |
if (*cp == '}') |
| 338 |
--depth; |
| 339 |
++cp; |
| 340 |
} |
| 341 |
if (*cp == '\0') |
| 342 |
|
| 343 |
return NULL; |
| 344 |
|
| 345 |
continue; |
| 346 |
} |
| 347 |
break; |
| 348 |
} |
| 349 |
|
| 350 |
return cp; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
int |
| 362 |
glob (pattern, flags, errfunc, pglob) |
| 363 |
const char *pattern; |
| 364 |
int flags; |
| 365 |
int (*errfunc) __P ((const char *, int)); |
| 366 |
glob_t *pglob; |
| 367 |
{ |
| 368 |
const char *filename; |
| 369 |
const char *dirname; |
| 370 |
size_t dirlen; |
| 371 |
int status; |
| 372 |
int oldcount; |
| 373 |
|
| 374 |
if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) |
| 375 |
{ |
| 376 |
__set_errno (EINVAL); |
| 377 |
return -1; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if (pattern[0] && pattern[strlen (pattern) - 1] == '/') |
| 383 |
flags |= GLOB_ONLYDIR; |
| 384 |
|
| 385 |
if (flags & GLOB_BRACE) |
| 386 |
{ |
| 387 |
const char *begin = strchr (pattern, '{'); |
| 388 |
if (begin != NULL) |
| 389 |
{ |
| 390 |
|
| 391 |
|
| 392 |
int firstc; |
| 393 |
char *alt_start; |
| 394 |
const char *p; |
| 395 |
const char *next; |
| 396 |
const char *rest; |
| 397 |
size_t rest_len; |
| 398 |
#ifdef __GNUC__ |
| 399 |
char onealt[strlen (pattern) - 1]; |
| 400 |
#else |
| 401 |
char *onealt = (char *) malloc (strlen (pattern) - 1); |
| 402 |
if (onealt == NULL) |
| 403 |
{ |
| 404 |
if (!(flags & GLOB_APPEND)) |
| 405 |
globfree (pglob); |
| 406 |
return GLOB_NOSPACE; |
| 407 |
} |
| 408 |
#endif |
| 409 |
|
| 410 |
|
| 411 |
#ifdef HAVE_MEMPCPY |
| 412 |
alt_start = mempcpy (onealt, pattern, begin - pattern); |
| 413 |
#else |
| 414 |
memcpy (onealt, pattern, begin - pattern); |
| 415 |
alt_start = &onealt[begin - pattern]; |
| 416 |
#endif |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
next = next_brace_sub (begin + 1); |
| 421 |
if (next == NULL) |
| 422 |
{ |
| 423 |
|
| 424 |
#ifndef __GNUC__ |
| 425 |
free (onealt); |
| 426 |
#endif |
| 427 |
return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
rest = next; |
| 432 |
while (*rest != '}') |
| 433 |
{ |
| 434 |
rest = next_brace_sub (rest + 1); |
| 435 |
if (rest == NULL) |
| 436 |
{ |
| 437 |
|
| 438 |
#ifndef __GNUC__ |
| 439 |
free (onealt); |
| 440 |
#endif |
| 441 |
return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
rest_len = strlen (++rest) + 1; |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
if (!(flags & GLOB_APPEND)) |
| 455 |
{ |
| 456 |
|
| 457 |
|
| 458 |
pglob->gl_pathc = 0; |
| 459 |
pglob->gl_pathv = NULL; |
| 460 |
} |
| 461 |
firstc = pglob->gl_pathc; |
| 462 |
|
| 463 |
p = begin + 1; |
| 464 |
while (1) |
| 465 |
{ |
| 466 |
int result; |
| 467 |
|
| 468 |
|
| 469 |
#ifdef HAVE_MEMPCPY |
| 470 |
mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len); |
| 471 |
#else |
| 472 |
memcpy (alt_start, p, next - p); |
| 473 |
memcpy (&alt_start[next - p], rest, rest_len); |
| 474 |
#endif |
| 475 |
|
| 476 |
result = glob (onealt, |
| 477 |
((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
| 478 |
| GLOB_APPEND), errfunc, pglob); |
| 479 |
|
| 480 |
|
| 481 |
if (result && result != GLOB_NOMATCH) |
| 482 |
{ |
| 483 |
#ifndef __GNUC__ |
| 484 |
free (onealt); |
| 485 |
#endif |
| 486 |
if (!(flags & GLOB_APPEND)) |
| 487 |
globfree (pglob); |
| 488 |
return result; |
| 489 |
} |
| 490 |
|
| 491 |
if (*next == '}') |
| 492 |
|
| 493 |
break; |
| 494 |
|
| 495 |
p = next + 1; |
| 496 |
next = next_brace_sub (p); |
| 497 |
assert (next != NULL); |
| 498 |
} |
| 499 |
|
| 500 |
#ifndef __GNUC__ |
| 501 |
free (onealt); |
| 502 |
#endif |
| 503 |
|
| 504 |
if (pglob->gl_pathc != firstc) |
| 505 |
|
| 506 |
return 0; |
| 507 |
else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) |
| 508 |
return GLOB_NOMATCH; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
filename = strrchr (pattern, '/'); |
| 514 |
#if defined __MSDOS__ || defined WINDOWS32 |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
if (filename == NULL) |
| 520 |
filename = strchr (pattern, ':'); |
| 521 |
#endif |
| 522 |
if (filename == NULL) |
| 523 |
{ |
| 524 |
|
| 525 |
|
| 526 |
if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~') |
| 527 |
{ |
| 528 |
dirname = pattern; |
| 529 |
dirlen = strlen (pattern); |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
filename = NULL; |
| 535 |
} |
| 536 |
else |
| 537 |
{ |
| 538 |
filename = pattern; |
| 539 |
#ifdef _AMIGA |
| 540 |
dirname = ""; |
| 541 |
#else |
| 542 |
dirname = "."; |
| 543 |
#endif |
| 544 |
dirlen = 0; |
| 545 |
} |
| 546 |
} |
| 547 |
else if (filename == pattern) |
| 548 |
{ |
| 549 |
|
| 550 |
dirname = "/"; |
| 551 |
dirlen = 1; |
| 552 |
++filename; |
| 553 |
} |
| 554 |
else |
| 555 |
{ |
| 556 |
char *newp; |
| 557 |
dirlen = filename - pattern; |
| 558 |
#if defined __MSDOS__ || defined WINDOWS32 |
| 559 |
if (*filename == ':' |
| 560 |
|| (filename > pattern + 1 && filename[-1] == ':')) |
| 561 |
{ |
| 562 |
char *drive_spec; |
| 563 |
|
| 564 |
++dirlen; |
| 565 |
drive_spec = (char *) __alloca (dirlen + 1); |
| 566 |
#ifdef HAVE_MEMPCPY |
| 567 |
*((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0'; |
| 568 |
#else |
| 569 |
memcpy (drive_spec, pattern, dirlen); |
| 570 |
drive_spec[dirlen] = '\0'; |
| 571 |
#endif |
| 572 |
|
| 573 |
|
| 574 |
if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE))) |
| 575 |
return GLOB_NOMATCH; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
} |
| 580 |
#endif |
| 581 |
newp = (char *) __alloca (dirlen + 1); |
| 582 |
#ifdef HAVE_MEMPCPY |
| 583 |
*((char *) mempcpy (newp, pattern, dirlen)) = '\0'; |
| 584 |
#else |
| 585 |
memcpy (newp, pattern, dirlen); |
| 586 |
newp[dirlen] = '\0'; |
| 587 |
#endif |
| 588 |
dirname = newp; |
| 589 |
++filename; |
| 590 |
|
| 591 |
if (filename[0] == '\0' |
| 592 |
#if defined __MSDOS__ || defined WINDOWS32 |
| 593 |
&& dirname[dirlen - 1] != ':' |
| 594 |
&& (dirlen < 3 || dirname[dirlen - 2] != ':' |
| 595 |
|| dirname[dirlen - 1] != '/') |
| 596 |
#endif |
| 597 |
&& dirlen > 1) |
| 598 |
|
| 599 |
{ |
| 600 |
int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); |
| 601 |
if (val == 0) |
| 602 |
pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK) |
| 603 |
| (flags & GLOB_MARK)); |
| 604 |
return val; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
if (!(flags & GLOB_APPEND)) |
| 609 |
{ |
| 610 |
pglob->gl_pathc = 0; |
| 611 |
pglob->gl_pathv = NULL; |
| 612 |
} |
| 613 |
|
| 614 |
oldcount = pglob->gl_pathc; |
| 615 |
|
| 616 |
#ifndef VMS |
| 617 |
if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~') |
| 618 |
{ |
| 619 |
if (dirname[1] == '\0' || dirname[1] == '/') |
| 620 |
{ |
| 621 |
|
| 622 |
#ifdef VMS |
| 623 |
|
| 624 |
const char *home_dir = getenv ("SYS$LOGIN"); |
| 625 |
#else |
| 626 |
const char *home_dir = getenv ("HOME"); |
| 627 |
#endif |
| 628 |
# ifdef _AMIGA |
| 629 |
if (home_dir == NULL || home_dir[0] == '\0') |
| 630 |
home_dir = "SYS:"; |
| 631 |
# else |
| 632 |
# ifdef WINDOWS32 |
| 633 |
if (home_dir == NULL || home_dir[0] == '\0') |
| 634 |
home_dir = "c:/users/default"; |
| 635 |
# else |
| 636 |
# ifdef VMS |
| 637 |
|
| 638 |
if (home_dir == NULL || home_dir[0] == '\0') |
| 639 |
home_dir = "SYS$DISK:[]"; |
| 640 |
# else |
| 641 |
if (home_dir == NULL || home_dir[0] == '\0') |
| 642 |
{ |
| 643 |
int success; |
| 644 |
char *name; |
| 645 |
# if defined HAVE_GETLOGIN_R || defined _LIBC |
| 646 |
size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1; |
| 647 |
|
| 648 |
if (buflen == 0) |
| 649 |
|
| 650 |
|
| 651 |
buflen = 20; |
| 652 |
name = (char *) __alloca (buflen); |
| 653 |
|
| 654 |
success = getlogin_r (name, buflen) >= 0; |
| 655 |
# else |
| 656 |
success = (name = getlogin ()) != NULL; |
| 657 |
# endif |
| 658 |
if (success) |
| 659 |
{ |
| 660 |
struct passwd *p; |
| 661 |
# if defined HAVE_GETPWNAM_R || defined _LIBC |
| 662 |
size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX); |
| 663 |
char *pwtmpbuf; |
| 664 |
struct passwd pwbuf; |
| 665 |
int save = errno; |
| 666 |
|
| 667 |
if (pwbuflen == -1) |
| 668 |
|
| 669 |
|
| 670 |
pwbuflen = 1024; |
| 671 |
pwtmpbuf = (char *) __alloca (pwbuflen); |
| 672 |
|
| 673 |
while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p) |
| 674 |
!= 0) |
| 675 |
{ |
| 676 |
if (errno != ERANGE) |
| 677 |
{ |
| 678 |
p = NULL; |
| 679 |
break; |
| 680 |
} |
| 681 |
pwbuflen *= 2; |
| 682 |
pwtmpbuf = (char *) __alloca (pwbuflen); |
| 683 |
__set_errno (save); |
| 684 |
} |
| 685 |
# else |
| 686 |
p = getpwnam (name); |
| 687 |
# endif |
| 688 |
if (p != NULL) |
| 689 |
home_dir = p->pw_dir; |
| 690 |
} |
| 691 |
} |
| 692 |
if (home_dir == NULL || home_dir[0] == '\0') |
| 693 |
{ |
| 694 |
if (flags & GLOB_TILDE_CHECK) |
| 695 |
return GLOB_NOMATCH; |
| 696 |
else |
| 697 |
home_dir = "~"; |
| 698 |
} |
| 699 |
# endif |
| 700 |
# endif |
| 701 |
# endif |
| 702 |
|
| 703 |
if (dirname[1] == '\0') |
| 704 |
dirname = home_dir; |
| 705 |
else |
| 706 |
{ |
| 707 |
char *newp; |
| 708 |
size_t home_len = strlen (home_dir); |
| 709 |
newp = (char *) __alloca (home_len + dirlen); |
| 710 |
# ifdef HAVE_MEMPCPY |
| 711 |
mempcpy (mempcpy (newp, home_dir, home_len), |
| 712 |
&dirname[1], dirlen); |
| 713 |
# else |
| 714 |
memcpy (newp, home_dir, home_len); |
| 715 |
memcpy (&newp[home_len], &dirname[1], dirlen); |
| 716 |
# endif |
| 717 |
dirname = newp; |
| 718 |
} |
| 719 |
} |
| 720 |
# if !defined _AMIGA && !defined WINDOWS32 && !defined VMS |
| 721 |
else |
| 722 |
{ |
| 723 |
char *end_name = strchr (dirname, '/'); |
| 724 |
const char *user_name; |
| 725 |
const char *home_dir; |
| 726 |
|
| 727 |
if (end_name == NULL) |
| 728 |
user_name = dirname + 1; |
| 729 |
else |
| 730 |
{ |
| 731 |
char *newp; |
| 732 |
newp = (char *) __alloca (end_name - dirname); |
| 733 |
# ifdef HAVE_MEMPCPY |
| 734 |
*((char *) mempcpy (newp, dirname + 1, end_name - dirname)) |
| 735 |
= '\0'; |
| 736 |
# else |
| 737 |
memcpy (newp, dirname + 1, end_name - dirname); |
| 738 |
newp[end_name - dirname - 1] = '\0'; |
| 739 |
# endif |
| 740 |
user_name = newp; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
{ |
| 745 |
struct passwd *p; |
| 746 |
# if defined HAVE_GETPWNAM_R || defined _LIBC |
| 747 |
size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX); |
| 748 |
char *pwtmpbuf; |
| 749 |
struct passwd pwbuf; |
| 750 |
int save = errno; |
| 751 |
|
| 752 |
if (buflen == -1) |
| 753 |
|
| 754 |
|
| 755 |
buflen = 1024; |
| 756 |
pwtmpbuf = (char *) __alloca (buflen); |
| 757 |
|
| 758 |
while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0) |
| 759 |
{ |
| 760 |
if (errno != ERANGE) |
| 761 |
{ |
| 762 |
p = NULL; |
| 763 |
break; |
| 764 |
} |
| 765 |
buflen *= 2; |
| 766 |
pwtmpbuf = __alloca (buflen); |
| 767 |
__set_errno (save); |
| 768 |
} |
| 769 |
# else |
| 770 |
p = getpwnam (user_name); |
| 771 |
# endif |
| 772 |
if (p != NULL) |
| 773 |
home_dir = p->pw_dir; |
| 774 |
else |
| 775 |
home_dir = NULL; |
| 776 |
} |
| 777 |
|
| 778 |
if (home_dir != NULL) |
| 779 |
{ |
| 780 |
char *newp; |
| 781 |
size_t home_len = strlen (home_dir); |
| 782 |
size_t rest_len = end_name == NULL ? 0 : strlen (end_name); |
| 783 |
newp = (char *) __alloca (home_len + rest_len + 1); |
| 784 |
# ifdef HAVE_MEMPCPY |
| 785 |
*((char *) mempcpy (mempcpy (newp, home_dir, home_len), |
| 786 |
end_name, rest_len)) = '\0'; |
| 787 |
# else |
| 788 |
memcpy (newp, home_dir, home_len); |
| 789 |
memcpy (&newp[home_len], end_name, rest_len); |
| 790 |
newp[home_len + rest_len] = '\0'; |
| 791 |
# endif |
| 792 |
dirname = newp; |
| 793 |
} |
| 794 |
else |
| 795 |
if (flags & GLOB_TILDE_CHECK) |
| 796 |
|
| 797 |
|
| 798 |
return GLOB_NOMATCH; |
| 799 |
} |
| 800 |
# endif |
| 801 |
} |
| 802 |
#endif |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
if (filename == NULL) |
| 807 |
{ |
| 808 |
struct stat st; |
| 809 |
|
| 810 |
|
| 811 |
if ((flags & GLOB_NOCHECK) |
| 812 |
|| (((flags & GLOB_ALTDIRFUNC) |
| 813 |
? (*pglob->gl_stat) (dirname, &st) |
| 814 |
: __stat (dirname, &st)) == 0 |
| 815 |
&& S_ISDIR (st.st_mode))) |
| 816 |
{ |
| 817 |
pglob->gl_pathv |
| 818 |
= (char **) realloc (pglob->gl_pathv, |
| 819 |
(pglob->gl_pathc + |
| 820 |
((flags & GLOB_DOOFFS) ? |
| 821 |
pglob->gl_offs : 0) + |
| 822 |
1 + 1) * |
| 823 |
sizeof (char *)); |
| 824 |
if (pglob->gl_pathv == NULL) |
| 825 |
return GLOB_NOSPACE; |
| 826 |
|
| 827 |
if (flags & GLOB_DOOFFS) |
| 828 |
while (pglob->gl_pathc < pglob->gl_offs) |
| 829 |
pglob->gl_pathv[pglob->gl_pathc++] = NULL; |
| 830 |
|
| 831 |
#if defined HAVE_STRDUP || defined _LIBC |
| 832 |
pglob->gl_pathv[pglob->gl_pathc] = strdup (dirname); |
| 833 |
#else |
| 834 |
{ |
| 835 |
size_t len = strlen (dirname) + 1; |
| 836 |
char *dircopy = malloc (len); |
| 837 |
if (dircopy != NULL) |
| 838 |
pglob->gl_pathv[pglob->gl_pathc] = memcpy (dircopy, dirname, |
| 839 |
len); |
| 840 |
} |
| 841 |
#endif |
| 842 |
if (pglob->gl_pathv[pglob->gl_pathc] == NULL) |
| 843 |
{ |
| 844 |
free (pglob->gl_pathv); |
| 845 |
return GLOB_NOSPACE; |
| 846 |
} |
| 847 |
pglob->gl_pathv[++pglob->gl_pathc] = NULL; |
| 848 |
pglob->gl_flags = flags; |
| 849 |
|
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
return GLOB_NOMATCH; |
| 855 |
} |
| 856 |
|
| 857 |
if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE))) |
| 858 |
{ |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
glob_t dirs; |
| 863 |
register int i; |
| 864 |
|
| 865 |
status = glob (dirname, |
| 866 |
((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
| 867 |
| GLOB_NOSORT | GLOB_ONLYDIR), |
| 868 |
errfunc, &dirs); |
| 869 |
if (status != 0) |
| 870 |
return status; |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
for (i = 0; i < dirs.gl_pathc; ++i) |
| 876 |
{ |
| 877 |
int old_pathc; |
| 878 |
|
| 879 |
#ifdef SHELL |
| 880 |
{ |
| 881 |
|
| 882 |
extern int interrupt_state; |
| 883 |
|
| 884 |
if (interrupt_state) |
| 885 |
{ |
| 886 |
globfree (&dirs); |
| 887 |
globfree (&files); |
| 888 |
return GLOB_ABORTED; |
| 889 |
} |
| 890 |
} |
| 891 |
#endif |
| 892 |
|
| 893 |
old_pathc = pglob->gl_pathc; |
| 894 |
status = glob_in_dir (filename, dirs.gl_pathv[i], |
| 895 |
((flags | GLOB_APPEND) |
| 896 |
& ~(GLOB_NOCHECK | GLOB_ERR)), |
| 897 |
errfunc, pglob); |
| 898 |
if (status == GLOB_NOMATCH) |
| 899 |
|
| 900 |
continue; |
| 901 |
|
| 902 |
if (status != 0) |
| 903 |
{ |
| 904 |
globfree (&dirs); |
| 905 |
globfree (pglob); |
| 906 |
return status; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
if (prefix_array (dirs.gl_pathv[i], |
| 911 |
&pglob->gl_pathv[old_pathc], |
| 912 |
pglob->gl_pathc - old_pathc)) |
| 913 |
{ |
| 914 |
globfree (&dirs); |
| 915 |
globfree (pglob); |
| 916 |
return GLOB_NOSPACE; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
flags |= GLOB_MAGCHAR; |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
if (pglob->gl_pathc == oldcount) |
| 927 |
{ |
| 928 |
|
| 929 |
if (flags & GLOB_NOCHECK) |
| 930 |
{ |
| 931 |
size_t filename_len = strlen (filename) + 1; |
| 932 |
char **new_pathv; |
| 933 |
struct stat st; |
| 934 |
|
| 935 |
|
| 936 |
pglob->gl_pathv |
| 937 |
= (char **) realloc (pglob->gl_pathv, |
| 938 |
(pglob->gl_pathc + |
| 939 |
((flags & GLOB_DOOFFS) ? |
| 940 |
pglob->gl_offs : 0) + |
| 941 |
dirs.gl_pathc + 1) * |
| 942 |
sizeof (char *)); |
| 943 |
if (pglob->gl_pathv == NULL) |
| 944 |
{ |
| 945 |
globfree (&dirs); |
| 946 |
return GLOB_NOSPACE; |
| 947 |
} |
| 948 |
|
| 949 |
if (flags & GLOB_DOOFFS) |
| 950 |
while (pglob->gl_pathc < pglob->gl_offs) |
| 951 |
pglob->gl_pathv[pglob->gl_pathc++] = NULL; |
| 952 |
|
| 953 |
for (i = 0; i < dirs.gl_pathc; ++i) |
| 954 |
{ |
| 955 |
const char *dir = dirs.gl_pathv[i]; |
| 956 |
size_t dir_len = strlen (dir); |
| 957 |
|
| 958 |
|
| 959 |
if (((flags & GLOB_ALTDIRFUNC) |
| 960 |
? (*pglob->gl_stat) (dir, &st) : __stat (dir, &st)) != 0 |
| 961 |
|| !S_ISDIR (st.st_mode)) |
| 962 |
|
| 963 |
continue; |
| 964 |
|
| 965 |
pglob->gl_pathv[pglob->gl_pathc] = malloc (dir_len + 1 |
| 966 |
+ filename_len); |
| 967 |
if (pglob->gl_pathv[pglob->gl_pathc] == NULL) |
| 968 |
{ |
| 969 |
globfree (&dirs); |
| 970 |
globfree (pglob); |
| 971 |
return GLOB_NOSPACE; |
| 972 |
} |
| 973 |
|
| 974 |
#ifdef HAVE_MEMPCPY |
| 975 |
mempcpy (mempcpy (mempcpy (pglob->gl_pathv[pglob->gl_pathc], |
| 976 |
dir, dir_len), |
| 977 |
"/", 1), |
| 978 |
filename, filename_len); |
| 979 |
#else |
| 980 |
memcpy (pglob->gl_pathv[pglob->gl_pathc], dir, dir_len); |
| 981 |
pglob->gl_pathv[pglob->gl_pathc][dir_len] = '/'; |
| 982 |
memcpy (&pglob->gl_pathv[pglob->gl_pathc][dir_len + 1], |
| 983 |
filename, filename_len); |
| 984 |
#endif |
| 985 |
++pglob->gl_pathc; |
| 986 |
} |
| 987 |
|
| 988 |
pglob->gl_pathv[pglob->gl_pathc] = NULL; |
| 989 |
pglob->gl_flags = flags; |
| 990 |
|
| 991 |
|
| 992 |
new_pathv = (char **) realloc (pglob->gl_pathv, |
| 993 |
((pglob->gl_pathc + 1) |
| 994 |
* sizeof (char *))); |
| 995 |
if (new_pathv != NULL) |
| 996 |
pglob->gl_pathv = new_pathv; |
| 997 |
} |
| 998 |
else |
| 999 |
return GLOB_NOMATCH; |
| 1000 |
} |
| 1001 |
|
| 1002 |
globfree (&dirs); |
| 1003 |
} |
| 1004 |
else |
| 1005 |
{ |
| 1006 |
status = glob_in_dir (filename, dirname, flags, errfunc, pglob); |
| 1007 |
if (status != 0) |
| 1008 |
return status; |
| 1009 |
|
| 1010 |
if (dirlen > 0) |
| 1011 |
{ |
| 1012 |
|
| 1013 |
int ignore = oldcount; |
| 1014 |
|
| 1015 |
if ((flags & GLOB_DOOFFS) && ignore < pglob->gl_offs) |
| 1016 |
ignore = pglob->gl_offs; |
| 1017 |
|
| 1018 |
if (prefix_array (dirname, |
| 1019 |
&pglob->gl_pathv[ignore], |
| 1020 |
pglob->gl_pathc - ignore)) |
| 1021 |
{ |
| 1022 |
globfree (pglob); |
| 1023 |
return GLOB_NOSPACE; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
if (flags & GLOB_MARK) |
| 1029 |
{ |
| 1030 |
|
| 1031 |
int i; |
| 1032 |
struct stat st; |
| 1033 |
for (i = oldcount; i < pglob->gl_pathc; ++i) |
| 1034 |
if (((flags & GLOB_ALTDIRFUNC) |
| 1035 |
? (*pglob->gl_stat) (pglob->gl_pathv[i], &st) |
| 1036 |
: __stat (pglob->gl_pathv[i], &st)) == 0 |
| 1037 |
&& S_ISDIR (st.st_mode)) |
| 1038 |
{ |
| 1039 |
size_t len = strlen (pglob->gl_pathv[i]) + 2; |
| 1040 |
char *new = realloc (pglob->gl_pathv[i], len); |
| 1041 |
if (new == NULL) |
| 1042 |
{ |
| 1043 |
globfree (pglob); |
| 1044 |
return GLOB_NOSPACE; |
| 1045 |
} |
| 1046 |
strcpy (&new[len - 2], "/"); |
| 1047 |
pglob->gl_pathv[i] = new; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (!(flags & GLOB_NOSORT)) |
| 1052 |
{ |
| 1053 |
|
| 1054 |
int non_sort = oldcount; |
| 1055 |
|
| 1056 |
if ((flags & GLOB_DOOFFS) && pglob->gl_offs > oldcount) |
| 1057 |
non_sort = pglob->gl_offs; |
| 1058 |
|
| 1059 |
qsort ((__ptr_t) &pglob->gl_pathv[non_sort], |
| 1060 |
pglob->gl_pathc - non_sort, |
| 1061 |
sizeof (char *), collated_compare); |
| 1062 |
} |
| 1063 |
|
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
void |
| 1070 |
globfree (pglob) |
| 1071 |
register glob_t *pglob; |
| 1072 |
{ |
| 1073 |
if (pglob->gl_pathv != NULL) |
| 1074 |
{ |
| 1075 |
register int i; |
| 1076 |
for (i = 0; i < pglob->gl_pathc; ++i) |
| 1077 |
if (pglob->gl_pathv[i] != NULL) |
| 1078 |
free ((__ptr_t) pglob->gl_pathv[i]); |
| 1079 |
free ((__ptr_t) pglob->gl_pathv); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
static int |
| 1086 |
collated_compare (a, b) |
| 1087 |
const __ptr_t a; |
| 1088 |
const __ptr_t b; |
| 1089 |
{ |
| 1090 |
const char *const s1 = *(const char *const * const) a; |
| 1091 |
const char *const s2 = *(const char *const * const) b; |
| 1092 |
|
| 1093 |
if (s1 == s2) |
| 1094 |
return 0; |
| 1095 |
if (s1 == NULL) |
| 1096 |
return 1; |
| 1097 |
if (s2 == NULL) |
| 1098 |
return -1; |
| 1099 |
return strcoll (s1, s2); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
static int |
| 1108 |
prefix_array (dirname, array, n) |
| 1109 |
const char *dirname; |
| 1110 |
char **array; |
| 1111 |
size_t n; |
| 1112 |
{ |
| 1113 |
register size_t i; |
| 1114 |
size_t dirlen = strlen (dirname); |
| 1115 |
#if defined __MSDOS__ || defined WINDOWS32 |
| 1116 |
int sep_char = '/'; |
| 1117 |
# define DIRSEP_CHAR sep_char |
| 1118 |
#else |
| 1119 |
# define DIRSEP_CHAR '/' |
| 1120 |
#endif |
| 1121 |
|
| 1122 |
if (dirlen == 1 && dirname[0] == '/') |
| 1123 |
|
| 1124 |
|
| 1125 |
dirlen = 0; |
| 1126 |
#if defined __MSDOS__ || defined WINDOWS32 |
| 1127 |
else if (dirlen > 1) |
| 1128 |
{ |
| 1129 |
if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':') |
| 1130 |
|
| 1131 |
--dirlen; |
| 1132 |
else if (dirname[dirlen - 1] == ':') |
| 1133 |
{ |
| 1134 |
|
| 1135 |
--dirlen; |
| 1136 |
sep_char = ':'; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
#endif |
| 1140 |
|
| 1141 |
for (i = 0; i < n; ++i) |
| 1142 |
{ |
| 1143 |
size_t eltlen = strlen (array[i]) + 1; |
| 1144 |
char *new = (char *) malloc (dirlen + 1 + eltlen); |
| 1145 |
if (new == NULL) |
| 1146 |
{ |
| 1147 |
while (i > 0) |
| 1148 |
free ((__ptr_t) array[--i]); |
| 1149 |
return 1; |
| 1150 |
} |
| 1151 |
|
| 1152 |
#ifdef HAVE_MEMPCPY |
| 1153 |
{ |
| 1154 |
char *endp = (char *) mempcpy (new, dirname, dirlen); |
| 1155 |
*endp++ = DIRSEP_CHAR; |
| 1156 |
mempcpy (endp, array[i], eltlen); |
| 1157 |
} |
| 1158 |
#else |
| 1159 |
memcpy (new, dirname, dirlen); |
| 1160 |
new[dirlen] = DIRSEP_CHAR; |
| 1161 |
memcpy (&new[dirlen + 1], array[i], eltlen); |
| 1162 |
#endif |
| 1163 |
free ((__ptr_t) array[i]); |
| 1164 |
array[i] = new; |
| 1165 |
} |
| 1166 |
|
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
#if !defined _LIBC || !defined NO_GLOB_PATTERN_P |
| 1173 |
|
| 1174 |
|
| 1175 |
int |
| 1176 |
__glob_pattern_p (pattern, quote) |
| 1177 |
const char *pattern; |
| 1178 |
int quote; |
| 1179 |
{ |
| 1180 |
register const char *p; |
| 1181 |
int open = 0; |
| 1182 |
|
| 1183 |
for (p = pattern; *p != '\0'; ++p) |
| 1184 |
switch (*p) |
| 1185 |
{ |
| 1186 |
case '?': |
| 1187 |
case '*': |
| 1188 |
return 1; |
| 1189 |
|
| 1190 |
case '\\': |
| 1191 |
if (quote && p[1] != '\0') |
| 1192 |
++p; |
| 1193 |
break; |
| 1194 |
|
| 1195 |
case '[': |
| 1196 |
open = 1; |
| 1197 |
break; |
| 1198 |
|
| 1199 |
case ']': |
| 1200 |
if (open) |
| 1201 |
return 1; |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
|
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
# ifdef _LIBC |
| 1208 |
weak_alias (__glob_pattern_p, glob_pattern_p) |
| 1209 |
# endif |
| 1210 |
#endif |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
static int |
| 1218 |
glob_in_dir (pattern, directory, flags, errfunc, pglob) |
| 1219 |
const char *pattern; |
| 1220 |
const char *directory; |
| 1221 |
int flags; |
| 1222 |
int (*errfunc) __P ((const char *, int)); |
| 1223 |
glob_t *pglob; |
| 1224 |
{ |
| 1225 |
__ptr_t stream = NULL; |
| 1226 |
|
| 1227 |
struct globlink |
| 1228 |
{ |
| 1229 |
struct globlink *next; |
| 1230 |
char *name; |
| 1231 |
}; |
| 1232 |
struct globlink *names = NULL; |
| 1233 |
size_t nfound; |
| 1234 |
int meta; |
| 1235 |
int save; |
| 1236 |
|
| 1237 |
#ifdef VMS |
| 1238 |
if (*directory == 0) |
| 1239 |
directory = "[]"; |
| 1240 |
#endif |
| 1241 |
meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)); |
| 1242 |
if (meta == 0) |
| 1243 |
{ |
| 1244 |
if (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)) |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
flags |= GLOB_NOCHECK; |
| 1249 |
else |
| 1250 |
{ |
| 1251 |
|
| 1252 |
|
| 1253 |
struct stat st; |
| 1254 |
size_t patlen = strlen (pattern); |
| 1255 |
size_t dirlen = strlen (directory); |
| 1256 |
char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1); |
| 1257 |
|
| 1258 |
# ifdef HAVE_MEMPCPY |
| 1259 |
mempcpy (mempcpy (mempcpy (fullname, directory, dirlen), |
| 1260 |
"/", 1), |
| 1261 |
pattern, patlen + 1); |
| 1262 |
# else |
| 1263 |
memcpy (fullname, directory, dirlen); |
| 1264 |
fullname[dirlen] = '/'; |
| 1265 |
memcpy (&fullname[dirlen + 1], pattern, patlen + 1); |
| 1266 |
# endif |
| 1267 |
if (((flags & GLOB_ALTDIRFUNC) |
| 1268 |
? (*pglob->gl_stat) (fullname, &st) |
| 1269 |
: __stat (fullname, &st)) == 0) |
| 1270 |
|
| 1271 |
|
| 1272 |
flags |= GLOB_NOCHECK; |
| 1273 |
} |
| 1274 |
|
| 1275 |
nfound = 0; |
| 1276 |
} |
| 1277 |
else |
| 1278 |
{ |
| 1279 |
if (pattern[0] == '\0') |
| 1280 |
{ |
| 1281 |
|
| 1282 |
|
| 1283 |
names = (struct globlink *) __alloca (sizeof (struct globlink)); |
| 1284 |
names->name = (char *) malloc (1); |
| 1285 |
if (names->name == NULL) |
| 1286 |
goto memory_error; |
| 1287 |
names->name[0] = '\0'; |
| 1288 |
names->next = NULL; |
| 1289 |
nfound = 1; |
| 1290 |
meta = 0; |
| 1291 |
} |
| 1292 |
else |
| 1293 |
{ |
| 1294 |
stream = ((flags & GLOB_ALTDIRFUNC) |
| 1295 |
? (*pglob->gl_opendir) (directory) |
| 1296 |
: (__ptr_t) opendir (directory)); |
| 1297 |
if (stream == NULL) |
| 1298 |
{ |
| 1299 |
if (errno != ENOTDIR |
| 1300 |
&& ((errfunc != NULL && (*errfunc) (directory, errno)) |
| 1301 |
|| (flags & GLOB_ERR))) |
| 1302 |
return GLOB_ABORTED; |
| 1303 |
nfound = 0; |
| 1304 |
meta = 0; |
| 1305 |
} |
| 1306 |
else |
| 1307 |
{ |
| 1308 |
int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
| 1309 |
| ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) |
| 1310 |
#if defined HAVE_CASE_INSENSITIVE_FS |
| 1311 |
| FNM_CASEFOLD |
| 1312 |
#endif |
| 1313 |
); |
| 1314 |
nfound = 0; |
| 1315 |
flags |= GLOB_MAGCHAR; |
| 1316 |
|
| 1317 |
while (1) |
| 1318 |
{ |
| 1319 |
const char *name; |
| 1320 |
size_t len; |
| 1321 |
struct dirent *d = ((flags & GLOB_ALTDIRFUNC) |
| 1322 |
? (*pglob->gl_readdir) (stream) |
| 1323 |
: readdir ((DIR *) stream)); |
| 1324 |
if (d == NULL) |
| 1325 |
break; |
| 1326 |
if (! REAL_DIR_ENTRY (d)) |
| 1327 |
continue; |
| 1328 |
|
| 1329 |
#ifdef HAVE_D_TYPE |
| 1330 |
|
| 1331 |
|
| 1332 |
if ((flags & GLOB_ONLYDIR) |
| 1333 |
&& d->d_type != DT_UNKNOWN && d->d_type != DT_DIR) |
| 1334 |
continue; |
| 1335 |
#endif |
| 1336 |
|
| 1337 |
name = d->d_name; |
| 1338 |
|
| 1339 |
if (fnmatch (pattern, name, fnm_flags) == 0) |
| 1340 |
{ |
| 1341 |
struct globlink *new = (struct globlink *) |
| 1342 |
__alloca (sizeof (struct globlink)); |
| 1343 |
len = NAMLEN (d); |
| 1344 |
new->name = (char *) malloc (len + 1); |
| 1345 |
if (new->name == NULL) |
| 1346 |
goto memory_error; |
| 1347 |
#ifdef HAVE_MEMPCPY |
| 1348 |
*((char *) mempcpy ((__ptr_t) new->name, name, len)) |
| 1349 |
= '\0'; |
| 1350 |
#else |
| 1351 |
memcpy ((__ptr_t) new->name, name, len); |
| 1352 |
new->name[len] = '\0'; |
| 1353 |
#endif |
| 1354 |
new->next = names; |
| 1355 |
names = new; |
| 1356 |
++nfound; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
if (nfound == 0 && (flags & GLOB_NOCHECK)) |
| 1364 |
{ |
| 1365 |
size_t len = strlen (pattern); |
| 1366 |
nfound = 1; |
| 1367 |
names = (struct globlink *) __alloca (sizeof (struct globlink)); |
| 1368 |
names->next = NULL; |
| 1369 |
names->name = (char *) malloc (len + 1); |
| 1370 |
if (names->name == NULL) |
| 1371 |
goto memory_error; |
| 1372 |
#ifdef HAVE_MEMPCPY |
| 1373 |
*((char *) mempcpy (names->name, pattern, len)) = '\0'; |
| 1374 |
#else |
| 1375 |
memcpy (names->name, pattern, len); |
| 1376 |
names->name[len] = '\0'; |
| 1377 |
#endif |
| 1378 |
} |
| 1379 |
|
| 1380 |
if (nfound != 0) |
| 1381 |
{ |
| 1382 |
pglob->gl_pathv |
| 1383 |
= (char **) realloc (pglob->gl_pathv, |
| 1384 |
(pglob->gl_pathc + |
| 1385 |
((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) + |
| 1386 |
nfound + 1) * |
| 1387 |
sizeof (char *)); |
| 1388 |
if (pglob->gl_pathv == NULL) |
| 1389 |
goto memory_error; |
| 1390 |
|
| 1391 |
if (flags & GLOB_DOOFFS) |
| 1392 |
while (pglob->gl_pathc < pglob->gl_offs) |
| 1393 |
pglob->gl_pathv[pglob->gl_pathc++] = NULL; |
| 1394 |
|
| 1395 |
for (; names != NULL; names = names->next) |
| 1396 |
pglob->gl_pathv[pglob->gl_pathc++] = names->name; |
| 1397 |
pglob->gl_pathv[pglob->gl_pathc] = NULL; |
| 1398 |
|
| 1399 |
pglob->gl_flags = flags; |
| 1400 |
} |
| 1401 |
|
| 1402 |
save = errno; |
| 1403 |
if (stream != NULL) |
| 1404 |
{ |
| 1405 |
if (flags & GLOB_ALTDIRFUNC) |
| 1406 |
(*pglob->gl_closedir) (stream); |
| 1407 |
else |
| 1408 |
closedir ((DIR *) stream); |
| 1409 |
} |
| 1410 |
__set_errno (save); |
| 1411 |
|
| 1412 |
return nfound == 0 ? GLOB_NOMATCH : 0; |
| 1413 |
|
| 1414 |
memory_error: |
| 1415 |
{ |
| 1416 |
int save = errno; |
| 1417 |
if (flags & GLOB_ALTDIRFUNC) |
| 1418 |
(*pglob->gl_closedir) (stream); |
| 1419 |
else |
| 1420 |
closedir ((DIR *) stream); |
| 1421 |
__set_errno (save); |
| 1422 |
} |
| 1423 |
while (names != NULL) |
| 1424 |
{ |
| 1425 |
if (names->name != NULL) |
| 1426 |
free ((__ptr_t) names->name); |
| 1427 |
names = names->next; |
| 1428 |
} |
| 1429 |
return GLOB_NOSPACE; |
| 1430 |
} |
| 1431 |
|
| 1432 |
#endif |