1 /* Copyright (C) 1991-2002, 2003, 2004, 2005 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
37 #if defined HAVE_UNISTD_H || defined _LIBC
40 # ifdef _POSIX_VERSION
50 # define __set_errno(val) errno = (val)
53 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
55 # define NAMLEN(dirent) strlen((dirent)->d_name)
57 # define dirent direct
58 # define NAMLEN(dirent) (dirent)->d_namlen
59 # ifdef HAVE_SYS_NDIR_H
60 # include <sys/ndir.h>
62 # ifdef HAVE_SYS_DIR_H
70 # endif /* HAVE_VMSDIR_H */
74 /* In GNU systems, <dirent.h> defines this macro for us. */
77 # define NAMLEN(d) _D_NAMLEN(d)
80 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
81 if the `d_type' member for `struct dirent' is available.
82 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
83 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
84 /* True if the directory entry D must be of type T. */
85 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
87 /* True if the directory entry D might be a symbolic link. */
88 # define DIRENT_MIGHT_BE_SYMLINK(d) \
89 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
91 /* True if the directory entry D might be a directory. */
92 # define DIRENT_MIGHT_BE_DIR(d) \
93 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
95 #else /* !HAVE_D_TYPE */
96 # define DIRENT_MUST_BE(d, t) false
97 # define DIRENT_MIGHT_BE_SYMLINK(d) true
98 # define DIRENT_MIGHT_BE_DIR(d) true
99 #endif /* HAVE_D_TYPE */
101 /* If the system has the `struct dirent64' type we use it internally. */
102 #if defined _LIBC && !defined COMPILE_GLOB64
103 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
104 # define CONVERT_D_NAMLEN(d64, d32)
106 # define CONVERT_D_NAMLEN(d64, d32) \
107 (d64)->d_namlen = (d32)->d_namlen;
110 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
111 # define CONVERT_D_INO(d64, d32)
113 # define CONVERT_D_INO(d64, d32) \
114 (d64)->d_ino = (d32)->d_ino;
117 # ifdef _DIRENT_HAVE_D_TYPE
118 # define CONVERT_D_TYPE(d64, d32) \
119 (d64)->d_type = (d32)->d_type;
121 # define CONVERT_D_TYPE(d64, d32)
124 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
125 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
126 CONVERT_D_NAMLEN (d64, d32) \
127 CONVERT_D_INO (d64, d32) \
128 CONVERT_D_TYPE (d64, d32)
132 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
133 /* Posix does not require that the d_ino field be present, and some
134 systems do not provide it. */
135 # define REAL_DIR_ENTRY(dp) 1
137 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
143 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
146 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
153 # define strdup(str) __strdup (str)
154 # define sysconf(id) __sysconf (id)
155 # define closedir(dir) __closedir (dir)
156 # define opendir(name) __opendir (name)
157 # define readdir(str) __readdir64 (str)
158 # define getpwnam_r(name, bufp, buf, len, res) \
159 __getpwnam_r (name, bufp, buf, len, res)
161 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
163 # define struct_stat64 struct stat64
165 # include "getlogin_r.h"
166 # include "mempcpy.h"
167 # include "stat-macros.h"
169 # define __stat64(fname, buf) stat (fname, buf)
170 # define struct_stat64 struct stat
171 # define __stat(fname, buf) stat (fname, buf)
172 # define __alloca alloca
173 # define __readdir readdir
174 # define __readdir64 readdir64
175 # define __glob_pattern_p glob_pattern_p
181 #ifdef _SC_GETPW_R_SIZE_MAX
182 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
184 # define GETPW_R_SIZE_MAX() (-1)
186 #ifdef _SC_LOGIN_NAME_MAX
187 # define LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
189 # define LOGIN_NAME_MAX() (-1)
192 static const char *next_brace_sub (const char *begin, int flags) __THROW;
194 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
196 static int glob_in_dir (const char *pattern, const char *directory,
197 int flags, int (*errfunc) (const char *, int),
200 #if !defined _LIBC || !defined GLOB_ONLY_P
201 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
202 static int collated_compare (const void *, const void *) __THROW;
205 /* Find the end of the sub-pattern in a brace expression. */
207 next_brace_sub (const char *cp, int flags)
209 unsigned int depth = 0;
211 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
219 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
226 return *cp != '\0' ? cp : NULL;
229 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
231 /* Do glob searching for PATTERN, placing results in PGLOB.
232 The bits defined above may be set in FLAGS.
233 If a directory cannot be opened or read and ERRFUNC is not nil,
234 it is called with the pathname that caused the error, and the
235 `errno' value from the failing call; if it returns non-zero
236 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
237 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
238 Otherwise, `glob' returns zero. */
240 #ifdef GLOB_ATTRIBUTE
243 glob (const char *pattern, int flags,
244 int (*errfunc) (const char *, int),
247 const char *filename;
253 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
255 __set_errno (EINVAL);
259 if (!(flags & GLOB_DOOFFS))
260 /* Have to do this so `globfree' knows where to start freeing. It
261 also makes all the code that uses gl_offs simpler. */
264 if (flags & GLOB_BRACE)
268 if (flags & GLOB_NOESCAPE)
269 begin = strchr (pattern, '{');
281 if (*begin == '\\' && begin[1] != '\0')
283 else if (*begin == '{')
292 /* Allocate working buffer large enough for our work. Note that
293 we have at least an opening and closing brace. */
301 char onealt[strlen (pattern) - 1];
303 char *onealt = malloc (strlen (pattern) - 1);
306 if (!(flags & GLOB_APPEND))
309 pglob->gl_pathv = NULL;
315 /* We know the prefix for all sub-patterns. */
316 alt_start = mempcpy (onealt, pattern, begin - pattern);
318 /* Find the first sub-pattern and at the same time find the
319 rest after the closing brace. */
320 next = next_brace_sub (begin + 1, flags);
323 /* It is an illegal expression. */
327 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
330 /* Now find the end of the whole brace expression. */
334 rest = next_brace_sub (rest + 1, flags);
337 /* It is an illegal expression. */
341 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
344 /* Please note that we now can be sure the brace expression
346 rest_len = strlen (++rest) + 1;
348 /* We have a brace expression. BEGIN points to the opening {,
349 NEXT points past the terminator of the first element, and END
350 points past the final }. We will accumulate result names from
351 recursive runs for each brace alternative in the buffer using
354 if (!(flags & GLOB_APPEND))
356 /* This call is to set a new vector, so clear out the
357 vector so we can append to it. */
359 pglob->gl_pathv = NULL;
361 firstc = pglob->gl_pathc;
368 /* Construct the new glob expression. */
369 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
371 result = glob (onealt,
372 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
373 | GLOB_APPEND), errfunc, pglob);
375 /* If we got an error, return it. */
376 if (result && result != GLOB_NOMATCH)
381 if (!(flags & GLOB_APPEND))
390 /* We saw the last entry. */
394 next = next_brace_sub (p, flags);
395 assert (next != NULL);
402 if (pglob->gl_pathc != firstc)
403 /* We found some entries. */
405 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
410 /* Find the filename. */
411 filename = strrchr (pattern, '/');
412 #if defined __MSDOS__ || defined WINDOWS32
413 /* The case of "d:pattern". Since `:' is not allowed in
414 file names, we can safely assume that wherever it
415 happens in pattern, it signals the filename part. This
416 is so we could some day support patterns like "[a-z]:foo". */
417 if (filename == NULL)
418 filename = strchr (pattern, ':');
419 #endif /* __MSDOS__ || WINDOWS32 */
420 if (filename == NULL)
422 /* This can mean two things: a simple name or "~name". The latter
423 case is nothing but a notation for a directory. */
424 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
427 dirlen = strlen (pattern);
429 /* Set FILENAME to NULL as a special flag. This is ugly but
430 other solutions would require much more code. We test for
431 this special case below. */
445 else if (filename == pattern)
455 dirlen = filename - pattern;
456 #if defined __MSDOS__ || defined WINDOWS32
458 || (filename > pattern + 1 && filename[-1] == ':'))
463 drive_spec = __alloca (dirlen + 1);
464 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
465 /* For now, disallow wildcards in the drive spec, to
466 prevent infinite recursion in glob. */
467 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
469 /* If this is "d:pattern", we need to copy `:' to DIRNAME
470 as well. If it's "d:/pattern", don't remove the slash
471 from "d:/", since "d:" and "d:/" are not the same.*/
474 newp = __alloca (dirlen + 1);
475 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
479 if (filename[0] == '\0'
480 #if defined __MSDOS__ || defined WINDOWS32
481 && dirname[dirlen - 1] != ':'
482 && (dirlen < 3 || dirname[dirlen - 2] != ':'
483 || dirname[dirlen - 1] != '/')
486 /* "pattern/". Expand "pattern", appending slashes. */
488 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
490 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
491 | (flags & GLOB_MARK));
496 if (!(flags & GLOB_APPEND))
499 if (!(flags & GLOB_DOOFFS))
500 pglob->gl_pathv = NULL;
504 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
505 if (pglob->gl_pathv == NULL)
508 for (i = 0; i <= pglob->gl_offs; ++i)
509 pglob->gl_pathv[i] = NULL;
513 oldcount = pglob->gl_pathc + pglob->gl_offs;
516 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
518 if (dirname[1] == '\0' || dirname[1] == '/')
520 /* Look up home directory. */
521 const char *home_dir = getenv ("HOME");
523 if (home_dir == NULL || home_dir[0] == '\0')
527 if (home_dir == NULL || home_dir[0] == '\0')
528 home_dir = "c:/users/default"; /* poor default */
530 if (home_dir == NULL || home_dir[0] == '\0')
534 size_t buflen = LOGIN_NAME_MAX() + 1;
537 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
540 name = __alloca (buflen);
542 success = getlogin_r (name, buflen) == 0;
546 # if defined HAVE_GETPWNAM_R || defined _LIBC
547 long int pwbuflen = GETPW_R_SIZE_MAX ();
554 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
555 Try a moderate value. */
558 pwtmpbuf = __alloca (pwbuflen);
560 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
569 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
573 pwtmpbuf = __alloca (pwbuflen);
581 home_dir = p->pw_dir;
584 if (home_dir == NULL || home_dir[0] == '\0')
586 if (flags & GLOB_TILDE_CHECK)
589 home_dir = "~"; /* No luck. */
591 # endif /* WINDOWS32 */
593 /* Now construct the full directory. */
594 if (dirname[1] == '\0')
599 size_t home_len = strlen (home_dir);
600 newp = __alloca (home_len + dirlen);
601 mempcpy (mempcpy (newp, home_dir, home_len),
602 &dirname[1], dirlen);
606 # if !defined _AMIGA && !defined WINDOWS32
609 char *end_name = strchr (dirname, '/');
610 const char *user_name;
611 const char *home_dir;
613 if (end_name == NULL)
614 user_name = dirname + 1;
618 newp = __alloca (end_name - dirname);
619 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
624 /* Look up specific user's home directory. */
627 # if defined HAVE_GETPWNAM_R || defined _LIBC
628 long int buflen = GETPW_R_SIZE_MAX ();
635 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
639 pwtmpbuf = __alloca (buflen);
641 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
649 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
652 pwtmpbuf = __alloca (buflen);
657 p = getpwnam (user_name);
660 home_dir = p->pw_dir;
664 /* If we found a home directory use this. */
665 if (home_dir != NULL)
668 size_t home_len = strlen (home_dir);
669 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
670 newp = __alloca (home_len + rest_len + 1);
671 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
672 end_name, rest_len)) = '\0';
676 if (flags & GLOB_TILDE_CHECK)
677 /* We have to regard it as an error if we cannot find the
681 # endif /* Not Amiga && not WINDOWS32. */
683 #endif /* Not VMS. */
685 /* Now test whether we looked for "~" or "~NAME". In this case we
686 can give the answer now. */
687 if (filename == NULL)
692 /* Return the directory if we don't check for error or if it exists. */
693 if ((flags & GLOB_NOCHECK)
694 || (((flags & GLOB_ALTDIRFUNC)
695 ? ((*pglob->gl_stat) (dirname, &st) == 0
696 && S_ISDIR (st.st_mode))
697 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
699 int newcount = pglob->gl_pathc + pglob->gl_offs;
703 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
704 if (new_gl_pathv == NULL)
707 free (pglob->gl_pathv);
708 pglob->gl_pathv = NULL;
712 pglob->gl_pathv = new_gl_pathv;
714 pglob->gl_pathv[newcount] = strdup (dirname);
715 if (pglob->gl_pathv[newcount] == NULL)
717 pglob->gl_pathv[++newcount] = NULL;
719 pglob->gl_flags = flags;
728 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
730 /* The directory name contains metacharacters, so we
731 have to glob for the directory, and then glob for
732 the pattern in each directory found. */
736 if ((flags & GLOB_ALTDIRFUNC) != 0)
738 /* Use the alternative access functions also in the recursive
740 dirs.gl_opendir = pglob->gl_opendir;
741 dirs.gl_readdir = pglob->gl_readdir;
742 dirs.gl_closedir = pglob->gl_closedir;
743 dirs.gl_stat = pglob->gl_stat;
744 dirs.gl_lstat = pglob->gl_lstat;
747 status = glob (dirname,
748 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
750 | GLOB_NOSORT | GLOB_ONLYDIR),
755 /* We have successfully globbed the preceding directory name.
756 For each name we found, call glob_in_dir on it and FILENAME,
757 appending the results to PGLOB. */
758 for (i = 0; i < dirs.gl_pathc; ++i)
764 /* Make globbing interruptible in the bash shell. */
765 extern int interrupt_state;
775 old_pathc = pglob->gl_pathc;
776 status = glob_in_dir (filename, dirs.gl_pathv[i],
777 ((flags | GLOB_APPEND)
778 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
780 if (status == GLOB_NOMATCH)
781 /* No matches in this directory. Try the next. */
792 /* Stick the directory on the front of each name. */
793 if (prefix_array (dirs.gl_pathv[i],
794 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
795 pglob->gl_pathc - old_pathc))
804 flags |= GLOB_MAGCHAR;
806 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
807 But if we have not found any matching entry and the GLOB_NOCHECK
808 flag was set we must return the input pattern itself. */
809 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
812 if (flags & GLOB_NOCHECK)
814 int newcount = pglob->gl_pathc + pglob->gl_offs;
817 new_gl_pathv = realloc (pglob->gl_pathv,
818 (newcount + 2) * sizeof (char *));
819 if (new_gl_pathv == NULL)
824 pglob->gl_pathv = new_gl_pathv;
826 pglob->gl_pathv[newcount] = strdup (pattern);
827 if (pglob->gl_pathv[newcount] == NULL)
838 pglob->gl_pathv[newcount] = NULL;
839 pglob->gl_flags = flags;
852 int old_pathc = pglob->gl_pathc;
854 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
860 /* Stick the directory on the front of each name. */
861 if (prefix_array (dirname,
862 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
863 pglob->gl_pathc - old_pathc))
872 if (!(flags & GLOB_NOSORT))
874 /* Sort the vector. */
875 qsort (&pglob->gl_pathv[oldcount],
876 pglob->gl_pathc + pglob->gl_offs - oldcount,
877 sizeof (char *), collated_compare);
882 #if defined _LIBC && !defined glob
883 libc_hidden_def (glob)
887 #if !defined _LIBC || !defined GLOB_ONLY_P
889 /* Free storage allocated in PGLOB by a previous `glob' call. */
891 globfree (register glob_t *pglob)
893 if (pglob->gl_pathv != NULL)
896 for (i = 0; i < pglob->gl_pathc; ++i)
897 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
898 free (pglob->gl_pathv[pglob->gl_offs + i]);
899 free (pglob->gl_pathv);
900 pglob->gl_pathv = NULL;
903 #if defined _LIBC && !defined globfree
904 libc_hidden_def (globfree)
908 /* Do a collated comparison of A and B. */
910 collated_compare (const void *a, const void *b)
912 const char *const s1 = *(const char *const * const) a;
913 const char *const s2 = *(const char *const * const) b;
921 return strcoll (s1, s2);
925 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
926 elements in place. Return nonzero if out of memory, zero if successful.
927 A slash is inserted between DIRNAME and each elt of ARRAY,
928 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
930 prefix_array (const char *dirname, char **array, size_t n)
933 size_t dirlen = strlen (dirname);
934 #if defined __MSDOS__ || defined WINDOWS32
936 # define DIRSEP_CHAR sep_char
938 # define DIRSEP_CHAR '/'
941 if (dirlen == 1 && dirname[0] == '/')
942 /* DIRNAME is just "/", so normal prepending would get us "//foo".
943 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
945 #if defined __MSDOS__ || defined WINDOWS32
948 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
949 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
951 else if (dirname[dirlen - 1] == ':')
953 /* DIRNAME is "d:". Use `:' instead of `/'. */
960 for (i = 0; i < n; ++i)
962 size_t eltlen = strlen (array[i]) + 1;
963 char *new = malloc (dirlen + 1 + eltlen);
972 char *endp = mempcpy (new, dirname, dirlen);
973 *endp++ = DIRSEP_CHAR;
974 mempcpy (endp, array[i], eltlen);
984 /* We must not compile this function twice. */
985 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
986 /* Return nonzero if PATTERN contains any metacharacters.
987 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
989 __glob_pattern_p (const char *pattern, int quote)
991 register const char *p;
994 for (p = pattern; *p != '\0'; ++p)
1002 if (quote && p[1] != '\0')
1019 weak_alias (__glob_pattern_p, glob_pattern_p)
1023 #endif /* !GLOB_ONLY_P */
1026 /* We put this in a separate function mainly to allow the memory
1027 allocated with alloca to be recycled. */
1028 #if !defined _LIBC || !defined GLOB_ONLY_P
1030 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1031 glob_t *pglob, int flags)
1033 size_t fnamelen = strlen (fname);
1034 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1038 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1039 fname, fnamelen + 1);
1041 return ((flags & GLOB_ALTDIRFUNC)
1042 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1043 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1048 /* Like `glob', but PATTERN is a final pathname component,
1049 and matches are searched for in DIRECTORY.
1050 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1051 The GLOB_APPEND flag is assumed to be set (always appends). */
1053 glob_in_dir (const char *pattern, const char *directory, int flags,
1054 int (*errfunc) (const char *, int),
1057 size_t dirlen = strlen (directory);
1058 void *stream = NULL;
1061 struct globlink *next;
1064 struct globlink *names = NULL;
1069 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1070 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1072 /* We need not do any tests. The PATTERN contains no meta
1073 characters and we must not return an error therefore the
1074 result will always contain exactly one name. */
1075 flags |= GLOB_NOCHECK;
1078 else if (meta == 0 &&
1079 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1081 /* Since we use the normal file functions we can also use stat()
1082 to verify the file is there. */
1085 size_t patlen = strlen (pattern);
1086 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1088 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1090 pattern, patlen + 1);
1091 if (((flags & GLOB_ALTDIRFUNC)
1092 ? (*pglob->gl_stat) (fullname, &st)
1093 : __stat64 (fullname, &st64)) == 0)
1094 /* We found this file to be existing. Now tell the rest
1095 of the function to copy this name into the result. */
1096 flags |= GLOB_NOCHECK;
1102 if (pattern[0] == '\0')
1104 /* This is a special case for matching directories like in
1106 names = __alloca (sizeof (struct globlink));
1107 names->name = malloc (1);
1108 if (names->name == NULL)
1110 names->name[0] = '\0';
1117 stream = ((flags & GLOB_ALTDIRFUNC)
1118 ? (*pglob->gl_opendir) (directory)
1119 : opendir (directory));
1122 if (errno != ENOTDIR
1123 && ((errfunc != NULL && (*errfunc) (directory, errno))
1124 || (flags & GLOB_ERR)))
1125 return GLOB_ABORTED;
1131 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1132 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1133 #if defined _AMIGA || defined VMS
1138 flags |= GLOB_MAGCHAR;
1144 #if defined _LIBC && !defined COMPILE_GLOB64
1148 struct dirent64 d64;
1149 char room [offsetof (struct dirent64, d_name[0])
1154 if (flags & GLOB_ALTDIRFUNC)
1156 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1159 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1166 d = __readdir64 (stream);
1168 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1169 ? ((*pglob->gl_readdir) (stream))
1170 : __readdir (stream));
1174 if (! REAL_DIR_ENTRY (d))
1177 /* If we shall match only directories use the information
1178 provided by the dirent call if possible. */
1179 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1184 if (fnmatch (pattern, name, fnm_flags) == 0)
1186 /* ISDIR will often be incorrectly set to false
1187 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1188 don't care. It won't be used and we save the
1189 expensive call to stat. */
1191 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1192 ? GLOB_ONLYDIR : 0));
1193 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1194 || ((flags & need_dir_test)
1195 && is_dir_p (directory, dirlen, name,
1198 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1199 if ((flags & GLOB_ONLYDIR) && !isdir)
1203 struct globlink *new =
1204 __alloca (sizeof (struct globlink));
1208 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1209 if (new->name == NULL)
1211 p = mempcpy (new->name, name, len);
1212 if ((flags & GLOB_MARK) && isdir)
1225 if (nfound == 0 && (flags & GLOB_NOCHECK))
1227 size_t len = strlen (pattern);
1229 names = __alloca (sizeof (struct globlink));
1231 names->name = malloc (len + 1);
1232 if (names->name == NULL)
1234 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1239 char **new_gl_pathv;
1242 = realloc (pglob->gl_pathv,
1243 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1245 if (new_gl_pathv == NULL)
1247 pglob->gl_pathv = new_gl_pathv;
1249 for (; names != NULL; names = names->next)
1250 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1251 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1253 pglob->gl_flags = flags;
1259 if (flags & GLOB_ALTDIRFUNC)
1260 (*pglob->gl_closedir) (stream);
1266 return nfound == 0 ? GLOB_NOMATCH : 0;
1271 if (flags & GLOB_ALTDIRFUNC)
1272 (*pglob->gl_closedir) (stream);
1277 while (names != NULL)
1279 if (names->name != NULL)
1281 names = names->next;
1283 return GLOB_NOSPACE;