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
56 # ifdef HAVE_SYS_NDIR_H
57 # include <sys/ndir.h>
59 # ifdef HAVE_SYS_DIR_H
67 # endif /* HAVE_VMSDIR_H */
71 /* In GNU systems, <dirent.h> defines this macro for us. */
74 # define NAMLEN(d) _D_NAMLEN(d)
77 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
78 if the `d_type' member for `struct dirent' is available.
79 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
80 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
81 /* True if the directory entry D must be of type T. */
82 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
84 /* True if the directory entry D might be a symbolic link. */
85 # define DIRENT_MIGHT_BE_SYMLINK(d) \
86 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
88 /* True if the directory entry D might be a directory. */
89 # define DIRENT_MIGHT_BE_DIR(d) \
90 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
92 #else /* !HAVE_D_TYPE */
93 # define DIRENT_MUST_BE(d, t) false
94 # define DIRENT_MIGHT_BE_SYMLINK(d) true
95 # define DIRENT_MIGHT_BE_DIR(d) true
96 #endif /* HAVE_D_TYPE */
98 /* If the system has the `struct dirent64' type we use it internally. */
99 #if defined _LIBC && !defined COMPILE_GLOB64
100 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
101 # define CONVERT_D_NAMLEN(d64, d32)
103 # define CONVERT_D_NAMLEN(d64, d32) \
104 (d64)->d_namlen = (d32)->d_namlen;
107 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
108 # define CONVERT_D_INO(d64, d32)
110 # define CONVERT_D_INO(d64, d32) \
111 (d64)->d_ino = (d32)->d_ino;
114 # ifdef _DIRENT_HAVE_D_TYPE
115 # define CONVERT_D_TYPE(d64, d32) \
116 (d64)->d_type = (d32)->d_type;
118 # define CONVERT_D_TYPE(d64, d32)
121 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
122 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
123 CONVERT_D_NAMLEN (d64, d32) \
124 CONVERT_D_INO (d64, d32) \
125 CONVERT_D_TYPE (d64, d32)
129 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
130 /* Posix does not require that the d_ino field be present, and some
131 systems do not provide it. */
132 # define REAL_DIR_ENTRY(dp) 1
134 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
140 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
143 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
150 # define strdup(str) __strdup (str)
151 # define sysconf(id) __sysconf (id)
152 # define closedir(dir) __closedir (dir)
153 # define opendir(name) __opendir (name)
154 # define readdir(str) __readdir64 (str)
155 # define getpwnam_r(name, bufp, buf, len, res) \
156 __getpwnam_r (name, bufp, buf, len, res)
158 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
160 # define struct_stat64 struct stat64
162 # include "getlogin_r.h"
163 # include "mempcpy.h"
164 # include "stat-macros.h"
166 # define __stat64(fname, buf) stat (fname, buf)
167 # define struct_stat64 struct stat
168 # define __stat(fname, buf) stat (fname, buf)
169 # define __alloca alloca
170 # define __readdir readdir
171 # define __readdir64 readdir64
172 # define __glob_pattern_p glob_pattern_p
178 #ifdef _SC_GETPW_R_SIZE_MAX
179 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
181 # define GETPW_R_SIZE_MAX() (-1)
183 #ifdef _SC_LOGIN_NAME_MAX
184 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
186 # define GET_LOGIN_NAME_MAX() (-1)
189 static const char *next_brace_sub (const char *begin, int flags) __THROW;
191 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
193 static int glob_in_dir (const char *pattern, const char *directory,
194 int flags, int (*errfunc) (const char *, int),
197 #if !defined _LIBC || !defined GLOB_ONLY_P
198 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
199 static int collated_compare (const void *, const void *) __THROW;
202 /* Find the end of the sub-pattern in a brace expression. */
204 next_brace_sub (const char *cp, int flags)
206 unsigned int depth = 0;
208 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
216 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
223 return *cp != '\0' ? cp : NULL;
226 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
228 /* Do glob searching for PATTERN, placing results in PGLOB.
229 The bits defined above may be set in FLAGS.
230 If a directory cannot be opened or read and ERRFUNC is not nil,
231 it is called with the pathname that caused the error, and the
232 `errno' value from the failing call; if it returns non-zero
233 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
234 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
235 Otherwise, `glob' returns zero. */
237 #ifdef GLOB_ATTRIBUTE
240 glob (pattern, flags, errfunc, pglob)
243 int (*errfunc) (const char *, int);
246 const char *filename;
252 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
254 __set_errno (EINVAL);
258 if (!(flags & GLOB_DOOFFS))
259 /* Have to do this so `globfree' knows where to start freeing. It
260 also makes all the code that uses gl_offs simpler. */
263 if (flags & GLOB_BRACE)
267 if (flags & GLOB_NOESCAPE)
268 begin = strchr (pattern, '{');
280 if (*begin == '\\' && begin[1] != '\0')
282 else if (*begin == '{')
291 /* Allocate working buffer large enough for our work. Note that
292 we have at least an opening and closing brace. */
300 char onealt[strlen (pattern) - 1];
302 char *onealt = malloc (strlen (pattern) - 1);
305 if (!(flags & GLOB_APPEND))
308 pglob->gl_pathv = NULL;
314 /* We know the prefix for all sub-patterns. */
315 alt_start = mempcpy (onealt, pattern, begin - pattern);
317 /* Find the first sub-pattern and at the same time find the
318 rest after the closing brace. */
319 next = next_brace_sub (begin + 1, flags);
322 /* It is an invalid expression. */
326 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
329 /* Now find the end of the whole brace expression. */
333 rest = next_brace_sub (rest + 1, flags);
336 /* It is an invalid expression. */
340 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
343 /* Please note that we now can be sure the brace expression
345 rest_len = strlen (++rest) + 1;
347 /* We have a brace expression. BEGIN points to the opening {,
348 NEXT points past the terminator of the first element, and END
349 points past the final }. We will accumulate result names from
350 recursive runs for each brace alternative in the buffer using
353 if (!(flags & GLOB_APPEND))
355 /* This call is to set a new vector, so clear out the
356 vector so we can append to it. */
358 pglob->gl_pathv = NULL;
360 firstc = pglob->gl_pathc;
367 /* Construct the new glob expression. */
368 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
370 result = glob (onealt,
371 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
372 | GLOB_APPEND), errfunc, pglob);
374 /* If we got an error, return it. */
375 if (result && result != GLOB_NOMATCH)
380 if (!(flags & GLOB_APPEND))
389 /* We saw the last entry. */
393 next = next_brace_sub (p, flags);
394 assert (next != NULL);
401 if (pglob->gl_pathc != firstc)
402 /* We found some entries. */
404 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
409 /* Find the filename. */
410 filename = strrchr (pattern, '/');
411 #if defined __MSDOS__ || defined WINDOWS32
412 /* The case of "d:pattern". Since `:' is not allowed in
413 file names, we can safely assume that wherever it
414 happens in pattern, it signals the filename part. This
415 is so we could some day support patterns like "[a-z]:foo". */
416 if (filename == NULL)
417 filename = strchr (pattern, ':');
418 #endif /* __MSDOS__ || WINDOWS32 */
419 if (filename == NULL)
421 /* This can mean two things: a simple name or "~name". The latter
422 case is nothing but a notation for a directory. */
423 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
426 dirlen = strlen (pattern);
428 /* Set FILENAME to NULL as a special flag. This is ugly but
429 other solutions would require much more code. We test for
430 this special case below. */
444 else if (filename == pattern)
454 dirlen = filename - pattern;
455 #if defined __MSDOS__ || defined WINDOWS32
457 || (filename > pattern + 1 && filename[-1] == ':'))
462 drive_spec = __alloca (dirlen + 1);
463 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
464 /* For now, disallow wildcards in the drive spec, to
465 prevent infinite recursion in glob. */
466 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
468 /* If this is "d:pattern", we need to copy `:' to DIRNAME
469 as well. If it's "d:/pattern", don't remove the slash
470 from "d:/", since "d:" and "d:/" are not the same.*/
473 newp = __alloca (dirlen + 1);
474 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
478 if (filename[0] == '\0'
479 #if defined __MSDOS__ || defined WINDOWS32
480 && dirname[dirlen - 1] != ':'
481 && (dirlen < 3 || dirname[dirlen - 2] != ':'
482 || dirname[dirlen - 1] != '/')
485 /* "pattern/". Expand "pattern", appending slashes. */
487 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
489 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
490 | (flags & GLOB_MARK));
495 if (!(flags & GLOB_APPEND))
498 if (!(flags & GLOB_DOOFFS))
499 pglob->gl_pathv = NULL;
503 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
504 if (pglob->gl_pathv == NULL)
507 for (i = 0; i <= pglob->gl_offs; ++i)
508 pglob->gl_pathv[i] = NULL;
512 oldcount = pglob->gl_pathc + pglob->gl_offs;
515 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
517 if (dirname[1] == '\0' || dirname[1] == '/')
519 /* Look up home directory. */
520 const char *home_dir = getenv ("HOME");
522 if (home_dir == NULL || home_dir[0] == '\0')
526 if (home_dir == NULL || home_dir[0] == '\0')
527 home_dir = "c:/users/default"; /* poor default */
529 if (home_dir == NULL || home_dir[0] == '\0')
533 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
536 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
539 name = __alloca (buflen);
541 success = getlogin_r (name, buflen) == 0;
545 # if defined HAVE_GETPWNAM_R || defined _LIBC
546 long int pwbuflen = GETPW_R_SIZE_MAX ();
553 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
554 Try a moderate value. */
557 pwtmpbuf = __alloca (pwbuflen);
559 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
568 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
572 pwtmpbuf = __alloca (pwbuflen);
580 home_dir = p->pw_dir;
583 if (home_dir == NULL || home_dir[0] == '\0')
585 if (flags & GLOB_TILDE_CHECK)
588 home_dir = "~"; /* No luck. */
590 # endif /* WINDOWS32 */
592 /* Now construct the full directory. */
593 if (dirname[1] == '\0')
598 size_t home_len = strlen (home_dir);
599 newp = __alloca (home_len + dirlen);
600 mempcpy (mempcpy (newp, home_dir, home_len),
601 &dirname[1], dirlen);
605 # if !defined _AMIGA && !defined WINDOWS32
608 char *end_name = strchr (dirname, '/');
609 const char *user_name;
610 const char *home_dir;
612 if (end_name == NULL)
613 user_name = dirname + 1;
617 newp = __alloca (end_name - dirname);
618 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
623 /* Look up specific user's home directory. */
626 # if defined HAVE_GETPWNAM_R || defined _LIBC
627 long int buflen = GETPW_R_SIZE_MAX ();
634 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
638 pwtmpbuf = __alloca (buflen);
640 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
648 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
651 pwtmpbuf = __alloca (buflen);
656 p = getpwnam (user_name);
659 home_dir = p->pw_dir;
663 /* If we found a home directory use this. */
664 if (home_dir != NULL)
667 size_t home_len = strlen (home_dir);
668 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
669 newp = __alloca (home_len + rest_len + 1);
670 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
671 end_name, rest_len)) = '\0';
675 if (flags & GLOB_TILDE_CHECK)
676 /* We have to regard it as an error if we cannot find the
680 # endif /* Not Amiga && not WINDOWS32. */
682 #endif /* Not VMS. */
684 /* Now test whether we looked for "~" or "~NAME". In this case we
685 can give the answer now. */
686 if (filename == NULL)
691 /* Return the directory if we don't check for error or if it exists. */
692 if ((flags & GLOB_NOCHECK)
693 || (((flags & GLOB_ALTDIRFUNC)
694 ? ((*pglob->gl_stat) (dirname, &st) == 0
695 && S_ISDIR (st.st_mode))
696 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
698 int newcount = pglob->gl_pathc + pglob->gl_offs;
702 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
703 if (new_gl_pathv == NULL)
706 free (pglob->gl_pathv);
707 pglob->gl_pathv = NULL;
711 pglob->gl_pathv = new_gl_pathv;
713 pglob->gl_pathv[newcount] = strdup (dirname);
714 if (pglob->gl_pathv[newcount] == NULL)
716 pglob->gl_pathv[++newcount] = NULL;
718 pglob->gl_flags = flags;
727 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
729 /* The directory name contains metacharacters, so we
730 have to glob for the directory, and then glob for
731 the pattern in each directory found. */
735 if ((flags & GLOB_ALTDIRFUNC) != 0)
737 /* Use the alternative access functions also in the recursive
739 dirs.gl_opendir = pglob->gl_opendir;
740 dirs.gl_readdir = pglob->gl_readdir;
741 dirs.gl_closedir = pglob->gl_closedir;
742 dirs.gl_stat = pglob->gl_stat;
743 dirs.gl_lstat = pglob->gl_lstat;
746 status = glob (dirname,
747 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
749 | GLOB_NOSORT | GLOB_ONLYDIR),
754 /* We have successfully globbed the preceding directory name.
755 For each name we found, call glob_in_dir on it and FILENAME,
756 appending the results to PGLOB. */
757 for (i = 0; i < dirs.gl_pathc; ++i)
763 /* Make globbing interruptible in the bash shell. */
764 extern int interrupt_state;
774 old_pathc = pglob->gl_pathc;
775 status = glob_in_dir (filename, dirs.gl_pathv[i],
776 ((flags | GLOB_APPEND)
777 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
779 if (status == GLOB_NOMATCH)
780 /* No matches in this directory. Try the next. */
791 /* Stick the directory on the front of each name. */
792 if (prefix_array (dirs.gl_pathv[i],
793 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
794 pglob->gl_pathc - old_pathc))
803 flags |= GLOB_MAGCHAR;
805 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
806 But if we have not found any matching entry and the GLOB_NOCHECK
807 flag was set we must return the input pattern itself. */
808 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
811 if (flags & GLOB_NOCHECK)
813 int newcount = pglob->gl_pathc + pglob->gl_offs;
816 new_gl_pathv = realloc (pglob->gl_pathv,
817 (newcount + 2) * sizeof (char *));
818 if (new_gl_pathv == NULL)
823 pglob->gl_pathv = new_gl_pathv;
825 pglob->gl_pathv[newcount] = strdup (pattern);
826 if (pglob->gl_pathv[newcount] == NULL)
837 pglob->gl_pathv[newcount] = NULL;
838 pglob->gl_flags = flags;
851 int old_pathc = pglob->gl_pathc;
853 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
859 /* Stick the directory on the front of each name. */
860 if (prefix_array (dirname,
861 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
862 pglob->gl_pathc - old_pathc))
871 if (!(flags & GLOB_NOSORT))
873 /* Sort the vector. */
874 qsort (&pglob->gl_pathv[oldcount],
875 pglob->gl_pathc + pglob->gl_offs - oldcount,
876 sizeof (char *), collated_compare);
881 #if defined _LIBC && !defined glob
882 libc_hidden_def (glob)
886 #if !defined _LIBC || !defined GLOB_ONLY_P
888 /* Free storage allocated in PGLOB by a previous `glob' call. */
891 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 char *const *ps1 = a; char *s1 = *ps1;
913 char *const *ps2 = b; char *s2 = *ps2;
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 (pattern, quote)
993 register const char *p;
996 for (p = pattern; *p != '\0'; ++p)
1004 if (quote && p[1] != '\0')
1021 weak_alias (__glob_pattern_p, glob_pattern_p)
1025 #endif /* !GLOB_ONLY_P */
1028 /* We put this in a separate function mainly to allow the memory
1029 allocated with alloca to be recycled. */
1030 #if !defined _LIBC || !defined GLOB_ONLY_P
1032 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1033 glob_t *pglob, int flags)
1035 size_t fnamelen = strlen (fname);
1036 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1040 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1041 fname, fnamelen + 1);
1043 return ((flags & GLOB_ALTDIRFUNC)
1044 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1045 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1050 /* Like `glob', but PATTERN is a final pathname component,
1051 and matches are searched for in DIRECTORY.
1052 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1053 The GLOB_APPEND flag is assumed to be set (always appends). */
1055 glob_in_dir (const char *pattern, const char *directory, int flags,
1056 int (*errfunc) (const char *, int),
1059 size_t dirlen = strlen (directory);
1060 void *stream = NULL;
1063 struct globlink *next;
1066 struct globlink *names = NULL;
1071 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1072 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1074 /* We need not do any tests. The PATTERN contains no meta
1075 characters and we must not return an error therefore the
1076 result will always contain exactly one name. */
1077 flags |= GLOB_NOCHECK;
1080 else if (meta == 0 &&
1081 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1083 /* Since we use the normal file functions we can also use stat()
1084 to verify the file is there. */
1087 size_t patlen = strlen (pattern);
1088 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1090 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1092 pattern, patlen + 1);
1093 if (((flags & GLOB_ALTDIRFUNC)
1094 ? (*pglob->gl_stat) (fullname, &st)
1095 : __stat64 (fullname, &st64)) == 0)
1096 /* We found this file to be existing. Now tell the rest
1097 of the function to copy this name into the result. */
1098 flags |= GLOB_NOCHECK;
1104 if (pattern[0] == '\0')
1106 /* This is a special case for matching directories like in
1108 names = __alloca (sizeof (struct globlink));
1109 names->name = malloc (1);
1110 if (names->name == NULL)
1112 names->name[0] = '\0';
1119 stream = ((flags & GLOB_ALTDIRFUNC)
1120 ? (*pglob->gl_opendir) (directory)
1121 : opendir (directory));
1124 if (errno != ENOTDIR
1125 && ((errfunc != NULL && (*errfunc) (directory, errno))
1126 || (flags & GLOB_ERR)))
1127 return GLOB_ABORTED;
1133 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1134 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1135 #if defined _AMIGA || defined VMS
1140 flags |= GLOB_MAGCHAR;
1146 #if defined _LIBC && !defined COMPILE_GLOB64
1150 struct dirent64 d64;
1151 char room [offsetof (struct dirent64, d_name[0])
1156 if (flags & GLOB_ALTDIRFUNC)
1158 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1161 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1168 d = __readdir64 (stream);
1170 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1171 ? ((*pglob->gl_readdir) (stream))
1172 : __readdir (stream));
1176 if (! REAL_DIR_ENTRY (d))
1179 /* If we shall match only directories use the information
1180 provided by the dirent call if possible. */
1181 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1186 if (fnmatch (pattern, name, fnm_flags) == 0)
1188 /* ISDIR will often be incorrectly set to false
1189 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1190 don't care. It won't be used and we save the
1191 expensive call to stat. */
1193 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1194 ? GLOB_ONLYDIR : 0));
1195 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1196 || ((flags & need_dir_test)
1197 && is_dir_p (directory, dirlen, name,
1200 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1201 if ((flags & GLOB_ONLYDIR) && !isdir)
1205 struct globlink *new =
1206 __alloca (sizeof (struct globlink));
1210 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1211 if (new->name == NULL)
1213 p = mempcpy (new->name, name, len);
1214 if ((flags & GLOB_MARK) && isdir)
1227 if (nfound == 0 && (flags & GLOB_NOCHECK))
1229 size_t len = strlen (pattern);
1231 names = __alloca (sizeof (struct globlink));
1233 names->name = malloc (len + 1);
1234 if (names->name == NULL)
1236 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1241 char **new_gl_pathv;
1244 = realloc (pglob->gl_pathv,
1245 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1247 if (new_gl_pathv == NULL)
1249 pglob->gl_pathv = new_gl_pathv;
1251 for (; names != NULL; names = names->next)
1252 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1253 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1255 pglob->gl_flags = flags;
1261 if (flags & GLOB_ALTDIRFUNC)
1262 (*pglob->gl_closedir) (stream);
1268 return nfound == 0 ? GLOB_NOMATCH : 0;
1273 if (flags & GLOB_ALTDIRFUNC)
1274 (*pglob->gl_closedir) (stream);
1279 while (names != NULL)
1281 if (names->name != NULL)
1283 names = names->next;
1285 return GLOB_NOSPACE;