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 # define __stat64(fname, buf) stat (fname, buf)
136 # define struct_stat64 struct stat
137 # define __stat(fname, buf) stat (fname, buf)
138 # define __alloca alloca
139 # define __readdir readdir
140 # define __readdir64 readdir64
141 # define __glob_pattern_p glob_pattern_p
147 #ifdef _SC_GETPW_R_SIZE_MAX
148 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
150 # define GETPW_R_SIZE_MAX() (-1)
152 #ifdef _SC_LOGIN_NAME_MAX
153 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
155 # define GET_LOGIN_NAME_MAX() (-1)
158 static const char *next_brace_sub (const char *begin, int flags) __THROW;
160 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
162 /* The results of opendir() in this file are not used with dirfd and fchdir,
163 therefore save some unnecessary work in fchdir.c. */
167 static int glob_in_dir (const char *pattern, const char *directory,
168 int flags, int (*errfunc) (const char *, int),
171 #if !defined _LIBC || !defined GLOB_ONLY_P
172 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
173 static int collated_compare (const void *, const void *) __THROW;
176 /* Find the end of the sub-pattern in a brace expression. */
178 next_brace_sub (const char *cp, int flags)
180 unsigned int depth = 0;
182 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
190 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
197 return *cp != '\0' ? cp : NULL;
200 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
202 /* Do glob searching for PATTERN, placing results in PGLOB.
203 The bits defined above may be set in FLAGS.
204 If a directory cannot be opened or read and ERRFUNC is not nil,
205 it is called with the pathname that caused the error, and the
206 `errno' value from the failing call; if it returns non-zero
207 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
208 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
209 Otherwise, `glob' returns zero. */
211 #ifdef GLOB_ATTRIBUTE
214 glob (pattern, flags, errfunc, pglob)
215 const char * restrict pattern;
217 int (*errfunc) (const char *, int);
218 glob_t * restrict pglob;
220 const char *filename;
226 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
228 __set_errno (EINVAL);
232 if (!(flags & GLOB_DOOFFS))
233 /* Have to do this so `globfree' knows where to start freeing. It
234 also makes all the code that uses gl_offs simpler. */
237 if (flags & GLOB_BRACE)
241 if (flags & GLOB_NOESCAPE)
242 begin = strchr (pattern, '{');
254 if (*begin == '\\' && begin[1] != '\0')
256 else if (*begin == '{')
265 /* Allocate working buffer large enough for our work. Note that
266 we have at least an opening and closing brace. */
274 char onealt[strlen (pattern) - 1];
276 char *onealt = malloc (strlen (pattern) - 1);
279 if (!(flags & GLOB_APPEND))
282 pglob->gl_pathv = NULL;
288 /* We know the prefix for all sub-patterns. */
289 alt_start = mempcpy (onealt, pattern, begin - pattern);
291 /* Find the first sub-pattern and at the same time find the
292 rest after the closing brace. */
293 next = next_brace_sub (begin + 1, flags);
296 /* It is an invalid expression. */
300 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
303 /* Now find the end of the whole brace expression. */
307 rest = next_brace_sub (rest + 1, flags);
310 /* It is an invalid expression. */
314 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
317 /* Please note that we now can be sure the brace expression
319 rest_len = strlen (++rest) + 1;
321 /* We have a brace expression. BEGIN points to the opening {,
322 NEXT points past the terminator of the first element, and END
323 points past the final }. We will accumulate result names from
324 recursive runs for each brace alternative in the buffer using
327 if (!(flags & GLOB_APPEND))
329 /* This call is to set a new vector, so clear out the
330 vector so we can append to it. */
332 pglob->gl_pathv = NULL;
334 firstc = pglob->gl_pathc;
341 /* Construct the new glob expression. */
342 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
344 result = glob (onealt,
345 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
346 | GLOB_APPEND), errfunc, pglob);
348 /* If we got an error, return it. */
349 if (result && result != GLOB_NOMATCH)
354 if (!(flags & GLOB_APPEND))
363 /* We saw the last entry. */
367 next = next_brace_sub (p, flags);
368 assert (next != NULL);
375 if (pglob->gl_pathc != firstc)
376 /* We found some entries. */
378 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
383 /* Find the filename. */
384 filename = strrchr (pattern, '/');
385 #if defined __MSDOS__ || defined WINDOWS32
386 /* The case of "d:pattern". Since `:' is not allowed in
387 file names, we can safely assume that wherever it
388 happens in pattern, it signals the filename part. This
389 is so we could some day support patterns like "[a-z]:foo". */
390 if (filename == NULL)
391 filename = strchr (pattern, ':');
392 #endif /* __MSDOS__ || WINDOWS32 */
393 if (filename == NULL)
395 /* This can mean two things: a simple name or "~name". The latter
396 case is nothing but a notation for a directory. */
397 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
400 dirlen = strlen (pattern);
402 /* Set FILENAME to NULL as a special flag. This is ugly but
403 other solutions would require much more code. We test for
404 this special case below. */
418 else if (filename == pattern)
428 dirlen = filename - pattern;
429 #if defined __MSDOS__ || defined WINDOWS32
431 || (filename > pattern + 1 && filename[-1] == ':'))
436 drive_spec = __alloca (dirlen + 1);
437 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
438 /* For now, disallow wildcards in the drive spec, to
439 prevent infinite recursion in glob. */
440 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
442 /* If this is "d:pattern", we need to copy `:' to DIRNAME
443 as well. If it's "d:/pattern", don't remove the slash
444 from "d:/", since "d:" and "d:/" are not the same.*/
447 newp = __alloca (dirlen + 1);
448 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
452 if (filename[0] == '\0'
453 #if defined __MSDOS__ || defined WINDOWS32
454 && dirname[dirlen - 1] != ':'
455 && (dirlen < 3 || dirname[dirlen - 2] != ':'
456 || dirname[dirlen - 1] != '/')
459 /* "pattern/". Expand "pattern", appending slashes. */
461 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
463 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
464 | (flags & GLOB_MARK));
469 if (!(flags & GLOB_APPEND))
472 if (!(flags & GLOB_DOOFFS))
473 pglob->gl_pathv = NULL;
477 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
478 if (pglob->gl_pathv == NULL)
481 for (i = 0; i <= pglob->gl_offs; ++i)
482 pglob->gl_pathv[i] = NULL;
486 oldcount = pglob->gl_pathc + pglob->gl_offs;
488 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
490 if (dirname[1] == '\0' || dirname[1] == '/')
492 /* Look up home directory. */
493 const char *home_dir = getenv ("HOME");
495 if (home_dir == NULL || home_dir[0] == '\0')
499 if (home_dir == NULL || home_dir[0] == '\0')
500 home_dir = "c:/users/default"; /* poor default */
502 if (home_dir == NULL || home_dir[0] == '\0')
506 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
509 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
512 name = __alloca (buflen);
514 success = getlogin_r (name, buflen) == 0;
518 # if defined HAVE_GETPWNAM_R || defined _LIBC
519 long int pwbuflen = GETPW_R_SIZE_MAX ();
526 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
527 Try a moderate value. */
530 pwtmpbuf = __alloca (pwbuflen);
532 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
541 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
545 pwtmpbuf = __alloca (pwbuflen);
553 home_dir = p->pw_dir;
556 if (home_dir == NULL || home_dir[0] == '\0')
558 if (flags & GLOB_TILDE_CHECK)
561 home_dir = "~"; /* No luck. */
563 # endif /* WINDOWS32 */
565 /* Now construct the full directory. */
566 if (dirname[1] == '\0')
571 size_t home_len = strlen (home_dir);
572 newp = __alloca (home_len + dirlen);
573 mempcpy (mempcpy (newp, home_dir, home_len),
574 &dirname[1], dirlen);
578 # if !defined _AMIGA && !defined WINDOWS32
581 char *end_name = strchr (dirname, '/');
582 const char *user_name;
583 const char *home_dir;
585 if (end_name == NULL)
586 user_name = dirname + 1;
590 newp = __alloca (end_name - dirname);
591 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
596 /* Look up specific user's home directory. */
599 # if defined HAVE_GETPWNAM_R || defined _LIBC
600 long int buflen = GETPW_R_SIZE_MAX ();
607 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
611 pwtmpbuf = __alloca (buflen);
613 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
621 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
624 pwtmpbuf = __alloca (buflen);
629 p = getpwnam (user_name);
632 home_dir = p->pw_dir;
636 /* If we found a home directory use this. */
637 if (home_dir != NULL)
640 size_t home_len = strlen (home_dir);
641 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
642 newp = __alloca (home_len + rest_len + 1);
643 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
644 end_name, rest_len)) = '\0';
648 if (flags & GLOB_TILDE_CHECK)
649 /* We have to regard it as an error if we cannot find the
653 # endif /* Not Amiga && not WINDOWS32. */
656 /* Now test whether we looked for "~" or "~NAME". In this case we
657 can give the answer now. */
658 if (filename == NULL)
663 /* Return the directory if we don't check for error or if it exists. */
664 if ((flags & GLOB_NOCHECK)
665 || (((flags & GLOB_ALTDIRFUNC)
666 ? ((*pglob->gl_stat) (dirname, &st) == 0
667 && S_ISDIR (st.st_mode))
668 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
670 int newcount = pglob->gl_pathc + pglob->gl_offs;
674 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
675 if (new_gl_pathv == NULL)
678 free (pglob->gl_pathv);
679 pglob->gl_pathv = NULL;
683 pglob->gl_pathv = new_gl_pathv;
685 pglob->gl_pathv[newcount] = strdup (dirname);
686 if (pglob->gl_pathv[newcount] == NULL)
688 pglob->gl_pathv[++newcount] = NULL;
690 pglob->gl_flags = flags;
699 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
701 /* The directory name contains metacharacters, so we
702 have to glob for the directory, and then glob for
703 the pattern in each directory found. */
707 if ((flags & GLOB_ALTDIRFUNC) != 0)
709 /* Use the alternative access functions also in the recursive
711 dirs.gl_opendir = pglob->gl_opendir;
712 dirs.gl_readdir = pglob->gl_readdir;
713 dirs.gl_closedir = pglob->gl_closedir;
714 dirs.gl_stat = pglob->gl_stat;
715 dirs.gl_lstat = pglob->gl_lstat;
718 status = glob (dirname,
719 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
721 | GLOB_NOSORT | GLOB_ONLYDIR),
726 /* We have successfully globbed the preceding directory name.
727 For each name we found, call glob_in_dir on it and FILENAME,
728 appending the results to PGLOB. */
729 for (i = 0; i < dirs.gl_pathc; ++i)
735 /* Make globbing interruptible in the bash shell. */
736 extern int interrupt_state;
746 old_pathc = pglob->gl_pathc;
747 status = glob_in_dir (filename, dirs.gl_pathv[i],
748 ((flags | GLOB_APPEND)
749 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
751 if (status == GLOB_NOMATCH)
752 /* No matches in this directory. Try the next. */
763 /* Stick the directory on the front of each name. */
764 if (prefix_array (dirs.gl_pathv[i],
765 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
766 pglob->gl_pathc - old_pathc))
775 flags |= GLOB_MAGCHAR;
777 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
778 But if we have not found any matching entry and the GLOB_NOCHECK
779 flag was set we must return the input pattern itself. */
780 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
783 if (flags & GLOB_NOCHECK)
785 int newcount = pglob->gl_pathc + pglob->gl_offs;
788 new_gl_pathv = realloc (pglob->gl_pathv,
789 (newcount + 2) * sizeof (char *));
790 if (new_gl_pathv == NULL)
795 pglob->gl_pathv = new_gl_pathv;
797 pglob->gl_pathv[newcount] = strdup (pattern);
798 if (pglob->gl_pathv[newcount] == NULL)
809 pglob->gl_pathv[newcount] = NULL;
810 pglob->gl_flags = flags;
823 int old_pathc = pglob->gl_pathc;
825 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
831 /* Stick the directory on the front of each name. */
832 if (prefix_array (dirname,
833 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
834 pglob->gl_pathc - old_pathc))
843 if (!(flags & GLOB_NOSORT))
845 /* Sort the vector. */
846 qsort (&pglob->gl_pathv[oldcount],
847 pglob->gl_pathc + pglob->gl_offs - oldcount,
848 sizeof (char *), collated_compare);
853 #if defined _LIBC && !defined glob
854 libc_hidden_def (glob)
858 #if !defined _LIBC || !defined GLOB_ONLY_P
860 /* Free storage allocated in PGLOB by a previous `glob' call. */
863 register glob_t *pglob;
865 if (pglob->gl_pathv != NULL)
868 for (i = 0; i < pglob->gl_pathc; ++i)
869 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
870 free (pglob->gl_pathv[pglob->gl_offs + i]);
871 free (pglob->gl_pathv);
872 pglob->gl_pathv = NULL;
875 #if defined _LIBC && !defined globfree
876 libc_hidden_def (globfree)
880 /* Do a collated comparison of A and B. */
882 collated_compare (const void *a, const void *b)
884 char *const *ps1 = a; char *s1 = *ps1;
885 char *const *ps2 = b; char *s2 = *ps2;
893 return strcoll (s1, s2);
897 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
898 elements in place. Return nonzero if out of memory, zero if successful.
899 A slash is inserted between DIRNAME and each elt of ARRAY,
900 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
902 prefix_array (const char *dirname, char **array, size_t n)
905 size_t dirlen = strlen (dirname);
906 #if defined __MSDOS__ || defined WINDOWS32
908 # define DIRSEP_CHAR sep_char
910 # define DIRSEP_CHAR '/'
913 if (dirlen == 1 && dirname[0] == '/')
914 /* DIRNAME is just "/", so normal prepending would get us "//foo".
915 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
917 #if defined __MSDOS__ || defined WINDOWS32
920 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
921 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
923 else if (dirname[dirlen - 1] == ':')
925 /* DIRNAME is "d:". Use `:' instead of `/'. */
932 for (i = 0; i < n; ++i)
934 size_t eltlen = strlen (array[i]) + 1;
935 char *new = malloc (dirlen + 1 + eltlen);
944 char *endp = mempcpy (new, dirname, dirlen);
945 *endp++ = DIRSEP_CHAR;
946 mempcpy (endp, array[i], eltlen);
956 /* We must not compile this function twice. */
957 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
958 /* Return nonzero if PATTERN contains any metacharacters.
959 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
961 __glob_pattern_p (pattern, quote)
965 register const char *p;
968 for (p = pattern; *p != '\0'; ++p)
976 if (quote && p[1] != '\0')
993 weak_alias (__glob_pattern_p, glob_pattern_p)
997 #endif /* !GLOB_ONLY_P */
1000 /* We put this in a separate function mainly to allow the memory
1001 allocated with alloca to be recycled. */
1002 #if !defined _LIBC || !defined GLOB_ONLY_P
1004 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1005 glob_t *pglob, int flags)
1007 size_t fnamelen = strlen (fname);
1008 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1012 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1013 fname, fnamelen + 1);
1015 return ((flags & GLOB_ALTDIRFUNC)
1016 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1017 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1022 /* Like `glob', but PATTERN is a final pathname component,
1023 and matches are searched for in DIRECTORY.
1024 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1025 The GLOB_APPEND flag is assumed to be set (always appends). */
1027 glob_in_dir (const char *pattern, const char *directory, int flags,
1028 int (*errfunc) (const char *, int),
1031 size_t dirlen = strlen (directory);
1032 void *stream = NULL;
1035 struct globlink *next;
1038 struct globlink *names = NULL;
1043 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1044 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1046 /* We need not do any tests. The PATTERN contains no meta
1047 characters and we must not return an error therefore the
1048 result will always contain exactly one name. */
1049 flags |= GLOB_NOCHECK;
1052 else if (meta == 0 &&
1053 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1055 /* Since we use the normal file functions we can also use stat()
1056 to verify the file is there. */
1059 size_t patlen = strlen (pattern);
1060 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1062 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1064 pattern, patlen + 1);
1065 if (((flags & GLOB_ALTDIRFUNC)
1066 ? (*pglob->gl_stat) (fullname, &st)
1067 : __stat64 (fullname, &st64)) == 0)
1068 /* We found this file to be existing. Now tell the rest
1069 of the function to copy this name into the result. */
1070 flags |= GLOB_NOCHECK;
1076 if (pattern[0] == '\0')
1078 /* This is a special case for matching directories like in
1080 names = __alloca (sizeof (struct globlink));
1081 names->name = malloc (1);
1082 if (names->name == NULL)
1084 names->name[0] = '\0';
1091 stream = ((flags & GLOB_ALTDIRFUNC)
1092 ? (*pglob->gl_opendir) (directory)
1093 : opendir (directory));
1096 if (errno != ENOTDIR
1097 && ((errfunc != NULL && (*errfunc) (directory, errno))
1098 || (flags & GLOB_ERR)))
1099 return GLOB_ABORTED;
1105 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1106 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1107 #if defined _AMIGA || defined __VMS
1112 flags |= GLOB_MAGCHAR;
1118 #if defined _LIBC && !defined COMPILE_GLOB64
1122 struct dirent64 d64;
1123 char room [offsetof (struct dirent64, d_name[0])
1128 if (flags & GLOB_ALTDIRFUNC)
1130 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1133 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1140 d = __readdir64 (stream);
1142 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1143 ? ((*pglob->gl_readdir) (stream))
1144 : __readdir (stream));
1148 if (! REAL_DIR_ENTRY (d))
1151 /* If we shall match only directories use the information
1152 provided by the dirent call if possible. */
1153 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1158 if (fnmatch (pattern, name, fnm_flags) == 0)
1160 /* ISDIR will often be incorrectly set to false
1161 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1162 don't care. It won't be used and we save the
1163 expensive call to stat. */
1165 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1166 ? GLOB_ONLYDIR : 0));
1167 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1168 || ((flags & need_dir_test)
1169 && is_dir_p (directory, dirlen, name,
1172 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1173 if ((flags & GLOB_ONLYDIR) && !isdir)
1177 struct globlink *new =
1178 __alloca (sizeof (struct globlink));
1180 len = _D_EXACT_NAMLEN (d);
1182 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1183 if (new->name == NULL)
1185 p = mempcpy (new->name, name, len);
1186 if ((flags & GLOB_MARK) && isdir)
1199 if (nfound == 0 && (flags & GLOB_NOCHECK))
1201 size_t len = strlen (pattern);
1203 names = __alloca (sizeof (struct globlink));
1205 names->name = malloc (len + 1);
1206 if (names->name == NULL)
1208 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1213 char **new_gl_pathv;
1216 = realloc (pglob->gl_pathv,
1217 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1219 if (new_gl_pathv == NULL)
1221 pglob->gl_pathv = new_gl_pathv;
1223 for (; names != NULL; names = names->next)
1224 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1225 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1227 pglob->gl_flags = flags;
1233 if (flags & GLOB_ALTDIRFUNC)
1234 (*pglob->gl_closedir) (stream);
1240 return nfound == 0 ? GLOB_NOMATCH : 0;
1245 if (flags & GLOB_ALTDIRFUNC)
1246 (*pglob->gl_closedir) (stream);
1251 while (names != NULL)
1253 if (names->name != NULL)
1255 names = names->next;
1257 return GLOB_NOSPACE;