Convert dlfcn/tststatic to use test-skeleton.
[glibc.git] / posix / glob.c
blob5b927763c4a875c4397e6c606c79d21b9dbc33d3
1 /* Copyright (C) 1991-2015 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) __THROWNL;
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) __THROWNL;
212 static int collated_compare (const void *, const void *) __THROWNL;
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 /* POSIX requires all slashes to be matched. This means that with
280 a trailing slash we must match only directories. */
281 if (pattern[0] && pattern[strlen (pattern) - 1] == '/')
282 flags |= GLOB_ONLYDIR;
284 if (!(flags & GLOB_DOOFFS))
285 /* Have to do this so `globfree' knows where to start freeing. It
286 also makes all the code that uses gl_offs simpler. */
287 pglob->gl_offs = 0;
289 if (flags & GLOB_BRACE)
291 const char *begin;
293 if (flags & GLOB_NOESCAPE)
294 begin = strchr (pattern, '{');
295 else
297 begin = pattern;
298 while (1)
300 if (*begin == '\0')
302 begin = NULL;
303 break;
306 if (*begin == '\\' && begin[1] != '\0')
307 ++begin;
308 else if (*begin == '{')
309 break;
311 ++begin;
315 if (begin != NULL)
317 /* Allocate working buffer large enough for our work. Note that
318 we have at least an opening and closing brace. */
319 size_t firstc;
320 char *alt_start;
321 const char *p;
322 const char *next;
323 const char *rest;
324 size_t rest_len;
325 char *onealt;
326 size_t pattern_len = strlen (pattern) - 1;
327 #ifdef _LIBC
328 int alloca_onealt = __libc_use_alloca (alloca_used + pattern_len);
329 if (alloca_onealt)
330 onealt = alloca_account (pattern_len, alloca_used);
331 else
332 #endif
334 onealt = (char *) malloc (pattern_len);
335 if (onealt == NULL)
337 if (!(flags & GLOB_APPEND))
339 pglob->gl_pathc = 0;
340 pglob->gl_pathv = NULL;
342 return GLOB_NOSPACE;
346 /* We know the prefix for all sub-patterns. */
347 alt_start = mempcpy (onealt, pattern, begin - pattern);
349 /* Find the first sub-pattern and at the same time find the
350 rest after the closing brace. */
351 next = next_brace_sub (begin + 1, flags);
352 if (next == NULL)
354 /* It is an illegal expression. */
355 illegal_brace:
356 #ifdef _LIBC
357 if (__glibc_unlikely (!alloca_onealt))
358 #endif
359 free (onealt);
360 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
363 /* Now find the end of the whole brace expression. */
364 rest = next;
365 while (*rest != '}')
367 rest = next_brace_sub (rest + 1, flags);
368 if (rest == NULL)
369 /* It is an illegal expression. */
370 goto illegal_brace;
372 /* Please note that we now can be sure the brace expression
373 is well-formed. */
374 rest_len = strlen (++rest) + 1;
376 /* We have a brace expression. BEGIN points to the opening {,
377 NEXT points past the terminator of the first element, and END
378 points past the final }. We will accumulate result names from
379 recursive runs for each brace alternative in the buffer using
380 GLOB_APPEND. */
382 if (!(flags & GLOB_APPEND))
384 /* This call is to set a new vector, so clear out the
385 vector so we can append to it. */
386 pglob->gl_pathc = 0;
387 pglob->gl_pathv = NULL;
389 firstc = pglob->gl_pathc;
391 p = begin + 1;
392 while (1)
394 int result;
396 /* Construct the new glob expression. */
397 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
399 result = glob (onealt,
400 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
401 | GLOB_APPEND), errfunc, pglob);
403 /* If we got an error, return it. */
404 if (result && result != GLOB_NOMATCH)
406 #ifdef _LIBC
407 if (__glibc_unlikely (!alloca_onealt))
408 #endif
409 free (onealt);
410 if (!(flags & GLOB_APPEND))
412 globfree (pglob);
413 pglob->gl_pathc = 0;
415 return result;
418 if (*next == '}')
419 /* We saw the last entry. */
420 break;
422 p = next + 1;
423 next = next_brace_sub (p, flags);
424 assert (next != NULL);
427 #ifdef _LIBC
428 if (__glibc_unlikely (!alloca_onealt))
429 #endif
430 free (onealt);
432 if (pglob->gl_pathc != firstc)
433 /* We found some entries. */
434 return 0;
435 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
436 return GLOB_NOMATCH;
440 if (!(flags & GLOB_APPEND))
442 pglob->gl_pathc = 0;
443 if (!(flags & GLOB_DOOFFS))
444 pglob->gl_pathv = NULL;
445 else
447 size_t i;
449 if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *))
450 return GLOB_NOSPACE;
452 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
453 * sizeof (char *));
454 if (pglob->gl_pathv == NULL)
455 return GLOB_NOSPACE;
457 for (i = 0; i <= pglob->gl_offs; ++i)
458 pglob->gl_pathv[i] = NULL;
462 oldcount = pglob->gl_pathc + pglob->gl_offs;
464 /* Find the filename. */
465 filename = strrchr (pattern, '/');
466 #if defined __MSDOS__ || defined WINDOWS32
467 /* The case of "d:pattern". Since `:' is not allowed in
468 file names, we can safely assume that wherever it
469 happens in pattern, it signals the filename part. This
470 is so we could some day support patterns like "[a-z]:foo". */
471 if (filename == NULL)
472 filename = strchr (pattern, ':');
473 #endif /* __MSDOS__ || WINDOWS32 */
474 dirname_modified = 0;
475 if (filename == NULL)
477 /* This can mean two things: a simple name or "~name". The latter
478 case is nothing but a notation for a directory. */
479 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
481 dirname = (char *) pattern;
482 dirlen = strlen (pattern);
484 /* Set FILENAME to NULL as a special flag. This is ugly but
485 other solutions would require much more code. We test for
486 this special case below. */
487 filename = NULL;
489 else
491 if (__glibc_unlikely (pattern[0] == '\0'))
493 dirs.gl_pathv = NULL;
494 goto no_matches;
497 filename = pattern;
498 #ifdef _AMIGA
499 dirname = (char *) "";
500 #else
501 dirname = (char *) ".";
502 #endif
503 dirlen = 0;
506 else if (filename == pattern
507 || (filename == pattern + 1 && pattern[0] == '\\'
508 && (flags & GLOB_NOESCAPE) == 0))
510 /* "/pattern" or "\\/pattern". */
511 dirname = (char *) "/";
512 dirlen = 1;
513 ++filename;
515 else
517 char *newp;
518 dirlen = filename - pattern;
519 #if defined __MSDOS__ || defined WINDOWS32
520 if (*filename == ':'
521 || (filename > pattern + 1 && filename[-1] == ':'))
523 char *drive_spec;
525 ++dirlen;
526 drive_spec = (char *) __alloca (dirlen + 1);
527 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
528 /* For now, disallow wildcards in the drive spec, to
529 prevent infinite recursion in glob. */
530 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
531 return GLOB_NOMATCH;
532 /* If this is "d:pattern", we need to copy `:' to DIRNAME
533 as well. If it's "d:/pattern", don't remove the slash
534 from "d:/", since "d:" and "d:/" are not the same.*/
536 #endif
537 #ifdef _LIBC
538 if (__libc_use_alloca (alloca_used + dirlen + 1))
539 newp = alloca_account (dirlen + 1, alloca_used);
540 else
541 #endif
543 newp = malloc (dirlen + 1);
544 if (newp == NULL)
545 return GLOB_NOSPACE;
546 malloc_dirname = 1;
548 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
549 dirname = newp;
550 ++filename;
552 if (filename[0] == '\0'
553 #if defined __MSDOS__ || defined WINDOWS32
554 && dirname[dirlen - 1] != ':'
555 && (dirlen < 3 || dirname[dirlen - 2] != ':'
556 || dirname[dirlen - 1] != '/')
557 #endif
558 && dirlen > 1)
559 /* "pattern/". Expand "pattern", appending slashes. */
561 int orig_flags = flags;
562 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
564 /* "pattern\\/". Remove the final backslash if it hasn't
565 been quoted. */
566 char *p = (char *) &dirname[dirlen - 1];
568 while (p > dirname && p[-1] == '\\') --p;
569 if ((&dirname[dirlen] - p) & 1)
571 *(char *) &dirname[--dirlen] = '\0';
572 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
575 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
576 if (val == 0)
577 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
578 | (flags & GLOB_MARK));
579 else if (val == GLOB_NOMATCH && flags != orig_flags)
581 /* Make sure globfree (&dirs); is a nop. */
582 dirs.gl_pathv = NULL;
583 flags = orig_flags;
584 oldcount = pglob->gl_pathc + pglob->gl_offs;
585 goto no_matches;
587 retval = val;
588 goto out;
592 #ifndef VMS
593 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
595 if (dirname[1] == '\0' || dirname[1] == '/'
596 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
597 && (dirname[2] == '\0' || dirname[2] == '/')))
599 /* Look up home directory. */
600 char *home_dir = getenv ("HOME");
601 int malloc_home_dir = 0;
602 # ifdef _AMIGA
603 if (home_dir == NULL || home_dir[0] == '\0')
604 home_dir = "SYS:";
605 # else
606 # ifdef WINDOWS32
607 if (home_dir == NULL || home_dir[0] == '\0')
608 home_dir = "c:/users/default"; /* poor default */
609 # else
610 if (home_dir == NULL || home_dir[0] == '\0')
612 int success;
613 char *name;
614 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
616 if (buflen == 0)
617 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
618 a moderate value. */
619 buflen = 20;
620 name = alloca_account (buflen, alloca_used);
622 success = getlogin_r (name, buflen) == 0;
623 if (success)
625 struct passwd *p;
626 # if defined HAVE_GETPWNAM_R || defined _LIBC
627 long int pwbuflen = GETPW_R_SIZE_MAX ();
628 char *pwtmpbuf;
629 struct passwd pwbuf;
630 int malloc_pwtmpbuf = 0;
631 int save = errno;
633 # ifndef _LIBC
634 if (pwbuflen == -1)
635 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
636 Try a moderate value. */
637 pwbuflen = 1024;
638 # endif
639 if (__libc_use_alloca (alloca_used + pwbuflen))
640 pwtmpbuf = alloca_account (pwbuflen, alloca_used);
641 else
643 pwtmpbuf = malloc (pwbuflen);
644 if (pwtmpbuf == NULL)
646 retval = GLOB_NOSPACE;
647 goto out;
649 malloc_pwtmpbuf = 1;
652 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
653 != 0)
655 if (errno != ERANGE)
657 p = NULL;
658 break;
661 if (!malloc_pwtmpbuf
662 && __libc_use_alloca (alloca_used
663 + 2 * pwbuflen))
664 pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen,
665 2 * pwbuflen,
666 alloca_used);
667 else
669 char *newp = realloc (malloc_pwtmpbuf
670 ? pwtmpbuf : NULL,
671 2 * pwbuflen);
672 if (newp == NULL)
674 if (__glibc_unlikely (malloc_pwtmpbuf))
675 free (pwtmpbuf);
676 retval = GLOB_NOSPACE;
677 goto out;
679 pwtmpbuf = newp;
680 pwbuflen = 2 * pwbuflen;
681 malloc_pwtmpbuf = 1;
683 __set_errno (save);
685 # else
686 p = getpwnam (name);
687 # endif
688 if (p != NULL)
690 if (!malloc_pwtmpbuf)
691 home_dir = p->pw_dir;
692 else
694 size_t home_dir_len = strlen (p->pw_dir) + 1;
695 if (__libc_use_alloca (alloca_used + home_dir_len))
696 home_dir = alloca_account (home_dir_len,
697 alloca_used);
698 else
700 home_dir = malloc (home_dir_len);
701 if (home_dir == NULL)
703 free (pwtmpbuf);
704 retval = GLOB_NOSPACE;
705 goto out;
707 malloc_home_dir = 1;
709 memcpy (home_dir, p->pw_dir, home_dir_len);
711 free (pwtmpbuf);
716 if (home_dir == NULL || home_dir[0] == '\0')
718 if (flags & GLOB_TILDE_CHECK)
720 if (__glibc_unlikely (malloc_home_dir))
721 free (home_dir);
722 retval = GLOB_NOMATCH;
723 goto out;
725 else
726 home_dir = (char *) "~"; /* No luck. */
728 # endif /* WINDOWS32 */
729 # endif
730 /* Now construct the full directory. */
731 if (dirname[1] == '\0')
733 if (__glibc_unlikely (malloc_dirname))
734 free (dirname);
736 dirname = home_dir;
737 dirlen = strlen (dirname);
738 malloc_dirname = malloc_home_dir;
740 else
742 char *newp;
743 size_t home_len = strlen (home_dir);
744 int use_alloca = __libc_use_alloca (alloca_used
745 + home_len + dirlen);
746 if (use_alloca)
747 newp = alloca_account (home_len + dirlen, alloca_used);
748 else
750 newp = malloc (home_len + dirlen);
751 if (newp == NULL)
753 if (__glibc_unlikely (malloc_home_dir))
754 free (home_dir);
755 retval = GLOB_NOSPACE;
756 goto out;
760 mempcpy (mempcpy (newp, home_dir, home_len),
761 &dirname[1], dirlen);
763 if (__glibc_unlikely (malloc_dirname))
764 free (dirname);
766 dirname = newp;
767 dirlen += home_len - 1;
768 malloc_dirname = !use_alloca;
770 dirname_modified = 1;
772 # if !defined _AMIGA && !defined WINDOWS32
773 else
775 char *end_name = strchr (dirname, '/');
776 char *user_name;
777 int malloc_user_name = 0;
778 char *unescape = NULL;
780 if (!(flags & GLOB_NOESCAPE))
782 if (end_name == NULL)
784 unescape = strchr (dirname, '\\');
785 if (unescape)
786 end_name = strchr (unescape, '\0');
788 else
789 unescape = memchr (dirname, '\\', end_name - dirname);
791 if (end_name == NULL)
792 user_name = dirname + 1;
793 else
795 char *newp;
796 if (__libc_use_alloca (alloca_used + (end_name - dirname)))
797 newp = alloca_account (end_name - dirname, alloca_used);
798 else
800 newp = malloc (end_name - dirname);
801 if (newp == NULL)
803 retval = GLOB_NOSPACE;
804 goto out;
806 malloc_user_name = 1;
808 if (unescape != NULL)
810 char *p = mempcpy (newp, dirname + 1,
811 unescape - dirname - 1);
812 char *q = unescape;
813 while (*q != '\0')
815 if (*q == '\\')
817 if (q[1] == '\0')
819 /* "~fo\\o\\" unescape to user_name "foo\\",
820 but "~fo\\o\\/" unescape to user_name
821 "foo". */
822 if (filename == NULL)
823 *p++ = '\\';
824 break;
826 ++q;
828 *p++ = *q++;
830 *p = '\0';
832 else
833 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
834 = '\0';
835 user_name = newp;
838 /* Look up specific user's home directory. */
840 struct passwd *p;
841 # if defined HAVE_GETPWNAM_R || defined _LIBC
842 long int buflen = GETPW_R_SIZE_MAX ();
843 char *pwtmpbuf;
844 int malloc_pwtmpbuf = 0;
845 struct passwd pwbuf;
846 int save = errno;
848 # ifndef _LIBC
849 if (buflen == -1)
850 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
851 moderate value. */
852 buflen = 1024;
853 # endif
854 if (__libc_use_alloca (alloca_used + buflen))
855 pwtmpbuf = alloca_account (buflen, alloca_used);
856 else
858 pwtmpbuf = malloc (buflen);
859 if (pwtmpbuf == NULL)
861 nomem_getpw:
862 if (__glibc_unlikely (malloc_user_name))
863 free (user_name);
864 retval = GLOB_NOSPACE;
865 goto out;
867 malloc_pwtmpbuf = 1;
870 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
872 if (errno != ERANGE)
874 p = NULL;
875 break;
877 if (!malloc_pwtmpbuf
878 && __libc_use_alloca (alloca_used + 2 * buflen))
879 pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen,
880 2 * buflen, alloca_used);
881 else
883 char *newp = realloc (malloc_pwtmpbuf ? pwtmpbuf : NULL,
884 2 * buflen);
885 if (newp == NULL)
887 if (__glibc_unlikely (malloc_pwtmpbuf))
888 free (pwtmpbuf);
889 goto nomem_getpw;
891 pwtmpbuf = newp;
892 malloc_pwtmpbuf = 1;
894 __set_errno (save);
896 # else
897 p = getpwnam (user_name);
898 # endif
900 if (__glibc_unlikely (malloc_user_name))
901 free (user_name);
903 /* If we found a home directory use this. */
904 if (p != NULL)
906 size_t home_len = strlen (p->pw_dir);
907 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
909 if (__glibc_unlikely (malloc_dirname))
910 free (dirname);
911 malloc_dirname = 0;
913 if (__libc_use_alloca (alloca_used + home_len + rest_len + 1))
914 dirname = alloca_account (home_len + rest_len + 1,
915 alloca_used);
916 else
918 dirname = malloc (home_len + rest_len + 1);
919 if (dirname == NULL)
921 if (__glibc_unlikely (malloc_pwtmpbuf))
922 free (pwtmpbuf);
923 retval = GLOB_NOSPACE;
924 goto out;
926 malloc_dirname = 1;
928 *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len),
929 end_name, rest_len)) = '\0';
931 dirlen = home_len + rest_len;
932 dirname_modified = 1;
934 if (__glibc_unlikely (malloc_pwtmpbuf))
935 free (pwtmpbuf);
937 else
939 if (__glibc_unlikely (malloc_pwtmpbuf))
940 free (pwtmpbuf);
942 if (flags & GLOB_TILDE_CHECK)
943 /* We have to regard it as an error if we cannot find the
944 home directory. */
945 return GLOB_NOMATCH;
949 # endif /* Not Amiga && not WINDOWS32. */
951 #endif /* Not VMS. */
953 /* Now test whether we looked for "~" or "~NAME". In this case we
954 can give the answer now. */
955 if (filename == NULL)
957 struct stat st;
958 struct_stat64 st64;
960 /* Return the directory if we don't check for error or if it exists. */
961 if ((flags & GLOB_NOCHECK)
962 || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
963 ? ((*pglob->gl_stat) (dirname, &st) == 0
964 && S_ISDIR (st.st_mode))
965 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
967 size_t newcount = pglob->gl_pathc + pglob->gl_offs;
968 char **new_gl_pathv;
970 if (newcount > UINTPTR_MAX - (1 + 1)
971 || newcount + 1 + 1 > ~((size_t) 0) / sizeof (char *))
973 nospace:
974 free (pglob->gl_pathv);
975 pglob->gl_pathv = NULL;
976 pglob->gl_pathc = 0;
977 return GLOB_NOSPACE;
980 new_gl_pathv
981 = (char **) realloc (pglob->gl_pathv,
982 (newcount + 1 + 1) * sizeof (char *));
983 if (new_gl_pathv == NULL)
984 goto nospace;
985 pglob->gl_pathv = new_gl_pathv;
987 if (flags & GLOB_MARK)
989 char *p;
990 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
991 if (pglob->gl_pathv[newcount] == NULL)
992 goto nospace;
993 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
994 p[0] = '/';
995 p[1] = '\0';
997 else
999 pglob->gl_pathv[newcount] = strdup (dirname);
1000 if (pglob->gl_pathv[newcount] == NULL)
1001 goto nospace;
1003 pglob->gl_pathv[++newcount] = NULL;
1004 ++pglob->gl_pathc;
1005 pglob->gl_flags = flags;
1007 return 0;
1010 /* Not found. */
1011 return GLOB_NOMATCH;
1014 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
1015 /* meta is 1 if correct glob pattern containing metacharacters.
1016 If meta has bit (1 << 2) set, it means there was an unterminated
1017 [ which we handle the same, using fnmatch. Broken unterminated
1018 pattern bracket expressions ought to be rare enough that it is
1019 not worth special casing them, fnmatch will do the right thing. */
1020 if (meta & 5)
1022 /* The directory name contains metacharacters, so we
1023 have to glob for the directory, and then glob for
1024 the pattern in each directory found. */
1025 size_t i;
1027 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
1029 /* "foo\\/bar". Remove the final backslash from dirname
1030 if it has not been quoted. */
1031 char *p = (char *) &dirname[dirlen - 1];
1033 while (p > dirname && p[-1] == '\\') --p;
1034 if ((&dirname[dirlen] - p) & 1)
1035 *(char *) &dirname[--dirlen] = '\0';
1038 if (__glibc_unlikely ((flags & GLOB_ALTDIRFUNC) != 0))
1040 /* Use the alternative access functions also in the recursive
1041 call. */
1042 dirs.gl_opendir = pglob->gl_opendir;
1043 dirs.gl_readdir = pglob->gl_readdir;
1044 dirs.gl_closedir = pglob->gl_closedir;
1045 dirs.gl_stat = pglob->gl_stat;
1046 dirs.gl_lstat = pglob->gl_lstat;
1049 status = glob (dirname,
1050 ((flags & (GLOB_ERR | GLOB_NOESCAPE
1051 | GLOB_ALTDIRFUNC))
1052 | GLOB_NOSORT | GLOB_ONLYDIR),
1053 errfunc, &dirs);
1054 if (status != 0)
1056 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
1057 return status;
1058 goto no_matches;
1061 /* We have successfully globbed the preceding directory name.
1062 For each name we found, call glob_in_dir on it and FILENAME,
1063 appending the results to PGLOB. */
1064 for (i = 0; i < dirs.gl_pathc; ++i)
1066 size_t old_pathc;
1068 #ifdef SHELL
1070 /* Make globbing interruptible in the bash shell. */
1071 extern int interrupt_state;
1073 if (interrupt_state)
1075 globfree (&dirs);
1076 return GLOB_ABORTED;
1079 #endif /* SHELL. */
1081 old_pathc = pglob->gl_pathc;
1082 status = glob_in_dir (filename, dirs.gl_pathv[i],
1083 ((flags | GLOB_APPEND)
1084 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
1085 errfunc, pglob, alloca_used);
1086 if (status == GLOB_NOMATCH)
1087 /* No matches in this directory. Try the next. */
1088 continue;
1090 if (status != 0)
1092 globfree (&dirs);
1093 globfree (pglob);
1094 pglob->gl_pathc = 0;
1095 return status;
1098 /* Stick the directory on the front of each name. */
1099 if (prefix_array (dirs.gl_pathv[i],
1100 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1101 pglob->gl_pathc - old_pathc))
1103 globfree (&dirs);
1104 globfree (pglob);
1105 pglob->gl_pathc = 0;
1106 return GLOB_NOSPACE;
1110 flags |= GLOB_MAGCHAR;
1112 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1113 But if we have not found any matching entry and the GLOB_NOCHECK
1114 flag was set we must return the input pattern itself. */
1115 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
1117 no_matches:
1118 /* No matches. */
1119 if (flags & GLOB_NOCHECK)
1121 size_t newcount = pglob->gl_pathc + pglob->gl_offs;
1122 char **new_gl_pathv;
1124 if (newcount > UINTPTR_MAX - 2
1125 || newcount + 2 > ~((size_t) 0) / sizeof (char *))
1127 nospace2:
1128 globfree (&dirs);
1129 return GLOB_NOSPACE;
1132 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
1133 (newcount + 2)
1134 * sizeof (char *));
1135 if (new_gl_pathv == NULL)
1136 goto nospace2;
1137 pglob->gl_pathv = new_gl_pathv;
1139 pglob->gl_pathv[newcount] = __strdup (pattern);
1140 if (pglob->gl_pathv[newcount] == NULL)
1142 globfree (&dirs);
1143 globfree (pglob);
1144 pglob->gl_pathc = 0;
1145 return GLOB_NOSPACE;
1148 ++pglob->gl_pathc;
1149 ++newcount;
1151 pglob->gl_pathv[newcount] = NULL;
1152 pglob->gl_flags = flags;
1154 else
1156 globfree (&dirs);
1157 return GLOB_NOMATCH;
1161 globfree (&dirs);
1163 else
1165 size_t old_pathc = pglob->gl_pathc;
1166 int orig_flags = flags;
1168 if (meta & 2)
1170 char *p = strchr (dirname, '\\'), *q;
1171 /* We need to unescape the dirname string. It is certainly
1172 allocated by alloca, as otherwise filename would be NULL
1173 or dirname wouldn't contain backslashes. */
1174 q = p;
1177 if (*p == '\\')
1179 *q = *++p;
1180 --dirlen;
1182 else
1183 *q = *p;
1184 ++q;
1186 while (*p++ != '\0');
1187 dirname_modified = 1;
1189 if (dirname_modified)
1190 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
1191 status = glob_in_dir (filename, dirname, flags, errfunc, pglob,
1192 alloca_used);
1193 if (status != 0)
1195 if (status == GLOB_NOMATCH && flags != orig_flags
1196 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1198 /* Make sure globfree (&dirs); is a nop. */
1199 dirs.gl_pathv = NULL;
1200 flags = orig_flags;
1201 goto no_matches;
1203 return status;
1206 if (dirlen > 0)
1208 /* Stick the directory on the front of each name. */
1209 if (prefix_array (dirname,
1210 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1211 pglob->gl_pathc - old_pathc))
1213 globfree (pglob);
1214 pglob->gl_pathc = 0;
1215 return GLOB_NOSPACE;
1220 if (flags & GLOB_MARK)
1222 /* Append slashes to directory names. */
1223 size_t i;
1224 struct stat st;
1225 struct_stat64 st64;
1227 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1228 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1229 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1230 && S_ISDIR (st.st_mode))
1231 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1232 && S_ISDIR (st64.st_mode))))
1234 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1235 char *new = realloc (pglob->gl_pathv[i], len);
1236 if (new == NULL)
1238 globfree (pglob);
1239 pglob->gl_pathc = 0;
1240 return GLOB_NOSPACE;
1242 strcpy (&new[len - 2], "/");
1243 pglob->gl_pathv[i] = new;
1247 if (!(flags & GLOB_NOSORT))
1249 /* Sort the vector. */
1250 qsort (&pglob->gl_pathv[oldcount],
1251 pglob->gl_pathc + pglob->gl_offs - oldcount,
1252 sizeof (char *), collated_compare);
1255 out:
1256 if (__glibc_unlikely (malloc_dirname))
1257 free (dirname);
1259 return retval;
1261 #if defined _LIBC && !defined glob
1262 libc_hidden_def (glob)
1263 #endif
1266 #if !defined _LIBC || !defined GLOB_ONLY_P
1268 /* Free storage allocated in PGLOB by a previous `glob' call. */
1269 void
1270 globfree (pglob)
1271 glob_t *pglob;
1273 if (pglob->gl_pathv != NULL)
1275 size_t i;
1276 for (i = 0; i < pglob->gl_pathc; ++i)
1277 free (pglob->gl_pathv[pglob->gl_offs + i]);
1278 free (pglob->gl_pathv);
1279 pglob->gl_pathv = NULL;
1282 #if defined _LIBC && !defined globfree
1283 libc_hidden_def (globfree)
1284 #endif
1287 /* Do a collated comparison of A and B. */
1288 static int
1289 collated_compare (const void *a, const void *b)
1291 const char *const s1 = *(const char *const * const) a;
1292 const char *const s2 = *(const char *const * const) b;
1294 if (s1 == s2)
1295 return 0;
1296 if (s1 == NULL)
1297 return 1;
1298 if (s2 == NULL)
1299 return -1;
1300 return strcoll (s1, s2);
1304 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1305 elements in place. Return nonzero if out of memory, zero if successful.
1306 A slash is inserted between DIRNAME and each elt of ARRAY,
1307 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1308 static int
1309 prefix_array (const char *dirname, char **array, size_t n)
1311 size_t i;
1312 size_t dirlen = strlen (dirname);
1313 #if defined __MSDOS__ || defined WINDOWS32
1314 int sep_char = '/';
1315 # define DIRSEP_CHAR sep_char
1316 #else
1317 # define DIRSEP_CHAR '/'
1318 #endif
1320 if (dirlen == 1 && dirname[0] == '/')
1321 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1322 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1323 dirlen = 0;
1324 #if defined __MSDOS__ || defined WINDOWS32
1325 else if (dirlen > 1)
1327 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1328 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1329 --dirlen;
1330 else if (dirname[dirlen - 1] == ':')
1332 /* DIRNAME is "d:". Use `:' instead of `/'. */
1333 --dirlen;
1334 sep_char = ':';
1337 #endif
1339 for (i = 0; i < n; ++i)
1341 size_t eltlen = strlen (array[i]) + 1;
1342 char *new = (char *) malloc (dirlen + 1 + eltlen);
1343 if (new == NULL)
1345 while (i > 0)
1346 free (array[--i]);
1347 return 1;
1351 char *endp = mempcpy (new, dirname, dirlen);
1352 *endp++ = DIRSEP_CHAR;
1353 mempcpy (endp, array[i], eltlen);
1355 free (array[i]);
1356 array[i] = new;
1359 return 0;
1363 /* We must not compile this function twice. */
1364 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1366 __glob_pattern_type (pattern, quote)
1367 const char *pattern;
1368 int quote;
1370 const char *p;
1371 int ret = 0;
1373 for (p = pattern; *p != '\0'; ++p)
1374 switch (*p)
1376 case '?':
1377 case '*':
1378 return 1;
1380 case '\\':
1381 if (quote)
1383 if (p[1] != '\0')
1384 ++p;
1385 ret |= 2;
1387 break;
1389 case '[':
1390 ret |= 4;
1391 break;
1393 case ']':
1394 if (ret & 4)
1395 return 1;
1396 break;
1399 return ret;
1402 /* Return nonzero if PATTERN contains any metacharacters.
1403 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1405 __glob_pattern_p (pattern, quote)
1406 const char *pattern;
1407 int quote;
1409 return __glob_pattern_type (pattern, quote) == 1;
1411 # ifdef _LIBC
1412 weak_alias (__glob_pattern_p, glob_pattern_p)
1413 # endif
1414 #endif
1416 #endif /* !GLOB_ONLY_P */
1419 /* We put this in a separate function mainly to allow the memory
1420 allocated with alloca to be recycled. */
1421 #if !defined _LIBC || !defined GLOB_ONLY_P
1422 static int
1423 __attribute_noinline__
1424 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
1425 glob_t *pglob
1426 # ifndef _LIBC
1427 , int flags
1428 # endif
1431 size_t fnamelen = strlen (fname);
1432 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1433 struct stat st;
1434 # ifndef _LIBC
1435 struct_stat64 st64;
1436 # endif
1438 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1439 fname, fnamelen + 1);
1441 # ifdef _LIBC
1442 return (*pglob->gl_stat) (fullname, &st) == 0;
1443 # else
1444 return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1445 ? (*pglob->gl_stat) (fullname, &st)
1446 : __stat64 (fullname, &st64)) == 0);
1447 # endif
1449 # ifdef _LIBC
1450 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1451 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1452 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1453 : ({ struct stat64 st64; \
1454 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1455 # else
1456 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1457 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1458 # endif
1459 #endif
1462 /* Like `glob', but PATTERN is a final pathname component,
1463 and matches are searched for in DIRECTORY.
1464 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1465 The GLOB_APPEND flag is assumed to be set (always appends). */
1466 static int
1467 glob_in_dir (const char *pattern, const char *directory, int flags,
1468 int (*errfunc) (const char *, int),
1469 glob_t *pglob, size_t alloca_used)
1471 size_t dirlen = strlen (directory);
1472 void *stream = NULL;
1473 struct globnames
1475 struct globnames *next;
1476 size_t count;
1477 char *name[64];
1479 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1480 struct globnames init_names;
1481 struct globnames *names = &init_names;
1482 struct globnames *names_alloca = &init_names;
1483 size_t nfound = 0;
1484 size_t cur = 0;
1485 int meta;
1486 int save;
1488 alloca_used += sizeof (init_names);
1490 init_names.next = NULL;
1491 init_names.count = INITIAL_COUNT;
1493 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1494 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1496 /* We need not do any tests. The PATTERN contains no meta
1497 characters and we must not return an error therefore the
1498 result will always contain exactly one name. */
1499 flags |= GLOB_NOCHECK;
1501 else if (meta == 0)
1503 /* Since we use the normal file functions we can also use stat()
1504 to verify the file is there. */
1505 union
1507 struct stat st;
1508 struct_stat64 st64;
1509 } ust;
1510 size_t patlen = strlen (pattern);
1511 int alloca_fullname = __libc_use_alloca (alloca_used
1512 + dirlen + 1 + patlen + 1);
1513 char *fullname;
1514 if (alloca_fullname)
1515 fullname = alloca_account (dirlen + 1 + patlen + 1, alloca_used);
1516 else
1518 fullname = malloc (dirlen + 1 + patlen + 1);
1519 if (fullname == NULL)
1520 return GLOB_NOSPACE;
1523 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1524 "/", 1),
1525 pattern, patlen + 1);
1526 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1527 ? (*pglob->gl_stat) (fullname, &ust.st)
1528 : __stat64 (fullname, &ust.st64)) == 0)
1529 /* We found this file to be existing. Now tell the rest
1530 of the function to copy this name into the result. */
1531 flags |= GLOB_NOCHECK;
1533 if (__glibc_unlikely (!alloca_fullname))
1534 free (fullname);
1536 else
1538 stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1539 ? (*pglob->gl_opendir) (directory)
1540 : opendir (directory));
1541 if (stream == NULL)
1543 if (errno != ENOTDIR
1544 && ((errfunc != NULL && (*errfunc) (directory, errno))
1545 || (flags & GLOB_ERR)))
1546 return GLOB_ABORTED;
1548 else
1550 #ifdef _LIBC
1551 int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1552 ? -1 : dirfd ((DIR *) stream));
1553 #endif
1554 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1555 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1556 #if defined _AMIGA || defined VMS
1557 | FNM_CASEFOLD
1558 #endif
1560 flags |= GLOB_MAGCHAR;
1562 while (1)
1564 const char *name;
1565 size_t len;
1566 #if defined _LIBC && !defined COMPILE_GLOB64
1567 struct dirent64 *d;
1568 union
1570 struct dirent64 d64;
1571 char room [offsetof (struct dirent64, d_name[0])
1572 + NAME_MAX + 1];
1574 d64buf;
1576 if (__glibc_unlikely (flags & GLOB_ALTDIRFUNC))
1578 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1579 if (d32 != NULL)
1581 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1582 d = &d64buf.d64;
1584 else
1585 d = NULL;
1587 else
1588 d = __readdir64 (stream);
1589 #else
1590 struct dirent *d = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1591 ? ((struct dirent *)
1592 (*pglob->gl_readdir) (stream))
1593 : __readdir (stream));
1594 #endif
1595 if (d == NULL)
1596 break;
1597 if (! REAL_DIR_ENTRY (d))
1598 continue;
1600 /* If we shall match only directories use the information
1601 provided by the dirent call if possible. */
1602 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1603 continue;
1605 name = d->d_name;
1607 if (fnmatch (pattern, name, fnm_flags) == 0)
1609 /* If the file we found is a symlink we have to
1610 make sure the target file exists. */
1611 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1612 || link_exists_p (dfd, directory, dirlen, name, pglob,
1613 flags))
1615 if (cur == names->count)
1617 struct globnames *newnames;
1618 size_t count = names->count * 2;
1619 size_t size = (sizeof (struct globnames)
1620 + ((count - INITIAL_COUNT)
1621 * sizeof (char *)));
1622 if (__libc_use_alloca (alloca_used + size))
1623 newnames = names_alloca
1624 = alloca_account (size, alloca_used);
1625 else if ((newnames = malloc (size))
1626 == NULL)
1627 goto memory_error;
1628 newnames->count = count;
1629 newnames->next = names;
1630 names = newnames;
1631 cur = 0;
1633 len = NAMLEN (d);
1634 names->name[cur] = (char *) malloc (len + 1);
1635 if (names->name[cur] == NULL)
1636 goto memory_error;
1637 *((char *) mempcpy (names->name[cur++], name, len))
1638 = '\0';
1639 ++nfound;
1646 if (nfound == 0 && (flags & GLOB_NOCHECK))
1648 size_t len = strlen (pattern);
1649 nfound = 1;
1650 names->name[cur] = (char *) malloc (len + 1);
1651 if (names->name[cur] == NULL)
1652 goto memory_error;
1653 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1656 int result = GLOB_NOMATCH;
1657 if (nfound != 0)
1659 result = 0;
1661 if (pglob->gl_pathc > UINTPTR_MAX - pglob->gl_offs
1662 || pglob->gl_pathc + pglob->gl_offs > UINTPTR_MAX - nfound
1663 || pglob->gl_pathc + pglob->gl_offs + nfound > UINTPTR_MAX - 1
1664 || (pglob->gl_pathc + pglob->gl_offs + nfound + 1
1665 > UINTPTR_MAX / sizeof (char *)))
1666 goto memory_error;
1668 char **new_gl_pathv;
1669 new_gl_pathv
1670 = (char **) realloc (pglob->gl_pathv,
1671 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1672 * sizeof (char *));
1673 if (new_gl_pathv == NULL)
1675 memory_error:
1676 while (1)
1678 struct globnames *old = names;
1679 for (size_t i = 0; i < cur; ++i)
1680 free (names->name[i]);
1681 names = names->next;
1682 /* NB: we will not leak memory here if we exit without
1683 freeing the current block assigned to OLD. At least
1684 the very first block is always allocated on the stack
1685 and this is the block assigned to OLD here. */
1686 if (names == NULL)
1688 assert (old == &init_names);
1689 break;
1691 cur = names->count;
1692 if (old == names_alloca)
1693 names_alloca = names;
1694 else
1695 free (old);
1697 result = GLOB_NOSPACE;
1699 else
1701 while (1)
1703 struct globnames *old = names;
1704 for (size_t i = 0; i < cur; ++i)
1705 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1706 = names->name[i];
1707 names = names->next;
1708 /* NB: we will not leak memory here if we exit without
1709 freeing the current block assigned to OLD. At least
1710 the very first block is always allocated on the stack
1711 and this is the block assigned to OLD here. */
1712 if (names == NULL)
1714 assert (old == &init_names);
1715 break;
1717 cur = names->count;
1718 if (old == names_alloca)
1719 names_alloca = names;
1720 else
1721 free (old);
1724 pglob->gl_pathv = new_gl_pathv;
1726 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1728 pglob->gl_flags = flags;
1732 if (stream != NULL)
1734 save = errno;
1735 if (__glibc_unlikely (flags & GLOB_ALTDIRFUNC))
1736 (*pglob->gl_closedir) (stream);
1737 else
1738 closedir (stream);
1739 __set_errno (save);
1742 return result;