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/>. */
21 #include <sys/types.h>
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45 # define strdup(str) __strdup (str)
46 # define sysconf(id) __sysconf (id)
47 # define closedir(dir) __closedir (dir)
48 # define opendir(name) __opendir (name)
49 # define readdir(str) __readdir64 (str)
50 # define getpwnam_r(name, bufp, buf, len, res) \
51 __getpwnam_r (name, bufp, buf, len, res)
53 # define __lstat64(fname, buf) __lxstat64 (_STAT_VER, fname, buf)
56 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
58 # define struct_stat64 struct stat64
59 # define FLEXIBLE_ARRAY_MEMBER
60 # include <shlib-compat.h>
63 # define __getlogin_r(buf, len) getlogin_r (buf, len)
64 # define __lstat64(fname, buf) lstat (fname, buf)
65 # define __stat64(fname, buf) stat (fname, buf)
66 # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
67 # define struct_stat64 struct stat
69 # define __alloca alloca
71 # define __readdir readdir
72 # define COMPILE_GLOB64
77 #include <flexmember.h>
78 #include <glob_internal.h>
79 #include <scratch_buffer.h>
81 static const char *next_brace_sub (const char *begin
, int flags
) __THROWNL
;
83 typedef uint_fast8_t dirent_type
;
85 #if !defined _LIBC && !defined HAVE_STRUCT_DIRENT_D_TYPE
86 /* Any distinct values will do here.
87 Undef any existing macros out of the way. */
96 /* A representation of a directory entry which does not depend on the
97 layout of struct dirent, or the size of ino_t. */
101 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
106 /* Initialize and return type member of struct readdir_result. */
108 readdir_result_type (struct readdir_result d
)
110 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
111 # define D_TYPE_TO_RESULT(source) (source)->d_type,
114 # define D_TYPE_TO_RESULT(source)
119 /* Construct an initializer for a struct readdir_result object from a
120 struct dirent *. No copy of the name is made. */
121 #define READDIR_RESULT_INITIALIZER(source) \
124 D_TYPE_TO_RESULT (source) \
127 /* Call gl_readdir on STREAM. This macro can be overridden to reduce
128 type safety if an old interface version needs to be supported. */
130 # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream))
133 /* Extract name and type from directory entry. No copy of the name is
134 made. If SOURCE is NULL, result name is NULL. Keep in sync with
135 convert_dirent64 below. */
136 static struct readdir_result
137 convert_dirent (const struct dirent
*source
)
141 struct readdir_result result
= { NULL
, };
144 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
148 #ifndef COMPILE_GLOB64
149 /* Like convert_dirent, but works on struct dirent64 instead. Keep in
150 sync with convert_dirent above. */
151 static struct readdir_result
152 convert_dirent64 (const struct dirent64
*source
)
156 struct readdir_result result
= { NULL
, };
159 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
165 /* The results of opendir() in this file are not used with dirfd and fchdir,
166 and we do not leak fds to any single-threaded code that could use stdio,
167 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
168 FIXME - if the kernel ever adds support for multi-thread safety for
169 avoiding standard fds, then we should use opendir_safer. */
170 # ifdef GNULIB_defined_opendir
173 # ifdef GNULIB_defined_closedir
177 /* Just use malloc. */
178 # define __libc_use_alloca(n) false
179 # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0)
180 # define extend_alloca_account(buf, len, newlen, avar) \
181 ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0)
185 glob_lstat (glob_t
*pglob
, int flags
, const char *fullname
)
187 /* Use on glob-lstat-compat.c to provide a compat symbol which does not
188 use lstat / gl_lstat. */
190 # define GL_LSTAT gl_stat
191 # define LSTAT64 __stat64
193 # define GL_LSTAT gl_lstat
194 # define LSTAT64 __lstat64
202 return (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
)
203 ? pglob
->GL_LSTAT (fullname
, &ust
.st
)
204 : LSTAT64 (fullname
, &ust
.st64
));
207 /* Set *R = A + B. Return true if the answer is mathematically
208 incorrect due to overflow; in this case, *R is the low order
209 bits of the correct answer. */
212 size_add_wrapv (size_t a
, size_t b
, size_t *r
)
214 #if 5 <= __GNUC__ && !defined __ICC
215 return __builtin_add_overflow (a
, b
, r
);
223 glob_use_alloca (size_t alloca_used
, size_t len
)
226 return (!size_add_wrapv (alloca_used
, len
, &size
)
227 && __libc_use_alloca (size
));
230 static int glob_in_dir (const char *pattern
, const char *directory
,
231 int flags
, int (*errfunc
) (const char *, int),
232 glob_t
*pglob
, size_t alloca_used
);
233 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROWNL
;
234 static int collated_compare (const void *, const void *) __THROWNL
;
237 /* Return true if FILENAME is a directory or a symbolic link to a directory.
238 Use FLAGS and PGLOB to resolve the filename. */
240 is_dir (char const *filename
, int flags
, glob_t
const *pglob
)
244 return (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
)
245 ? pglob
->gl_stat (filename
, &st
) == 0 && S_ISDIR (st
.st_mode
)
246 : __stat64 (filename
, &st64
) == 0 && S_ISDIR (st64
.st_mode
));
249 /* Find the end of the sub-pattern in a brace expression. */
251 next_brace_sub (const char *cp
, int flags
)
255 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
263 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
270 return *cp
!= '\0' ? cp
: NULL
;
273 #ifndef GLOB_ATTRIBUTE
274 # define GLOB_ATTRIBUTE
277 /* Do glob searching for PATTERN, placing results in PGLOB.
278 The bits defined above may be set in FLAGS.
279 If a directory cannot be opened or read and ERRFUNC is not nil,
280 it is called with the pathname that caused the error, and the
281 'errno' value from the failing call; if it returns non-zero
282 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
283 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
284 Otherwise, 'glob' returns zero. */
287 __glob (const char *pattern
, int flags
, int (*errfunc
) (const char *, int),
290 const char *filename
;
291 char *dirname
= NULL
;
296 int dirname_modified
;
297 int malloc_dirname
= 0;
300 size_t alloca_used
= 0;
302 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
304 __set_errno (EINVAL
);
308 /* POSIX requires all slashes to be matched. This means that with
309 a trailing slash we must match only directories. */
310 if (pattern
[0] && pattern
[strlen (pattern
) - 1] == '/')
311 flags
|= GLOB_ONLYDIR
;
313 if (!(flags
& GLOB_DOOFFS
))
314 /* Have to do this so 'globfree' knows where to start freeing. It
315 also makes all the code that uses gl_offs simpler. */
318 if (!(flags
& GLOB_APPEND
))
321 if (!(flags
& GLOB_DOOFFS
))
322 pglob
->gl_pathv
= NULL
;
327 if (pglob
->gl_offs
>= ~((size_t) 0) / sizeof (char *))
330 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
332 if (pglob
->gl_pathv
== NULL
)
335 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
336 pglob
->gl_pathv
[i
] = NULL
;
340 if (flags
& GLOB_BRACE
)
344 if (flags
& GLOB_NOESCAPE
)
345 begin
= strchr (pattern
, '{');
357 if (*begin
== '\\' && begin
[1] != '\0')
359 else if (*begin
== '{')
368 /* Allocate working buffer large enough for our work. Note that
369 we have at least an opening and closing brace. */
377 size_t pattern_len
= strlen (pattern
) - 1;
378 int alloca_onealt
= glob_use_alloca (alloca_used
, pattern_len
);
380 onealt
= alloca_account (pattern_len
, alloca_used
);
383 onealt
= malloc (pattern_len
);
388 /* We know the prefix for all sub-patterns. */
389 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
391 /* Find the first sub-pattern and at the same time find the
392 rest after the closing brace. */
393 next
= next_brace_sub (begin
+ 1, flags
);
396 /* It is an invalid expression. */
398 if (__glibc_unlikely (!alloca_onealt
))
400 flags
&= ~GLOB_BRACE
;
404 /* Now find the end of the whole brace expression. */
408 rest
= next_brace_sub (rest
+ 1, flags
);
410 /* It is an illegal expression. */
413 /* Please note that we now can be sure the brace expression
415 rest_len
= strlen (++rest
) + 1;
417 /* We have a brace expression. BEGIN points to the opening {,
418 NEXT points past the terminator of the first element, and END
419 points past the final }. We will accumulate result names from
420 recursive runs for each brace alternative in the buffer using
422 firstc
= pglob
->gl_pathc
;
429 /* Construct the new glob expression. */
430 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
432 result
= __glob (onealt
,
433 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
437 /* If we got an error, return it. */
438 if (result
&& result
!= GLOB_NOMATCH
)
440 if (__glibc_unlikely (!alloca_onealt
))
442 if (!(flags
& GLOB_APPEND
))
451 /* We saw the last entry. */
455 next
= next_brace_sub (p
, flags
);
456 assert (next
!= NULL
);
459 if (__glibc_unlikely (!alloca_onealt
))
462 if (pglob
->gl_pathc
!= firstc
)
463 /* We found some entries. */
465 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
471 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
473 /* Find the filename. */
474 filename
= strrchr (pattern
, '/');
476 #if defined __MSDOS__ || defined WINDOWS32
477 /* The case of "d:pattern". Since ':' is not allowed in
478 file names, we can safely assume that wherever it
479 happens in pattern, it signals the filename part. This
480 is so we could some day support patterns like "[a-z]:foo". */
481 if (filename
== NULL
)
482 filename
= strchr (pattern
, ':');
483 #endif /* __MSDOS__ || WINDOWS32 */
485 dirname_modified
= 0;
486 if (filename
== NULL
)
488 /* This can mean two things: a simple name or "~name". The latter
489 case is nothing but a notation for a directory. */
490 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
492 dirname
= (char *) pattern
;
493 dirlen
= strlen (pattern
);
495 /* Set FILENAME to NULL as a special flag. This is ugly but
496 other solutions would require much more code. We test for
497 this special case below. */
502 if (__glibc_unlikely (pattern
[0] == '\0'))
504 dirs
.gl_pathv
= NULL
;
509 dirname
= (char *) ".";
513 else if (filename
== pattern
514 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
515 && (flags
& GLOB_NOESCAPE
) == 0))
517 /* "/pattern" or "\\/pattern". */
518 dirname
= (char *) "/";
525 dirlen
= filename
- pattern
;
526 #if defined __MSDOS__ || defined WINDOWS32
528 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
533 drive_spec
= __alloca (dirlen
+ 1);
534 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
535 /* For now, disallow wildcards in the drive spec, to
536 prevent infinite recursion in glob. */
537 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
539 /* If this is "d:pattern", we need to copy ':' to DIRNAME
540 as well. If it's "d:/pattern", don't remove the slash
541 from "d:/", since "d:" and "d:/" are not the same.*/
545 if (glob_use_alloca (alloca_used
, dirlen
+ 1))
546 newp
= alloca_account (dirlen
+ 1, alloca_used
);
549 newp
= malloc (dirlen
+ 1);
554 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
558 #if defined __MSDOS__ || defined WINDOWS32
559 bool drive_root
= (dirlen
> 1
560 && (dirname
[dirlen
- 1] == ':'
561 || (dirlen
> 2 && dirname
[dirlen
- 2] == ':'
562 && dirname
[dirlen
- 1] == '/')));
564 bool drive_root
= false;
567 if (filename
[0] == '\0' && dirlen
> 1 && !drive_root
)
568 /* "pattern/". Expand "pattern", appending slashes. */
570 int orig_flags
= flags
;
571 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
573 /* "pattern\\/". Remove the final backslash if it hasn't
575 char *p
= (char *) &dirname
[dirlen
- 1];
577 while (p
> dirname
&& p
[-1] == '\\') --p
;
578 if ((&dirname
[dirlen
] - p
) & 1)
580 *(char *) &dirname
[--dirlen
] = '\0';
581 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
584 int val
= __glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
586 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
587 | (flags
& GLOB_MARK
));
588 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
590 /* Make sure globfree (&dirs); is a nop. */
591 dirs
.gl_pathv
= NULL
;
593 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
601 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
603 if (dirname
[1] == '\0' || dirname
[1] == '/'
604 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
605 && (dirname
[2] == '\0' || dirname
[2] == '/')))
607 /* Look up home directory. */
608 char *home_dir
= getenv ("HOME");
609 int malloc_home_dir
= 0;
610 if (home_dir
== NULL
|| home_dir
[0] == '\0')
613 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give
614 preference to HOME, because the user can change HOME. */
615 const char *home_drive
= getenv ("HOMEDRIVE");
616 const char *home_path
= getenv ("HOMEPATH");
618 if (home_drive
!= NULL
&& home_path
!= NULL
)
620 size_t home_drive_len
= strlen (home_drive
);
621 size_t home_path_len
= strlen (home_path
);
622 char *mem
= alloca (home_drive_len
+ home_path_len
+ 1);
624 memcpy (mem
, home_drive
, home_drive_len
);
625 memcpy (mem
+ home_drive_len
, home_path
, home_path_len
+ 1);
629 home_dir
= "c:/users/default"; /* poor default */
634 struct scratch_buffer s
;
635 scratch_buffer_init (&s
);
639 err
= __getlogin_r (s
.data
, s
.length
);
642 # if defined HAVE_GETPWNAM_R || defined _LIBC
643 size_t ssize
= strlen (s
.data
) + 1;
644 err
= getpwnam_r (s
.data
, &pwbuf
, s
.data
+ ssize
,
645 s
.length
- ssize
, &p
);
647 p
= getpwnam (s
.data
);
654 if (!scratch_buffer_grow (&s
))
656 retval
= GLOB_NOSPACE
;
662 home_dir
= strdup (p
->pw_dir
);
665 scratch_buffer_free (&s
);
666 if (err
== 0 && home_dir
== NULL
)
668 retval
= GLOB_NOSPACE
;
671 #endif /* WINDOWS32 */
673 if (home_dir
== NULL
|| home_dir
[0] == '\0')
675 if (__glibc_unlikely (malloc_home_dir
))
677 if (flags
& GLOB_TILDE_CHECK
)
679 retval
= GLOB_NOMATCH
;
684 home_dir
= (char *) "~"; /* No luck. */
688 /* Now construct the full directory. */
689 if (dirname
[1] == '\0')
691 if (__glibc_unlikely (malloc_dirname
))
695 dirlen
= strlen (dirname
);
696 malloc_dirname
= malloc_home_dir
;
701 size_t home_len
= strlen (home_dir
);
702 int use_alloca
= glob_use_alloca (alloca_used
, home_len
+ dirlen
);
704 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
707 newp
= malloc (home_len
+ dirlen
);
710 if (__glibc_unlikely (malloc_home_dir
))
712 retval
= GLOB_NOSPACE
;
717 mempcpy (mempcpy (newp
, home_dir
, home_len
),
718 &dirname
[1], dirlen
);
720 if (__glibc_unlikely (malloc_dirname
))
724 dirlen
+= home_len
- 1;
725 malloc_dirname
= !use_alloca
;
727 if (__glibc_unlikely (malloc_home_dir
))
730 dirname_modified
= 1;
735 char *end_name
= strchr (dirname
, '/');
737 int malloc_user_name
= 0;
738 char *unescape
= NULL
;
740 if (!(flags
& GLOB_NOESCAPE
))
742 if (end_name
== NULL
)
744 unescape
= strchr (dirname
, '\\');
746 end_name
= strchr (unescape
, '\0');
749 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
751 if (end_name
== NULL
)
752 user_name
= dirname
+ 1;
756 if (glob_use_alloca (alloca_used
, end_name
- dirname
))
757 newp
= alloca_account (end_name
- dirname
, alloca_used
);
760 newp
= malloc (end_name
- dirname
);
763 retval
= GLOB_NOSPACE
;
766 malloc_user_name
= 1;
768 if (unescape
!= NULL
)
770 char *p
= mempcpy (newp
, dirname
+ 1,
771 unescape
- dirname
- 1);
773 while (q
!= end_name
)
777 if (q
+ 1 == end_name
)
779 /* "~fo\\o\\" unescape to user_name "foo\\",
780 but "~fo\\o\\/" unescape to user_name
782 if (filename
== NULL
)
793 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
- 1))
798 /* Look up specific user's home directory. */
801 struct scratch_buffer pwtmpbuf
;
802 scratch_buffer_init (&pwtmpbuf
);
804 # if defined HAVE_GETPWNAM_R || defined _LIBC
807 while (getpwnam_r (user_name
, &pwbuf
,
808 pwtmpbuf
.data
, pwtmpbuf
.length
, &p
)
811 if (!scratch_buffer_grow (&pwtmpbuf
))
813 retval
= GLOB_NOSPACE
;
818 p
= getpwnam (user_name
);
821 if (__glibc_unlikely (malloc_user_name
))
824 /* If we found a home directory use this. */
827 size_t home_len
= strlen (p
->pw_dir
);
828 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
830 if (__glibc_unlikely (malloc_dirname
))
834 if (glob_use_alloca (alloca_used
, home_len
+ rest_len
+ 1))
835 dirname
= alloca_account (home_len
+ rest_len
+ 1,
839 dirname
= malloc (home_len
+ rest_len
+ 1);
842 scratch_buffer_free (&pwtmpbuf
);
843 retval
= GLOB_NOSPACE
;
848 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
849 end_name
, rest_len
)) = '\0';
851 dirlen
= home_len
+ rest_len
;
852 dirname_modified
= 1;
856 if (flags
& GLOB_TILDE_CHECK
)
858 /* We have to regard it as an error if we cannot find the
860 retval
= GLOB_NOMATCH
;
864 scratch_buffer_free (&pwtmpbuf
);
866 #endif /* !WINDOWS32 */
870 /* Now test whether we looked for "~" or "~NAME". In this case we
871 can give the answer now. */
872 if (filename
== NULL
)
874 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
877 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
880 free (pglob
->gl_pathv
);
881 pglob
->gl_pathv
= NULL
;
883 retval
= GLOB_NOSPACE
;
887 new_gl_pathv
= realloc (pglob
->gl_pathv
,
888 (newcount
+ 2) * sizeof (char *));
889 if (new_gl_pathv
== NULL
)
891 pglob
->gl_pathv
= new_gl_pathv
;
893 if (flags
& GLOB_MARK
&& is_dir (dirname
, flags
, pglob
))
896 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
897 if (pglob
->gl_pathv
[newcount
] == NULL
)
899 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
902 if (__glibc_unlikely (malloc_dirname
))
907 if (__glibc_unlikely (malloc_dirname
))
908 pglob
->gl_pathv
[newcount
] = dirname
;
911 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
912 if (pglob
->gl_pathv
[newcount
] == NULL
)
916 pglob
->gl_pathv
[++newcount
] = NULL
;
918 pglob
->gl_flags
= flags
;
923 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
924 /* meta is 1 if correct glob pattern containing metacharacters.
925 If meta has bit (1 << 2) set, it means there was an unterminated
926 [ which we handle the same, using fnmatch. Broken unterminated
927 pattern bracket expressions ought to be rare enough that it is
928 not worth special casing them, fnmatch will do the right thing. */
929 if (meta
& (GLOBPAT_SPECIAL
| GLOBPAT_BRACKET
))
931 /* The directory name contains metacharacters, so we
932 have to glob for the directory, and then glob for
933 the pattern in each directory found. */
936 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
938 /* "foo\\/bar". Remove the final backslash from dirname
939 if it has not been quoted. */
940 char *p
= (char *) &dirname
[dirlen
- 1];
942 while (p
> dirname
&& p
[-1] == '\\') --p
;
943 if ((&dirname
[dirlen
] - p
) & 1)
944 *(char *) &dirname
[--dirlen
] = '\0';
947 if (__glibc_unlikely ((flags
& GLOB_ALTDIRFUNC
) != 0))
949 /* Use the alternative access functions also in the recursive
951 dirs
.gl_opendir
= pglob
->gl_opendir
;
952 dirs
.gl_readdir
= pglob
->gl_readdir
;
953 dirs
.gl_closedir
= pglob
->gl_closedir
;
954 dirs
.gl_stat
= pglob
->gl_stat
;
955 dirs
.gl_lstat
= pglob
->gl_lstat
;
958 status
= __glob (dirname
,
959 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
| GLOB_ALTDIRFUNC
))
960 | GLOB_NOSORT
| GLOB_ONLYDIR
),
964 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
972 /* We have successfully globbed the preceding directory name.
973 For each name we found, call glob_in_dir on it and FILENAME,
974 appending the results to PGLOB. */
975 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
979 old_pathc
= pglob
->gl_pathc
;
980 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
981 ((flags
| GLOB_APPEND
)
982 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
983 errfunc
, pglob
, alloca_used
);
984 if (status
== GLOB_NOMATCH
)
985 /* No matches in this directory. Try the next. */
997 /* Stick the directory on the front of each name. */
998 if (prefix_array (dirs
.gl_pathv
[i
],
999 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1000 pglob
->gl_pathc
- old_pathc
))
1004 pglob
->gl_pathc
= 0;
1005 retval
= GLOB_NOSPACE
;
1010 flags
|= GLOB_MAGCHAR
;
1012 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
1013 But if we have not found any matching entry and the GLOB_NOCHECK
1014 flag was set we must return the input pattern itself. */
1015 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1019 if (flags
& GLOB_NOCHECK
)
1021 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1022 char **new_gl_pathv
;
1024 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1028 retval
= GLOB_NOSPACE
;
1032 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1033 (newcount
+ 2) * sizeof (char *));
1034 if (new_gl_pathv
== NULL
)
1036 pglob
->gl_pathv
= new_gl_pathv
;
1038 pglob
->gl_pathv
[newcount
] = strdup (pattern
);
1039 if (pglob
->gl_pathv
[newcount
] == NULL
)
1043 pglob
->gl_pathc
= 0;
1044 retval
= GLOB_NOSPACE
;
1051 pglob
->gl_pathv
[newcount
] = NULL
;
1052 pglob
->gl_flags
= flags
;
1057 retval
= GLOB_NOMATCH
;
1066 size_t old_pathc
= pglob
->gl_pathc
;
1067 int orig_flags
= flags
;
1069 if (meta
& GLOBPAT_BACKSLASH
)
1071 char *p
= strchr (dirname
, '\\'), *q
;
1072 /* We need to unescape the dirname string. It is certainly
1073 allocated by alloca, as otherwise filename would be NULL
1074 or dirname wouldn't contain backslashes. */
1087 while (*p
++ != '\0');
1088 dirname_modified
= 1;
1090 if (dirname_modified
)
1091 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1092 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1096 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1097 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1099 /* Make sure globfree (&dirs); is a nop. */
1100 dirs
.gl_pathv
= NULL
;
1110 /* Stick the directory on the front of each name. */
1111 if (prefix_array (dirname
,
1112 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1113 pglob
->gl_pathc
- old_pathc
))
1116 pglob
->gl_pathc
= 0;
1117 retval
= GLOB_NOSPACE
;
1123 if (flags
& GLOB_MARK
)
1125 /* Append slashes to directory names. */
1128 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1129 if (is_dir (pglob
->gl_pathv
[i
], flags
, pglob
))
1131 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1132 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1136 pglob
->gl_pathc
= 0;
1137 retval
= GLOB_NOSPACE
;
1140 strcpy (&new[len
- 2], "/");
1141 pglob
->gl_pathv
[i
] = new;
1145 if (!(flags
& GLOB_NOSORT
))
1147 /* Sort the vector. */
1148 qsort (&pglob
->gl_pathv
[oldcount
],
1149 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1150 sizeof (char *), collated_compare
);
1154 if (__glibc_unlikely (malloc_dirname
))
1159 #if defined _LIBC && !defined __glob
1160 versioned_symbol (libc
, __glob
, glob
, GLIBC_2_27
);
1161 libc_hidden_ver (__glob
, glob
)
1165 /* Do a collated comparison of A and B. */
1167 collated_compare (const void *a
, const void *b
)
1169 char *const *ps1
= a
; char *s1
= *ps1
;
1170 char *const *ps2
= b
; char *s2
= *ps2
;
1178 return strcoll (s1
, s2
);
1182 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1183 elements in place. Return nonzero if out of memory, zero if successful.
1184 A slash is inserted between DIRNAME and each elt of ARRAY,
1185 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1187 prefix_array (const char *dirname
, char **array
, size_t n
)
1190 size_t dirlen
= strlen (dirname
);
1191 char dirsep_char
= '/';
1193 if (dirlen
== 1 && dirname
[0] == '/')
1194 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1195 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1198 #if defined __MSDOS__ || defined WINDOWS32
1201 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1202 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1204 else if (dirname
[dirlen
- 1] == ':')
1206 /* DIRNAME is "d:". Use ':' instead of '/'. */
1213 for (i
= 0; i
< n
; ++i
)
1215 size_t eltlen
= strlen (array
[i
]) + 1;
1216 char *new = malloc (dirlen
+ 1 + eltlen
);
1225 char *endp
= mempcpy (new, dirname
, dirlen
);
1226 *endp
++ = dirsep_char
;
1227 mempcpy (endp
, array
[i
], eltlen
);
1236 /* Like 'glob', but PATTERN is a final pathname component,
1237 and matches are searched for in DIRECTORY.
1238 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1239 The GLOB_APPEND flag is assumed to be set (always appends). */
1241 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1242 int (*errfunc
) (const char *, int),
1243 glob_t
*pglob
, size_t alloca_used
)
1245 size_t dirlen
= strlen (directory
);
1246 void *stream
= NULL
;
1247 # define GLOBNAMES_MEMBERS(nnames) \
1248 struct globnames *next; size_t count; char *name[nnames];
1249 struct globnames
{ GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER
) };
1250 struct { GLOBNAMES_MEMBERS (64) } init_names_buf
;
1251 struct globnames
*init_names
= (struct globnames
*) &init_names_buf
;
1252 struct globnames
*names
= init_names
;
1253 struct globnames
*names_alloca
= init_names
;
1260 alloca_used
+= sizeof init_names_buf
;
1262 init_names
->next
= NULL
;
1263 init_names
->count
= ((sizeof init_names_buf
1264 - offsetof (struct globnames
, name
))
1265 / sizeof init_names
->name
[0]);
1267 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1268 if (meta
== GLOBPAT_NONE
&& (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1270 /* We need not do any tests. The PATTERN contains no meta
1271 characters and we must not return an error therefore the
1272 result will always contain exactly one name. */
1273 flags
|= GLOB_NOCHECK
;
1275 else if (meta
== GLOBPAT_NONE
)
1277 size_t patlen
= strlen (pattern
);
1279 bool alloca_fullname
1280 = (! size_add_wrapv (dirlen
+ 1, patlen
+ 1, &fullsize
)
1281 && glob_use_alloca (alloca_used
, fullsize
));
1283 if (alloca_fullname
)
1284 fullname
= alloca_account (fullsize
, alloca_used
);
1287 fullname
= malloc (fullsize
);
1288 if (fullname
== NULL
)
1289 return GLOB_NOSPACE
;
1292 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1294 pattern
, patlen
+ 1);
1295 if (glob_lstat (pglob
, flags
, fullname
) == 0
1296 || errno
== EOVERFLOW
)
1297 /* We found this file to be existing. Now tell the rest
1298 of the function to copy this name into the result. */
1299 flags
|= GLOB_NOCHECK
;
1301 if (__glibc_unlikely (!alloca_fullname
))
1306 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1307 ? (*pglob
->gl_opendir
) (directory
)
1308 : opendir (directory
));
1311 if (errno
!= ENOTDIR
1312 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1313 || (flags
& GLOB_ERR
)))
1314 return GLOB_ABORTED
;
1318 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1319 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0));
1320 flags
|= GLOB_MAGCHAR
;
1324 struct readdir_result d
;
1326 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1327 d
= convert_dirent (GL_READDIR (pglob
, stream
));
1330 #ifdef COMPILE_GLOB64
1331 d
= convert_dirent (__readdir (stream
));
1333 d
= convert_dirent64 (__readdir64 (stream
));
1340 /* If we shall match only directories use the information
1341 provided by the dirent call if possible. */
1342 if (flags
& GLOB_ONLYDIR
)
1343 switch (readdir_result_type (d
))
1345 case DT_DIR
: case DT_LNK
: case DT_UNKNOWN
: break;
1349 if (fnmatch (pattern
, d
.name
, fnm_flags
) == 0)
1351 if (cur
== names
->count
)
1353 struct globnames
*newnames
;
1354 size_t count
= names
->count
* 2;
1355 size_t nameoff
= offsetof (struct globnames
, name
);
1356 size_t size
= FLEXSIZEOF (struct globnames
, name
,
1357 count
* sizeof (char *));
1358 if ((SIZE_MAX
- nameoff
) / 2 / sizeof (char *)
1361 if (glob_use_alloca (alloca_used
, size
))
1362 newnames
= names_alloca
1363 = alloca_account (size
, alloca_used
);
1364 else if ((newnames
= malloc (size
))
1367 newnames
->count
= count
;
1368 newnames
->next
= names
;
1372 names
->name
[cur
] = strdup (d
.name
);
1373 if (names
->name
[cur
] == NULL
)
1377 if (SIZE_MAX
- pglob
->gl_offs
<= nfound
)
1384 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1386 size_t len
= strlen (pattern
);
1388 names
->name
[cur
] = malloc (len
+ 1);
1389 if (names
->name
[cur
] == NULL
)
1391 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1394 result
= GLOB_NOMATCH
;
1397 char **new_gl_pathv
;
1400 if (SIZE_MAX
/ sizeof (char *) - pglob
->gl_pathc
1401 < pglob
->gl_offs
+ nfound
+ 1)
1405 = realloc (pglob
->gl_pathv
,
1406 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1409 if (new_gl_pathv
== NULL
)
1414 struct globnames
*old
= names
;
1415 for (size_t i
= 0; i
< cur
; ++i
)
1416 free (names
->name
[i
]);
1417 names
= names
->next
;
1418 /* NB: we will not leak memory here if we exit without
1419 freeing the current block assigned to OLD. At least
1420 the very first block is always allocated on the stack
1421 and this is the block assigned to OLD here. */
1424 assert (old
== init_names
);
1428 if (old
== names_alloca
)
1429 names_alloca
= names
;
1433 result
= GLOB_NOSPACE
;
1439 struct globnames
*old
= names
;
1440 for (size_t i
= 0; i
< cur
; ++i
)
1441 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1443 names
= names
->next
;
1444 /* NB: we will not leak memory here if we exit without
1445 freeing the current block assigned to OLD. At least
1446 the very first block is always allocated on the stack
1447 and this is the block assigned to OLD here. */
1450 assert (old
== init_names
);
1454 if (old
== names_alloca
)
1455 names_alloca
= names
;
1460 pglob
->gl_pathv
= new_gl_pathv
;
1462 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1464 pglob
->gl_flags
= flags
;
1471 if (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
))
1472 (*pglob
->gl_closedir
) (stream
);