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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/types.h>
31 /* Outcomment the following line for production quality code. */
32 /* #define NDEBUG 1 */
35 #include <stdio.h> /* Needed on stupid SunOS for assert. */
37 #if !defined _LIBC || !defined GLOB_ONLY_P
38 #if defined HAVE_UNISTD_H || defined _LIBC
41 # ifdef _POSIX_VERSION
51 # define __set_errno(val) errno = (val)
54 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
56 # define NAMLEN(dirent) strlen((dirent)->d_name)
58 # define dirent direct
59 # define NAMLEN(dirent) (dirent)->d_namlen
60 # ifdef HAVE_SYS_NDIR_H
61 # include <sys/ndir.h>
63 # ifdef HAVE_SYS_DIR_H
71 # endif /* HAVE_VMSDIR_H */
75 /* In GNU systems, <dirent.h> defines this macro for us. */
78 # define NAMLEN(d) _D_NAMLEN(d)
81 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
82 if the `d_type' member for `struct dirent' is available.
83 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
84 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
85 /* True if the directory entry D must be of type T. */
86 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
88 /* True if the directory entry D might be a symbolic link. */
89 # define DIRENT_MIGHT_BE_SYMLINK(d) \
90 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
92 /* True if the directory entry D might be a directory. */
93 # define DIRENT_MIGHT_BE_DIR(d) \
94 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
96 #else /* !HAVE_D_TYPE */
97 # define DIRENT_MUST_BE(d, t) false
98 # define DIRENT_MIGHT_BE_SYMLINK(d) true
99 # define DIRENT_MIGHT_BE_DIR(d) true
100 #endif /* HAVE_D_TYPE */
102 /* If the system has the `struct dirent64' type we use it internally. */
103 #if defined _LIBC && !defined COMPILE_GLOB64
104 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
105 # define CONVERT_D_NAMLEN(d64, d32)
107 # define CONVERT_D_NAMLEN(d64, d32) \
108 (d64)->d_namlen = (d32)->d_namlen;
111 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
112 # define CONVERT_D_INO(d64, d32)
114 # define CONVERT_D_INO(d64, d32) \
115 (d64)->d_ino = (d32)->d_ino;
118 # ifdef _DIRENT_HAVE_D_TYPE
119 # define CONVERT_D_TYPE(d64, d32) \
120 (d64)->d_type = (d32)->d_type;
122 # define CONVERT_D_TYPE(d64, d32)
125 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
126 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
127 CONVERT_D_NAMLEN (d64, d32) \
128 CONVERT_D_INO (d64, d32) \
129 CONVERT_D_TYPE (d64, d32)
133 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
134 /* Posix does not require that the d_ino field be present, and some
135 systems do not provide it. */
136 # define REAL_DIR_ENTRY(dp) 1
138 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
144 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
147 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
154 # define strdup(str) __strdup (str)
155 # define sysconf(id) __sysconf (id)
156 # define closedir(dir) __closedir (dir)
157 # define opendir(name) __opendir (name)
158 # define readdir(str) __readdir64 (str)
159 # define getpwnam_r(name, bufp, buf, len, res) \
160 __getpwnam_r (name, bufp, buf, len, res)
162 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
164 # define struct_stat64 struct stat64
166 # include "getlogin_r.h"
167 # include "mempcpy.h"
168 # include "stat-macros.h"
170 # define __stat64(fname, buf) stat (fname, buf)
171 # define struct_stat64 struct stat
172 # define __stat(fname, buf) stat (fname, buf)
173 # define __alloca alloca
174 # define __readdir readdir
175 # define __readdir64 readdir64
176 # define __glob_pattern_p glob_pattern_p
181 #ifdef _SC_GETPW_R_SIZE_MAX
182 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
184 # define GETPW_R_SIZE_MAX() (-1)
186 #ifdef _SC_LOGIN_NAME_MAX
187 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
189 # define GET_LOGIN_NAME_MAX() (-1)
192 static const char *next_brace_sub (const char *begin, int flags) __THROW;
194 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
196 #ifndef attribute_hidden
197 # define attribute_hidden
200 static int glob_in_dir (const char *pattern, const char *directory,
201 int flags, int (*errfunc) (const char *, int),
202 glob_t *pglob, size_t alloca_used);
203 extern int __glob_pattern_type (const char *pattern, int quote)
206 #if !defined _LIBC || !defined GLOB_ONLY_P
207 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
208 static int collated_compare (const void *, const void *) __THROW;
211 /* Find the end of the sub-pattern in a brace expression. */
213 next_brace_sub (const char *cp, int flags)
215 unsigned int depth = 0;
217 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
225 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
232 return *cp != '\0' ? cp : NULL;
235 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
237 /* Do glob searching for PATTERN, placing results in PGLOB.
238 The bits defined above may be set in FLAGS.
239 If a directory cannot be opened or read and ERRFUNC is not nil,
240 it is called with the pathname that caused the error, and the
241 `errno' value from the failing call; if it returns non-zero
242 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
243 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
244 Otherwise, `glob' returns zero. */
246 #ifdef GLOB_ATTRIBUTE
249 glob (pattern, flags, errfunc, pglob)
252 int (*errfunc) (const char *, int);
255 const char *filename;
256 char *dirname = NULL;
261 int dirname_modified;
262 int malloc_dirname = 0;
266 size_t alloca_used = 0;
269 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
271 __set_errno (EINVAL);
275 if (!(flags & GLOB_DOOFFS))
276 /* Have to do this so `globfree' knows where to start freeing. It
277 also makes all the code that uses gl_offs simpler. */
280 if (flags & GLOB_BRACE)
284 if (flags & GLOB_NOESCAPE)
285 begin = strchr (pattern, '{');
297 if (*begin == '\\' && begin[1] != '\0')
299 else if (*begin == '{')
308 /* Allocate working buffer large enough for our work. Note that
309 we have at least an opening and closing brace. */
317 size_t pattern_len = strlen (pattern) - 1;
319 int alloca_onealt = __libc_use_alloca (alloca_used + pattern_len);
321 onealt = alloca_account (pattern_len, alloca_used);
325 onealt = (char *) malloc (pattern_len);
328 if (!(flags & GLOB_APPEND))
331 pglob->gl_pathv = NULL;
337 /* We know the prefix for all sub-patterns. */
338 alt_start = mempcpy (onealt, pattern, begin - pattern);
340 /* Find the first sub-pattern and at the same time find the
341 rest after the closing brace. */
342 next = next_brace_sub (begin + 1, flags);
345 /* It is an illegal expression. */
348 if (__builtin_expect (!alloca_onealt, 0))
351 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
354 /* Now find the end of the whole brace expression. */
358 rest = next_brace_sub (rest + 1, flags);
360 /* It is an illegal expression. */
363 /* Please note that we now can be sure the brace expression
365 rest_len = strlen (++rest) + 1;
367 /* We have a brace expression. BEGIN points to the opening {,
368 NEXT points past the terminator of the first element, and END
369 points past the final }. We will accumulate result names from
370 recursive runs for each brace alternative in the buffer using
373 if (!(flags & GLOB_APPEND))
375 /* This call is to set a new vector, so clear out the
376 vector so we can append to it. */
378 pglob->gl_pathv = NULL;
380 firstc = pglob->gl_pathc;
387 /* Construct the new glob expression. */
388 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
390 result = glob (onealt,
391 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
392 | GLOB_APPEND), errfunc, pglob);
394 /* If we got an error, return it. */
395 if (result && result != GLOB_NOMATCH)
398 if (__builtin_expect (!alloca_onealt, 0))
401 if (!(flags & GLOB_APPEND))
410 /* We saw the last entry. */
414 next = next_brace_sub (p, flags);
415 assert (next != NULL);
419 if (__builtin_expect (!alloca_onealt, 0))
423 if (pglob->gl_pathc != firstc)
424 /* We found some entries. */
426 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
431 if (!(flags & GLOB_APPEND))
434 if (!(flags & GLOB_DOOFFS))
435 pglob->gl_pathv = NULL;
439 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
441 if (pglob->gl_pathv == NULL)
444 for (i = 0; i <= pglob->gl_offs; ++i)
445 pglob->gl_pathv[i] = NULL;
449 oldcount = pglob->gl_pathc + pglob->gl_offs;
451 /* Find the filename. */
452 filename = strrchr (pattern, '/');
453 #if defined __MSDOS__ || defined WINDOWS32
454 /* The case of "d:pattern". Since `:' is not allowed in
455 file names, we can safely assume that wherever it
456 happens in pattern, it signals the filename part. This
457 is so we could some day support patterns like "[a-z]:foo". */
458 if (filename == NULL)
459 filename = strchr (pattern, ':');
460 #endif /* __MSDOS__ || WINDOWS32 */
461 dirname_modified = 0;
462 if (filename == NULL)
464 /* This can mean two things: a simple name or "~name". The latter
465 case is nothing but a notation for a directory. */
466 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
468 dirname = (char *) pattern;
469 dirlen = strlen (pattern);
471 /* Set FILENAME to NULL as a special flag. This is ugly but
472 other solutions would require much more code. We test for
473 this special case below. */
478 if (__builtin_expect (pattern[0] == '\0', 0))
480 dirs.gl_pathv = NULL;
486 dirname = (char *) "";
488 dirname = (char *) ".";
493 else if (filename == pattern
494 || (filename == pattern + 1 && pattern[0] == '\\'
495 && (flags & GLOB_NOESCAPE) == 0))
497 /* "/pattern" or "\\/pattern". */
498 dirname = (char *) "/";
505 dirlen = filename - pattern;
506 #if defined __MSDOS__ || defined WINDOWS32
508 || (filename > pattern + 1 && filename[-1] == ':'))
513 drive_spec = (char *) __alloca (dirlen + 1);
514 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
515 /* For now, disallow wildcards in the drive spec, to
516 prevent infinite recursion in glob. */
517 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
519 /* If this is "d:pattern", we need to copy `:' to DIRNAME
520 as well. If it's "d:/pattern", don't remove the slash
521 from "d:/", since "d:" and "d:/" are not the same.*/
525 if (__libc_use_alloca (alloca_used + dirlen + 1))
526 newp = alloca_account (dirlen + 1, alloca_used);
530 newp = malloc (dirlen + 1);
535 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
539 if (filename[0] == '\0'
540 #if defined __MSDOS__ || defined WINDOWS32
541 && dirname[dirlen - 1] != ':'
542 && (dirlen < 3 || dirname[dirlen - 2] != ':'
543 || dirname[dirlen - 1] != '/')
546 /* "pattern/". Expand "pattern", appending slashes. */
548 int orig_flags = flags;
549 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
551 /* "pattern\\/". Remove the final backslash if it hasn't
553 char *p = (char *) &dirname[dirlen - 1];
555 while (p > dirname && p[-1] == '\\') --p;
556 if ((&dirname[dirlen] - p) & 1)
558 *(char *) &dirname[--dirlen] = '\0';
559 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
562 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
564 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
565 | (flags & GLOB_MARK));
566 else if (val == GLOB_NOMATCH && flags != orig_flags)
568 /* Make sure globfree (&dirs); is a nop. */
569 dirs.gl_pathv = NULL;
571 oldcount = pglob->gl_pathc + pglob->gl_offs;
580 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
582 if (dirname[1] == '\0' || dirname[1] == '/'
583 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
584 && (dirname[2] == '\0' || dirname[2] == '/')))
586 /* Look up home directory. */
587 char *home_dir = getenv ("HOME");
588 int malloc_home_dir = 0;
590 if (home_dir == NULL || home_dir[0] == '\0')
594 if (home_dir == NULL || home_dir[0] == '\0')
595 home_dir = "c:/users/default"; /* poor default */
597 if (home_dir == NULL || home_dir[0] == '\0')
601 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
604 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
607 name = alloca_account (buflen, alloca_used);
609 success = getlogin_r (name, buflen) == 0;
613 # if defined HAVE_GETPWNAM_R || defined _LIBC
614 long int pwbuflen = GETPW_R_SIZE_MAX ();
617 int malloc_pwtmpbuf = 0;
622 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
623 Try a moderate value. */
626 if (__libc_use_alloca (alloca_used + pwbuflen))
627 pwtmpbuf = alloca_account (pwbuflen, alloca_used);
630 pwtmpbuf = malloc (pwbuflen);
631 if (pwtmpbuf == NULL)
633 retval = GLOB_NOSPACE;
639 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
649 && __libc_use_alloca (alloca_used
651 pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen,
656 char *newp = realloc (malloc_pwtmpbuf
661 if (__builtin_expect (malloc_pwtmpbuf, 0))
663 retval = GLOB_NOSPACE;
667 pwbuflen = 2 * pwbuflen;
677 if (!malloc_pwtmpbuf)
678 home_dir = p->pw_dir;
681 size_t home_dir_len = strlen (p->pw_dir) + 1;
682 if (__libc_use_alloca (alloca_used + home_dir_len))
683 home_dir = alloca_account (home_dir_len,
687 home_dir = malloc (home_dir_len);
688 if (home_dir == NULL)
691 retval = GLOB_NOSPACE;
696 memcpy (home_dir, p->pw_dir, home_dir_len);
703 if (home_dir == NULL || home_dir[0] == '\0')
705 if (flags & GLOB_TILDE_CHECK)
707 if (__builtin_expect (malloc_home_dir, 0))
709 retval = GLOB_NOMATCH;
713 home_dir = (char *) "~"; /* No luck. */
715 # endif /* WINDOWS32 */
717 /* Now construct the full directory. */
718 if (dirname[1] == '\0')
720 if (__builtin_expect (malloc_dirname, 0))
724 dirlen = strlen (dirname);
725 malloc_dirname = malloc_home_dir;
730 size_t home_len = strlen (home_dir);
731 int use_alloca = __libc_use_alloca (alloca_used
732 + home_len + dirlen);
734 newp = alloca_account (home_len + dirlen, alloca_used);
737 newp = malloc (home_len + dirlen);
740 if (__builtin_expect (malloc_home_dir, 0))
742 retval = GLOB_NOSPACE;
747 mempcpy (mempcpy (newp, home_dir, home_len),
748 &dirname[1], dirlen);
750 if (__builtin_expect (malloc_dirname, 0))
754 dirlen += home_len - 1;
755 malloc_dirname = !use_alloca;
757 dirname_modified = 1;
759 # if !defined _AMIGA && !defined WINDOWS32
762 char *end_name = strchr (dirname, '/');
764 int malloc_user_name = 0;
765 char *unescape = NULL;
767 if (!(flags & GLOB_NOESCAPE))
769 if (end_name == NULL)
771 unescape = strchr (dirname, '\\');
773 end_name = strchr (unescape, '\0');
776 unescape = memchr (dirname, '\\', end_name - dirname);
778 if (end_name == NULL)
779 user_name = dirname + 1;
783 if (__libc_use_alloca (alloca_used + (end_name - dirname)))
784 newp = alloca_account (end_name - dirname, alloca_used);
787 newp = malloc (end_name - dirname);
790 retval = GLOB_NOSPACE;
793 malloc_user_name = 1;
795 if (unescape != NULL)
797 char *p = mempcpy (newp, dirname + 1,
798 unescape - dirname - 1);
806 /* "~fo\\o\\" unescape to user_name "foo\\",
807 but "~fo\\o\\/" unescape to user_name
809 if (filename == NULL)
820 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
825 /* Look up specific user's home directory. */
828 # if defined HAVE_GETPWNAM_R || defined _LIBC
829 long int buflen = GETPW_R_SIZE_MAX ();
831 int malloc_pwtmpbuf = 0;
837 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
841 if (__libc_use_alloca (alloca_used + buflen))
842 pwtmpbuf = alloca_account (buflen, alloca_used);
845 pwtmpbuf = malloc (buflen);
846 if (pwtmpbuf == NULL)
849 if (__builtin_expect (malloc_user_name, 0))
851 retval = GLOB_NOSPACE;
857 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
865 && __libc_use_alloca (alloca_used + 2 * buflen))
866 pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen,
867 2 * buflen, alloca_used);
870 char *newp = realloc (malloc_pwtmpbuf ? pwtmpbuf : NULL,
874 if (__builtin_expect (malloc_pwtmpbuf, 0))
884 p = getpwnam (user_name);
887 if (__builtin_expect (malloc_user_name, 0))
890 /* If we found a home directory use this. */
893 size_t home_len = strlen (p->pw_dir);
894 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
896 if (__builtin_expect (malloc_dirname, 0))
900 if (__libc_use_alloca (alloca_used + home_len + rest_len + 1))
901 dirname = alloca_account (home_len + rest_len + 1,
905 dirname = malloc (home_len + rest_len + 1);
908 if (__builtin_expect (malloc_pwtmpbuf, 0))
910 retval = GLOB_NOSPACE;
915 *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len),
916 end_name, rest_len)) = '\0';
918 dirlen = home_len + rest_len;
919 dirname_modified = 1;
921 if (__builtin_expect (malloc_pwtmpbuf, 0))
926 if (__builtin_expect (malloc_pwtmpbuf, 0))
929 if (flags & GLOB_TILDE_CHECK)
930 /* We have to regard it as an error if we cannot find the
936 # endif /* Not Amiga && not WINDOWS32. */
938 #endif /* Not VMS. */
940 /* Now test whether we looked for "~" or "~NAME". In this case we
941 can give the answer now. */
942 if (filename == NULL)
947 /* Return the directory if we don't check for error or if it exists. */
948 if ((flags & GLOB_NOCHECK)
949 || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
950 ? ((*pglob->gl_stat) (dirname, &st) == 0
951 && S_ISDIR (st.st_mode))
952 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
954 int newcount = pglob->gl_pathc + pglob->gl_offs;
958 = (char **) realloc (pglob->gl_pathv,
959 (newcount + 1 + 1) * sizeof (char *));
960 if (new_gl_pathv == NULL)
963 free (pglob->gl_pathv);
964 pglob->gl_pathv = NULL;
968 pglob->gl_pathv = new_gl_pathv;
970 if (flags & GLOB_MARK)
973 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
974 if (pglob->gl_pathv[newcount] == NULL)
976 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
982 pglob->gl_pathv[newcount] = strdup (dirname);
983 if (pglob->gl_pathv[newcount] == NULL)
986 pglob->gl_pathv[++newcount] = NULL;
988 pglob->gl_flags = flags;
997 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
998 /* meta is 1 if correct glob pattern containing metacharacters.
999 If meta has bit (1 << 2) set, it means there was an unterminated
1000 [ which we handle the same, using fnmatch. Broken unterminated
1001 pattern bracket expressions ought to be rare enough that it is
1002 not worth special casing them, fnmatch will do the right thing. */
1005 /* The directory name contains metacharacters, so we
1006 have to glob for the directory, and then glob for
1007 the pattern in each directory found. */
1010 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
1012 /* "foo\\/bar". Remove the final backslash from dirname
1013 if it has not been quoted. */
1014 char *p = (char *) &dirname[dirlen - 1];
1016 while (p > dirname && p[-1] == '\\') --p;
1017 if ((&dirname[dirlen] - p) & 1)
1018 *(char *) &dirname[--dirlen] = '\0';
1021 if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) != 0, 0))
1023 /* Use the alternative access functions also in the recursive
1025 dirs.gl_opendir = pglob->gl_opendir;
1026 dirs.gl_readdir = pglob->gl_readdir;
1027 dirs.gl_closedir = pglob->gl_closedir;
1028 dirs.gl_stat = pglob->gl_stat;
1029 dirs.gl_lstat = pglob->gl_lstat;
1032 status = glob (dirname,
1033 ((flags & (GLOB_ERR | GLOB_NOESCAPE
1035 | GLOB_NOSORT | GLOB_ONLYDIR),
1039 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
1044 /* We have successfully globbed the preceding directory name.
1045 For each name we found, call glob_in_dir on it and FILENAME,
1046 appending the results to PGLOB. */
1047 for (i = 0; i < dirs.gl_pathc; ++i)
1053 /* Make globbing interruptible in the bash shell. */
1054 extern int interrupt_state;
1056 if (interrupt_state)
1059 return GLOB_ABORTED;
1064 old_pathc = pglob->gl_pathc;
1065 status = glob_in_dir (filename, dirs.gl_pathv[i],
1066 ((flags | GLOB_APPEND)
1067 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
1068 errfunc, pglob, alloca_used);
1069 if (status == GLOB_NOMATCH)
1070 /* No matches in this directory. Try the next. */
1077 pglob->gl_pathc = 0;
1081 /* Stick the directory on the front of each name. */
1082 if (prefix_array (dirs.gl_pathv[i],
1083 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1084 pglob->gl_pathc - old_pathc))
1088 pglob->gl_pathc = 0;
1089 return GLOB_NOSPACE;
1093 flags |= GLOB_MAGCHAR;
1095 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1096 But if we have not found any matching entry and the GLOB_NOCHECK
1097 flag was set we must return the input pattern itself. */
1098 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
1102 if (flags & GLOB_NOCHECK)
1104 int newcount = pglob->gl_pathc + pglob->gl_offs;
1105 char **new_gl_pathv;
1107 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
1110 if (new_gl_pathv == NULL)
1113 return GLOB_NOSPACE;
1115 pglob->gl_pathv = new_gl_pathv;
1117 pglob->gl_pathv[newcount] = __strdup (pattern);
1118 if (pglob->gl_pathv[newcount] == NULL)
1122 pglob->gl_pathc = 0;
1123 return GLOB_NOSPACE;
1129 pglob->gl_pathv[newcount] = NULL;
1130 pglob->gl_flags = flags;
1135 return GLOB_NOMATCH;
1143 int old_pathc = pglob->gl_pathc;
1144 int orig_flags = flags;
1148 char *p = strchr (dirname, '\\'), *q;
1149 /* We need to unescape the dirname string. It is certainly
1150 allocated by alloca, as otherwise filename would be NULL
1151 or dirname wouldn't contain backslashes. */
1164 while (*p++ != '\0');
1165 dirname_modified = 1;
1167 if (dirname_modified)
1168 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
1169 status = glob_in_dir (filename, dirname, flags, errfunc, pglob,
1173 if (status == GLOB_NOMATCH && flags != orig_flags
1174 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1176 /* Make sure globfree (&dirs); is a nop. */
1177 dirs.gl_pathv = NULL;
1186 /* Stick the directory on the front of each name. */
1187 if (prefix_array (dirname,
1188 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1189 pglob->gl_pathc - old_pathc))
1192 pglob->gl_pathc = 0;
1193 return GLOB_NOSPACE;
1198 if (flags & GLOB_MARK)
1200 /* Append slashes to directory names. */
1205 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1206 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1207 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1208 && S_ISDIR (st.st_mode))
1209 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1210 && S_ISDIR (st64.st_mode))))
1212 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1213 char *new = realloc (pglob->gl_pathv[i], len);
1217 pglob->gl_pathc = 0;
1218 return GLOB_NOSPACE;
1220 strcpy (&new[len - 2], "/");
1221 pglob->gl_pathv[i] = new;
1225 if (!(flags & GLOB_NOSORT))
1227 /* Sort the vector. */
1228 qsort (&pglob->gl_pathv[oldcount],
1229 pglob->gl_pathc + pglob->gl_offs - oldcount,
1230 sizeof (char *), collated_compare);
1234 if (__builtin_expect (malloc_dirname, 0))
1239 #if defined _LIBC && !defined glob
1240 libc_hidden_def (glob)
1244 #if !defined _LIBC || !defined GLOB_ONLY_P
1246 /* Free storage allocated in PGLOB by a previous `glob' call. */
1249 register glob_t *pglob;
1251 if (pglob->gl_pathv != NULL)
1254 for (i = 0; i < pglob->gl_pathc; ++i)
1255 free (pglob->gl_pathv[pglob->gl_offs + i]);
1256 free (pglob->gl_pathv);
1257 pglob->gl_pathv = NULL;
1260 #if defined _LIBC && !defined globfree
1261 libc_hidden_def (globfree)
1265 /* Do a collated comparison of A and B. */
1267 collated_compare (const void *a, const void *b)
1269 const char *const s1 = *(const char *const * const) a;
1270 const char *const s2 = *(const char *const * const) b;
1278 return strcoll (s1, s2);
1282 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1283 elements in place. Return nonzero if out of memory, zero if successful.
1284 A slash is inserted between DIRNAME and each elt of ARRAY,
1285 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1287 prefix_array (const char *dirname, char **array, size_t n)
1290 size_t dirlen = strlen (dirname);
1291 #if defined __MSDOS__ || defined WINDOWS32
1293 # define DIRSEP_CHAR sep_char
1295 # define DIRSEP_CHAR '/'
1298 if (dirlen == 1 && dirname[0] == '/')
1299 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1300 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1302 #if defined __MSDOS__ || defined WINDOWS32
1303 else if (dirlen > 1)
1305 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1306 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1308 else if (dirname[dirlen - 1] == ':')
1310 /* DIRNAME is "d:". Use `:' instead of `/'. */
1317 for (i = 0; i < n; ++i)
1319 size_t eltlen = strlen (array[i]) + 1;
1320 char *new = (char *) malloc (dirlen + 1 + eltlen);
1329 char *endp = mempcpy (new, dirname, dirlen);
1330 *endp++ = DIRSEP_CHAR;
1331 mempcpy (endp, array[i], eltlen);
1341 /* We must not compile this function twice. */
1342 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1344 __glob_pattern_type (pattern, quote)
1345 const char *pattern;
1348 register const char *p;
1351 for (p = pattern; *p != '\0'; ++p)
1380 /* Return nonzero if PATTERN contains any metacharacters.
1381 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1383 __glob_pattern_p (pattern, quote)
1384 const char *pattern;
1387 return __glob_pattern_type (pattern, quote) == 1;
1390 weak_alias (__glob_pattern_p, glob_pattern_p)
1394 #endif /* !GLOB_ONLY_P */
1397 /* We put this in a separate function mainly to allow the memory
1398 allocated with alloca to be recycled. */
1399 #if !defined _LIBC || !defined GLOB_ONLY_P
1401 __attribute_noinline__
1402 link_exists2_p (const char *dir, size_t dirlen, const char *fname,
1409 size_t fnamelen = strlen (fname);
1410 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1416 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1417 fname, fnamelen + 1);
1420 return (*pglob->gl_stat) (fullname, &st) == 0;
1422 return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1423 ? (*pglob->gl_stat) (fullname, &st)
1424 : __stat64 (fullname, &st64)) == 0);
1428 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1429 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1430 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1431 : ({ struct stat64 st64; \
1432 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1434 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1435 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1440 /* Like `glob', but PATTERN is a final pathname component,
1441 and matches are searched for in DIRECTORY.
1442 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1443 The GLOB_APPEND flag is assumed to be set (always appends). */
1445 glob_in_dir (const char *pattern, const char *directory, int flags,
1446 int (*errfunc) (const char *, int),
1447 glob_t *pglob, size_t alloca_used)
1449 size_t dirlen = strlen (directory);
1450 void *stream = NULL;
1453 struct globnames *next;
1457 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1458 struct globnames init_names;
1459 struct globnames *names = &init_names;
1460 struct globnames *names_alloca = &init_names;
1466 alloca_used += sizeof (init_names);
1468 init_names.next = NULL;
1469 init_names.count = INITIAL_COUNT;
1471 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1472 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1474 /* We need not do any tests. The PATTERN contains no meta
1475 characters and we must not return an error therefore the
1476 result will always contain exactly one name. */
1477 flags |= GLOB_NOCHECK;
1481 /* Since we use the normal file functions we can also use stat()
1482 to verify the file is there. */
1488 size_t patlen = strlen (pattern);
1489 int alloca_fullname = __libc_use_alloca (alloca_used
1490 + dirlen + 1 + patlen + 1);
1492 if (alloca_fullname)
1493 fullname = alloca_account (dirlen + 1 + patlen + 1, alloca_used);
1496 fullname = malloc (dirlen + 1 + patlen + 1);
1497 if (fullname == NULL)
1498 return GLOB_NOSPACE;
1501 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1503 pattern, patlen + 1);
1504 if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1505 ? (*pglob->gl_stat) (fullname, &ust.st)
1506 : __stat64 (fullname, &ust.st64)) == 0)
1507 /* We found this file to be existing. Now tell the rest
1508 of the function to copy this name into the result. */
1509 flags |= GLOB_NOCHECK;
1511 if (__builtin_expect (!alloca_fullname, 0))
1516 stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1517 ? (*pglob->gl_opendir) (directory)
1518 : opendir (directory));
1521 if (errno != ENOTDIR
1522 && ((errfunc != NULL && (*errfunc) (directory, errno))
1523 || (flags & GLOB_ERR)))
1524 return GLOB_ABORTED;
1529 int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1530 ? -1 : dirfd ((DIR *) stream));
1532 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1533 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1534 #if defined _AMIGA || defined VMS
1538 flags |= GLOB_MAGCHAR;
1544 #if defined _LIBC && !defined COMPILE_GLOB64
1548 struct dirent64 d64;
1549 char room [offsetof (struct dirent64, d_name[0])
1554 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1556 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1559 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1566 d = __readdir64 (stream);
1568 struct dirent *d = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
1569 ? ((struct dirent *)
1570 (*pglob->gl_readdir) (stream))
1571 : __readdir (stream));
1575 if (! REAL_DIR_ENTRY (d))
1578 /* If we shall match only directories use the information
1579 provided by the dirent call if possible. */
1580 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1585 if (fnmatch (pattern, name, fnm_flags) == 0)
1587 /* If the file we found is a symlink we have to
1588 make sure the target file exists. */
1589 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1590 || link_exists_p (dfd, directory, dirlen, name, pglob,
1593 if (cur == names->count)
1595 struct globnames *newnames;
1596 size_t count = names->count * 2;
1597 size_t size = (sizeof (struct globnames)
1598 + ((count - INITIAL_COUNT)
1599 * sizeof (char *)));
1600 if (__libc_use_alloca (alloca_used + size))
1601 newnames = names_alloca
1602 = alloca_account (size, alloca_used);
1603 else if ((newnames = malloc (size))
1606 newnames->count = count;
1607 newnames->next = names;
1612 names->name[cur] = (char *) malloc (len + 1);
1613 if (names->name[cur] == NULL)
1615 *((char *) mempcpy (names->name[cur++], name, len))
1624 if (nfound == 0 && (flags & GLOB_NOCHECK))
1626 size_t len = strlen (pattern);
1628 names->name[cur] = (char *) malloc (len + 1);
1629 if (names->name[cur] == NULL)
1631 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1634 int result = GLOB_NOMATCH;
1639 char **new_gl_pathv;
1641 = (char **) realloc (pglob->gl_pathv,
1642 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1644 if (new_gl_pathv == NULL)
1649 struct globnames *old = names;
1650 for (size_t i = 0; i < cur; ++i)
1651 free (names->name[i]);
1652 names = names->next;
1653 /* NB: we will not leak memory here if we exit without
1654 freeing the current block assigned to OLD. At least
1655 the very first block is always allocated on the stack
1656 and this is the block assigned to OLD here. */
1659 assert (old == &init_names);
1663 if (old == names_alloca)
1664 names_alloca = names;
1668 result = GLOB_NOSPACE;
1674 struct globnames *old = names;
1675 for (size_t i = 0; i < cur; ++i)
1676 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1678 names = names->next;
1679 /* NB: we will not leak memory here if we exit without
1680 freeing the current block assigned to OLD. At least
1681 the very first block is always allocated on the stack
1682 and this is the block assigned to OLD here. */
1685 assert (old == &init_names);
1689 if (old == names_alloca)
1690 names_alloca = names;
1695 pglob->gl_pathv = new_gl_pathv;
1697 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1699 pglob->gl_flags = flags;
1706 if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0))
1707 (*pglob->gl_closedir) (stream);