1 /* Copyright (C) 1991-2002,2003,2004,2005,2006 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 # include "mempcpy.h"
138 # define __stat64(fname, buf) stat (fname, buf)
139 # define struct_stat64 struct stat
140 # define __stat(fname, buf) stat (fname, buf)
141 # define __alloca alloca
142 # define __readdir readdir
143 # define __readdir64 readdir64
144 # define __glob_pattern_p glob_pattern_p
150 #ifdef _SC_GETPW_R_SIZE_MAX
151 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
153 # define GETPW_R_SIZE_MAX() (-1)
155 #ifdef _SC_LOGIN_NAME_MAX
156 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
158 # define GET_LOGIN_NAME_MAX() (-1)
161 static const char *next_brace_sub (const char *begin, int flags) __THROW;
163 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
165 /* The results of opendir() in this file are not used with dirfd and fchdir,
166 therefore save some unnecessary work in fchdir.c. */
170 static int glob_in_dir (const char *pattern, const char *directory,
171 int flags, int (*errfunc) (const char *, int),
174 #if !defined _LIBC || !defined GLOB_ONLY_P
175 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
176 static int collated_compare (const void *, const void *) __THROW;
179 /* Find the end of the sub-pattern in a brace expression. */
181 next_brace_sub (const char *cp, int flags)
183 unsigned int depth = 0;
185 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
193 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
200 return *cp != '\0' ? cp : NULL;
203 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
205 /* Do glob searching for PATTERN, placing results in PGLOB.
206 The bits defined above may be set in FLAGS.
207 If a directory cannot be opened or read and ERRFUNC is not nil,
208 it is called with the pathname that caused the error, and the
209 `errno' value from the failing call; if it returns non-zero
210 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
211 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
212 Otherwise, `glob' returns zero. */
214 #ifdef GLOB_ATTRIBUTE
217 glob (pattern, flags, errfunc, pglob)
220 int (*errfunc) (const char *, int);
223 const char *filename;
229 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
231 __set_errno (EINVAL);
235 if (!(flags & GLOB_DOOFFS))
236 /* Have to do this so `globfree' knows where to start freeing. It
237 also makes all the code that uses gl_offs simpler. */
240 if (flags & GLOB_BRACE)
244 if (flags & GLOB_NOESCAPE)
245 begin = strchr (pattern, '{');
257 if (*begin == '\\' && begin[1] != '\0')
259 else if (*begin == '{')
268 /* Allocate working buffer large enough for our work. Note that
269 we have at least an opening and closing brace. */
277 char onealt[strlen (pattern) - 1];
279 char *onealt = malloc (strlen (pattern) - 1);
282 if (!(flags & GLOB_APPEND))
285 pglob->gl_pathv = NULL;
291 /* We know the prefix for all sub-patterns. */
292 alt_start = mempcpy (onealt, pattern, begin - pattern);
294 /* Find the first sub-pattern and at the same time find the
295 rest after the closing brace. */
296 next = next_brace_sub (begin + 1, flags);
299 /* It is an invalid expression. */
303 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
306 /* Now find the end of the whole brace expression. */
310 rest = next_brace_sub (rest + 1, flags);
313 /* It is an invalid expression. */
317 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
320 /* Please note that we now can be sure the brace expression
322 rest_len = strlen (++rest) + 1;
324 /* We have a brace expression. BEGIN points to the opening {,
325 NEXT points past the terminator of the first element, and END
326 points past the final }. We will accumulate result names from
327 recursive runs for each brace alternative in the buffer using
330 if (!(flags & GLOB_APPEND))
332 /* This call is to set a new vector, so clear out the
333 vector so we can append to it. */
335 pglob->gl_pathv = NULL;
337 firstc = pglob->gl_pathc;
344 /* Construct the new glob expression. */
345 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
347 result = glob (onealt,
348 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
349 | GLOB_APPEND), errfunc, pglob);
351 /* If we got an error, return it. */
352 if (result && result != GLOB_NOMATCH)
357 if (!(flags & GLOB_APPEND))
366 /* We saw the last entry. */
370 next = next_brace_sub (p, flags);
371 assert (next != NULL);
378 if (pglob->gl_pathc != firstc)
379 /* We found some entries. */
381 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
386 /* Find the filename. */
387 filename = strrchr (pattern, '/');
388 #if defined __MSDOS__ || defined WINDOWS32
389 /* The case of "d:pattern". Since `:' is not allowed in
390 file names, we can safely assume that wherever it
391 happens in pattern, it signals the filename part. This
392 is so we could some day support patterns like "[a-z]:foo". */
393 if (filename == NULL)
394 filename = strchr (pattern, ':');
395 #endif /* __MSDOS__ || WINDOWS32 */
396 if (filename == NULL)
398 /* This can mean two things: a simple name or "~name". The latter
399 case is nothing but a notation for a directory. */
400 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
403 dirlen = strlen (pattern);
405 /* Set FILENAME to NULL as a special flag. This is ugly but
406 other solutions would require much more code. We test for
407 this special case below. */
421 else if (filename == pattern)
431 dirlen = filename - pattern;
432 #if defined __MSDOS__ || defined WINDOWS32
434 || (filename > pattern + 1 && filename[-1] == ':'))
439 drive_spec = __alloca (dirlen + 1);
440 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
441 /* For now, disallow wildcards in the drive spec, to
442 prevent infinite recursion in glob. */
443 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
445 /* If this is "d:pattern", we need to copy `:' to DIRNAME
446 as well. If it's "d:/pattern", don't remove the slash
447 from "d:/", since "d:" and "d:/" are not the same.*/
450 newp = __alloca (dirlen + 1);
451 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
455 if (filename[0] == '\0'
456 #if defined __MSDOS__ || defined WINDOWS32
457 && dirname[dirlen - 1] != ':'
458 && (dirlen < 3 || dirname[dirlen - 2] != ':'
459 || dirname[dirlen - 1] != '/')
462 /* "pattern/". Expand "pattern", appending slashes. */
464 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
466 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
467 | (flags & GLOB_MARK));
472 if (!(flags & GLOB_APPEND))
475 if (!(flags & GLOB_DOOFFS))
476 pglob->gl_pathv = NULL;
480 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
481 if (pglob->gl_pathv == NULL)
484 for (i = 0; i <= pglob->gl_offs; ++i)
485 pglob->gl_pathv[i] = NULL;
489 oldcount = pglob->gl_pathc + pglob->gl_offs;
491 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
493 if (dirname[1] == '\0' || dirname[1] == '/')
495 /* Look up home directory. */
496 const char *home_dir = getenv ("HOME");
498 if (home_dir == NULL || home_dir[0] == '\0')
502 if (home_dir == NULL || home_dir[0] == '\0')
503 home_dir = "c:/users/default"; /* poor default */
505 if (home_dir == NULL || home_dir[0] == '\0')
509 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
512 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
515 name = __alloca (buflen);
517 success = getlogin_r (name, buflen) == 0;
521 # if defined HAVE_GETPWNAM_R || defined _LIBC
522 long int pwbuflen = GETPW_R_SIZE_MAX ();
529 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
530 Try a moderate value. */
533 pwtmpbuf = __alloca (pwbuflen);
535 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
544 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
548 pwtmpbuf = __alloca (pwbuflen);
556 home_dir = p->pw_dir;
559 if (home_dir == NULL || home_dir[0] == '\0')
561 if (flags & GLOB_TILDE_CHECK)
564 home_dir = "~"; /* No luck. */
566 # endif /* WINDOWS32 */
568 /* Now construct the full directory. */
569 if (dirname[1] == '\0')
574 size_t home_len = strlen (home_dir);
575 newp = __alloca (home_len + dirlen);
576 mempcpy (mempcpy (newp, home_dir, home_len),
577 &dirname[1], dirlen);
581 # if !defined _AMIGA && !defined WINDOWS32
584 char *end_name = strchr (dirname, '/');
585 const char *user_name;
586 const char *home_dir;
588 if (end_name == NULL)
589 user_name = dirname + 1;
593 newp = __alloca (end_name - dirname);
594 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
599 /* Look up specific user's home directory. */
602 # if defined HAVE_GETPWNAM_R || defined _LIBC
603 long int buflen = GETPW_R_SIZE_MAX ();
610 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
614 pwtmpbuf = __alloca (buflen);
616 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
624 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
627 pwtmpbuf = __alloca (buflen);
632 p = getpwnam (user_name);
635 home_dir = p->pw_dir;
639 /* If we found a home directory use this. */
640 if (home_dir != NULL)
643 size_t home_len = strlen (home_dir);
644 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
645 newp = __alloca (home_len + rest_len + 1);
646 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
647 end_name, rest_len)) = '\0';
651 if (flags & GLOB_TILDE_CHECK)
652 /* We have to regard it as an error if we cannot find the
656 # endif /* Not Amiga && not WINDOWS32. */
659 /* Now test whether we looked for "~" or "~NAME". In this case we
660 can give the answer now. */
661 if (filename == NULL)
666 /* Return the directory if we don't check for error or if it exists. */
667 if ((flags & GLOB_NOCHECK)
668 || (((flags & GLOB_ALTDIRFUNC)
669 ? ((*pglob->gl_stat) (dirname, &st) == 0
670 && S_ISDIR (st.st_mode))
671 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
673 int newcount = pglob->gl_pathc + pglob->gl_offs;
677 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
678 if (new_gl_pathv == NULL)
681 free (pglob->gl_pathv);
682 pglob->gl_pathv = NULL;
686 pglob->gl_pathv = new_gl_pathv;
688 pglob->gl_pathv[newcount] = strdup (dirname);
689 if (pglob->gl_pathv[newcount] == NULL)
691 pglob->gl_pathv[++newcount] = NULL;
693 pglob->gl_flags = flags;
702 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
704 /* The directory name contains metacharacters, so we
705 have to glob for the directory, and then glob for
706 the pattern in each directory found. */
710 if ((flags & GLOB_ALTDIRFUNC) != 0)
712 /* Use the alternative access functions also in the recursive
714 dirs.gl_opendir = pglob->gl_opendir;
715 dirs.gl_readdir = pglob->gl_readdir;
716 dirs.gl_closedir = pglob->gl_closedir;
717 dirs.gl_stat = pglob->gl_stat;
718 dirs.gl_lstat = pglob->gl_lstat;
721 status = glob (dirname,
722 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
724 | GLOB_NOSORT | GLOB_ONLYDIR),
729 /* We have successfully globbed the preceding directory name.
730 For each name we found, call glob_in_dir on it and FILENAME,
731 appending the results to PGLOB. */
732 for (i = 0; i < dirs.gl_pathc; ++i)
738 /* Make globbing interruptible in the bash shell. */
739 extern int interrupt_state;
749 old_pathc = pglob->gl_pathc;
750 status = glob_in_dir (filename, dirs.gl_pathv[i],
751 ((flags | GLOB_APPEND)
752 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
754 if (status == GLOB_NOMATCH)
755 /* No matches in this directory. Try the next. */
766 /* Stick the directory on the front of each name. */
767 if (prefix_array (dirs.gl_pathv[i],
768 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
769 pglob->gl_pathc - old_pathc))
778 flags |= GLOB_MAGCHAR;
780 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
781 But if we have not found any matching entry and the GLOB_NOCHECK
782 flag was set we must return the input pattern itself. */
783 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
786 if (flags & GLOB_NOCHECK)
788 int newcount = pglob->gl_pathc + pglob->gl_offs;
791 new_gl_pathv = realloc (pglob->gl_pathv,
792 (newcount + 2) * sizeof (char *));
793 if (new_gl_pathv == NULL)
798 pglob->gl_pathv = new_gl_pathv;
800 pglob->gl_pathv[newcount] = strdup (pattern);
801 if (pglob->gl_pathv[newcount] == NULL)
812 pglob->gl_pathv[newcount] = NULL;
813 pglob->gl_flags = flags;
826 int old_pathc = pglob->gl_pathc;
828 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
834 /* Stick the directory on the front of each name. */
835 if (prefix_array (dirname,
836 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
837 pglob->gl_pathc - old_pathc))
846 if (!(flags & GLOB_NOSORT))
848 /* Sort the vector. */
849 qsort (&pglob->gl_pathv[oldcount],
850 pglob->gl_pathc + pglob->gl_offs - oldcount,
851 sizeof (char *), collated_compare);
856 #if defined _LIBC && !defined glob
857 libc_hidden_def (glob)
861 #if !defined _LIBC || !defined GLOB_ONLY_P
863 /* Free storage allocated in PGLOB by a previous `glob' call. */
866 register glob_t *pglob;
868 if (pglob->gl_pathv != NULL)
871 for (i = 0; i < pglob->gl_pathc; ++i)
872 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
873 free (pglob->gl_pathv[pglob->gl_offs + i]);
874 free (pglob->gl_pathv);
875 pglob->gl_pathv = NULL;
878 #if defined _LIBC && !defined globfree
879 libc_hidden_def (globfree)
883 /* Do a collated comparison of A and B. */
885 collated_compare (const void *a, const void *b)
887 char *const *ps1 = a; char *s1 = *ps1;
888 char *const *ps2 = b; char *s2 = *ps2;
896 return strcoll (s1, s2);
900 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
901 elements in place. Return nonzero if out of memory, zero if successful.
902 A slash is inserted between DIRNAME and each elt of ARRAY,
903 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
905 prefix_array (const char *dirname, char **array, size_t n)
908 size_t dirlen = strlen (dirname);
909 #if defined __MSDOS__ || defined WINDOWS32
911 # define DIRSEP_CHAR sep_char
913 # define DIRSEP_CHAR '/'
916 if (dirlen == 1 && dirname[0] == '/')
917 /* DIRNAME is just "/", so normal prepending would get us "//foo".
918 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
920 #if defined __MSDOS__ || defined WINDOWS32
923 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
924 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
926 else if (dirname[dirlen - 1] == ':')
928 /* DIRNAME is "d:". Use `:' instead of `/'. */
935 for (i = 0; i < n; ++i)
937 size_t eltlen = strlen (array[i]) + 1;
938 char *new = malloc (dirlen + 1 + eltlen);
947 char *endp = mempcpy (new, dirname, dirlen);
948 *endp++ = DIRSEP_CHAR;
949 mempcpy (endp, array[i], eltlen);
959 /* We must not compile this function twice. */
960 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
961 /* Return nonzero if PATTERN contains any metacharacters.
962 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
964 __glob_pattern_p (pattern, quote)
968 register const char *p;
971 for (p = pattern; *p != '\0'; ++p)
979 if (quote && p[1] != '\0')
996 weak_alias (__glob_pattern_p, glob_pattern_p)
1000 #endif /* !GLOB_ONLY_P */
1003 /* We put this in a separate function mainly to allow the memory
1004 allocated with alloca to be recycled. */
1005 #if !defined _LIBC || !defined GLOB_ONLY_P
1007 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1008 glob_t *pglob, int flags)
1010 size_t fnamelen = strlen (fname);
1011 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1015 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1016 fname, fnamelen + 1);
1018 return ((flags & GLOB_ALTDIRFUNC)
1019 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1020 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1025 /* Like `glob', but PATTERN is a final pathname component,
1026 and matches are searched for in DIRECTORY.
1027 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1028 The GLOB_APPEND flag is assumed to be set (always appends). */
1030 glob_in_dir (const char *pattern, const char *directory, int flags,
1031 int (*errfunc) (const char *, int),
1034 size_t dirlen = strlen (directory);
1035 void *stream = NULL;
1038 struct globlink *next;
1041 struct globlink *names = NULL;
1046 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1047 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1049 /* We need not do any tests. The PATTERN contains no meta
1050 characters and we must not return an error therefore the
1051 result will always contain exactly one name. */
1052 flags |= GLOB_NOCHECK;
1055 else if (meta == 0 &&
1056 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1058 /* Since we use the normal file functions we can also use stat()
1059 to verify the file is there. */
1062 size_t patlen = strlen (pattern);
1063 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1065 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1067 pattern, patlen + 1);
1068 if (((flags & GLOB_ALTDIRFUNC)
1069 ? (*pglob->gl_stat) (fullname, &st)
1070 : __stat64 (fullname, &st64)) == 0)
1071 /* We found this file to be existing. Now tell the rest
1072 of the function to copy this name into the result. */
1073 flags |= GLOB_NOCHECK;
1079 if (pattern[0] == '\0')
1081 /* This is a special case for matching directories like in
1083 names = __alloca (sizeof (struct globlink));
1084 names->name = malloc (1);
1085 if (names->name == NULL)
1087 names->name[0] = '\0';
1094 stream = ((flags & GLOB_ALTDIRFUNC)
1095 ? (*pglob->gl_opendir) (directory)
1096 : opendir (directory));
1099 if (errno != ENOTDIR
1100 && ((errfunc != NULL && (*errfunc) (directory, errno))
1101 || (flags & GLOB_ERR)))
1102 return GLOB_ABORTED;
1108 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1109 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1110 #if defined _AMIGA || defined __VMS
1115 flags |= GLOB_MAGCHAR;
1121 #if defined _LIBC && !defined COMPILE_GLOB64
1125 struct dirent64 d64;
1126 char room [offsetof (struct dirent64, d_name[0])
1131 if (flags & GLOB_ALTDIRFUNC)
1133 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1136 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1143 d = __readdir64 (stream);
1145 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1146 ? ((*pglob->gl_readdir) (stream))
1147 : __readdir (stream));
1151 if (! REAL_DIR_ENTRY (d))
1154 /* If we shall match only directories use the information
1155 provided by the dirent call if possible. */
1156 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1161 if (fnmatch (pattern, name, fnm_flags) == 0)
1163 /* ISDIR will often be incorrectly set to false
1164 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1165 don't care. It won't be used and we save the
1166 expensive call to stat. */
1168 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1169 ? GLOB_ONLYDIR : 0));
1170 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1171 || ((flags & need_dir_test)
1172 && is_dir_p (directory, dirlen, name,
1175 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1176 if ((flags & GLOB_ONLYDIR) && !isdir)
1180 struct globlink *new =
1181 __alloca (sizeof (struct globlink));
1183 len = _D_EXACT_NAMLEN (d);
1185 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1186 if (new->name == NULL)
1188 p = mempcpy (new->name, name, len);
1189 if ((flags & GLOB_MARK) && isdir)
1202 if (nfound == 0 && (flags & GLOB_NOCHECK))
1204 size_t len = strlen (pattern);
1206 names = __alloca (sizeof (struct globlink));
1208 names->name = malloc (len + 1);
1209 if (names->name == NULL)
1211 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1216 char **new_gl_pathv;
1219 = realloc (pglob->gl_pathv,
1220 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1222 if (new_gl_pathv == NULL)
1224 pglob->gl_pathv = new_gl_pathv;
1226 for (; names != NULL; names = names->next)
1227 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1228 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1230 pglob->gl_flags = flags;
1236 if (flags & GLOB_ALTDIRFUNC)
1237 (*pglob->gl_closedir) (stream);
1243 return nfound == 0 ? GLOB_NOMATCH : 0;
1248 if (flags & GLOB_ALTDIRFUNC)
1249 (*pglob->gl_closedir) (stream);
1254 while (names != NULL)
1256 if (names->name != NULL)
1258 names = names->next;
1260 return GLOB_NOSPACE;