1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/types.h>
31 /* Outcomment the following line for production quality code. */
32 /* #define NDEBUG 1 */
35 #include <stdio.h> /* Needed on stupid SunOS for assert. */
37 #if !defined _LIBC || !defined GLOB_ONLY_P
38 #if defined HAVE_UNISTD_H || defined _LIBC
41 # ifdef _POSIX_VERSION
51 # define __set_errno(val) errno = (val)
54 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
56 # define NAMLEN(dirent) strlen((dirent)->d_name)
58 # define dirent direct
59 # define NAMLEN(dirent) (dirent)->d_namlen
60 # ifdef HAVE_SYS_NDIR_H
61 # include <sys/ndir.h>
63 # ifdef HAVE_SYS_DIR_H
71 # endif /* HAVE_VMSDIR_H */
75 /* In GNU systems, <dirent.h> defines this macro for us. */
78 # define NAMLEN(d) _D_NAMLEN(d)
81 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
82 if the `d_type' member for `struct dirent' is available.
83 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
84 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
85 /* True if the directory entry D must be of type T. */
86 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
88 /* True if the directory entry D might be a symbolic link. */
89 # define DIRENT_MIGHT_BE_SYMLINK(d) \
90 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
92 /* True if the directory entry D might be a directory. */
93 # define DIRENT_MIGHT_BE_DIR(d) \
94 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
96 #else /* !HAVE_D_TYPE */
97 # define DIRENT_MUST_BE(d, t) false
98 # define DIRENT_MIGHT_BE_SYMLINK(d) true
99 # define DIRENT_MIGHT_BE_DIR(d) true
100 #endif /* HAVE_D_TYPE */
102 /* If the system has the `struct dirent64' type we use it internally. */
103 #if defined _LIBC && !defined COMPILE_GLOB64
104 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
105 # define CONVERT_D_NAMLEN(d64, d32)
107 # define CONVERT_D_NAMLEN(d64, d32) \
108 (d64)->d_namlen = (d32)->d_namlen;
111 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
112 # define CONVERT_D_INO(d64, d32)
114 # define CONVERT_D_INO(d64, d32) \
115 (d64)->d_ino = (d32)->d_ino;
118 # ifdef _DIRENT_HAVE_D_TYPE
119 # define CONVERT_D_TYPE(d64, d32) \
120 (d64)->d_type = (d32)->d_type;
122 # define CONVERT_D_TYPE(d64, d32)
125 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
126 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
127 CONVERT_D_NAMLEN (d64, d32) \
128 CONVERT_D_INO (d64, d32) \
129 CONVERT_D_TYPE (d64, d32)
133 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
134 /* Posix does not require that the d_ino field be present, and some
135 systems do not provide it. */
136 # define REAL_DIR_ENTRY(dp) 1
138 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
144 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
147 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
154 # define strdup(str) __strdup (str)
155 # define sysconf(id) __sysconf (id)
156 # define closedir(dir) __closedir (dir)
157 # define opendir(name) __opendir (name)
158 # define readdir(str) __readdir64 (str)
159 # define getpwnam_r(name, bufp, buf, len, res) \
160 __getpwnam_r (name, bufp, buf, len, res)
162 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
164 # define struct_stat64 struct stat64
166 # include "getlogin_r.h"
167 # include "mempcpy.h"
168 # include "stat-macros.h"
170 # define __stat64(fname, buf) stat (fname, buf)
171 # define struct_stat64 struct stat
172 # define __stat(fname, buf) stat (fname, buf)
173 # define __alloca alloca
174 # define __readdir readdir
175 # define __readdir64 readdir64
176 # 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 GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
189 # define GET_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 #ifndef attribute_hidden
197 # define attribute_hidden
200 static int glob_in_dir (const char *pattern, const char *directory,
201 int flags, int (*errfunc) (const char *, int),
203 extern int __glob_pattern_type (const char *pattern, int quote)
206 #if !defined _LIBC || !defined GLOB_ONLY_P
207 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
208 static int collated_compare (const void *, const void *) __THROW;
211 /* Find the end of the sub-pattern in a brace expression. */
213 next_brace_sub (const char *cp, int flags)
215 unsigned int depth = 0;
217 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
225 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
232 return *cp != '\0' ? cp : NULL;
235 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
237 /* Do glob searching for PATTERN, placing results in PGLOB.
238 The bits defined above may be set in FLAGS.
239 If a directory cannot be opened or read and ERRFUNC is not nil,
240 it is called with the pathname that caused the error, and the
241 `errno' value from the failing call; if it returns non-zero
242 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
243 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
244 Otherwise, `glob' returns zero. */
246 #ifdef GLOB_ATTRIBUTE
249 glob (pattern, flags, errfunc, pglob)
252 int (*errfunc) (const char *, int);
255 const char *filename;
261 int dirname_modified;
264 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
266 __set_errno (EINVAL);
270 if (!(flags & GLOB_DOOFFS))
271 /* Have to do this so `globfree' knows where to start freeing. It
272 also makes all the code that uses gl_offs simpler. */
275 if (flags & GLOB_BRACE)
279 if (flags & GLOB_NOESCAPE)
280 begin = strchr (pattern, '{');
292 if (*begin == '\\' && begin[1] != '\0')
294 else if (*begin == '{')
303 /* Allocate working buffer large enough for our work. Note that
304 we have at least an opening and closing brace. */
312 char onealt[strlen (pattern) - 1];
314 char *onealt = (char *) malloc (strlen (pattern) - 1);
317 if (!(flags & GLOB_APPEND))
320 pglob->gl_pathv = NULL;
326 /* We know the prefix for all sub-patterns. */
327 alt_start = mempcpy (onealt, pattern, begin - pattern);
329 /* Find the first sub-pattern and at the same time find the
330 rest after the closing brace. */
331 next = next_brace_sub (begin + 1, flags);
334 /* It is an illegal expression. */
338 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
341 /* Now find the end of the whole brace expression. */
345 rest = next_brace_sub (rest + 1, flags);
348 /* It is an illegal expression. */
352 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
355 /* Please note that we now can be sure the brace expression
357 rest_len = strlen (++rest) + 1;
359 /* We have a brace expression. BEGIN points to the opening {,
360 NEXT points past the terminator of the first element, and END
361 points past the final }. We will accumulate result names from
362 recursive runs for each brace alternative in the buffer using
365 if (!(flags & GLOB_APPEND))
367 /* This call is to set a new vector, so clear out the
368 vector so we can append to it. */
370 pglob->gl_pathv = NULL;
372 firstc = pglob->gl_pathc;
379 /* Construct the new glob expression. */
380 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
382 result = glob (onealt,
383 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
384 | GLOB_APPEND), errfunc, pglob);
386 /* If we got an error, return it. */
387 if (result && result != GLOB_NOMATCH)
392 if (!(flags & GLOB_APPEND))
401 /* We saw the last entry. */
405 next = next_brace_sub (p, flags);
406 assert (next != NULL);
413 if (pglob->gl_pathc != firstc)
414 /* We found some entries. */
416 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
421 /* Find the filename. */
422 filename = strrchr (pattern, '/');
423 #if defined __MSDOS__ || defined WINDOWS32
424 /* The case of "d:pattern". Since `:' is not allowed in
425 file names, we can safely assume that wherever it
426 happens in pattern, it signals the filename part. This
427 is so we could some day support patterns like "[a-z]:foo". */
428 if (filename == NULL)
429 filename = strchr (pattern, ':');
430 #endif /* __MSDOS__ || WINDOWS32 */
431 dirname_modified = 0;
432 if (filename == NULL)
434 /* This can mean two things: a simple name or "~name". The latter
435 case is nothing but a notation for a directory. */
436 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
439 dirlen = strlen (pattern);
441 /* Set FILENAME to NULL as a special flag. This is ugly but
442 other solutions would require much more code. We test for
443 this special case below. */
457 else if (filename == pattern
458 || (filename == pattern + 1 && pattern[0] == '\\'))
460 /* "/pattern" or "\\/pattern". */
468 dirlen = filename - pattern;
469 #if defined __MSDOS__ || defined WINDOWS32
471 || (filename > pattern + 1 && filename[-1] == ':'))
476 drive_spec = (char *) __alloca (dirlen + 1);
477 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
478 /* For now, disallow wildcards in the drive spec, to
479 prevent infinite recursion in glob. */
480 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
482 /* If this is "d:pattern", we need to copy `:' to DIRNAME
483 as well. If it's "d:/pattern", don't remove the slash
484 from "d:/", since "d:" and "d:/" are not the same.*/
487 newp = (char *) __alloca (dirlen + 1);
488 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
492 if (filename[0] == '\0'
493 #if defined __MSDOS__ || defined WINDOWS32
494 && dirname[dirlen - 1] != ':'
495 && (dirlen < 3 || dirname[dirlen - 2] != ':'
496 || dirname[dirlen - 1] != '/')
499 /* "pattern/". Expand "pattern", appending slashes. */
501 int orig_flags = flags;
502 if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\')
504 /* "pattern\\/". Remove the final backslash if it hasn't
506 char *p = (char *) &dirname[dirlen - 1];
508 while (p > dirname && p[-1] == '\\') --p;
509 if ((&dirname[dirlen] - p) & 1)
511 *(char *) &dirname[--dirlen] = '\0';
512 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
515 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
517 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
518 | (flags & GLOB_MARK));
519 else if (val == GLOB_NOMATCH && flags != orig_flags)
521 /* Make sure globfree (&dirs); is a nop. */
522 dirs.gl_pathv = NULL;
524 oldcount = pglob->gl_pathc + pglob->gl_offs;
531 if (!(flags & GLOB_APPEND))
534 if (!(flags & GLOB_DOOFFS))
535 pglob->gl_pathv = NULL;
539 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
541 if (pglob->gl_pathv == NULL)
544 for (i = 0; i <= pglob->gl_offs; ++i)
545 pglob->gl_pathv[i] = NULL;
549 oldcount = pglob->gl_pathc + pglob->gl_offs;
552 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
554 if (dirname[1] == '\0' || dirname[1] == '/'
555 || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\'
556 && (dirname[2] == '\0' || dirname[2] == '/')))
558 /* Look up home directory. */
559 const char *home_dir = getenv ("HOME");
561 if (home_dir == NULL || home_dir[0] == '\0')
565 if (home_dir == NULL || home_dir[0] == '\0')
566 home_dir = "c:/users/default"; /* poor default */
568 if (home_dir == NULL || home_dir[0] == '\0')
572 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
575 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
578 name = (char *) __alloca (buflen);
580 success = getlogin_r (name, buflen) == 0;
584 # if defined HAVE_GETPWNAM_R || defined _LIBC
585 long int pwbuflen = GETPW_R_SIZE_MAX ();
592 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
593 Try a moderate value. */
596 pwtmpbuf = (char *) __alloca (pwbuflen);
598 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
607 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
611 pwtmpbuf = (char *) __alloca (pwbuflen);
619 home_dir = p->pw_dir;
622 if (home_dir == NULL || home_dir[0] == '\0')
624 if (flags & GLOB_TILDE_CHECK)
627 home_dir = "~"; /* No luck. */
629 # endif /* WINDOWS32 */
631 /* Now construct the full directory. */
632 if (dirname[1] == '\0')
635 dirlen = strlen (dirname);
640 size_t home_len = strlen (home_dir);
641 newp = (char *) __alloca (home_len + dirlen);
642 mempcpy (mempcpy (newp, home_dir, home_len),
643 &dirname[1], dirlen);
645 dirlen += home_len - 1;
647 dirname_modified = 1;
649 # if !defined _AMIGA && !defined WINDOWS32
652 char *end_name = strchr (dirname, '/');
653 const char *user_name;
654 const char *home_dir;
655 char *unescape = NULL;
657 if (!(flags & GLOB_NOESCAPE))
659 if (end_name == NULL)
661 unescape = strchr (dirname, '\\');
663 end_name = strchr (unescape, '\0');
666 unescape = memchr (dirname, '\\', end_name - dirname);
668 if (end_name == NULL)
669 user_name = dirname + 1;
673 newp = (char *) __alloca (end_name - dirname);
674 if (unescape != NULL)
676 char *p = mempcpy (newp, dirname + 1,
677 unescape - dirname - 1);
685 /* "~fo\\o\\" unescape to user_name "foo\\",
686 but "~fo\\o\\/" unescape to user_name
688 if (filename == NULL)
699 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
704 /* Look up specific user's home directory. */
707 # if defined HAVE_GETPWNAM_R || defined _LIBC
708 long int buflen = GETPW_R_SIZE_MAX ();
715 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
719 pwtmpbuf = (char *) __alloca (buflen);
721 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
729 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
732 pwtmpbuf = __alloca (buflen);
737 p = getpwnam (user_name);
740 home_dir = p->pw_dir;
744 /* If we found a home directory use this. */
745 if (home_dir != NULL)
748 size_t home_len = strlen (home_dir);
749 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
750 newp = (char *) __alloca (home_len + rest_len + 1);
751 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
752 end_name, rest_len)) = '\0';
754 dirlen = home_len + rest_len;
755 dirname_modified = 1;
758 if (flags & GLOB_TILDE_CHECK)
759 /* We have to regard it as an error if we cannot find the
763 # endif /* Not Amiga && not WINDOWS32. */
765 #endif /* Not VMS. */
767 /* Now test whether we looked for "~" or "~NAME". In this case we
768 can give the answer now. */
769 if (filename == NULL)
774 /* Return the directory if we don't check for error or if it exists. */
775 if ((flags & GLOB_NOCHECK)
776 || (((flags & GLOB_ALTDIRFUNC)
777 ? ((*pglob->gl_stat) (dirname, &st) == 0
778 && S_ISDIR (st.st_mode))
779 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
781 int newcount = pglob->gl_pathc + pglob->gl_offs;
785 = (char **) realloc (pglob->gl_pathv,
786 (newcount + 1 + 1) * sizeof (char *));
787 if (new_gl_pathv == NULL)
790 free (pglob->gl_pathv);
791 pglob->gl_pathv = NULL;
795 pglob->gl_pathv = new_gl_pathv;
797 if (flags & GLOB_MARK)
800 pglob->gl_pathv[newcount] = malloc (dirlen + 2);
801 if (pglob->gl_pathv[newcount] == NULL)
803 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
809 pglob->gl_pathv[newcount] = strdup (dirname);
810 if (pglob->gl_pathv[newcount] == NULL)
813 pglob->gl_pathv[++newcount] = NULL;
815 pglob->gl_flags = flags;
824 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
825 /* meta is 1 if correct glob pattern containing metacharacters.
826 If meta has bit (1 << 2) set, it means there was an unterminated
827 [ which we handle the same, using fnmatch. Broken unterminated
828 pattern bracket expressions ought to be rare enough that it is
829 not worth special casing them, fnmatch will do the right thing. */
832 /* The directory name contains metacharacters, so we
833 have to glob for the directory, and then glob for
834 the pattern in each directory found. */
837 if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\')
839 /* "foo\\/bar". Remove the final backslash from dirname
840 if it has not been quoted. */
841 char *p = (char *) &dirname[dirlen - 1];
843 while (p > dirname && p[-1] == '\\') --p;
844 if ((&dirname[dirlen] - p) & 1)
845 *(char *) &dirname[--dirlen] = '\0';
848 if ((flags & GLOB_ALTDIRFUNC) != 0)
850 /* Use the alternative access functions also in the recursive
852 dirs.gl_opendir = pglob->gl_opendir;
853 dirs.gl_readdir = pglob->gl_readdir;
854 dirs.gl_closedir = pglob->gl_closedir;
855 dirs.gl_stat = pglob->gl_stat;
856 dirs.gl_lstat = pglob->gl_lstat;
859 status = glob (dirname,
860 ((flags & (GLOB_ERR | GLOB_NOESCAPE
862 | GLOB_NOSORT | GLOB_ONLYDIR),
866 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
871 /* We have successfully globbed the preceding directory name.
872 For each name we found, call glob_in_dir on it and FILENAME,
873 appending the results to PGLOB. */
874 for (i = 0; i < dirs.gl_pathc; ++i)
880 /* Make globbing interruptible in the bash shell. */
881 extern int interrupt_state;
891 old_pathc = pglob->gl_pathc;
892 status = glob_in_dir (filename, dirs.gl_pathv[i],
893 ((flags | GLOB_APPEND)
894 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
896 if (status == GLOB_NOMATCH)
897 /* No matches in this directory. Try the next. */
908 /* Stick the directory on the front of each name. */
909 if (prefix_array (dirs.gl_pathv[i],
910 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
911 pglob->gl_pathc - old_pathc))
920 flags |= GLOB_MAGCHAR;
922 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
923 But if we have not found any matching entry and the GLOB_NOCHECK
924 flag was set we must return the input pattern itself. */
925 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
929 if (flags & GLOB_NOCHECK)
931 int newcount = pglob->gl_pathc + pglob->gl_offs;
934 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
937 if (new_gl_pathv == NULL)
942 pglob->gl_pathv = new_gl_pathv;
944 pglob->gl_pathv[newcount] = __strdup (pattern);
945 if (pglob->gl_pathv[newcount] == NULL)
956 pglob->gl_pathv[newcount] = NULL;
957 pglob->gl_flags = flags;
970 int old_pathc = pglob->gl_pathc;
971 int orig_flags = flags;
975 char *p = strchr (dirname, '\\'), *q;
976 /* We need to unescape the dirname string. It is certainly
977 allocated by alloca, as otherwise filename would be NULL
978 or dirname wouldn't contain backslashes. */
991 while (*p++ != '\0');
992 dirname_modified = 1;
994 if (dirname_modified)
995 flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC);
996 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
999 if (status == GLOB_NOMATCH && flags != orig_flags
1000 && pglob->gl_pathc + pglob->gl_offs == oldcount)
1002 /* Make sure globfree (&dirs); is a nop. */
1003 dirs.gl_pathv = NULL;
1012 /* Stick the directory on the front of each name. */
1013 if (prefix_array (dirname,
1014 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1015 pglob->gl_pathc - old_pathc))
1018 pglob->gl_pathc = 0;
1019 return GLOB_NOSPACE;
1024 if (flags & GLOB_MARK)
1026 /* Append slashes to directory names. */
1031 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1032 if (((flags & GLOB_ALTDIRFUNC)
1033 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1034 && S_ISDIR (st.st_mode))
1035 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1036 && S_ISDIR (st64.st_mode))))
1038 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1039 char *new = realloc (pglob->gl_pathv[i], len);
1043 pglob->gl_pathc = 0;
1044 return GLOB_NOSPACE;
1046 strcpy (&new[len - 2], "/");
1047 pglob->gl_pathv[i] = new;
1051 if (!(flags & GLOB_NOSORT))
1053 /* Sort the vector. */
1054 qsort (&pglob->gl_pathv[oldcount],
1055 pglob->gl_pathc + pglob->gl_offs - oldcount,
1056 sizeof (char *), collated_compare);
1061 #if defined _LIBC && !defined glob
1062 libc_hidden_def (glob)
1066 #if !defined _LIBC || !defined GLOB_ONLY_P
1068 /* Free storage allocated in PGLOB by a previous `glob' call. */
1071 register glob_t *pglob;
1073 if (pglob->gl_pathv != NULL)
1076 for (i = 0; i < pglob->gl_pathc; ++i)
1077 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
1078 free (pglob->gl_pathv[pglob->gl_offs + i]);
1079 free (pglob->gl_pathv);
1080 pglob->gl_pathv = NULL;
1083 #if defined _LIBC && !defined globfree
1084 libc_hidden_def (globfree)
1088 /* Do a collated comparison of A and B. */
1090 collated_compare (const void *a, const void *b)
1092 const char *const s1 = *(const char *const * const) a;
1093 const char *const s2 = *(const char *const * const) b;
1101 return strcoll (s1, s2);
1105 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1106 elements in place. Return nonzero if out of memory, zero if successful.
1107 A slash is inserted between DIRNAME and each elt of ARRAY,
1108 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1110 prefix_array (const char *dirname, char **array, size_t n)
1113 size_t dirlen = strlen (dirname);
1114 #if defined __MSDOS__ || defined WINDOWS32
1116 # define DIRSEP_CHAR sep_char
1118 # define DIRSEP_CHAR '/'
1121 if (dirlen == 1 && dirname[0] == '/')
1122 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1123 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1125 #if defined __MSDOS__ || defined WINDOWS32
1126 else if (dirlen > 1)
1128 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1129 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1131 else if (dirname[dirlen - 1] == ':')
1133 /* DIRNAME is "d:". Use `:' instead of `/'. */
1140 for (i = 0; i < n; ++i)
1142 size_t eltlen = strlen (array[i]) + 1;
1143 char *new = (char *) malloc (dirlen + 1 + eltlen);
1152 char *endp = mempcpy (new, dirname, dirlen);
1153 *endp++ = DIRSEP_CHAR;
1154 mempcpy (endp, array[i], eltlen);
1164 /* We must not compile this function twice. */
1165 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1167 __glob_pattern_type (pattern, quote)
1168 const char *pattern;
1171 register const char *p;
1174 for (p = pattern; *p != '\0'; ++p)
1203 /* Return nonzero if PATTERN contains any metacharacters.
1204 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1206 __glob_pattern_p (pattern, quote)
1207 const char *pattern;
1210 return __glob_pattern_type (pattern, quote) == 1;
1213 weak_alias (__glob_pattern_p, glob_pattern_p)
1217 #endif /* !GLOB_ONLY_P */
1220 /* We put this in a separate function mainly to allow the memory
1221 allocated with alloca to be recycled. */
1222 #if !defined _LIBC || !defined GLOB_ONLY_P
1224 link_exists_p (const char *dir, size_t dirlen, const char *fname,
1225 glob_t *pglob, int flags)
1227 size_t fnamelen = strlen (fname);
1228 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1232 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1233 fname, fnamelen + 1);
1235 return (((flags & GLOB_ALTDIRFUNC)
1236 ? (*pglob->gl_stat) (fullname, &st)
1237 : __stat64 (fullname, &st64)) == 0);
1242 /* Like `glob', but PATTERN is a final pathname component,
1243 and matches are searched for in DIRECTORY.
1244 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1245 The GLOB_APPEND flag is assumed to be set (always appends). */
1247 glob_in_dir (const char *pattern, const char *directory, int flags,
1248 int (*errfunc) (const char *, int),
1251 size_t dirlen = strlen (directory);
1252 void *stream = NULL;
1255 struct globnames *next;
1259 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1260 struct globnames init_names;
1261 struct globnames *names = &init_names;
1262 struct globnames *names_alloca = &init_names;
1264 size_t allocasize = sizeof (init_names);
1269 init_names.next = NULL;
1270 init_names.count = INITIAL_COUNT;
1272 meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE));
1273 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1275 /* We need not do any tests. The PATTERN contains no meta
1276 characters and we must not return an error therefore the
1277 result will always contain exactly one name. */
1278 flags |= GLOB_NOCHECK;
1282 /* Since we use the normal file functions we can also use stat()
1283 to verify the file is there. */
1286 size_t patlen = strlen (pattern);
1287 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1289 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1291 pattern, patlen + 1);
1292 if (((flags & GLOB_ALTDIRFUNC)
1293 ? (*pglob->gl_stat) (fullname, &st)
1294 : __stat64 (fullname, &st64)) == 0)
1295 /* We found this file to be existing. Now tell the rest
1296 of the function to copy this name into the result. */
1297 flags |= GLOB_NOCHECK;
1301 stream = ((flags & GLOB_ALTDIRFUNC)
1302 ? (*pglob->gl_opendir) (directory)
1303 : opendir (directory));
1306 if (errno != ENOTDIR
1307 && ((errfunc != NULL && (*errfunc) (directory, errno))
1308 || (flags & GLOB_ERR)))
1309 return GLOB_ABORTED;
1313 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1314 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1315 #if defined _AMIGA || defined VMS
1319 flags |= GLOB_MAGCHAR;
1325 #if defined _LIBC && !defined COMPILE_GLOB64
1329 struct dirent64 d64;
1330 char room [offsetof (struct dirent64, d_name[0])
1335 if (flags & GLOB_ALTDIRFUNC)
1337 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1340 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1347 d = __readdir64 (stream);
1349 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1350 ? ((struct dirent *)
1351 (*pglob->gl_readdir) (stream))
1352 : __readdir (stream));
1356 if (! REAL_DIR_ENTRY (d))
1359 /* If we shall match only directories use the information
1360 provided by the dirent call if possible. */
1361 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1366 if (fnmatch (pattern, name, fnm_flags) == 0)
1368 /* If the file we found is a symlink we have to
1369 make sure the target file exists. */
1370 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1371 || link_exists_p (directory, dirlen, name, pglob,
1374 if (cur == names->count)
1376 struct globnames *newnames;
1377 size_t count = names->count * 2;
1378 size_t size = (sizeof (struct globnames)
1379 + ((count - INITIAL_COUNT)
1380 * sizeof (char *)));
1382 if (__libc_use_alloca (allocasize))
1383 newnames = names_alloca = __alloca (size);
1384 else if ((newnames = malloc (size))
1387 newnames->count = count;
1388 newnames->next = names;
1393 names->name[cur] = (char *) malloc (len + 1);
1394 if (names->name[cur] == NULL)
1396 *((char *) mempcpy (names->name[cur++], name, len))
1405 if (nfound == 0 && (flags & GLOB_NOCHECK))
1407 size_t len = strlen (pattern);
1409 names->name[cur] = (char *) malloc (len + 1);
1410 if (names->name[cur] == NULL)
1412 *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0';
1415 int result = GLOB_NOMATCH;
1420 char **new_gl_pathv;
1422 = (char **) realloc (pglob->gl_pathv,
1423 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1425 if (new_gl_pathv == NULL)
1430 struct globnames *old = names;
1431 for (size_t i = 0; i < cur; ++i)
1432 free (names->name[i]);
1433 names = names->next;
1434 /* NB: we will not leak memory here if we exit without
1435 freeing the current block assigned to OLD. At least
1436 the very first block is always allocated on the stack
1437 and this is the block assigned to OLD here. */
1440 assert (old == &init_names);
1444 if (old == names_alloca)
1445 names_alloca = names;
1449 result = GLOB_NOSPACE;
1455 struct globnames *old = names;
1456 for (size_t i = 0; i < cur; ++i)
1457 new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++]
1459 names = names->next;
1460 /* NB: we will not leak memory here if we exit without
1461 freeing the current block assigned to OLD. At least
1462 the very first block is always allocated on the stack
1463 and this is the block assigned to OLD here. */
1466 assert (old == &init_names);
1470 if (old == names_alloca)
1471 names_alloca = names;
1476 pglob->gl_pathv = new_gl_pathv;
1478 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1480 pglob->gl_flags = flags;
1487 if (flags & GLOB_ALTDIRFUNC)
1488 (*pglob->gl_closedir) (stream);