1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
26 #include <sys/types.h>
30 /* Outcomment the following line for production quality code. */
31 /* #define NDEBUG 1 */
34 #include <stdio.h> /* Needed on stupid SunOS for assert. */
36 #if !defined _LIBC || !defined GLOB_ONLY_P
37 #if defined HAVE_UNISTD_H || defined _LIBC
40 # ifdef _POSIX_VERSION
48 #if defined HAVE_STDINT_H || defined _LIBC
50 #elif !defined UINTPTR_MAX
51 # define UINTPTR_MAX (~((size_t) 0))
56 # define __set_errno(val) errno = (val)
59 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
61 # define NAMLEN(dirent) strlen((dirent)->d_name)
63 # define dirent direct
64 # define NAMLEN(dirent) (dirent)->d_namlen
65 # ifdef HAVE_SYS_NDIR_H
66 # include <sys/ndir.h>
68 # ifdef HAVE_SYS_DIR_H
76 # endif /* HAVE_VMSDIR_H */
80 /* In GNU systems, <dirent.h> defines this macro for us. */
83 # define NAMLEN(d) _D_NAMLEN(d)
86 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
87 if the `d_type' member for `struct dirent' is available.
88 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
89 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
90 /* True if the directory entry D must be of type T. */
91 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
93 /* True if the directory entry D might be a symbolic link. */
94 # define DIRENT_MIGHT_BE_SYMLINK(d) \
95 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
97 /* True if the directory entry D might be a directory. */
98 # define DIRENT_MIGHT_BE_DIR(d) \
99 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
101 #else /* !HAVE_D_TYPE */
102 # define DIRENT_MUST_BE(d, t) false
103 # define DIRENT_MIGHT_BE_SYMLINK(d) true
104 # define DIRENT_MIGHT_BE_DIR(d) true
105 #endif /* HAVE_D_TYPE */
107 /* If the system has the `struct dirent64' type we use it internally. */
108 #if defined _LIBC && !defined COMPILE_GLOB64
109 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
110 # define CONVERT_D_NAMLEN(d64, d32)
112 # define CONVERT_D_NAMLEN(d64, d32) \
113 (d64)->d_namlen = (d32)->d_namlen;
116 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
117 # define CONVERT_D_INO(d64, d32)
119 # define CONVERT_D_INO(d64, d32) \
120 (d64)->d_ino = (d32)->d_ino;
123 # ifdef _DIRENT_HAVE_D_TYPE
124 # define CONVERT_D_TYPE(d64, d32) \
125 (d64)->d_type = (d32)->d_type;
127 # define CONVERT_D_TYPE(d64, d32)
130 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
131 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
132 CONVERT_D_NAMLEN (d64, d32) \
133 CONVERT_D_INO (d64, d32) \
134 CONVERT_D_TYPE (d64, d32)
138 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
139 /* Posix does not require that the d_ino field be present, and some
140 systems do not provide it. */
141 # define REAL_DIR_ENTRY(dp) 1
143 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
149 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
152 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
159 # define strdup(str) __strdup (str)
160 # define sysconf(id) __sysconf (id)
161 # define closedir(dir) __closedir (dir)
162 # define opendir(name) __opendir (name)
163 # define readdir(str) __readdir64 (str)
164 # define getpwnam_r(name, bufp, buf, len, res) \
165 __getpwnam_r (name, bufp, buf, len, res)
167 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
169 # define struct_stat64 struct stat64
171 # include "getlogin_r.h"
172 # include "mempcpy.h"
173 # include "stat-macros.h"
175 # define __stat64(fname, buf) stat (fname, buf)
176 # define struct_stat64 struct stat
177 # define __stat(fname, buf) stat (fname, buf)
178 # define __alloca alloca
179 # define __readdir readdir
180 # define __readdir64 readdir64
181 # define __glob_pattern_p glob_pattern_p
186 #ifdef _SC_GETPW_R_SIZE_MAX
187 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
189 # define GETPW_R_SIZE_MAX() (-1)
191 #ifdef _SC_LOGIN_NAME_MAX
192 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
194 # define GET_LOGIN_NAME_MAX() (-1)
197 static const char *next_brace_sub (const char *begin
, int flags
) __THROW
;
199 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
201 #ifndef attribute_hidden
202 # define attribute_hidden
205 static int glob_in_dir (const char *pattern
, const char *directory
,
206 int flags
, int (*errfunc
) (const char *, int),
207 glob_t
*pglob
, size_t alloca_used
);
208 extern int __glob_pattern_type (const char *pattern
, int quote
)
211 #if !defined _LIBC || !defined GLOB_ONLY_P
212 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROW
;
213 static int collated_compare (const void *, const void *) __THROW
;
216 /* Find the end of the sub-pattern in a brace expression. */
218 next_brace_sub (const char *cp
, int flags
)
220 unsigned int depth
= 0;
222 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
230 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
237 return *cp
!= '\0' ? cp
: NULL
;
240 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
242 /* Do glob searching for PATTERN, placing results in PGLOB.
243 The bits defined above may be set in FLAGS.
244 If a directory cannot be opened or read and ERRFUNC is not nil,
245 it is called with the pathname that caused the error, and the
246 `errno' value from the failing call; if it returns non-zero
247 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
248 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
249 Otherwise, `glob' returns zero. */
251 #ifdef GLOB_ATTRIBUTE
254 glob (pattern
, flags
, errfunc
, pglob
)
257 int (*errfunc
) (const char *, int);
260 const char *filename
;
261 char *dirname
= NULL
;
266 int dirname_modified
;
267 int malloc_dirname
= 0;
271 size_t alloca_used
= 0;
274 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
276 __set_errno (EINVAL
);
280 if (!(flags
& GLOB_DOOFFS
))
281 /* Have to do this so `globfree' knows where to start freeing. It
282 also makes all the code that uses gl_offs simpler. */
285 if (flags
& GLOB_BRACE
)
289 if (flags
& GLOB_NOESCAPE
)
290 begin
= strchr (pattern
, '{');
302 if (*begin
== '\\' && begin
[1] != '\0')
304 else if (*begin
== '{')
313 /* Allocate working buffer large enough for our work. Note that
314 we have at least an opening and closing brace. */
322 size_t pattern_len
= strlen (pattern
) - 1;
324 int alloca_onealt
= __libc_use_alloca (alloca_used
+ pattern_len
);
326 onealt
= alloca_account (pattern_len
, alloca_used
);
330 onealt
= (char *) malloc (pattern_len
);
333 if (!(flags
& GLOB_APPEND
))
336 pglob
->gl_pathv
= NULL
;
342 /* We know the prefix for all sub-patterns. */
343 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
345 /* Find the first sub-pattern and at the same time find the
346 rest after the closing brace. */
347 next
= next_brace_sub (begin
+ 1, flags
);
350 /* It is an illegal expression. */
353 if (__builtin_expect (!alloca_onealt
, 0))
356 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
359 /* Now find the end of the whole brace expression. */
363 rest
= next_brace_sub (rest
+ 1, flags
);
365 /* It is an illegal expression. */
368 /* Please note that we now can be sure the brace expression
370 rest_len
= strlen (++rest
) + 1;
372 /* We have a brace expression. BEGIN points to the opening {,
373 NEXT points past the terminator of the first element, and END
374 points past the final }. We will accumulate result names from
375 recursive runs for each brace alternative in the buffer using
378 if (!(flags
& GLOB_APPEND
))
380 /* This call is to set a new vector, so clear out the
381 vector so we can append to it. */
383 pglob
->gl_pathv
= NULL
;
385 firstc
= pglob
->gl_pathc
;
392 /* Construct the new glob expression. */
393 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
395 result
= glob (onealt
,
396 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
397 | GLOB_APPEND
), errfunc
, pglob
);
399 /* If we got an error, return it. */
400 if (result
&& result
!= GLOB_NOMATCH
)
403 if (__builtin_expect (!alloca_onealt
, 0))
406 if (!(flags
& GLOB_APPEND
))
415 /* We saw the last entry. */
419 next
= next_brace_sub (p
, flags
);
420 assert (next
!= NULL
);
424 if (__builtin_expect (!alloca_onealt
, 0))
428 if (pglob
->gl_pathc
!= firstc
)
429 /* We found some entries. */
431 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
436 if (!(flags
& GLOB_APPEND
))
439 if (!(flags
& GLOB_DOOFFS
))
440 pglob
->gl_pathv
= NULL
;
445 if (pglob
->gl_offs
>= ~((size_t) 0) / sizeof (char *))
448 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
450 if (pglob
->gl_pathv
== NULL
)
453 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
454 pglob
->gl_pathv
[i
] = NULL
;
458 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
460 /* Find the filename. */
461 filename
= strrchr (pattern
, '/');
462 #if defined __MSDOS__ || defined WINDOWS32
463 /* The case of "d:pattern". Since `:' is not allowed in
464 file names, we can safely assume that wherever it
465 happens in pattern, it signals the filename part. This
466 is so we could some day support patterns like "[a-z]:foo". */
467 if (filename
== NULL
)
468 filename
= strchr (pattern
, ':');
469 #endif /* __MSDOS__ || WINDOWS32 */
470 dirname_modified
= 0;
471 if (filename
== NULL
)
473 /* This can mean two things: a simple name or "~name". The latter
474 case is nothing but a notation for a directory. */
475 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
477 dirname
= (char *) pattern
;
478 dirlen
= strlen (pattern
);
480 /* Set FILENAME to NULL as a special flag. This is ugly but
481 other solutions would require much more code. We test for
482 this special case below. */
487 if (__builtin_expect (pattern
[0] == '\0', 0))
489 dirs
.gl_pathv
= NULL
;
495 dirname
= (char *) "";
497 dirname
= (char *) ".";
502 else if (filename
== pattern
503 || (filename
== pattern
+ 1 && pattern
[0] == '\\'
504 && (flags
& GLOB_NOESCAPE
) == 0))
506 /* "/pattern" or "\\/pattern". */
507 dirname
= (char *) "/";
514 dirlen
= filename
- pattern
;
515 #if defined __MSDOS__ || defined WINDOWS32
517 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
522 drive_spec
= (char *) __alloca (dirlen
+ 1);
523 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
524 /* For now, disallow wildcards in the drive spec, to
525 prevent infinite recursion in glob. */
526 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
528 /* If this is "d:pattern", we need to copy `:' to DIRNAME
529 as well. If it's "d:/pattern", don't remove the slash
530 from "d:/", since "d:" and "d:/" are not the same.*/
534 if (__libc_use_alloca (alloca_used
+ dirlen
+ 1))
535 newp
= alloca_account (dirlen
+ 1, alloca_used
);
539 newp
= malloc (dirlen
+ 1);
544 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
548 if (filename
[0] == '\0'
549 #if defined __MSDOS__ || defined WINDOWS32
550 && dirname
[dirlen
- 1] != ':'
551 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
552 || dirname
[dirlen
- 1] != '/')
555 /* "pattern/". Expand "pattern", appending slashes. */
557 int orig_flags
= flags
;
558 if (!(flags
& GLOB_NOESCAPE
) && dirname
[dirlen
- 1] == '\\')
560 /* "pattern\\/". Remove the final backslash if it hasn't
562 char *p
= (char *) &dirname
[dirlen
- 1];
564 while (p
> dirname
&& p
[-1] == '\\') --p
;
565 if ((&dirname
[dirlen
] - p
) & 1)
567 *(char *) &dirname
[--dirlen
] = '\0';
568 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
571 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
573 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
574 | (flags
& GLOB_MARK
));
575 else if (val
== GLOB_NOMATCH
&& flags
!= orig_flags
)
577 /* Make sure globfree (&dirs); is a nop. */
578 dirs
.gl_pathv
= NULL
;
580 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
589 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
591 if (dirname
[1] == '\0' || dirname
[1] == '/'
592 || (!(flags
& GLOB_NOESCAPE
) && dirname
[1] == '\\'
593 && (dirname
[2] == '\0' || dirname
[2] == '/')))
595 /* Look up home directory. */
596 char *home_dir
= getenv ("HOME");
597 int malloc_home_dir
= 0;
599 if (home_dir
== NULL
|| home_dir
[0] == '\0')
603 if (home_dir
== NULL
|| home_dir
[0] == '\0')
604 home_dir
= "c:/users/default"; /* poor default */
606 if (home_dir
== NULL
|| home_dir
[0] == '\0')
610 size_t buflen
= GET_LOGIN_NAME_MAX () + 1;
613 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
616 name
= alloca_account (buflen
, alloca_used
);
618 success
= getlogin_r (name
, buflen
) == 0;
622 # if defined HAVE_GETPWNAM_R || defined _LIBC
623 long int pwbuflen
= GETPW_R_SIZE_MAX ();
626 int malloc_pwtmpbuf
= 0;
631 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
632 Try a moderate value. */
635 if (__libc_use_alloca (alloca_used
+ pwbuflen
))
636 pwtmpbuf
= alloca_account (pwbuflen
, alloca_used
);
639 pwtmpbuf
= malloc (pwbuflen
);
640 if (pwtmpbuf
== NULL
)
642 retval
= GLOB_NOSPACE
;
648 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
658 && __libc_use_alloca (alloca_used
660 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, pwbuflen
,
665 char *newp
= realloc (malloc_pwtmpbuf
670 if (__builtin_expect (malloc_pwtmpbuf
, 0))
672 retval
= GLOB_NOSPACE
;
676 pwbuflen
= 2 * pwbuflen
;
686 if (!malloc_pwtmpbuf
)
687 home_dir
= p
->pw_dir
;
690 size_t home_dir_len
= strlen (p
->pw_dir
) + 1;
691 if (__libc_use_alloca (alloca_used
+ home_dir_len
))
692 home_dir
= alloca_account (home_dir_len
,
696 home_dir
= malloc (home_dir_len
);
697 if (home_dir
== NULL
)
700 retval
= GLOB_NOSPACE
;
705 memcpy (home_dir
, p
->pw_dir
, home_dir_len
);
712 if (home_dir
== NULL
|| home_dir
[0] == '\0')
714 if (flags
& GLOB_TILDE_CHECK
)
716 if (__builtin_expect (malloc_home_dir
, 0))
718 retval
= GLOB_NOMATCH
;
722 home_dir
= (char *) "~"; /* No luck. */
724 # endif /* WINDOWS32 */
726 /* Now construct the full directory. */
727 if (dirname
[1] == '\0')
729 if (__builtin_expect (malloc_dirname
, 0))
733 dirlen
= strlen (dirname
);
734 malloc_dirname
= malloc_home_dir
;
739 size_t home_len
= strlen (home_dir
);
740 int use_alloca
= __libc_use_alloca (alloca_used
741 + home_len
+ dirlen
);
743 newp
= alloca_account (home_len
+ dirlen
, alloca_used
);
746 newp
= malloc (home_len
+ dirlen
);
749 if (__builtin_expect (malloc_home_dir
, 0))
751 retval
= GLOB_NOSPACE
;
756 mempcpy (mempcpy (newp
, home_dir
, home_len
),
757 &dirname
[1], dirlen
);
759 if (__builtin_expect (malloc_dirname
, 0))
763 dirlen
+= home_len
- 1;
764 malloc_dirname
= !use_alloca
;
766 dirname_modified
= 1;
768 # if !defined _AMIGA && !defined WINDOWS32
771 char *end_name
= strchr (dirname
, '/');
773 int malloc_user_name
= 0;
774 char *unescape
= NULL
;
776 if (!(flags
& GLOB_NOESCAPE
))
778 if (end_name
== NULL
)
780 unescape
= strchr (dirname
, '\\');
782 end_name
= strchr (unescape
, '\0');
785 unescape
= memchr (dirname
, '\\', end_name
- dirname
);
787 if (end_name
== NULL
)
788 user_name
= dirname
+ 1;
792 if (__libc_use_alloca (alloca_used
+ (end_name
- dirname
)))
793 newp
= alloca_account (end_name
- dirname
, alloca_used
);
796 newp
= malloc (end_name
- dirname
);
799 retval
= GLOB_NOSPACE
;
802 malloc_user_name
= 1;
804 if (unescape
!= NULL
)
806 char *p
= mempcpy (newp
, dirname
+ 1,
807 unescape
- dirname
- 1);
815 /* "~fo\\o\\" unescape to user_name "foo\\",
816 but "~fo\\o\\/" unescape to user_name
818 if (filename
== NULL
)
829 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
834 /* Look up specific user's home directory. */
837 # if defined HAVE_GETPWNAM_R || defined _LIBC
838 long int buflen
= GETPW_R_SIZE_MAX ();
840 int malloc_pwtmpbuf
= 0;
846 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
850 if (__libc_use_alloca (alloca_used
+ buflen
))
851 pwtmpbuf
= alloca_account (buflen
, alloca_used
);
854 pwtmpbuf
= malloc (buflen
);
855 if (pwtmpbuf
== NULL
)
858 if (__builtin_expect (malloc_user_name
, 0))
860 retval
= GLOB_NOSPACE
;
866 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
874 && __libc_use_alloca (alloca_used
+ 2 * buflen
))
875 pwtmpbuf
= extend_alloca_account (pwtmpbuf
, buflen
,
876 2 * buflen
, alloca_used
);
879 char *newp
= realloc (malloc_pwtmpbuf
? pwtmpbuf
: NULL
,
883 if (__builtin_expect (malloc_pwtmpbuf
, 0))
893 p
= getpwnam (user_name
);
896 if (__builtin_expect (malloc_user_name
, 0))
899 /* If we found a home directory use this. */
902 size_t home_len
= strlen (p
->pw_dir
);
903 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
905 if (__builtin_expect (malloc_dirname
, 0))
909 if (__libc_use_alloca (alloca_used
+ home_len
+ rest_len
+ 1))
910 dirname
= alloca_account (home_len
+ rest_len
+ 1,
914 dirname
= malloc (home_len
+ rest_len
+ 1);
917 if (__builtin_expect (malloc_pwtmpbuf
, 0))
919 retval
= GLOB_NOSPACE
;
924 *((char *) mempcpy (mempcpy (dirname
, p
->pw_dir
, home_len
),
925 end_name
, rest_len
)) = '\0';
927 dirlen
= home_len
+ rest_len
;
928 dirname_modified
= 1;
930 if (__builtin_expect (malloc_pwtmpbuf
, 0))
935 if (__builtin_expect (malloc_pwtmpbuf
, 0))
938 if (flags
& GLOB_TILDE_CHECK
)
939 /* We have to regard it as an error if we cannot find the
945 # endif /* Not Amiga && not WINDOWS32. */
947 #endif /* Not VMS. */
949 /* Now test whether we looked for "~" or "~NAME". In this case we
950 can give the answer now. */
951 if (filename
== NULL
)
956 /* Return the directory if we don't check for error or if it exists. */
957 if ((flags
& GLOB_NOCHECK
)
958 || (((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
959 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
960 && S_ISDIR (st
.st_mode
))
961 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
963 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
966 if (newcount
> UINTPTR_MAX
- (1 + 1)
967 || newcount
+ 1 + 1 > ~((size_t) 0) / sizeof (char *))
970 free (pglob
->gl_pathv
);
971 pglob
->gl_pathv
= NULL
;
977 = (char **) realloc (pglob
->gl_pathv
,
978 (newcount
+ 1 + 1) * sizeof (char *));
979 if (new_gl_pathv
== NULL
)
981 pglob
->gl_pathv
= new_gl_pathv
;
983 if (flags
& GLOB_MARK
)
986 pglob
->gl_pathv
[newcount
] = malloc (dirlen
+ 2);
987 if (pglob
->gl_pathv
[newcount
] == NULL
)
989 p
= mempcpy (pglob
->gl_pathv
[newcount
], dirname
, dirlen
);
995 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
996 if (pglob
->gl_pathv
[newcount
] == NULL
)
999 pglob
->gl_pathv
[++newcount
] = NULL
;
1001 pglob
->gl_flags
= flags
;
1007 return GLOB_NOMATCH
;
1010 meta
= __glob_pattern_type (dirname
, !(flags
& GLOB_NOESCAPE
));
1011 /* meta is 1 if correct glob pattern containing metacharacters.
1012 If meta has bit (1 << 2) set, it means there was an unterminated
1013 [ which we handle the same, using fnmatch. Broken unterminated
1014 pattern bracket expressions ought to be rare enough that it is
1015 not worth special casing them, fnmatch will do the right thing. */
1018 /* The directory name contains metacharacters, so we
1019 have to glob for the directory, and then glob for
1020 the pattern in each directory found. */
1023 if (!(flags
& GLOB_NOESCAPE
) && dirlen
> 0 && dirname
[dirlen
- 1] == '\\')
1025 /* "foo\\/bar". Remove the final backslash from dirname
1026 if it has not been quoted. */
1027 char *p
= (char *) &dirname
[dirlen
- 1];
1029 while (p
> dirname
&& p
[-1] == '\\') --p
;
1030 if ((&dirname
[dirlen
] - p
) & 1)
1031 *(char *) &dirname
[--dirlen
] = '\0';
1034 if (__builtin_expect ((flags
& GLOB_ALTDIRFUNC
) != 0, 0))
1036 /* Use the alternative access functions also in the recursive
1038 dirs
.gl_opendir
= pglob
->gl_opendir
;
1039 dirs
.gl_readdir
= pglob
->gl_readdir
;
1040 dirs
.gl_closedir
= pglob
->gl_closedir
;
1041 dirs
.gl_stat
= pglob
->gl_stat
;
1042 dirs
.gl_lstat
= pglob
->gl_lstat
;
1045 status
= glob (dirname
,
1046 ((flags
& (GLOB_ERR
| GLOB_NOESCAPE
1048 | GLOB_NOSORT
| GLOB_ONLYDIR
),
1052 if ((flags
& GLOB_NOCHECK
) == 0 || status
!= GLOB_NOMATCH
)
1057 /* We have successfully globbed the preceding directory name.
1058 For each name we found, call glob_in_dir on it and FILENAME,
1059 appending the results to PGLOB. */
1060 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1066 /* Make globbing interruptible in the bash shell. */
1067 extern int interrupt_state
;
1069 if (interrupt_state
)
1072 return GLOB_ABORTED
;
1077 old_pathc
= pglob
->gl_pathc
;
1078 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
1079 ((flags
| GLOB_APPEND
)
1080 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
1081 errfunc
, pglob
, alloca_used
);
1082 if (status
== GLOB_NOMATCH
)
1083 /* No matches in this directory. Try the next. */
1090 pglob
->gl_pathc
= 0;
1094 /* Stick the directory on the front of each name. */
1095 if (prefix_array (dirs
.gl_pathv
[i
],
1096 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1097 pglob
->gl_pathc
- old_pathc
))
1101 pglob
->gl_pathc
= 0;
1102 return GLOB_NOSPACE
;
1106 flags
|= GLOB_MAGCHAR
;
1108 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1109 But if we have not found any matching entry and the GLOB_NOCHECK
1110 flag was set we must return the input pattern itself. */
1111 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1115 if (flags
& GLOB_NOCHECK
)
1117 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1118 char **new_gl_pathv
;
1120 if (newcount
> UINTPTR_MAX
- 2
1121 || newcount
+ 2 > ~((size_t) 0) / sizeof (char *))
1125 return GLOB_NOSPACE
;
1128 new_gl_pathv
= (char **) realloc (pglob
->gl_pathv
,
1131 if (new_gl_pathv
== NULL
)
1133 pglob
->gl_pathv
= new_gl_pathv
;
1135 pglob
->gl_pathv
[newcount
] = __strdup (pattern
);
1136 if (pglob
->gl_pathv
[newcount
] == NULL
)
1140 pglob
->gl_pathc
= 0;
1141 return GLOB_NOSPACE
;
1147 pglob
->gl_pathv
[newcount
] = NULL
;
1148 pglob
->gl_flags
= flags
;
1153 return GLOB_NOMATCH
;
1161 int old_pathc
= pglob
->gl_pathc
;
1162 int orig_flags
= flags
;
1166 char *p
= strchr (dirname
, '\\'), *q
;
1167 /* We need to unescape the dirname string. It is certainly
1168 allocated by alloca, as otherwise filename would be NULL
1169 or dirname wouldn't contain backslashes. */
1182 while (*p
++ != '\0');
1183 dirname_modified
= 1;
1185 if (dirname_modified
)
1186 flags
&= ~(GLOB_NOCHECK
| GLOB_NOMAGIC
);
1187 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
,
1191 if (status
== GLOB_NOMATCH
&& flags
!= orig_flags
1192 && pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1194 /* Make sure globfree (&dirs); is a nop. */
1195 dirs
.gl_pathv
= NULL
;
1204 /* Stick the directory on the front of each name. */
1205 if (prefix_array (dirname
,
1206 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1207 pglob
->gl_pathc
- old_pathc
))
1210 pglob
->gl_pathc
= 0;
1211 return GLOB_NOSPACE
;
1216 if (flags
& GLOB_MARK
)
1218 /* Append slashes to directory names. */
1223 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1224 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1225 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1226 && S_ISDIR (st
.st_mode
))
1227 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1228 && S_ISDIR (st64
.st_mode
))))
1230 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1231 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1235 pglob
->gl_pathc
= 0;
1236 return GLOB_NOSPACE
;
1238 strcpy (&new[len
- 2], "/");
1239 pglob
->gl_pathv
[i
] = new;
1243 if (!(flags
& GLOB_NOSORT
))
1245 /* Sort the vector. */
1246 qsort (&pglob
->gl_pathv
[oldcount
],
1247 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1248 sizeof (char *), collated_compare
);
1252 if (__builtin_expect (malloc_dirname
, 0))
1257 #if defined _LIBC && !defined glob
1258 libc_hidden_def (glob
)
1262 #if !defined _LIBC || !defined GLOB_ONLY_P
1264 /* Free storage allocated in PGLOB by a previous `glob' call. */
1267 register glob_t
*pglob
;
1269 if (pglob
->gl_pathv
!= NULL
)
1272 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1273 free (pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1274 free (pglob
->gl_pathv
);
1275 pglob
->gl_pathv
= NULL
;
1278 #if defined _LIBC && !defined globfree
1279 libc_hidden_def (globfree
)
1283 /* Do a collated comparison of A and B. */
1285 collated_compare (const void *a
, const void *b
)
1287 const char *const s1
= *(const char *const * const) a
;
1288 const char *const s2
= *(const char *const * const) b
;
1296 return strcoll (s1
, s2
);
1300 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1301 elements in place. Return nonzero if out of memory, zero if successful.
1302 A slash is inserted between DIRNAME and each elt of ARRAY,
1303 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1305 prefix_array (const char *dirname
, char **array
, size_t n
)
1308 size_t dirlen
= strlen (dirname
);
1309 #if defined __MSDOS__ || defined WINDOWS32
1311 # define DIRSEP_CHAR sep_char
1313 # define DIRSEP_CHAR '/'
1316 if (dirlen
== 1 && dirname
[0] == '/')
1317 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1318 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1320 #if defined __MSDOS__ || defined WINDOWS32
1321 else if (dirlen
> 1)
1323 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1324 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1326 else if (dirname
[dirlen
- 1] == ':')
1328 /* DIRNAME is "d:". Use `:' instead of `/'. */
1335 for (i
= 0; i
< n
; ++i
)
1337 size_t eltlen
= strlen (array
[i
]) + 1;
1338 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1347 char *endp
= mempcpy (new, dirname
, dirlen
);
1348 *endp
++ = DIRSEP_CHAR
;
1349 mempcpy (endp
, array
[i
], eltlen
);
1359 /* We must not compile this function twice. */
1360 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1362 __glob_pattern_type (pattern
, quote
)
1363 const char *pattern
;
1366 register const char *p
;
1369 for (p
= pattern
; *p
!= '\0'; ++p
)
1398 /* Return nonzero if PATTERN contains any metacharacters.
1399 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1401 __glob_pattern_p (pattern
, quote
)
1402 const char *pattern
;
1405 return __glob_pattern_type (pattern
, quote
) == 1;
1408 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1412 #endif /* !GLOB_ONLY_P */
1415 /* We put this in a separate function mainly to allow the memory
1416 allocated with alloca to be recycled. */
1417 #if !defined _LIBC || !defined GLOB_ONLY_P
1419 __attribute_noinline__
1420 link_exists2_p (const char *dir
, size_t dirlen
, const char *fname
,
1427 size_t fnamelen
= strlen (fname
);
1428 char *fullname
= (char *) __alloca (dirlen
+ 1 + fnamelen
+ 1);
1434 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1435 fname
, fnamelen
+ 1);
1438 return (*pglob
->gl_stat
) (fullname
, &st
) == 0;
1440 return ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1441 ? (*pglob
->gl_stat
) (fullname
, &st
)
1442 : __stat64 (fullname
, &st64
)) == 0);
1446 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1447 (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \
1448 ? link_exists2_p (dirname, dirnamelen, fname, pglob) \
1449 : ({ struct stat64 st64; \
1450 __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; }))
1452 # define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \
1453 link_exists2_p (dirname, dirnamelen, fname, pglob, flags)
1458 /* Like `glob', but PATTERN is a final pathname component,
1459 and matches are searched for in DIRECTORY.
1460 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1461 The GLOB_APPEND flag is assumed to be set (always appends). */
1463 glob_in_dir (const char *pattern
, const char *directory
, int flags
,
1464 int (*errfunc
) (const char *, int),
1465 glob_t
*pglob
, size_t alloca_used
)
1467 size_t dirlen
= strlen (directory
);
1468 void *stream
= NULL
;
1471 struct globnames
*next
;
1475 #define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0])
1476 struct globnames init_names
;
1477 struct globnames
*names
= &init_names
;
1478 struct globnames
*names_alloca
= &init_names
;
1484 alloca_used
+= sizeof (init_names
);
1486 init_names
.next
= NULL
;
1487 init_names
.count
= INITIAL_COUNT
;
1489 meta
= __glob_pattern_type (pattern
, !(flags
& GLOB_NOESCAPE
));
1490 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1492 /* We need not do any tests. The PATTERN contains no meta
1493 characters and we must not return an error therefore the
1494 result will always contain exactly one name. */
1495 flags
|= GLOB_NOCHECK
;
1499 /* Since we use the normal file functions we can also use stat()
1500 to verify the file is there. */
1506 size_t patlen
= strlen (pattern
);
1507 int alloca_fullname
= __libc_use_alloca (alloca_used
1508 + dirlen
+ 1 + patlen
+ 1);
1510 if (alloca_fullname
)
1511 fullname
= alloca_account (dirlen
+ 1 + patlen
+ 1, alloca_used
);
1514 fullname
= malloc (dirlen
+ 1 + patlen
+ 1);
1515 if (fullname
== NULL
)
1516 return GLOB_NOSPACE
;
1519 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1521 pattern
, patlen
+ 1);
1522 if ((__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1523 ? (*pglob
->gl_stat
) (fullname
, &ust
.st
)
1524 : __stat64 (fullname
, &ust
.st64
)) == 0)
1525 /* We found this file to be existing. Now tell the rest
1526 of the function to copy this name into the result. */
1527 flags
|= GLOB_NOCHECK
;
1529 if (__builtin_expect (!alloca_fullname
, 0))
1534 stream
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1535 ? (*pglob
->gl_opendir
) (directory
)
1536 : opendir (directory
));
1539 if (errno
!= ENOTDIR
1540 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1541 || (flags
& GLOB_ERR
)))
1542 return GLOB_ABORTED
;
1547 int dfd
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1548 ? -1 : dirfd ((DIR *) stream
));
1550 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1551 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1552 #if defined _AMIGA || defined VMS
1556 flags
|= GLOB_MAGCHAR
;
1562 #if defined _LIBC && !defined COMPILE_GLOB64
1566 struct dirent64 d64
;
1567 char room
[offsetof (struct dirent64
, d_name
[0])
1572 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1574 struct dirent
*d32
= (*pglob
->gl_readdir
) (stream
);
1577 CONVERT_DIRENT_DIRENT64 (&d64buf
.d64
, d32
);
1584 d
= __readdir64 (stream
);
1586 struct dirent
*d
= (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0)
1587 ? ((struct dirent
*)
1588 (*pglob
->gl_readdir
) (stream
))
1589 : __readdir (stream
));
1593 if (! REAL_DIR_ENTRY (d
))
1596 /* If we shall match only directories use the information
1597 provided by the dirent call if possible. */
1598 if ((flags
& GLOB_ONLYDIR
) && !DIRENT_MIGHT_BE_DIR (d
))
1603 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1605 /* If the file we found is a symlink we have to
1606 make sure the target file exists. */
1607 if (!DIRENT_MIGHT_BE_SYMLINK (d
)
1608 || link_exists_p (dfd
, directory
, dirlen
, name
, pglob
,
1611 if (cur
== names
->count
)
1613 struct globnames
*newnames
;
1614 size_t count
= names
->count
* 2;
1615 size_t size
= (sizeof (struct globnames
)
1616 + ((count
- INITIAL_COUNT
)
1617 * sizeof (char *)));
1618 if (__libc_use_alloca (alloca_used
+ size
))
1619 newnames
= names_alloca
1620 = alloca_account (size
, alloca_used
);
1621 else if ((newnames
= malloc (size
))
1624 newnames
->count
= count
;
1625 newnames
->next
= names
;
1630 names
->name
[cur
] = (char *) malloc (len
+ 1);
1631 if (names
->name
[cur
] == NULL
)
1633 *((char *) mempcpy (names
->name
[cur
++], name
, len
))
1642 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1644 size_t len
= strlen (pattern
);
1646 names
->name
[cur
] = (char *) malloc (len
+ 1);
1647 if (names
->name
[cur
] == NULL
)
1649 *((char *) mempcpy (names
->name
[cur
++], pattern
, len
)) = '\0';
1652 int result
= GLOB_NOMATCH
;
1657 if (pglob
->gl_pathc
> UINTPTR_MAX
- pglob
->gl_offs
1658 || pglob
->gl_pathc
+ pglob
->gl_offs
> UINTPTR_MAX
- nfound
1659 || pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
> UINTPTR_MAX
- 1
1660 || (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1
1661 > UINTPTR_MAX
/ sizeof (char *)))
1664 char **new_gl_pathv
;
1666 = (char **) realloc (pglob
->gl_pathv
,
1667 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1669 if (new_gl_pathv
== NULL
)
1674 struct globnames
*old
= names
;
1675 for (size_t i
= 0; i
< cur
; ++i
)
1676 free (names
->name
[i
]);
1677 names
= names
->next
;
1678 /* NB: we will not leak memory here if we exit without
1679 freeing the current block assigned to OLD. At least
1680 the very first block is always allocated on the stack
1681 and this is the block assigned to OLD here. */
1684 assert (old
== &init_names
);
1688 if (old
== names_alloca
)
1689 names_alloca
= names
;
1693 result
= GLOB_NOSPACE
;
1699 struct globnames
*old
= names
;
1700 for (size_t i
= 0; i
< cur
; ++i
)
1701 new_gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++]
1703 names
= names
->next
;
1704 /* NB: we will not leak memory here if we exit without
1705 freeing the current block assigned to OLD. At least
1706 the very first block is always allocated on the stack
1707 and this is the block assigned to OLD here. */
1710 assert (old
== &init_names
);
1714 if (old
== names_alloca
)
1715 names_alloca
= names
;
1720 pglob
->gl_pathv
= new_gl_pathv
;
1722 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1724 pglob
->gl_flags
= flags
;
1731 if (__builtin_expect (flags
& GLOB_ALTDIRFUNC
, 0))
1732 (*pglob
->gl_closedir
) (stream
);