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"
137 # include "stat-macros.h"
139 # define __stat64(fname, buf) stat (fname, buf)
140 # define struct_stat64 struct stat
141 # define __stat(fname, buf) stat (fname, buf)
142 # define __alloca alloca
143 # define __readdir readdir
144 # define __readdir64 readdir64
145 # define __glob_pattern_p glob_pattern_p
151 #ifdef _SC_GETPW_R_SIZE_MAX
152 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
154 # define GETPW_R_SIZE_MAX() (-1)
156 #ifdef _SC_LOGIN_NAME_MAX
157 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
159 # define GET_LOGIN_NAME_MAX() (-1)
162 static const char *next_brace_sub (const char *begin, int flags) __THROW;
164 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
166 static int glob_in_dir (const char *pattern, const char *directory,
167 int flags, int (*errfunc) (const char *, int),
170 #if !defined _LIBC || !defined GLOB_ONLY_P
171 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
172 static int collated_compare (const void *, const void *) __THROW;
175 /* Find the end of the sub-pattern in a brace expression. */
177 next_brace_sub (const char *cp, int flags)
179 unsigned int depth = 0;
181 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
189 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
196 return *cp != '\0' ? cp : NULL;
199 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
201 /* Do glob searching for PATTERN, placing results in PGLOB.
202 The bits defined above may be set in FLAGS.
203 If a directory cannot be opened or read and ERRFUNC is not nil,
204 it is called with the pathname that caused the error, and the
205 `errno' value from the failing call; if it returns non-zero
206 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
207 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
208 Otherwise, `glob' returns zero. */
210 #ifdef GLOB_ATTRIBUTE
213 glob (pattern, flags, errfunc, pglob)
216 int (*errfunc) (const char *, int);
219 const char *filename;
225 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
227 __set_errno (EINVAL);
231 if (!(flags & GLOB_DOOFFS))
232 /* Have to do this so `globfree' knows where to start freeing. It
233 also makes all the code that uses gl_offs simpler. */
236 if (flags & GLOB_BRACE)
240 if (flags & GLOB_NOESCAPE)
241 begin = strchr (pattern, '{');
253 if (*begin == '\\' && begin[1] != '\0')
255 else if (*begin == '{')
264 /* Allocate working buffer large enough for our work. Note that
265 we have at least an opening and closing brace. */
273 char onealt[strlen (pattern) - 1];
275 char *onealt = malloc (strlen (pattern) - 1);
278 if (!(flags & GLOB_APPEND))
281 pglob->gl_pathv = NULL;
287 /* We know the prefix for all sub-patterns. */
288 alt_start = mempcpy (onealt, pattern, begin - pattern);
290 /* Find the first sub-pattern and at the same time find the
291 rest after the closing brace. */
292 next = next_brace_sub (begin + 1, flags);
295 /* It is an invalid expression. */
299 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
302 /* Now find the end of the whole brace expression. */
306 rest = next_brace_sub (rest + 1, flags);
309 /* It is an invalid expression. */
313 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
316 /* Please note that we now can be sure the brace expression
318 rest_len = strlen (++rest) + 1;
320 /* We have a brace expression. BEGIN points to the opening {,
321 NEXT points past the terminator of the first element, and END
322 points past the final }. We will accumulate result names from
323 recursive runs for each brace alternative in the buffer using
326 if (!(flags & GLOB_APPEND))
328 /* This call is to set a new vector, so clear out the
329 vector so we can append to it. */
331 pglob->gl_pathv = NULL;
333 firstc = pglob->gl_pathc;
340 /* Construct the new glob expression. */
341 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
343 result = glob (onealt,
344 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
345 | GLOB_APPEND), errfunc, pglob);
347 /* If we got an error, return it. */
348 if (result && result != GLOB_NOMATCH)
353 if (!(flags & GLOB_APPEND))
362 /* We saw the last entry. */
366 next = next_brace_sub (p, flags);
367 assert (next != NULL);
374 if (pglob->gl_pathc != firstc)
375 /* We found some entries. */
377 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
382 /* Find the filename. */
383 filename = strrchr (pattern, '/');
384 #if defined __MSDOS__ || defined WINDOWS32
385 /* The case of "d:pattern". Since `:' is not allowed in
386 file names, we can safely assume that wherever it
387 happens in pattern, it signals the filename part. This
388 is so we could some day support patterns like "[a-z]:foo". */
389 if (filename == NULL)
390 filename = strchr (pattern, ':');
391 #endif /* __MSDOS__ || WINDOWS32 */
392 if (filename == NULL)
394 /* This can mean two things: a simple name or "~name". The latter
395 case is nothing but a notation for a directory. */
396 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
399 dirlen = strlen (pattern);
401 /* Set FILENAME to NULL as a special flag. This is ugly but
402 other solutions would require much more code. We test for
403 this special case below. */
417 else if (filename == pattern)
427 dirlen = filename - pattern;
428 #if defined __MSDOS__ || defined WINDOWS32
430 || (filename > pattern + 1 && filename[-1] == ':'))
435 drive_spec = __alloca (dirlen + 1);
436 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
437 /* For now, disallow wildcards in the drive spec, to
438 prevent infinite recursion in glob. */
439 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
441 /* If this is "d:pattern", we need to copy `:' to DIRNAME
442 as well. If it's "d:/pattern", don't remove the slash
443 from "d:/", since "d:" and "d:/" are not the same.*/
446 newp = __alloca (dirlen + 1);
447 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
451 if (filename[0] == '\0'
452 #if defined __MSDOS__ || defined WINDOWS32
453 && dirname[dirlen - 1] != ':'
454 && (dirlen < 3 || dirname[dirlen - 2] != ':'
455 || dirname[dirlen - 1] != '/')
458 /* "pattern/". Expand "pattern", appending slashes. */
460 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
462 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
463 | (flags & GLOB_MARK));
468 if (!(flags & GLOB_APPEND))
471 if (!(flags & GLOB_DOOFFS))
472 pglob->gl_pathv = NULL;
476 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
477 if (pglob->gl_pathv == NULL)
480 for (i = 0; i <= pglob->gl_offs; ++i)
481 pglob->gl_pathv[i] = NULL;
485 oldcount = pglob->gl_pathc + pglob->gl_offs;
487 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
489 if (dirname[1] == '\0' || dirname[1] == '/')
491 /* Look up home directory. */
492 const char *home_dir = getenv ("HOME");
494 if (home_dir == NULL || home_dir[0] == '\0')
498 if (home_dir == NULL || home_dir[0] == '\0')
499 home_dir = "c:/users/default"; /* poor default */
501 if (home_dir == NULL || home_dir[0] == '\0')
505 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
508 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
511 name = __alloca (buflen);
513 success = getlogin_r (name, buflen) == 0;
517 # if defined HAVE_GETPWNAM_R || defined _LIBC
518 long int pwbuflen = GETPW_R_SIZE_MAX ();
525 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
526 Try a moderate value. */
529 pwtmpbuf = __alloca (pwbuflen);
531 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
540 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
544 pwtmpbuf = __alloca (pwbuflen);
552 home_dir = p->pw_dir;
555 if (home_dir == NULL || home_dir[0] == '\0')
557 if (flags & GLOB_TILDE_CHECK)
560 home_dir = "~"; /* No luck. */
562 # endif /* WINDOWS32 */
564 /* Now construct the full directory. */
565 if (dirname[1] == '\0')
570 size_t home_len = strlen (home_dir);
571 newp = __alloca (home_len + dirlen);
572 mempcpy (mempcpy (newp, home_dir, home_len),
573 &dirname[1], dirlen);
577 # if !defined _AMIGA && !defined WINDOWS32
580 char *end_name = strchr (dirname, '/');
581 const char *user_name;
582 const char *home_dir;
584 if (end_name == NULL)
585 user_name = dirname + 1;
589 newp = __alloca (end_name - dirname);
590 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
595 /* Look up specific user's home directory. */
598 # if defined HAVE_GETPWNAM_R || defined _LIBC
599 long int buflen = GETPW_R_SIZE_MAX ();
606 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
610 pwtmpbuf = __alloca (buflen);
612 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
620 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
623 pwtmpbuf = __alloca (buflen);
628 p = getpwnam (user_name);
631 home_dir = p->pw_dir;
635 /* If we found a home directory use this. */
636 if (home_dir != NULL)
639 size_t home_len = strlen (home_dir);
640 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
641 newp = __alloca (home_len + rest_len + 1);
642 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
643 end_name, rest_len)) = '\0';
647 if (flags & GLOB_TILDE_CHECK)
648 /* We have to regard it as an error if we cannot find the
652 # endif /* Not Amiga && not WINDOWS32. */
655 /* Now test whether we looked for "~" or "~NAME". In this case we
656 can give the answer now. */
657 if (filename == NULL)
662 /* Return the directory if we don't check for error or if it exists. */
663 if ((flags & GLOB_NOCHECK)
664 || (((flags & GLOB_ALTDIRFUNC)
665 ? ((*pglob->gl_stat) (dirname, &st) == 0
666 && S_ISDIR (st.st_mode))
667 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
669 int newcount = pglob->gl_pathc + pglob->gl_offs;
673 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
674 if (new_gl_pathv == NULL)
677 free (pglob->gl_pathv);
678 pglob->gl_pathv = NULL;
682 pglob->gl_pathv = new_gl_pathv;
684 pglob->gl_pathv[newcount] = strdup (dirname);
685 if (pglob->gl_pathv[newcount] == NULL)
687 pglob->gl_pathv[++newcount] = NULL;
689 pglob->gl_flags = flags;
698 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
700 /* The directory name contains metacharacters, so we
701 have to glob for the directory, and then glob for
702 the pattern in each directory found. */
706 if ((flags & GLOB_ALTDIRFUNC) != 0)
708 /* Use the alternative access functions also in the recursive
710 dirs.gl_opendir = pglob->gl_opendir;
711 dirs.gl_readdir = pglob->gl_readdir;
712 dirs.gl_closedir = pglob->gl_closedir;
713 dirs.gl_stat = pglob->gl_stat;
714 dirs.gl_lstat = pglob->gl_lstat;
717 status = glob (dirname,
718 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
720 | GLOB_NOSORT | GLOB_ONLYDIR),
725 /* We have successfully globbed the preceding directory name.
726 For each name we found, call glob_in_dir on it and FILENAME,
727 appending the results to PGLOB. */
728 for (i = 0; i < dirs.gl_pathc; ++i)
734 /* Make globbing interruptible in the bash shell. */
735 extern int interrupt_state;
745 old_pathc = pglob->gl_pathc;
746 status = glob_in_dir (filename, dirs.gl_pathv[i],
747 ((flags | GLOB_APPEND)
748 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
750 if (status == GLOB_NOMATCH)
751 /* No matches in this directory. Try the next. */
762 /* Stick the directory on the front of each name. */
763 if (prefix_array (dirs.gl_pathv[i],
764 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
765 pglob->gl_pathc - old_pathc))
774 flags |= GLOB_MAGCHAR;
776 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
777 But if we have not found any matching entry and the GLOB_NOCHECK
778 flag was set we must return the input pattern itself. */
779 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
782 if (flags & GLOB_NOCHECK)
784 int newcount = pglob->gl_pathc + pglob->gl_offs;
787 new_gl_pathv = realloc (pglob->gl_pathv,
788 (newcount + 2) * sizeof (char *));
789 if (new_gl_pathv == NULL)
794 pglob->gl_pathv = new_gl_pathv;
796 pglob->gl_pathv[newcount] = strdup (pattern);
797 if (pglob->gl_pathv[newcount] == NULL)
808 pglob->gl_pathv[newcount] = NULL;
809 pglob->gl_flags = flags;
822 int old_pathc = pglob->gl_pathc;
824 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
830 /* Stick the directory on the front of each name. */
831 if (prefix_array (dirname,
832 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
833 pglob->gl_pathc - old_pathc))
842 if (!(flags & GLOB_NOSORT))
844 /* Sort the vector. */
845 qsort (&pglob->gl_pathv[oldcount],
846 pglob->gl_pathc + pglob->gl_offs - oldcount,
847 sizeof (char *), collated_compare);
852 #if defined _LIBC && !defined glob
853 libc_hidden_def (glob)
857 #if !defined _LIBC || !defined GLOB_ONLY_P
859 /* Free storage allocated in PGLOB by a previous `glob' call. */
862 register glob_t *pglob;
864 if (pglob->gl_pathv != NULL)
867 for (i = 0; i < pglob->gl_pathc; ++i)
868 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
869 free (pglob->gl_pathv[pglob->gl_offs + i]);
870 free (pglob->gl_pathv);
871 pglob->gl_pathv = NULL;
874 #if defined _LIBC && !defined globfree
875 libc_hidden_def (globfree)
879 /* Do a collated comparison of A and B. */
881 collated_compare (const void *a, const void *b)
883 char *const *ps1 = a; char *s1 = *ps1;
884 char *const *ps2 = b; char *s2 = *ps2;
892 return strcoll (s1, s2);
896 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
897 elements in place. Return nonzero if out of memory, zero if successful.
898 A slash is inserted between DIRNAME and each elt of ARRAY,
899 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
901 prefix_array (const char *dirname, char **array, size_t n)
904 size_t dirlen = strlen (dirname);
905 #if defined __MSDOS__ || defined WINDOWS32
907 # define DIRSEP_CHAR sep_char
909 # define DIRSEP_CHAR '/'
912 if (dirlen == 1 && dirname[0] == '/')
913 /* DIRNAME is just "/", so normal prepending would get us "//foo".
914 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
916 #if defined __MSDOS__ || defined WINDOWS32
919 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
920 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
922 else if (dirname[dirlen - 1] == ':')
924 /* DIRNAME is "d:". Use `:' instead of `/'. */
931 for (i = 0; i < n; ++i)
933 size_t eltlen = strlen (array[i]) + 1;
934 char *new = malloc (dirlen + 1 + eltlen);
943 char *endp = mempcpy (new, dirname, dirlen);
944 *endp++ = DIRSEP_CHAR;
945 mempcpy (endp, array[i], eltlen);
955 /* We must not compile this function twice. */
956 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
957 /* Return nonzero if PATTERN contains any metacharacters.
958 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
960 __glob_pattern_p (pattern, quote)
964 register const char *p;
967 for (p = pattern; *p != '\0'; ++p)
975 if (quote && p[1] != '\0')
992 weak_alias (__glob_pattern_p, glob_pattern_p)
996 #endif /* !GLOB_ONLY_P */
999 /* We put this in a separate function mainly to allow the memory
1000 allocated with alloca to be recycled. */
1001 #if !defined _LIBC || !defined GLOB_ONLY_P
1003 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1004 glob_t *pglob, int flags)
1006 size_t fnamelen = strlen (fname);
1007 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1011 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1012 fname, fnamelen + 1);
1014 return ((flags & GLOB_ALTDIRFUNC)
1015 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1016 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1021 /* Like `glob', but PATTERN is a final pathname component,
1022 and matches are searched for in DIRECTORY.
1023 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1024 The GLOB_APPEND flag is assumed to be set (always appends). */
1026 glob_in_dir (const char *pattern, const char *directory, int flags,
1027 int (*errfunc) (const char *, int),
1030 size_t dirlen = strlen (directory);
1031 void *stream = NULL;
1034 struct globlink *next;
1037 struct globlink *names = NULL;
1042 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1043 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1045 /* We need not do any tests. The PATTERN contains no meta
1046 characters and we must not return an error therefore the
1047 result will always contain exactly one name. */
1048 flags |= GLOB_NOCHECK;
1051 else if (meta == 0 &&
1052 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1054 /* Since we use the normal file functions we can also use stat()
1055 to verify the file is there. */
1058 size_t patlen = strlen (pattern);
1059 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1061 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1063 pattern, patlen + 1);
1064 if (((flags & GLOB_ALTDIRFUNC)
1065 ? (*pglob->gl_stat) (fullname, &st)
1066 : __stat64 (fullname, &st64)) == 0)
1067 /* We found this file to be existing. Now tell the rest
1068 of the function to copy this name into the result. */
1069 flags |= GLOB_NOCHECK;
1075 if (pattern[0] == '\0')
1077 /* This is a special case for matching directories like in
1079 names = __alloca (sizeof (struct globlink));
1080 names->name = malloc (1);
1081 if (names->name == NULL)
1083 names->name[0] = '\0';
1090 stream = ((flags & GLOB_ALTDIRFUNC)
1091 ? (*pglob->gl_opendir) (directory)
1092 : opendir (directory));
1095 if (errno != ENOTDIR
1096 && ((errfunc != NULL && (*errfunc) (directory, errno))
1097 || (flags & GLOB_ERR)))
1098 return GLOB_ABORTED;
1104 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1105 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1106 #if defined _AMIGA || defined __VMS
1111 flags |= GLOB_MAGCHAR;
1117 #if defined _LIBC && !defined COMPILE_GLOB64
1121 struct dirent64 d64;
1122 char room [offsetof (struct dirent64, d_name[0])
1127 if (flags & GLOB_ALTDIRFUNC)
1129 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1132 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1139 d = __readdir64 (stream);
1141 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1142 ? ((*pglob->gl_readdir) (stream))
1143 : __readdir (stream));
1147 if (! REAL_DIR_ENTRY (d))
1150 /* If we shall match only directories use the information
1151 provided by the dirent call if possible. */
1152 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1157 if (fnmatch (pattern, name, fnm_flags) == 0)
1159 /* ISDIR will often be incorrectly set to false
1160 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1161 don't care. It won't be used and we save the
1162 expensive call to stat. */
1164 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1165 ? GLOB_ONLYDIR : 0));
1166 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1167 || ((flags & need_dir_test)
1168 && is_dir_p (directory, dirlen, name,
1171 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1172 if ((flags & GLOB_ONLYDIR) && !isdir)
1176 struct globlink *new =
1177 __alloca (sizeof (struct globlink));
1179 len = _D_EXACT_NAMLEN (d);
1181 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1182 if (new->name == NULL)
1184 p = mempcpy (new->name, name, len);
1185 if ((flags & GLOB_MARK) && isdir)
1198 if (nfound == 0 && (flags & GLOB_NOCHECK))
1200 size_t len = strlen (pattern);
1202 names = __alloca (sizeof (struct globlink));
1204 names->name = malloc (len + 1);
1205 if (names->name == NULL)
1207 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1212 char **new_gl_pathv;
1215 = realloc (pglob->gl_pathv,
1216 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1218 if (new_gl_pathv == NULL)
1220 pglob->gl_pathv = new_gl_pathv;
1222 for (; names != NULL; names = names->next)
1223 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1224 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1226 pglob->gl_flags = flags;
1232 if (flags & GLOB_ALTDIRFUNC)
1233 (*pglob->gl_closedir) (stream);
1239 return nfound == 0 ? GLOB_NOMATCH : 0;
1244 if (flags & GLOB_ALTDIRFUNC)
1245 (*pglob->gl_closedir) (stream);
1250 while (names != NULL)
1252 if (names->name != NULL)
1254 names = names->next;
1256 return GLOB_NOSPACE;