1 /* Copyright (C) 1991-2017 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, see
16 <http://www.gnu.org/licenses/>. */
25 #include <sys/types.h>
33 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
43 # define __set_errno(val) errno = (val)
53 # define strdup(str) __strdup (str)
54 # define sysconf(id) __sysconf (id)
55 # define closedir(dir) __closedir (dir)
56 # define opendir(name) __opendir (name)
57 # define readdir(str) __readdir64 (str)
58 # define getpwnam_r(name, bufp, buf, len, res) \
59 __getpwnam_r (name, bufp, buf, len, res)
61 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
63 # define struct_stat64 struct stat64
64 # define FLEXIBLE_ARRAY_MEMBER
66 # define __getlogin_r(buf, len) getlogin_r (buf, len)
67 # define __stat64(fname, buf) stat (fname, buf)
68 # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
69 # define struct_stat64 struct stat
71 # define __alloca alloca
73 # define __readdir readdir
74 # define COMPILE_GLOB64
79 #include <flexmember.h>
80 #include <glob_internal.h>
82 #ifdef _SC_GETPW_R_SIZE_MAX
83 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
85 # define GETPW_R_SIZE_MAX() (-1)
87 #ifdef _SC_LOGIN_NAME_MAX
88 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
90 # define GET_LOGIN_NAME_MAX() (-1)
93 static const char *next_brace_sub (const char *begin
, int flags
) __THROWNL
;
95 typedef uint_fast8_t dirent_type
;
97 #if !defined _LIBC && !defined HAVE_STRUCT_DIRENT_D_TYPE
98 /* Any distinct values will do here.
99 Undef any existing macros out of the way. */
103 # define DT_UNKNOWN 0
108 /* A representation of a directory entry which does not depend on the
109 layout of struct dirent, or the size of ino_t. */
110 struct readdir_result
113 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
116 #if defined _LIBC || defined D_INO_IN_DIRENT
121 /* Initialize and return type member of struct readdir_result. */
123 readdir_result_type (struct readdir_result d
)
125 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
126 # define D_TYPE_TO_RESULT(source) (source)->d_type,
129 # define D_TYPE_TO_RESULT(source)
134 /* Initialize and return skip_entry member of struct readdir_result. */
136 readdir_result_skip_entry (struct readdir_result d
)
138 /* Initializer for skip_entry. POSIX does not require that the d_ino
139 field be present, and some systems do not provide it. */
140 #if defined _LIBC || defined D_INO_IN_DIRENT
141 # define D_INO_TO_RESULT(source) (source)->d_ino == 0,
144 # define D_INO_TO_RESULT(source)
149 /* Construct an initializer for a struct readdir_result object from a
150 struct dirent *. No copy of the name is made. */
151 #define READDIR_RESULT_INITIALIZER(source) \
154 D_TYPE_TO_RESULT (source) \
155 D_INO_TO_RESULT (source) \
158 /* Call gl_readdir on STREAM. This macro can be overridden to reduce
159 type safety if an old interface version needs to be supported. */
161 # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream))
164 /* Extract name and type from directory entry. No copy of the name is
165 made. If SOURCE is NULL, result name is NULL. Keep in sync with
166 convert_dirent64 below. */
167 static struct readdir_result
168 convert_dirent (const struct dirent
*source
)
172 struct readdir_result result
= { NULL
, };
175 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
179 #ifndef COMPILE_GLOB64
180 /* Like convert_dirent, but works on struct dirent64 instead. Keep in
181 sync with convert_dirent above. */
182 static struct readdir_result
183 convert_dirent64 (const struct dirent64
*source
)
187 struct readdir_result result
= { NULL
, };
190 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
196 /* The results of opendir() in this file are not used with dirfd and fchdir,
197 and we do not leak fds to any single-threaded code that could use stdio,
198 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
199 FIXME - if the kernel ever adds support for multi-thread safety for
200 avoiding standard fds, then we should use opendir_safer. */
201 # ifdef GNULIB_defined_opendir
204 # ifdef GNULIB_defined_closedir
208 /* Just use malloc. */
209 # define __libc_use_alloca(n) false
210 # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0)
211 # define extend_alloca_account(buf, len, newlen, avar) \
212 ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0)
215 /* Set *R = A + B. Return true if the answer is mathematically
216 incorrect due to overflow; in this case, *R is the low order
217 bits of the correct answer. */
220 size_add_wrapv (size_t a
, size_t b
, size_t *r
)
222 #if 5 <= __GNUC__ && !defined __ICC
223 return __builtin_add_overflow (a
, b
, r
);
231 glob_use_alloca (size_t alloca_used
, size_t len
)
234 return (!size_add_wrapv (alloca_used
, len
, &size
)
235 && __libc_use_alloca (size
));
238 static int glob_in_dir (const char *pattern
, const char *directory
,
239 int flags
, int (*errfunc
) (const char *, int),
240 glob_t
*pglob
, size_t alloca_used
);
241 extern int __glob_pattern_type (const char *pattern
, int quote
)
244 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROWNL
;
245 static int collated_compare (const void *, const void *) __THROWNL
;
248 /* Find the end of the sub-pattern in a brace expression. */
250 next_brace_sub (const char *cp
, int flags
)
254 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
262 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
269 return *cp
!= '\0' ? cp
: NULL
;
273 /* Do glob searching for PATTERN, placing results in PGLOB.
274 The bits defined above may be set in FLAGS.
275 If a directory cannot be opened or read and ERRFUNC is not nil,
276 it is called with the pathname that caused the error, and the
277 'errno' value from the failing call; if it returns non-zero
278 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
279 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
280 Otherwise, 'glob' returns zero. */
282 #ifdef GLOB_ATTRIBUTE
285 glob (const char *pattern
, int flags
, int (*errfunc
) (const char *, int),
288 const char *filename
;
289 char *dirname
= NULL
;
294 int dirname_modified
;
295 int malloc_dirname
= 0;
298 size_t alloca_used
= 0;
300 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
302 __set_errno (EINVAL
);
306 /* POSIX requires all slashes to be matched. This means that with
307 a trailing slash we must match only directories. */
308 if (pattern
[0] && pattern
[strlen (pattern
) - 1] == '/')
309 flags
|= GLOB_ONLYDIR
;
311 if (!(flags
& GLOB_DOOFFS
))
312 /* Have to do this so 'globfree' knows where to start freeing. It
313 also makes all the code that uses gl_offs simpler. */
316 if (!(flags
& GLOB_APPEND
))
319 if (!(flags
& GLOB_DOOFFS
))
320 pglob
->gl_pathv
= NULL
;
325 if (pglob
->gl_offs
>= ~((size_t) 0) / sizeof (char *))
328 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
330 if (pglob
->gl_pathv
== NULL
)
333 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
334 pglob
->gl_pathv
[i
] = NULL
;
338 if (flags
& GLOB_BRACE
)
342 if (flags
& GLOB_NOESCAPE
)
343 begin
= strchr (pattern
, '{');
355 if (*begin
== '\\' && begin
[1] != '\0')
357 else if (*begin
== '{')
366 /* Allocate working buffer large enough for our work. Note that
367 we have at least an opening and closing brace. */
375 size_t pattern_len
= strlen (pattern
) - 1;
376 int alloca_onealt
= glob_use_alloca (alloca_used
, pattern_len
);
378 onealt
= alloca_account (pattern_len
, alloca_used
);
381 onealt
= malloc (pattern_len
);
386 /* We know the prefix for all sub-patterns. */
387 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
389 /* Find the first sub-pattern and at the same time find the
390 rest after the closing brace. */
391 next
= next_brace_sub (begin
+ 1, flags
);
394 /* It is an invalid expression. */
396 if (__glibc_unlikely (!alloca_onealt
))
398 flags
&= ~GLOB_BRACE
;
402 /* Now find the end of the whole brace expression. */
406 rest
= next_brace_sub (rest
+ 1, flags
);
408 /* It is an illegal expression. */
411 /* Please note that we now can be sure the brace expression
413 rest_len
= strlen (++rest
) + 1;
415 /* We have a brace expression. BEGIN points to the opening {,
416 NEXT points past the terminator of the first element, and END
417 points past the final }. We will accumulate result names from
418 recursive runs for each brace alternative in the buffer using
420 firstc
= pglob
->gl_pathc
;
427 /* Construct the new glob expression. */
428 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
430 result
= glob (onealt
,
431 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
432 | GLOB_APPEND
), errfunc
, pglob
);
434 /* If we got an error, return it. */
435 if (result
&& result
!= GLOB_NOMATCH
)
437 if (__glibc_unlikely (!alloca_onealt
))
439 if (!(flags
& GLOB_APPEND
))
448 /* We saw the last entry. */
452 next
= next_brace_sub (p
, flags
);
453 assert (next
!= NULL
);
456 if (__glibc_unlikely (!alloca_onealt
))
459 if (pglob
->gl_pathc
!= firstc
)
460 /* We found some entries. */
462 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
468 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
470 /* Find the filename. */
471 filename
= strrchr (pattern
, '/');
473 #if defined __MSDOS__ || defined WINDOWS32
474 /* The case of "d:pattern". Since ':' is not allowed in
475 file names, we can safely assume that wherever it
476 happens in pattern, it signals the filename part. This
477 is so we could some day support patterns like "[a-z]:foo". */
478 if (filename
== NULL
)
479 filename
= strchr (pattern
, ':');
480 #endif /* __MSDOS__ || WINDOWS32 */
482 dirname_modified
= 0;
483 if (filename
== NULL
)
485 /* This can mean two things: a simple name or "~name". The latter
486 case is nothing but a notation for a directory. */
487 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
489 dirname
= (char *) pattern
;
490 dirlen
= strlen (pattern
);
492 /* Set FILENAME to NULL as a special flag. This is ugly but
493 other solutions would require much more code. We test for
494 this special case below. */
499 if (__glibc_unlikely (pattern
[0] == '\0'))
501 dirs
.gl_pathv
= NULL
;
506 dirname
= (char *) ".";
510 else if (filename
== pattern
511 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
512 && (flags
& GLOB_NOESCAPE
) == 0))
514 /* "/pattern" or "\\/pattern". */
515 dirname
= (char *) "/";
522 dirlen
= filename
- pattern
;
523 #if defined __MSDOS__ || defined WINDOWS32
525 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
530 drive_spec
= __alloca (dirlen
+ 1);
531 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
532 /* For now, disallow wildcards in the drive spec, to
533 prevent infinite recursion in glob. */
534 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
536 /* If this is "d:pattern", we need to copy ':' to DIRNAME
537 as well. If it's "d:/pattern", don't remove the slash
538 from "d:/", since "d:" and "d:/" are not the same.*/
542 if (glob_use_alloca (alloca_used
, dirlen
+ 1))
543 newp
= alloca_account (dirlen
+ 1, alloca_used
);
546 newp
= malloc (dirlen
+ 1);
551 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
555 #if defined __MSDOS__ || defined WINDOWS32
556 bool drive_root
= (dirlen
> 1
557 && (dirname
[dirlen
- 1] == ':'
558 || (dirlen
> 2 && dirname
[dirlen
- 2] == ':'
559 && dirname
[dirlen
- 1] == '/')));
561 bool drive_root
= false;
564 if (filename
[0] == '\0' && dirlen
> 1 && !drive_root
)
565 /* "pattern/". Expand "pattern", appending slashes. */
567 int orig_flags
= flags
;
568 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
570 /* "pattern\\/". Remove the final backslash if it hasn't
572 char *p
= (char *) &dirname
[dirlen
- 1];
574 while (p
> dirname
&& p
[-1] == '\\') --p
;
575 if ((&dirname
[dirlen
] - p
) & 1)
577 *(char *) &dirname
[--dirlen
] = '\0';
578 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
581 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
583 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
584 | (flags
& GLOB_MARK
));
585 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
587 /* Make sure globfree (&dirs); is a nop. */
588 dirs
.gl_pathv
= NULL
;
590 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
598 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
600 if (dirname
[1] == '\0' || dirname
[1] == '/'
601 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
602 && (dirname
[2] == '\0' || dirname
[2] == '/')))
604 /* Look up home directory. */
605 char *home_dir
= getenv ("HOME");
606 int malloc_home_dir
= 0;
607 if (home_dir
== NULL
|| home_dir
[0] == '\0')
610 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give
611 preference to HOME, because the user can change HOME. */
612 const char *home_drive
= getenv ("HOMEDRIVE");
613 const char *home_path
= getenv ("HOMEPATH");
615 if (home_drive
!= NULL
&& home_path
!= NULL
)
617 size_t home_drive_len
= strlen (home_drive
);
618 size_t home_path_len
= strlen (home_path
);
619 char *mem
= alloca (home_drive_len
+ home_path_len
+ 1);
621 memcpy (mem
, home_drive
, home_drive_len
);
622 memcpy (mem
+ home_drive_len
, home_path
, home_path_len
+ 1);
626 home_dir
= "c:/users/default"; /* poor default */
631 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
634 /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try
637 if (glob_use_alloca (alloca_used
, buflen
))
638 name
= alloca_account (buflen
, alloca_used
);
641 name
= malloc (buflen
);
644 retval
= GLOB_NOSPACE
;
650 success
= __getlogin_r (name
, buflen
) == 0;
654 char *malloc_pwtmpbuf
= NULL
;
656 # if defined HAVE_GETPWNAM_R || defined _LIBC
657 long int pwbuflenmax
= GETPW_R_SIZE_MAX ();
658 size_t pwbuflen
= pwbuflenmax
;
663 if (! (0 < pwbuflenmax
&& pwbuflenmax
<= SIZE_MAX
))
664 /* 'sysconf' does not support _SC_GETPW_R_SIZE_MAX.
665 Try a moderate value. */
668 if (glob_use_alloca (alloca_used
, pwbuflen
))
669 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
672 pwtmpbuf
= malloc (pwbuflen
);
673 if (pwtmpbuf
== NULL
)
675 if (__glibc_unlikely (malloc_name
))
677 retval
= GLOB_NOSPACE
;
680 malloc_pwtmpbuf
= pwtmpbuf
;
683 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
693 v
= size_add_wrapv (pwbuflen
, pwbuflen
, &newlen
);
694 if (!v
&& malloc_pwtmpbuf
== NULL
695 && glob_use_alloca (alloca_used
, newlen
))
696 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
697 newlen
, alloca_used
);
700 char *newp
= (v
? NULL
701 : realloc (malloc_pwtmpbuf
, newlen
));
704 free (malloc_pwtmpbuf
);
705 if (__glibc_unlikely (malloc_name
))
707 retval
= GLOB_NOSPACE
;
710 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
718 if (__glibc_unlikely (malloc_name
))
722 if (malloc_pwtmpbuf
== NULL
)
723 home_dir
= p
->pw_dir
;
726 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
727 if (glob_use_alloca (alloca_used
, home_dir_len
))
728 home_dir
= alloca_account (home_dir_len
,
732 home_dir
= malloc (home_dir_len
);
733 if (home_dir
== NULL
)
736 retval
= GLOB_NOSPACE
;
741 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
744 free (malloc_pwtmpbuf
);
748 if (__glibc_unlikely (malloc_name
))
751 #endif /* WINDOWS32 */
753 if (home_dir
== NULL
|| home_dir
[0] == '\0')
755 if (__glibc_unlikely (malloc_home_dir
))
757 if (flags
& GLOB_TILDE_CHECK
)
759 retval
= GLOB_NOMATCH
;
764 home_dir
= (char *) "~"; /* No luck. */
768 /* Now construct the full directory. */
769 if (dirname
[1] == '\0')
771 if (__glibc_unlikely (malloc_dirname
))
775 dirlen
= strlen (dirname
);
776 malloc_dirname
= malloc_home_dir
;
781 size_t home_len
= strlen (home_dir
);
782 int use_alloca
= glob_use_alloca (alloca_used
, home_len
+ dirlen
);
784 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
787 newp
= malloc (home_len
+ dirlen
);
790 if (__glibc_unlikely (malloc_home_dir
))
792 retval
= GLOB_NOSPACE
;
797 mempcpy (mempcpy (newp
, home_dir
, home_len
),
798 &dirname
[1], dirlen
);
800 if (__glibc_unlikely (malloc_dirname
))
804 dirlen
+= home_len
- 1;
805 malloc_dirname
= !use_alloca
;
807 if (__glibc_unlikely (malloc_home_dir
))
810 dirname_modified
= 1;
815 char *end_name
= strchr (dirname
, '/');
817 int malloc_user_name
= 0;
818 char *unescape
= NULL
;
820 if (!(flags
& GLOB_NOESCAPE
))
822 if (end_name
== NULL
)
824 unescape
= strchr (dirname
, '\\');
826 end_name
= strchr (unescape
, '\0');
829 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
831 if (end_name
== NULL
)
832 user_name
= dirname
+ 1;
836 if (glob_use_alloca (alloca_used
, end_name
- dirname
))
837 newp
= alloca_account (end_name
- dirname
, alloca_used
);
840 newp
= malloc (end_name
- dirname
);
843 retval
= GLOB_NOSPACE
;
846 malloc_user_name
= 1;
848 if (unescape
!= NULL
)
850 char *p
= mempcpy (newp
, dirname
+ 1,
851 unescape
- dirname
- 1);
859 /* "~fo\\o\\" unescape to user_name "foo\\",
860 but "~fo\\o\\/" unescape to user_name
862 if (filename
== NULL
)
873 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
878 /* Look up specific user's home directory. */
881 char *malloc_pwtmpbuf
= NULL
;
882 # if defined HAVE_GETPWNAM_R || defined _LIBC
883 long int buflenmax
= GETPW_R_SIZE_MAX ();
884 size_t buflen
= buflenmax
;
890 if (! (0 <= buflenmax
&& buflenmax
<= SIZE_MAX
))
891 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
895 if (glob_use_alloca (alloca_used
, buflen
))
896 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
899 pwtmpbuf
= malloc (buflen
);
900 if (pwtmpbuf
== NULL
)
903 if (__glibc_unlikely (malloc_user_name
))
905 retval
= GLOB_NOSPACE
;
908 malloc_pwtmpbuf
= pwtmpbuf
;
911 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
920 v
= size_add_wrapv (buflen
, buflen
, &newlen
);
921 if (!v
&& malloc_pwtmpbuf
== NULL
922 && glob_use_alloca (alloca_used
, newlen
))
923 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
924 newlen
, alloca_used
);
927 char *newp
= v
? NULL
: realloc (malloc_pwtmpbuf
, newlen
);
930 free (malloc_pwtmpbuf
);
933 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
938 p
= getpwnam (user_name
);
941 if (__glibc_unlikely (malloc_user_name
))
944 /* If we found a home directory use this. */
947 size_t home_len
= strlen (p
->pw_dir
);
948 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
950 if (__glibc_unlikely (malloc_dirname
))
954 if (glob_use_alloca (alloca_used
, home_len
+ rest_len
+ 1))
955 dirname
= alloca_account (home_len
+ rest_len
+ 1,
959 dirname
= malloc (home_len
+ rest_len
+ 1);
962 free (malloc_pwtmpbuf
);
963 retval
= GLOB_NOSPACE
;
968 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
969 end_name
, rest_len
)) = '\0';
971 dirlen
= home_len
+ rest_len
;
972 dirname_modified
= 1;
974 free (malloc_pwtmpbuf
);
978 free (malloc_pwtmpbuf
);
980 if (flags
& GLOB_TILDE_CHECK
)
982 /* We have to regard it as an error if we cannot find the
984 retval
= GLOB_NOMATCH
;
989 #endif /* !WINDOWS32 */
993 /* Now test whether we looked for "~" or "~NAME". In this case we
994 can give the answer now. */
995 if (filename
== NULL
)
1000 /* Return the directory if we don't check for error or if it exists. */
1001 if ((flags
& GLOB_NOCHECK
)
1002 || (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1003 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
1004 && S_ISDIR (st
.st_mode
))
1005 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
1007 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1008 char **new_gl_pathv
;
1010 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1013 free (pglob
->gl_pathv
);
1014 pglob
->gl_pathv
= NULL
;
1015 pglob
->gl_pathc
= 0;
1016 retval
= GLOB_NOSPACE
;
1020 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1021 (newcount
+ 2) * sizeof (char *));
1022 if (new_gl_pathv
== NULL
)
1024 pglob
->gl_pathv
= new_gl_pathv
;
1026 if (flags
& GLOB_MARK
)
1029 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
1030 if (pglob
->gl_pathv
[newcount
] == NULL
)
1032 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
1035 if (__glibc_unlikely (malloc_dirname
))
1040 if (__glibc_unlikely (malloc_dirname
))
1041 pglob
->gl_pathv
[newcount
] = dirname
;
1044 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
1045 if (pglob
->gl_pathv
[newcount
] == NULL
)
1049 pglob
->gl_pathv
[++newcount
] = NULL
;
1051 pglob
->gl_flags
= flags
;
1057 retval
= GLOB_NOMATCH
;
1061 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1062 /* meta is 1 if correct glob pattern containing metacharacters.
1063 If meta has bit (1 << 2) set, it means there was an unterminated
1064 [ which we handle the same, using fnmatch. Broken unterminated
1065 pattern bracket expressions ought to be rare enough that it is
1066 not worth special casing them, fnmatch will do the right thing. */
1069 /* The directory name contains metacharacters, so we
1070 have to glob for the directory, and then glob for
1071 the pattern in each directory found. */
1074 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1076 /* "foo\\/bar". Remove the final backslash from dirname
1077 if it has not been quoted. */
1078 char *p
= (char *) &dirname
[dirlen
- 1];
1080 while (p
> dirname
&& p
[-1] == '\\') --p
;
1081 if ((&dirname
[dirlen
] - p
) & 1)
1082 *(char *) &dirname
[--dirlen
] = '\0';
1085 if (__glibc_unlikely ((flags
& GLOB_ALTDIRFUNC
) != 0))
1087 /* Use the alternative access functions also in the recursive
1089 dirs
.gl_opendir
= pglob
->gl_opendir
;
1090 dirs
.gl_readdir
= pglob
->gl_readdir
;
1091 dirs
.gl_closedir
= pglob
->gl_closedir
;
1092 dirs
.gl_stat
= pglob
->gl_stat
;
1093 dirs
.gl_lstat
= pglob
->gl_lstat
;
1096 status
= glob (dirname
,
1097 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1099 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1103 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1111 /* We have successfully globbed the preceding directory name.
1112 For each name we found, call glob_in_dir on it and FILENAME,
1113 appending the results to PGLOB. */
1114 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1118 old_pathc
= pglob
->gl_pathc
;
1119 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1120 ((flags
| GLOB_APPEND
)
1121 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1122 errfunc
, pglob
, alloca_used
);
1123 if (status
== GLOB_NOMATCH
)
1124 /* No matches in this directory. Try the next. */
1131 pglob
->gl_pathc
= 0;
1136 /* Stick the directory on the front of each name. */
1137 if (prefix_array (dirs
.gl_pathv
[i
],
1138 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1139 pglob
->gl_pathc
- old_pathc
))
1143 pglob
->gl_pathc
= 0;
1144 retval
= GLOB_NOSPACE
;
1149 flags
|= GLOB_MAGCHAR
;
1151 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
1152 But if we have not found any matching entry and the GLOB_NOCHECK
1153 flag was set we must return the input pattern itself. */
1154 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1158 if (flags
& GLOB_NOCHECK
)
1160 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1161 char **new_gl_pathv
;
1163 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1167 retval
= GLOB_NOSPACE
;
1171 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1172 (newcount
+ 2) * sizeof (char *));
1173 if (new_gl_pathv
== NULL
)
1175 pglob
->gl_pathv
= new_gl_pathv
;
1177 pglob
->gl_pathv
[newcount
] = strdup (pattern
);
1178 if (pglob
->gl_pathv
[newcount
] == NULL
)
1182 pglob
->gl_pathc
= 0;
1183 retval
= GLOB_NOSPACE
;
1190 pglob
->gl_pathv
[newcount
] = NULL
;
1191 pglob
->gl_flags
= flags
;
1196 retval
= GLOB_NOMATCH
;
1205 size_t old_pathc
= pglob
->gl_pathc
;
1206 int orig_flags
= flags
;
1210 char *p
= strchr (dirname
, '\\'), *q
;
1211 /* We need to unescape the dirname string. It is certainly
1212 allocated by alloca, as otherwise filename would be NULL
1213 or dirname wouldn't contain backslashes. */
1226 while (*p
++ != '\0');
1227 dirname_modified
= 1;
1229 if (dirname_modified
)
1230 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1231 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1235 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1236 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1238 /* Make sure globfree (&dirs); is a nop. */
1239 dirs
.gl_pathv
= NULL
;
1249 /* Stick the directory on the front of each name. */
1250 if (prefix_array (dirname
,
1251 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1252 pglob
->gl_pathc
- old_pathc
))
1255 pglob
->gl_pathc
= 0;
1256 retval
= GLOB_NOSPACE
;
1262 if (flags
& GLOB_MARK
)
1264 /* Append slashes to directory names. */
1269 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1270 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1271 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1272 && S_ISDIR (st
.st_mode
))
1273 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1274 && S_ISDIR (st64
.st_mode
))))
1276 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1277 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1281 pglob
->gl_pathc
= 0;
1282 retval
= GLOB_NOSPACE
;
1285 strcpy (&new[len
- 2], "/");
1286 pglob
->gl_pathv
[i
] = new;
1290 if (!(flags
& GLOB_NOSORT
))
1292 /* Sort the vector. */
1293 qsort (&pglob
->gl_pathv
[oldcount
],
1294 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1295 sizeof (char *), collated_compare
);
1299 if (__glibc_unlikely (malloc_dirname
))
1304 #if defined _LIBC && !defined glob
1305 libc_hidden_def (glob
)
1309 /* Do a collated comparison of A and B. */
1311 collated_compare (const void *a
, const void *b
)
1313 char *const *ps1
= a
; char *s1
= *ps1
;
1314 char *const *ps2
= b
; char *s2
= *ps2
;
1322 return strcoll (s1
, s2
);
1326 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1327 elements in place. Return nonzero if out of memory, zero if successful.
1328 A slash is inserted between DIRNAME and each elt of ARRAY,
1329 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1331 prefix_array (const char *dirname
, char **array
, size_t n
)
1334 size_t dirlen
= strlen (dirname
);
1335 char dirsep_char
= '/';
1337 if (dirlen
== 1 && dirname
[0] == '/')
1338 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1339 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1342 #if defined __MSDOS__ || defined WINDOWS32
1345 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1346 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1348 else if (dirname
[dirlen
- 1] == ':')
1350 /* DIRNAME is "d:". Use ':' instead of '/'. */
1357 for (i
= 0; i
< n
; ++i
)
1359 size_t eltlen
= strlen (array
[i
]) + 1;
1360 char *new = malloc (dirlen
+ 1 + eltlen
);
1369 char *endp
= mempcpy (new, dirname
, dirlen
);
1370 *endp
++ = dirsep_char
;
1371 mempcpy (endp
, array
[i
], eltlen
);
1380 /* We put this in a separate function mainly to allow the memory
1381 allocated with alloca to be recycled. */
1383 __attribute_noinline__
1384 link_stat (const char *dir
, size_t dirlen
, const char *fname
,
1386 # if !defined _LIBC && !HAVE_FSTATAT
1391 size_t fnamelen
= strlen (fname
);
1392 char *fullname
= __alloca (dirlen
+ 1 + fnamelen
+ 1);
1395 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1396 fname
, fnamelen
+ 1);
1398 # if !defined _LIBC && !HAVE_FSTATAT
1399 if (__builtin_expect ((flags
& GLOB_ALTDIRFUNC
) == 0, 1))
1402 return __stat64 (fullname
, &st64
);
1405 return (*pglob
->gl_stat
) (fullname
, &st
);
1408 /* Return true if DIR/FNAME exists. */
1410 link_exists_p (int dfd
, const char *dir
, size_t dirlen
, const char *fname
,
1411 glob_t
*pglob
, int flags
)
1414 # if defined _LIBC || HAVE_FSTATAT
1415 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1416 status
= link_stat (dir
, dirlen
, fname
, pglob
);
1419 /* dfd cannot be -1 here, because dirfd never returns -1 on
1420 glibc, or on hosts that have fstatat. */
1422 status
= __fxstatat64 (_STAT_VER
, dfd
, fname
, &st64
, 0);
1425 status
= link_stat (dir
, dirlen
, fname
, pglob
, flags
);
1427 return status
== 0 || errno
== EOVERFLOW
;
1430 /* Like 'glob', but PATTERN is a final pathname component,
1431 and matches are searched for in DIRECTORY.
1432 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1433 The GLOB_APPEND flag is assumed to be set (always appends). */
1435 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1436 int (*errfunc
) (const char *, int),
1437 glob_t
*pglob
, size_t alloca_used
)
1439 size_t dirlen
= strlen (directory
);
1440 void *stream
= NULL
;
1441 # define GLOBNAMES_MEMBERS(nnames) \
1442 struct globnames *next; size_t count; char *name[nnames];
1443 struct globnames
{ GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER
) };
1444 struct { GLOBNAMES_MEMBERS (64) } init_names_buf
;
1445 struct globnames
*init_names
= (struct globnames
*) &init_names_buf
;
1446 struct globnames
*names
= init_names
;
1447 struct globnames
*names_alloca
= init_names
;
1454 alloca_used
+= sizeof init_names_buf
;
1456 init_names
->next
= NULL
;
1457 init_names
->count
= ((sizeof init_names_buf
1458 - offsetof (struct globnames
, name
))
1459 / sizeof init_names
->name
[0]);
1461 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1462 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1464 /* We need not do any tests. The PATTERN contains no meta
1465 characters and we must not return an error therefore the
1466 result will always contain exactly one name. */
1467 flags
|= GLOB_NOCHECK
;
1471 /* Since we use the normal file functions we can also use stat()
1472 to verify the file is there. */
1478 size_t patlen
= strlen (pattern
);
1480 bool alloca_fullname
1481 = (! size_add_wrapv (dirlen
+ 1, patlen
+ 1, &fullsize
)
1482 && glob_use_alloca (alloca_used
, fullsize
));
1484 if (alloca_fullname
)
1485 fullname
= alloca_account (fullsize
, alloca_used
);
1488 fullname
= malloc (fullsize
);
1489 if (fullname
== NULL
)
1490 return GLOB_NOSPACE
;
1493 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1495 pattern
, patlen
+ 1);
1496 if (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1497 ? (*pglob
->gl_stat
) (fullname
, &ust
.st
)
1498 : __stat64 (fullname
, &ust
.st64
))
1500 || errno
== EOVERFLOW
)
1501 /* We found this file to be existing. Now tell the rest
1502 of the function to copy this name into the result. */
1503 flags
|= GLOB_NOCHECK
;
1505 if (__glibc_unlikely (!alloca_fullname
))
1510 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1511 ? (*pglob
->gl_opendir
) (directory
)
1512 : opendir (directory
));
1515 if (errno
!= ENOTDIR
1516 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1517 || (flags
& GLOB_ERR
)))
1518 return GLOB_ABORTED
;
1522 int dfd
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1523 ? -1 : dirfd ((DIR *) stream
));
1524 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1525 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0));
1526 flags
|= GLOB_MAGCHAR
;
1530 struct readdir_result d
;
1532 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1533 d
= convert_dirent (GL_READDIR (pglob
, stream
));
1536 #ifdef COMPILE_GLOB64
1537 d
= convert_dirent (__readdir (stream
));
1539 d
= convert_dirent64 (__readdir64 (stream
));
1545 if (readdir_result_skip_entry (d
))
1548 /* If we shall match only directories use the information
1549 provided by the dirent call if possible. */
1550 if (flags
& GLOB_ONLYDIR
)
1551 switch (readdir_result_type (d
))
1553 case DT_DIR
: case DT_LNK
: case DT_UNKNOWN
: break;
1557 if (fnmatch (pattern
, d
.name
, fnm_flags
) == 0)
1559 /* If the file we found is a symlink we have to
1560 make sure the target file exists. */
1561 dirent_type type
= readdir_result_type (d
);
1562 if (! (type
== DT_LNK
|| type
== DT_UNKNOWN
)
1563 || link_exists_p (dfd
, directory
, dirlen
, d
.name
,
1566 if (cur
== names
->count
)
1568 struct globnames
*newnames
;
1569 size_t count
= names
->count
* 2;
1570 size_t nameoff
= offsetof (struct globnames
, name
);
1571 size_t size
= FLEXSIZEOF (struct globnames
, name
,
1572 count
* sizeof (char *));
1573 if ((SIZE_MAX
- nameoff
) / 2 / sizeof (char *)
1576 if (glob_use_alloca (alloca_used
, size
))
1577 newnames
= names_alloca
1578 = alloca_account (size
, alloca_used
);
1579 else if ((newnames
= malloc (size
))
1582 newnames
->count
= count
;
1583 newnames
->next
= names
;
1587 names
->name
[cur
] = strdup (d
.name
);
1588 if (names
->name
[cur
] == NULL
)
1592 if (SIZE_MAX
- pglob
->gl_offs
<= nfound
)
1600 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1602 size_t len
= strlen (pattern
);
1604 names
->name
[cur
] = malloc (len
+ 1);
1605 if (names
->name
[cur
] == NULL
)
1607 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1610 result
= GLOB_NOMATCH
;
1613 char **new_gl_pathv
;
1616 if (SIZE_MAX
/ sizeof (char *) - pglob
->gl_pathc
1617 < pglob
->gl_offs
+ nfound
+ 1)
1621 = realloc (pglob
->gl_pathv
,
1622 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1625 if (new_gl_pathv
== NULL
)
1630 struct globnames
*old
= names
;
1631 for (size_t i
= 0; i
< cur
; ++i
)
1632 free (names
->name
[i
]);
1633 names
= names
->next
;
1634 /* NB: we will not leak memory here if we exit without
1635 freeing the current block assigned to OLD. At least
1636 the very first block is always allocated on the stack
1637 and this is the block assigned to OLD here. */
1640 assert (old
== init_names
);
1644 if (old
== names_alloca
)
1645 names_alloca
= names
;
1649 result
= GLOB_NOSPACE
;
1655 struct globnames
*old
= names
;
1656 for (size_t i
= 0; i
< cur
; ++i
)
1657 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1659 names
= names
->next
;
1660 /* NB: we will not leak memory here if we exit without
1661 freeing the current block assigned to OLD. At least
1662 the very first block is always allocated on the stack
1663 and this is the block assigned to OLD here. */
1666 assert (old
== init_names
);
1670 if (old
== names_alloca
)
1671 names_alloca
= names
;
1676 pglob
->gl_pathv
= new_gl_pathv
;
1678 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1680 pglob
->gl_flags
= flags
;
1687 if (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
))
1688 (*pglob
->gl_closedir
) (stream
);