1 /* Copyright (C) 1991-2016 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 General Public
6 License as published by the Free Software Foundation; either
7 version 3 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 General Public License for more details.
14 You should have received a copy of the GNU 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
, };
192 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
196 #ifndef COMPILE_GLOB64
197 /* Like convert_dirent, but works on struct dirent64 instead. Keep in
198 sync with convert_dirent above. */
199 static struct readdir_result
200 convert_dirent64 (const struct dirent64
*source
)
204 struct readdir_result result
= { NULL
, };
207 struct readdir_result result
= READDIR_RESULT_INITIALIZER (source
);
213 #ifndef attribute_hidden
214 # define attribute_hidden
217 #ifndef __attribute_noinline__
218 # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1)
219 # define __attribute_noinline__ /* Ignore */
221 # define __attribute_noinline__ __attribute__ ((__noinline__))
225 #if ! defined __builtin_expect && __GNUC__ < 3
226 # define __builtin_expect(expr, expected) (expr)
229 #ifndef __glibc_unlikely
230 # define __glibc_unlikely(expr) __builtin_expect (expr, 0)
234 /* The results of opendir() in this file are not used with dirfd and fchdir,
235 and we do not leak fds to any single-threaded code that could use stdio,
236 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
237 FIXME - if the kernel ever adds support for multi-thread safety for
238 avoiding standard fds, then we should use opendir_safer. */
239 # ifdef GNULIB_defined_opendir
242 # ifdef GNULIB_defined_closedir
246 /* Just use malloc. */
247 # define __libc_use_alloca(n) false
248 # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0)
249 # define extend_alloca_account(buf, len, newlen, avar) \
250 ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0)
253 #ifndef __has_builtin
254 # define __has_builtin(x) 0
257 /* Set *R = A + B. Return true if the answer is mathematically
258 incorrect due to overflow; in this case, *R is the low order
259 bits of the correct answer.. */
262 size_add_wrapv (size_t a
, size_t b
, size_t *r
)
264 #if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
265 return __builtin_add_overflow (a
, b
, r
);
273 glob_use_alloca (size_t alloca_used
, size_t len
)
276 return (!size_add_wrapv (alloca_used
, len
, &size
)
277 && __libc_use_alloca (size
));
280 static int glob_in_dir (const char *pattern
, const char *directory
,
281 int flags
, int (*errfunc
) (const char *, int),
282 glob_t
*pglob
, size_t alloca_used
);
283 extern int __glob_pattern_type (const char *pattern
, int quote
)
287 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROWNL
;
288 static int collated_compare (const void *, const void *) __THROWNL
;
291 /* Find the end of the sub-pattern in a brace expression. */
293 next_brace_sub (const char *cp
, int flags
)
297 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
305 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
312 return *cp
!= '\0' ? cp
: NULL
;
315 #endif /* !defined GLOB_ONLY_P */
317 /* Do glob searching for PATTERN, placing results in PGLOB.
318 The bits defined above may be set in FLAGS.
319 If a directory cannot be opened or read and ERRFUNC is not nil,
320 it is called with the pathname that caused the error, and the
321 'errno' value from the failing call; if it returns non-zero
322 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
323 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
324 Otherwise, 'glob' returns zero. */
326 #ifdef GLOB_ATTRIBUTE
329 glob (const char *pattern
, int flags
, int (*errfunc
) (const char *, int),
332 const char *filename
;
333 char *dirname
= NULL
;
338 int dirname_modified
;
339 int malloc_dirname
= 0;
342 size_t alloca_used
= 0;
344 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
346 __set_errno (EINVAL
);
350 /* POSIX requires all slashes to be matched. This means that with
351 a trailing slash we must match only directories. */
352 if (pattern
[0] && pattern
[strlen (pattern
) - 1] == '/')
353 flags
|= GLOB_ONLYDIR
;
355 if (!(flags
& GLOB_DOOFFS
))
356 /* Have to do this so 'globfree' knows where to start freeing. It
357 also makes all the code that uses gl_offs simpler. */
360 if (flags
& GLOB_BRACE
)
364 if (flags
& GLOB_NOESCAPE
)
365 begin
= strchr (pattern
, '{');
377 if (*begin
== '\\' && begin
[1] != '\0')
379 else if (*begin
== '{')
388 /* Allocate working buffer large enough for our work. Note that
389 we have at least an opening and closing brace. */
397 size_t pattern_len
= strlen (pattern
) - 1;
398 int alloca_onealt
= glob_use_alloca (alloca_used
, pattern_len
);
400 onealt
= alloca_account (pattern_len
, alloca_used
);
403 onealt
= malloc (pattern_len
);
406 if (!(flags
& GLOB_APPEND
))
409 pglob
->gl_pathv
= NULL
;
415 /* We know the prefix for all sub-patterns. */
416 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
418 /* Find the first sub-pattern and at the same time find the
419 rest after the closing brace. */
420 next
= next_brace_sub (begin
+ 1, flags
);
423 /* It is an invalid expression. */
425 if (__glibc_unlikely (!alloca_onealt
))
427 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
430 /* Now find the end of the whole brace expression. */
434 rest
= next_brace_sub (rest
+ 1, flags
);
436 /* It is an illegal expression. */
439 /* Please note that we now can be sure the brace expression
441 rest_len
= strlen (++rest
) + 1;
443 /* We have a brace expression. BEGIN points to the opening {,
444 NEXT points past the terminator of the first element, and END
445 points past the final }. We will accumulate result names from
446 recursive runs for each brace alternative in the buffer using
449 if (!(flags
& GLOB_APPEND
))
451 /* This call is to set a new vector, so clear out the
452 vector so we can append to it. */
454 pglob
->gl_pathv
= NULL
;
456 firstc
= pglob
->gl_pathc
;
463 /* Construct the new glob expression. */
464 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
466 result
= glob (onealt
,
467 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
468 | GLOB_APPEND
), errfunc
, pglob
);
470 /* If we got an error, return it. */
471 if (result
&& result
!= GLOB_NOMATCH
)
473 if (__glibc_unlikely (!alloca_onealt
))
475 if (!(flags
& GLOB_APPEND
))
484 /* We saw the last entry. */
488 next
= next_brace_sub (p
, flags
);
489 assert (next
!= NULL
);
492 if (__glibc_unlikely (!alloca_onealt
))
495 if (pglob
->gl_pathc
!= firstc
)
496 /* We found some entries. */
498 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
503 if (!(flags
& GLOB_APPEND
))
506 if (!(flags
& GLOB_DOOFFS
))
507 pglob
->gl_pathv
= NULL
;
512 if (pglob
->gl_offs
>= SIZE_MAX
/ sizeof (char *))
515 pglob
->gl_pathv
= malloc ((pglob
->gl_offs
+ 1) * sizeof (char *));
516 if (pglob
->gl_pathv
== NULL
)
519 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
520 pglob
->gl_pathv
[i
] = NULL
;
524 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
526 /* Find the filename. */
527 filename
= strrchr (pattern
, '/');
528 #if defined __MSDOS__ || defined WINDOWS32
529 /* The case of "d:pattern". Since ':' is not allowed in
530 file names, we can safely assume that wherever it
531 happens in pattern, it signals the filename part. This
532 is so we could some day support patterns like "[a-z]:foo". */
533 if (filename
== NULL
)
534 filename
= strchr (pattern
, ':');
535 #endif /* __MSDOS__ || WINDOWS32 */
536 dirname_modified
= 0;
537 if (filename
== NULL
)
539 /* This can mean two things: a simple name or "~name". The latter
540 case is nothing but a notation for a directory. */
541 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
543 dirname
= (char *) pattern
;
544 dirlen
= strlen (pattern
);
546 /* Set FILENAME to NULL as a special flag. This is ugly but
547 other solutions would require much more code. We test for
548 this special case below. */
553 if (__glibc_unlikely (pattern
[0] == '\0'))
555 dirs
.gl_pathv
= NULL
;
561 dirname
= (char *) "";
563 dirname
= (char *) ".";
568 else if (filename
== pattern
569 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
570 && (flags
& GLOB_NOESCAPE
) == 0))
572 /* "/pattern" or "\\/pattern". */
573 dirname
= (char *) "/";
580 dirlen
= filename
- pattern
;
581 #if defined __MSDOS__ || defined WINDOWS32
583 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
588 drive_spec
= __alloca (dirlen
+ 1);
589 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
590 /* For now, disallow wildcards in the drive spec, to
591 prevent infinite recursion in glob. */
592 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
594 /* If this is "d:pattern", we need to copy ':' to DIRNAME
595 as well. If it's "d:/pattern", don't remove the slash
596 from "d:/", since "d:" and "d:/" are not the same.*/
599 if (glob_use_alloca (alloca_used
, dirlen
+ 1))
600 newp
= alloca_account (dirlen
+ 1, alloca_used
);
603 newp
= malloc (dirlen
+ 1);
608 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
612 if (filename
[0] == '\0'
613 #if defined __MSDOS__ || defined WINDOWS32
614 && dirname
[dirlen
- 1] != ':'
615 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
616 || dirname
[dirlen
- 1] != '/')
619 /* "pattern/". Expand "pattern", appending slashes. */
621 int orig_flags
= flags
;
623 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
625 /* "pattern\\/". Remove the final backslash if it hasn't
627 char *p
= (char *) &dirname
[dirlen
- 1];
629 while (p
> dirname
&& p
[-1] == '\\') --p
;
630 if ((&dirname
[dirlen
] - p
) & 1)
632 *(char *) &dirname
[--dirlen
] = '\0';
633 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
636 val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
638 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
639 | (flags
& GLOB_MARK
));
640 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
642 /* Make sure globfree (&dirs); is a nop. */
643 dirs
.gl_pathv
= NULL
;
645 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
653 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
655 if (dirname
[1] == '\0' || dirname
[1] == '/'
656 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
657 && (dirname
[2] == '\0' || dirname
[2] == '/')))
659 /* Look up home directory. */
660 char *home_dir
= getenv ("HOME");
661 int malloc_home_dir
= 0;
663 if (home_dir
== NULL
|| home_dir
[0] == '\0')
667 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference
668 to HOME, because the user can change HOME. */
669 if (home_dir
== NULL
|| home_dir
[0] == '\0')
671 const char *home_drive
= getenv ("HOMEDRIVE");
672 const char *home_path
= getenv ("HOMEPATH");
674 if (home_drive
!= NULL
&& home_path
!= NULL
)
676 size_t home_drive_len
= strlen (home_drive
);
677 size_t home_path_len
= strlen (home_path
);
678 char *mem
= alloca (home_drive_len
+ home_path_len
+ 1);
680 memcpy (mem
, home_drive
, home_drive_len
);
681 memcpy (mem
+ home_drive_len
, home_path
, home_path_len
+ 1);
685 home_dir
= "c:/users/default"; /* poor default */
688 if (home_dir
== NULL
|| home_dir
[0] == '\0')
693 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
696 /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try
699 if (glob_use_alloca (alloca_used
, buflen
))
700 name
= alloca_account (buflen
, alloca_used
);
703 name
= malloc (buflen
);
706 retval
= GLOB_NOSPACE
;
712 success
= __getlogin_r (name
, buflen
) == 0;
716 # if defined HAVE_GETPWNAM_R || defined _LIBC
717 long int pwbuflenmax
= GETPW_R_SIZE_MAX ();
718 size_t pwbuflen
= pwbuflenmax
;
721 char *malloc_pwtmpbuf
= NULL
;
725 if (! (0 < pwbuflenmax
&& pwbuflenmax
<= SIZE_MAX
))
726 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX.
727 Try a moderate value. */
730 if (glob_use_alloca (alloca_used
, pwbuflen
))
731 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
734 pwtmpbuf
= malloc (pwbuflen
);
735 if (pwtmpbuf
== NULL
)
737 if (__glibc_unlikely (malloc_name
))
739 retval
= GLOB_NOSPACE
;
742 malloc_pwtmpbuf
= pwtmpbuf
;
745 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
755 v
= size_add_wrapv (pwbuflen
, pwbuflen
, &newlen
);
756 if (!v
&& malloc_pwtmpbuf
== NULL
757 && glob_use_alloca (alloca_used
, newlen
))
758 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
759 newlen
, alloca_used
);
762 char *newp
= (v
? NULL
763 : realloc (malloc_pwtmpbuf
, newlen
));
766 free (malloc_pwtmpbuf
);
767 if (__glibc_unlikely (malloc_name
))
769 retval
= GLOB_NOSPACE
;
772 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
780 if (__glibc_unlikely (malloc_name
))
784 if (malloc_pwtmpbuf
== NULL
)
785 home_dir
= p
->pw_dir
;
788 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
789 if (glob_use_alloca (alloca_used
, home_dir_len
))
790 home_dir
= alloca_account (home_dir_len
,
794 home_dir
= malloc (home_dir_len
);
795 if (home_dir
== NULL
)
798 retval
= GLOB_NOSPACE
;
803 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
806 free (malloc_pwtmpbuf
);
810 if (__glibc_unlikely (malloc_name
))
814 if (home_dir
== NULL
|| home_dir
[0] == '\0')
816 if (__glibc_unlikely (malloc_home_dir
))
818 if (flags
& GLOB_TILDE_CHECK
)
820 retval
= GLOB_NOMATCH
;
825 home_dir
= (char *) "~"; /* No luck. */
829 # endif /* WINDOWS32 */
831 /* Now construct the full directory. */
832 if (dirname
[1] == '\0')
834 if (__glibc_unlikely (malloc_dirname
))
838 dirlen
= strlen (dirname
);
839 malloc_dirname
= malloc_home_dir
;
844 size_t home_len
= strlen (home_dir
);
845 int use_alloca
= glob_use_alloca (alloca_used
, home_len
+ dirlen
);
847 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
850 newp
= malloc (home_len
+ dirlen
);
853 if (__glibc_unlikely (malloc_home_dir
))
855 retval
= GLOB_NOSPACE
;
860 mempcpy (mempcpy (newp
, home_dir
, home_len
),
861 &dirname
[1], dirlen
);
863 if (__glibc_unlikely (malloc_dirname
))
867 dirlen
+= home_len
- 1;
868 malloc_dirname
= !use_alloca
;
870 if (__glibc_unlikely (malloc_home_dir
))
873 dirname_modified
= 1;
875 # if !defined _AMIGA && !defined WINDOWS32
878 char *end_name
= strchr (dirname
, '/');
880 int malloc_user_name
= 0;
881 char *unescape
= NULL
;
883 if (!(flags
& GLOB_NOESCAPE
))
885 if (end_name
== NULL
)
887 unescape
= strchr (dirname
, '\\');
889 end_name
= strchr (unescape
, '\0');
892 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
894 if (end_name
== NULL
)
895 user_name
= dirname
+ 1;
899 if (glob_use_alloca (alloca_used
, end_name
- dirname
))
900 newp
= alloca_account (end_name
- dirname
, alloca_used
);
903 newp
= malloc (end_name
- dirname
);
906 retval
= GLOB_NOSPACE
;
909 malloc_user_name
= 1;
911 if (unescape
!= NULL
)
913 char *p
= mempcpy (newp
, dirname
+ 1,
914 unescape
- dirname
- 1);
922 /* "~fo\\o\\" unescape to user_name "foo\\",
923 but "~fo\\o\\/" unescape to user_name
925 if (filename
== NULL
)
936 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
941 /* Look up specific user's home directory. */
944 # if defined HAVE_GETPWNAM_R || defined _LIBC
945 long int buflenmax
= GETPW_R_SIZE_MAX ();
946 size_t buflen
= buflenmax
;
948 char *malloc_pwtmpbuf
= NULL
;
953 if (! (0 <= buflenmax
&& buflenmax
<= SIZE_MAX
))
954 /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
958 if (glob_use_alloca (alloca_used
, buflen
))
959 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
962 pwtmpbuf
= malloc (buflen
);
963 if (pwtmpbuf
== NULL
)
966 if (__glibc_unlikely (malloc_user_name
))
968 retval
= GLOB_NOSPACE
;
971 malloc_pwtmpbuf
= pwtmpbuf
;
974 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
983 v
= size_add_wrapv (buflen
, buflen
, &newlen
);
984 if (!v
&& malloc_pwtmpbuf
== NULL
985 && glob_use_alloca (alloca_used
, newlen
))
986 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
987 newlen
, alloca_used
);
990 char *newp
= v
? NULL
: realloc (malloc_pwtmpbuf
, newlen
);
993 free (malloc_pwtmpbuf
);
996 malloc_pwtmpbuf
= pwtmpbuf
= newp
;
1001 p
= getpwnam (user_name
);
1004 if (__glibc_unlikely (malloc_user_name
))
1007 /* If we found a home directory use this. */
1010 size_t home_len
= strlen (p
->pw_dir
);
1011 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
1013 if (__glibc_unlikely (malloc_dirname
))
1017 if (glob_use_alloca (alloca_used
, home_len
+ rest_len
+ 1))
1018 dirname
= alloca_account (home_len
+ rest_len
+ 1,
1022 dirname
= malloc (home_len
+ rest_len
+ 1);
1023 if (dirname
== NULL
)
1025 free (malloc_pwtmpbuf
);
1026 retval
= GLOB_NOSPACE
;
1031 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
1032 end_name
, rest_len
)) = '\0';
1034 dirlen
= home_len
+ rest_len
;
1035 dirname_modified
= 1;
1037 free (malloc_pwtmpbuf
);
1041 free (malloc_pwtmpbuf
);
1043 if (flags
& GLOB_TILDE_CHECK
)
1045 /* We have to regard it as an error if we cannot find the
1047 retval
= GLOB_NOMATCH
;
1053 # endif /* Not Amiga && not WINDOWS32. */
1056 /* Now test whether we looked for "~" or "~NAME". In this case we
1057 can give the answer now. */
1058 if (filename
== NULL
)
1063 /* Return the directory if we don't check for error or if it exists. */
1064 if ((flags
& GLOB_NOCHECK
)
1065 || (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1066 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
1067 && S_ISDIR (st
.st_mode
))
1068 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
1070 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1071 char **new_gl_pathv
;
1073 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1076 free (pglob
->gl_pathv
);
1077 pglob
->gl_pathv
= NULL
;
1078 pglob
->gl_pathc
= 0;
1079 retval
= GLOB_NOSPACE
;
1083 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1084 (newcount
+ 2) * sizeof (char *));
1085 if (new_gl_pathv
== NULL
)
1087 pglob
->gl_pathv
= new_gl_pathv
;
1089 if (flags
& GLOB_MARK
)
1092 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
1093 if (pglob
->gl_pathv
[newcount
] == NULL
)
1095 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
1098 if (__glibc_unlikely (malloc_dirname
))
1103 if (__glibc_unlikely (malloc_dirname
))
1104 pglob
->gl_pathv
[newcount
] = dirname
;
1107 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
1108 if (pglob
->gl_pathv
[newcount
] == NULL
)
1112 pglob
->gl_pathv
[++newcount
] = NULL
;
1114 pglob
->gl_flags
= flags
;
1120 retval
= GLOB_NOMATCH
;
1124 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1125 /* meta is 1 if correct glob pattern containing metacharacters.
1126 If meta has bit (1 << 2) set, it means there was an unterminated
1127 [ which we handle the same, using fnmatch. Broken unterminated
1128 pattern bracket expressions ought to be rare enough that it is
1129 not worth special casing them, fnmatch will do the right thing. */
1132 /* The directory name contains metacharacters, so we
1133 have to glob for the directory, and then glob for
1134 the pattern in each directory found. */
1137 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1139 /* "foo\\/bar". Remove the final backslash from dirname
1140 if it has not been quoted. */
1141 char *p
= (char *) &dirname
[dirlen
- 1];
1143 while (p
> dirname
&& p
[-1] == '\\') --p
;
1144 if ((&dirname
[dirlen
] - p
) & 1)
1145 *(char *) &dirname
[--dirlen
] = '\0';
1148 if (__glibc_unlikely ((flags
& GLOB_ALTDIRFUNC
) != 0))
1150 /* Use the alternative access functions also in the recursive
1152 dirs
.gl_opendir
= pglob
->gl_opendir
;
1153 dirs
.gl_readdir
= pglob
->gl_readdir
;
1154 dirs
.gl_closedir
= pglob
->gl_closedir
;
1155 dirs
.gl_stat
= pglob
->gl_stat
;
1156 dirs
.gl_lstat
= pglob
->gl_lstat
;
1159 status
= glob (dirname
,
1160 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1162 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1166 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1174 /* We have successfully globbed the preceding directory name.
1175 For each name we found, call glob_in_dir on it and FILENAME,
1176 appending the results to PGLOB. */
1177 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1183 /* Make globbing interruptible in the bash shell. */
1184 extern int interrupt_state
;
1186 if (interrupt_state
)
1189 retval
= GLOB_ABORTED
;
1195 old_pathc
= pglob
->gl_pathc
;
1196 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1197 ((flags
| GLOB_APPEND
)
1198 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1199 errfunc
, pglob
, alloca_used
);
1200 if (status
== GLOB_NOMATCH
)
1201 /* No matches in this directory. Try the next. */
1208 pglob
->gl_pathc
= 0;
1213 /* Stick the directory on the front of each name. */
1214 if (prefix_array (dirs
.gl_pathv
[i
],
1215 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1216 pglob
->gl_pathc
- old_pathc
))
1220 pglob
->gl_pathc
= 0;
1221 retval
= GLOB_NOSPACE
;
1226 flags
|= GLOB_MAGCHAR
;
1228 /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls.
1229 But if we have not found any matching entry and the GLOB_NOCHECK
1230 flag was set we must return the input pattern itself. */
1231 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1235 if (flags
& GLOB_NOCHECK
)
1237 size_t newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1238 char **new_gl_pathv
;
1240 if (newcount
> SIZE_MAX
/ sizeof (char *) - 2)
1244 retval
= GLOB_NOSPACE
;
1248 new_gl_pathv
= realloc (pglob
->gl_pathv
,
1249 (newcount
+ 2) * sizeof (char *));
1250 if (new_gl_pathv
== NULL
)
1252 pglob
->gl_pathv
= new_gl_pathv
;
1254 pglob
->gl_pathv
[newcount
] = strdup (pattern
);
1255 if (pglob
->gl_pathv
[newcount
] == NULL
)
1259 pglob
->gl_pathc
= 0;
1260 retval
= GLOB_NOSPACE
;
1267 pglob
->gl_pathv
[newcount
] = NULL
;
1268 pglob
->gl_flags
= flags
;
1273 retval
= GLOB_NOMATCH
;
1282 size_t old_pathc
= pglob
->gl_pathc
;
1283 int orig_flags
= flags
;
1287 char *p
= strchr (dirname
, '\\'), *q
;
1288 /* We need to unescape the dirname string. It is certainly
1289 allocated by alloca, as otherwise filename would be NULL
1290 or dirname wouldn't contain backslashes. */
1303 while (*p
++ != '\0');
1304 dirname_modified
= 1;
1306 if (dirname_modified
)
1307 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1308 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1312 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1313 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1315 /* Make sure globfree (&dirs); is a nop. */
1316 dirs
.gl_pathv
= NULL
;
1326 /* Stick the directory on the front of each name. */
1327 if (prefix_array (dirname
,
1328 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1329 pglob
->gl_pathc
- old_pathc
))
1332 pglob
->gl_pathc
= 0;
1333 retval
= GLOB_NOSPACE
;
1339 if (flags
& GLOB_MARK
)
1341 /* Append slashes to directory names. */
1346 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1347 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1348 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1349 && S_ISDIR (st
.st_mode
))
1350 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1351 && S_ISDIR (st64
.st_mode
))))
1353 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1354 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1358 pglob
->gl_pathc
= 0;
1359 retval
= GLOB_NOSPACE
;
1362 strcpy (&new[len
- 2], "/");
1363 pglob
->gl_pathv
[i
] = new;
1367 if (!(flags
& GLOB_NOSORT
))
1369 /* Sort the vector. */
1370 qsort (&pglob
->gl_pathv
[oldcount
],
1371 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1372 sizeof (char *), collated_compare
);
1376 if (__glibc_unlikely (malloc_dirname
))
1381 #if defined _LIBC && !defined glob
1382 libc_hidden_def (glob
)
1388 /* Free storage allocated in PGLOB by a previous 'glob' call. */
1390 globfree (glob_t
*pglob
)
1392 if (pglob
->gl_pathv
!= NULL
)
1395 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1396 free (pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1397 free (pglob
->gl_pathv
);
1398 pglob
->gl_pathv
= NULL
;
1401 #if defined _LIBC && !defined globfree
1402 libc_hidden_def (globfree
)
1406 /* Do a collated comparison of A and B. */
1408 collated_compare (const void *a
, const void *b
)
1410 char *const *ps1
= a
; char *s1
= *ps1
;
1411 char *const *ps2
= b
; char *s2
= *ps2
;
1419 return strcoll (s1
, s2
);
1423 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1424 elements in place. Return nonzero if out of memory, zero if successful.
1425 A slash is inserted between DIRNAME and each elt of ARRAY,
1426 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1428 prefix_array (const char *dirname
, char **array
, size_t n
)
1431 size_t dirlen
= strlen (dirname
);
1432 #if defined __MSDOS__ || defined WINDOWS32
1434 # define DIRSEP_CHAR sep_char
1436 # define DIRSEP_CHAR '/'
1439 if (dirlen
== 1 && dirname
[0] == '/')
1440 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1441 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1443 #if defined __MSDOS__ || defined WINDOWS32
1444 else if (dirlen
> 1)
1446 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1447 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1449 else if (dirname
[dirlen
- 1] == ':')
1451 /* DIRNAME is "d:". Use ':' instead of '/'. */
1458 for (i
= 0; i
< n
; ++i
)
1460 size_t eltlen
= strlen (array
[i
]) + 1;
1461 char *new = malloc (dirlen
+ 1 + eltlen
);
1470 char *endp
= mempcpy (new, dirname
, dirlen
);
1471 *endp
++ = DIRSEP_CHAR
;
1472 mempcpy (endp
, array
[i
], eltlen
);
1482 /* We must not compile this function twice. */
1483 #ifndef NO_GLOB_PATTERN_P
1485 __glob_pattern_type (const char *pattern
, int quote
)
1490 for (p
= pattern
; *p
!= '\0'; ++p
)
1519 /* Return nonzero if PATTERN contains any metacharacters.
1520 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1522 __glob_pattern_p (const char *pattern
, int quote
)
1524 return __glob_pattern_type (pattern
, quote
) == 1;
1527 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1532 /* We put this in a separate function mainly to allow the memory
1533 allocated with alloca to be recycled. */
1535 __attribute_noinline__
1536 link_exists2_p (const char *dir
, size_t dirlen
, const char *fname
,
1538 # if !defined _LIBC && !HAVE_FSTATAT
1543 size_t fnamelen
= strlen (fname
);
1544 char *fullname
= __alloca (dirlen
+ 1 + fnamelen
+ 1);
1547 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1548 fname
, fnamelen
+ 1);
1550 # if !defined _LIBC && !HAVE_FSTATAT
1551 if (__builtin_expect ((flags
& GLOB_ALTDIRFUNC
) == 0, 1))
1554 return __stat64 (fullname
, &st64
) == 0;
1557 return (*pglob
->gl_stat
) (fullname
, &st
) == 0;
1560 /* Return true if DIR/FNAME exists. */
1562 link_exists_p (int dfd
, const char *dir
, size_t dirlen
, const char *fname
,
1563 glob_t
*pglob
, int flags
)
1565 # if defined _LIBC || HAVE_FSTATAT
1566 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1567 return link_exists2_p (dir
, dirlen
, fname
, pglob
);
1570 /* dfd cannot be -1 here, because dirfd never returns -1 on
1571 glibc, or on hosts that have fstatat. */
1573 return __fxstatat64 (_STAT_VER
, dfd
, fname
, &st64
, 0) == 0;
1576 return link_exists2_p (dir
, dirlen
, fname
, pglob
, flags
);
1579 #endif /* !defined GLOB_ONLY_P */
1582 /* Like 'glob', but PATTERN is a final pathname component,
1583 and matches are searched for in DIRECTORY.
1584 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1585 The GLOB_APPEND flag is assumed to be set (always appends). */
1587 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1588 int (*errfunc
) (const char *, int),
1589 glob_t
*pglob
, size_t alloca_used
)
1591 size_t dirlen
= strlen (directory
);
1592 void *stream
= NULL
;
1595 struct globnames
*next
;
1599 struct globnames init_names
;
1600 struct globnames
*names
= &init_names
;
1601 struct globnames
*names_alloca
= &init_names
;
1608 alloca_used
+= sizeof (init_names
);
1610 init_names
.next
= NULL
;
1611 init_names
.count
= sizeof init_names
.name
/ sizeof init_names
.name
[0];
1613 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1614 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1616 /* We need not do any tests. The PATTERN contains no meta
1617 characters and we must not return an error therefore the
1618 result will always contain exactly one name. */
1619 flags
|= GLOB_NOCHECK
;
1623 /* Since we use the normal file functions we can also use stat()
1624 to verify the file is there. */
1630 size_t patlen
= strlen (pattern
);
1632 bool alloca_fullname
1633 = (! size_add_wrapv (dirlen
+ 1, patlen
+ 1, &fullsize
)
1634 && glob_use_alloca (alloca_used
, fullsize
));
1636 if (alloca_fullname
)
1637 fullname
= alloca_account (fullsize
, alloca_used
);
1640 fullname
= malloc (fullsize
);
1641 if (fullname
== NULL
)
1642 return GLOB_NOSPACE
;
1645 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1647 pattern
, patlen
+ 1);
1648 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1649 ? (*pglob
->gl_stat
) (fullname
, &ust
.st
)
1650 : __stat64 (fullname
, &ust
.st64
)) == 0)
1651 /* We found this file to be existing. Now tell the rest
1652 of the function to copy this name into the result. */
1653 flags
|= GLOB_NOCHECK
;
1655 if (__glibc_unlikely (!alloca_fullname
))
1660 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1661 ? (*pglob
->gl_opendir
) (directory
)
1662 : opendir (directory
));
1665 if (errno
!= ENOTDIR
1666 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1667 || (flags
& GLOB_ERR
)))
1668 return GLOB_ABORTED
;
1672 int dfd
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1673 ? -1 : dirfd ((DIR *) stream
));
1674 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1675 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1676 #if defined _AMIGA || defined VMS
1680 flags
|= GLOB_MAGCHAR
;
1684 struct readdir_result d
;
1686 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1687 d
= convert_dirent (GL_READDIR (pglob
, stream
));
1690 #ifdef COMPILE_GLOB64
1691 d
= convert_dirent (__readdir (stream
));
1693 d
= convert_dirent64 (__readdir64 (stream
));
1702 /* If we shall match only directories use the information
1703 provided by the dirent call if possible. */
1704 if ((flags
& GLOB_ONLYDIR
) && !readdir_result_might_be_dir (d
))
1707 if (fnmatch (pattern
, d
.name
, fnm_flags
) == 0)
1709 /* If the file we found is a symlink we have to
1710 make sure the target file exists. */
1711 if (!readdir_result_might_be_symlink (d
)
1712 || link_exists_p (dfd
, directory
, dirlen
, d
.name
,
1715 if (cur
== names
->count
)
1717 struct globnames
*newnames
;
1718 size_t count
= names
->count
* 2;
1719 size_t nameoff
= offsetof (struct globnames
, name
);
1720 size_t size
= FLEXSIZEOF (struct globnames
, name
,
1721 count
* sizeof (char *));
1722 if ((SIZE_MAX
- nameoff
) / 2 / sizeof (char *)
1725 if (glob_use_alloca (alloca_used
, size
))
1726 newnames
= names_alloca
1727 = alloca_account (size
, alloca_used
);
1728 else if ((newnames
= malloc (size
))
1731 newnames
->count
= count
;
1732 newnames
->next
= names
;
1736 names
->name
[cur
] = strdup (d
.name
);
1737 if (names
->name
[cur
] == NULL
)
1741 if (SIZE_MAX
- pglob
->gl_offs
<= nfound
)
1749 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1751 size_t len
= strlen (pattern
);
1753 names
->name
[cur
] = malloc (len
+ 1);
1754 if (names
->name
[cur
] == NULL
)
1756 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1759 result
= GLOB_NOMATCH
;
1762 char **new_gl_pathv
;
1765 if (SIZE_MAX
/ sizeof (char *) - pglob
->gl_pathc
1766 < pglob
->gl_offs
+ nfound
+ 1)
1770 = realloc (pglob
->gl_pathv
,
1771 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1774 if (new_gl_pathv
== NULL
)
1779 struct globnames
*old
= names
;
1781 for (i
= 0; i
< cur
; ++i
)
1782 free (names
->name
[i
]);
1783 names
= names
->next
;
1784 /* NB: we will not leak memory here if we exit without
1785 freeing the current block assigned to OLD. At least
1786 the very first block is always allocated on the stack
1787 and this is the block assigned to OLD here. */
1790 assert (old
== &init_names
);
1794 if (old
== names_alloca
)
1795 names_alloca
= names
;
1799 result
= GLOB_NOSPACE
;
1805 struct globnames
*old
= names
;
1807 for (i
= 0; i
< cur
; ++i
)
1808 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1810 names
= names
->next
;
1811 /* NB: we will not leak memory here if we exit without
1812 freeing the current block assigned to OLD. At least
1813 the very first block is always allocated on the stack
1814 and this is the block assigned to OLD here. */
1817 assert (old
== &init_names
);
1821 if (old
== names_alloca
)
1822 names_alloca
= names
;
1827 pglob
->gl_pathv
= new_gl_pathv
;
1829 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1831 pglob
->gl_flags
= flags
;
1838 if (__glibc_unlikely (flags
& GLOB_ALTDIRFUNC
))
1839 (*pglob
->gl_closedir
) (stream
);
This page took 0.092594 seconds and 4 git commands to generate.