1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef _LIBC
18 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
19 optimizes away the pattern == NULL || pglob == NULL tests below. */
20 # define _GL_ARG_NONNULL(params)
21 # include <config.h>
22 #endif
23
24 #include <glob.h>
25
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stddef.h>
30
31 /* Outcomment the following line for production quality code. */
32 /* #define NDEBUG 1 */
33 #include <assert.h>
34
35 #include <stdbool.h>
36
37 #include <stdio.h> /* Needed on stupid SunOS for assert. */
38
39 #if !defined _LIBC || !defined GLOB_ONLY_P
40
41 #include <unistd.h>
42 #if !defined POSIX && defined _POSIX_VERSION
43 # define POSIX
44 #endif
45
46 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
47 # define WINDOWS32
48 #endif
49
50 #ifndef WINDOWS32
51 # include <pwd.h>
52 #endif
53
54 #include <errno.h>
55 #ifndef __set_errno
56 # define __set_errno(val) errno = (val)
57 #endif
58
59 #include <dirent.h>
60
61
62 /* In GNU systems, <dirent.h> defines this macro for us. */
63 #ifndef _D_EXACT_NAMLEN
64 # define _D_EXACT_NAMLEN(dirent) strlen ((dirent)->d_name)
65 #endif
66
67 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
68 if the 'd_type' member for 'struct dirent' is available.
69 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
70 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
71 /* True if the directory entry D must be of type T. */
72 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
73
74 /* True if the directory entry D might be a symbolic link. */
75 # define DIRENT_MIGHT_BE_SYMLINK(d) \
76 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
77
78 /* True if the directory entry D might be a directory. */
79 # define DIRENT_MIGHT_BE_DIR(d) \
80 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
81
82 #else /* !HAVE_D_TYPE */
83 # define DIRENT_MUST_BE(d, t) false
84 # define DIRENT_MIGHT_BE_SYMLINK(d) true
85 # define DIRENT_MIGHT_BE_DIR(d) true
86 #endif /* HAVE_D_TYPE */
87
88 /* If the system has the 'struct dirent64' type we use it internally. */
89 #if defined _LIBC && !defined COMPILE_GLOB64
90 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
91 # define CONVERT_D_INO(d64, d32)
92 # else
93 # define CONVERT_D_INO(d64, d32) \
94 (d64)->d_ino = (d32)->d_ino;
95 # endif
96
97 # ifdef _DIRENT_HAVE_D_TYPE
98 # define CONVERT_D_TYPE(d64, d32) \
99 (d64)->d_type = (d32)->d_type;
100 # else
101 # define CONVERT_D_TYPE(d64, d32)
102 # endif
103
104 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
105 memcpy ((d64)->d_name, (d32)->d_name, _D_EXACT_NAMLEN (d32) + 1); \
106 CONVERT_D_INO (d64, d32) \
107 CONVERT_D_TYPE (d64, d32)
108 #endif
109
110
111 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
112 /* Posix does not require that the d_ino field be present, and some
113 systems do not provide it. */
114 # define REAL_DIR_ENTRY(dp) 1
115 #else
116 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
117 #endif /* POSIX */
118
119 #include <stdlib.h>
120 #include <string.h>
121
122 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
123 #include <limits.h>
124 #ifndef NAME_MAX
125 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
126 #endif
127
128 #include <alloca.h>
129
130 #ifdef _LIBC
131 # undef strdup
132 # define strdup(str) __strdup (str)
133 # define sysconf(id) __sysconf (id)
134 # define closedir(dir) __closedir (dir)
135 # define opendir(name) __opendir (name)
136 # define readdir(str) __readdir64 (str)
137 # define getpwnam_r(name, bufp, buf, len, res) \
138 __getpwnam_r (name, bufp, buf, len, res)
139 # ifndef __stat64
140 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
141 # endif
142 # define struct_stat64 struct stat64
143 #else /* !_LIBC */
144 # define __stat64(fname, buf) stat (fname, buf)
145 # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
146 # define struct_stat64 struct stat
147 # define __alloca alloca
148 # define __readdir readdir
149 # define __glob_pattern_p glob_pattern_p
150 #endif /* _LIBC */
151
152 #include <fnmatch.h>
153
154 #ifdef _SC_GETPW_R_SIZE_MAX
155 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
156 #else
157 # define GETPW_R_SIZE_MAX() (-1)
158 #endif
159 #ifdef _SC_LOGIN_NAME_MAX
160 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
161 #else
162 # define GET_LOGIN_NAME_MAX() (-1)
163 #endif
164
165 static const char *next_brace_sub (const char *begin, int flags) __THROWNL;
166
167 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
168
169 #ifndef attribute_hidden
170 # define attribute_hidden
171 #endif
172
173 #ifndef __attribute_noinline__
174 # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1)
175 # define __attribute_noinline__ /* Ignore */
176 #else
177 # define __attribute_noinline__ __attribute__ ((__noinline__))
178 # endif
179 #endif
180
181 #if ! defined __builtin_expect && __GNUC__ < 3
182 # define __builtin_expect(expr, expected) (expr)
183 #endif
184
185 #ifndef _LIBC
186 /* The results of opendir() in this file are not used with dirfd and fchdir,
187 and we do not leak fds to any single-threaded code that could use stdio,
188 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
189 FIXME - if the kernel ever adds support for multi-thread safety for
190 avoiding standard fds, then we should use opendir_safer. */
191 # undef opendir
192 # undef closedir
193
194 # if HAVE_ALLOCA
195 /* The OS usually guarantees only one guard page at the bottom of the stack,
196 and a page size can be as small as 4096 bytes. So we cannot safely
197 allocate anything larger than 4096 bytes. Also care for the possibility
198 of a few compiler-allocated temporary stack slots. */
199 # define __libc_use_alloca(n) ((n) < 4032)
200 # else
201 /* alloca is implemented with malloc, so just use malloc. */
202 # define __libc_use_alloca(n) 0
203 # endif
204 #endif
205
206 static int glob_in_dir (const char *pattern, const char *directory,
207 int flags, int (*errfunc) (const char *, int),
208 glob_t *pglob);
209 extern int __glob_pattern_type (const char *pattern, int quote)
210 attribute_hidden;
211
212 #if !defined _LIBC || !defined GLOB_ONLY_P
213 static int prefix_array (const char *prefix, char **array, size_t n) __THROWNL;
214 static int collated_compare (const void *, const void *) __THROWNL;
215
216
217 /* Find the end of the sub-pattern in a brace expression. */
218 static const char *
next_brace_sub(const char * cp,int flags)219 next_brace_sub (const char *cp, int flags)
220 {
221 unsigned int depth = 0;
222 while (*cp != '\0')
223 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
224 {
225 if (*++cp == '\0')
226 break;
227 ++cp;
228 }
229 else
230 {
231 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
232 break;
233
234 if (*cp++ == '{')
235 depth++;
236 }
237
238 return *cp != '\0' ? cp : NULL;
239 }
240
241 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
242
243 /* Do glob searching for PATTERN, placing results in PGLOB.
244 The bits defined above may be set in FLAGS.
245 If a directory cannot be opened or read and ERRFUNC is not nil,
246 it is called with the pathname that caused the error, and the
247 'errno' value from the failing call; if it returns non-zero
248 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
249 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
250 Otherwise, 'glob' returns zero. */
251 int
252 #ifdef GLOB_ATTRIBUTE
253 GLOB_ATTRIBUTE
254 #endif
glob(pattern,flags,errfunc,pglob)255 glob (pattern, flags, errfunc, pglob)
256 const char * restrict pattern;
257 int flags;
258 int (*errfunc) (const char *, int);
259 glob_t * restrict pglob;
260 {
261 const char *filename;
262 const char *dirname;
263 size_t dirlen;
264 int status;
265 size_t oldcount;
266 int meta;
267 int dirname_modified;
268 glob_t dirs;
269
270 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
271 {
272 __set_errno (EINVAL);
273 return -1;
274 }
275
276 if (!(flags & GLOB_DOOFFS))
277 /* Have to do this so 'globfree' knows where to start freeing. It
278 also makes all the code that uses gl_offs simpler. */
279 pglob->gl_offs = 0;
280
281 if (flags & GLOB_BRACE)
282 {
283 const char *begin;
284
285 if (flags & GLOB_NOESCAPE)
286 begin = strchr (pattern, '{');
287 else
288 {
289 begin = pattern;
290 while (1)
291 {
292 if (*begin == '\0')
293 {
294 begin = NULL;
295 break;
296 }
297
298 if (*begin == '\\' && begin[1] != '\0')
299 ++begin;
300 else if (*begin == '{')
301 break;
302
303 ++begin;
304 }
305 }
306
307 if (begin != NULL)
308 {
309 /* Allocate working buffer large enough for our work. Note that
310 we have at least an opening and closing brace. */
311 size_t firstc;
312 char *alt_start;
313 const char *p;
314 const char *next;
315 const char *rest;
316 size_t rest_len;
317 #ifdef __GNUC__
318 char onealt[strlen (pattern) - 1];
319 #else
320 char *onealt = malloc (strlen (pattern) - 1);
321 if (onealt == NULL)
322 {
323 if (!(flags & GLOB_APPEND))
324 {
325 pglob->gl_pathc = 0;
326 pglob->gl_pathv = NULL;
327 }
328 return GLOB_NOSPACE;
329 }
330 #endif
331
332 /* We know the prefix for all sub-patterns. */
333 alt_start = mempcpy (onealt, pattern, begin - pattern);
334
335 /* Find the first sub-pattern and at the same time find the
336 rest after the closing brace. */
337 next = next_brace_sub (begin + 1, flags);
338 if (next == NULL)
339 {
340 /* It is an invalid expression. */
341 #ifndef __GNUC__
342 free (onealt);
343 #endif
344 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
345 }
346
347 /* Now find the end of the whole brace expression. */
348 rest = next;
349 while (*rest != '}')
350 {
351 rest = next_brace_sub (rest + 1, flags);
352 if (rest == NULL)
353 {
354 /* It is an invalid expression. */
355 #ifndef __GNUC__
356 free (onealt);
357 #endif
358 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
359 }
360 }
361 /* Please note that we now can be sure the brace expression
362 is well-formed. */
363 rest_len = strlen (++rest) + 1;
364
365 /* We have a brace expression. BEGIN points to the opening {,
366 NEXT points past the terminator of the first element, and END
367 points past the final }. We will accumulate result names from
368 recursive runs for each brace alternative in the buffer using
369 GLOB_APPEND. */
370
371 if (!(flags & GLOB_APPEND))
372 {
373 /* This call is to set a new vector, so clear out the
374 vector so we can append to it. */
375 pglob->gl_pathc = 0;
376 pglob->gl_pathv = NULL;
377 }
378 firstc = pglob->gl_pathc;
379
380 p = begin + 1;
381 while (1)
382 {
383 int result;
384
385 /* Construct the new glob expression. */
386 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
387
388 result = glob (onealt,
389 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
390 | GLOB_APPEND), errfunc, pglob);
391
392 /* If we got an error, return it. */
393 if (result && result != GLOB_NOMATCH)
394 {
395 #ifndef __GNUC__
396 free (onealt);
397 #endif
398 if (!(flags & GLOB_APPEND))
399 {
400 globfree (pglob);
401 pglob->gl_pathc = 0;
402 }
403 return result;
404 }
405
406 if (*next == '}')
407 /* We saw the last entry. */
408 break;
409
410 p = next + 1;
411 next = next_brace_sub (p, flags);
412 assert (next != NULL);
413 }
414
415 #ifndef __GNUC__
416 free (onealt);
417 #endif
418
419 if (pglob->gl_pathc != firstc)
420 /* We found some entries. */
421 return 0;
422 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
423 return GLOB_NOMATCH;
424 }
425 }
426
427 /* Find the filename. */
428 filename = strrchr (pattern, '/');
429 #if defined __MSDOS__ || defined WINDOWS32
430 /* The case of "d:pattern". Since ':' is not allowed in
431 file names, we can safely assume that wherever it
432 happens in pattern, it signals the filename part. This
433 is so we could some day support patterns like "[a-z]:foo". */
434 if (filename == NULL)
435 filename = strchr (pattern, ':');
436 #endif /* __MSDOS__ || WINDOWS32 */
437 dirname_modified = 0;
438 if (filename == NULL)
439 {
440 /* This can mean two things: a simple name or "~name". The latter
441 case is nothing but a notation for a directory. */
442 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
443 {
444 dirname = pattern;
445 dirlen = strlen (pattern);
446
447 /* Set FILENAME to NULL as a special flag. This is ugly but
448 other solutions would require much more code. We test for
449 this special case below. */
450 filename = NULL;
451 }
452 else
453 {
454 filename = pattern;
455 #ifdef _AMIGA
456 dirname = "";
457 #else
458 dirname = ".";
459 #endif
460 dirlen = 0;
461 }
462 }
463 else if (filename == pattern
464 || (filename == pattern + 1 && pattern[0] == '\\'
465 && (flags & GLOB_NOESCAPE) == 0))
466 {
467 /* "/pattern" or "\\/pattern". */
468 dirname = "/";
469 dirlen = 1;
470 ++filename;
471 }
472 else
473 {
474 char *newp;
475 dirlen = filename - pattern;
476 #if defined __MSDOS__ || defined WINDOWS32
477 if (*filename == ':'
478 || (filename > pattern + 1 && filename[-1] == ':'))
479 {
480 char *drive_spec;
481
482 ++dirlen;
483 drive_spec = __alloca (dirlen + 1);
484 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
485 /* For now, disallow wildcards in the drive spec, to
486 prevent infinite recursion in glob. */
487 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
488 return GLOB_NOMATCH;
489 /* If this is "d:pattern", we need to copy ':' to DIRNAME
490 as well. If it's "d:/pattern", don't remove the slash
491 from "d:/", since "d:" and "d:/" are not the same.*/
492 }
493 #endif
494 newp = __alloca (dirlen + 1);
495 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
496 dirname = newp;
497 ++filename;
498
499 if (filename[0] == '\0'
500 #if defined __MSDOS__ || defined WINDOWS32
501 && dirname[dirlen - 1] != ':'
502 && (dirlen < 3 || dirname[dirlen - 2] != ':'
503 || dirname[dirlen - 1] != '/')
504 #endif
505 && dirlen > 1)
506 /* "pattern/". Expand "pattern", appending slashes. */
507 {
508 int orig_flags = flags;
509 int val;
510 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
511 {
512 /* "pattern\\/". Remove the final backslash if it hasn't
513 been quoted. */
514 char *p = (char *) &dirname[dirlen - 1];
515
516 while (p > dirname && p[-1] == '\\') --p;
517 if ((&dirname[dirlen] - p) & 1)
518 {
519 *(char *) &dirname[--dirlen] = '\0';
520 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
521 }
522 }
523 val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
524 if (val == 0)
525 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
526 | (flags & GLOB_MARK));
527 else if (val == GLOB_NOMATCH && flags != orig_flags)
528 {
529 /* Make sure globfree (&dirs); is a nop. */
530 dirs.gl_pathv = NULL;
531 flags = orig_flags;
532 oldcount = pglob->gl_pathc + pglob->gl_offs;
533 goto no_matches;
534 }
535 return val;
536 }
537 }
538
539 if (!(flags & GLOB_APPEND))
540 {
541 pglob->gl_pathc = 0;
542 if (!(flags & GLOB_DOOFFS))
543 pglob->gl_pathv = NULL;
544 else
545 {
546 size_t i;
547 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
548 if (pglob->gl_pathv == NULL)
549 return GLOB_NOSPACE;
550
551 for (i = 0; i <= pglob->gl_offs; ++i)
552 pglob->gl_pathv[i] = NULL;
553 }
554 }
555
556 oldcount = pglob->gl_pathc + pglob->gl_offs;
557
558 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
559 {
560 if (dirname[1] == '\0' || dirname[1] == '/'
561 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
562 && (dirname[2] == '\0' || dirname[2] == '/')))
563 {
564 /* Look up home directory. */
565 const char *home_dir = getenv ("HOME");
566 # ifdef _AMIGA
567 if (home_dir == NULL || home_dir[0] == '\0')
568 home_dir = "SYS:";
569 # else
570 # ifdef WINDOWS32
571 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference
572 to HOME, because the user can change HOME. */
573 if (home_dir == NULL || home_dir[0] == '\0')
574 {
575 const char *home_drive = getenv ("HOMEDRIVE");
576 const char *home_path = getenv ("HOMEPATH");
577
578 if (home_drive != NULL && home_path != NULL)
579 {
580 size_t home_drive_len = strlen (home_drive);
581 size_t home_path_len = strlen (home_path);
582 char *mem = alloca (home_drive_len + home_path_len + 1);
583
584 memcpy (mem, home_drive, home_drive_len);
585 memcpy (mem + home_drive_len, home_path, home_path_len + 1);
586 home_dir = mem;
587 }
588 else
589 home_dir = "c:/users/default"; /* poor default */
590 }
591 # else
592 if (home_dir == NULL || home_dir[0] == '\0')
593 {
594 int success;
595 char *name;
596 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
597
598 if (buflen == 0)
599 /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try
600 a moderate value. */
601 buflen = 20;
602 name = __alloca (buflen);
603
604 success = getlogin_r (name, buflen) == 0;
605 if (success)
606 {
607 struct passwd *p;
608 # if defined HAVE_GETPWNAM_R || defined _LIBC
609 long int pwbuflen = GETPW_R_SIZE_MAX ();
610 char *pwtmpbuf;
611 struct passwd pwbuf;
612 int save = errno;
613
614 # ifndef _LIBC
615 if (pwbuflen == -1)
616 /* 'sysconf' does not support _SC_GETPW_R_SIZE_MAX.
617 Try a moderate value. */
618 pwbuflen = 1024;
619 # endif
620 pwtmpbuf = __alloca (pwbuflen);
621
622 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
623 != 0)
624 {
625 if (errno != ERANGE)
626 {
627 p = NULL;
628 break;
629 }
630 # ifdef _LIBC
631 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
632 2 * pwbuflen);
633 # else
634 pwbuflen *= 2;
635 pwtmpbuf = __alloca (pwbuflen);
636 # endif
637 __set_errno (save);
638 }
639 # else
640 p = getpwnam (name);
641 # endif
642 if (p != NULL)
643 home_dir = p->pw_dir;
644 }
645 }
646 if (home_dir == NULL || home_dir[0] == '\0')
647 {
648 if (flags & GLOB_TILDE_CHECK)
649 return GLOB_NOMATCH;
650 else
651 home_dir = "~"; /* No luck. */
652 }
653 # endif /* WINDOWS32 */
654 # endif
655 /* Now construct the full directory. */
656 if (dirname[1] == '\0')
657 {
658 dirname = home_dir;
659 dirlen = strlen (dirname);
660 }
661 else
662 {
663 char *newp;
664 size_t home_len = strlen (home_dir);
665 newp = __alloca (home_len + dirlen);
666 mempcpy (mempcpy (newp, home_dir, home_len),
667 &dirname[1], dirlen);
668 dirname = newp;
669 dirlen += home_len - 1;
670 }
671 dirname_modified = 1;
672 }
673 # if !defined _AMIGA && !defined WINDOWS32
674 else
675 {
676 char *end_name = strchr (dirname, '/');
677 const char *user_name;
678 const char *home_dir;
679 char *unescape = NULL;
680
681 if (!(flags & GLOB_NOESCAPE))
682 {
683 if (end_name == NULL)
684 {
685 unescape = strchr (dirname, '\\');
686 if (unescape)
687 end_name = strchr (unescape, '\0');
688 }
689 else
690 unescape = memchr (dirname, '\\', end_name - dirname);
691 }
692 if (end_name == NULL)
693 user_name = dirname + 1;
694 else
695 {
696 char *newp;
697 newp = __alloca (end_name - dirname);
698 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
699 = '\0';
700 if (unescape != NULL)
701 {
702 char *p = mempcpy (newp, dirname + 1,
703 unescape - dirname - 1);
704 char *q = unescape;
705 while (*q != '\0')
706 {
707 if (*q == '\\')
708 {
709 if (q[1] == '\0')
710 {
711 /* "~fo\\o\\" unescape to user_name "foo\\",
712 but "~fo\\o\\/" unescape to user_name
713 "foo". */
714 if (filename == NULL)
715 *p++ = '\\';
716 break;
717 }
718 ++q;
719 }
720 *p++ = *q++;
721 }
722 *p = '\0';
723 }
724 else
725 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
726 = '\0';
727 user_name = newp;
728 }
729
730 /* Look up specific user's home directory. */
731 {
732 struct passwd *p;
733 # if defined HAVE_GETPWNAM_R || defined _LIBC
734 long int buflen = GETPW_R_SIZE_MAX ();
735 char *pwtmpbuf;
736 struct passwd pwbuf;
737 int save = errno;
738
739 # ifndef _LIBC
740 if (buflen == -1)
741 /* 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
742 moderate value. */
743 buflen = 1024;
744 # endif
745 pwtmpbuf = __alloca (buflen);
746
747 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
748 {
749 if (errno != ERANGE)
750 {
751 p = NULL;
752 break;
753 }
754 # ifdef _LIBC
755 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
756 # else
757 buflen *= 2;
758 pwtmpbuf = __alloca (buflen);
759 # endif
760 __set_errno (save);
761 }
762 # else
763 p = getpwnam (user_name);
764 # endif
765 if (p != NULL)
766 home_dir = p->pw_dir;
767 else
768 home_dir = NULL;
769 }
770 /* If we found a home directory use this. */
771 if (home_dir != NULL)
772 {
773 char *newp;
774 size_t home_len = strlen (home_dir);
775 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
776 newp = __alloca (home_len + rest_len + 1);
777 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
778 end_name, rest_len)) = '\0';
779 dirname = newp;
780 dirlen = home_len + rest_len;
781 dirname_modified = 1;
782 }
783 else
784 if (flags & GLOB_TILDE_CHECK)
785 /* We have to regard it as an error if we cannot find the
786 home directory. */
787 return GLOB_NOMATCH;
788 }
789 # endif /* Not Amiga && not WINDOWS32. */
790 }
791
792 /* Now test whether we looked for "~" or "~NAME". In this case we
793 can give the answer now. */
794 if (filename == NULL)
795 {
796 struct stat st;
797 struct_stat64 st64;
798
799 /* Return the directory if we don't check for error or if it exists. */
800 if ((flags & GLOB_NOCHECK)
801 || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
802 ? ((*pglob->gl_stat) (dirname, &st) == 0
803 && S_ISDIR (st.st_mode))
804 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
805 {
806 int newcount = pglob->gl_pathc + pglob->gl_offs;
807 char **new_gl_pathv;
808
809 new_gl_pathv
810 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
811 if (new_gl_pathv == NULL)
812 {
813 nospace:
814 free (pglob->gl_pathv);
815 pglob->gl_pathv = NULL;
816 pglob->gl_pathc = 0;
817 return GLOB_NOSPACE;
818 }
819 pglob->gl_pathv = new_gl_pathv;
820
821 if (flags & GLOB_MARK)
822 {
823 char *p;
824 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
825 if (pglob->gl_pathv[newcount] == NULL)
826 goto nospace;
827 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
828 p[0] = '/';
829 p[1] = '\0';
830 }
831 else
832 {
833 pglob->gl_pathv[newcount] = strdup (dirname);
834 if (pglob->gl_pathv[newcount] == NULL)
835 goto nospace;
836 }
837 pglob->gl_pathv[++newcount] = NULL;
838 ++pglob->gl_pathc;
839 pglob->gl_flags = flags;
840
841 return 0;
842 }
843
844 /* Not found. */
845 return GLOB_NOMATCH;
846 }
847
848 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
849 /* meta is 1 if correct glob pattern containing metacharacters.
850 If meta has bit (1 << 2) set, it means there was an unterminated
851 [ which we handle the same, using fnmatch. Broken unterminated
852 pattern bracket expressions ought to be rare enough that it is
853 not worth special casing them, fnmatch will do the right thing. */
854 if (meta & 5)
855 {
856 /* The directory name contains metacharacters, so we
857 have to glob for the directory, and then glob for
858 the pattern in each directory found. */
859 size_t i;
860
861 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
862 {
863 /* "foo\\/bar". Remove the final backslash from dirname
864 if it has not been quoted. */
865 char *p = (char *) &dirname[dirlen - 1];
866
867 while (p > dirname && p[-1] == '\\') --p;
868 if ((&dirname[dirlen] - p) & 1)
869 *(char *) &dirname[--dirlen] = '\0';
870 }
871
872 if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) != 0, 0))
873 {
874 /* Use the alternative access functions also in the recursive
875 call. */
876 dirs.gl_opendir = pglob->gl_opendir;
877 dirs.gl_readdir = pglob->gl_readdir;
878 dirs.gl_closedir = pglob->gl_closedir;
879 dirs.gl_stat = pglob->gl_stat;
880 dirs.gl_lstat = pglob->gl_lstat;
881 }
882
883 status = glob (dirname,
884 ((flags & (GLOB_ERR | GLOB_NOESCAPE
885 | GLOB_ALTDIRFUNC))
886 | GLOB_NOSORT | GLOB_ONLYDIR),
887 errfunc, &dirs);
888 if (status != 0)
889 {
890 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
891 return status;
892 goto no_matches;
893 }
894
895 /* We have successfully globbed the preceding directory name.
896 For each name we found, call glob_in_dir on it and FILENAME,
897 appending the results to PGLOB. */
898 for (i = 0; i < dirs.gl_pathc; ++i)
899 {
900 int old_pathc;
901
902 #ifdef SHELL
903 {
904 /* Make globbing interruptible in the bash shell. */
905 extern int interrupt_state;
906
907 if (interrupt_state)
908 {
909 globfree (&dirs);
910 return GLOB_ABORTED;
911 }
912 }
913 #endif /* SHELL. */
914
915 old_pathc = pglob->gl_pathc;
916 status = glob_in_dir (filename, dirs.gl_pathv[i],
917 ((flags | GLOB_APPEND)
918 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
919 errfunc, pglob);
920 if (status == GLOB_NOMATCH)
921 /* No matches in this directory. Try the next. */
922 continue;
923
924 if (status != 0)
925 {
926 globfree (&dirs);
927 globfree (pglob);
928 pglob->gl_pathc = 0;
929 return status;
930 }
931
932 /* Stick the directory on the front of each name. */
933 if (prefix_array (dirs.gl_pathv[i],
934 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
935 pglob->gl_pathc - old_pathc))
936 {
937 globfree (&dirs);
938 globfree (pglob);
939 pglob->gl_pathc = 0;
940 return GLOB_NOSPACE;
941 }
942 }
943
944 flags |= GLOB_MAGCHAR;
945
946 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
947 But if we have not found any matching entry and the GLOB_NOCHECK
948 flag was set we must return the input pattern itself. */
949 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
950 {
951 no_matches:
952 /* No matches. */
953 if (flags & GLOB_NOCHECK)
954 {
955 int newcount = pglob->gl_pathc + pglob->gl_offs;
956 char **new_gl_pathv;
957
958 new_gl_pathv = realloc (pglob->gl_pathv,
959 (newcount + 2) * sizeof (char *));
960 if (new_gl_pathv == NULL)
961 {
962 globfree (&dirs);
963 return GLOB_NOSPACE;
964 }
965 pglob->gl_pathv = new_gl_pathv;
966
967 pglob->gl_pathv[newcount] = strdup (pattern);
968 if (pglob->gl_pathv[newcount] == NULL)
969 {
970 globfree (&dirs);
971 globfree (pglob);
972 pglob->gl_pathc = 0;
973 return GLOB_NOSPACE;
974 }
975
976 ++pglob->gl_pathc;
977 ++newcount;
978
979 pglob->gl_pathv[newcount] = NULL;
980 pglob->gl_flags = flags;
981 }
982 else
983 {
984 globfree (&dirs);
985 return GLOB_NOMATCH;
986 }
987 }
988
989 globfree (&dirs);
990 }
991 else
992 {
993 int old_pathc = pglob->gl_pathc;
994 int orig_flags = flags;
995
996 if (meta & 2)
997 {
998 char *p = strchr (dirname, '\\'), *q;
999 /* We need to unescape the dirname string. It is certainly
1000 allocated by alloca, as otherwise filename would be NULL
1001 or dirname wouldn't contain backslashes. */
1002 q = p;
1003 do
1004 {
1005 if (*p == '\\')
1006 {
1007 *q = *++p;
1008 --dirlen;
1009 }
1010 else
1011 *q = *p;
1012 ++q;
1013 }
1014 while (*p++ != '\0');
1015 dirname_modified = 1;
1016 }
1017 if (dirname_modified)
1018 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
1019 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
1020 if (status != 0)
1021 {
1022 if (status == GLOB_NOMATCH && flags != orig_flags
1023 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1024 {
1025 /* Make sure globfree (&dirs); is a nop. */
1026 dirs.gl_pathv = NULL;
1027 flags = orig_flags;
1028 goto no_matches;
1029 }
1030 return status;
1031 }
1032
1033 if (dirlen > 0)
1034 {
1035 /* Stick the directory on the front of each name. */
1036 if (prefix_array (dirname,
1037 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1038 pglob->gl_pathc - old_pathc))
1039 {
1040 globfree (pglob);
1041 pglob->gl_pathc = 0;
1042 return GLOB_NOSPACE;
1043 }
1044 }
1045 }
1046
1047 if (flags & GLOB_MARK)
1048 {
1049 /* Append slashes to directory names. */
1050 size_t i;
1051 struct stat st;
1052 struct_stat64 st64;
1053
1054 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1055 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1056 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1057 && S_ISDIR (st.st_mode))
1058 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1059 && S_ISDIR (st64.st_mode))))
1060 {
1061 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1062 char *new = realloc (pglob->gl_pathv[i], len);
1063 if (new == NULL)
1064 {
1065 globfree (pglob);
1066 pglob->gl_pathc = 0;
1067 return GLOB_NOSPACE;
1068 }
1069 strcpy (&new[len - 2], "/");
1070 pglob->gl_pathv[i] = new;
1071 }
1072 }
1073
1074 if (!(flags & GLOB_NOSORT))
1075 {
1076 /* Sort the vector. */
1077 qsort (&pglob->gl_pathv[oldcount],
1078 pglob->gl_pathc + pglob->gl_offs - oldcount,
1079 sizeof (char *), collated_compare);
1080 }
1081
1082 return 0;
1083 }
1084 #if defined _LIBC && !defined glob
1085 libc_hidden_def (glob)
1086 #endif
1087
1088
1089 #if !defined _LIBC || !defined GLOB_ONLY_P
1090
1091 /* Free storage allocated in PGLOB by a previous 'glob' call. */
1092 void
1093 globfree (pglob)
1094 register glob_t *pglob;
1095 {
1096 if (pglob->gl_pathv != NULL)
1097 {
1098 size_t i;
1099 for (i = 0; i < pglob->gl_pathc; ++i)
1100 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
1101 free (pglob->gl_pathv[pglob->gl_offs + i]);
1102 free (pglob->gl_pathv);
1103 pglob->gl_pathv = NULL;
1104 }
1105 }
1106 #if defined _LIBC && !defined globfree
libc_hidden_def(globfree)1107 libc_hidden_def (globfree)
1108 #endif
1109
1110
1111 /* Do a collated comparison of A and B. */
1112 static int
1113 collated_compare (const void *a, const void *b)
1114 {
1115 char *const *ps1 = a; char *s1 = *ps1;
1116 char *const *ps2 = b; char *s2 = *ps2;
1117
1118 if (s1 == s2)
1119 return 0;
1120 if (s1 == NULL)
1121 return 1;
1122 if (s2 == NULL)
1123 return -1;
1124 return strcoll (s1, s2);
1125 }
1126
1127
1128 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1129 elements in place. Return nonzero if out of memory, zero if successful.
1130 A slash is inserted between DIRNAME and each elt of ARRAY,
1131 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1132 static int
prefix_array(const char * dirname,char ** array,size_t n)1133 prefix_array (const char *dirname, char **array, size_t n)
1134 {
1135 register size_t i;
1136 size_t dirlen = strlen (dirname);
1137 #if defined __MSDOS__ || defined WINDOWS32
1138 int sep_char = '/';
1139 # define DIRSEP_CHAR sep_char
1140 #else
1141 # define DIRSEP_CHAR '/'
1142 #endif
1143
1144 if (dirlen == 1 && dirname[0] == '/')
1145 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1146 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1147 dirlen = 0;
1148 #if defined __MSDOS__ || defined WINDOWS32
1149 else if (dirlen > 1)
1150 {
1151 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1152 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1153 --dirlen;
1154 else if (dirname[dirlen - 1] == ':')
1155 {
1156 /* DIRNAME is "d:". Use ':' instead of '/'. */
1157 --dirlen;
1158 sep_char = ':';
1159 }
1160 }
1161 #endif
1162
1163 for (i = 0; i < n; ++i)
1164 {
1165 size_t eltlen = strlen (array[i]) + 1;
1166 char *new = malloc (dirlen + 1 + eltlen);
1167 if (new == NULL)
1168 {
1169 while (i > 0)
1170 free (array[--i]);
1171 return 1;
1172 }
1173
1174 {
1175 char *endp = mempcpy (new, dirname, dirlen);
1176 *endp++ = DIRSEP_CHAR;
1177 mempcpy (endp, array[i], eltlen);
1178 }
1179 free (array[i]);
1180 array[i] = new;
1181 }
1182
1183 return 0;
1184 }
1185
1186
1187 /* We must not compile this function twice. */
1188 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1189 int
__glob_pattern_type(pattern,quote)1190 __glob_pattern_type (pattern, quote)
1191 const char *pattern;
1192 int quote;
1193 {
1194 register const char *p;
1195 int ret = 0;
1196
1197 for (p = pattern; *p != '\0'; ++p)
1198 switch (*p)
1199 {
1200 case '?':
1201 case '*':
1202 return 1;
1203
1204 case '\\':
1205 if (quote)
1206 {
1207 if (p[1] != '\0')
1208 ++p;
1209 ret |= 2;
1210 }
1211 break;
1212
1213 case '[':
1214 ret |= 4;
1215 break;
1216
1217 case ']':
1218 if (ret & 4)
1219 return 1;
1220 break;
1221 }
1222
1223 return ret;
1224 }
1225
1226 /* Return nonzero if PATTERN contains any metacharacters.
1227 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1228 int
__glob_pattern_p(pattern,quote)1229 __glob_pattern_p (pattern, quote)
1230 const char *pattern;
1231 int quote;
1232 {
1233 return __glob_pattern_type (pattern, quote) == 1;
1234 }
1235 # ifdef _LIBC
weak_alias(__glob_pattern_p,glob_pattern_p)1236 weak_alias (__glob_pattern_p, glob_pattern_p)
1237 # endif
1238 #endif
1239
1240 #endif /* !GLOB_ONLY_P */
1241
1242
1243 #if !defined _LIBC || !defined GLOB_ONLY_P
1244 /* We put this in a separate function mainly to allow the memory
1245 allocated with alloca to be recycled. */
1246 static int
1247 __attribute_noinline__
1248 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
1249 glob_t *pglob
1250 # if !defined _LIBC && !HAVE_FSTATAT
1251 , int flags
1252 # endif
1253 )
1254 {
1255 size_t fnamelen = strlen (fname);
1256 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1257 struct stat st;
1258
1259 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1260 fname, fnamelen + 1);
1261
1262 # if !defined _LIBC && !HAVE_FSTATAT
1263 if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) == 0, 1))
1264 {
1265 struct_stat64 st64;
1266 return __stat64 (fullname, &st64) == 0;
1267 }
1268 # endif
1269 return (*pglob->gl_stat) (fullname, &st) == 0;
1270 }
1271
1272 /* Return true if DIR/FNAME exists. */
1273 static int
link_exists_p(int dfd,const char * dir,size_t dirlen,const char * fname,glob_t * pglob,int flags)1274 link_exists_p (int dfd, const char *dir, size_t dirlen, const char *fname,
1275 glob_t *pglob, int flags)
1276 {
1277 # if defined _LIBC || HAVE_FSTATAT
1278 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1279 return link_exists2_p (dir, dirlen, fname, pglob);
1280 else
1281 {
1282 /* dfd cannot be -1 here, because dirfd never returns -1 on
1283 glibc, or on hosts that have fstatat. */
1284 struct_stat64 st64;
1285 return __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0;
1286 }
1287 # else
1288 return link_exists2_p (dir, dirlen, fname, pglob, flags);
1289 # endif
1290 }
1291 #endif
1292
1293
1294 /* Like 'glob', but PATTERN is a final pathname component,
1295 and matches are searched for in DIRECTORY.
1296 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1297 The GLOB_APPEND flag is assumed to be set (always appends). */
1298 static int
glob_in_dir(const char * pattern,const char * directory,int flags,int (* errfunc)(const char *,int),glob_t * pglob)1299 glob_in_dir (const char *pattern, const char *directory, int flags,
1300 int (*errfunc) (const char *, int),
1301 glob_t *pglob)
1302 {
1303 size_t dirlen = strlen (directory);
1304 void *stream = NULL;
1305 struct globnames
1306 {
1307 struct globnames *next;
1308 size_t count;
1309 char *name[64];
1310 };
1311 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1312 struct globnames init_names;
1313 struct globnames *names = &init_names;
1314 struct globnames *names_alloca = &init_names;
1315 size_t nfound = 0;
1316 size_t allocasize = sizeof (init_names);
1317 size_t cur = 0;
1318 int meta;
1319 int save;
1320 int result;
1321
1322 init_names.next = NULL;
1323 init_names.count = INITIAL_COUNT;
1324
1325 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1326 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1327 {
1328 /* We need not do any tests. The PATTERN contains no meta
1329 characters and we must not return an error therefore the
1330 result will always contain exactly one name. */
1331 flags |= GLOB_NOCHECK;
1332 }
1333 else if (meta == 0)
1334 {
1335 /* Since we use the normal file functions we can also use stat()
1336 to verify the file is there. */
1337 struct stat st;
1338 struct_stat64 st64;
1339 size_t patlen = strlen (pattern);
1340 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1341
1342 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1343 "/", 1),
1344 pattern, patlen + 1);
1345 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1346 ? (*pglob->gl_stat) (fullname, &st)
1347 : __stat64 (fullname, &st64)) == 0)
1348 /* We found this file to be existing. Now tell the rest
1349 of the function to copy this name into the result. */
1350 flags |= GLOB_NOCHECK;
1351 }
1352 else
1353 {
1354 stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1355 ? (*pglob->gl_opendir) (directory)
1356 : opendir (directory));
1357 if (stream == NULL)
1358 {
1359 if (errno != ENOTDIR
1360 && ((errfunc != NULL && (*errfunc) (directory, errno))
1361 || (flags & GLOB_ERR)))
1362 return GLOB_ABORTED;
1363 }
1364 else
1365 {
1366 int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1367 ? -1 : dirfd ((DIR *) stream));
1368 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1369 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1370 #if defined _AMIGA || defined VMS
1371 | FNM_CASEFOLD
1372 #endif
1373 );
1374 flags |= GLOB_MAGCHAR;
1375
1376 while (1)
1377 {
1378 const char *name;
1379 size_t len;
1380 #if defined _LIBC && !defined COMPILE_GLOB64
1381 struct dirent64 *d;
1382 union
1383 {
1384 struct dirent64 d64;
1385 char room [offsetof (struct dirent64, d_name[0])
1386 + NAME_MAX + 1];
1387 }
1388 d64buf;
1389
1390 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1391 {
1392 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1393 if (d32 != NULL)
1394 {
1395 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1396 d = &d64buf.d64;
1397 }
1398 else
1399 d = NULL;
1400 }
1401 else
1402 d = __readdir64 (stream);
1403 #else
1404 struct dirent *d = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1405 ? ((struct dirent *)
1406 (*pglob->gl_readdir) (stream))
1407 : __readdir (stream));
1408 #endif
1409 if (d == NULL)
1410 break;
1411 if (! REAL_DIR_ENTRY (d))
1412 continue;
1413
1414 /* If we shall match only directories use the information
1415 provided by the dirent call if possible. */
1416 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1417 continue;
1418
1419 name = d->d_name;
1420
1421 if (fnmatch (pattern, name, fnm_flags) == 0)
1422 {
1423 /* If the file we found is a symlink we have to
1424 make sure the target file exists. */
1425 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1426 || link_exists_p (dfd, directory, dirlen, name, pglob,
1427 flags))
1428 {
1429 if (cur == names->count)
1430 {
1431 struct globnames *newnames;
1432 size_t count = names->count * 2;
1433 size_t size = (sizeof (struct globnames)
1434 + ((count - INITIAL_COUNT)
1435 * sizeof (char *)));
1436 allocasize += size;
1437 if (__libc_use_alloca (allocasize))
1438 newnames = names_alloca = __alloca (size);
1439 else if ((newnames = malloc (size))
1440 == NULL)
1441 goto memory_error;
1442 newnames->count = count;
1443 newnames->next = names;
1444 names = newnames;
1445 cur = 0;
1446 }
1447 len = _D_EXACT_NAMLEN (d);
1448 names->name[cur] = malloc (len + 1);
1449 if (names->name[cur] == NULL)
1450 goto memory_error;
1451 *((char *) mempcpy (names->name[cur++], name, len))
1452 = '\0';
1453 ++nfound;
1454 }
1455 }
1456 }
1457 }
1458 }
1459
1460 if (nfound == 0 && (flags & GLOB_NOCHECK))
1461 {
1462 size_t len = strlen (pattern);
1463 nfound = 1;
1464 names->name[cur] = malloc (len + 1);
1465 if (names->name[cur] == NULL)
1466 goto memory_error;
1467 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1468 }
1469
1470 result = GLOB_NOMATCH;
1471 if (nfound != 0)
1472 {
1473 char **new_gl_pathv
1474 = realloc (pglob->gl_pathv,
1475 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1476 * sizeof (char *));
1477 result = 0;
1478
1479 if (new_gl_pathv == NULL)
1480 {
1481 memory_error:
1482 while (1)
1483 {
1484 struct globnames *old = names;
1485 size_t i;
1486 for (i = 0; i < cur; ++i)
1487 free (names->name[i]);
1488 names = names->next;
1489 /* NB: we will not leak memory here if we exit without
1490 freeing the current block assigned to OLD. At least
1491 the very first block is always allocated on the stack
1492 and this is the block assigned to OLD here. */
1493 if (names == NULL)
1494 {
1495 assert (old == &init_names);
1496 break;
1497 }
1498 cur = names->count;
1499 if (old == names_alloca)
1500 names_alloca = names;
1501 else
1502 free (old);
1503 }
1504 result = GLOB_NOSPACE;
1505 }
1506 else
1507 {
1508 while (1)
1509 {
1510 struct globnames *old = names;
1511 size_t i;
1512 for (i = 0; i < cur; ++i)
1513 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1514 = names->name[i];
1515 names = names->next;
1516 /* NB: we will not leak memory here if we exit without
1517 freeing the current block assigned to OLD. At least
1518 the very first block is always allocated on the stack
1519 and this is the block assigned to OLD here. */
1520 if (names == NULL)
1521 {
1522 assert (old == &init_names);
1523 break;
1524 }
1525 cur = names->count;
1526 if (old == names_alloca)
1527 names_alloca = names;
1528 else
1529 free (old);
1530 }
1531
1532 pglob->gl_pathv = new_gl_pathv;
1533
1534 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1535
1536 pglob->gl_flags = flags;
1537 }
1538 }
1539
1540 if (stream != NULL)
1541 {
1542 save = errno;
1543 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1544 (*pglob->gl_closedir) (stream);
1545 else
1546 closedir (stream);
1547 __set_errno (save);
1548 }
1549
1550 return result;
1551 }
1552