1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011
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
49 #if defined HAVE_STDINT_H || defined _LIBC
51 #elif !defined UINTPTR_MAX
52 # define UINTPTR_MAX (~((size_t) 0))
57 # define __set_errno(val) errno = (val)
60 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
62 # define NAMLEN(dirent) strlen((dirent)->d_name)
64 # define dirent direct
65 # define NAMLEN(dirent) (dirent)->d_namlen
66 # ifdef HAVE_SYS_NDIR_H
67 # include <sys/ndir.h>
69 # ifdef HAVE_SYS_DIR_H
77 # endif /* HAVE_VMSDIR_H */
81 /* In GNU systems, <dirent.h> defines this macro for us. */
84 # define NAMLEN(d) _D_NAMLEN(d)
87 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
88 if the `d_type' member for `struct dirent' is available.
89 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
90 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
91 /* True if the directory entry D must be of type T. */
92 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
94 /* True if the directory entry D might be a symbolic link. */
95 # define DIRENT_MIGHT_BE_SYMLINK(d) \
96 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
98 /* True if the directory entry D might be a directory. */
99 # define DIRENT_MIGHT_BE_DIR(d) \
100 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
102 #else /* !HAVE_D_TYPE */
103 # define DIRENT_MUST_BE(d, t) false
104 # define DIRENT_MIGHT_BE_SYMLINK(d) true
105 # define DIRENT_MIGHT_BE_DIR(d) true
106 #endif /* HAVE_D_TYPE */
108 /* If the system has the `struct dirent64' type we use it internally. */
109 #if defined _LIBC && !defined COMPILE_GLOB64
110 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
111 # define CONVERT_D_NAMLEN(d64, d32)
113 # define CONVERT_D_NAMLEN(d64, d32) \
114 (d64)->d_namlen = (d32)->d_namlen;
117 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
118 # define CONVERT_D_INO(d64, d32)
120 # define CONVERT_D_INO(d64, d32) \
121 (d64)->d_ino = (d32)->d_ino;
124 # ifdef _DIRENT_HAVE_D_TYPE
125 # define CONVERT_D_TYPE(d64, d32) \
126 (d64)->d_type = (d32)->d_type;
128 # define CONVERT_D_TYPE(d64, d32)
131 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
132 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
133 CONVERT_D_NAMLEN (d64, d32) \
134 CONVERT_D_INO (d64, d32) \
135 CONVERT_D_TYPE (d64, d32)
139 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
140 /* Posix does not require that the d_ino field be present, and some
141 systems do not provide it. */
142 # define REAL_DIR_ENTRY(dp) 1
144 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
150 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
153 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
160 # define strdup(str) __strdup (str)
161 # define sysconf(id) __sysconf (id)
162 # define closedir(dir) __closedir (dir)
163 # define opendir(name) __opendir (name)
164 # define readdir(str) __readdir64 (str)
165 # define getpwnam_r(name, bufp, buf, len, res) \
166 __getpwnam_r (name, bufp, buf, len, res)
168 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
170 # define struct_stat64 struct stat64
172 # include "getlogin_r.h"
173 # include "mempcpy.h"
174 # include "stat-macros.h"
176 # define __stat64(fname, buf) stat (fname, buf)
177 # define struct_stat64 struct stat
178 # define __stat(fname, buf) stat (fname, buf)
179 # define __alloca alloca
180 # define __readdir readdir
181 # define __readdir64 readdir64
182 # define __glob_pattern_p glob_pattern_p
187 #ifdef _SC_GETPW_R_SIZE_MAX
188 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
190 # define GETPW_R_SIZE_MAX() (-1)
192 #ifdef _SC_LOGIN_NAME_MAX
193 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
195 # define GET_LOGIN_NAME_MAX() (-1)
198 static const char *next_brace_sub (const char *begin
, int flags
) __THROW
;
200 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
202 #ifndef attribute_hidden
203 # define attribute_hidden
206 static int glob_in_dir (const char *pattern
, const char *directory
,
207 int flags
, int (*errfunc
) (const char *, int),
208 glob_t
*pglob
, size_t alloca_used
);
209 extern int __glob_pattern_type (const char *pattern
, int quote
)
212 #if !defined _LIBC || !defined GLOB_ONLY_P
213 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROW
;
214 static int collated_compare (const void *, const void *) __THROW
;
217 /* Find the end of the sub-pattern in a brace expression. */
219 next_brace_sub (const char *cp
, int flags
)
221 unsigned int depth
= 0;
223 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
231 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
238 return *cp
!= '\0' ? cp
: NULL
;
241 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
243 /* Do glob searching for PATTERN, placing results in PGLOB.
244 The bits defined above may be set in FLAGS.
245 If a directory cannot be opened or read and ERRFUNC is not nil,
246 it is called with the pathname that caused the error, and the
247 `errno' value from the failing call; if it returns non-zero
248 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
249 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
250 Otherwise, `glob' returns zero. */
252 #ifdef GLOB_ATTRIBUTE
255 glob (pattern
, flags
, errfunc
, pglob
)
258 int (*errfunc
) (const char *, int);
261 const char *filename
;
262 char *dirname
= NULL
;
267 int dirname_modified
;
268 int malloc_dirname
= 0;
272 size_t alloca_used
= 0;
275 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
277 __set_errno (EINVAL
);
281 if (!(flags
& GLOB_DOOFFS
))
282 /* Have to do this so `globfree' knows where to start freeing. It
283 also makes all the code that uses gl_offs simpler. */
286 if (flags
& GLOB_BRACE
)
290 if (flags
& GLOB_NOESCAPE
)
291 begin
= strchr (pattern
, '{');
303 if (*begin
== '\\' && begin
[1] != '\0')
305 else if (*begin
== '{')
314 /* Allocate working buffer large enough for our work. Note that
315 we have at least an opening and closing brace. */
323 size_t pattern_len
= strlen (pattern
) - 1;
325 int alloca_onealt
= __libc_use_alloca (alloca_used
+ pattern_len
);
327 onealt
= alloca_account (pattern_len
, alloca_used
);
331 onealt
= (char *) malloc (pattern_len
);
334 if (!(flags
& GLOB_APPEND
))
337 pglob
->gl_pathv
= NULL
;
343 /* We know the prefix for all sub-patterns. */
344 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
346 /* Find the first sub-pattern and at the same time find the
347 rest after the closing brace. */
348 next
= next_brace_sub (begin
+ 1, flags
);
351 /* It is an illegal expression. */
354 if (__builtin_expect (!alloca_onealt
, 0))
357 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
360 /* Now find the end of the whole brace expression. */
364 rest
= next_brace_sub (rest
+ 1, flags
);
366 /* It is an illegal expression. */
369 /* Please note that we now can be sure the brace expression
371 rest_len
= strlen (++rest
) + 1;
373 /* We have a brace expression. BEGIN points to the opening {,
374 NEXT points past the terminator of the first element, and END
375 points past the final }. We will accumulate result names from
376 recursive runs for each brace alternative in the buffer using
379 if (!(flags
& GLOB_APPEND
))
381 /* This call is to set a new vector, so clear out the
382 vector so we can append to it. */
384 pglob
->gl_pathv
= NULL
;
386 firstc
= pglob
->gl_pathc
;
393 /* Construct the new glob expression. */
394 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
396 result
= glob (onealt
,
397 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
398 | GLOB_APPEND
), errfunc
, pglob
);
400 /* If we got an error, return it. */
401 if (result
&& result
!= GLOB_NOMATCH
)
404 if (__builtin_expect (!alloca_onealt
, 0))
407 if (!(flags
& GLOB_APPEND
))
416 /* We saw the last entry. */
420 next
= next_brace_sub (p
, flags
);
421 assert (next
!= NULL
);
425 if (__builtin_expect (!alloca_onealt
, 0))
429 if (pglob
->gl_pathc
!= firstc
)
430 /* We found some entries. */
432 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
437 if (!(flags
& GLOB_APPEND
))
440 if (!(flags
& GLOB_DOOFFS
))
441 pglob
->gl_pathv
= NULL
;
446 if (pglob
->gl_offs
>= ~((size_t) 0) / sizeof (char *))
449 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
451 if (pglob
->gl_pathv
== NULL
)
454 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
455 pglob
->gl_pathv
[i
] = NULL
;
459 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
461 /* Find the filename. */
462 filename
= strrchr (pattern
, '/');
463 #if defined __MSDOS__ || defined WINDOWS32
464 /* The case of "d:pattern". Since `:' is not allowed in
465 file names, we can safely assume that wherever it
466 happens in pattern, it signals the filename part. This
467 is so we could some day support patterns like "[a-z]:foo". */
468 if (filename
== NULL
)
469 filename
= strchr (pattern
, ':');
470 #endif /* __MSDOS__ || WINDOWS32 */
471 dirname_modified
= 0;
472 if (filename
== NULL
)
474 /* This can mean two things: a simple name or "~name". The latter
475 case is nothing but a notation for a directory. */
476 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
478 dirname
= (char *) pattern
;
479 dirlen
= strlen (pattern
);
481 /* Set FILENAME to NULL as a special flag. This is ugly but
482 other solutions would require much more code. We test for
483 this special case below. */
488 if (__builtin_expect (pattern
[0] == '\0', 0))
490 dirs
.gl_pathv
= NULL
;
496 dirname
= (char *) "";
498 dirname
= (char *) ".";
503 else if (filename
== pattern
504 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
505 && (flags
& GLOB_NOESCAPE
) == 0))
507 /* "/pattern" or "\\/pattern". */
508 dirname
= (char *) "/";
515 dirlen
= filename
- pattern
;
516 #if defined __MSDOS__ || defined WINDOWS32
518 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
523 drive_spec
= (char *) __alloca (dirlen
+ 1);
524 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
525 /* For now, disallow wildcards in the drive spec, to
526 prevent infinite recursion in glob. */
527 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
529 /* If this is "d:pattern", we need to copy `:' to DIRNAME
530 as well. If it's "d:/pattern", don't remove the slash
531 from "d:/", since "d:" and "d:/" are not the same.*/
535 if (__libc_use_alloca (alloca_used
+ dirlen
+ 1))
536 newp
= alloca_account (dirlen
+ 1, alloca_used
);
540 newp
= malloc (dirlen
+ 1);
545 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
549 if (filename
[0] == '\0'
550 #if defined __MSDOS__ || defined WINDOWS32
551 && dirname
[dirlen
- 1] != ':'
552 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
553 || dirname
[dirlen
- 1] != '/')
556 /* "pattern/". Expand "pattern", appending slashes. */
558 int orig_flags
= flags
;
559 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
561 /* "pattern\\/". Remove the final backslash if it hasn't
563 char *p
= (char *) &dirname
[dirlen
- 1];
565 while (p
> dirname
&& p
[-1] == '\\') --p
;
566 if ((&dirname
[dirlen
] - p
) & 1)
568 *(char *) &dirname
[--dirlen
] = '\0';
569 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
572 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
574 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
575 | (flags
& GLOB_MARK
));
576 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
578 /* Make sure globfree (&dirs); is a nop. */
579 dirs
.gl_pathv
= NULL
;
581 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
590 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
592 if (dirname
[1] == '\0' || dirname
[1] == '/'
593 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
594 && (dirname
[2] == '\0' || dirname
[2] == '/')))
596 /* Look up home directory. */
597 char *home_dir
= getenv ("HOME");
598 int malloc_home_dir
= 0;
600 if (home_dir
== NULL
|| home_dir
[0] == '\0')
604 if (home_dir
== NULL
|| home_dir
[0] == '\0')
605 home_dir
= "c:/users/default"; /* poor default */
607 if (home_dir
== NULL
|| home_dir
[0] == '\0')
611 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
614 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
617 name
= alloca_account (buflen
, alloca_used
);
619 success
= getlogin_r (name
, buflen
) == 0;
623 # if defined HAVE_GETPWNAM_R || defined _LIBC
624 long int pwbuflen
= GETPW_R_SIZE_MAX ();
627 int malloc_pwtmpbuf
= 0;
632 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
633 Try a moderate value. */
636 if (__libc_use_alloca (alloca_used
+ pwbuflen
))
637 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
640 pwtmpbuf
= malloc (pwbuflen
);
641 if (pwtmpbuf
== NULL
)
643 retval
= GLOB_NOSPACE
;
649 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
659 && __libc_use_alloca (alloca_used
661 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
666 char *newp
= realloc (malloc_pwtmpbuf
671 if (__builtin_expect (malloc_pwtmpbuf
, 0))
673 retval
= GLOB_NOSPACE
;
677 pwbuflen
= 2 * pwbuflen
;
687 if (!malloc_pwtmpbuf
)
688 home_dir
= p
->pw_dir
;
691 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
692 if (__libc_use_alloca (alloca_used
+ home_dir_len
))
693 home_dir
= alloca_account (home_dir_len
,
697 home_dir
= malloc (home_dir_len
);
698 if (home_dir
== NULL
)
701 retval
= GLOB_NOSPACE
;
706 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
713 if (home_dir
== NULL
|| home_dir
[0] == '\0')
715 if (flags
& GLOB_TILDE_CHECK
)
717 if (__builtin_expect (malloc_home_dir
, 0))
719 retval
= GLOB_NOMATCH
;
723 home_dir
= (char *) "~"; /* No luck. */
725 # endif /* WINDOWS32 */
727 /* Now construct the full directory. */
728 if (dirname
[1] == '\0')
730 if (__builtin_expect (malloc_dirname
, 0))
734 dirlen
= strlen (dirname
);
735 malloc_dirname
= malloc_home_dir
;
740 size_t home_len
= strlen (home_dir
);
741 int use_alloca
= __libc_use_alloca (alloca_used
742 + home_len
+ dirlen
);
744 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
747 newp
= malloc (home_len
+ dirlen
);
750 if (__builtin_expect (malloc_home_dir
, 0))
752 retval
= GLOB_NOSPACE
;
757 mempcpy (mempcpy (newp
, home_dir
, home_len
),
758 &dirname
[1], dirlen
);
760 if (__builtin_expect (malloc_dirname
, 0))
764 dirlen
+= home_len
- 1;
765 malloc_dirname
= !use_alloca
;
767 dirname_modified
= 1;
769 # if !defined _AMIGA && !defined WINDOWS32
772 char *end_name
= strchr (dirname
, '/');
774 int malloc_user_name
= 0;
775 char *unescape
= NULL
;
777 if (!(flags
& GLOB_NOESCAPE
))
779 if (end_name
== NULL
)
781 unescape
= strchr (dirname
, '\\');
783 end_name
= strchr (unescape
, '\0');
786 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
788 if (end_name
== NULL
)
789 user_name
= dirname
+ 1;
793 if (__libc_use_alloca (alloca_used
+ (end_name
- dirname
)))
794 newp
= alloca_account (end_name
- dirname
, alloca_used
);
797 newp
= malloc (end_name
- dirname
);
800 retval
= GLOB_NOSPACE
;
803 malloc_user_name
= 1;
805 if (unescape
!= NULL
)
807 char *p
= mempcpy (newp
, dirname
+ 1,
808 unescape
- dirname
- 1);
816 /* "~fo\\o\\" unescape to user_name "foo\\",
817 but "~fo\\o\\/" unescape to user_name
819 if (filename
== NULL
)
830 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
835 /* Look up specific user's home directory. */
838 # if defined HAVE_GETPWNAM_R || defined _LIBC
839 long int buflen
= GETPW_R_SIZE_MAX ();
841 int malloc_pwtmpbuf
= 0;
847 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
851 if (__libc_use_alloca (alloca_used
+ buflen
))
852 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
855 pwtmpbuf
= malloc (buflen
);
856 if (pwtmpbuf
== NULL
)
859 if (__builtin_expect (malloc_user_name
, 0))
861 retval
= GLOB_NOSPACE
;
867 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
875 && __libc_use_alloca (alloca_used
+ 2 * buflen
))
876 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
877 2 * buflen
, alloca_used
);
880 char *newp
= realloc (malloc_pwtmpbuf
? pwtmpbuf
: NULL
,
884 if (__builtin_expect (malloc_pwtmpbuf
, 0))
894 p
= getpwnam (user_name
);
897 if (__builtin_expect (malloc_user_name
, 0))
900 /* If we found a home directory use this. */
903 size_t home_len
= strlen (p
->pw_dir
);
904 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
906 if (__builtin_expect (malloc_dirname
, 0))
910 if (__libc_use_alloca (alloca_used
+ home_len
+ rest_len
+ 1))
911 dirname
= alloca_account (home_len
+ rest_len
+ 1,
915 dirname
= malloc (home_len
+ rest_len
+ 1);
918 if (__builtin_expect (malloc_pwtmpbuf
, 0))
920 retval
= GLOB_NOSPACE
;
925 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
926 end_name
, rest_len
)) = '\0';
928 dirlen
= home_len
+ rest_len
;
929 dirname_modified
= 1;
931 if (__builtin_expect (malloc_pwtmpbuf
, 0))
936 if (__builtin_expect (malloc_pwtmpbuf
, 0))
939 if (flags
& GLOB_TILDE_CHECK
)
940 /* We have to regard it as an error if we cannot find the
946 # endif /* Not Amiga && not WINDOWS32. */
948 #endif /* Not VMS. */
950 /* Now test whether we looked for "~" or "~NAME". In this case we
951 can give the answer now. */
952 if (filename
== NULL
)
957 /* Return the directory if we don't check for error or if it exists. */
958 if ((flags
& GLOB_NOCHECK
)
959 || (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
960 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
961 && S_ISDIR (st
.st_mode
))
962 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
964 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
967 if (newcount
> UINTPTR_MAX
- (1 + 1)
968 || newcount
+ 1 + 1 > ~((size_t) 0) / sizeof (char *))
971 free (pglob
->gl_pathv
);
972 pglob
->gl_pathv
= NULL
;
978 = (char **) realloc (pglob
->gl_pathv
,
979 (newcount
+ 1 + 1) * sizeof (char *));
980 if (new_gl_pathv
== NULL
)
982 pglob
->gl_pathv
= new_gl_pathv
;
984 if (flags
& GLOB_MARK
)
987 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
988 if (pglob
->gl_pathv
[newcount
] == NULL
)
990 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
996 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
997 if (pglob
->gl_pathv
[newcount
] == NULL
)
1000 pglob
->gl_pathv
[++newcount
] = NULL
;
1002 pglob
->gl_flags
= flags
;
1008 return GLOB_NOMATCH
;
1011 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1012 /* meta is 1 if correct glob pattern containing metacharacters.
1013 If meta has bit (1 << 2) set, it means there was an unterminated
1014 [ which we handle the same, using fnmatch. Broken unterminated
1015 pattern bracket expressions ought to be rare enough that it is
1016 not worth special casing them, fnmatch will do the right thing. */
1019 /* The directory name contains metacharacters, so we
1020 have to glob for the directory, and then glob for
1021 the pattern in each directory found. */
1024 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1026 /* "foo\\/bar". Remove the final backslash from dirname
1027 if it has not been quoted. */
1028 char *p
= (char *) &dirname
[dirlen
- 1];
1030 while (p
> dirname
&& p
[-1] == '\\') --p
;
1031 if ((&dirname
[dirlen
] - p
) & 1)
1032 *(char *) &dirname
[--dirlen
] = '\0';
1035 if (__builtin_expect ((flags
& GLOB_ALTDIRFUNC
) != 0, 0))
1037 /* Use the alternative access functions also in the recursive
1039 dirs
.gl_opendir
= pglob
->gl_opendir
;
1040 dirs
.gl_readdir
= pglob
->gl_readdir
;
1041 dirs
.gl_closedir
= pglob
->gl_closedir
;
1042 dirs
.gl_stat
= pglob
->gl_stat
;
1043 dirs
.gl_lstat
= pglob
->gl_lstat
;
1046 status
= glob (dirname
,
1047 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1049 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1053 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1058 /* We have successfully globbed the preceding directory name.
1059 For each name we found, call glob_in_dir on it and FILENAME,
1060 appending the results to PGLOB. */
1061 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1067 /* Make globbing interruptible in the bash shell. */
1068 extern int interrupt_state
;
1070 if (interrupt_state
)
1073 return GLOB_ABORTED
;
1078 old_pathc
= pglob
->gl_pathc
;
1079 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1080 ((flags
| GLOB_APPEND
)
1081 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1082 errfunc
, pglob
, alloca_used
);
1083 if (status
== GLOB_NOMATCH
)
1084 /* No matches in this directory. Try the next. */
1091 pglob
->gl_pathc
= 0;
1095 /* Stick the directory on the front of each name. */
1096 if (prefix_array (dirs
.gl_pathv
[i
],
1097 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1098 pglob
->gl_pathc
- old_pathc
))
1102 pglob
->gl_pathc
= 0;
1103 return GLOB_NOSPACE
;
1107 flags
|= GLOB_MAGCHAR
;
1109 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1110 But if we have not found any matching entry and the GLOB_NOCHECK
1111 flag was set we must return the input pattern itself. */
1112 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1116 if (flags
& GLOB_NOCHECK
)
1118 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1119 char **new_gl_pathv
;
1121 if (newcount
> UINTPTR_MAX
- 2
1122 || newcount
+ 2 > ~((size_t) 0) / sizeof (char *))
1126 return GLOB_NOSPACE
;
1129 new_gl_pathv
= (char **) realloc (pglob
->gl_pathv
,
1132 if (new_gl_pathv
== NULL
)
1134 pglob
->gl_pathv
= new_gl_pathv
;
1136 pglob
->gl_pathv
[newcount
] = __strdup (pattern
);
1137 if (pglob
->gl_pathv
[newcount
] == NULL
)
1141 pglob
->gl_pathc
= 0;
1142 return GLOB_NOSPACE
;
1148 pglob
->gl_pathv
[newcount
] = NULL
;
1149 pglob
->gl_flags
= flags
;
1154 return GLOB_NOMATCH
;
1162 int old_pathc
= pglob
->gl_pathc
;
1163 int orig_flags
= flags
;
1167 char *p
= strchr (dirname
, '\\'), *q
;
1168 /* We need to unescape the dirname string. It is certainly
1169 allocated by alloca, as otherwise filename would be NULL
1170 or dirname wouldn't contain backslashes. */
1183 while (*p
++ != '\0');
1184 dirname_modified
= 1;
1186 if (dirname_modified
)
1187 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1188 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1192 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1193 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1195 /* Make sure globfree (&dirs); is a nop. */
1196 dirs
.gl_pathv
= NULL
;
1205 /* Stick the directory on the front of each name. */
1206 if (prefix_array (dirname
,
1207 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1208 pglob
->gl_pathc
- old_pathc
))
1211 pglob
->gl_pathc
= 0;
1212 return GLOB_NOSPACE
;
1217 if (flags
& GLOB_MARK
)
1219 /* Append slashes to directory names. */
1224 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1225 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1226 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1227 && S_ISDIR (st
.st_mode
))
1228 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1229 && S_ISDIR (st64
.st_mode
))))
1231 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1232 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1236 pglob
->gl_pathc
= 0;
1237 return GLOB_NOSPACE
;
1239 strcpy (&new[len
- 2], "/");
1240 pglob
->gl_pathv
[i
] = new;
1244 if (!(flags
& GLOB_NOSORT
))
1246 /* Sort the vector. */
1247 qsort (&pglob
->gl_pathv
[oldcount
],
1248 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1249 sizeof (char *), collated_compare
);
1253 if (__builtin_expect (malloc_dirname
, 0))
1258 #if defined _LIBC && !defined glob
1259 libc_hidden_def (glob
)
1263 #if !defined _LIBC || !defined GLOB_ONLY_P
1265 /* Free storage allocated in PGLOB by a previous `glob' call. */
1268 register glob_t
*pglob
;
1270 if (pglob
->gl_pathv
!= NULL
)
1273 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1274 free (pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1275 free (pglob
->gl_pathv
);
1276 pglob
->gl_pathv
= NULL
;
1279 #if defined _LIBC && !defined globfree
1280 libc_hidden_def (globfree
)
1284 /* Do a collated comparison of A and B. */
1286 collated_compare (const void *a
, const void *b
)
1288 const char *const s1
= *(const char *const * const) a
;
1289 const char *const s2
= *(const char *const * const) b
;
1297 return strcoll (s1
, s2
);
1301 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1302 elements in place. Return nonzero if out of memory, zero if successful.
1303 A slash is inserted between DIRNAME and each elt of ARRAY,
1304 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1306 prefix_array (const char *dirname
, char **array
, size_t n
)
1309 size_t dirlen
= strlen (dirname
);
1310 #if defined __MSDOS__ || defined WINDOWS32
1312 # define DIRSEP_CHAR sep_char
1314 # define DIRSEP_CHAR '/'
1317 if (dirlen
== 1 && dirname
[0] == '/')
1318 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1319 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1321 #if defined __MSDOS__ || defined WINDOWS32
1322 else if (dirlen
> 1)
1324 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1325 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1327 else if (dirname
[dirlen
- 1] == ':')
1329 /* DIRNAME is "d:". Use `:' instead of `/'. */
1336 for (i
= 0; i
< n
; ++i
)
1338 size_t eltlen
= strlen (array
[i
]) + 1;
1339 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1348 char *endp
= mempcpy (new, dirname
, dirlen
);
1349 *endp
++ = DIRSEP_CHAR
;
1350 mempcpy (endp
, array
[i
], eltlen
);
1360 /* We must not compile this function twice. */
1361 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1363 __glob_pattern_type (pattern
, quote
)
1364 const char *pattern
;
1367 register const char *p
;
1370 for (p
= pattern
; *p
!= '\0'; ++p
)
1399 /* Return nonzero if PATTERN contains any metacharacters.
1400 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1402 __glob_pattern_p (pattern
, quote
)
1403 const char *pattern
;
1406 return __glob_pattern_type (pattern
, quote
) == 1;
1409 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1413 #endif /* !GLOB_ONLY_P */
1416 /* We put this in a separate function mainly to allow the memory
1417 allocated with alloca to be recycled. */
1418 #if !defined _LIBC || !defined GLOB_ONLY_P
1420 __attribute_noinline__
1421 link_exists2_p (const char *dir
, size_t dirlen
, const char *fname
,
1428 size_t fnamelen
= strlen (fname
);
1429 char *fullname
= (char *) __alloca (dirlen
+ 1 + fnamelen
+ 1);
1435 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1436 fname
, fnamelen
+ 1);
1439 return (*pglob
->gl_stat
) (fullname
, &st
) == 0;
1441 return ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1442 ? (*pglob
->gl_stat
) (fullname
, &st
)
1443 : __stat64 (fullname
, &st64
)) == 0);
1447 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1448 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1449 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1450 : ({ struct stat64 st64; \
1451 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1453 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1454 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1459 /* Like `glob', but PATTERN is a final pathname component,
1460 and matches are searched for in DIRECTORY.
1461 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1462 The GLOB_APPEND flag is assumed to be set (always appends). */
1464 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1465 int (*errfunc
) (const char *, int),
1466 glob_t
*pglob
, size_t alloca_used
)
1468 size_t dirlen
= strlen (directory
);
1469 void *stream
= NULL
;
1472 struct globnames
*next
;
1476 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1477 struct globnames init_names
;
1478 struct globnames
*names
= &init_names
;
1479 struct globnames
*names_alloca
= &init_names
;
1485 alloca_used
+= sizeof (init_names
);
1487 init_names
.next
= NULL
;
1488 init_names
.count
= INITIAL_COUNT
;
1490 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1491 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1493 /* We need not do any tests. The PATTERN contains no meta
1494 characters and we must not return an error therefore the
1495 result will always contain exactly one name. */
1496 flags
|= GLOB_NOCHECK
;
1500 /* Since we use the normal file functions we can also use stat()
1501 to verify the file is there. */
1507 size_t patlen
= strlen (pattern
);
1508 int alloca_fullname
= __libc_use_alloca (alloca_used
1509 + dirlen
+ 1 + patlen
+ 1);
1511 if (alloca_fullname
)
1512 fullname
= alloca_account (dirlen
+ 1 + patlen
+ 1, alloca_used
);
1515 fullname
= malloc (dirlen
+ 1 + patlen
+ 1);
1516 if (fullname
== NULL
)
1517 return GLOB_NOSPACE
;
1520 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1522 pattern
, patlen
+ 1);
1523 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1524 ? (*pglob
->gl_stat
) (fullname
, &ust
.st
)
1525 : __stat64 (fullname
, &ust
.st64
)) == 0)
1526 /* We found this file to be existing. Now tell the rest
1527 of the function to copy this name into the result. */
1528 flags
|= GLOB_NOCHECK
;
1530 if (__builtin_expect (!alloca_fullname
, 0))
1535 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1536 ? (*pglob
->gl_opendir
) (directory
)
1537 : opendir (directory
));
1540 if (errno
!= ENOTDIR
1541 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1542 || (flags
& GLOB_ERR
)))
1543 return GLOB_ABORTED
;
1548 int dfd
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1549 ? -1 : dirfd ((DIR *) stream
));
1551 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1552 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1553 #if defined _AMIGA || defined VMS
1557 flags
|= GLOB_MAGCHAR
;
1563 #if defined _LIBC && !defined COMPILE_GLOB64
1567 struct dirent64 d64
;
1568 char room
[offsetof (struct dirent64
, d_name
[0])
1573 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1575 struct dirent
*d32
= (*pglob
->gl_readdir
) (stream
);
1578 CONVERT_DIRENT_DIRENT64 (&d64buf
.d64
, d32
);
1585 d
= __readdir64 (stream
);
1587 struct dirent
*d
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1588 ? ((struct dirent
*)
1589 (*pglob
->gl_readdir
) (stream
))
1590 : __readdir (stream
));
1594 if (! REAL_DIR_ENTRY (d
))
1597 /* If we shall match only directories use the information
1598 provided by the dirent call if possible. */
1599 if ((flags
& GLOB_ONLYDIR
) && !DIRENT_MIGHT_BE_DIR (d
))
1604 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1606 /* If the file we found is a symlink we have to
1607 make sure the target file exists. */
1608 if (!DIRENT_MIGHT_BE_SYMLINK (d
)
1609 || link_exists_p (dfd
, directory
, dirlen
, name
, pglob
,
1612 if (cur
== names
->count
)
1614 struct globnames
*newnames
;
1615 size_t count
= names
->count
* 2;
1616 size_t size
= (sizeof (struct globnames
)
1617 + ((count
- INITIAL_COUNT
)
1618 * sizeof (char *)));
1619 if (__libc_use_alloca (alloca_used
+ size
))
1620 newnames
= names_alloca
1621 = alloca_account (size
, alloca_used
);
1622 else if ((newnames
= malloc (size
))
1625 newnames
->count
= count
;
1626 newnames
->next
= names
;
1631 names
->name
[cur
] = (char *) malloc (len
+ 1);
1632 if (names
->name
[cur
] == NULL
)
1634 *((char *) mempcpy (names
->name
[cur
++], name
, len
))
1643 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1645 size_t len
= strlen (pattern
);
1647 names
->name
[cur
] = (char *) malloc (len
+ 1);
1648 if (names
->name
[cur
] == NULL
)
1650 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1653 int result
= GLOB_NOMATCH
;
1658 if (pglob
->gl_pathc
> UINTPTR_MAX
- pglob
->gl_offs
1659 || pglob
->gl_pathc
+ pglob
->gl_offs
> UINTPTR_MAX
- nfound
1660 || pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
> UINTPTR_MAX
- 1
1661 || (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1
1662 > UINTPTR_MAX
/ sizeof (char *)))
1665 char **new_gl_pathv
;
1667 = (char **) realloc (pglob
->gl_pathv
,
1668 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1670 if (new_gl_pathv
== NULL
)
1675 struct globnames
*old
= names
;
1676 for (size_t i
= 0; i
< cur
; ++i
)
1677 free (names
->name
[i
]);
1678 names
= names
->next
;
1679 /* NB: we will not leak memory here if we exit without
1680 freeing the current block assigned to OLD. At least
1681 the very first block is always allocated on the stack
1682 and this is the block assigned to OLD here. */
1685 assert (old
== &init_names
);
1689 if (old
== names_alloca
)
1690 names_alloca
= names
;
1694 result
= GLOB_NOSPACE
;
1700 struct globnames
*old
= names
;
1701 for (size_t i
= 0; i
< cur
; ++i
)
1702 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1704 names
= names
->next
;
1705 /* NB: we will not leak memory here if we exit without
1706 freeing the current block assigned to OLD. At least
1707 the very first block is always allocated on the stack
1708 and this is the block assigned to OLD here. */
1711 assert (old
== &init_names
);
1715 if (old
== names_alloca
)
1716 names_alloca
= names
;
1721 pglob
->gl_pathv
= new_gl_pathv
;
1723 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1725 pglob
->gl_flags
= flags
;
1732 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1733 (*pglob
->gl_closedir
) (stream
);