1 /* Copyright (C) 1991-2002,2003,2004,2005,2006,2007 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/types.h>
30 /* Outcomment the following line for production quality code. */
31 /* #define NDEBUG 1 */
34 #include <stdio.h> /* Needed on stupid SunOS for assert. */
36 #if !defined _LIBC || !defined GLOB_ONLY_P
39 #if !defined POSIX && defined _POSIX_VERSION
47 # define __set_errno(val) errno = (val)
53 /* In GNU systems, <dirent.h> defines this macro for us. */
54 #ifndef _D_EXACT_NAMLEN
55 # define _D_EXACT_NAMLEN(dirent) strlen ((dirent)->d_name)
58 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
59 if the `d_type' member for `struct dirent' is available.
60 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
61 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
62 /* True if the directory entry D must be of type T. */
63 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
65 /* True if the directory entry D might be a symbolic link. */
66 # define DIRENT_MIGHT_BE_SYMLINK(d) \
67 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
69 /* True if the directory entry D might be a directory. */
70 # define DIRENT_MIGHT_BE_DIR(d) \
71 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
73 #else /* !HAVE_D_TYPE */
74 # define DIRENT_MUST_BE(d, t) false
75 # define DIRENT_MIGHT_BE_SYMLINK(d) true
76 # define DIRENT_MIGHT_BE_DIR(d) true
77 #endif /* HAVE_D_TYPE */
79 /* If the system has the `struct dirent64' type we use it internally. */
80 #if defined _LIBC && !defined COMPILE_GLOB64
81 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
82 # define CONVERT_D_INO(d64, d32)
84 # define CONVERT_D_INO(d64, d32) \
85 (d64)->d_ino = (d32)->d_ino;
88 # ifdef _DIRENT_HAVE_D_TYPE
89 # define CONVERT_D_TYPE(d64, d32) \
90 (d64)->d_type = (d32)->d_type;
92 # define CONVERT_D_TYPE(d64, d32)
95 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
96 memcpy ((d64)->d_name, (d32)->d_name, _D_EXACT_NAMLEN (d32) + 1); \
97 CONVERT_D_INO (d64, d32) \
98 CONVERT_D_TYPE (d64, d32)
102 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
103 /* Posix does not require that the d_ino field be present, and some
104 systems do not provide it. */
105 # define REAL_DIR_ENTRY(dp) 1
107 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
113 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
116 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
123 # define strdup(str) __strdup (str)
124 # define sysconf(id) __sysconf (id)
125 # define closedir(dir) __closedir (dir)
126 # define opendir(name) __opendir (name)
127 # define readdir(str) __readdir64 (str)
128 # define getpwnam_r(name, bufp, buf, len, res) \
129 __getpwnam_r (name, bufp, buf, len, res)
131 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
133 # define struct_stat64 struct stat64
135 # include "getlogin_r.h"
136 # define __stat64(fname, buf) stat (fname, buf)
137 # define struct_stat64 struct stat
138 # define __stat(fname, buf) stat (fname, buf)
139 # define __alloca alloca
140 # define __readdir readdir
141 # define __readdir64 readdir64
142 # define __glob_pattern_p glob_pattern_p
148 #ifdef _SC_GETPW_R_SIZE_MAX
149 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
151 # define GETPW_R_SIZE_MAX() (-1)
153 #ifdef _SC_LOGIN_NAME_MAX
154 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
156 # define GET_LOGIN_NAME_MAX() (-1)
159 static const char *next_brace_sub (const char *begin, int flags) __THROW;
161 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
163 /* The results of opendir() in this file are not used with dirfd and fchdir,
164 therefore save some unnecessary work in fchdir.c. */
168 static int glob_in_dir (const char *pattern, const char *directory,
169 int flags, int (*errfunc) (const char *, int),
172 #if !defined _LIBC || !defined GLOB_ONLY_P
173 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
174 static int collated_compare (const void *, const void *) __THROW;
177 /* Find the end of the sub-pattern in a brace expression. */
179 next_brace_sub (const char *cp, int flags)
181 unsigned int depth = 0;
183 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
191 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
198 return *cp != '\0' ? cp : NULL;
201 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
203 /* Do glob searching for PATTERN, placing results in PGLOB.
204 The bits defined above may be set in FLAGS.
205 If a directory cannot be opened or read and ERRFUNC is not nil,
206 it is called with the pathname that caused the error, and the
207 `errno' value from the failing call; if it returns non-zero
208 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
209 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
210 Otherwise, `glob' returns zero. */
212 #ifdef GLOB_ATTRIBUTE
215 glob (pattern, flags, errfunc, pglob)
218 int (*errfunc) (const char *, int);
221 const char *filename;
227 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
229 __set_errno (EINVAL);
233 if (!(flags & GLOB_DOOFFS))
234 /* Have to do this so `globfree' knows where to start freeing. It
235 also makes all the code that uses gl_offs simpler. */
238 if (flags & GLOB_BRACE)
242 if (flags & GLOB_NOESCAPE)
243 begin = strchr (pattern, '{');
255 if (*begin == '\\' && begin[1] != '\0')
257 else if (*begin == '{')
266 /* Allocate working buffer large enough for our work. Note that
267 we have at least an opening and closing brace. */
275 char onealt[strlen (pattern) - 1];
277 char *onealt = malloc (strlen (pattern) - 1);
280 if (!(flags & GLOB_APPEND))
283 pglob->gl_pathv = NULL;
289 /* We know the prefix for all sub-patterns. */
290 alt_start = mempcpy (onealt, pattern, begin - pattern);
292 /* Find the first sub-pattern and at the same time find the
293 rest after the closing brace. */
294 next = next_brace_sub (begin + 1, flags);
297 /* It is an invalid expression. */
301 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
304 /* Now find the end of the whole brace expression. */
308 rest = next_brace_sub (rest + 1, flags);
311 /* It is an invalid expression. */
315 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
318 /* Please note that we now can be sure the brace expression
320 rest_len = strlen (++rest) + 1;
322 /* We have a brace expression. BEGIN points to the opening {,
323 NEXT points past the terminator of the first element, and END
324 points past the final }. We will accumulate result names from
325 recursive runs for each brace alternative in the buffer using
328 if (!(flags & GLOB_APPEND))
330 /* This call is to set a new vector, so clear out the
331 vector so we can append to it. */
333 pglob->gl_pathv = NULL;
335 firstc = pglob->gl_pathc;
342 /* Construct the new glob expression. */
343 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
345 result = glob (onealt,
346 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
347 | GLOB_APPEND), errfunc, pglob);
349 /* If we got an error, return it. */
350 if (result && result != GLOB_NOMATCH)
355 if (!(flags & GLOB_APPEND))
364 /* We saw the last entry. */
368 next = next_brace_sub (p, flags);
369 assert (next != NULL);
376 if (pglob->gl_pathc != firstc)
377 /* We found some entries. */
379 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
384 /* Find the filename. */
385 filename = strrchr (pattern, '/');
386 #if defined __MSDOS__ || defined WINDOWS32
387 /* The case of "d:pattern". Since `:' is not allowed in
388 file names, we can safely assume that wherever it
389 happens in pattern, it signals the filename part. This
390 is so we could some day support patterns like "[a-z]:foo". */
391 if (filename == NULL)
392 filename = strchr (pattern, ':');
393 #endif /* __MSDOS__ || WINDOWS32 */
394 if (filename == NULL)
396 /* This can mean two things: a simple name or "~name". The latter
397 case is nothing but a notation for a directory. */
398 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
401 dirlen = strlen (pattern);
403 /* Set FILENAME to NULL as a special flag. This is ugly but
404 other solutions would require much more code. We test for
405 this special case below. */
419 else if (filename == pattern)
429 dirlen = filename - pattern;
430 #if defined __MSDOS__ || defined WINDOWS32
432 || (filename > pattern + 1 && filename[-1] == ':'))
437 drive_spec = __alloca (dirlen + 1);
438 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
439 /* For now, disallow wildcards in the drive spec, to
440 prevent infinite recursion in glob. */
441 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
443 /* If this is "d:pattern", we need to copy `:' to DIRNAME
444 as well. If it's "d:/pattern", don't remove the slash
445 from "d:/", since "d:" and "d:/" are not the same.*/
448 newp = __alloca (dirlen + 1);
449 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
453 if (filename[0] == '\0'
454 #if defined __MSDOS__ || defined WINDOWS32
455 && dirname[dirlen - 1] != ':'
456 && (dirlen < 3 || dirname[dirlen - 2] != ':'
457 || dirname[dirlen - 1] != '/')
460 /* "pattern/". Expand "pattern", appending slashes. */
462 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
464 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
465 | (flags & GLOB_MARK));
470 if (!(flags & GLOB_APPEND))
473 if (!(flags & GLOB_DOOFFS))
474 pglob->gl_pathv = NULL;
478 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
479 if (pglob->gl_pathv == NULL)
482 for (i = 0; i <= pglob->gl_offs; ++i)
483 pglob->gl_pathv[i] = NULL;
487 oldcount = pglob->gl_pathc + pglob->gl_offs;
489 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
491 if (dirname[1] == '\0' || dirname[1] == '/')
493 /* Look up home directory. */
494 const char *home_dir = getenv ("HOME");
496 if (home_dir == NULL || home_dir[0] == '\0')
500 if (home_dir == NULL || home_dir[0] == '\0')
501 home_dir = "c:/users/default"; /* poor default */
503 if (home_dir == NULL || home_dir[0] == '\0')
507 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
510 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
513 name = __alloca (buflen);
515 success = getlogin_r (name, buflen) == 0;
519 # if defined HAVE_GETPWNAM_R || defined _LIBC
520 long int pwbuflen = GETPW_R_SIZE_MAX ();
527 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
528 Try a moderate value. */
531 pwtmpbuf = __alloca (pwbuflen);
533 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
542 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
546 pwtmpbuf = __alloca (pwbuflen);
554 home_dir = p->pw_dir;
557 if (home_dir == NULL || home_dir[0] == '\0')
559 if (flags & GLOB_TILDE_CHECK)
562 home_dir = "~"; /* No luck. */
564 # endif /* WINDOWS32 */
566 /* Now construct the full directory. */
567 if (dirname[1] == '\0')
572 size_t home_len = strlen (home_dir);
573 newp = __alloca (home_len + dirlen);
574 mempcpy (mempcpy (newp, home_dir, home_len),
575 &dirname[1], dirlen);
579 # if !defined _AMIGA && !defined WINDOWS32
582 char *end_name = strchr (dirname, '/');
583 const char *user_name;
584 const char *home_dir;
586 if (end_name == NULL)
587 user_name = dirname + 1;
591 newp = __alloca (end_name - dirname);
592 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
597 /* Look up specific user's home directory. */
600 # if defined HAVE_GETPWNAM_R || defined _LIBC
601 long int buflen = GETPW_R_SIZE_MAX ();
608 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
612 pwtmpbuf = __alloca (buflen);
614 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
622 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
625 pwtmpbuf = __alloca (buflen);
630 p = getpwnam (user_name);
633 home_dir = p->pw_dir;
637 /* If we found a home directory use this. */
638 if (home_dir != NULL)
641 size_t home_len = strlen (home_dir);
642 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
643 newp = __alloca (home_len + rest_len + 1);
644 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
645 end_name, rest_len)) = '\0';
649 if (flags & GLOB_TILDE_CHECK)
650 /* We have to regard it as an error if we cannot find the
654 # endif /* Not Amiga && not WINDOWS32. */
657 /* Now test whether we looked for "~" or "~NAME". In this case we
658 can give the answer now. */
659 if (filename == NULL)
664 /* Return the directory if we don't check for error or if it exists. */
665 if ((flags & GLOB_NOCHECK)
666 || (((flags & GLOB_ALTDIRFUNC)
667 ? ((*pglob->gl_stat) (dirname, &st) == 0
668 && S_ISDIR (st.st_mode))
669 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
671 int newcount = pglob->gl_pathc + pglob->gl_offs;
675 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
676 if (new_gl_pathv == NULL)
679 free (pglob->gl_pathv);
680 pglob->gl_pathv = NULL;
684 pglob->gl_pathv = new_gl_pathv;
686 pglob->gl_pathv[newcount] = strdup (dirname);
687 if (pglob->gl_pathv[newcount] == NULL)
689 pglob->gl_pathv[++newcount] = NULL;
691 pglob->gl_flags = flags;
700 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
702 /* The directory name contains metacharacters, so we
703 have to glob for the directory, and then glob for
704 the pattern in each directory found. */
708 if ((flags & GLOB_ALTDIRFUNC) != 0)
710 /* Use the alternative access functions also in the recursive
712 dirs.gl_opendir = pglob->gl_opendir;
713 dirs.gl_readdir = pglob->gl_readdir;
714 dirs.gl_closedir = pglob->gl_closedir;
715 dirs.gl_stat = pglob->gl_stat;
716 dirs.gl_lstat = pglob->gl_lstat;
719 status = glob (dirname,
720 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
722 | GLOB_NOSORT | GLOB_ONLYDIR),
727 /* We have successfully globbed the preceding directory name.
728 For each name we found, call glob_in_dir on it and FILENAME,
729 appending the results to PGLOB. */
730 for (i = 0; i < dirs.gl_pathc; ++i)
736 /* Make globbing interruptible in the bash shell. */
737 extern int interrupt_state;
747 old_pathc = pglob->gl_pathc;
748 status = glob_in_dir (filename, dirs.gl_pathv[i],
749 ((flags | GLOB_APPEND)
750 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
752 if (status == GLOB_NOMATCH)
753 /* No matches in this directory. Try the next. */
764 /* Stick the directory on the front of each name. */
765 if (prefix_array (dirs.gl_pathv[i],
766 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
767 pglob->gl_pathc - old_pathc))
776 flags |= GLOB_MAGCHAR;
778 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
779 But if we have not found any matching entry and the GLOB_NOCHECK
780 flag was set we must return the input pattern itself. */
781 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
784 if (flags & GLOB_NOCHECK)
786 int newcount = pglob->gl_pathc + pglob->gl_offs;
789 new_gl_pathv = realloc (pglob->gl_pathv,
790 (newcount + 2) * sizeof (char *));
791 if (new_gl_pathv == NULL)
796 pglob->gl_pathv = new_gl_pathv;
798 pglob->gl_pathv[newcount] = strdup (pattern);
799 if (pglob->gl_pathv[newcount] == NULL)
810 pglob->gl_pathv[newcount] = NULL;
811 pglob->gl_flags = flags;
824 int old_pathc = pglob->gl_pathc;
826 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
832 /* Stick the directory on the front of each name. */
833 if (prefix_array (dirname,
834 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
835 pglob->gl_pathc - old_pathc))
844 if (!(flags & GLOB_NOSORT))
846 /* Sort the vector. */
847 qsort (&pglob->gl_pathv[oldcount],
848 pglob->gl_pathc + pglob->gl_offs - oldcount,
849 sizeof (char *), collated_compare);
854 #if defined _LIBC && !defined glob
855 libc_hidden_def (glob)
859 #if !defined _LIBC || !defined GLOB_ONLY_P
861 /* Free storage allocated in PGLOB by a previous `glob' call. */
864 register glob_t *pglob;
866 if (pglob->gl_pathv != NULL)
869 for (i = 0; i < pglob->gl_pathc; ++i)
870 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
871 free (pglob->gl_pathv[pglob->gl_offs + i]);
872 free (pglob->gl_pathv);
873 pglob->gl_pathv = NULL;
876 #if defined _LIBC && !defined globfree
877 libc_hidden_def (globfree)
881 /* Do a collated comparison of A and B. */
883 collated_compare (const void *a, const void *b)
885 char *const *ps1 = a; char *s1 = *ps1;
886 char *const *ps2 = b; char *s2 = *ps2;
894 return strcoll (s1, s2);
898 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
899 elements in place. Return nonzero if out of memory, zero if successful.
900 A slash is inserted between DIRNAME and each elt of ARRAY,
901 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
903 prefix_array (const char *dirname, char **array, size_t n)
906 size_t dirlen = strlen (dirname);
907 #if defined __MSDOS__ || defined WINDOWS32
909 # define DIRSEP_CHAR sep_char
911 # define DIRSEP_CHAR '/'
914 if (dirlen == 1 && dirname[0] == '/')
915 /* DIRNAME is just "/", so normal prepending would get us "//foo".
916 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
918 #if defined __MSDOS__ || defined WINDOWS32
921 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
922 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
924 else if (dirname[dirlen - 1] == ':')
926 /* DIRNAME is "d:". Use `:' instead of `/'. */
933 for (i = 0; i < n; ++i)
935 size_t eltlen = strlen (array[i]) + 1;
936 char *new = malloc (dirlen + 1 + eltlen);
945 char *endp = mempcpy (new, dirname, dirlen);
946 *endp++ = DIRSEP_CHAR;
947 mempcpy (endp, array[i], eltlen);
957 /* We must not compile this function twice. */
958 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
959 /* Return nonzero if PATTERN contains any metacharacters.
960 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
962 __glob_pattern_p (pattern, quote)
966 register const char *p;
969 for (p = pattern; *p != '\0'; ++p)
977 if (quote && p[1] != '\0')
994 weak_alias (__glob_pattern_p, glob_pattern_p)
998 #endif /* !GLOB_ONLY_P */
1001 /* We put this in a separate function mainly to allow the memory
1002 allocated with alloca to be recycled. */
1003 #if !defined _LIBC || !defined GLOB_ONLY_P
1005 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1006 glob_t *pglob, int flags)
1008 size_t fnamelen = strlen (fname);
1009 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1013 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1014 fname, fnamelen + 1);
1016 return ((flags & GLOB_ALTDIRFUNC)
1017 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1018 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1023 /* Like `glob', but PATTERN is a final pathname component,
1024 and matches are searched for in DIRECTORY.
1025 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1026 The GLOB_APPEND flag is assumed to be set (always appends). */
1028 glob_in_dir (const char *pattern, const char *directory, int flags,
1029 int (*errfunc) (const char *, int),
1032 size_t dirlen = strlen (directory);
1033 void *stream = NULL;
1036 struct globlink *next;
1039 struct globlink *names = NULL;
1044 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1045 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1047 /* We need not do any tests. The PATTERN contains no meta
1048 characters and we must not return an error therefore the
1049 result will always contain exactly one name. */
1050 flags |= GLOB_NOCHECK;
1053 else if (meta == 0 &&
1054 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1056 /* Since we use the normal file functions we can also use stat()
1057 to verify the file is there. */
1060 size_t patlen = strlen (pattern);
1061 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1063 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1065 pattern, patlen + 1);
1066 if (((flags & GLOB_ALTDIRFUNC)
1067 ? (*pglob->gl_stat) (fullname, &st)
1068 : __stat64 (fullname, &st64)) == 0)
1069 /* We found this file to be existing. Now tell the rest
1070 of the function to copy this name into the result. */
1071 flags |= GLOB_NOCHECK;
1077 if (pattern[0] == '\0')
1079 /* This is a special case for matching directories like in
1081 names = __alloca (sizeof (struct globlink));
1082 names->name = malloc (1);
1083 if (names->name == NULL)
1085 names->name[0] = '\0';
1092 stream = ((flags & GLOB_ALTDIRFUNC)
1093 ? (*pglob->gl_opendir) (directory)
1094 : opendir (directory));
1097 if (errno != ENOTDIR
1098 && ((errfunc != NULL && (*errfunc) (directory, errno))
1099 || (flags & GLOB_ERR)))
1100 return GLOB_ABORTED;
1106 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1107 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1108 #if defined _AMIGA || defined __VMS
1113 flags |= GLOB_MAGCHAR;
1119 #if defined _LIBC && !defined COMPILE_GLOB64
1123 struct dirent64 d64;
1124 char room [offsetof (struct dirent64, d_name[0])
1129 if (flags & GLOB_ALTDIRFUNC)
1131 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1134 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1141 d = __readdir64 (stream);
1143 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1144 ? ((*pglob->gl_readdir) (stream))
1145 : __readdir (stream));
1149 if (! REAL_DIR_ENTRY (d))
1152 /* If we shall match only directories use the information
1153 provided by the dirent call if possible. */
1154 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1159 if (fnmatch (pattern, name, fnm_flags) == 0)
1161 /* ISDIR will often be incorrectly set to false
1162 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1163 don't care. It won't be used and we save the
1164 expensive call to stat. */
1166 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1167 ? GLOB_ONLYDIR : 0));
1168 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1169 || ((flags & need_dir_test)
1170 && is_dir_p (directory, dirlen, name,
1173 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1174 if ((flags & GLOB_ONLYDIR) && !isdir)
1178 struct globlink *new =
1179 __alloca (sizeof (struct globlink));
1181 len = _D_EXACT_NAMLEN (d);
1183 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1184 if (new->name == NULL)
1186 p = mempcpy (new->name, name, len);
1187 if ((flags & GLOB_MARK) && isdir)
1200 if (nfound == 0 && (flags & GLOB_NOCHECK))
1202 size_t len = strlen (pattern);
1204 names = __alloca (sizeof (struct globlink));
1206 names->name = malloc (len + 1);
1207 if (names->name == NULL)
1209 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1214 char **new_gl_pathv;
1217 = realloc (pglob->gl_pathv,
1218 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1220 if (new_gl_pathv == NULL)
1222 pglob->gl_pathv = new_gl_pathv;
1224 for (; names != NULL; names = names->next)
1225 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1226 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1228 pglob->gl_flags = flags;
1234 if (flags & GLOB_ALTDIRFUNC)
1235 (*pglob->gl_closedir) (stream);
1241 return nfound == 0 ? GLOB_NOMATCH : 0;
1246 if (flags & GLOB_ALTDIRFUNC)
1247 (*pglob->gl_closedir) (stream);
1252 while (names != NULL)
1254 if (names->name != NULL)
1256 names = names->next;
1258 return GLOB_NOSPACE;