1 /* Copyright (C) 1991-2002, 2003, 2004 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 /* AIX requires this to be the first thing in the file. */
20 #if defined _AIX && !defined __GNUC__
28 /* Enable GNU extensions in glob.h. */
30 # define _GNU_SOURCE 1
34 #include <sys/types.h>
37 /* Outcomment the following line for production quality code. */
38 /* #define NDEBUG 1 */
41 #include <stdio.h> /* Needed on stupid SunOS for assert. */
44 /* Comment out all this code if we are using the GNU C Library, and are not
45 actually compiling the library itself. This code is part of the GNU C
46 Library, but also included in many other GNU distributions. Compiling
47 and linking in this code is a waste when using the GNU C library
48 (especially if it is a shared library). Rather than having every GNU
49 program understand `configure --with-gnu-libc' and omit the object files,
50 it is simpler to just do this in the source for each such file. */
52 #define GLOB_INTERFACE_VERSION 1
53 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
54 # include <gnu-versions.h>
55 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
61 #if !defined _LIBC || !defined GLOB_ONLY_P
63 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
67 #if defined HAVE_UNISTD_H || defined _LIBC
70 # ifdef _POSIX_VERSION
76 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
80 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
84 # define __set_errno(val) errno = (val)
92 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
94 # define NAMLEN(dirent) strlen((dirent)->d_name)
96 # define dirent direct
97 # define NAMLEN(dirent) (dirent)->d_namlen
98 # ifdef HAVE_SYS_NDIR_H
99 # include <sys/ndir.h>
101 # ifdef HAVE_SYS_DIR_H
102 # include <sys/dir.h>
107 # ifdef HAVE_VMSDIR_H
109 # endif /* HAVE_VMSDIR_H */
113 /* In GNU systems, <dirent.h> defines this macro for us. */
116 # define NAMLEN(d) _D_NAMLEN(d)
119 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
120 if the `d_type' member for `struct dirent' is available. */
121 #ifdef _DIRENT_HAVE_D_TYPE
122 # define HAVE_D_TYPE 1
126 # define HAVE_DIRENT64 1
129 /* If the system has the `struct dirent64' type we use it internally. */
130 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
131 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
132 # define CONVERT_D_NAMLEN(d64, d32)
134 # define CONVERT_D_NAMLEN(d64, d32) \
135 (d64)->d_namlen = (d32)->d_namlen;
138 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
139 # define CONVERT_D_INO(d64, d32)
141 # define CONVERT_D_INO(d64, d32) \
142 (d64)->d_ino = (d32)->d_ino;
146 # define CONVERT_D_TYPE(d64, d32) \
147 (d64)->d_type = (d32)->d_type;
149 # define CONVERT_D_TYPE(d64, d32)
152 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
153 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
154 CONVERT_D_NAMLEN (d64, d32) \
155 CONVERT_D_INO (d64, d32) \
156 CONVERT_D_TYPE (d64, d32)
160 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
161 /* Posix does not require that the d_ino field be present, and some
162 systems do not provide it. */
163 # define REAL_DIR_ENTRY(dp) 1
165 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
168 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
172 #else /* No standard headers. */
174 extern char *getenv ();
176 # ifdef HAVE_STRING_H
180 # include <strings.h>
182 # ifdef HAVE_MEMORY_H
186 extern char *malloc (), *realloc ();
189 extern void qsort ();
190 extern void abort (), exit ();
192 #endif /* Standard headers. */
194 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
195 #if defined HAVE_LIMITS_H || defined __GNU_LIBRARY__
199 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
205 extern void bzero ();
208 extern void bcopy ();
211 # define memcpy(d, s, n) bcopy ((s), (d), (n))
212 # define strrchr rindex
213 /* memset is only used for zero here, but let's be paranoid. */
214 # define memset(s, better_be_zero, n) \
215 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
216 #endif /* Not ANSI_STRING. */
218 #if !defined HAVE_STRCOLL && !defined _LIBC
219 # define strcoll strcmp
222 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
223 # define HAVE_MEMPCPY 1
225 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
228 #ifndef __GNU_LIBRARY__
242 /* These casts are the for sake of the broken Ultrix compiler,
243 which warns of illegal pointer combinations otherwise. */
245 return (char *) malloc (n
);
246 return (char *) realloc (p
, n
);
248 # define realloc my_realloc
250 #endif /* __GNU_LIBRARY__ */
253 #if !defined __alloca && !defined __GNU_LIBRARY__
257 # define alloca(n) __builtin_alloca (n)
258 # else /* Not GCC. */
259 # ifdef HAVE_ALLOCA_H
261 # else /* Not HAVE_ALLOCA_H. */
266 extern char *alloca ();
267 # endif /* WINDOWS32 */
268 # endif /* Not _AIX. */
269 # endif /* sparc or HAVE_ALLOCA_H. */
272 # define __alloca alloca
276 #ifndef __GNU_LIBRARY__
278 # ifdef STAT_MACROS_BROKEN
282 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
289 # define strdup(str) __strdup (str)
290 # define sysconf(id) __sysconf (id)
291 # define closedir(dir) __closedir (dir)
292 # define opendir(name) __opendir (name)
293 # define readdir(str) __readdir64 (str)
294 # define getpwnam_r(name, bufp, buf, len, res) \
295 __getpwnam_r (name, bufp, buf, len, res)
297 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
299 # define HAVE_STAT64 1
303 # define __stat64(fname, buf) __stat (fname, buf)
304 /* This is the variable name we are using. */
308 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
310 # define size_t unsigned int
313 /* Some system header files erroneously define these.
314 We want our own definitions from <fnmatch.h> to take precedence. */
315 #ifndef __GNU_LIBRARY__
322 /* Some system header files erroneously define these.
323 We want our own definitions from <glob.h> to take precedence. */
324 #ifndef __GNU_LIBRARY__
331 # undef GLOB_NOESCAPE
336 #ifdef HAVE_GETLOGIN_R
337 extern int getlogin_r (char *, size_t);
339 extern char *getlogin (void);
342 static const char *next_brace_sub (const char *begin
, int flags
) __THROW
;
344 #endif /* GLOB_ONLY_P */
346 static int glob_in_dir (const char *pattern
, const char *directory
,
347 int flags
, int (*errfunc
) (const char *, int),
350 #if !defined _LIBC || !defined GLOB_ONLY_P
351 static int prefix_array (const char *prefix
, char **array
, size_t n
) __THROW
;
352 static int collated_compare (const __ptr_t
, const __ptr_t
) __THROW
;
355 /* Find the end of the sub-pattern in a brace expression. */
357 next_brace_sub (cp
, flags
)
361 unsigned int depth
= 0;
363 if ((flags
& GLOB_NOESCAPE
) == 0 && *cp
== '\\')
371 if ((*cp
== '}' && depth
-- == 0) || (*cp
== ',' && depth
== 0))
378 return *cp
!= '\0' ? cp
: NULL
;
381 #endif /* !GLOB_ONLY_P */
383 /* Do glob searching for PATTERN, placing results in PGLOB.
384 The bits defined above may be set in FLAGS.
385 If a directory cannot be opened or read and ERRFUNC is not nil,
386 it is called with the pathname that caused the error, and the
387 `errno' value from the failing call; if it returns non-zero
388 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
389 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
390 Otherwise, `glob' returns zero. */
392 #ifdef GLOB_ATTRIBUTE
395 glob (pattern
, flags
, errfunc
, pglob
)
398 int (*errfunc
) (const char *, int);
401 const char *filename
;
407 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
409 __set_errno (EINVAL
);
413 if (!(flags
& GLOB_DOOFFS
))
414 /* Have to do this so `globfree' knows where to start freeing. It
415 also makes all the code that uses gl_offs simpler. */
418 if (flags
& GLOB_BRACE
)
422 if (flags
& GLOB_NOESCAPE
)
423 begin
= strchr (pattern
, '{');
435 if (*begin
== '\\' && begin
[1] != '\0')
437 else if (*begin
== '{')
446 /* Allocate working buffer large enough for our work. Note that
447 we have at least an opening and closing brace. */
455 char onealt
[strlen (pattern
) - 1];
457 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
460 if (!(flags
& GLOB_APPEND
))
463 pglob
->gl_pathv
= NULL
;
469 /* We know the prefix for all sub-patterns. */
471 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
473 memcpy (onealt
, pattern
, begin
- pattern
);
474 alt_start
= &onealt
[begin
- pattern
];
477 /* Find the first sub-pattern and at the same time find the
478 rest after the closing brace. */
479 next
= next_brace_sub (begin
+ 1, flags
);
482 /* It is an illegal expression. */
486 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
489 /* Now find the end of the whole brace expression. */
493 rest
= next_brace_sub (rest
+ 1, flags
);
496 /* It is an illegal expression. */
500 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
503 /* Please note that we now can be sure the brace expression
505 rest_len
= strlen (++rest
) + 1;
507 /* We have a brace expression. BEGIN points to the opening {,
508 NEXT points past the terminator of the first element, and END
509 points past the final }. We will accumulate result names from
510 recursive runs for each brace alternative in the buffer using
513 if (!(flags
& GLOB_APPEND
))
515 /* This call is to set a new vector, so clear out the
516 vector so we can append to it. */
518 pglob
->gl_pathv
= NULL
;
520 firstc
= pglob
->gl_pathc
;
527 /* Construct the new glob expression. */
529 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
531 memcpy (alt_start
, p
, next
- p
);
532 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
535 result
= glob (onealt
,
536 ((flags
& ~(GLOB_NOCHECK
| GLOB_NOMAGIC
))
537 | GLOB_APPEND
), errfunc
, pglob
);
539 /* If we got an error, return it. */
540 if (result
&& result
!= GLOB_NOMATCH
)
545 if (!(flags
& GLOB_APPEND
))
554 /* We saw the last entry. */
558 next
= next_brace_sub (p
, flags
);
559 assert (next
!= NULL
);
566 if (pglob
->gl_pathc
!= firstc
)
567 /* We found some entries. */
569 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
574 /* Find the filename. */
575 filename
= strrchr (pattern
, '/');
576 #if defined __MSDOS__ || defined WINDOWS32
577 /* The case of "d:pattern". Since `:' is not allowed in
578 file names, we can safely assume that wherever it
579 happens in pattern, it signals the filename part. This
580 is so we could some day support patterns like "[a-z]:foo". */
581 if (filename
== NULL
)
582 filename
= strchr (pattern
, ':');
583 #endif /* __MSDOS__ || WINDOWS32 */
584 if (filename
== NULL
)
586 /* This can mean two things: a simple name or "~name". The latter
587 case is nothing but a notation for a directory. */
588 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
591 dirlen
= strlen (pattern
);
593 /* Set FILENAME to NULL as a special flag. This is ugly but
594 other solutions would require much more code. We test for
595 this special case below. */
609 else if (filename
== pattern
)
619 dirlen
= filename
- pattern
;
620 #if defined __MSDOS__ || defined WINDOWS32
622 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
627 drive_spec
= (char *) __alloca (dirlen
+ 1);
629 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
631 memcpy (drive_spec
, pattern
, dirlen
);
632 drive_spec
[dirlen
] = '\0';
634 /* For now, disallow wildcards in the drive spec, to
635 prevent infinite recursion in glob. */
636 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
638 /* If this is "d:pattern", we need to copy `:' to DIRNAME
639 as well. If it's "d:/pattern", don't remove the slash
640 from "d:/", since "d:" and "d:/" are not the same.*/
643 newp
= (char *) __alloca (dirlen
+ 1);
645 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
647 memcpy (newp
, pattern
, dirlen
);
653 if (filename
[0] == '\0'
654 #if defined __MSDOS__ || defined WINDOWS32
655 && dirname
[dirlen
- 1] != ':'
656 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
657 || dirname
[dirlen
- 1] != '/')
660 /* "pattern/". Expand "pattern", appending slashes. */
662 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
664 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
665 | (flags
& GLOB_MARK
));
670 if (!(flags
& GLOB_APPEND
))
673 if (!(flags
& GLOB_DOOFFS
))
674 pglob
->gl_pathv
= NULL
;
678 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
680 if (pglob
->gl_pathv
== NULL
)
683 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
684 pglob
->gl_pathv
[i
] = NULL
;
688 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
691 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
693 if (dirname
[1] == '\0' || dirname
[1] == '/')
695 /* Look up home directory. */
696 const char *home_dir
= getenv ("HOME");
698 if (home_dir
== NULL
|| home_dir
[0] == '\0')
702 if (home_dir
== NULL
|| home_dir
[0] == '\0')
703 home_dir
= "c:/users/default"; /* poor default */
705 if (home_dir
== NULL
|| home_dir
[0] == '\0')
709 # if defined HAVE_GETLOGIN_R || defined _LIBC
710 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
713 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
716 name
= (char *) __alloca (buflen
);
718 success
= getlogin_r (name
, buflen
) >= 0;
720 success
= (name
= getlogin ()) != NULL
;
725 # if defined HAVE_GETPWNAM_R || defined _LIBC
726 long int pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
733 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
734 Try a moderate value. */
737 pwtmpbuf
= (char *) __alloca (pwbuflen
);
739 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
748 pwtmpbuf
= extend_alloca (pwtmpbuf
, pwbuflen
,
752 pwtmpbuf
= (char *) __alloca (pwbuflen
);
760 home_dir
= p
->pw_dir
;
763 if (home_dir
== NULL
|| home_dir
[0] == '\0')
765 if (flags
& GLOB_TILDE_CHECK
)
768 home_dir
= "~"; /* No luck. */
770 # endif /* WINDOWS32 */
772 /* Now construct the full directory. */
773 if (dirname
[1] == '\0')
778 size_t home_len
= strlen (home_dir
);
779 newp
= (char *) __alloca (home_len
+ dirlen
);
781 mempcpy (mempcpy (newp
, home_dir
, home_len
),
782 &dirname
[1], dirlen
);
784 memcpy (newp
, home_dir
, home_len
);
785 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
790 # if !defined _AMIGA && !defined WINDOWS32
793 char *end_name
= strchr (dirname
, '/');
794 const char *user_name
;
795 const char *home_dir
;
797 if (end_name
== NULL
)
798 user_name
= dirname
+ 1;
802 newp
= (char *) __alloca (end_name
- dirname
);
804 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
807 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
808 newp
[end_name
- dirname
- 1] = '\0';
813 /* Look up specific user's home directory. */
816 # if defined HAVE_GETPWNAM_R || defined _LIBC
817 long int buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
824 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
828 pwtmpbuf
= (char *) __alloca (buflen
);
830 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
838 pwtmpbuf
= extend_alloca (pwtmpbuf
, buflen
, 2 * buflen
);
841 pwtmpbuf
= __alloca (buflen
);
846 p
= getpwnam (user_name
);
849 home_dir
= p
->pw_dir
;
853 /* If we found a home directory use this. */
854 if (home_dir
!= NULL
)
857 size_t home_len
= strlen (home_dir
);
858 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
859 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
861 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
862 end_name
, rest_len
)) = '\0';
864 memcpy (newp
, home_dir
, home_len
);
865 memcpy (&newp
[home_len
], end_name
, rest_len
);
866 newp
[home_len
+ rest_len
] = '\0';
871 if (flags
& GLOB_TILDE_CHECK
)
872 /* We have to regard it as an error if we cannot find the
876 # endif /* Not Amiga && not WINDOWS32. */
878 #endif /* Not VMS. */
880 /* Now test whether we looked for "~" or "~NAME". In this case we
881 can give the answer now. */
882 if (filename
== NULL
)
889 /* Return the directory if we don't check for error or if it exists. */
890 if ((flags
& GLOB_NOCHECK
)
891 || (((flags
& GLOB_ALTDIRFUNC
)
892 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
893 && S_ISDIR (st
.st_mode
))
894 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
896 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
900 = (char **) realloc (pglob
->gl_pathv
,
901 (newcount
+ 1 + 1) * sizeof (char *));
902 if (new_gl_pathv
== NULL
)
905 free (pglob
->gl_pathv
);
906 pglob
->gl_pathv
= NULL
;
910 pglob
->gl_pathv
= new_gl_pathv
;
912 #if defined HAVE_STRDUP || defined _LIBC
913 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
916 size_t len
= strlen (dirname
) + 1;
917 char *dircopy
= (char *) malloc (len
);
919 pglob
->gl_pathv
[newcount
] = memcpy (dircopy
, dirname
, len
);
922 if (pglob
->gl_pathv
[newcount
] == NULL
)
924 pglob
->gl_pathv
[++newcount
] = NULL
;
926 pglob
->gl_flags
= flags
;
935 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
937 /* The directory name contains metacharacters, so we
938 have to glob for the directory, and then glob for
939 the pattern in each directory found. */
943 if ((flags
& GLOB_ALTDIRFUNC
) != 0)
945 /* Use the alternative access functions also in the recursive
947 dirs
.gl_opendir
= pglob
->gl_opendir
;
948 dirs
.gl_readdir
= pglob
->gl_readdir
;
949 dirs
.gl_closedir
= pglob
->gl_closedir
;
950 dirs
.gl_stat
= pglob
->gl_stat
;
951 dirs
.gl_lstat
= pglob
->gl_lstat
;
954 status
= glob (dirname
,
955 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
957 | GLOB_NOSORT
| GLOB_ONLYDIR
),
962 /* We have successfully globbed the preceding directory name.
963 For each name we found, call glob_in_dir on it and FILENAME,
964 appending the results to PGLOB. */
965 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
971 /* Make globbing interruptible in the bash shell. */
972 extern int interrupt_state
;
982 old_pathc
= pglob
->gl_pathc
;
983 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
984 ((flags
| GLOB_APPEND
)
985 & ~(GLOB_NOCHECK
| GLOB_NOMAGIC
)),
987 if (status
== GLOB_NOMATCH
)
988 /* No matches in this directory. Try the next. */
999 /* Stick the directory on the front of each name. */
1000 if (prefix_array (dirs
.gl_pathv
[i
],
1001 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1002 pglob
->gl_pathc
- old_pathc
))
1006 pglob
->gl_pathc
= 0;
1007 return GLOB_NOSPACE
;
1011 flags
|= GLOB_MAGCHAR
;
1013 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
1014 But if we have not found any matching entry and the GLOB_NOCHECK
1015 flag was set we must return the input pattern itself. */
1016 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
1019 if (flags
& GLOB_NOCHECK
)
1021 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
1022 char **new_gl_pathv
;
1024 new_gl_pathv
= (char **) realloc (pglob
->gl_pathv
,
1027 if (new_gl_pathv
== NULL
)
1030 return GLOB_NOSPACE
;
1032 pglob
->gl_pathv
= new_gl_pathv
;
1034 pglob
->gl_pathv
[newcount
] = __strdup (pattern
);
1035 if (pglob
->gl_pathv
[newcount
] == NULL
)
1039 pglob
->gl_pathc
= 0;
1040 return GLOB_NOSPACE
;
1046 pglob
->gl_pathv
[newcount
] = NULL
;
1047 pglob
->gl_flags
= flags
;
1052 return GLOB_NOMATCH
;
1060 int old_pathc
= pglob
->gl_pathc
;
1062 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
1068 /* Stick the directory on the front of each name. */
1069 if (prefix_array (dirname
,
1070 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1071 pglob
->gl_pathc
- old_pathc
))
1074 pglob
->gl_pathc
= 0;
1075 return GLOB_NOSPACE
;
1080 if (flags
& GLOB_MARK
)
1082 /* Append slashes to directory names. */
1089 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1090 if (((flags
& GLOB_ALTDIRFUNC
)
1091 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1092 && S_ISDIR (st
.st_mode
))
1093 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1094 && S_ISDIR (st64
.st_mode
))))
1096 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1097 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1101 pglob
->gl_pathc
= 0;
1102 return GLOB_NOSPACE
;
1104 strcpy (&new[len
- 2], "/");
1105 pglob
->gl_pathv
[i
] = new;
1109 if (!(flags
& GLOB_NOSORT
))
1111 /* Sort the vector. */
1112 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
1113 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1114 sizeof (char *), collated_compare
);
1119 #if defined _LIBC && !defined glob
1120 libc_hidden_def (glob
)
1124 #if !defined _LIBC || !defined GLOB_ONLY_P
1126 /* Free storage allocated in PGLOB by a previous `glob' call. */
1129 register glob_t
*pglob
;
1131 if (pglob
->gl_pathv
!= NULL
)
1134 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1135 if (pglob
->gl_pathv
[pglob
->gl_offs
+ i
] != NULL
)
1136 free ((__ptr_t
) pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1137 free ((__ptr_t
) pglob
->gl_pathv
);
1140 #if defined _LIBC && !defined globfree
1141 libc_hidden_def (globfree
)
1145 /* Do a collated comparison of A and B. */
1147 collated_compare (a
, b
)
1151 const char *const s1
= *(const char *const * const) a
;
1152 const char *const s2
= *(const char *const * const) b
;
1160 return strcoll (s1
, s2
);
1164 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1165 elements in place. Return nonzero if out of memory, zero if successful.
1166 A slash is inserted between DIRNAME and each elt of ARRAY,
1167 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1169 prefix_array (dirname
, array
, n
)
1170 const char *dirname
;
1175 size_t dirlen
= strlen (dirname
);
1176 #if defined __MSDOS__ || defined WINDOWS32
1178 # define DIRSEP_CHAR sep_char
1180 # define DIRSEP_CHAR '/'
1183 if (dirlen
== 1 && dirname
[0] == '/')
1184 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1185 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1187 #if defined __MSDOS__ || defined WINDOWS32
1188 else if (dirlen
> 1)
1190 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1191 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1193 else if (dirname
[dirlen
- 1] == ':')
1195 /* DIRNAME is "d:". Use `:' instead of `/'. */
1202 for (i
= 0; i
< n
; ++i
)
1204 size_t eltlen
= strlen (array
[i
]) + 1;
1205 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1209 free ((__ptr_t
) array
[--i
]);
1215 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1216 *endp
++ = DIRSEP_CHAR
;
1217 mempcpy (endp
, array
[i
], eltlen
);
1220 memcpy (new, dirname
, dirlen
);
1221 new[dirlen
] = DIRSEP_CHAR
;
1222 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1224 free ((__ptr_t
) array
[i
]);
1232 /* We must not compile this function twice. */
1233 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1234 /* Return nonzero if PATTERN contains any metacharacters.
1235 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1237 __glob_pattern_p (pattern
, quote
)
1238 const char *pattern
;
1241 register const char *p
;
1244 for (p
= pattern
; *p
!= '\0'; ++p
)
1252 if (quote
&& p
[1] != '\0')
1269 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1273 #endif /* !GLOB_ONLY_P */
1276 /* We put this in a separate function mainly to allow the memory
1277 allocated with alloca to be recycled. */
1278 #if !defined _LIBC || !defined GLOB_ONLY_P
1280 link_exists_p (const char *dir
, size_t dirlen
, const char *fname
,
1281 glob_t
*pglob
, int flags
)
1283 size_t fnamelen
= strlen (fname
);
1284 char *fullname
= (char *) __alloca (dirlen
+ 1 + fnamelen
+ 1);
1288 # ifdef HAVE_MEMPCPY
1289 mempcpy (mempcpy (mempcpy (fullname
, dir
, dirlen
), "/", 1),
1290 fname
, fnamelen
+ 1);
1292 memcpy (fullname
, dir
, dirlen
);
1293 fullname
[dirlen
] = '/';
1294 memcpy (&fullname
[dirlen
+ 1], fname
, fnamelen
+ 1);
1297 return (((flags
& GLOB_ALTDIRFUNC
)
1298 ? (*pglob
->gl_stat
) (fullname
, &st
)
1299 : __stat64 (fullname
, &st64
)) == 0);
1304 /* Like `glob', but PATTERN is a final pathname component,
1305 and matches are searched for in DIRECTORY.
1306 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1307 The GLOB_APPEND flag is assumed to be set (always appends). */
1309 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1310 const char *pattern
;
1311 const char *directory
;
1313 int (*errfunc
) (const char *, int);
1316 size_t dirlen
= strlen (directory
);
1317 __ptr_t stream
= NULL
;
1320 struct globlink
*next
;
1323 struct globlink
*names
= NULL
;
1328 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1329 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1331 /* We need not do any tests. The PATTERN contains no meta
1332 characters and we must not return an error therefore the
1333 result will always contain exactly one name. */
1334 flags
|= GLOB_NOCHECK
;
1337 else if (meta
== 0 &&
1338 ((flags
& GLOB_NOESCAPE
) || strchr (pattern
, '\\') == NULL
))
1340 /* Since we use the normal file functions we can also use stat()
1341 to verify the file is there. */
1346 size_t patlen
= strlen (pattern
);
1347 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1349 # ifdef HAVE_MEMPCPY
1350 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1352 pattern
, patlen
+ 1);
1354 memcpy (fullname
, directory
, dirlen
);
1355 fullname
[dirlen
] = '/';
1356 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1358 if (((flags
& GLOB_ALTDIRFUNC
)
1359 ? (*pglob
->gl_stat
) (fullname
, &st
)
1360 : __stat64 (fullname
, &st64
)) == 0)
1361 /* We found this file to be existing. Now tell the rest
1362 of the function to copy this name into the result. */
1363 flags
|= GLOB_NOCHECK
;
1369 if (pattern
[0] == '\0')
1371 /* This is a special case for matching directories like in
1373 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1374 names
->name
= (char *) malloc (1);
1375 if (names
->name
== NULL
)
1377 names
->name
[0] = '\0';
1384 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1385 ? (*pglob
->gl_opendir
) (directory
)
1386 : (__ptr_t
) opendir (directory
));
1389 if (errno
!= ENOTDIR
1390 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1391 || (flags
& GLOB_ERR
)))
1392 return GLOB_ABORTED
;
1398 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1399 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1400 #if defined _AMIGA || defined VMS
1405 flags
|= GLOB_MAGCHAR
;
1411 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
1415 struct dirent64 d64
;
1416 char room
[offsetof (struct dirent64
, d_name
[0])
1421 if (flags
& GLOB_ALTDIRFUNC
)
1423 struct dirent
*d32
= (*pglob
->gl_readdir
) (stream
);
1426 CONVERT_DIRENT_DIRENT64 (&d64buf
.d64
, d32
);
1433 d
= __readdir64 ((DIR *) stream
);
1435 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1436 ? ((struct dirent
*)
1437 (*pglob
->gl_readdir
) (stream
))
1438 : __readdir ((DIR *) stream
));
1442 if (! REAL_DIR_ENTRY (d
))
1446 /* If we shall match only directories use the information
1447 provided by the dirent call if possible. */
1448 if ((flags
& GLOB_ONLYDIR
)
1449 && d
->d_type
!= DT_UNKNOWN
1450 && d
->d_type
!= DT_DIR
1451 && d
->d_type
!= DT_LNK
)
1457 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1459 /* If the file we found is a symlink we have to
1460 make sure the target file exists. */
1463 (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_LNK
) ||
1465 link_exists_p (directory
, dirlen
, name
, pglob
,
1468 struct globlink
*new = (struct globlink
*)
1469 __alloca (sizeof (struct globlink
));
1471 new->name
= (char *) malloc (len
+ 1);
1472 if (new->name
== NULL
)
1475 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1478 memcpy ((__ptr_t
) new->name
, name
, len
);
1479 new->name
[len
] = '\0';
1491 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1493 size_t len
= strlen (pattern
);
1495 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1497 names
->name
= (char *) malloc (len
+ 1);
1498 if (names
->name
== NULL
)
1501 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1503 memcpy (names
->name
, pattern
, len
);
1504 names
->name
[len
] = '\0';
1510 char **new_gl_pathv
;
1513 = (char **) realloc (pglob
->gl_pathv
,
1514 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1516 if (new_gl_pathv
== NULL
)
1518 pglob
->gl_pathv
= new_gl_pathv
;
1520 for (; names
!= NULL
; names
= names
->next
)
1521 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = names
->name
;
1522 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1524 pglob
->gl_flags
= flags
;
1530 if (flags
& GLOB_ALTDIRFUNC
)
1531 (*pglob
->gl_closedir
) (stream
);
1533 closedir ((DIR *) stream
);
1537 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1542 if (flags
& GLOB_ALTDIRFUNC
)
1543 (*pglob
->gl_closedir
) (stream
);
1545 closedir ((DIR *) stream
);
1548 while (names
!= NULL
)
1550 if (names
->name
!= NULL
)
1551 free ((__ptr_t
) names
->name
);
1552 names
= names
->next
;
1554 return GLOB_NOSPACE
;
1557 #endif /* Not ELIDE_CODE. */