Disable x87 inline functions for x86-64
[glibc.git] / posix / glob.c
blob68ea2055daff90d343a33016001eb9bac170a1a5
1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <glob.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stddef.h>
30 /* Outcomment the following line for production quality code. */
31 /* #define NDEBUG 1 */
32 #include <assert.h>
34 #include <stdio.h> /* Needed on stupid SunOS for assert. */
36 #if !defined _LIBC || !defined GLOB_ONLY_P
37 #if defined HAVE_UNISTD_H || defined _LIBC
38 # include <unistd.h>
39 # ifndef POSIX
40 # ifdef _POSIX_VERSION
41 # define POSIX
42 # endif
43 # endif
44 #endif
46 #include <pwd.h>
48 #if defined HAVE_STDINT_H || defined _LIBC
49 # include <stdint.h>
50 #elif !defined UINTPTR_MAX
51 # define UINTPTR_MAX (~((size_t) 0))
52 #endif
54 #include <errno.h>
55 #ifndef __set_errno
56 # define __set_errno(val) errno = (val)
57 #endif
59 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
60 # include <dirent.h>
61 # define NAMLEN(dirent) strlen((dirent)->d_name)
62 #else
63 # define dirent direct
64 # define NAMLEN(dirent) (dirent)->d_namlen
65 # ifdef HAVE_SYS_NDIR_H
66 # include <sys/ndir.h>
67 # endif
68 # ifdef HAVE_SYS_DIR_H
69 # include <sys/dir.h>
70 # endif
71 # ifdef HAVE_NDIR_H
72 # include <ndir.h>
73 # endif
74 # ifdef HAVE_VMSDIR_H
75 # include "vmsdir.h"
76 # endif /* HAVE_VMSDIR_H */
77 #endif
80 /* In GNU systems, <dirent.h> defines this macro for us. */
81 #ifdef _D_NAMLEN
82 # undef NAMLEN
83 # define NAMLEN(d) _D_NAMLEN(d)
84 #endif
86 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
87 if the `d_type' member for `struct dirent' is available.
88 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
89 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
90 /* True if the directory entry D must be of type T. */
91 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
93 /* True if the directory entry D might be a symbolic link. */
94 # define DIRENT_MIGHT_BE_SYMLINK(d) \
95 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
97 /* True if the directory entry D might be a directory. */
98 # define DIRENT_MIGHT_BE_DIR(d) \
99 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
101 #else /* !HAVE_D_TYPE */
102 # define DIRENT_MUST_BE(d, t) false
103 # define DIRENT_MIGHT_BE_SYMLINK(d) true
104 # define DIRENT_MIGHT_BE_DIR(d) true
105 #endif /* HAVE_D_TYPE */
107 /* If the system has the `struct dirent64' type we use it internally. */
108 #if defined _LIBC && !defined COMPILE_GLOB64
109 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
110 # define CONVERT_D_NAMLEN(d64, d32)
111 # else
112 # define CONVERT_D_NAMLEN(d64, d32) \
113 (d64)->d_namlen = (d32)->d_namlen;
114 # endif
116 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
117 # define CONVERT_D_INO(d64, d32)
118 # else
119 # define CONVERT_D_INO(d64, d32) \
120 (d64)->d_ino = (d32)->d_ino;
121 # endif
123 # ifdef _DIRENT_HAVE_D_TYPE
124 # define CONVERT_D_TYPE(d64, d32) \
125 (d64)->d_type = (d32)->d_type;
126 # else
127 # define CONVERT_D_TYPE(d64, d32)
128 # endif
130 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
131 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
132 CONVERT_D_NAMLEN (d64, d32) \
133 CONVERT_D_INO (d64, d32) \
134 CONVERT_D_TYPE (d64, d32)
135 #endif
138 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
139 /* Posix does not require that the d_ino field be present, and some
140 systems do not provide it. */
141 # define REAL_DIR_ENTRY(dp) 1
142 #else
143 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
144 #endif /* POSIX */
146 #include <stdlib.h>
147 #include <string.h>
149 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
150 #include <limits.h>
151 #ifndef NAME_MAX
152 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
153 #endif
155 #include <alloca.h>
157 #ifdef _LIBC
158 # undef strdup
159 # define strdup(str) __strdup (str)
160 # define sysconf(id) __sysconf (id)
161 # define closedir(dir) __closedir (dir)
162 # define opendir(name) __opendir (name)
163 # define readdir(str) __readdir64 (str)
164 # define getpwnam_r(name, bufp, buf, len, res) \
165 __getpwnam_r (name, bufp, buf, len, res)
166 # ifndef __stat64
167 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
168 # endif
169 # define struct_stat64 struct stat64
170 #else /* !_LIBC */
171 # include "getlogin_r.h"
172 # include "mempcpy.h"
173 # include "stat-macros.h"
174 # include "strdup.h"
175 # define __stat64(fname, buf) stat (fname, buf)
176 # define struct_stat64 struct stat
177 # define __stat(fname, buf) stat (fname, buf)
178 # define __alloca alloca
179 # define __readdir readdir
180 # define __readdir64 readdir64
181 # define __glob_pattern_p glob_pattern_p
182 #endif /* _LIBC */
184 #include <fnmatch.h>
186 #ifdef _SC_GETPW_R_SIZE_MAX
187 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
188 #else
189 # define GETPW_R_SIZE_MAX() (-1)
190 #endif
191 #ifdef _SC_LOGIN_NAME_MAX
192 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
193 #else
194 # define GET_LOGIN_NAME_MAX() (-1)
195 #endif
197 static const char *next_brace_sub (const char *begin, int flags) __THROW;
199 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
201 #ifndef attribute_hidden
202 # define attribute_hidden
203 #endif
205 static int glob_in_dir (const char *pattern, const char *directory,
206 int flags, int (*errfunc) (const char *, int),
207 glob_t *pglob, size_t alloca_used);
208 extern int __glob_pattern_type (const char *pattern, int quote)
209 attribute_hidden;
211 #if !defined _LIBC || !defined GLOB_ONLY_P
212 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
213 static int collated_compare (const void *, const void *) __THROW;
216 /* Find the end of the sub-pattern in a brace expression. */
217 static const char *
218 next_brace_sub (const char *cp, int flags)
220 unsigned int depth = 0;
221 while (*cp != '\0')
222 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
224 if (*++cp == '\0')
225 break;
226 ++cp;
228 else
230 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
231 break;
233 if (*cp++ == '{')
234 depth++;
237 return *cp != '\0' ? cp : NULL;
240 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
242 /* Do glob searching for PATTERN, placing results in PGLOB.
243 The bits defined above may be set in FLAGS.
244 If a directory cannot be opened or read and ERRFUNC is not nil,
245 it is called with the pathname that caused the error, and the
246 `errno' value from the failing call; if it returns non-zero
247 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
248 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
249 Otherwise, `glob' returns zero. */
251 #ifdef GLOB_ATTRIBUTE
252 GLOB_ATTRIBUTE
253 #endif
254 glob (pattern, flags, errfunc, pglob)
255 const char *pattern;
256 int flags;
257 int (*errfunc) (const char *, int);
258 glob_t *pglob;
260 const char *filename;
261 char *dirname = NULL;
262 size_t dirlen;
263 int status;
264 size_t oldcount;
265 int meta;
266 int dirname_modified;
267 int malloc_dirname = 0;
268 glob_t dirs;
269 int retval = 0;
270 #ifdef _LIBC
271 size_t alloca_used = 0;
272 #endif
274 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
276 __set_errno (EINVAL);
277 return -1;
280 if (!(flags & GLOB_DOOFFS))
281 /* Have to do this so `globfree' knows where to start freeing. It
282 also makes all the code that uses gl_offs simpler. */
283 pglob->gl_offs = 0;
285 if (flags & GLOB_BRACE)
287 const char *begin;
289 if (flags & GLOB_NOESCAPE)
290 begin = strchr (pattern, '{');
291 else
293 begin = pattern;
294 while (1)
296 if (*begin == '\0')
298 begin = NULL;
299 break;
302 if (*begin == '\\' && begin[1] != '\0')
303 ++begin;
304 else if (*begin == '{')
305 break;
307 ++begin;
311 if (begin != NULL)
313 /* Allocate working buffer large enough for our work. Note that
314 we have at least an opening and closing brace. */
315 size_t firstc;
316 char *alt_start;
317 const char *p;
318 const char *next;
319 const char *rest;
320 size_t rest_len;
321 char *onealt;
322 size_t pattern_len = strlen (pattern) - 1;
323 #ifdef _LIBC
324 int alloca_onealt = __libc_use_alloca (alloca_used + pattern_len);
325 if (alloca_onealt)
326 onealt = alloca_account (pattern_len, alloca_used);
327 else
328 #endif
330 onealt = (char *) malloc (pattern_len);
331 if (onealt == NULL)
333 if (!(flags & GLOB_APPEND))
335 pglob->gl_pathc = 0;
336 pglob->gl_pathv = NULL;
338 return GLOB_NOSPACE;
342 /* We know the prefix for all sub-patterns. */
343 alt_start = mempcpy (onealt, pattern, begin - pattern);
345 /* Find the first sub-pattern and at the same time find the
346 rest after the closing brace. */
347 next = next_brace_sub (begin + 1, flags);
348 if (next == NULL)
350 /* It is an illegal expression. */
351 illegal_brace:
352 #ifdef _LIBC
353 if (__builtin_expect (!alloca_onealt, 0))
354 #endif
355 free (onealt);
356 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
359 /* Now find the end of the whole brace expression. */
360 rest = next;
361 while (*rest != '}')
363 rest = next_brace_sub (rest + 1, flags);
364 if (rest == NULL)
365 /* It is an illegal expression. */
366 goto illegal_brace;
368 /* Please note that we now can be sure the brace expression
369 is well-formed. */
370 rest_len = strlen (++rest) + 1;
372 /* We have a brace expression. BEGIN points to the opening {,
373 NEXT points past the terminator of the first element, and END
374 points past the final }. We will accumulate result names from
375 recursive runs for each brace alternative in the buffer using
376 GLOB_APPEND. */
378 if (!(flags & GLOB_APPEND))
380 /* This call is to set a new vector, so clear out the
381 vector so we can append to it. */
382 pglob->gl_pathc = 0;
383 pglob->gl_pathv = NULL;
385 firstc = pglob->gl_pathc;
387 p = begin + 1;
388 while (1)
390 int result;
392 /* Construct the new glob expression. */
393 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
395 result = glob (onealt,
396 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
397 | GLOB_APPEND), errfunc, pglob);
399 /* If we got an error, return it. */
400 if (result && result != GLOB_NOMATCH)
402 #ifdef _LIBC
403 if (__builtin_expect (!alloca_onealt, 0))
404 #endif
405 free (onealt);
406 if (!(flags & GLOB_APPEND))
408 globfree (pglob);
409 pglob->gl_pathc = 0;
411 return result;
414 if (*next == '}')
415 /* We saw the last entry. */
416 break;
418 p = next + 1;
419 next = next_brace_sub (p, flags);
420 assert (next != NULL);
423 #ifdef _LIBC
424 if (__builtin_expect (!alloca_onealt, 0))
425 #endif
426 free (onealt);
428 if (pglob->gl_pathc != firstc)
429 /* We found some entries. */
430 return 0;
431 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
432 return GLOB_NOMATCH;
436 if (!(flags & GLOB_APPEND))
438 pglob->gl_pathc = 0;
439 if (!(flags & GLOB_DOOFFS))
440 pglob->gl_pathv = NULL;
441 else
443 size_t i;
445 if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *))
446 return GLOB_NOSPACE;
448 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
449 * sizeof (char *));
450 if (pglob->gl_pathv == NULL)
451 return GLOB_NOSPACE;
453 for (i = 0; i <= pglob->gl_offs; ++i)
454 pglob->gl_pathv[i] = NULL;
458 oldcount = pglob->gl_pathc + pglob->gl_offs;
460 /* Find the filename. */
461 filename = strrchr (pattern, '/');
462 #if defined __MSDOS__ || defined WINDOWS32
463 /* The case of "d:pattern". Since `:' is not allowed in
464 file names, we can safely assume that wherever it
465 happens in pattern, it signals the filename part. This
466 is so we could some day support patterns like "[a-z]:foo". */
467 if (filename == NULL)
468 filename = strchr (pattern, ':');
469 #endif /* __MSDOS__ || WINDOWS32 */
470 dirname_modified = 0;
471 if (filename == NULL)
473 /* This can mean two things: a simple name or "~name". The latter
474 case is nothing but a notation for a directory. */
475 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
477 dirname = (char *) pattern;
478 dirlen = strlen (pattern);
480 /* Set FILENAME to NULL as a special flag. This is ugly but
481 other solutions would require much more code. We test for
482 this special case below. */
483 filename = NULL;
485 else
487 if (__builtin_expect (pattern[0] == '\0', 0))
489 dirs.gl_pathv = NULL;
490 goto no_matches;
493 filename = pattern;
494 #ifdef _AMIGA
495 dirname = (char *) "";
496 #else
497 dirname = (char *) ".";
498 #endif
499 dirlen = 0;
502 else if (filename == pattern
503 || (filename == pattern + 1 && pattern[0] == '\\'
504 && (flags & GLOB_NOESCAPE) == 0))
506 /* "/pattern" or "\\/pattern". */
507 dirname = (char *) "/";
508 dirlen = 1;
509 ++filename;
511 else
513 char *newp;
514 dirlen = filename - pattern;
515 #if defined __MSDOS__ || defined WINDOWS32
516 if (*filename == ':'
517 || (filename > pattern + 1 && filename[-1] == ':'))
519 char *drive_spec;
521 ++dirlen;
522 drive_spec = (char *) __alloca (dirlen + 1);
523 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
524 /* For now, disallow wildcards in the drive spec, to
525 prevent infinite recursion in glob. */
526 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
527 return GLOB_NOMATCH;
528 /* If this is "d:pattern", we need to copy `:' to DIRNAME
529 as well. If it's "d:/pattern", don't remove the slash
530 from "d:/", since "d:" and "d:/" are not the same.*/
532 #endif
533 #ifdef _LIBC
534 if (__libc_use_alloca (alloca_used + dirlen + 1))
535 newp = alloca_account (dirlen + 1, alloca_used);
536 else
537 #endif
539 newp = malloc (dirlen + 1);
540 if (newp == NULL)
541 return GLOB_NOSPACE;
542 malloc_dirname = 1;
544 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
545 dirname = newp;
546 ++filename;
548 if (filename[0] == '\0'
549 #if defined __MSDOS__ || defined WINDOWS32
550 && dirname[dirlen - 1] != ':'
551 && (dirlen < 3 || dirname[dirlen - 2] != ':'
552 || dirname[dirlen - 1] != '/')
553 #endif
554 && dirlen > 1)
555 /* "pattern/". Expand "pattern", appending slashes. */
557 int orig_flags = flags;
558 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
560 /* "pattern\\/". Remove the final backslash if it hasn't
561 been quoted. */
562 char *p = (char *) &dirname[dirlen - 1];
564 while (p > dirname && p[-1] == '\\') --p;
565 if ((&dirname[dirlen] - p) & 1)
567 *(char *) &dirname[--dirlen] = '\0';
568 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
571 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
572 if (val == 0)
573 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
574 | (flags & GLOB_MARK));
575 else if (val == GLOB_NOMATCH && flags != orig_flags)
577 /* Make sure globfree (&dirs); is a nop. */
578 dirs.gl_pathv = NULL;
579 flags = orig_flags;
580 oldcount = pglob->gl_pathc + pglob->gl_offs;
581 goto no_matches;
583 retval = val;
584 goto out;
588 #ifndef VMS
589 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
591 if (dirname[1] == '\0' || dirname[1] == '/'
592 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
593 && (dirname[2] == '\0' || dirname[2] == '/')))
595 /* Look up home directory. */
596 char *home_dir = getenv ("HOME");
597 int malloc_home_dir = 0;
598 # ifdef _AMIGA
599 if (home_dir == NULL || home_dir[0] == '\0')
600 home_dir = "SYS:";
601 # else
602 # ifdef WINDOWS32
603 if (home_dir == NULL || home_dir[0] == '\0')
604 home_dir = "c:/users/default"; /* poor default */
605 # else
606 if (home_dir == NULL || home_dir[0] == '\0')
608 int success;
609 char *name;
610 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
612 if (buflen == 0)
613 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
614 a moderate value. */
615 buflen = 20;
616 name = alloca_account (buflen, alloca_used);
618 success = getlogin_r (name, buflen) == 0;
619 if (success)
621 struct passwd *p;
622 # if defined HAVE_GETPWNAM_R || defined _LIBC
623 long int pwbuflen = GETPW_R_SIZE_MAX ();
624 char *pwtmpbuf;
625 struct passwd pwbuf;
626 int malloc_pwtmpbuf = 0;
627 int save = errno;
629 # ifndef _LIBC
630 if (pwbuflen == -1)
631 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
632 Try a moderate value. */
633 pwbuflen = 1024;
634 # endif
635 if (__libc_use_alloca (alloca_used + pwbuflen))
636 pwtmpbuf = alloca_account (pwbuflen, alloca_used);
637 else
639 pwtmpbuf = malloc (pwbuflen);
640 if (pwtmpbuf == NULL)
642 retval = GLOB_NOSPACE;
643 goto out;
645 malloc_pwtmpbuf = 1;
648 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
649 != 0)
651 if (errno != ERANGE)
653 p = NULL;
654 break;
657 if (!malloc_pwtmpbuf
658 && __libc_use_alloca (alloca_used
659 + 2 * pwbuflen))
660 pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen,
661 2 * pwbuflen,
662 alloca_used);
663 else
665 char *newp = realloc (malloc_pwtmpbuf
666 ? pwtmpbuf : NULL,
667 2 * pwbuflen);
668 if (newp == NULL)
670 if (__builtin_expect (malloc_pwtmpbuf, 0))
671 free (pwtmpbuf);
672 retval = GLOB_NOSPACE;
673 goto out;
675 pwtmpbuf = newp;
676 pwbuflen = 2 * pwbuflen;
677 malloc_pwtmpbuf = 1;
679 __set_errno (save);
681 # else
682 p = getpwnam (name);
683 # endif
684 if (p != NULL)
686 if (!malloc_pwtmpbuf)
687 home_dir = p->pw_dir;
688 else
690 size_t home_dir_len = strlen (p->pw_dir) + 1;
691 if (__libc_use_alloca (alloca_used + home_dir_len))
692 home_dir = alloca_account (home_dir_len,
693 alloca_used);
694 else
696 home_dir = malloc (home_dir_len);
697 if (home_dir == NULL)
699 free (pwtmpbuf);
700 retval = GLOB_NOSPACE;
701 goto out;
703 malloc_home_dir = 1;
705 memcpy (home_dir, p->pw_dir, home_dir_len);
707 free (pwtmpbuf);
712 if (home_dir == NULL || home_dir[0] == '\0')
714 if (flags & GLOB_TILDE_CHECK)
716 if (__builtin_expect (malloc_home_dir, 0))
717 free (home_dir);
718 retval = GLOB_NOMATCH;
719 goto out;
721 else
722 home_dir = (char *) "~"; /* No luck. */
724 # endif /* WINDOWS32 */
725 # endif
726 /* Now construct the full directory. */
727 if (dirname[1] == '\0')
729 if (__builtin_expect (malloc_dirname, 0))
730 free (dirname);
732 dirname = home_dir;
733 dirlen = strlen (dirname);
734 malloc_dirname = malloc_home_dir;
736 else
738 char *newp;
739 size_t home_len = strlen (home_dir);
740 int use_alloca = __libc_use_alloca (alloca_used
741 + home_len + dirlen);
742 if (use_alloca)
743 newp = alloca_account (home_len + dirlen, alloca_used);
744 else
746 newp = malloc (home_len + dirlen);
747 if (newp == NULL)
749 if (__builtin_expect (malloc_home_dir, 0))
750 free (home_dir);
751 retval = GLOB_NOSPACE;
752 goto out;
756 mempcpy (mempcpy (newp, home_dir, home_len),
757 &dirname[1], dirlen);
759 if (__builtin_expect (malloc_dirname, 0))
760 free (dirname);
762 dirname = newp;
763 dirlen += home_len - 1;
764 malloc_dirname = !use_alloca;
766 dirname_modified = 1;
768 # if !defined _AMIGA && !defined WINDOWS32
769 else
771 char *end_name = strchr (dirname, '/');
772 char *user_name;
773 int malloc_user_name = 0;
774 char *unescape = NULL;
776 if (!(flags & GLOB_NOESCAPE))
778 if (end_name == NULL)
780 unescape = strchr (dirname, '\\');
781 if (unescape)
782 end_name = strchr (unescape, '\0');
784 else
785 unescape = memchr (dirname, '\\', end_name - dirname);
787 if (end_name == NULL)
788 user_name = dirname + 1;
789 else
791 char *newp;
792 if (__libc_use_alloca (alloca_used + (end_name - dirname)))
793 newp = alloca_account (end_name - dirname, alloca_used);
794 else
796 newp = malloc (end_name - dirname);
797 if (newp == NULL)
799 retval = GLOB_NOSPACE;
800 goto out;
802 malloc_user_name = 1;
804 if (unescape != NULL)
806 char *p = mempcpy (newp, dirname + 1,
807 unescape - dirname - 1);
808 char *q = unescape;
809 while (*q != '\0')
811 if (*q == '\\')
813 if (q[1] == '\0')
815 /* "~fo\\o\\" unescape to user_name "foo\\",
816 but "~fo\\o\\/" unescape to user_name
817 "foo". */
818 if (filename == NULL)
819 *p++ = '\\';
820 break;
822 ++q;
824 *p++ = *q++;
826 *p = '\0';
828 else
829 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
830 = '\0';
831 user_name = newp;
834 /* Look up specific user's home directory. */
836 struct passwd *p;
837 # if defined HAVE_GETPWNAM_R || defined _LIBC
838 long int buflen = GETPW_R_SIZE_MAX ();
839 char *pwtmpbuf;
840 int malloc_pwtmpbuf = 0;
841 struct passwd pwbuf;
842 int save = errno;
844 # ifndef _LIBC
845 if (buflen == -1)
846 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
847 moderate value. */
848 buflen = 1024;
849 # endif
850 if (__libc_use_alloca (alloca_used + buflen))
851 pwtmpbuf = alloca_account (buflen, alloca_used);
852 else
854 pwtmpbuf = malloc (buflen);
855 if (pwtmpbuf == NULL)
857 nomem_getpw:
858 if (__builtin_expect (malloc_user_name, 0))
859 free (user_name);
860 retval = GLOB_NOSPACE;
861 goto out;
863 malloc_pwtmpbuf = 1;
866 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
868 if (errno != ERANGE)
870 p = NULL;
871 break;
873 if (!malloc_pwtmpbuf
874 && __libc_use_alloca (alloca_used + 2 * buflen))
875 pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen,
876 2 * buflen, alloca_used);
877 else
879 char *newp = realloc (malloc_pwtmpbuf ? pwtmpbuf : NULL,
880 2 * buflen);
881 if (newp == NULL)
883 if (__builtin_expect (malloc_pwtmpbuf, 0))
884 free (pwtmpbuf);
885 goto nomem_getpw;
887 pwtmpbuf = newp;
888 malloc_pwtmpbuf = 1;
890 __set_errno (save);
892 # else
893 p = getpwnam (user_name);
894 # endif
896 if (__builtin_expect (malloc_user_name, 0))
897 free (user_name);
899 /* If we found a home directory use this. */
900 if (p != NULL)
902 size_t home_len = strlen (p->pw_dir);
903 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
905 if (__builtin_expect (malloc_dirname, 0))
906 free (dirname);
907 malloc_dirname = 0;
909 if (__libc_use_alloca (alloca_used + home_len + rest_len + 1))
910 dirname = alloca_account (home_len + rest_len + 1,
911 alloca_used);
912 else
914 dirname = malloc (home_len + rest_len + 1);
915 if (dirname == NULL)
917 if (__builtin_expect (malloc_pwtmpbuf, 0))
918 free (pwtmpbuf);
919 retval = GLOB_NOSPACE;
920 goto out;
922 malloc_dirname = 1;
924 *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len),
925 end_name, rest_len)) = '\0';
927 dirlen = home_len + rest_len;
928 dirname_modified = 1;
930 if (__builtin_expect (malloc_pwtmpbuf, 0))
931 free (pwtmpbuf);
933 else
935 if (__builtin_expect (malloc_pwtmpbuf, 0))
936 free (pwtmpbuf);
938 if (flags & GLOB_TILDE_CHECK)
939 /* We have to regard it as an error if we cannot find the
940 home directory. */
941 return GLOB_NOMATCH;
945 # endif /* Not Amiga && not WINDOWS32. */
947 #endif /* Not VMS. */
949 /* Now test whether we looked for "~" or "~NAME". In this case we
950 can give the answer now. */
951 if (filename == NULL)
953 struct stat st;
954 struct_stat64 st64;
956 /* Return the directory if we don't check for error or if it exists. */
957 if ((flags & GLOB_NOCHECK)
958 || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
959 ? ((*pglob->gl_stat) (dirname, &st) == 0
960 && S_ISDIR (st.st_mode))
961 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
963 int newcount = pglob->gl_pathc + pglob->gl_offs;
964 char **new_gl_pathv;
966 if (newcount > UINTPTR_MAX - (1 + 1)
967 || newcount + 1 + 1 > ~((size_t) 0) / sizeof (char *))
969 nospace:
970 free (pglob->gl_pathv);
971 pglob->gl_pathv = NULL;
972 pglob->gl_pathc = 0;
973 return GLOB_NOSPACE;
976 new_gl_pathv
977 = (char **) realloc (pglob->gl_pathv,
978 (newcount + 1 + 1) * sizeof (char *));
979 if (new_gl_pathv == NULL)
980 goto nospace;
981 pglob->gl_pathv = new_gl_pathv;
983 if (flags & GLOB_MARK)
985 char *p;
986 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
987 if (pglob->gl_pathv[newcount] == NULL)
988 goto nospace;
989 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
990 p[0] = '/';
991 p[1] = '\0';
993 else
995 pglob->gl_pathv[newcount] = strdup (dirname);
996 if (pglob->gl_pathv[newcount] == NULL)
997 goto nospace;
999 pglob->gl_pathv[++newcount] = NULL;
1000 ++pglob->gl_pathc;
1001 pglob->gl_flags = flags;
1003 return 0;
1006 /* Not found. */
1007 return GLOB_NOMATCH;
1010 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
1011 /* meta is 1 if correct glob pattern containing metacharacters.
1012 If meta has bit (1 << 2) set, it means there was an unterminated
1013 [ which we handle the same, using fnmatch. Broken unterminated
1014 pattern bracket expressions ought to be rare enough that it is
1015 not worth special casing them, fnmatch will do the right thing. */
1016 if (meta & 5)
1018 /* The directory name contains metacharacters, so we
1019 have to glob for the directory, and then glob for
1020 the pattern in each directory found. */
1021 size_t i;
1023 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
1025 /* "foo\\/bar". Remove the final backslash from dirname
1026 if it has not been quoted. */
1027 char *p = (char *) &dirname[dirlen - 1];
1029 while (p > dirname && p[-1] == '\\') --p;
1030 if ((&dirname[dirlen] - p) & 1)
1031 *(char *) &dirname[--dirlen] = '\0';
1034 if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) != 0, 0))
1036 /* Use the alternative access functions also in the recursive
1037 call. */
1038 dirs.gl_opendir = pglob->gl_opendir;
1039 dirs.gl_readdir = pglob->gl_readdir;
1040 dirs.gl_closedir = pglob->gl_closedir;
1041 dirs.gl_stat = pglob->gl_stat;
1042 dirs.gl_lstat = pglob->gl_lstat;
1045 status = glob (dirname,
1046 ((flags & (GLOB_ERR | GLOB_NOESCAPE
1047 | GLOB_ALTDIRFUNC))
1048 | GLOB_NOSORT | GLOB_ONLYDIR),
1049 errfunc, &dirs);
1050 if (status != 0)
1052 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
1053 return status;
1054 goto no_matches;
1057 /* We have successfully globbed the preceding directory name.
1058 For each name we found, call glob_in_dir on it and FILENAME,
1059 appending the results to PGLOB. */
1060 for (i = 0; i < dirs.gl_pathc; ++i)
1062 int old_pathc;
1064 #ifdef SHELL
1066 /* Make globbing interruptible in the bash shell. */
1067 extern int interrupt_state;
1069 if (interrupt_state)
1071 globfree (&dirs);
1072 return GLOB_ABORTED;
1075 #endif /* SHELL. */
1077 old_pathc = pglob->gl_pathc;
1078 status = glob_in_dir (filename, dirs.gl_pathv[i],
1079 ((flags | GLOB_APPEND)
1080 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
1081 errfunc, pglob, alloca_used);
1082 if (status == GLOB_NOMATCH)
1083 /* No matches in this directory. Try the next. */
1084 continue;
1086 if (status != 0)
1088 globfree (&dirs);
1089 globfree (pglob);
1090 pglob->gl_pathc = 0;
1091 return status;
1094 /* Stick the directory on the front of each name. */
1095 if (prefix_array (dirs.gl_pathv[i],
1096 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1097 pglob->gl_pathc - old_pathc))
1099 globfree (&dirs);
1100 globfree (pglob);
1101 pglob->gl_pathc = 0;
1102 return GLOB_NOSPACE;
1106 flags |= GLOB_MAGCHAR;
1108 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1109 But if we have not found any matching entry and the GLOB_NOCHECK
1110 flag was set we must return the input pattern itself. */
1111 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
1113 no_matches:
1114 /* No matches. */
1115 if (flags & GLOB_NOCHECK)
1117 int newcount = pglob->gl_pathc + pglob->gl_offs;
1118 char **new_gl_pathv;
1120 if (newcount > UINTPTR_MAX - 2
1121 || newcount + 2 > ~((size_t) 0) / sizeof (char *))
1123 nospace2:
1124 globfree (&dirs);
1125 return GLOB_NOSPACE;
1128 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
1129 (newcount + 2)
1130 * sizeof (char *));
1131 if (new_gl_pathv == NULL)
1132 goto nospace2;
1133 pglob->gl_pathv = new_gl_pathv;
1135 pglob->gl_pathv[newcount] = __strdup (pattern);
1136 if (pglob->gl_pathv[newcount] == NULL)
1138 globfree (&dirs);
1139 globfree (pglob);
1140 pglob->gl_pathc = 0;
1141 return GLOB_NOSPACE;
1144 ++pglob->gl_pathc;
1145 ++newcount;
1147 pglob->gl_pathv[newcount] = NULL;
1148 pglob->gl_flags = flags;
1150 else
1152 globfree (&dirs);
1153 return GLOB_NOMATCH;
1157 globfree (&dirs);
1159 else
1161 int old_pathc = pglob->gl_pathc;
1162 int orig_flags = flags;
1164 if (meta & 2)
1166 char *p = strchr (dirname, '\\'), *q;
1167 /* We need to unescape the dirname string. It is certainly
1168 allocated by alloca, as otherwise filename would be NULL
1169 or dirname wouldn't contain backslashes. */
1170 q = p;
1173 if (*p == '\\')
1175 *q = *++p;
1176 --dirlen;
1178 else
1179 *q = *p;
1180 ++q;
1182 while (*p++ != '\0');
1183 dirname_modified = 1;
1185 if (dirname_modified)
1186 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
1187 status = glob_in_dir (filename, dirname, flags, errfunc, pglob,
1188 alloca_used);
1189 if (status != 0)
1191 if (status == GLOB_NOMATCH && flags != orig_flags
1192 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1194 /* Make sure globfree (&dirs); is a nop. */
1195 dirs.gl_pathv = NULL;
1196 flags = orig_flags;
1197 goto no_matches;
1199 return status;
1202 if (dirlen > 0)
1204 /* Stick the directory on the front of each name. */
1205 if (prefix_array (dirname,
1206 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1207 pglob->gl_pathc - old_pathc))
1209 globfree (pglob);
1210 pglob->gl_pathc = 0;
1211 return GLOB_NOSPACE;
1216 if (flags & GLOB_MARK)
1218 /* Append slashes to directory names. */
1219 size_t i;
1220 struct stat st;
1221 struct_stat64 st64;
1223 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1224 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1225 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1226 && S_ISDIR (st.st_mode))
1227 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1228 && S_ISDIR (st64.st_mode))))
1230 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1231 char *new = realloc (pglob->gl_pathv[i], len);
1232 if (new == NULL)
1234 globfree (pglob);
1235 pglob->gl_pathc = 0;
1236 return GLOB_NOSPACE;
1238 strcpy (&new[len - 2], "/");
1239 pglob->gl_pathv[i] = new;
1243 if (!(flags & GLOB_NOSORT))
1245 /* Sort the vector. */
1246 qsort (&pglob->gl_pathv[oldcount],
1247 pglob->gl_pathc + pglob->gl_offs - oldcount,
1248 sizeof (char *), collated_compare);
1251 out:
1252 if (__builtin_expect (malloc_dirname, 0))
1253 free (dirname);
1255 return retval;
1257 #if defined _LIBC && !defined glob
1258 libc_hidden_def (glob)
1259 #endif
1262 #if !defined _LIBC || !defined GLOB_ONLY_P
1264 /* Free storage allocated in PGLOB by a previous `glob' call. */
1265 void
1266 globfree (pglob)
1267 register glob_t *pglob;
1269 if (pglob->gl_pathv != NULL)
1271 size_t i;
1272 for (i = 0; i < pglob->gl_pathc; ++i)
1273 free (pglob->gl_pathv[pglob->gl_offs + i]);
1274 free (pglob->gl_pathv);
1275 pglob->gl_pathv = NULL;
1278 #if defined _LIBC && !defined globfree
1279 libc_hidden_def (globfree)
1280 #endif
1283 /* Do a collated comparison of A and B. */
1284 static int
1285 collated_compare (const void *a, const void *b)
1287 const char *const s1 = *(const char *const * const) a;
1288 const char *const s2 = *(const char *const * const) b;
1290 if (s1 == s2)
1291 return 0;
1292 if (s1 == NULL)
1293 return 1;
1294 if (s2 == NULL)
1295 return -1;
1296 return strcoll (s1, s2);
1300 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1301 elements in place. Return nonzero if out of memory, zero if successful.
1302 A slash is inserted between DIRNAME and each elt of ARRAY,
1303 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1304 static int
1305 prefix_array (const char *dirname, char **array, size_t n)
1307 register size_t i;
1308 size_t dirlen = strlen (dirname);
1309 #if defined __MSDOS__ || defined WINDOWS32
1310 int sep_char = '/';
1311 # define DIRSEP_CHAR sep_char
1312 #else
1313 # define DIRSEP_CHAR '/'
1314 #endif
1316 if (dirlen == 1 && dirname[0] == '/')
1317 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1318 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1319 dirlen = 0;
1320 #if defined __MSDOS__ || defined WINDOWS32
1321 else if (dirlen > 1)
1323 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1324 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1325 --dirlen;
1326 else if (dirname[dirlen - 1] == ':')
1328 /* DIRNAME is "d:". Use `:' instead of `/'. */
1329 --dirlen;
1330 sep_char = ':';
1333 #endif
1335 for (i = 0; i < n; ++i)
1337 size_t eltlen = strlen (array[i]) + 1;
1338 char *new = (char *) malloc (dirlen + 1 + eltlen);
1339 if (new == NULL)
1341 while (i > 0)
1342 free (array[--i]);
1343 return 1;
1347 char *endp = mempcpy (new, dirname, dirlen);
1348 *endp++ = DIRSEP_CHAR;
1349 mempcpy (endp, array[i], eltlen);
1351 free (array[i]);
1352 array[i] = new;
1355 return 0;
1359 /* We must not compile this function twice. */
1360 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1362 __glob_pattern_type (pattern, quote)
1363 const char *pattern;
1364 int quote;
1366 register const char *p;
1367 int ret = 0;
1369 for (p = pattern; *p != '\0'; ++p)
1370 switch (*p)
1372 case '?':
1373 case '*':
1374 return 1;
1376 case '\\':
1377 if (quote)
1379 if (p[1] != '\0')
1380 ++p;
1381 ret |= 2;
1383 break;
1385 case '[':
1386 ret |= 4;
1387 break;
1389 case ']':
1390 if (ret & 4)
1391 return 1;
1392 break;
1395 return ret;
1398 /* Return nonzero if PATTERN contains any metacharacters.
1399 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1401 __glob_pattern_p (pattern, quote)
1402 const char *pattern;
1403 int quote;
1405 return __glob_pattern_type (pattern, quote) == 1;
1407 # ifdef _LIBC
1408 weak_alias (__glob_pattern_p, glob_pattern_p)
1409 # endif
1410 #endif
1412 #endif /* !GLOB_ONLY_P */
1415 /* We put this in a separate function mainly to allow the memory
1416 allocated with alloca to be recycled. */
1417 #if !defined _LIBC || !defined GLOB_ONLY_P
1418 static int
1419 __attribute_noinline__
1420 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
1421 glob_t *pglob
1422 # ifndef _LIBC
1423 , int flags
1424 # endif
1427 size_t fnamelen = strlen (fname);
1428 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1429 struct stat st;
1430 # ifndef _LIBC
1431 struct_stat64 st64;
1432 # endif
1434 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1435 fname, fnamelen + 1);
1437 # ifdef _LIBC
1438 return (*pglob->gl_stat) (fullname, &st) == 0;
1439 # else
1440 return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1441 ? (*pglob->gl_stat) (fullname, &st)
1442 : __stat64 (fullname, &st64)) == 0);
1443 # endif
1445 # ifdef _LIBC
1446 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1447 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1448 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1449 : ({ struct stat64 st64; \
1450 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1451 # else
1452 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1453 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1454 # endif
1455 #endif
1458 /* Like `glob', but PATTERN is a final pathname component,
1459 and matches are searched for in DIRECTORY.
1460 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1461 The GLOB_APPEND flag is assumed to be set (always appends). */
1462 static int
1463 glob_in_dir (const char *pattern, const char *directory, int flags,
1464 int (*errfunc) (const char *, int),
1465 glob_t *pglob, size_t alloca_used)
1467 size_t dirlen = strlen (directory);
1468 void *stream = NULL;
1469 struct globnames
1471 struct globnames *next;
1472 size_t count;
1473 char *name[64];
1475 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1476 struct globnames init_names;
1477 struct globnames *names = &init_names;
1478 struct globnames *names_alloca = &init_names;
1479 size_t nfound = 0;
1480 size_t cur = 0;
1481 int meta;
1482 int save;
1484 alloca_used += sizeof (init_names);
1486 init_names.next = NULL;
1487 init_names.count = INITIAL_COUNT;
1489 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1490 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1492 /* We need not do any tests. The PATTERN contains no meta
1493 characters and we must not return an error therefore the
1494 result will always contain exactly one name. */
1495 flags |= GLOB_NOCHECK;
1497 else if (meta == 0)
1499 /* Since we use the normal file functions we can also use stat()
1500 to verify the file is there. */
1501 union
1503 struct stat st;
1504 struct_stat64 st64;
1505 } ust;
1506 size_t patlen = strlen (pattern);
1507 int alloca_fullname = __libc_use_alloca (alloca_used
1508 + dirlen + 1 + patlen + 1);
1509 char *fullname;
1510 if (alloca_fullname)
1511 fullname = alloca_account (dirlen + 1 + patlen + 1, alloca_used);
1512 else
1514 fullname = malloc (dirlen + 1 + patlen + 1);
1515 if (fullname == NULL)
1516 return GLOB_NOSPACE;
1519 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1520 "/", 1),
1521 pattern, patlen + 1);
1522 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1523 ? (*pglob->gl_stat) (fullname, &ust.st)
1524 : __stat64 (fullname, &ust.st64)) == 0)
1525 /* We found this file to be existing. Now tell the rest
1526 of the function to copy this name into the result. */
1527 flags |= GLOB_NOCHECK;
1529 if (__builtin_expect (!alloca_fullname, 0))
1530 free (fullname);
1532 else
1534 stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1535 ? (*pglob->gl_opendir) (directory)
1536 : opendir (directory));
1537 if (stream == NULL)
1539 if (errno != ENOTDIR
1540 && ((errfunc != NULL && (*errfunc) (directory, errno))
1541 || (flags & GLOB_ERR)))
1542 return GLOB_ABORTED;
1544 else
1546 #ifdef _LIBC
1547 int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1548 ? -1 : dirfd ((DIR *) stream));
1549 #endif
1550 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1551 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1552 #if defined _AMIGA || defined VMS
1553 | FNM_CASEFOLD
1554 #endif
1556 flags |= GLOB_MAGCHAR;
1558 while (1)
1560 const char *name;
1561 size_t len;
1562 #if defined _LIBC && !defined COMPILE_GLOB64
1563 struct dirent64 *d;
1564 union
1566 struct dirent64 d64;
1567 char room [offsetof (struct dirent64, d_name[0])
1568 + NAME_MAX + 1];
1570 d64buf;
1572 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1574 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1575 if (d32 != NULL)
1577 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1578 d = &d64buf.d64;
1580 else
1581 d = NULL;
1583 else
1584 d = __readdir64 (stream);
1585 #else
1586 struct dirent *d = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1587 ? ((struct dirent *)
1588 (*pglob->gl_readdir) (stream))
1589 : __readdir (stream));
1590 #endif
1591 if (d == NULL)
1592 break;
1593 if (! REAL_DIR_ENTRY (d))
1594 continue;
1596 /* If we shall match only directories use the information
1597 provided by the dirent call if possible. */
1598 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1599 continue;
1601 name = d->d_name;
1603 if (fnmatch (pattern, name, fnm_flags) == 0)
1605 /* If the file we found is a symlink we have to
1606 make sure the target file exists. */
1607 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1608 || link_exists_p (dfd, directory, dirlen, name, pglob,
1609 flags))
1611 if (cur == names->count)
1613 struct globnames *newnames;
1614 size_t count = names->count * 2;
1615 size_t size = (sizeof (struct globnames)
1616 + ((count - INITIAL_COUNT)
1617 * sizeof (char *)));
1618 if (__libc_use_alloca (alloca_used + size))
1619 newnames = names_alloca
1620 = alloca_account (size, alloca_used);
1621 else if ((newnames = malloc (size))
1622 == NULL)
1623 goto memory_error;
1624 newnames->count = count;
1625 newnames->next = names;
1626 names = newnames;
1627 cur = 0;
1629 len = NAMLEN (d);
1630 names->name[cur] = (char *) malloc (len + 1);
1631 if (names->name[cur] == NULL)
1632 goto memory_error;
1633 *((char *) mempcpy (names->name[cur++], name, len))
1634 = '\0';
1635 ++nfound;
1642 if (nfound == 0 && (flags & GLOB_NOCHECK))
1644 size_t len = strlen (pattern);
1645 nfound = 1;
1646 names->name[cur] = (char *) malloc (len + 1);
1647 if (names->name[cur] == NULL)
1648 goto memory_error;
1649 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1652 int result = GLOB_NOMATCH;
1653 if (nfound != 0)
1655 result = 0;
1657 if (pglob->gl_pathc > UINTPTR_MAX - pglob->gl_offs
1658 || pglob->gl_pathc + pglob->gl_offs > UINTPTR_MAX - nfound
1659 || pglob->gl_pathc + pglob->gl_offs + nfound > UINTPTR_MAX - 1
1660 || (pglob->gl_pathc + pglob->gl_offs + nfound + 1
1661 > UINTPTR_MAX / sizeof (char *)))
1662 goto memory_error;
1664 char **new_gl_pathv;
1665 new_gl_pathv
1666 = (char **) realloc (pglob->gl_pathv,
1667 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1668 * sizeof (char *));
1669 if (new_gl_pathv == NULL)
1671 memory_error:
1672 while (1)
1674 struct globnames *old = names;
1675 for (size_t i = 0; i < cur; ++i)
1676 free (names->name[i]);
1677 names = names->next;
1678 /* NB: we will not leak memory here if we exit without
1679 freeing the current block assigned to OLD. At least
1680 the very first block is always allocated on the stack
1681 and this is the block assigned to OLD here. */
1682 if (names == NULL)
1684 assert (old == &init_names);
1685 break;
1687 cur = names->count;
1688 if (old == names_alloca)
1689 names_alloca = names;
1690 else
1691 free (old);
1693 result = GLOB_NOSPACE;
1695 else
1697 while (1)
1699 struct globnames *old = names;
1700 for (size_t i = 0; i < cur; ++i)
1701 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1702 = names->name[i];
1703 names = names->next;
1704 /* NB: we will not leak memory here if we exit without
1705 freeing the current block assigned to OLD. At least
1706 the very first block is always allocated on the stack
1707 and this is the block assigned to OLD here. */
1708 if (names == NULL)
1710 assert (old == &init_names);
1711 break;
1713 cur = names->count;
1714 if (old == names_alloca)
1715 names_alloca = names;
1716 else
1717 free (old);
1720 pglob->gl_pathv = new_gl_pathv;
1722 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1724 pglob->gl_flags = flags;
1728 if (stream != NULL)
1730 save = errno;
1731 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1732 (*pglob->gl_closedir) (stream);
1733 else
1734 closedir (stream);
1735 __set_errno (save);
1738 return result;