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