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/>. */
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 optimizes away the pattern == NULL || pglob == NULL tests below. */
21 # define _GL_ARG_NONNULL(params)
28 #include <sys/types.h>
34 /* Outcomment the following line for production quality code. */
35 /* #define NDEBUG 1 */
38 #include <stdio.h> /* Needed on stupid SunOS for assert. */
43 #if !defined POSIX && defined _POSIX_VERSION
47 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
57 # define __set_errno(val) errno = (val)
67 # define strdup(str) __strdup (str)
68 # define sysconf(id) __sysconf (id)
69 # define closedir(dir) __closedir (dir)
70 # define opendir(name) __opendir (name)
71 # define readdir(str) __readdir64 (str)
72 # define getpwnam_r(name, bufp, buf, len, res) \
73 __getpwnam_r (name, bufp, buf, len, res)
75 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
77 # define struct_stat64 struct stat64
79 # define __getlogin_r(buf, len) getlogin_r (buf, len)
80 # define __stat64(fname, buf) stat (fname, buf)
81 # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag)
82 # define struct_stat64 struct stat
84 # define __alloca alloca
86 # define __readdir readdir
87 # define __glob_pattern_p glob_pattern_p
88 # define COMPILE_GLOB64
93 #include "flexmember.h"
95 #ifdef _SC_GETPW_R_SIZE_MAX
96 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
98 # define GETPW_R_SIZE_MAX() (-1)
100 #ifdef _SC_LOGIN_NAME_MAX
101 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
103 # define GET_LOGIN_NAME_MAX() (-1)
106 static const char *next_brace_sub (const char *begin
, int flags
) __THROWNL
;
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
119 # if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
120 /* Initializer based on the d_type member of struct dirent. */
121 # define D_TYPE_TO_RESULT(source) (source)->d_type,
123 /* True if the directory entry D might be a symbolic link. */
125 readdir_result_might_be_symlink (struct readdir_result d
)
127 return d
.type
== DT_UNKNOWN
|| d
.type
== DT_LNK
;
130 /* True if the directory entry D might be a directory. */
132 readdir_result_might_be_dir (struct readdir_result d
)
134 return d
.type
== DT_DIR
|| readdir_result_might_be_symlink (d
);
136 # else /* defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE */
137 # define D_TYPE_TO_RESULT(source)
139 /* If we do not have type information, symbolic links and directories
140 are always a possibility. */
143 readdir_result_might_be_symlink (struct readdir_result d
)
149 readdir_result_might_be_dir (struct readdir_result d
)
154 # endif /* defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE */
156 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
157 /* Initializer for skip_entry. POSIX does not require that the d_ino
158 field be present, and some systems do not provide it. */
159 # define D_INO_TO_RESULT(source) false,
161 # define D_INO_TO_RESULT(source) (source)->d_ino == 0,
164 /* Construct an initializer for a struct readdir_result object from a
165 struct dirent *. No copy of the name is made. */
166 #define READDIR_RESULT_INITIALIZER(source) \
169 D_TYPE_TO_RESULT (source) \
170 D_INO_TO_RESULT (source) \
173 #endif /* !defined GLOB_ONLY_P */
175 /* Call gl_readdir on STREAM. This macro can be overridden to reduce
176 type safety if an old interface version needs to be supported. */
178 # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream))
181 /* Extract name and type from directory entry. No copy of the name is
182 made. If SOURCE is NULL, result name is NULL. Keep in sync with
183 convert_dirent64 below. */
184 static struct readdir_result
185 convert_dirent (const struct dirent
*source
)
189 struct readdir_result result
= { NULL
, };
194 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
199 #ifndef COMPILE_GLOB64
200 /* Like convert_dirent, but works on struct dirent64 instead. Keep in
201 sync with convert_dirent above. */
202 static struct readdir_result
203 convert_dirent64 (const struct dirent64
*source
)
207 struct readdir_result result
= { NULL
, };
212 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
219 #ifndef attribute_hidden
220 # define attribute_hidden
223 #ifndef __attribute_noinline__
224 # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1)
225 # define __attribute_noinline__ /* Ignore */
227 # define __attribute_noinline__ __attribute__ ((__noinline__))
231 #ifndef __glibc_unlikely
232 # define __glibc_unlikely(expr) __builtin_expect (expr, 0)
236 /* The results of opendir() in this file are not used with dirfd and fchdir,
237 and we do not leak fds to any single-threaded code that could use stdio,
238 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
239 FIXME - if the kernel ever adds support for multi-thread safety for
240 avoiding standard fds, then we should use opendir_safer. */
241 # ifdef GNULIB_defined_opendir
244 # ifdef GNULIB_defined_closedir
248 /* Just use malloc. */
249 # define __libc_use_alloca(n) false
250 # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0)
251 # define extend_alloca_account(buf, len, newlen, avar) \
252 ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0)
255 /* Set *R = A + B. Return true if the answer is mathematically
256 incorrect due to overflow; in this case, *R is the low order
257 bits of the correct answer.. */
260 size_add_wrapv (size_t a
, size_t b
, size_t *r
)
263 return __builtin_add_overflow (a
, b
, r
);
271 glob_use_alloca (size_t alloca_used
, size_t len
)
274 return (!size_add_wrapv (alloca_used
, len
, &size
)
275 && __libc_use_alloca (size
));
278 static int glob_in_dir (const char *pattern
, const char *directory
,
279 int flags
, int (*errfunc
) (const char *, int),
280 glob_t
*pglob
, size_t alloca_used
);
281 extern int __glob_pattern_type (const char *pattern
, int quote
)
285 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROWNL
;
286 static int collated_compare (const void *, const void *) __THROWNL
;
289 /* Find the end of the sub-pattern in a brace expression. */
291 next_brace_sub (const char *cp
, int flags
)
295 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
303 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
310 return *cp
!= '\0' ? cp
: NULL
;
313 #endif /* !defined GLOB_ONLY_P */
315 /* Do glob searching for PATTERN, placing results in PGLOB.
316 The bits defined above may be set in FLAGS.
317 If a directory cannot be opened or read and ERRFUNC is not nil,
318 it is called with the pathname that caused the error, and the
319 'errno' value from the failing call; if it returns non-zero
320 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
321 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
322 Otherwise, 'glob' returns zero. */
324 #ifdef GLOB_ATTRIBUTE
327 glob (const char *pattern
, int flags
, int (*errfunc
) (const char *, int),
330 const char *filename
;
331 char *dirname
= NULL
;
336 int dirname_modified
;
337 int malloc_dirname
= 0;
340 size_t alloca_used
= 0;
342 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
344 __set_errno (EINVAL
);
348 /* POSIX requires all slashes to be matched. This means that with
349 a trailing slash we must match only directories. */
350 if (pattern
[0] && pattern
[strlen (pattern
) - 1] == '/')
351 flags
|= GLOB_ONLYDIR
;
353 if (!(flags
& GLOB_DOOFFS
))
354 /* Have to do this so 'globfree' knows where to start freeing. It
355 also makes all the code that uses gl_offs simpler. */
358 if (flags
& GLOB_BRACE
)
362 if (flags
& GLOB_NOESCAPE
)
363 begin
= strchr (pattern
, '{');
375 if (*begin
== '\\' && begin
[1] != '\0')
377 else if (*begin
== '{')
386 /* Allocate working buffer large enough for our work. Note that
387 we have at least an opening and closing brace. */
395 size_t pattern_len
= strlen (pattern
) - 1;
396 int alloca_onealt
= glob_use_alloca (alloca_used
, pattern_len
);
398 onealt
= alloca_account (pattern_len
, alloca_used
);
401 onealt
= malloc (pattern_len
);
404 if (!(flags
& GLOB_APPEND
))
407 pglob
->gl_pathv
= NULL
;
413 /* We know the prefix for all sub-patterns. */
414 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
416 /* Find the first sub-pattern and at the same time find the
417 rest after the closing brace. */
418 next
= next_brace_sub (begin
+ 1, flags
);
421 /* It is an invalid expression. */
423 if (__glibc_unlikely (!alloca_onealt
))
425 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
428 /* Now find the end of the whole brace expression. */
432 rest
= next_brace_sub (rest
+ 1, flags
);
434 /* It is an illegal expression. */
437 /* Please note that we now can be sure the brace expression
439 rest_len
= strlen (++rest
) + 1;
441 /* We have a brace expression. BEGIN points to the opening {,
442 NEXT points past the terminator of the first element, and END
443 points past the final }. We will accumulate result names from
444 recursive runs for each brace alternative in the buffer using
447 if (!(flags
& GLOB_APPEND
))
449 /* This call is to set a new vector, so clear out the
450 vector so we can append to it. */
452 pglob
->gl_pathv
= NULL
;
454 firstc
= pglob
->gl_pathc
;
461 /* Construct the new glob expression. */
462 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
464 result
= glob (onealt
,
465 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
466 | GLOB_APPEND
), errfunc
, pglob
);
468 /* If we got an error, return it. */
469 if (result
&& result
!= GLOB_NOMATCH
)
471 if (__glibc_unlikely (!alloca_onealt
))
473 if (!(flags
& GLOB_APPEND
))
482 /* We saw the last entry. */
486 next
= next_brace_sub (p
, flags
);
487 assert (next
!= NULL
);
490 if (__glibc_unlikely (!alloca_onealt
))
493 if (pglob
->gl_pathc
!= firstc
)
494 /* We found some entries. */
496 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
501 if (!(flags
& GLOB_APPEND
))
504 if (!(flags
& GLOB_DOOFFS
))
505 pglob
->gl_pathv
= NULL
;
510 if (pglob
->gl_offs
>= SIZE_MAX
/ sizeof (char *))
513 pglob
->gl_pathv
= malloc ((pglob
->gl_offs
+ 1) * sizeof (char *));
514 if (pglob
->gl_pathv
== NULL
)
517 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
518 pglob
->gl_pathv
[i
] = NULL
;
522 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
524 /* Find the filename. */
525 filename
= strrchr (pattern
, '/');
526 #if defined __MSDOS__ || defined WINDOWS32
527 /* The case of "d:pattern". Since ':' is not allowed in
528 file names, we can safely assume that wherever it
529 happens in pattern, it signals the filename part. This
530 is so we could some day support patterns like "[a-z]:foo". */
531 if (filename
== NULL
)
532 filename
= strchr (pattern
, ':');
533 #endif /* __MSDOS__ || WINDOWS32 */
534 dirname_modified
= 0;
535 if (filename
== NULL
)
537 /* This can mean two things: a simple name or "~name". The latter
538 case is nothing but a notation for a directory. */
539 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
541 dirname
= (char *) pattern
;
542 dirlen
= strlen (pattern
);
544 /* Set FILENAME to NULL as a special flag. This is ugly but
545 other solutions would require much more code. We test for
546 this special case below. */
551 if (__glibc_unlikely (pattern
[0] == '\0'))
553 dirs
.gl_pathv
= NULL
;
559 dirname
= (char *) "";
561 dirname
= (char *) ".";
566 else if (filename
== pattern
567 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
568 && (flags
& GLOB_NOESCAPE
) == 0))
570 /* "/pattern" or "\\/pattern". */
571 dirname
= (char *) "/";
578 dirlen
= filename
- pattern
;
579 #if defined __MSDOS__ || defined WINDOWS32
581 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
586 drive_spec
= __alloca (dirlen
+ 1);
587 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
588 /* For now, disallow wildcards in the drive spec, to
589 prevent infinite recursion in glob. */
590 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
592 /* If this is "d:pattern", we need to copy ':' to DIRNAME
593 as well. If it's "d:/pattern", don't remove the slash
594 from "d:/", since "d:" and "d:/" are not the same.*/
597 if (glob_use_alloca (alloca_used
, dirlen
+ 1))
598 newp
= alloca_account (dirlen
+ 1, alloca_used
);
601 newp
= malloc (dirlen
+ 1);
606 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
610 if (filename
[0] == '\0'
611 #if defined __MSDOS__ || defined WINDOWS32
612 && dirname
[dirlen
- 1] != ':'
613 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
614 || dirname
[dirlen
- 1] != '/')
617 /* "pattern/". Expand "pattern", appending slashes. */
619 int orig_flags
= flags
;
621 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
623 /* "pattern\\/". Remove the final backslash if it hasn't
625 char *p
= (char *) &dirname
[dirlen
- 1];
627 while (p
> dirname
&& p
[-1] == '\\') --p
;
628 if ((&dirname
[dirlen
] - p
) & 1)
630 *(char *) &dirname
[--dirlen
] = '\0';
631 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
634 val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
636 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
637 | (flags
& GLOB_MARK
));
638 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
640 /* Make sure globfree (&dirs); is a nop. */
641 dirs
.gl_pathv
= NULL
;
643 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
651 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
653 if (dirname
[1] == '\0' || dirname
[1] == '/'
654 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
655 && (dirname
[2] == '\0' || dirname
[2] == '/')))
657 /* Look up home directory. */
658 char *home_dir
= getenv ("HOME");
659 int malloc_home_dir
= 0;
661 if (home_dir
== NULL
|| home_dir
[0] == '\0')
665 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference
666 to HOME, because the user can change HOME. */
667 if (home_dir
== NULL
|| home_dir
[0] == '\0')
669 const char *home_drive
= getenv ("HOMEDRIVE");
670 const char *home_path
= getenv ("HOMEPATH");
672 if (home_drive
!= NULL
&& home_path
!= NULL
)
674 size_t home_drive_len
= strlen (home_drive
);
675 size_t home_path_len
= strlen (home_path
);
676 char *mem
= alloca (home_drive_len
+ home_path_len
+ 1);
678 memcpy (mem
, home_drive
, home_drive_len
);
679 memcpy (mem
+ home_drive_len
, home_path
, home_path_len
+ 1);
683 home_dir
= "c:/users/default"; /* poor default */
686 if (home_dir
== NULL
|| home_dir
[0] == '\0')
691 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
694 /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try
697 if (glob_use_alloca (alloca_used
, buflen
))
698 name
= alloca_account (buflen
, alloca_used
);
701 name
= malloc (buflen
);
704 retval
= GLOB_NOSPACE
;
710 success
= __getlogin_r (name
, buflen
) == 0;
714 char *malloc_pwtmpbuf
= NULL
;
716 # if defined HAVE_GETPWNAM_R || defined _LIBC
717 long int pwbuflenmax
= GETPW_R_SIZE_MAX ();
718 size_t pwbuflen
= pwbuflenmax
;
723 if (! (0 < pwbuflenmax
&& pwbuflenmax
<= SIZE_MAX
))
724 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX.
725 Try a moderate value. */
728 if (glob_use_alloca (alloca_used
, pwbuflen
))
729 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
732 pwtmpbuf
= malloc (pwbuflen
);
733 if (pwtmpbuf
== NULL
)
735 if (__glibc_unlikely (malloc_name
))
737 retval
= GLOB_NOSPACE
;
740 malloc_pwtmpbuf
= pwtmpbuf
;
743 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
753 v
= size_add_wrapv (pwbuflen
, pwbuflen
, &newlen
);
754 if (!v
&& malloc_pwtmpbuf
== NULL
755 && glob_use_alloca (alloca_used
, newlen
))
756 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
757 newlen
, alloca_used
);
760 char *newp
= (v
? NULL
761 : realloc (malloc_pwtmpbuf
, newlen
));
764 free (malloc_pwtmpbuf
);
765 if (__glibc_unlikely (malloc_name
))
767 retval
= GLOB_NOSPACE
;
770 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
778 if (__glibc_unlikely (malloc_name
))
782 if (malloc_pwtmpbuf
== NULL
)
783 home_dir
= p
->pw_dir
;
786 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
787 if (glob_use_alloca (alloca_used
, home_dir_len
))
788 home_dir
= alloca_account (home_dir_len
,
792 home_dir
= malloc (home_dir_len
);
793 if (home_dir
== NULL
)
796 retval
= GLOB_NOSPACE
;
801 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
804 free (malloc_pwtmpbuf
);
808 if (__glibc_unlikely (malloc_name
))
812 if (home_dir
== NULL
|| home_dir
[0] == '\0')
814 if (__glibc_unlikely (malloc_home_dir
))
816 if (flags
& GLOB_TILDE_CHECK
)
818 retval
= GLOB_NOMATCH
;
823 home_dir
= (char *) "~"; /* No luck. */
827 # endif /* WINDOWS32 */
829 /* Now construct the full directory. */
830 if (dirname
[1] == '\0')
832 if (__glibc_unlikely (malloc_dirname
))
836 dirlen
= strlen (dirname
);
837 malloc_dirname
= malloc_home_dir
;
842 size_t home_len
= strlen (home_dir
);
843 int use_alloca
= glob_use_alloca (alloca_used
, home_len
+ dirlen
);
845 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
848 newp
= malloc (home_len
+ dirlen
);
851 if (__glibc_unlikely (malloc_home_dir
))
853 retval
= GLOB_NOSPACE
;
858 mempcpy (mempcpy (newp
, home_dir
, home_len
),
859 &dirname
[1], dirlen
);
861 if (__glibc_unlikely (malloc_dirname
))
865 dirlen
+= home_len
- 1;
866 malloc_dirname
= !use_alloca
;
868 if (__glibc_unlikely (malloc_home_dir
))
871 dirname_modified
= 1;
873 # if !defined _AMIGA && !defined WINDOWS32
876 char *end_name
= strchr (dirname
, '/');
878 int malloc_user_name
= 0;
879 char *unescape
= NULL
;
881 if (!(flags
& GLOB_NOESCAPE
))
883 if (end_name
== NULL
)
885 unescape
= strchr (dirname
, '\\');
887 end_name
= strchr (unescape
, '\0');
890 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
892 if (end_name
== NULL
)
893 user_name
= dirname
+ 1;
897 if (glob_use_alloca (alloca_used
, end_name
- dirname
))
898 newp
= alloca_account (end_name
- dirname
, alloca_used
);
901 newp
= malloc (end_name
- dirname
);
904 retval
= GLOB_NOSPACE
;
907 malloc_user_name
= 1;
909 if (unescape
!= NULL
)
911 char *p
= mempcpy (newp
, dirname
+ 1,
912 unescape
- dirname
- 1);
920 /* "~fo\\o\\" unescape to user_name "foo\\",
921 but "~fo\\o\\/" unescape to user_name
923 if (filename
== NULL
)
934 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
939 /* Look up specific user's home directory. */
942 char *malloc_pwtmpbuf
= NULL
;
943 # if defined HAVE_GETPWNAM_R || defined _LIBC
944 long int buflenmax
= GETPW_R_SIZE_MAX ();
945 size_t buflen
= buflenmax
;
951 if (! (0 <= buflenmax
&& buflenmax
<= SIZE_MAX
))
952 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
956 if (glob_use_alloca (alloca_used
, buflen
))
957 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
960 pwtmpbuf
= malloc (buflen
);
961 if (pwtmpbuf
== NULL
)
964 if (__glibc_unlikely (malloc_user_name
))
966 retval
= GLOB_NOSPACE
;
969 malloc_pwtmpbuf
= pwtmpbuf
;
972 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
981 v
= size_add_wrapv (buflen
, buflen
, &newlen
);
982 if (!v
&& malloc_pwtmpbuf
== NULL
983 && glob_use_alloca (alloca_used
, newlen
))
984 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
985 newlen
, alloca_used
);
988 char *newp
= v
? NULL
: realloc (malloc_pwtmpbuf
, newlen
);
991 free (malloc_pwtmpbuf
);
994 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
999 p
= getpwnam (user_name
);
1002 if (__glibc_unlikely (malloc_user_name
))
1005 /* If we found a home directory use this. */
1008 size_t home_len
= strlen (p
->pw_dir
);
1009 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
1011 if (__glibc_unlikely (malloc_dirname
))
1015 if (glob_use_alloca (alloca_used
, home_len
+ rest_len
+ 1))
1016 dirname
= alloca_account (home_len
+ rest_len
+ 1,
1020 dirname
= malloc (home_len
+ rest_len
+ 1);
1021 if (dirname
== NULL
)
1023 free (malloc_pwtmpbuf
);
1024 retval
= GLOB_NOSPACE
;
1029 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
1030 end_name
, rest_len
)) = '\0';
1032 dirlen
= home_len
+ rest_len
;
1033 dirname_modified
= 1;
1035 free (malloc_pwtmpbuf
);
1039 free (malloc_pwtmpbuf
);
1041 if (flags
& GLOB_TILDE_CHECK
)
1043 /* We have to regard it as an error if we cannot find the
1045 retval
= GLOB_NOMATCH
;
1051 # endif /* Not Amiga && not WINDOWS32. */
1054 /* Now test whether we looked for "~" or "~NAME". In this case we
1055 can give the answer now. */
1056 if (filename
== NULL
)
1061 /* Return the directory if we don't check for error or if it exists. */
1062 if ((flags
& GLOB_NOCHECK
)
1063 || (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1064 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
1065 && S_ISDIR (st
.st_mode
))
1066 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
1068 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1069 char **new_gl_pathv
;
1071 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1074 free (pglob
->gl_pathv
);
1075 pglob
->gl_pathv
= NULL
;
1076 pglob
->gl_pathc
= 0;
1077 retval
= GLOB_NOSPACE
;
1081 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1082 (newcount
+ 2) * sizeof (char *));
1083 if (new_gl_pathv
== NULL
)
1085 pglob
->gl_pathv
= new_gl_pathv
;
1087 if (flags
& GLOB_MARK
)
1090 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
1091 if (pglob
->gl_pathv
[newcount
] == NULL
)
1093 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
1096 if (__glibc_unlikely (malloc_dirname
))
1101 if (__glibc_unlikely (malloc_dirname
))
1102 pglob
->gl_pathv
[newcount
] = dirname
;
1105 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
1106 if (pglob
->gl_pathv
[newcount
] == NULL
)
1110 pglob
->gl_pathv
[++newcount
] = NULL
;
1112 pglob
->gl_flags
= flags
;
1118 retval
= GLOB_NOMATCH
;
1122 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1123 /* meta is 1 if correct glob pattern containing metacharacters.
1124 If meta has bit (1 << 2) set, it means there was an unterminated
1125 [ which we handle the same, using fnmatch. Broken unterminated
1126 pattern bracket expressions ought to be rare enough that it is
1127 not worth special casing them, fnmatch will do the right thing. */
1130 /* The directory name contains metacharacters, so we
1131 have to glob for the directory, and then glob for
1132 the pattern in each directory found. */
1135 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1137 /* "foo\\/bar". Remove the final backslash from dirname
1138 if it has not been quoted. */
1139 char *p
= (char *) &dirname
[dirlen
- 1];
1141 while (p
> dirname
&& p
[-1] == '\\') --p
;
1142 if ((&dirname
[dirlen
] - p
) & 1)
1143 *(char *) &dirname
[--dirlen
] = '\0';
1146 if (__glibc_unlikely ((flags
& GLOB_ALTDIRFUNC
) != 0))
1148 /* Use the alternative access functions also in the recursive
1150 dirs
.gl_opendir
= pglob
->gl_opendir
;
1151 dirs
.gl_readdir
= pglob
->gl_readdir
;
1152 dirs
.gl_closedir
= pglob
->gl_closedir
;
1153 dirs
.gl_stat
= pglob
->gl_stat
;
1154 dirs
.gl_lstat
= pglob
->gl_lstat
;
1157 status
= glob (dirname
,
1158 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1160 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1164 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1172 /* We have successfully globbed the preceding directory name.
1173 For each name we found, call glob_in_dir on it and FILENAME,
1174 appending the results to PGLOB. */
1175 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1181 /* Make globbing interruptible in the bash shell. */
1182 extern int interrupt_state
;
1184 if (interrupt_state
)
1187 retval
= GLOB_ABORTED
;
1193 old_pathc
= pglob
->gl_pathc
;
1194 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1195 ((flags
| GLOB_APPEND
)
1196 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1197 errfunc
, pglob
, alloca_used
);
1198 if (status
== GLOB_NOMATCH
)
1199 /* No matches in this directory. Try the next. */
1206 pglob
->gl_pathc
= 0;
1211 /* Stick the directory on the front of each name. */
1212 if (prefix_array (dirs
.gl_pathv
[i
],
1213 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1214 pglob
->gl_pathc
- old_pathc
))
1218 pglob
->gl_pathc
= 0;
1219 retval
= GLOB_NOSPACE
;
1224 flags
|= GLOB_MAGCHAR
;
1226 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
1227 But if we have not found any matching entry and the GLOB_NOCHECK
1228 flag was set we must return the input pattern itself. */
1229 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1233 if (flags
& GLOB_NOCHECK
)
1235 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1236 char **new_gl_pathv
;
1238 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1242 retval
= GLOB_NOSPACE
;
1246 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1247 (newcount
+ 2) * sizeof (char *));
1248 if (new_gl_pathv
== NULL
)
1250 pglob
->gl_pathv
= new_gl_pathv
;
1252 pglob
->gl_pathv
[newcount
] = strdup (pattern
);
1253 if (pglob
->gl_pathv
[newcount
] == NULL
)
1257 pglob
->gl_pathc
= 0;
1258 retval
= GLOB_NOSPACE
;
1265 pglob
->gl_pathv
[newcount
] = NULL
;
1266 pglob
->gl_flags
= flags
;
1271 retval
= GLOB_NOMATCH
;
1280 size_t old_pathc
= pglob
->gl_pathc
;
1281 int orig_flags
= flags
;
1285 char *p
= strchr (dirname
, '\\'), *q
;
1286 /* We need to unescape the dirname string. It is certainly
1287 allocated by alloca, as otherwise filename would be NULL
1288 or dirname wouldn't contain backslashes. */
1301 while (*p
++ != '\0');
1302 dirname_modified
= 1;
1304 if (dirname_modified
)
1305 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1306 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1310 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1311 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1313 /* Make sure globfree (&dirs); is a nop. */
1314 dirs
.gl_pathv
= NULL
;
1324 /* Stick the directory on the front of each name. */
1325 if (prefix_array (dirname
,
1326 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1327 pglob
->gl_pathc
- old_pathc
))
1330 pglob
->gl_pathc
= 0;
1331 retval
= GLOB_NOSPACE
;
1337 if (flags
& GLOB_MARK
)
1339 /* Append slashes to directory names. */
1344 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1345 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1346 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1347 && S_ISDIR (st
.st_mode
))
1348 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1349 && S_ISDIR (st64
.st_mode
))))
1351 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1352 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1356 pglob
->gl_pathc
= 0;
1357 retval
= GLOB_NOSPACE
;
1360 strcpy (&new[len
- 2], "/");
1361 pglob
->gl_pathv
[i
] = new;
1365 if (!(flags
& GLOB_NOSORT
))
1367 /* Sort the vector. */
1368 qsort (&pglob
->gl_pathv
[oldcount
],
1369 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1370 sizeof (char *), collated_compare
);
1374 if (__glibc_unlikely (malloc_dirname
))
1379 #if defined _LIBC && !defined glob
1380 libc_hidden_def (glob
)
1386 /* Free storage allocated in PGLOB by a previous 'glob' call. */
1388 globfree (glob_t
*pglob
)
1390 if (pglob
->gl_pathv
!= NULL
)
1393 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1394 free (pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1395 free (pglob
->gl_pathv
);
1396 pglob
->gl_pathv
= NULL
;
1399 #if defined _LIBC && !defined globfree
1400 libc_hidden_def (globfree
)
1404 /* Do a collated comparison of A and B. */
1406 collated_compare (const void *a
, const void *b
)
1408 char *const *ps1
= a
; char *s1
= *ps1
;
1409 char *const *ps2
= b
; char *s2
= *ps2
;
1417 return strcoll (s1
, s2
);
1421 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1422 elements in place. Return nonzero if out of memory, zero if successful.
1423 A slash is inserted between DIRNAME and each elt of ARRAY,
1424 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1426 prefix_array (const char *dirname
, char **array
, size_t n
)
1429 size_t dirlen
= strlen (dirname
);
1430 #if defined __MSDOS__ || defined WINDOWS32
1432 # define DIRSEP_CHAR sep_char
1434 # define DIRSEP_CHAR '/'
1437 if (dirlen
== 1 && dirname
[0] == '/')
1438 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1439 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1441 #if defined __MSDOS__ || defined WINDOWS32
1442 else if (dirlen
> 1)
1444 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1445 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1447 else if (dirname
[dirlen
- 1] == ':')
1449 /* DIRNAME is "d:". Use ':' instead of '/'. */
1456 for (i
= 0; i
< n
; ++i
)
1458 size_t eltlen
= strlen (array
[i
]) + 1;
1459 char *new = malloc (dirlen
+ 1 + eltlen
);
1468 char *endp
= mempcpy (new, dirname
, dirlen
);
1469 *endp
++ = DIRSEP_CHAR
;
1470 mempcpy (endp
, array
[i
], eltlen
);
1480 /* We must not compile this function twice. */
1481 #ifndef NO_GLOB_PATTERN_P
1483 __glob_pattern_type (const char *pattern
, int quote
)
1488 for (p
= pattern
; *p
!= '\0'; ++p
)
1517 /* Return nonzero if PATTERN contains any metacharacters.
1518 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1520 __glob_pattern_p (const char *pattern
, int quote
)
1522 return __glob_pattern_type (pattern
, quote
) == 1;
1525 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1530 /* We put this in a separate function mainly to allow the memory
1531 allocated with alloca to be recycled. */
1533 __attribute_noinline__
1534 link_exists2_p (const char *dir
, size_t dirlen
, const char *fname
,
1536 # if !defined _LIBC && !HAVE_FSTATAT
1541 size_t fnamelen
= strlen (fname
);
1542 char *fullname
= __alloca (dirlen
+ 1 + fnamelen
+ 1);
1545 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1546 fname
, fnamelen
+ 1);
1548 # if !defined _LIBC && !HAVE_FSTATAT
1549 if (__builtin_expect ((flags
& GLOB_ALTDIRFUNC
) == 0, 1))
1552 return __stat64 (fullname
, &st64
) == 0;
1555 return (*pglob
->gl_stat
) (fullname
, &st
) == 0;
1558 /* Return true if DIR/FNAME exists. */
1560 link_exists_p (int dfd
, const char *dir
, size_t dirlen
, const char *fname
,
1561 glob_t
*pglob
, int flags
)
1563 # if defined _LIBC || HAVE_FSTATAT
1564 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1565 return link_exists2_p (dir
, dirlen
, fname
, pglob
);
1568 /* dfd cannot be -1 here, because dirfd never returns -1 on
1569 glibc, or on hosts that have fstatat. */
1571 return __fxstatat64 (_STAT_VER
, dfd
, fname
, &st64
, 0) == 0;
1574 return link_exists2_p (dir
, dirlen
, fname
, pglob
, flags
);
1577 #endif /* !defined GLOB_ONLY_P */
1580 /* Like 'glob', but PATTERN is a final pathname component,
1581 and matches are searched for in DIRECTORY.
1582 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1583 The GLOB_APPEND flag is assumed to be set (always appends). */
1585 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1586 int (*errfunc
) (const char *, int),
1587 glob_t
*pglob
, size_t alloca_used
)
1589 size_t dirlen
= strlen (directory
);
1590 void *stream
= NULL
;
1593 struct globnames
*next
;
1597 struct globnames init_names
;
1598 struct globnames
*names
= &init_names
;
1599 struct globnames
*names_alloca
= &init_names
;
1606 alloca_used
+= sizeof (init_names
);
1608 init_names
.next
= NULL
;
1609 init_names
.count
= sizeof init_names
.name
/ sizeof init_names
.name
[0];
1611 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1612 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1614 /* We need not do any tests. The PATTERN contains no meta
1615 characters and we must not return an error therefore the
1616 result will always contain exactly one name. */
1617 flags
|= GLOB_NOCHECK
;
1621 /* Since we use the normal file functions we can also use stat()
1622 to verify the file is there. */
1628 size_t patlen
= strlen (pattern
);
1630 bool alloca_fullname
1631 = (! size_add_wrapv (dirlen
+ 1, patlen
+ 1, &fullsize
)
1632 && glob_use_alloca (alloca_used
, fullsize
));
1634 if (alloca_fullname
)
1635 fullname
= alloca_account (fullsize
, alloca_used
);
1638 fullname
= malloc (fullsize
);
1639 if (fullname
== NULL
)
1640 return GLOB_NOSPACE
;
1643 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1645 pattern
, patlen
+ 1);
1646 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1647 ? (*pglob
->gl_stat
) (fullname
, &ust
.st
)
1648 : __stat64 (fullname
, &ust
.st64
)) == 0)
1649 /* We found this file to be existing. Now tell the rest
1650 of the function to copy this name into the result. */
1651 flags
|= GLOB_NOCHECK
;
1653 if (__glibc_unlikely (!alloca_fullname
))
1658 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1659 ? (*pglob
->gl_opendir
) (directory
)
1660 : opendir (directory
));
1663 if (errno
!= ENOTDIR
1664 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1665 || (flags
& GLOB_ERR
)))
1666 return GLOB_ABORTED
;
1670 int dfd
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1671 ? -1 : dirfd ((DIR *) stream
));
1672 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1673 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1674 #if defined _AMIGA || defined VMS
1678 flags
|= GLOB_MAGCHAR
;
1682 struct readdir_result d
;
1684 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1685 d
= convert_dirent (GL_READDIR (pglob
, stream
));
1688 #ifdef COMPILE_GLOB64
1689 d
= convert_dirent (__readdir (stream
));
1691 d
= convert_dirent64 (__readdir64 (stream
));
1700 /* If we shall match only directories use the information
1701 provided by the dirent call if possible. */
1702 if ((flags
& GLOB_ONLYDIR
) && !readdir_result_might_be_dir (d
))
1705 if (fnmatch (pattern
, d
.name
, fnm_flags
) == 0)
1707 /* If the file we found is a symlink we have to
1708 make sure the target file exists. */
1709 if (!readdir_result_might_be_symlink (d
)
1710 || link_exists_p (dfd
, directory
, dirlen
, d
.name
,
1713 if (cur
== names
->count
)
1715 struct globnames
*newnames
;
1716 size_t count
= names
->count
* 2;
1717 size_t nameoff
= offsetof (struct globnames
, name
);
1718 size_t size
= FLEXSIZEOF (struct globnames
, name
,
1719 count
* sizeof (char *));
1720 if ((SIZE_MAX
- nameoff
) / 2 / sizeof (char *)
1723 if (glob_use_alloca (alloca_used
, size
))
1724 newnames
= names_alloca
1725 = alloca_account (size
, alloca_used
);
1726 else if ((newnames
= malloc (size
))
1729 newnames
->count
= count
;
1730 newnames
->next
= names
;
1734 names
->name
[cur
] = strdup (d
.name
);
1735 if (names
->name
[cur
] == NULL
)
1739 if (SIZE_MAX
- pglob
->gl_offs
<= nfound
)
1747 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1749 size_t len
= strlen (pattern
);
1751 names
->name
[cur
] = malloc (len
+ 1);
1752 if (names
->name
[cur
] == NULL
)
1754 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1757 result
= GLOB_NOMATCH
;
1760 char **new_gl_pathv
;
1763 if (SIZE_MAX
/ sizeof (char *) - pglob
->gl_pathc
1764 < pglob
->gl_offs
+ nfound
+ 1)
1768 = realloc (pglob
->gl_pathv
,
1769 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1772 if (new_gl_pathv
== NULL
)
1777 struct globnames
*old
= names
;
1779 for (i
= 0; i
< cur
; ++i
)
1780 free (names
->name
[i
]);
1781 names
= names
->next
;
1782 /* NB: we will not leak memory here if we exit without
1783 freeing the current block assigned to OLD. At least
1784 the very first block is always allocated on the stack
1785 and this is the block assigned to OLD here. */
1788 assert (old
== &init_names
);
1792 if (old
== names_alloca
)
1793 names_alloca
= names
;
1797 result
= GLOB_NOSPACE
;
1803 struct globnames
*old
= names
;
1805 for (i
= 0; i
< cur
; ++i
)
1806 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1808 names
= names
->next
;
1809 /* NB: we will not leak memory here if we exit without
1810 freeing the current block assigned to OLD. At least
1811 the very first block is always allocated on the stack
1812 and this is the block assigned to OLD here. */
1815 assert (old
== &init_names
);
1819 if (old
== names_alloca
)
1820 names_alloca
= names
;
1825 pglob
->gl_pathv
= new_gl_pathv
;
1827 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1829 pglob
->gl_flags
= flags
;
1836 if (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
))
1837 (*pglob
->gl_closedir
) (stream
);