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 static int glob_in_dir (const char *pattern, const char *directory,
166 int flags, int (*errfunc) (const char *, int),
169 #if !defined _LIBC || !defined GLOB_ONLY_P
170 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
171 static int collated_compare (const void *, const void *) __THROW;
174 /* Find the end of the sub-pattern in a brace expression. */
176 next_brace_sub (const char *cp, int flags)
178 unsigned int depth = 0;
180 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
188 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
195 return *cp != '\0' ? cp : NULL;
198 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
200 /* Do glob searching for PATTERN, placing results in PGLOB.
201 The bits defined above may be set in FLAGS.
202 If a directory cannot be opened or read and ERRFUNC is not nil,
203 it is called with the pathname that caused the error, and the
204 `errno' value from the failing call; if it returns non-zero
205 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
206 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
207 Otherwise, `glob' returns zero. */
209 #ifdef GLOB_ATTRIBUTE
212 glob (pattern, flags, errfunc, pglob)
215 int (*errfunc) (const char *, int);
218 const char *filename;
224 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
226 __set_errno (EINVAL);
230 if (!(flags & GLOB_DOOFFS))
231 /* Have to do this so `globfree' knows where to start freeing. It
232 also makes all the code that uses gl_offs simpler. */
235 if (flags & GLOB_BRACE)
239 if (flags & GLOB_NOESCAPE)
240 begin = strchr (pattern, '{');
252 if (*begin == '\\' && begin[1] != '\0')
254 else if (*begin == '{')
263 /* Allocate working buffer large enough for our work. Note that
264 we have at least an opening and closing brace. */
272 char onealt[strlen (pattern) - 1];
274 char *onealt = malloc (strlen (pattern) - 1);
277 if (!(flags & GLOB_APPEND))
280 pglob->gl_pathv = NULL;
286 /* We know the prefix for all sub-patterns. */
287 alt_start = mempcpy (onealt, pattern, begin - pattern);
289 /* Find the first sub-pattern and at the same time find the
290 rest after the closing brace. */
291 next = next_brace_sub (begin + 1, flags);
294 /* It is an invalid expression. */
298 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
301 /* Now find the end of the whole brace expression. */
305 rest = next_brace_sub (rest + 1, flags);
308 /* It is an invalid expression. */
312 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
315 /* Please note that we now can be sure the brace expression
317 rest_len = strlen (++rest) + 1;
319 /* We have a brace expression. BEGIN points to the opening {,
320 NEXT points past the terminator of the first element, and END
321 points past the final }. We will accumulate result names from
322 recursive runs for each brace alternative in the buffer using
325 if (!(flags & GLOB_APPEND))
327 /* This call is to set a new vector, so clear out the
328 vector so we can append to it. */
330 pglob->gl_pathv = NULL;
332 firstc = pglob->gl_pathc;
339 /* Construct the new glob expression. */
340 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
342 result = glob (onealt,
343 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
344 | GLOB_APPEND), errfunc, pglob);
346 /* If we got an error, return it. */
347 if (result && result != GLOB_NOMATCH)
352 if (!(flags & GLOB_APPEND))
361 /* We saw the last entry. */
365 next = next_brace_sub (p, flags);
366 assert (next != NULL);
373 if (pglob->gl_pathc != firstc)
374 /* We found some entries. */
376 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
381 /* Find the filename. */
382 filename = strrchr (pattern, '/');
383 #if defined __MSDOS__ || defined WINDOWS32
384 /* The case of "d:pattern". Since `:' is not allowed in
385 file names, we can safely assume that wherever it
386 happens in pattern, it signals the filename part. This
387 is so we could some day support patterns like "[a-z]:foo". */
388 if (filename == NULL)
389 filename = strchr (pattern, ':');
390 #endif /* __MSDOS__ || WINDOWS32 */
391 if (filename == NULL)
393 /* This can mean two things: a simple name or "~name". The latter
394 case is nothing but a notation for a directory. */
395 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
398 dirlen = strlen (pattern);
400 /* Set FILENAME to NULL as a special flag. This is ugly but
401 other solutions would require much more code. We test for
402 this special case below. */
416 else if (filename == pattern)
426 dirlen = filename - pattern;
427 #if defined __MSDOS__ || defined WINDOWS32
429 || (filename > pattern + 1 && filename[-1] == ':'))
434 drive_spec = __alloca (dirlen + 1);
435 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
436 /* For now, disallow wildcards in the drive spec, to
437 prevent infinite recursion in glob. */
438 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
440 /* If this is "d:pattern", we need to copy `:' to DIRNAME
441 as well. If it's "d:/pattern", don't remove the slash
442 from "d:/", since "d:" and "d:/" are not the same.*/
445 newp = __alloca (dirlen + 1);
446 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
450 if (filename[0] == '\0'
451 #if defined __MSDOS__ || defined WINDOWS32
452 && dirname[dirlen - 1] != ':'
453 && (dirlen < 3 || dirname[dirlen - 2] != ':'
454 || dirname[dirlen - 1] != '/')
457 /* "pattern/". Expand "pattern", appending slashes. */
459 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
461 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
462 | (flags & GLOB_MARK));
467 if (!(flags & GLOB_APPEND))
470 if (!(flags & GLOB_DOOFFS))
471 pglob->gl_pathv = NULL;
475 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
476 if (pglob->gl_pathv == NULL)
479 for (i = 0; i <= pglob->gl_offs; ++i)
480 pglob->gl_pathv[i] = NULL;
484 oldcount = pglob->gl_pathc + pglob->gl_offs;
486 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
488 if (dirname[1] == '\0' || dirname[1] == '/')
490 /* Look up home directory. */
491 const char *home_dir = getenv ("HOME");
493 if (home_dir == NULL || home_dir[0] == '\0')
497 if (home_dir == NULL || home_dir[0] == '\0')
498 home_dir = "c:/users/default"; /* poor default */
500 if (home_dir == NULL || home_dir[0] == '\0')
504 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
507 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
510 name = __alloca (buflen);
512 success = getlogin_r (name, buflen) == 0;
516 # if defined HAVE_GETPWNAM_R || defined _LIBC
517 long int pwbuflen = GETPW_R_SIZE_MAX ();
524 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
525 Try a moderate value. */
528 pwtmpbuf = __alloca (pwbuflen);
530 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
539 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
543 pwtmpbuf = __alloca (pwbuflen);
551 home_dir = p->pw_dir;
554 if (home_dir == NULL || home_dir[0] == '\0')
556 if (flags & GLOB_TILDE_CHECK)
559 home_dir = "~"; /* No luck. */
561 # endif /* WINDOWS32 */
563 /* Now construct the full directory. */
564 if (dirname[1] == '\0')
569 size_t home_len = strlen (home_dir);
570 newp = __alloca (home_len + dirlen);
571 mempcpy (mempcpy (newp, home_dir, home_len),
572 &dirname[1], dirlen);
576 # if !defined _AMIGA && !defined WINDOWS32
579 char *end_name = strchr (dirname, '/');
580 const char *user_name;
581 const char *home_dir;
583 if (end_name == NULL)
584 user_name = dirname + 1;
588 newp = __alloca (end_name - dirname);
589 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
594 /* Look up specific user's home directory. */
597 # if defined HAVE_GETPWNAM_R || defined _LIBC
598 long int buflen = GETPW_R_SIZE_MAX ();
605 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
609 pwtmpbuf = __alloca (buflen);
611 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
619 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
622 pwtmpbuf = __alloca (buflen);
627 p = getpwnam (user_name);
630 home_dir = p->pw_dir;
634 /* If we found a home directory use this. */
635 if (home_dir != NULL)
638 size_t home_len = strlen (home_dir);
639 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
640 newp = __alloca (home_len + rest_len + 1);
641 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
642 end_name, rest_len)) = '\0';
646 if (flags & GLOB_TILDE_CHECK)
647 /* We have to regard it as an error if we cannot find the
651 # endif /* Not Amiga && not WINDOWS32. */
654 /* Now test whether we looked for "~" or "~NAME". In this case we
655 can give the answer now. */
656 if (filename == NULL)
661 /* Return the directory if we don't check for error or if it exists. */
662 if ((flags & GLOB_NOCHECK)
663 || (((flags & GLOB_ALTDIRFUNC)
664 ? ((*pglob->gl_stat) (dirname, &st) == 0
665 && S_ISDIR (st.st_mode))
666 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
668 int newcount = pglob->gl_pathc + pglob->gl_offs;
672 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
673 if (new_gl_pathv == NULL)
676 free (pglob->gl_pathv);
677 pglob->gl_pathv = NULL;
681 pglob->gl_pathv = new_gl_pathv;
683 pglob->gl_pathv[newcount] = strdup (dirname);
684 if (pglob->gl_pathv[newcount] == NULL)
686 pglob->gl_pathv[++newcount] = NULL;
688 pglob->gl_flags = flags;
697 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
699 /* The directory name contains metacharacters, so we
700 have to glob for the directory, and then glob for
701 the pattern in each directory found. */
705 if ((flags & GLOB_ALTDIRFUNC) != 0)
707 /* Use the alternative access functions also in the recursive
709 dirs.gl_opendir = pglob->gl_opendir;
710 dirs.gl_readdir = pglob->gl_readdir;
711 dirs.gl_closedir = pglob->gl_closedir;
712 dirs.gl_stat = pglob->gl_stat;
713 dirs.gl_lstat = pglob->gl_lstat;
716 status = glob (dirname,
717 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
719 | GLOB_NOSORT | GLOB_ONLYDIR),
724 /* We have successfully globbed the preceding directory name.
725 For each name we found, call glob_in_dir on it and FILENAME,
726 appending the results to PGLOB. */
727 for (i = 0; i < dirs.gl_pathc; ++i)
733 /* Make globbing interruptible in the bash shell. */
734 extern int interrupt_state;
744 old_pathc = pglob->gl_pathc;
745 status = glob_in_dir (filename, dirs.gl_pathv[i],
746 ((flags | GLOB_APPEND)
747 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
749 if (status == GLOB_NOMATCH)
750 /* No matches in this directory. Try the next. */
761 /* Stick the directory on the front of each name. */
762 if (prefix_array (dirs.gl_pathv[i],
763 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
764 pglob->gl_pathc - old_pathc))
773 flags |= GLOB_MAGCHAR;
775 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
776 But if we have not found any matching entry and the GLOB_NOCHECK
777 flag was set we must return the input pattern itself. */
778 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
781 if (flags & GLOB_NOCHECK)
783 int newcount = pglob->gl_pathc + pglob->gl_offs;
786 new_gl_pathv = realloc (pglob->gl_pathv,
787 (newcount + 2) * sizeof (char *));
788 if (new_gl_pathv == NULL)
793 pglob->gl_pathv = new_gl_pathv;
795 pglob->gl_pathv[newcount] = strdup (pattern);
796 if (pglob->gl_pathv[newcount] == NULL)
807 pglob->gl_pathv[newcount] = NULL;
808 pglob->gl_flags = flags;
821 int old_pathc = pglob->gl_pathc;
823 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
829 /* Stick the directory on the front of each name. */
830 if (prefix_array (dirname,
831 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
832 pglob->gl_pathc - old_pathc))
841 if (!(flags & GLOB_NOSORT))
843 /* Sort the vector. */
844 qsort (&pglob->gl_pathv[oldcount],
845 pglob->gl_pathc + pglob->gl_offs - oldcount,
846 sizeof (char *), collated_compare);
851 #if defined _LIBC && !defined glob
852 libc_hidden_def (glob)
856 #if !defined _LIBC || !defined GLOB_ONLY_P
858 /* Free storage allocated in PGLOB by a previous `glob' call. */
861 register glob_t *pglob;
863 if (pglob->gl_pathv != NULL)
866 for (i = 0; i < pglob->gl_pathc; ++i)
867 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
868 free (pglob->gl_pathv[pglob->gl_offs + i]);
869 free (pglob->gl_pathv);
870 pglob->gl_pathv = NULL;
873 #if defined _LIBC && !defined globfree
874 libc_hidden_def (globfree)
878 /* Do a collated comparison of A and B. */
880 collated_compare (const void *a, const void *b)
882 char *const *ps1 = a; char *s1 = *ps1;
883 char *const *ps2 = b; char *s2 = *ps2;
891 return strcoll (s1, s2);
895 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
896 elements in place. Return nonzero if out of memory, zero if successful.
897 A slash is inserted between DIRNAME and each elt of ARRAY,
898 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
900 prefix_array (const char *dirname, char **array, size_t n)
903 size_t dirlen = strlen (dirname);
904 #if defined __MSDOS__ || defined WINDOWS32
906 # define DIRSEP_CHAR sep_char
908 # define DIRSEP_CHAR '/'
911 if (dirlen == 1 && dirname[0] == '/')
912 /* DIRNAME is just "/", so normal prepending would get us "//foo".
913 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
915 #if defined __MSDOS__ || defined WINDOWS32
918 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
919 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
921 else if (dirname[dirlen - 1] == ':')
923 /* DIRNAME is "d:". Use `:' instead of `/'. */
930 for (i = 0; i < n; ++i)
932 size_t eltlen = strlen (array[i]) + 1;
933 char *new = malloc (dirlen + 1 + eltlen);
942 char *endp = mempcpy (new, dirname, dirlen);
943 *endp++ = DIRSEP_CHAR;
944 mempcpy (endp, array[i], eltlen);
954 /* We must not compile this function twice. */
955 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
956 /* Return nonzero if PATTERN contains any metacharacters.
957 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
959 __glob_pattern_p (pattern, quote)
963 register const char *p;
966 for (p = pattern; *p != '\0'; ++p)
974 if (quote && p[1] != '\0')
991 weak_alias (__glob_pattern_p, glob_pattern_p)
995 #endif /* !GLOB_ONLY_P */
998 /* We put this in a separate function mainly to allow the memory
999 allocated with alloca to be recycled. */
1000 #if !defined _LIBC || !defined GLOB_ONLY_P
1002 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1003 glob_t *pglob, int flags)
1005 size_t fnamelen = strlen (fname);
1006 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1010 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1011 fname, fnamelen + 1);
1013 return ((flags & GLOB_ALTDIRFUNC)
1014 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1015 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1020 /* Like `glob', but PATTERN is a final pathname component,
1021 and matches are searched for in DIRECTORY.
1022 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1023 The GLOB_APPEND flag is assumed to be set (always appends). */
1025 glob_in_dir (const char *pattern, const char *directory, int flags,
1026 int (*errfunc) (const char *, int),
1029 size_t dirlen = strlen (directory);
1030 void *stream = NULL;
1033 struct globlink *next;
1036 struct globlink *names = NULL;
1041 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1042 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1044 /* We need not do any tests. The PATTERN contains no meta
1045 characters and we must not return an error therefore the
1046 result will always contain exactly one name. */
1047 flags |= GLOB_NOCHECK;
1050 else if (meta == 0 &&
1051 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1053 /* Since we use the normal file functions we can also use stat()
1054 to verify the file is there. */
1057 size_t patlen = strlen (pattern);
1058 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1060 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1062 pattern, patlen + 1);
1063 if (((flags & GLOB_ALTDIRFUNC)
1064 ? (*pglob->gl_stat) (fullname, &st)
1065 : __stat64 (fullname, &st64)) == 0)
1066 /* We found this file to be existing. Now tell the rest
1067 of the function to copy this name into the result. */
1068 flags |= GLOB_NOCHECK;
1074 if (pattern[0] == '\0')
1076 /* This is a special case for matching directories like in
1078 names = __alloca (sizeof (struct globlink));
1079 names->name = malloc (1);
1080 if (names->name == NULL)
1082 names->name[0] = '\0';
1089 stream = ((flags & GLOB_ALTDIRFUNC)
1090 ? (*pglob->gl_opendir) (directory)
1091 : opendir (directory));
1094 if (errno != ENOTDIR
1095 && ((errfunc != NULL && (*errfunc) (directory, errno))
1096 || (flags & GLOB_ERR)))
1097 return GLOB_ABORTED;
1103 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1104 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1105 #if defined _AMIGA || defined __VMS
1110 flags |= GLOB_MAGCHAR;
1116 #if defined _LIBC && !defined COMPILE_GLOB64
1120 struct dirent64 d64;
1121 char room [offsetof (struct dirent64, d_name[0])
1126 if (flags & GLOB_ALTDIRFUNC)
1128 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1131 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1138 d = __readdir64 (stream);
1140 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1141 ? ((*pglob->gl_readdir) (stream))
1142 : __readdir (stream));
1146 if (! REAL_DIR_ENTRY (d))
1149 /* If we shall match only directories use the information
1150 provided by the dirent call if possible. */
1151 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1156 if (fnmatch (pattern, name, fnm_flags) == 0)
1158 /* ISDIR will often be incorrectly set to false
1159 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1160 don't care. It won't be used and we save the
1161 expensive call to stat. */
1163 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1164 ? GLOB_ONLYDIR : 0));
1165 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1166 || ((flags & need_dir_test)
1167 && is_dir_p (directory, dirlen, name,
1170 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1171 if ((flags & GLOB_ONLYDIR) && !isdir)
1175 struct globlink *new =
1176 __alloca (sizeof (struct globlink));
1178 len = _D_EXACT_NAMLEN (d);
1180 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1181 if (new->name == NULL)
1183 p = mempcpy (new->name, name, len);
1184 if ((flags & GLOB_MARK) && isdir)
1197 if (nfound == 0 && (flags & GLOB_NOCHECK))
1199 size_t len = strlen (pattern);
1201 names = __alloca (sizeof (struct globlink));
1203 names->name = malloc (len + 1);
1204 if (names->name == NULL)
1206 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1211 char **new_gl_pathv;
1214 = realloc (pglob->gl_pathv,
1215 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1217 if (new_gl_pathv == NULL)
1219 pglob->gl_pathv = new_gl_pathv;
1221 for (; names != NULL; names = names->next)
1222 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1223 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1225 pglob->gl_flags = flags;
1231 if (flags & GLOB_ALTDIRFUNC)
1232 (*pglob->gl_closedir) (stream);
1238 return nfound == 0 ? GLOB_NOMATCH : 0;
1243 if (flags & GLOB_ALTDIRFUNC)
1244 (*pglob->gl_closedir) (stream);
1249 while (names != NULL)
1251 if (names->name != NULL)
1253 names = names->next;
1255 return GLOB_NOSPACE;