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)
50 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
52 # define NAMLEN(dirent) strlen((dirent)->d_name)
54 # define dirent direct
55 # define NAMLEN(dirent) (dirent)->d_namlen
60 /* In GNU systems, <dirent.h> defines this macro for us. */
63 # define NAMLEN(d) _D_NAMLEN(d)
66 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
67 if the `d_type' member for `struct dirent' is available.
68 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
69 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
70 /* True if the directory entry D must be of type T. */
71 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
73 /* True if the directory entry D might be a symbolic link. */
74 # define DIRENT_MIGHT_BE_SYMLINK(d) \
75 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
77 /* True if the directory entry D might be a directory. */
78 # define DIRENT_MIGHT_BE_DIR(d) \
79 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
81 #else /* !HAVE_D_TYPE */
82 # define DIRENT_MUST_BE(d, t) false
83 # define DIRENT_MIGHT_BE_SYMLINK(d) true
84 # define DIRENT_MIGHT_BE_DIR(d) true
85 #endif /* HAVE_D_TYPE */
87 /* If the system has the `struct dirent64' type we use it internally. */
88 #if defined _LIBC && !defined COMPILE_GLOB64
89 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
90 # define CONVERT_D_NAMLEN(d64, d32)
92 # define CONVERT_D_NAMLEN(d64, d32) \
93 (d64)->d_namlen = (d32)->d_namlen;
96 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
97 # define CONVERT_D_INO(d64, d32)
99 # define CONVERT_D_INO(d64, d32) \
100 (d64)->d_ino = (d32)->d_ino;
103 # ifdef _DIRENT_HAVE_D_TYPE
104 # define CONVERT_D_TYPE(d64, d32) \
105 (d64)->d_type = (d32)->d_type;
107 # define CONVERT_D_TYPE(d64, d32)
110 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
111 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
112 CONVERT_D_NAMLEN (d64, d32) \
113 CONVERT_D_INO (d64, d32) \
114 CONVERT_D_TYPE (d64, d32)
118 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
119 /* Posix does not require that the d_ino field be present, and some
120 systems do not provide it. */
121 # define REAL_DIR_ENTRY(dp) 1
123 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
129 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
132 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
139 # define strdup(str) __strdup (str)
140 # define sysconf(id) __sysconf (id)
141 # define closedir(dir) __closedir (dir)
142 # define opendir(name) __opendir (name)
143 # define readdir(str) __readdir64 (str)
144 # define getpwnam_r(name, bufp, buf, len, res) \
145 __getpwnam_r (name, bufp, buf, len, res)
147 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
149 # define struct_stat64 struct stat64
151 # include "getlogin_r.h"
152 # include "mempcpy.h"
153 # include "stat-macros.h"
155 # define __stat64(fname, buf) stat (fname, buf)
156 # define struct_stat64 struct stat
157 # define __stat(fname, buf) stat (fname, buf)
158 # define __alloca alloca
159 # define __readdir readdir
160 # define __readdir64 readdir64
161 # define __glob_pattern_p glob_pattern_p
167 #ifdef _SC_GETPW_R_SIZE_MAX
168 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
170 # define GETPW_R_SIZE_MAX() (-1)
172 #ifdef _SC_LOGIN_NAME_MAX
173 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
175 # define GET_LOGIN_NAME_MAX() (-1)
178 static const char *next_brace_sub (const char *begin, int flags) __THROW;
180 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
182 static int glob_in_dir (const char *pattern, const char *directory,
183 int flags, int (*errfunc) (const char *, int),
186 #if !defined _LIBC || !defined GLOB_ONLY_P
187 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
188 static int collated_compare (const void *, const void *) __THROW;
191 /* Find the end of the sub-pattern in a brace expression. */
193 next_brace_sub (const char *cp, int flags)
195 unsigned int depth = 0;
197 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
205 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
212 return *cp != '\0' ? cp : NULL;
215 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
217 /* Do glob searching for PATTERN, placing results in PGLOB.
218 The bits defined above may be set in FLAGS.
219 If a directory cannot be opened or read and ERRFUNC is not nil,
220 it is called with the pathname that caused the error, and the
221 `errno' value from the failing call; if it returns non-zero
222 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
223 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
224 Otherwise, `glob' returns zero. */
226 #ifdef GLOB_ATTRIBUTE
229 glob (pattern, flags, errfunc, pglob)
232 int (*errfunc) (const char *, int);
235 const char *filename;
241 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
243 __set_errno (EINVAL);
247 if (!(flags & GLOB_DOOFFS))
248 /* Have to do this so `globfree' knows where to start freeing. It
249 also makes all the code that uses gl_offs simpler. */
252 if (flags & GLOB_BRACE)
256 if (flags & GLOB_NOESCAPE)
257 begin = strchr (pattern, '{');
269 if (*begin == '\\' && begin[1] != '\0')
271 else if (*begin == '{')
280 /* Allocate working buffer large enough for our work. Note that
281 we have at least an opening and closing brace. */
289 char onealt[strlen (pattern) - 1];
291 char *onealt = malloc (strlen (pattern) - 1);
294 if (!(flags & GLOB_APPEND))
297 pglob->gl_pathv = NULL;
303 /* We know the prefix for all sub-patterns. */
304 alt_start = mempcpy (onealt, pattern, begin - pattern);
306 /* Find the first sub-pattern and at the same time find the
307 rest after the closing brace. */
308 next = next_brace_sub (begin + 1, flags);
311 /* It is an invalid expression. */
315 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
318 /* Now find the end of the whole brace expression. */
322 rest = next_brace_sub (rest + 1, flags);
325 /* It is an invalid expression. */
329 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
332 /* Please note that we now can be sure the brace expression
334 rest_len = strlen (++rest) + 1;
336 /* We have a brace expression. BEGIN points to the opening {,
337 NEXT points past the terminator of the first element, and END
338 points past the final }. We will accumulate result names from
339 recursive runs for each brace alternative in the buffer using
342 if (!(flags & GLOB_APPEND))
344 /* This call is to set a new vector, so clear out the
345 vector so we can append to it. */
347 pglob->gl_pathv = NULL;
349 firstc = pglob->gl_pathc;
356 /* Construct the new glob expression. */
357 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
359 result = glob (onealt,
360 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
361 | GLOB_APPEND), errfunc, pglob);
363 /* If we got an error, return it. */
364 if (result && result != GLOB_NOMATCH)
369 if (!(flags & GLOB_APPEND))
378 /* We saw the last entry. */
382 next = next_brace_sub (p, flags);
383 assert (next != NULL);
390 if (pglob->gl_pathc != firstc)
391 /* We found some entries. */
393 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
398 /* Find the filename. */
399 filename = strrchr (pattern, '/');
400 #if defined __MSDOS__ || defined WINDOWS32
401 /* The case of "d:pattern". Since `:' is not allowed in
402 file names, we can safely assume that wherever it
403 happens in pattern, it signals the filename part. This
404 is so we could some day support patterns like "[a-z]:foo". */
405 if (filename == NULL)
406 filename = strchr (pattern, ':');
407 #endif /* __MSDOS__ || WINDOWS32 */
408 if (filename == NULL)
410 /* This can mean two things: a simple name or "~name". The latter
411 case is nothing but a notation for a directory. */
412 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
415 dirlen = strlen (pattern);
417 /* Set FILENAME to NULL as a special flag. This is ugly but
418 other solutions would require much more code. We test for
419 this special case below. */
433 else if (filename == pattern)
443 dirlen = filename - pattern;
444 #if defined __MSDOS__ || defined WINDOWS32
446 || (filename > pattern + 1 && filename[-1] == ':'))
451 drive_spec = __alloca (dirlen + 1);
452 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
453 /* For now, disallow wildcards in the drive spec, to
454 prevent infinite recursion in glob. */
455 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
457 /* If this is "d:pattern", we need to copy `:' to DIRNAME
458 as well. If it's "d:/pattern", don't remove the slash
459 from "d:/", since "d:" and "d:/" are not the same.*/
462 newp = __alloca (dirlen + 1);
463 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
467 if (filename[0] == '\0'
468 #if defined __MSDOS__ || defined WINDOWS32
469 && dirname[dirlen - 1] != ':'
470 && (dirlen < 3 || dirname[dirlen - 2] != ':'
471 || dirname[dirlen - 1] != '/')
474 /* "pattern/". Expand "pattern", appending slashes. */
476 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
478 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
479 | (flags & GLOB_MARK));
484 if (!(flags & GLOB_APPEND))
487 if (!(flags & GLOB_DOOFFS))
488 pglob->gl_pathv = NULL;
492 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
493 if (pglob->gl_pathv == NULL)
496 for (i = 0; i <= pglob->gl_offs; ++i)
497 pglob->gl_pathv[i] = NULL;
501 oldcount = pglob->gl_pathc + pglob->gl_offs;
503 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
505 if (dirname[1] == '\0' || dirname[1] == '/')
507 /* Look up home directory. */
508 const char *home_dir = getenv ("HOME");
510 if (home_dir == NULL || home_dir[0] == '\0')
514 if (home_dir == NULL || home_dir[0] == '\0')
515 home_dir = "c:/users/default"; /* poor default */
517 if (home_dir == NULL || home_dir[0] == '\0')
521 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
524 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
527 name = __alloca (buflen);
529 success = getlogin_r (name, buflen) == 0;
533 # if defined HAVE_GETPWNAM_R || defined _LIBC
534 long int pwbuflen = GETPW_R_SIZE_MAX ();
541 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
542 Try a moderate value. */
545 pwtmpbuf = __alloca (pwbuflen);
547 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
556 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
560 pwtmpbuf = __alloca (pwbuflen);
568 home_dir = p->pw_dir;
571 if (home_dir == NULL || home_dir[0] == '\0')
573 if (flags & GLOB_TILDE_CHECK)
576 home_dir = "~"; /* No luck. */
578 # endif /* WINDOWS32 */
580 /* Now construct the full directory. */
581 if (dirname[1] == '\0')
586 size_t home_len = strlen (home_dir);
587 newp = __alloca (home_len + dirlen);
588 mempcpy (mempcpy (newp, home_dir, home_len),
589 &dirname[1], dirlen);
593 # if !defined _AMIGA && !defined WINDOWS32
596 char *end_name = strchr (dirname, '/');
597 const char *user_name;
598 const char *home_dir;
600 if (end_name == NULL)
601 user_name = dirname + 1;
605 newp = __alloca (end_name - dirname);
606 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
611 /* Look up specific user's home directory. */
614 # if defined HAVE_GETPWNAM_R || defined _LIBC
615 long int buflen = GETPW_R_SIZE_MAX ();
622 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
626 pwtmpbuf = __alloca (buflen);
628 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
636 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
639 pwtmpbuf = __alloca (buflen);
644 p = getpwnam (user_name);
647 home_dir = p->pw_dir;
651 /* If we found a home directory use this. */
652 if (home_dir != NULL)
655 size_t home_len = strlen (home_dir);
656 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
657 newp = __alloca (home_len + rest_len + 1);
658 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
659 end_name, rest_len)) = '\0';
663 if (flags & GLOB_TILDE_CHECK)
664 /* We have to regard it as an error if we cannot find the
668 # endif /* Not Amiga && not WINDOWS32. */
671 /* Now test whether we looked for "~" or "~NAME". In this case we
672 can give the answer now. */
673 if (filename == NULL)
678 /* Return the directory if we don't check for error or if it exists. */
679 if ((flags & GLOB_NOCHECK)
680 || (((flags & GLOB_ALTDIRFUNC)
681 ? ((*pglob->gl_stat) (dirname, &st) == 0
682 && S_ISDIR (st.st_mode))
683 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
685 int newcount = pglob->gl_pathc + pglob->gl_offs;
689 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
690 if (new_gl_pathv == NULL)
693 free (pglob->gl_pathv);
694 pglob->gl_pathv = NULL;
698 pglob->gl_pathv = new_gl_pathv;
700 pglob->gl_pathv[newcount] = strdup (dirname);
701 if (pglob->gl_pathv[newcount] == NULL)
703 pglob->gl_pathv[++newcount] = NULL;
705 pglob->gl_flags = flags;
714 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
716 /* The directory name contains metacharacters, so we
717 have to glob for the directory, and then glob for
718 the pattern in each directory found. */
722 if ((flags & GLOB_ALTDIRFUNC) != 0)
724 /* Use the alternative access functions also in the recursive
726 dirs.gl_opendir = pglob->gl_opendir;
727 dirs.gl_readdir = pglob->gl_readdir;
728 dirs.gl_closedir = pglob->gl_closedir;
729 dirs.gl_stat = pglob->gl_stat;
730 dirs.gl_lstat = pglob->gl_lstat;
733 status = glob (dirname,
734 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
736 | GLOB_NOSORT | GLOB_ONLYDIR),
741 /* We have successfully globbed the preceding directory name.
742 For each name we found, call glob_in_dir on it and FILENAME,
743 appending the results to PGLOB. */
744 for (i = 0; i < dirs.gl_pathc; ++i)
750 /* Make globbing interruptible in the bash shell. */
751 extern int interrupt_state;
761 old_pathc = pglob->gl_pathc;
762 status = glob_in_dir (filename, dirs.gl_pathv[i],
763 ((flags | GLOB_APPEND)
764 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
766 if (status == GLOB_NOMATCH)
767 /* No matches in this directory. Try the next. */
778 /* Stick the directory on the front of each name. */
779 if (prefix_array (dirs.gl_pathv[i],
780 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
781 pglob->gl_pathc - old_pathc))
790 flags |= GLOB_MAGCHAR;
792 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
793 But if we have not found any matching entry and the GLOB_NOCHECK
794 flag was set we must return the input pattern itself. */
795 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
798 if (flags & GLOB_NOCHECK)
800 int newcount = pglob->gl_pathc + pglob->gl_offs;
803 new_gl_pathv = realloc (pglob->gl_pathv,
804 (newcount + 2) * sizeof (char *));
805 if (new_gl_pathv == NULL)
810 pglob->gl_pathv = new_gl_pathv;
812 pglob->gl_pathv[newcount] = strdup (pattern);
813 if (pglob->gl_pathv[newcount] == NULL)
824 pglob->gl_pathv[newcount] = NULL;
825 pglob->gl_flags = flags;
838 int old_pathc = pglob->gl_pathc;
840 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
846 /* Stick the directory on the front of each name. */
847 if (prefix_array (dirname,
848 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
849 pglob->gl_pathc - old_pathc))
858 if (!(flags & GLOB_NOSORT))
860 /* Sort the vector. */
861 qsort (&pglob->gl_pathv[oldcount],
862 pglob->gl_pathc + pglob->gl_offs - oldcount,
863 sizeof (char *), collated_compare);
868 #if defined _LIBC && !defined glob
869 libc_hidden_def (glob)
873 #if !defined _LIBC || !defined GLOB_ONLY_P
875 /* Free storage allocated in PGLOB by a previous `glob' call. */
878 register glob_t *pglob;
880 if (pglob->gl_pathv != NULL)
883 for (i = 0; i < pglob->gl_pathc; ++i)
884 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
885 free (pglob->gl_pathv[pglob->gl_offs + i]);
886 free (pglob->gl_pathv);
887 pglob->gl_pathv = NULL;
890 #if defined _LIBC && !defined globfree
891 libc_hidden_def (globfree)
895 /* Do a collated comparison of A and B. */
897 collated_compare (const void *a, const void *b)
899 char *const *ps1 = a; char *s1 = *ps1;
900 char *const *ps2 = b; char *s2 = *ps2;
908 return strcoll (s1, s2);
912 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
913 elements in place. Return nonzero if out of memory, zero if successful.
914 A slash is inserted between DIRNAME and each elt of ARRAY,
915 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
917 prefix_array (const char *dirname, char **array, size_t n)
920 size_t dirlen = strlen (dirname);
921 #if defined __MSDOS__ || defined WINDOWS32
923 # define DIRSEP_CHAR sep_char
925 # define DIRSEP_CHAR '/'
928 if (dirlen == 1 && dirname[0] == '/')
929 /* DIRNAME is just "/", so normal prepending would get us "//foo".
930 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
932 #if defined __MSDOS__ || defined WINDOWS32
935 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
936 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
938 else if (dirname[dirlen - 1] == ':')
940 /* DIRNAME is "d:". Use `:' instead of `/'. */
947 for (i = 0; i < n; ++i)
949 size_t eltlen = strlen (array[i]) + 1;
950 char *new = malloc (dirlen + 1 + eltlen);
959 char *endp = mempcpy (new, dirname, dirlen);
960 *endp++ = DIRSEP_CHAR;
961 mempcpy (endp, array[i], eltlen);
971 /* We must not compile this function twice. */
972 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
973 /* Return nonzero if PATTERN contains any metacharacters.
974 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
976 __glob_pattern_p (pattern, quote)
980 register const char *p;
983 for (p = pattern; *p != '\0'; ++p)
991 if (quote && p[1] != '\0')
1008 weak_alias (__glob_pattern_p, glob_pattern_p)
1012 #endif /* !GLOB_ONLY_P */
1015 /* We put this in a separate function mainly to allow the memory
1016 allocated with alloca to be recycled. */
1017 #if !defined _LIBC || !defined GLOB_ONLY_P
1019 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1020 glob_t *pglob, int flags)
1022 size_t fnamelen = strlen (fname);
1023 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1027 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1028 fname, fnamelen + 1);
1030 return ((flags & GLOB_ALTDIRFUNC)
1031 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1032 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1037 /* Like `glob', but PATTERN is a final pathname component,
1038 and matches are searched for in DIRECTORY.
1039 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1040 The GLOB_APPEND flag is assumed to be set (always appends). */
1042 glob_in_dir (const char *pattern, const char *directory, int flags,
1043 int (*errfunc) (const char *, int),
1046 size_t dirlen = strlen (directory);
1047 void *stream = NULL;
1050 struct globlink *next;
1053 struct globlink *names = NULL;
1058 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1059 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1061 /* We need not do any tests. The PATTERN contains no meta
1062 characters and we must not return an error therefore the
1063 result will always contain exactly one name. */
1064 flags |= GLOB_NOCHECK;
1067 else if (meta == 0 &&
1068 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1070 /* Since we use the normal file functions we can also use stat()
1071 to verify the file is there. */
1074 size_t patlen = strlen (pattern);
1075 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1077 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1079 pattern, patlen + 1);
1080 if (((flags & GLOB_ALTDIRFUNC)
1081 ? (*pglob->gl_stat) (fullname, &st)
1082 : __stat64 (fullname, &st64)) == 0)
1083 /* We found this file to be existing. Now tell the rest
1084 of the function to copy this name into the result. */
1085 flags |= GLOB_NOCHECK;
1091 if (pattern[0] == '\0')
1093 /* This is a special case for matching directories like in
1095 names = __alloca (sizeof (struct globlink));
1096 names->name = malloc (1);
1097 if (names->name == NULL)
1099 names->name[0] = '\0';
1106 stream = ((flags & GLOB_ALTDIRFUNC)
1107 ? (*pglob->gl_opendir) (directory)
1108 : opendir (directory));
1111 if (errno != ENOTDIR
1112 && ((errfunc != NULL && (*errfunc) (directory, errno))
1113 || (flags & GLOB_ERR)))
1114 return GLOB_ABORTED;
1120 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1121 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1122 #if defined _AMIGA || defined __VMS
1127 flags |= GLOB_MAGCHAR;
1133 #if defined _LIBC && !defined COMPILE_GLOB64
1137 struct dirent64 d64;
1138 char room [offsetof (struct dirent64, d_name[0])
1143 if (flags & GLOB_ALTDIRFUNC)
1145 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1148 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1155 d = __readdir64 (stream);
1157 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1158 ? ((*pglob->gl_readdir) (stream))
1159 : __readdir (stream));
1163 if (! REAL_DIR_ENTRY (d))
1166 /* If we shall match only directories use the information
1167 provided by the dirent call if possible. */
1168 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1173 if (fnmatch (pattern, name, fnm_flags) == 0)
1175 /* ISDIR will often be incorrectly set to false
1176 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1177 don't care. It won't be used and we save the
1178 expensive call to stat. */
1180 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1181 ? GLOB_ONLYDIR : 0));
1182 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1183 || ((flags & need_dir_test)
1184 && is_dir_p (directory, dirlen, name,
1187 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1188 if ((flags & GLOB_ONLYDIR) && !isdir)
1192 struct globlink *new =
1193 __alloca (sizeof (struct globlink));
1197 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1198 if (new->name == NULL)
1200 p = mempcpy (new->name, name, len);
1201 if ((flags & GLOB_MARK) && isdir)
1214 if (nfound == 0 && (flags & GLOB_NOCHECK))
1216 size_t len = strlen (pattern);
1218 names = __alloca (sizeof (struct globlink));
1220 names->name = malloc (len + 1);
1221 if (names->name == NULL)
1223 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1228 char **new_gl_pathv;
1231 = realloc (pglob->gl_pathv,
1232 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1234 if (new_gl_pathv == NULL)
1236 pglob->gl_pathv = new_gl_pathv;
1238 for (; names != NULL; names = names->next)
1239 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1240 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1242 pglob->gl_flags = flags;
1248 if (flags & GLOB_ALTDIRFUNC)
1249 (*pglob->gl_closedir) (stream);
1255 return nfound == 0 ? GLOB_NOMATCH : 0;
1260 if (flags & GLOB_ALTDIRFUNC)
1261 (*pglob->gl_closedir) (stream);
1266 while (names != NULL)
1268 if (names->name != NULL)
1270 names = names->next;
1272 return GLOB_NOSPACE;