]>
git.ipfire.org Git - thirdparty/glibc.git/blob - posix/glob.c
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 __lstat64(fname, buf) __lxstat64 (_STAT_VER, fname, buf)
64 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
66 # define struct_stat64 struct stat64
67 # define FLEXIBLE_ARRAY_MEMBER
69 # define __getlogin_r(buf, len) getlogin_r (buf, len)
70 # define __lstat64(fname, buf) lstat (fname, buf)
71 # define __stat64(fname, buf) stat (fname, buf)
72 # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
73 # define struct_stat64 struct stat
75 # define __alloca alloca
77 # define __readdir readdir
78 # define COMPILE_GLOB64
83 #include <flexmember.h>
84 #include <glob_internal.h>
86 #ifdef _SC_GETPW_R_SIZE_MAX
87 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
89 # define GETPW_R_SIZE_MAX() (-1)
91 #ifdef _SC_LOGIN_NAME_MAX
92 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
94 # define GET_LOGIN_NAME_MAX() (-1)
97 static const char *next_brace_sub (const char *begin
, int flags
) __THROWNL
;
99 typedef uint_fast8_t dirent_type
;
101 #if !defined _LIBC && !defined HAVE_STRUCT_DIRENT_D_TYPE
102 /* Any distinct values will do here.
103 Undef any existing macros out of the way. */
107 # define DT_UNKNOWN 0
112 /* A representation of a directory entry which does not depend on the
113 layout of struct dirent, or the size of ino_t. */
114 struct readdir_result
117 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
122 /* Initialize and return type member of struct readdir_result. */
124 readdir_result_type (struct readdir_result d
)
126 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
127 # define D_TYPE_TO_RESULT(source) (source)->d_type,
130 # define D_TYPE_TO_RESULT(source)
135 /* Construct an initializer for a struct readdir_result object from a
136 struct dirent *. No copy of the name is made. */
137 #define READDIR_RESULT_INITIALIZER(source) \
140 D_TYPE_TO_RESULT (source) \
143 /* Call gl_readdir on STREAM. This macro can be overridden to reduce
144 type safety if an old interface version needs to be supported. */
146 # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream))
149 /* Extract name and type from directory entry. No copy of the name is
150 made. If SOURCE is NULL, result name is NULL. Keep in sync with
151 convert_dirent64 below. */
152 static struct readdir_result
153 convert_dirent (const struct dirent
*source
)
157 struct readdir_result result
= { NULL
, };
160 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
164 #ifndef COMPILE_GLOB64
165 /* Like convert_dirent, but works on struct dirent64 instead. Keep in
166 sync with convert_dirent above. */
167 static struct readdir_result
168 convert_dirent64 (const struct dirent64
*source
)
172 struct readdir_result result
= { NULL
, };
175 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
181 /* The results of opendir() in this file are not used with dirfd and fchdir,
182 and we do not leak fds to any single-threaded code that could use stdio,
183 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
184 FIXME - if the kernel ever adds support for multi-thread safety for
185 avoiding standard fds, then we should use opendir_safer. */
186 # ifdef GNULIB_defined_opendir
189 # ifdef GNULIB_defined_closedir
193 /* Just use malloc. */
194 # define __libc_use_alloca(n) false
195 # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0)
196 # define extend_alloca_account(buf, len, newlen, avar) \
197 ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0)
200 /* Set *R = A + B. Return true if the answer is mathematically
201 incorrect due to overflow; in this case, *R is the low order
202 bits of the correct answer. */
205 size_add_wrapv (size_t a
, size_t b
, size_t *r
)
207 #if 5 <= __GNUC__ && !defined __ICC
208 return __builtin_add_overflow (a
, b
, r
);
216 glob_use_alloca (size_t alloca_used
, size_t len
)
219 return (!size_add_wrapv (alloca_used
, len
, &size
)
220 && __libc_use_alloca (size
));
223 static int glob_in_dir (const char *pattern
, const char *directory
,
224 int flags
, int (*errfunc
) (const char *, int),
225 glob_t
*pglob
, size_t alloca_used
);
226 extern int __glob_pattern_type (const char *pattern
, int quote
)
229 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROWNL
;
230 static int collated_compare (const void *, const void *) __THROWNL
;
233 /* Return true if FILENAME is a directory or a symbolic link to a directory.
234 Use FLAGS and PGLOB to resolve the filename. */
236 is_dir (char const *filename
, int flags
, glob_t
const *pglob
)
240 return (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
)
241 ? pglob
->gl_stat (filename
, &st
) == 0 && S_ISDIR (st
.st_mode
)
242 : __stat64 (filename
, &st64
) == 0 && S_ISDIR (st64
.st_mode
));
245 /* Find the end of the sub-pattern in a brace expression. */
247 next_brace_sub (const char *cp
, int flags
)
251 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
259 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
266 return *cp
!= '\0' ? cp
: NULL
;
270 /* Do glob searching for PATTERN, placing results in PGLOB.
271 The bits defined above may be set in FLAGS.
272 If a directory cannot be opened or read and ERRFUNC is not nil,
273 it is called with the pathname that caused the error, and the
274 'errno' value from the failing call; if it returns non-zero
275 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
276 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
277 Otherwise, 'glob' returns zero. */
279 #ifdef GLOB_ATTRIBUTE
282 glob (const char *pattern
, int flags
, int (*errfunc
) (const char *, int),
285 const char *filename
;
286 char *dirname
= NULL
;
291 int dirname_modified
;
292 int malloc_dirname
= 0;
295 size_t alloca_used
= 0;
297 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
299 __set_errno (EINVAL
);
303 /* POSIX requires all slashes to be matched. This means that with
304 a trailing slash we must match only directories. */
305 if (pattern
[0] && pattern
[strlen (pattern
) - 1] == '/')
306 flags
|= GLOB_ONLYDIR
;
308 if (!(flags
& GLOB_DOOFFS
))
309 /* Have to do this so 'globfree' knows where to start freeing. It
310 also makes all the code that uses gl_offs simpler. */
313 if (!(flags
& GLOB_APPEND
))
316 if (!(flags
& GLOB_DOOFFS
))
317 pglob
->gl_pathv
= NULL
;
322 if (pglob
->gl_offs
>= ~((size_t) 0) / sizeof (char *))
325 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
327 if (pglob
->gl_pathv
== NULL
)
330 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
331 pglob
->gl_pathv
[i
] = NULL
;
335 if (flags
& GLOB_BRACE
)
339 if (flags
& GLOB_NOESCAPE
)
340 begin
= strchr (pattern
, '{');
352 if (*begin
== '\\' && begin
[1] != '\0')
354 else if (*begin
== '{')
363 /* Allocate working buffer large enough for our work. Note that
364 we have at least an opening and closing brace. */
372 size_t pattern_len
= strlen (pattern
) - 1;
373 int alloca_onealt
= glob_use_alloca (alloca_used
, pattern_len
);
375 onealt
= alloca_account (pattern_len
, alloca_used
);
378 onealt
= malloc (pattern_len
);
383 /* We know the prefix for all sub-patterns. */
384 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
386 /* Find the first sub-pattern and at the same time find the
387 rest after the closing brace. */
388 next
= next_brace_sub (begin
+ 1, flags
);
391 /* It is an invalid expression. */
393 if (__glibc_unlikely (!alloca_onealt
))
395 flags
&= ~GLOB_BRACE
;
399 /* Now find the end of the whole brace expression. */
403 rest
= next_brace_sub (rest
+ 1, flags
);
405 /* It is an illegal expression. */
408 /* Please note that we now can be sure the brace expression
410 rest_len
= strlen (++rest
) + 1;
412 /* We have a brace expression. BEGIN points to the opening {,
413 NEXT points past the terminator of the first element, and END
414 points past the final }. We will accumulate result names from
415 recursive runs for each brace alternative in the buffer using
417 firstc
= pglob
->gl_pathc
;
424 /* Construct the new glob expression. */
425 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
427 result
= glob (onealt
,
428 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
429 | GLOB_APPEND
), errfunc
, pglob
);
431 /* If we got an error, return it. */
432 if (result
&& result
!= GLOB_NOMATCH
)
434 if (__glibc_unlikely (!alloca_onealt
))
436 if (!(flags
& GLOB_APPEND
))
445 /* We saw the last entry. */
449 next
= next_brace_sub (p
, flags
);
450 assert (next
!= NULL
);
453 if (__glibc_unlikely (!alloca_onealt
))
456 if (pglob
->gl_pathc
!= firstc
)
457 /* We found some entries. */
459 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
465 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
467 /* Find the filename. */
468 filename
= strrchr (pattern
, '/');
470 #if defined __MSDOS__ || defined WINDOWS32
471 /* The case of "d:pattern". Since ':' is not allowed in
472 file names, we can safely assume that wherever it
473 happens in pattern, it signals the filename part. This
474 is so we could some day support patterns like "[a-z]:foo". */
475 if (filename
== NULL
)
476 filename
= strchr (pattern
, ':');
477 #endif /* __MSDOS__ || WINDOWS32 */
479 dirname_modified
= 0;
480 if (filename
== NULL
)
482 /* This can mean two things: a simple name or "~name". The latter
483 case is nothing but a notation for a directory. */
484 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
486 dirname
= (char *) pattern
;
487 dirlen
= strlen (pattern
);
489 /* Set FILENAME to NULL as a special flag. This is ugly but
490 other solutions would require much more code. We test for
491 this special case below. */
496 if (__glibc_unlikely (pattern
[0] == '\0'))
498 dirs
.gl_pathv
= NULL
;
503 dirname
= (char *) ".";
507 else if (filename
== pattern
508 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
509 && (flags
& GLOB_NOESCAPE
) == 0))
511 /* "/pattern" or "\\/pattern". */
512 dirname
= (char *) "/";
519 dirlen
= filename
- pattern
;
520 #if defined __MSDOS__ || defined WINDOWS32
522 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
527 drive_spec
= __alloca (dirlen
+ 1);
528 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
529 /* For now, disallow wildcards in the drive spec, to
530 prevent infinite recursion in glob. */
531 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
533 /* If this is "d:pattern", we need to copy ':' to DIRNAME
534 as well. If it's "d:/pattern", don't remove the slash
535 from "d:/", since "d:" and "d:/" are not the same.*/
539 if (glob_use_alloca (alloca_used
, dirlen
+ 1))
540 newp
= alloca_account (dirlen
+ 1, alloca_used
);
543 newp
= malloc (dirlen
+ 1);
548 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
552 #if defined __MSDOS__ || defined WINDOWS32
553 bool drive_root
= (dirlen
> 1
554 && (dirname
[dirlen
- 1] == ':'
555 || (dirlen
> 2 && dirname
[dirlen
- 2] == ':'
556 && dirname
[dirlen
- 1] == '/')));
558 bool drive_root
= false;
561 if (filename
[0] == '\0' && dirlen
> 1 && !drive_root
)
562 /* "pattern/". Expand "pattern", appending slashes. */
564 int orig_flags
= flags
;
565 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
567 /* "pattern\\/". Remove the final backslash if it hasn't
569 char *p
= (char *) &dirname
[dirlen
- 1];
571 while (p
> dirname
&& p
[-1] == '\\') --p
;
572 if ((&dirname
[dirlen
] - p
) & 1)
574 *(char *) &dirname
[--dirlen
] = '\0';
575 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
578 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
580 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
581 | (flags
& GLOB_MARK
));
582 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
584 /* Make sure globfree (&dirs); is a nop. */
585 dirs
.gl_pathv
= NULL
;
587 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
595 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
597 if (dirname
[1] == '\0' || dirname
[1] == '/'
598 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
599 && (dirname
[2] == '\0' || dirname
[2] == '/')))
601 /* Look up home directory. */
602 char *home_dir
= getenv ("HOME");
603 int malloc_home_dir
= 0;
604 if (home_dir
== NULL
|| home_dir
[0] == '\0')
607 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give
608 preference to HOME, because the user can change HOME. */
609 const char *home_drive
= getenv ("HOMEDRIVE");
610 const char *home_path
= getenv ("HOMEPATH");
612 if (home_drive
!= NULL
&& home_path
!= NULL
)
614 size_t home_drive_len
= strlen (home_drive
);
615 size_t home_path_len
= strlen (home_path
);
616 char *mem
= alloca (home_drive_len
+ home_path_len
+ 1);
618 memcpy (mem
, home_drive
, home_drive_len
);
619 memcpy (mem
+ home_drive_len
, home_path
, home_path_len
+ 1);
623 home_dir
= "c:/users/default"; /* poor default */
628 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
631 /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try
634 if (glob_use_alloca (alloca_used
, buflen
))
635 name
= alloca_account (buflen
, alloca_used
);
638 name
= malloc (buflen
);
641 retval
= GLOB_NOSPACE
;
647 success
= __getlogin_r (name
, buflen
) == 0;
651 char *malloc_pwtmpbuf
= NULL
;
653 # if defined HAVE_GETPWNAM_R || defined _LIBC
654 long int pwbuflenmax
= GETPW_R_SIZE_MAX ();
655 size_t pwbuflen
= pwbuflenmax
;
660 if (! (0 < pwbuflenmax
&& pwbuflenmax
<= SIZE_MAX
))
661 /* 'sysconf' does not support _SC_GETPW_R_SIZE_MAX.
662 Try a moderate value. */
665 if (glob_use_alloca (alloca_used
, pwbuflen
))
666 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
669 pwtmpbuf
= malloc (pwbuflen
);
670 if (pwtmpbuf
== NULL
)
672 if (__glibc_unlikely (malloc_name
))
674 retval
= GLOB_NOSPACE
;
677 malloc_pwtmpbuf
= pwtmpbuf
;
680 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
690 v
= size_add_wrapv (pwbuflen
, pwbuflen
, &newlen
);
691 if (!v
&& malloc_pwtmpbuf
== NULL
692 && glob_use_alloca (alloca_used
, newlen
))
693 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
694 newlen
, alloca_used
);
697 char *newp
= (v
? NULL
698 : realloc (malloc_pwtmpbuf
, newlen
));
701 free (malloc_pwtmpbuf
);
702 if (__glibc_unlikely (malloc_name
))
704 retval
= GLOB_NOSPACE
;
707 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
715 if (__glibc_unlikely (malloc_name
))
719 if (malloc_pwtmpbuf
== NULL
)
720 home_dir
= p
->pw_dir
;
723 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
724 if (glob_use_alloca (alloca_used
, home_dir_len
))
725 home_dir
= alloca_account (home_dir_len
,
729 home_dir
= malloc (home_dir_len
);
730 if (home_dir
== NULL
)
733 retval
= GLOB_NOSPACE
;
738 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
741 free (malloc_pwtmpbuf
);
745 if (__glibc_unlikely (malloc_name
))
748 #endif /* WINDOWS32 */
750 if (home_dir
== NULL
|| home_dir
[0] == '\0')
752 if (__glibc_unlikely (malloc_home_dir
))
754 if (flags
& GLOB_TILDE_CHECK
)
756 retval
= GLOB_NOMATCH
;
761 home_dir
= (char *) "~"; /* No luck. */
765 /* Now construct the full directory. */
766 if (dirname
[1] == '\0')
768 if (__glibc_unlikely (malloc_dirname
))
772 dirlen
= strlen (dirname
);
773 malloc_dirname
= malloc_home_dir
;
778 size_t home_len
= strlen (home_dir
);
779 int use_alloca
= glob_use_alloca (alloca_used
, home_len
+ dirlen
);
781 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
784 newp
= malloc (home_len
+ dirlen
);
787 if (__glibc_unlikely (malloc_home_dir
))
789 retval
= GLOB_NOSPACE
;
794 mempcpy (mempcpy (newp
, home_dir
, home_len
),
795 &dirname
[1], dirlen
);
797 if (__glibc_unlikely (malloc_dirname
))
801 dirlen
+= home_len
- 1;
802 malloc_dirname
= !use_alloca
;
804 if (__glibc_unlikely (malloc_home_dir
))
807 dirname_modified
= 1;
812 char *end_name
= strchr (dirname
, '/');
814 int malloc_user_name
= 0;
815 char *unescape
= NULL
;
817 if (!(flags
& GLOB_NOESCAPE
))
819 if (end_name
== NULL
)
821 unescape
= strchr (dirname
, '\\');
823 end_name
= strchr (unescape
, '\0');
826 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
828 if (end_name
== NULL
)
829 user_name
= dirname
+ 1;
833 if (glob_use_alloca (alloca_used
, end_name
- dirname
))
834 newp
= alloca_account (end_name
- dirname
, alloca_used
);
837 newp
= malloc (end_name
- dirname
);
840 retval
= GLOB_NOSPACE
;
843 malloc_user_name
= 1;
845 if (unescape
!= NULL
)
847 char *p
= mempcpy (newp
, dirname
+ 1,
848 unescape
- dirname
- 1);
856 /* "~fo\\o\\" unescape to user_name "foo\\",
857 but "~fo\\o\\/" unescape to user_name
859 if (filename
== NULL
)
870 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
875 /* Look up specific user's home directory. */
878 char *malloc_pwtmpbuf
= NULL
;
879 # if defined HAVE_GETPWNAM_R || defined _LIBC
880 long int buflenmax
= GETPW_R_SIZE_MAX ();
881 size_t buflen
= buflenmax
;
887 if (! (0 <= buflenmax
&& buflenmax
<= SIZE_MAX
))
888 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
892 if (glob_use_alloca (alloca_used
, buflen
))
893 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
896 pwtmpbuf
= malloc (buflen
);
897 if (pwtmpbuf
== NULL
)
900 if (__glibc_unlikely (malloc_user_name
))
902 retval
= GLOB_NOSPACE
;
905 malloc_pwtmpbuf
= pwtmpbuf
;
908 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
917 v
= size_add_wrapv (buflen
, buflen
, &newlen
);
918 if (!v
&& malloc_pwtmpbuf
== NULL
919 && glob_use_alloca (alloca_used
, newlen
))
920 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
921 newlen
, alloca_used
);
924 char *newp
= v
? NULL
: realloc (malloc_pwtmpbuf
, newlen
);
927 free (malloc_pwtmpbuf
);
930 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
935 p
= getpwnam (user_name
);
938 if (__glibc_unlikely (malloc_user_name
))
941 /* If we found a home directory use this. */
944 size_t home_len
= strlen (p
->pw_dir
);
945 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
947 if (__glibc_unlikely (malloc_dirname
))
951 if (glob_use_alloca (alloca_used
, home_len
+ rest_len
+ 1))
952 dirname
= alloca_account (home_len
+ rest_len
+ 1,
956 dirname
= malloc (home_len
+ rest_len
+ 1);
959 free (malloc_pwtmpbuf
);
960 retval
= GLOB_NOSPACE
;
965 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
966 end_name
, rest_len
)) = '\0';
968 dirlen
= home_len
+ rest_len
;
969 dirname_modified
= 1;
971 free (malloc_pwtmpbuf
);
975 free (malloc_pwtmpbuf
);
977 if (flags
& GLOB_TILDE_CHECK
)
979 /* We have to regard it as an error if we cannot find the
981 retval
= GLOB_NOMATCH
;
986 #endif /* !WINDOWS32 */
990 /* Now test whether we looked for "~" or "~NAME". In this case we
991 can give the answer now. */
992 if (filename
== NULL
)
994 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
997 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1000 free (pglob
->gl_pathv
);
1001 pglob
->gl_pathv
= NULL
;
1002 pglob
->gl_pathc
= 0;
1003 retval
= GLOB_NOSPACE
;
1007 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1008 (newcount
+ 2) * sizeof (char *));
1009 if (new_gl_pathv
== NULL
)
1011 pglob
->gl_pathv
= new_gl_pathv
;
1013 if (flags
& GLOB_MARK
&& is_dir (dirname
, flags
, pglob
))
1016 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
1017 if (pglob
->gl_pathv
[newcount
] == NULL
)
1019 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
1022 if (__glibc_unlikely (malloc_dirname
))
1027 if (__glibc_unlikely (malloc_dirname
))
1028 pglob
->gl_pathv
[newcount
] = dirname
;
1031 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
1032 if (pglob
->gl_pathv
[newcount
] == NULL
)
1036 pglob
->gl_pathv
[++newcount
] = NULL
;
1038 pglob
->gl_flags
= flags
;
1043 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1044 /* meta is 1 if correct glob pattern containing metacharacters.
1045 If meta has bit (1 << 2) set, it means there was an unterminated
1046 [ which we handle the same, using fnmatch. Broken unterminated
1047 pattern bracket expressions ought to be rare enough that it is
1048 not worth special casing them, fnmatch will do the right thing. */
1051 /* The directory name contains metacharacters, so we
1052 have to glob for the directory, and then glob for
1053 the pattern in each directory found. */
1056 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1058 /* "foo\\/bar". Remove the final backslash from dirname
1059 if it has not been quoted. */
1060 char *p
= (char *) &dirname
[dirlen
- 1];
1062 while (p
> dirname
&& p
[-1] == '\\') --p
;
1063 if ((&dirname
[dirlen
] - p
) & 1)
1064 *(char *) &dirname
[--dirlen
] = '\0';
1067 if (__glibc_unlikely ((flags
& GLOB_ALTDIRFUNC
) != 0))
1069 /* Use the alternative access functions also in the recursive
1071 dirs
.gl_opendir
= pglob
->gl_opendir
;
1072 dirs
.gl_readdir
= pglob
->gl_readdir
;
1073 dirs
.gl_closedir
= pglob
->gl_closedir
;
1074 dirs
.gl_stat
= pglob
->gl_stat
;
1075 dirs
.gl_lstat
= pglob
->gl_lstat
;
1078 status
= glob (dirname
,
1079 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1081 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1085 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1093 /* We have successfully globbed the preceding directory name.
1094 For each name we found, call glob_in_dir on it and FILENAME,
1095 appending the results to PGLOB. */
1096 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1100 old_pathc
= pglob
->gl_pathc
;
1101 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1102 ((flags
| GLOB_APPEND
)
1103 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1104 errfunc
, pglob
, alloca_used
);
1105 if (status
== GLOB_NOMATCH
)
1106 /* No matches in this directory. Try the next. */
1113 pglob
->gl_pathc
= 0;
1118 /* Stick the directory on the front of each name. */
1119 if (prefix_array (dirs
.gl_pathv
[i
],
1120 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1121 pglob
->gl_pathc
- old_pathc
))
1125 pglob
->gl_pathc
= 0;
1126 retval
= GLOB_NOSPACE
;
1131 flags
|= GLOB_MAGCHAR
;
1133 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
1134 But if we have not found any matching entry and the GLOB_NOCHECK
1135 flag was set we must return the input pattern itself. */
1136 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1140 if (flags
& GLOB_NOCHECK
)
1142 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1143 char **new_gl_pathv
;
1145 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1149 retval
= GLOB_NOSPACE
;
1153 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1154 (newcount
+ 2) * sizeof (char *));
1155 if (new_gl_pathv
== NULL
)
1157 pglob
->gl_pathv
= new_gl_pathv
;
1159 pglob
->gl_pathv
[newcount
] = strdup (pattern
);
1160 if (pglob
->gl_pathv
[newcount
] == NULL
)
1164 pglob
->gl_pathc
= 0;
1165 retval
= GLOB_NOSPACE
;
1172 pglob
->gl_pathv
[newcount
] = NULL
;
1173 pglob
->gl_flags
= flags
;
1178 retval
= GLOB_NOMATCH
;
1187 size_t old_pathc
= pglob
->gl_pathc
;
1188 int orig_flags
= flags
;
1192 char *p
= strchr (dirname
, '\\'), *q
;
1193 /* We need to unescape the dirname string. It is certainly
1194 allocated by alloca, as otherwise filename would be NULL
1195 or dirname wouldn't contain backslashes. */
1208 while (*p
++ != '\0');
1209 dirname_modified
= 1;
1211 if (dirname_modified
)
1212 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1213 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1217 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1218 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1220 /* Make sure globfree (&dirs); is a nop. */
1221 dirs
.gl_pathv
= NULL
;
1231 /* Stick the directory on the front of each name. */
1232 if (prefix_array (dirname
,
1233 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1234 pglob
->gl_pathc
- old_pathc
))
1237 pglob
->gl_pathc
= 0;
1238 retval
= GLOB_NOSPACE
;
1244 if (flags
& GLOB_MARK
)
1246 /* Append slashes to directory names. */
1249 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1250 if (is_dir (pglob
->gl_pathv
[i
], flags
, pglob
))
1252 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1253 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1257 pglob
->gl_pathc
= 0;
1258 retval
= GLOB_NOSPACE
;
1261 strcpy (&new[len
- 2], "/");
1262 pglob
->gl_pathv
[i
] = new;
1266 if (!(flags
& GLOB_NOSORT
))
1268 /* Sort the vector. */
1269 qsort (&pglob
->gl_pathv
[oldcount
],
1270 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1271 sizeof (char *), collated_compare
);
1275 if (__glibc_unlikely (malloc_dirname
))
1280 #if defined _LIBC && !defined glob
1281 libc_hidden_def (glob
)
1285 /* Do a collated comparison of A and B. */
1287 collated_compare (const void *a
, const void *b
)
1289 char *const *ps1
= a
; char *s1
= *ps1
;
1290 char *const *ps2
= b
; char *s2
= *ps2
;
1298 return strcoll (s1
, s2
);
1302 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1303 elements in place. Return nonzero if out of memory, zero if successful.
1304 A slash is inserted between DIRNAME and each elt of ARRAY,
1305 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1307 prefix_array (const char *dirname
, char **array
, size_t n
)
1310 size_t dirlen
= strlen (dirname
);
1311 char dirsep_char
= '/';
1313 if (dirlen
== 1 && dirname
[0] == '/')
1314 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1315 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1318 #if defined __MSDOS__ || defined WINDOWS32
1321 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1322 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1324 else if (dirname
[dirlen
- 1] == ':')
1326 /* DIRNAME is "d:". Use ':' instead of '/'. */
1333 for (i
= 0; i
< n
; ++i
)
1335 size_t eltlen
= strlen (array
[i
]) + 1;
1336 char *new = malloc (dirlen
+ 1 + eltlen
);
1345 char *endp
= mempcpy (new, dirname
, dirlen
);
1346 *endp
++ = dirsep_char
;
1347 mempcpy (endp
, array
[i
], eltlen
);
1356 /* Like 'glob', but PATTERN is a final pathname component,
1357 and matches are searched for in DIRECTORY.
1358 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1359 The GLOB_APPEND flag is assumed to be set (always appends). */
1361 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1362 int (*errfunc
) (const char *, int),
1363 glob_t
*pglob
, size_t alloca_used
)
1365 size_t dirlen
= strlen (directory
);
1366 void *stream
= NULL
;
1367 # define GLOBNAMES_MEMBERS(nnames) \
1368 struct globnames *next; size_t count; char *name[nnames];
1369 struct globnames
{ GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER
) };
1370 struct { GLOBNAMES_MEMBERS (64) } init_names_buf
;
1371 struct globnames
*init_names
= (struct globnames
*) &init_names_buf
;
1372 struct globnames
*names
= init_names
;
1373 struct globnames
*names_alloca
= init_names
;
1380 alloca_used
+= sizeof init_names_buf
;
1382 init_names
->next
= NULL
;
1383 init_names
->count
= ((sizeof init_names_buf
1384 - offsetof (struct globnames
, name
))
1385 / sizeof init_names
->name
[0]);
1387 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1388 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1390 /* We need not do any tests. The PATTERN contains no meta
1391 characters and we must not return an error therefore the
1392 result will always contain exactly one name. */
1393 flags
|= GLOB_NOCHECK
;
1402 size_t patlen
= strlen (pattern
);
1404 bool alloca_fullname
1405 = (! size_add_wrapv (dirlen
+ 1, patlen
+ 1, &fullsize
)
1406 && glob_use_alloca (alloca_used
, fullsize
));
1408 if (alloca_fullname
)
1409 fullname
= alloca_account (fullsize
, alloca_used
);
1412 fullname
= malloc (fullsize
);
1413 if (fullname
== NULL
)
1414 return GLOB_NOSPACE
;
1417 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1419 pattern
, patlen
+ 1);
1420 if (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1421 ? (*pglob
->gl_lstat
) (fullname
, &ust
.st
)
1422 : __lstat64 (fullname
, &ust
.st64
))
1424 || errno
== EOVERFLOW
)
1425 /* We found this file to be existing. Now tell the rest
1426 of the function to copy this name into the result. */
1427 flags
|= GLOB_NOCHECK
;
1429 if (__glibc_unlikely (!alloca_fullname
))
1434 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1435 ? (*pglob
->gl_opendir
) (directory
)
1436 : opendir (directory
));
1439 if (errno
!= ENOTDIR
1440 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1441 || (flags
& GLOB_ERR
)))
1442 return GLOB_ABORTED
;
1446 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1447 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0));
1448 flags
|= GLOB_MAGCHAR
;
1452 struct readdir_result d
;
1454 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1455 d
= convert_dirent (GL_READDIR (pglob
, stream
));
1458 #ifdef COMPILE_GLOB64
1459 d
= convert_dirent (__readdir (stream
));
1461 d
= convert_dirent64 (__readdir64 (stream
));
1468 /* If we shall match only directories use the information
1469 provided by the dirent call if possible. */
1470 if (flags
& GLOB_ONLYDIR
)
1471 switch (readdir_result_type (d
))
1473 case DT_DIR
: case DT_LNK
: case DT_UNKNOWN
: break;
1477 if (fnmatch (pattern
, d
.name
, fnm_flags
) == 0)
1479 if (cur
== names
->count
)
1481 struct globnames
*newnames
;
1482 size_t count
= names
->count
* 2;
1483 size_t nameoff
= offsetof (struct globnames
, name
);
1484 size_t size
= FLEXSIZEOF (struct globnames
, name
,
1485 count
* sizeof (char *));
1486 if ((SIZE_MAX
- nameoff
) / 2 / sizeof (char *)
1489 if (glob_use_alloca (alloca_used
, size
))
1490 newnames
= names_alloca
1491 = alloca_account (size
, alloca_used
);
1492 else if ((newnames
= malloc (size
))
1495 newnames
->count
= count
;
1496 newnames
->next
= names
;
1500 names
->name
[cur
] = strdup (d
.name
);
1501 if (names
->name
[cur
] == NULL
)
1505 if (SIZE_MAX
- pglob
->gl_offs
<= nfound
)
1512 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1514 size_t len
= strlen (pattern
);
1516 names
->name
[cur
] = malloc (len
+ 1);
1517 if (names
->name
[cur
] == NULL
)
1519 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1522 result
= GLOB_NOMATCH
;
1525 char **new_gl_pathv
;
1528 if (SIZE_MAX
/ sizeof (char *) - pglob
->gl_pathc
1529 < pglob
->gl_offs
+ nfound
+ 1)
1533 = realloc (pglob
->gl_pathv
,
1534 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1537 if (new_gl_pathv
== NULL
)
1542 struct globnames
*old
= names
;
1543 for (size_t i
= 0; i
< cur
; ++i
)
1544 free (names
->name
[i
]);
1545 names
= names
->next
;
1546 /* NB: we will not leak memory here if we exit without
1547 freeing the current block assigned to OLD. At least
1548 the very first block is always allocated on the stack
1549 and this is the block assigned to OLD here. */
1552 assert (old
== init_names
);
1556 if (old
== names_alloca
)
1557 names_alloca
= names
;
1561 result
= GLOB_NOSPACE
;
1567 struct globnames
*old
= names
;
1568 for (size_t i
= 0; i
< cur
; ++i
)
1569 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1571 names
= names
->next
;
1572 /* NB: we will not leak memory here if we exit without
1573 freeing the current block assigned to OLD. At least
1574 the very first block is always allocated on the stack
1575 and this is the block assigned to OLD here. */
1578 assert (old
== init_names
);
1582 if (old
== names_alloca
)
1583 names_alloca
= names
;
1588 pglob
->gl_pathv
= new_gl_pathv
;
1590 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1592 pglob
->gl_flags
= flags
;
1599 if (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
))
1600 (*pglob
->gl_closedir
) (stream
);