Replace __builtin_expect(..., 0) with __glibc_unlikely().
[glibc.git] / posix / glob.c
blob83b4f2fed3abc7133ef49d201a59686a55d4118c
1 /* Copyright (C) 1991-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <glob.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stddef.h>
29 /* Outcomment the following line for production quality code. */
30 /* #define NDEBUG 1 */
31 #include <assert.h>
33 #include <stdio.h> /* Needed on stupid SunOS for assert. */
35 #if !defined _LIBC || !defined GLOB_ONLY_P
36 #if defined HAVE_UNISTD_H || defined _LIBC
37 # include <unistd.h>
38 # ifndef POSIX
39 # ifdef _POSIX_VERSION
40 # define POSIX
41 # endif
42 # endif
43 #endif
45 #include <pwd.h>
47 #if defined HAVE_STDINT_H || defined _LIBC
48 # include <stdint.h>
49 #elif !defined UINTPTR_MAX
50 # define UINTPTR_MAX (~((size_t) 0))
51 #endif
53 #include <errno.h>
54 #ifndef __set_errno
55 # define __set_errno(val) errno = (val)
56 #endif
58 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
59 # include <dirent.h>
60 # define NAMLEN(dirent) strlen((dirent)->d_name)
61 #else
62 # define dirent direct
63 # define NAMLEN(dirent) (dirent)->d_namlen
64 # ifdef HAVE_SYS_NDIR_H
65 # include <sys/ndir.h>
66 # endif
67 # ifdef HAVE_SYS_DIR_H
68 # include <sys/dir.h>
69 # endif
70 # ifdef HAVE_NDIR_H
71 # include <ndir.h>
72 # endif
73 # ifdef HAVE_VMSDIR_H
74 # include "vmsdir.h"
75 # endif /* HAVE_VMSDIR_H */
76 #endif
79 /* In GNU systems, <dirent.h> defines this macro for us. */
80 #ifdef _D_NAMLEN
81 # undef NAMLEN
82 # define NAMLEN(d) _D_NAMLEN(d)
83 #endif
85 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
86 if the `d_type' member for `struct dirent' is available.
87 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
88 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
89 /* True if the directory entry D must be of type T. */
90 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
92 /* True if the directory entry D might be a symbolic link. */
93 # define DIRENT_MIGHT_BE_SYMLINK(d) \
94 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
96 /* True if the directory entry D might be a directory. */
97 # define DIRENT_MIGHT_BE_DIR(d) \
98 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
100 #else /* !HAVE_D_TYPE */
101 # define DIRENT_MUST_BE(d, t) false
102 # define DIRENT_MIGHT_BE_SYMLINK(d) true
103 # define DIRENT_MIGHT_BE_DIR(d) true
104 #endif /* HAVE_D_TYPE */
106 /* If the system has the `struct dirent64' type we use it internally. */
107 #if defined _LIBC && !defined COMPILE_GLOB64
108 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
109 # define CONVERT_D_NAMLEN(d64, d32)
110 # else
111 # define CONVERT_D_NAMLEN(d64, d32) \
112 (d64)->d_namlen = (d32)->d_namlen;
113 # endif
115 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
116 # define CONVERT_D_INO(d64, d32)
117 # else
118 # define CONVERT_D_INO(d64, d32) \
119 (d64)->d_ino = (d32)->d_ino;
120 # endif
122 # ifdef _DIRENT_HAVE_D_TYPE
123 # define CONVERT_D_TYPE(d64, d32) \
124 (d64)->d_type = (d32)->d_type;
125 # else
126 # define CONVERT_D_TYPE(d64, d32)
127 # endif
129 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
130 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
131 CONVERT_D_NAMLEN (d64, d32) \
132 CONVERT_D_INO (d64, d32) \
133 CONVERT_D_TYPE (d64, d32)
134 #endif
137 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
138 /* Posix does not require that the d_ino field be present, and some
139 systems do not provide it. */
140 # define REAL_DIR_ENTRY(dp) 1
141 #else
142 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
143 #endif /* POSIX */
145 #include <stdlib.h>
146 #include <string.h>
148 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
149 #include <limits.h>
150 #ifndef NAME_MAX
151 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
152 #endif
154 #include <alloca.h>
156 #ifdef _LIBC
157 # undef strdup
158 # define strdup(str) __strdup (str)
159 # define sysconf(id) __sysconf (id)
160 # define closedir(dir) __closedir (dir)
161 # define opendir(name) __opendir (name)
162 # define readdir(str) __readdir64 (str)
163 # define getpwnam_r(name, bufp, buf, len, res) \
164 __getpwnam_r (name, bufp, buf, len, res)
165 # ifndef __stat64
166 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
167 # endif
168 # define struct_stat64 struct stat64
169 #else /* !_LIBC */
170 # include "getlogin_r.h"
171 # include "mempcpy.h"
172 # include "stat-macros.h"
173 # include "strdup.h"
174 # define __stat64(fname, buf) stat (fname, buf)
175 # define struct_stat64 struct stat
176 # define __stat(fname, buf) stat (fname, buf)
177 # define __alloca alloca
178 # define __readdir readdir
179 # define __readdir64 readdir64
180 # define __glob_pattern_p glob_pattern_p
181 #endif /* _LIBC */
183 #include <fnmatch.h>
185 #ifdef _SC_GETPW_R_SIZE_MAX
186 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
187 #else
188 # define GETPW_R_SIZE_MAX() (-1)
189 #endif
190 #ifdef _SC_LOGIN_NAME_MAX
191 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
192 #else
193 # define GET_LOGIN_NAME_MAX() (-1)
194 #endif
196 static const char *next_brace_sub (const char *begin, int flags) __THROW;
198 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
200 #ifndef attribute_hidden
201 # define attribute_hidden
202 #endif
204 static int glob_in_dir (const char *pattern, const char *directory,
205 int flags, int (*errfunc) (const char *, int),
206 glob_t *pglob, size_t alloca_used);
207 extern int __glob_pattern_type (const char *pattern, int quote)
208 attribute_hidden;
210 #if !defined _LIBC || !defined GLOB_ONLY_P
211 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
212 static int collated_compare (const void *, const void *) __THROW;
215 /* Find the end of the sub-pattern in a brace expression. */
216 static const char *
217 next_brace_sub (const char *cp, int flags)
219 size_t depth = 0;
220 while (*cp != '\0')
221 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
223 if (*++cp == '\0')
224 break;
225 ++cp;
227 else
229 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
230 break;
232 if (*cp++ == '{')
233 depth++;
236 return *cp != '\0' ? cp : NULL;
239 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
241 /* Do glob searching for PATTERN, placing results in PGLOB.
242 The bits defined above may be set in FLAGS.
243 If a directory cannot be opened or read and ERRFUNC is not nil,
244 it is called with the pathname that caused the error, and the
245 `errno' value from the failing call; if it returns non-zero
246 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
247 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
248 Otherwise, `glob' returns zero. */
250 #ifdef GLOB_ATTRIBUTE
251 GLOB_ATTRIBUTE
252 #endif
253 glob (pattern, flags, errfunc, pglob)
254 const char *pattern;
255 int flags;
256 int (*errfunc) (const char *, int);
257 glob_t *pglob;
259 const char *filename;
260 char *dirname = NULL;
261 size_t dirlen;
262 int status;
263 size_t oldcount;
264 int meta;
265 int dirname_modified;
266 int malloc_dirname = 0;
267 glob_t dirs;
268 int retval = 0;
269 #ifdef _LIBC
270 size_t alloca_used = 0;
271 #endif
273 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
275 __set_errno (EINVAL);
276 return -1;
279 if (!(flags & GLOB_DOOFFS))
280 /* Have to do this so `globfree' knows where to start freeing. It
281 also makes all the code that uses gl_offs simpler. */
282 pglob->gl_offs = 0;
284 if (flags & GLOB_BRACE)
286 const char *begin;
288 if (flags & GLOB_NOESCAPE)
289 begin = strchr (pattern, '{');
290 else
292 begin = pattern;
293 while (1)
295 if (*begin == '\0')
297 begin = NULL;
298 break;
301 if (*begin == '\\' && begin[1] != '\0')
302 ++begin;
303 else if (*begin == '{')
304 break;
306 ++begin;
310 if (begin != NULL)
312 /* Allocate working buffer large enough for our work. Note that
313 we have at least an opening and closing brace. */
314 size_t firstc;
315 char *alt_start;
316 const char *p;
317 const char *next;
318 const char *rest;
319 size_t rest_len;
320 char *onealt;
321 size_t pattern_len = strlen (pattern) - 1;
322 #ifdef _LIBC
323 int alloca_onealt = __libc_use_alloca (alloca_used + pattern_len);
324 if (alloca_onealt)
325 onealt = alloca_account (pattern_len, alloca_used);
326 else
327 #endif
329 onealt = (char *) malloc (pattern_len);
330 if (onealt == NULL)
332 if (!(flags & GLOB_APPEND))
334 pglob->gl_pathc = 0;
335 pglob->gl_pathv = NULL;
337 return GLOB_NOSPACE;
341 /* We know the prefix for all sub-patterns. */
342 alt_start = mempcpy (onealt, pattern, begin - pattern);
344 /* Find the first sub-pattern and at the same time find the
345 rest after the closing brace. */
346 next = next_brace_sub (begin + 1, flags);
347 if (next == NULL)
349 /* It is an illegal expression. */
350 illegal_brace:
351 #ifdef _LIBC
352 if (__builtin_expect (!alloca_onealt, 0))
353 #endif
354 free (onealt);
355 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
358 /* Now find the end of the whole brace expression. */
359 rest = next;
360 while (*rest != '}')
362 rest = next_brace_sub (rest + 1, flags);
363 if (rest == NULL)
364 /* It is an illegal expression. */
365 goto illegal_brace;
367 /* Please note that we now can be sure the brace expression
368 is well-formed. */
369 rest_len = strlen (++rest) + 1;
371 /* We have a brace expression. BEGIN points to the opening {,
372 NEXT points past the terminator of the first element, and END
373 points past the final }. We will accumulate result names from
374 recursive runs for each brace alternative in the buffer using
375 GLOB_APPEND. */
377 if (!(flags & GLOB_APPEND))
379 /* This call is to set a new vector, so clear out the
380 vector so we can append to it. */
381 pglob->gl_pathc = 0;
382 pglob->gl_pathv = NULL;
384 firstc = pglob->gl_pathc;
386 p = begin + 1;
387 while (1)
389 int result;
391 /* Construct the new glob expression. */
392 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
394 result = glob (onealt,
395 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
396 | GLOB_APPEND), errfunc, pglob);
398 /* If we got an error, return it. */
399 if (result && result != GLOB_NOMATCH)
401 #ifdef _LIBC
402 if (__builtin_expect (!alloca_onealt, 0))
403 #endif
404 free (onealt);
405 if (!(flags & GLOB_APPEND))
407 globfree (pglob);
408 pglob->gl_pathc = 0;
410 return result;
413 if (*next == '}')
414 /* We saw the last entry. */
415 break;
417 p = next + 1;
418 next = next_brace_sub (p, flags);
419 assert (next != NULL);
422 #ifdef _LIBC
423 if (__builtin_expect (!alloca_onealt, 0))
424 #endif
425 free (onealt);
427 if (pglob->gl_pathc != firstc)
428 /* We found some entries. */
429 return 0;
430 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
431 return GLOB_NOMATCH;
435 if (!(flags & GLOB_APPEND))
437 pglob->gl_pathc = 0;
438 if (!(flags & GLOB_DOOFFS))
439 pglob->gl_pathv = NULL;
440 else
442 size_t i;
444 if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *))
445 return GLOB_NOSPACE;
447 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
448 * sizeof (char *));
449 if (pglob->gl_pathv == NULL)
450 return GLOB_NOSPACE;
452 for (i = 0; i <= pglob->gl_offs; ++i)
453 pglob->gl_pathv[i] = NULL;
457 oldcount = pglob->gl_pathc + pglob->gl_offs;
459 /* Find the filename. */
460 filename = strrchr (pattern, '/');
461 #if defined __MSDOS__ || defined WINDOWS32
462 /* The case of "d:pattern". Since `:' is not allowed in
463 file names, we can safely assume that wherever it
464 happens in pattern, it signals the filename part. This
465 is so we could some day support patterns like "[a-z]:foo". */
466 if (filename == NULL)
467 filename = strchr (pattern, ':');
468 #endif /* __MSDOS__ || WINDOWS32 */
469 dirname_modified = 0;
470 if (filename == NULL)
472 /* This can mean two things: a simple name or "~name". The latter
473 case is nothing but a notation for a directory. */
474 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
476 dirname = (char *) pattern;
477 dirlen = strlen (pattern);
479 /* Set FILENAME to NULL as a special flag. This is ugly but
480 other solutions would require much more code. We test for
481 this special case below. */
482 filename = NULL;
484 else
486 if (__builtin_expect (pattern[0] == '\0', 0))
488 dirs.gl_pathv = NULL;
489 goto no_matches;
492 filename = pattern;
493 #ifdef _AMIGA
494 dirname = (char *) "";
495 #else
496 dirname = (char *) ".";
497 #endif
498 dirlen = 0;
501 else if (filename == pattern
502 || (filename == pattern + 1 && pattern[0] == '\\'
503 && (flags & GLOB_NOESCAPE) == 0))
505 /* "/pattern" or "\\/pattern". */
506 dirname = (char *) "/";
507 dirlen = 1;
508 ++filename;
510 else
512 char *newp;
513 dirlen = filename - pattern;
514 #if defined __MSDOS__ || defined WINDOWS32
515 if (*filename == ':'
516 || (filename > pattern + 1 && filename[-1] == ':'))
518 char *drive_spec;
520 ++dirlen;
521 drive_spec = (char *) __alloca (dirlen + 1);
522 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
523 /* For now, disallow wildcards in the drive spec, to
524 prevent infinite recursion in glob. */
525 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
526 return GLOB_NOMATCH;
527 /* If this is "d:pattern", we need to copy `:' to DIRNAME
528 as well. If it's "d:/pattern", don't remove the slash
529 from "d:/", since "d:" and "d:/" are not the same.*/
531 #endif
532 #ifdef _LIBC
533 if (__libc_use_alloca (alloca_used + dirlen + 1))
534 newp = alloca_account (dirlen + 1, alloca_used);
535 else
536 #endif
538 newp = malloc (dirlen + 1);
539 if (newp == NULL)
540 return GLOB_NOSPACE;
541 malloc_dirname = 1;
543 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
544 dirname = newp;
545 ++filename;
547 if (filename[0] == '\0'
548 #if defined __MSDOS__ || defined WINDOWS32
549 && dirname[dirlen - 1] != ':'
550 && (dirlen < 3 || dirname[dirlen - 2] != ':'
551 || dirname[dirlen - 1] != '/')
552 #endif
553 && dirlen > 1)
554 /* "pattern/". Expand "pattern", appending slashes. */
556 int orig_flags = flags;
557 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
559 /* "pattern\\/". Remove the final backslash if it hasn't
560 been quoted. */
561 char *p = (char *) &dirname[dirlen - 1];
563 while (p > dirname && p[-1] == '\\') --p;
564 if ((&dirname[dirlen] - p) & 1)
566 *(char *) &dirname[--dirlen] = '\0';
567 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
570 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
571 if (val == 0)
572 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
573 | (flags & GLOB_MARK));
574 else if (val == GLOB_NOMATCH && flags != orig_flags)
576 /* Make sure globfree (&dirs); is a nop. */
577 dirs.gl_pathv = NULL;
578 flags = orig_flags;
579 oldcount = pglob->gl_pathc + pglob->gl_offs;
580 goto no_matches;
582 retval = val;
583 goto out;
587 #ifndef VMS
588 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
590 if (dirname[1] == '\0' || dirname[1] == '/'
591 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
592 && (dirname[2] == '\0' || dirname[2] == '/')))
594 /* Look up home directory. */
595 char *home_dir = getenv ("HOME");
596 int malloc_home_dir = 0;
597 # ifdef _AMIGA
598 if (home_dir == NULL || home_dir[0] == '\0')
599 home_dir = "SYS:";
600 # else
601 # ifdef WINDOWS32
602 if (home_dir == NULL || home_dir[0] == '\0')
603 home_dir = "c:/users/default"; /* poor default */
604 # else
605 if (home_dir == NULL || home_dir[0] == '\0')
607 int success;
608 char *name;
609 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
611 if (buflen == 0)
612 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
613 a moderate value. */
614 buflen = 20;
615 name = alloca_account (buflen, alloca_used);
617 success = getlogin_r (name, buflen) == 0;
618 if (success)
620 struct passwd *p;
621 # if defined HAVE_GETPWNAM_R || defined _LIBC
622 long int pwbuflen = GETPW_R_SIZE_MAX ();
623 char *pwtmpbuf;
624 struct passwd pwbuf;
625 int malloc_pwtmpbuf = 0;
626 int save = errno;
628 # ifndef _LIBC
629 if (pwbuflen == -1)
630 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
631 Try a moderate value. */
632 pwbuflen = 1024;
633 # endif
634 if (__libc_use_alloca (alloca_used + pwbuflen))
635 pwtmpbuf = alloca_account (pwbuflen, alloca_used);
636 else
638 pwtmpbuf = malloc (pwbuflen);
639 if (pwtmpbuf == NULL)
641 retval = GLOB_NOSPACE;
642 goto out;
644 malloc_pwtmpbuf = 1;
647 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
648 != 0)
650 if (errno != ERANGE)
652 p = NULL;
653 break;
656 if (!malloc_pwtmpbuf
657 && __libc_use_alloca (alloca_used
658 + 2 * pwbuflen))
659 pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen,
660 2 * pwbuflen,
661 alloca_used);
662 else
664 char *newp = realloc (malloc_pwtmpbuf
665 ? pwtmpbuf : NULL,
666 2 * pwbuflen);
667 if (newp == NULL)
669 if (__builtin_expect (malloc_pwtmpbuf, 0))
670 free (pwtmpbuf);
671 retval = GLOB_NOSPACE;
672 goto out;
674 pwtmpbuf = newp;
675 pwbuflen = 2 * pwbuflen;
676 malloc_pwtmpbuf = 1;
678 __set_errno (save);
680 # else
681 p = getpwnam (name);
682 # endif
683 if (p != NULL)
685 if (!malloc_pwtmpbuf)
686 home_dir = p->pw_dir;
687 else
689 size_t home_dir_len = strlen (p->pw_dir) + 1;
690 if (__libc_use_alloca (alloca_used + home_dir_len))
691 home_dir = alloca_account (home_dir_len,
692 alloca_used);
693 else
695 home_dir = malloc (home_dir_len);
696 if (home_dir == NULL)
698 free (pwtmpbuf);
699 retval = GLOB_NOSPACE;
700 goto out;
702 malloc_home_dir = 1;
704 memcpy (home_dir, p->pw_dir, home_dir_len);
706 free (pwtmpbuf);
711 if (home_dir == NULL || home_dir[0] == '\0')
713 if (flags & GLOB_TILDE_CHECK)
715 if (__builtin_expect (malloc_home_dir, 0))
716 free (home_dir);
717 retval = GLOB_NOMATCH;
718 goto out;
720 else
721 home_dir = (char *) "~"; /* No luck. */
723 # endif /* WINDOWS32 */
724 # endif
725 /* Now construct the full directory. */
726 if (dirname[1] == '\0')
728 if (__builtin_expect (malloc_dirname, 0))
729 free (dirname);
731 dirname = home_dir;
732 dirlen = strlen (dirname);
733 malloc_dirname = malloc_home_dir;
735 else
737 char *newp;
738 size_t home_len = strlen (home_dir);
739 int use_alloca = __libc_use_alloca (alloca_used
740 + home_len + dirlen);
741 if (use_alloca)
742 newp = alloca_account (home_len + dirlen, alloca_used);
743 else
745 newp = malloc (home_len + dirlen);
746 if (newp == NULL)
748 if (__builtin_expect (malloc_home_dir, 0))
749 free (home_dir);
750 retval = GLOB_NOSPACE;
751 goto out;
755 mempcpy (mempcpy (newp, home_dir, home_len),
756 &dirname[1], dirlen);
758 if (__builtin_expect (malloc_dirname, 0))
759 free (dirname);
761 dirname = newp;
762 dirlen += home_len - 1;
763 malloc_dirname = !use_alloca;
765 dirname_modified = 1;
767 # if !defined _AMIGA && !defined WINDOWS32
768 else
770 char *end_name = strchr (dirname, '/');
771 char *user_name;
772 int malloc_user_name = 0;
773 char *unescape = NULL;
775 if (!(flags & GLOB_NOESCAPE))
777 if (end_name == NULL)
779 unescape = strchr (dirname, '\\');
780 if (unescape)
781 end_name = strchr (unescape, '\0');
783 else
784 unescape = memchr (dirname, '\\', end_name - dirname);
786 if (end_name == NULL)
787 user_name = dirname + 1;
788 else
790 char *newp;
791 if (__libc_use_alloca (alloca_used + (end_name - dirname)))
792 newp = alloca_account (end_name - dirname, alloca_used);
793 else
795 newp = malloc (end_name - dirname);
796 if (newp == NULL)
798 retval = GLOB_NOSPACE;
799 goto out;
801 malloc_user_name = 1;
803 if (unescape != NULL)
805 char *p = mempcpy (newp, dirname + 1,
806 unescape - dirname - 1);
807 char *q = unescape;
808 while (*q != '\0')
810 if (*q == '\\')
812 if (q[1] == '\0')
814 /* "~fo\\o\\" unescape to user_name "foo\\",
815 but "~fo\\o\\/" unescape to user_name
816 "foo". */
817 if (filename == NULL)
818 *p++ = '\\';
819 break;
821 ++q;
823 *p++ = *q++;
825 *p = '\0';
827 else
828 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
829 = '\0';
830 user_name = newp;
833 /* Look up specific user's home directory. */
835 struct passwd *p;
836 # if defined HAVE_GETPWNAM_R || defined _LIBC
837 long int buflen = GETPW_R_SIZE_MAX ();
838 char *pwtmpbuf;
839 int malloc_pwtmpbuf = 0;
840 struct passwd pwbuf;
841 int save = errno;
843 # ifndef _LIBC
844 if (buflen == -1)
845 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
846 moderate value. */
847 buflen = 1024;
848 # endif
849 if (__libc_use_alloca (alloca_used + buflen))
850 pwtmpbuf = alloca_account (buflen, alloca_used);
851 else
853 pwtmpbuf = malloc (buflen);
854 if (pwtmpbuf == NULL)
856 nomem_getpw:
857 if (__builtin_expect (malloc_user_name, 0))
858 free (user_name);
859 retval = GLOB_NOSPACE;
860 goto out;
862 malloc_pwtmpbuf = 1;
865 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
867 if (errno != ERANGE)
869 p = NULL;
870 break;
872 if (!malloc_pwtmpbuf
873 && __libc_use_alloca (alloca_used + 2 * buflen))
874 pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen,
875 2 * buflen, alloca_used);
876 else
878 char *newp = realloc (malloc_pwtmpbuf ? pwtmpbuf : NULL,
879 2 * buflen);
880 if (newp == NULL)
882 if (__builtin_expect (malloc_pwtmpbuf, 0))
883 free (pwtmpbuf);
884 goto nomem_getpw;
886 pwtmpbuf = newp;
887 malloc_pwtmpbuf = 1;
889 __set_errno (save);
891 # else
892 p = getpwnam (user_name);
893 # endif
895 if (__builtin_expect (malloc_user_name, 0))
896 free (user_name);
898 /* If we found a home directory use this. */
899 if (p != NULL)
901 size_t home_len = strlen (p->pw_dir);
902 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
904 if (__builtin_expect (malloc_dirname, 0))
905 free (dirname);
906 malloc_dirname = 0;
908 if (__libc_use_alloca (alloca_used + home_len + rest_len + 1))
909 dirname = alloca_account (home_len + rest_len + 1,
910 alloca_used);
911 else
913 dirname = malloc (home_len + rest_len + 1);
914 if (dirname == NULL)
916 if (__builtin_expect (malloc_pwtmpbuf, 0))
917 free (pwtmpbuf);
918 retval = GLOB_NOSPACE;
919 goto out;
921 malloc_dirname = 1;
923 *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len),
924 end_name, rest_len)) = '\0';
926 dirlen = home_len + rest_len;
927 dirname_modified = 1;
929 if (__builtin_expect (malloc_pwtmpbuf, 0))
930 free (pwtmpbuf);
932 else
934 if (__builtin_expect (malloc_pwtmpbuf, 0))
935 free (pwtmpbuf);
937 if (flags & GLOB_TILDE_CHECK)
938 /* We have to regard it as an error if we cannot find the
939 home directory. */
940 return GLOB_NOMATCH;
944 # endif /* Not Amiga && not WINDOWS32. */
946 #endif /* Not VMS. */
948 /* Now test whether we looked for "~" or "~NAME". In this case we
949 can give the answer now. */
950 if (filename == NULL)
952 struct stat st;
953 struct_stat64 st64;
955 /* Return the directory if we don't check for error or if it exists. */
956 if ((flags & GLOB_NOCHECK)
957 || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
958 ? ((*pglob->gl_stat) (dirname, &st) == 0
959 && S_ISDIR (st.st_mode))
960 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
962 size_t newcount = pglob->gl_pathc + pglob->gl_offs;
963 char **new_gl_pathv;
965 if (newcount > UINTPTR_MAX - (1 + 1)
966 || newcount + 1 + 1 > ~((size_t) 0) / sizeof (char *))
968 nospace:
969 free (pglob->gl_pathv);
970 pglob->gl_pathv = NULL;
971 pglob->gl_pathc = 0;
972 return GLOB_NOSPACE;
975 new_gl_pathv
976 = (char **) realloc (pglob->gl_pathv,
977 (newcount + 1 + 1) * sizeof (char *));
978 if (new_gl_pathv == NULL)
979 goto nospace;
980 pglob->gl_pathv = new_gl_pathv;
982 if (flags & GLOB_MARK)
984 char *p;
985 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
986 if (pglob->gl_pathv[newcount] == NULL)
987 goto nospace;
988 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
989 p[0] = '/';
990 p[1] = '\0';
992 else
994 pglob->gl_pathv[newcount] = strdup (dirname);
995 if (pglob->gl_pathv[newcount] == NULL)
996 goto nospace;
998 pglob->gl_pathv[++newcount] = NULL;
999 ++pglob->gl_pathc;
1000 pglob->gl_flags = flags;
1002 return 0;
1005 /* Not found. */
1006 return GLOB_NOMATCH;
1009 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
1010 /* meta is 1 if correct glob pattern containing metacharacters.
1011 If meta has bit (1 << 2) set, it means there was an unterminated
1012 [ which we handle the same, using fnmatch. Broken unterminated
1013 pattern bracket expressions ought to be rare enough that it is
1014 not worth special casing them, fnmatch will do the right thing. */
1015 if (meta & 5)
1017 /* The directory name contains metacharacters, so we
1018 have to glob for the directory, and then glob for
1019 the pattern in each directory found. */
1020 size_t i;
1022 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
1024 /* "foo\\/bar". Remove the final backslash from dirname
1025 if it has not been quoted. */
1026 char *p = (char *) &dirname[dirlen - 1];
1028 while (p > dirname && p[-1] == '\\') --p;
1029 if ((&dirname[dirlen] - p) & 1)
1030 *(char *) &dirname[--dirlen] = '\0';
1033 if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) != 0, 0))
1035 /* Use the alternative access functions also in the recursive
1036 call. */
1037 dirs.gl_opendir = pglob->gl_opendir;
1038 dirs.gl_readdir = pglob->gl_readdir;
1039 dirs.gl_closedir = pglob->gl_closedir;
1040 dirs.gl_stat = pglob->gl_stat;
1041 dirs.gl_lstat = pglob->gl_lstat;
1044 status = glob (dirname,
1045 ((flags & (GLOB_ERR | GLOB_NOESCAPE
1046 | GLOB_ALTDIRFUNC))
1047 | GLOB_NOSORT | GLOB_ONLYDIR),
1048 errfunc, &dirs);
1049 if (status != 0)
1051 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
1052 return status;
1053 goto no_matches;
1056 /* We have successfully globbed the preceding directory name.
1057 For each name we found, call glob_in_dir on it and FILENAME,
1058 appending the results to PGLOB. */
1059 for (i = 0; i < dirs.gl_pathc; ++i)
1061 size_t old_pathc;
1063 #ifdef SHELL
1065 /* Make globbing interruptible in the bash shell. */
1066 extern int interrupt_state;
1068 if (interrupt_state)
1070 globfree (&dirs);
1071 return GLOB_ABORTED;
1074 #endif /* SHELL. */
1076 old_pathc = pglob->gl_pathc;
1077 status = glob_in_dir (filename, dirs.gl_pathv[i],
1078 ((flags | GLOB_APPEND)
1079 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
1080 errfunc, pglob, alloca_used);
1081 if (status == GLOB_NOMATCH)
1082 /* No matches in this directory. Try the next. */
1083 continue;
1085 if (status != 0)
1087 globfree (&dirs);
1088 globfree (pglob);
1089 pglob->gl_pathc = 0;
1090 return status;
1093 /* Stick the directory on the front of each name. */
1094 if (prefix_array (dirs.gl_pathv[i],
1095 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1096 pglob->gl_pathc - old_pathc))
1098 globfree (&dirs);
1099 globfree (pglob);
1100 pglob->gl_pathc = 0;
1101 return GLOB_NOSPACE;
1105 flags |= GLOB_MAGCHAR;
1107 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1108 But if we have not found any matching entry and the GLOB_NOCHECK
1109 flag was set we must return the input pattern itself. */
1110 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
1112 no_matches:
1113 /* No matches. */
1114 if (flags & GLOB_NOCHECK)
1116 size_t newcount = pglob->gl_pathc + pglob->gl_offs;
1117 char **new_gl_pathv;
1119 if (newcount > UINTPTR_MAX - 2
1120 || newcount + 2 > ~((size_t) 0) / sizeof (char *))
1122 nospace2:
1123 globfree (&dirs);
1124 return GLOB_NOSPACE;
1127 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
1128 (newcount + 2)
1129 * sizeof (char *));
1130 if (new_gl_pathv == NULL)
1131 goto nospace2;
1132 pglob->gl_pathv = new_gl_pathv;
1134 pglob->gl_pathv[newcount] = __strdup (pattern);
1135 if (pglob->gl_pathv[newcount] == NULL)
1137 globfree (&dirs);
1138 globfree (pglob);
1139 pglob->gl_pathc = 0;
1140 return GLOB_NOSPACE;
1143 ++pglob->gl_pathc;
1144 ++newcount;
1146 pglob->gl_pathv[newcount] = NULL;
1147 pglob->gl_flags = flags;
1149 else
1151 globfree (&dirs);
1152 return GLOB_NOMATCH;
1156 globfree (&dirs);
1158 else
1160 size_t old_pathc = pglob->gl_pathc;
1161 int orig_flags = flags;
1163 if (meta & 2)
1165 char *p = strchr (dirname, '\\'), *q;
1166 /* We need to unescape the dirname string. It is certainly
1167 allocated by alloca, as otherwise filename would be NULL
1168 or dirname wouldn't contain backslashes. */
1169 q = p;
1172 if (*p == '\\')
1174 *q = *++p;
1175 --dirlen;
1177 else
1178 *q = *p;
1179 ++q;
1181 while (*p++ != '\0');
1182 dirname_modified = 1;
1184 if (dirname_modified)
1185 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
1186 status = glob_in_dir (filename, dirname, flags, errfunc, pglob,
1187 alloca_used);
1188 if (status != 0)
1190 if (status == GLOB_NOMATCH && flags != orig_flags
1191 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1193 /* Make sure globfree (&dirs); is a nop. */
1194 dirs.gl_pathv = NULL;
1195 flags = orig_flags;
1196 goto no_matches;
1198 return status;
1201 if (dirlen > 0)
1203 /* Stick the directory on the front of each name. */
1204 if (prefix_array (dirname,
1205 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1206 pglob->gl_pathc - old_pathc))
1208 globfree (pglob);
1209 pglob->gl_pathc = 0;
1210 return GLOB_NOSPACE;
1215 if (flags & GLOB_MARK)
1217 /* Append slashes to directory names. */
1218 size_t i;
1219 struct stat st;
1220 struct_stat64 st64;
1222 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1223 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1224 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1225 && S_ISDIR (st.st_mode))
1226 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1227 && S_ISDIR (st64.st_mode))))
1229 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1230 char *new = realloc (pglob->gl_pathv[i], len);
1231 if (new == NULL)
1233 globfree (pglob);
1234 pglob->gl_pathc = 0;
1235 return GLOB_NOSPACE;
1237 strcpy (&new[len - 2], "/");
1238 pglob->gl_pathv[i] = new;
1242 if (!(flags & GLOB_NOSORT))
1244 /* Sort the vector. */
1245 qsort (&pglob->gl_pathv[oldcount],
1246 pglob->gl_pathc + pglob->gl_offs - oldcount,
1247 sizeof (char *), collated_compare);
1250 out:
1251 if (__builtin_expect (malloc_dirname, 0))
1252 free (dirname);
1254 return retval;
1256 #if defined _LIBC && !defined glob
1257 libc_hidden_def (glob)
1258 #endif
1261 #if !defined _LIBC || !defined GLOB_ONLY_P
1263 /* Free storage allocated in PGLOB by a previous `glob' call. */
1264 void
1265 globfree (pglob)
1266 register glob_t *pglob;
1268 if (pglob->gl_pathv != NULL)
1270 size_t i;
1271 for (i = 0; i < pglob->gl_pathc; ++i)
1272 free (pglob->gl_pathv[pglob->gl_offs + i]);
1273 free (pglob->gl_pathv);
1274 pglob->gl_pathv = NULL;
1277 #if defined _LIBC && !defined globfree
1278 libc_hidden_def (globfree)
1279 #endif
1282 /* Do a collated comparison of A and B. */
1283 static int
1284 collated_compare (const void *a, const void *b)
1286 const char *const s1 = *(const char *const * const) a;
1287 const char *const s2 = *(const char *const * const) b;
1289 if (s1 == s2)
1290 return 0;
1291 if (s1 == NULL)
1292 return 1;
1293 if (s2 == NULL)
1294 return -1;
1295 return strcoll (s1, s2);
1299 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1300 elements in place. Return nonzero if out of memory, zero if successful.
1301 A slash is inserted between DIRNAME and each elt of ARRAY,
1302 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1303 static int
1304 prefix_array (const char *dirname, char **array, size_t n)
1306 register size_t i;
1307 size_t dirlen = strlen (dirname);
1308 #if defined __MSDOS__ || defined WINDOWS32
1309 int sep_char = '/';
1310 # define DIRSEP_CHAR sep_char
1311 #else
1312 # define DIRSEP_CHAR '/'
1313 #endif
1315 if (dirlen == 1 && dirname[0] == '/')
1316 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1317 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1318 dirlen = 0;
1319 #if defined __MSDOS__ || defined WINDOWS32
1320 else if (dirlen > 1)
1322 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1323 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1324 --dirlen;
1325 else if (dirname[dirlen - 1] == ':')
1327 /* DIRNAME is "d:". Use `:' instead of `/'. */
1328 --dirlen;
1329 sep_char = ':';
1332 #endif
1334 for (i = 0; i < n; ++i)
1336 size_t eltlen = strlen (array[i]) + 1;
1337 char *new = (char *) malloc (dirlen + 1 + eltlen);
1338 if (new == NULL)
1340 while (i > 0)
1341 free (array[--i]);
1342 return 1;
1346 char *endp = mempcpy (new, dirname, dirlen);
1347 *endp++ = DIRSEP_CHAR;
1348 mempcpy (endp, array[i], eltlen);
1350 free (array[i]);
1351 array[i] = new;
1354 return 0;
1358 /* We must not compile this function twice. */
1359 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1361 __glob_pattern_type (pattern, quote)
1362 const char *pattern;
1363 int quote;
1365 register const char *p;
1366 int ret = 0;
1368 for (p = pattern; *p != '\0'; ++p)
1369 switch (*p)
1371 case '?':
1372 case '*':
1373 return 1;
1375 case '\\':
1376 if (quote)
1378 if (p[1] != '\0')
1379 ++p;
1380 ret |= 2;
1382 break;
1384 case '[':
1385 ret |= 4;
1386 break;
1388 case ']':
1389 if (ret & 4)
1390 return 1;
1391 break;
1394 return ret;
1397 /* Return nonzero if PATTERN contains any metacharacters.
1398 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1400 __glob_pattern_p (pattern, quote)
1401 const char *pattern;
1402 int quote;
1404 return __glob_pattern_type (pattern, quote) == 1;
1406 # ifdef _LIBC
1407 weak_alias (__glob_pattern_p, glob_pattern_p)
1408 # endif
1409 #endif
1411 #endif /* !GLOB_ONLY_P */
1414 /* We put this in a separate function mainly to allow the memory
1415 allocated with alloca to be recycled. */
1416 #if !defined _LIBC || !defined GLOB_ONLY_P
1417 static int
1418 __attribute_noinline__
1419 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
1420 glob_t *pglob
1421 # ifndef _LIBC
1422 , int flags
1423 # endif
1426 size_t fnamelen = strlen (fname);
1427 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1428 struct stat st;
1429 # ifndef _LIBC
1430 struct_stat64 st64;
1431 # endif
1433 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1434 fname, fnamelen + 1);
1436 # ifdef _LIBC
1437 return (*pglob->gl_stat) (fullname, &st) == 0;
1438 # else
1439 return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1440 ? (*pglob->gl_stat) (fullname, &st)
1441 : __stat64 (fullname, &st64)) == 0);
1442 # endif
1444 # ifdef _LIBC
1445 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1446 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1447 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1448 : ({ struct stat64 st64; \
1449 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1450 # else
1451 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1452 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1453 # endif
1454 #endif
1457 /* Like `glob', but PATTERN is a final pathname component,
1458 and matches are searched for in DIRECTORY.
1459 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1460 The GLOB_APPEND flag is assumed to be set (always appends). */
1461 static int
1462 glob_in_dir (const char *pattern, const char *directory, int flags,
1463 int (*errfunc) (const char *, int),
1464 glob_t *pglob, size_t alloca_used)
1466 size_t dirlen = strlen (directory);
1467 void *stream = NULL;
1468 struct globnames
1470 struct globnames *next;
1471 size_t count;
1472 char *name[64];
1474 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1475 struct globnames init_names;
1476 struct globnames *names = &init_names;
1477 struct globnames *names_alloca = &init_names;
1478 size_t nfound = 0;
1479 size_t cur = 0;
1480 int meta;
1481 int save;
1483 alloca_used += sizeof (init_names);
1485 init_names.next = NULL;
1486 init_names.count = INITIAL_COUNT;
1488 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1489 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1491 /* We need not do any tests. The PATTERN contains no meta
1492 characters and we must not return an error therefore the
1493 result will always contain exactly one name. */
1494 flags |= GLOB_NOCHECK;
1496 else if (meta == 0)
1498 /* Since we use the normal file functions we can also use stat()
1499 to verify the file is there. */
1500 union
1502 struct stat st;
1503 struct_stat64 st64;
1504 } ust;
1505 size_t patlen = strlen (pattern);
1506 int alloca_fullname = __libc_use_alloca (alloca_used
1507 + dirlen + 1 + patlen + 1);
1508 char *fullname;
1509 if (alloca_fullname)
1510 fullname = alloca_account (dirlen + 1 + patlen + 1, alloca_used);
1511 else
1513 fullname = malloc (dirlen + 1 + patlen + 1);
1514 if (fullname == NULL)
1515 return GLOB_NOSPACE;
1518 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1519 "/", 1),
1520 pattern, patlen + 1);
1521 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1522 ? (*pglob->gl_stat) (fullname, &ust.st)
1523 : __stat64 (fullname, &ust.st64)) == 0)
1524 /* We found this file to be existing. Now tell the rest
1525 of the function to copy this name into the result. */
1526 flags |= GLOB_NOCHECK;
1528 if (__builtin_expect (!alloca_fullname, 0))
1529 free (fullname);
1531 else
1533 stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1534 ? (*pglob->gl_opendir) (directory)
1535 : opendir (directory));
1536 if (stream == NULL)
1538 if (errno != ENOTDIR
1539 && ((errfunc != NULL && (*errfunc) (directory, errno))
1540 || (flags & GLOB_ERR)))
1541 return GLOB_ABORTED;
1543 else
1545 #ifdef _LIBC
1546 int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1547 ? -1 : dirfd ((DIR *) stream));
1548 #endif
1549 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1550 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1551 #if defined _AMIGA || defined VMS
1552 | FNM_CASEFOLD
1553 #endif
1555 flags |= GLOB_MAGCHAR;
1557 while (1)
1559 const char *name;
1560 size_t len;
1561 #if defined _LIBC && !defined COMPILE_GLOB64
1562 struct dirent64 *d;
1563 union
1565 struct dirent64 d64;
1566 char room [offsetof (struct dirent64, d_name[0])
1567 + NAME_MAX + 1];
1569 d64buf;
1571 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1573 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1574 if (d32 != NULL)
1576 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1577 d = &d64buf.d64;
1579 else
1580 d = NULL;
1582 else
1583 d = __readdir64 (stream);
1584 #else
1585 struct dirent *d = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1586 ? ((struct dirent *)
1587 (*pglob->gl_readdir) (stream))
1588 : __readdir (stream));
1589 #endif
1590 if (d == NULL)
1591 break;
1592 if (! REAL_DIR_ENTRY (d))
1593 continue;
1595 /* If we shall match only directories use the information
1596 provided by the dirent call if possible. */
1597 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1598 continue;
1600 name = d->d_name;
1602 if (fnmatch (pattern, name, fnm_flags) == 0)
1604 /* If the file we found is a symlink we have to
1605 make sure the target file exists. */
1606 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1607 || link_exists_p (dfd, directory, dirlen, name, pglob,
1608 flags))
1610 if (cur == names->count)
1612 struct globnames *newnames;
1613 size_t count = names->count * 2;
1614 size_t size = (sizeof (struct globnames)
1615 + ((count - INITIAL_COUNT)
1616 * sizeof (char *)));
1617 if (__libc_use_alloca (alloca_used + size))
1618 newnames = names_alloca
1619 = alloca_account (size, alloca_used);
1620 else if ((newnames = malloc (size))
1621 == NULL)
1622 goto memory_error;
1623 newnames->count = count;
1624 newnames->next = names;
1625 names = newnames;
1626 cur = 0;
1628 len = NAMLEN (d);
1629 names->name[cur] = (char *) malloc (len + 1);
1630 if (names->name[cur] == NULL)
1631 goto memory_error;
1632 *((char *) mempcpy (names->name[cur++], name, len))
1633 = '\0';
1634 ++nfound;
1641 if (nfound == 0 && (flags & GLOB_NOCHECK))
1643 size_t len = strlen (pattern);
1644 nfound = 1;
1645 names->name[cur] = (char *) malloc (len + 1);
1646 if (names->name[cur] == NULL)
1647 goto memory_error;
1648 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1651 int result = GLOB_NOMATCH;
1652 if (nfound != 0)
1654 result = 0;
1656 if (pglob->gl_pathc > UINTPTR_MAX - pglob->gl_offs
1657 || pglob->gl_pathc + pglob->gl_offs > UINTPTR_MAX - nfound
1658 || pglob->gl_pathc + pglob->gl_offs + nfound > UINTPTR_MAX - 1
1659 || (pglob->gl_pathc + pglob->gl_offs + nfound + 1
1660 > UINTPTR_MAX / sizeof (char *)))
1661 goto memory_error;
1663 char **new_gl_pathv;
1664 new_gl_pathv
1665 = (char **) realloc (pglob->gl_pathv,
1666 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1667 * sizeof (char *));
1668 if (new_gl_pathv == NULL)
1670 memory_error:
1671 while (1)
1673 struct globnames *old = names;
1674 for (size_t i = 0; i < cur; ++i)
1675 free (names->name[i]);
1676 names = names->next;
1677 /* NB: we will not leak memory here if we exit without
1678 freeing the current block assigned to OLD. At least
1679 the very first block is always allocated on the stack
1680 and this is the block assigned to OLD here. */
1681 if (names == NULL)
1683 assert (old == &init_names);
1684 break;
1686 cur = names->count;
1687 if (old == names_alloca)
1688 names_alloca = names;
1689 else
1690 free (old);
1692 result = GLOB_NOSPACE;
1694 else
1696 while (1)
1698 struct globnames *old = names;
1699 for (size_t i = 0; i < cur; ++i)
1700 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1701 = names->name[i];
1702 names = names->next;
1703 /* NB: we will not leak memory here if we exit without
1704 freeing the current block assigned to OLD. At least
1705 the very first block is always allocated on the stack
1706 and this is the block assigned to OLD here. */
1707 if (names == NULL)
1709 assert (old == &init_names);
1710 break;
1712 cur = names->count;
1713 if (old == names_alloca)
1714 names_alloca = names;
1715 else
1716 free (old);
1719 pglob->gl_pathv = new_gl_pathv;
1721 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1723 pglob->gl_flags = flags;
1727 if (stream != NULL)
1729 save = errno;
1730 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1731 (*pglob->gl_closedir) (stream);
1732 else
1733 closedir (stream);
1734 __set_errno (save);
1737 return result;