1 /* Copyright (C) 1991-1999, 2000, 2001 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. */
197 extern void bzero ();
200 extern void bcopy ();
203 # define memcpy(d, s, n) bcopy ((s), (d), (n))
204 # define strrchr rindex
205 /* memset is only used for zero here, but let's be paranoid. */
206 # define memset(s, better_be_zero, n) \
207 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
208 #endif /* Not ANSI_STRING. */
210 #if !defined HAVE_STRCOLL && !defined _LIBC
211 # define strcoll strcmp
214 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
215 # define HAVE_MEMPCPY 1
217 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
220 #ifndef __GNU_LIBRARY__
234 /* These casts are the for sake of the broken Ultrix compiler,
235 which warns of illegal pointer combinations otherwise. */
237 return (char *) malloc (n
);
238 return (char *) realloc (p
, n
);
240 # define realloc my_realloc
242 #endif /* __GNU_LIBRARY__ */
245 #if !defined __alloca && !defined __GNU_LIBRARY__
249 # define alloca(n) __builtin_alloca (n)
250 # else /* Not GCC. */
251 # ifdef HAVE_ALLOCA_H
253 # else /* Not HAVE_ALLOCA_H. */
258 extern char *alloca ();
259 # endif /* WINDOWS32 */
260 # endif /* Not _AIX. */
261 # endif /* sparc or HAVE_ALLOCA_H. */
264 # define __alloca alloca
268 #ifndef __GNU_LIBRARY__
270 # ifdef STAT_MACROS_BROKEN
274 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
280 # define strdup(str) __strdup (str)
281 # define sysconf(id) __sysconf (id)
282 # define closedir(dir) __closedir (dir)
283 # define opendir(name) __opendir (name)
284 # define readdir(str) __readdir64 (str)
285 # define getpwnam_r(name, bufp, buf, len, res) \
286 __getpwnam_r (name, bufp, buf, len, res)
288 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
290 # define HAVE_STAT64 1
294 # define __stat64(fname, buf) __stat (fname, buf)
295 /* This is the variable name we are using. */
299 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
301 # define size_t unsigned int
304 /* Some system header files erroneously define these.
305 We want our own definitions from <fnmatch.h> to take precedence. */
306 #ifndef __GNU_LIBRARY__
313 /* Some system header files erroneously define these.
314 We want our own definitions from <glob.h> to take precedence. */
315 #ifndef __GNU_LIBRARY__
322 # undef GLOB_NOESCAPE
327 #ifdef HAVE_GETLOGIN_R
328 extern int getlogin_r
__P ((char *, size_t));
330 extern char *getlogin
__P ((void));
334 #if __GNUC__ - 0 >= 2
337 const char *next_brace_sub
__P ((const char *begin
));
339 #endif /* GLOB_ONLY_P */
341 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
343 int (*errfunc
) (const char *, int),
346 #if !defined _LIBC || !defined GLOB_ONLY_P
347 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
348 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
351 /* Find the end of the sub-pattern in a brace expression. We define
352 this as an inline function if the compiler permits. */
354 #if __GNUC__ - 0 >= 2
358 next_brace_sub (begin
)
361 unsigned int depth
= 0;
362 const char *cp
= begin
;
368 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
378 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
385 /* An incorrectly terminated brace expression. */
396 #endif /* !GLOB_ONLY_P */
398 /* Do glob searching for PATTERN, placing results in PGLOB.
399 The bits defined above may be set in FLAGS.
400 If a directory cannot be opened or read and ERRFUNC is not nil,
401 it is called with the pathname that caused the error, and the
402 `errno' value from the failing call; if it returns non-zero
403 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
404 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
405 Otherwise, `glob' returns zero. */
407 glob (pattern
, flags
, errfunc
, pglob
)
410 int (*errfunc
) __P ((const char *, int));
413 const char *filename
;
419 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
421 __set_errno (EINVAL
);
425 if (!(flags
& GLOB_DOOFFS
))
426 /* Have to do this so `globfree' knows where to start freeing. It
427 also makes all the code that uses gl_offs simpler. */
430 if (flags
& GLOB_BRACE
)
432 const char *begin
= strchr (pattern
, '{');
435 /* Allocate working buffer large enough for our work. Note that
436 we have at least an opening and closing brace. */
444 char onealt
[strlen (pattern
) - 1];
446 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
449 if (!(flags
& GLOB_APPEND
))
455 /* We know the prefix for all sub-patterns. */
457 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
459 memcpy (onealt
, pattern
, begin
- pattern
);
460 alt_start
= &onealt
[begin
- pattern
];
463 /* Find the first sub-pattern and at the same time find the
464 rest after the closing brace. */
465 next
= next_brace_sub (begin
+ 1);
468 /* It is an illegal expression. */
472 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
475 /* Now find the end of the whole brace expression. */
479 rest
= next_brace_sub (rest
+ 1);
482 /* It is an illegal expression. */
486 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
489 /* Please note that we now can be sure the brace expression
491 rest_len
= strlen (++rest
) + 1;
493 /* We have a brace expression. BEGIN points to the opening {,
494 NEXT points past the terminator of the first element, and END
495 points past the final }. We will accumulate result names from
496 recursive runs for each brace alternative in the buffer using
499 if (!(flags
& GLOB_APPEND
))
501 /* This call is to set a new vector, so clear out the
502 vector so we can append to it. */
504 pglob
->gl_pathv
= NULL
;
506 firstc
= pglob
->gl_pathc
;
513 /* Construct the new glob expression. */
515 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
517 memcpy (alt_start
, p
, next
- p
);
518 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
521 result
= glob (onealt
,
522 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
523 | GLOB_APPEND
), errfunc
, pglob
);
525 /* If we got an error, return it. */
526 if (result
&& result
!= GLOB_NOMATCH
)
531 if (!(flags
& GLOB_APPEND
))
537 /* We saw the last entry. */
541 next
= next_brace_sub (p
);
542 assert (next
!= NULL
);
549 if (pglob
->gl_pathc
!= firstc
)
550 /* We found some entries. */
552 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
557 /* Find the filename. */
558 filename
= strrchr (pattern
, '/');
559 #if defined __MSDOS__ || defined WINDOWS32
560 /* The case of "d:pattern". Since `:' is not allowed in
561 file names, we can safely assume that wherever it
562 happens in pattern, it signals the filename part. This
563 is so we could some day support patterns like "[a-z]:foo". */
564 if (filename
== NULL
)
565 filename
= strchr (pattern
, ':');
566 #endif /* __MSDOS__ || WINDOWS32 */
567 if (filename
== NULL
)
569 /* This can mean two things: a simple name or "~name". The latter
570 case is nothing but a notation for a directory. */
571 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
574 dirlen
= strlen (pattern
);
576 /* Set FILENAME to NULL as a special flag. This is ugly but
577 other solutions would require much more code. We test for
578 this special case below. */
592 else if (filename
== pattern
)
602 dirlen
= filename
- pattern
;
603 #if defined __MSDOS__ || defined WINDOWS32
605 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
610 drive_spec
= (char *) __alloca (dirlen
+ 1);
612 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
614 memcpy (drive_spec
, pattern
, dirlen
);
615 drive_spec
[dirlen
] = '\0';
617 /* For now, disallow wildcards in the drive spec, to
618 prevent infinite recursion in glob. */
619 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
621 /* If this is "d:pattern", we need to copy `:' to DIRNAME
622 as well. If it's "d:/pattern", don't remove the slash
623 from "d:/", since "d:" and "d:/" are not the same.*/
626 newp
= (char *) __alloca (dirlen
+ 1);
628 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
630 memcpy (newp
, pattern
, dirlen
);
636 if (filename
[0] == '\0'
637 #if defined __MSDOS__ || defined WINDOWS32
638 && dirname
[dirlen
- 1] != ':'
639 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
640 || dirname
[dirlen
- 1] != '/')
643 /* "pattern/". Expand "pattern", appending slashes. */
645 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
647 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
648 | (flags
& GLOB_MARK
));
653 if (!(flags
& GLOB_APPEND
))
656 if (!(flags
& GLOB_DOOFFS
))
657 pglob
->gl_pathv
= NULL
;
661 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
663 if (pglob
->gl_pathv
== NULL
)
666 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
667 pglob
->gl_pathv
[i
] = NULL
;
671 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
674 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
676 if (dirname
[1] == '\0' || dirname
[1] == '/')
678 /* Look up home directory. */
679 const char *home_dir
= getenv ("HOME");
681 if (home_dir
== NULL
|| home_dir
[0] == '\0')
685 if (home_dir
== NULL
|| home_dir
[0] == '\0')
686 home_dir
= "c:/users/default"; /* poor default */
688 if (home_dir
== NULL
|| home_dir
[0] == '\0')
692 # if defined HAVE_GETLOGIN_R || defined _LIBC
693 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
696 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
699 name
= (char *) __alloca (buflen
);
701 success
= getlogin_r (name
, buflen
) >= 0;
703 success
= (name
= getlogin ()) != NULL
;
708 # if defined HAVE_GETPWNAM_R || defined _LIBC
709 long int pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
715 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
716 Try a moderate value. */
718 pwtmpbuf
= (char *) __alloca (pwbuflen
);
720 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
729 pwtmpbuf
= (char *) __alloca (pwbuflen
);
736 home_dir
= p
->pw_dir
;
739 if (home_dir
== NULL
|| home_dir
[0] == '\0')
741 if (flags
& GLOB_TILDE_CHECK
)
744 home_dir
= "~"; /* No luck. */
746 # endif /* WINDOWS32 */
748 /* Now construct the full directory. */
749 if (dirname
[1] == '\0')
754 size_t home_len
= strlen (home_dir
);
755 newp
= (char *) __alloca (home_len
+ dirlen
);
757 mempcpy (mempcpy (newp
, home_dir
, home_len
),
758 &dirname
[1], dirlen
);
760 memcpy (newp
, home_dir
, home_len
);
761 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
766 # if !defined _AMIGA && !defined WINDOWS32
769 char *end_name
= strchr (dirname
, '/');
770 const char *user_name
;
771 const char *home_dir
;
773 if (end_name
== NULL
)
774 user_name
= dirname
+ 1;
778 newp
= (char *) __alloca (end_name
- dirname
);
780 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
783 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
784 newp
[end_name
- dirname
- 1] = '\0';
789 /* Look up specific user's home directory. */
792 # if defined HAVE_GETPWNAM_R || defined _LIBC
793 long int buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
799 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
802 pwtmpbuf
= (char *) __alloca (buflen
);
804 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
812 pwtmpbuf
= __alloca (buflen
);
816 p
= getpwnam (user_name
);
819 home_dir
= p
->pw_dir
;
823 /* If we found a home directory use this. */
824 if (home_dir
!= NULL
)
827 size_t home_len
= strlen (home_dir
);
828 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
829 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
831 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
832 end_name
, rest_len
)) = '\0';
834 memcpy (newp
, home_dir
, home_len
);
835 memcpy (&newp
[home_len
], end_name
, rest_len
);
836 newp
[home_len
+ rest_len
] = '\0';
841 if (flags
& GLOB_TILDE_CHECK
)
842 /* We have to regard it as an error if we cannot find the
846 # endif /* Not Amiga && not WINDOWS32. */
848 #endif /* Not VMS. */
850 /* Now test whether we looked for "~" or "~NAME". In this case we
851 can give the answer now. */
852 if (filename
== NULL
)
859 /* Return the directory if we don't check for error or if it exists. */
860 if ((flags
& GLOB_NOCHECK
)
861 || (((flags
& GLOB_ALTDIRFUNC
)
862 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
863 && S_ISDIR (st
.st_mode
))
864 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
866 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
869 = (char **) realloc (pglob
->gl_pathv
,
870 (newcount
+ 1 + 1) * sizeof (char *));
871 if (pglob
->gl_pathv
== NULL
)
874 #if defined HAVE_STRDUP || defined _LIBC
875 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
878 size_t len
= strlen (dirname
) + 1;
879 char *dircopy
= malloc (len
);
881 pglob
->gl_pathv
[newcount
] = memcpy (dircopy
, dirname
, len
);
884 if (pglob
->gl_pathv
[newcount
] == NULL
)
886 free (pglob
->gl_pathv
);
889 pglob
->gl_pathv
[++newcount
] = NULL
;
891 pglob
->gl_flags
= flags
;
900 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
902 /* The directory name contains metacharacters, so we
903 have to glob for the directory, and then glob for
904 the pattern in each directory found. */
908 if ((flags
& GLOB_ALTDIRFUNC
) != 0)
910 /* Use the alternative access functions also in the recursive
912 dirs
.gl_opendir
= pglob
->gl_opendir
;
913 dirs
.gl_readdir
= pglob
->gl_readdir
;
914 dirs
.gl_closedir
= pglob
->gl_closedir
;
915 dirs
.gl_stat
= pglob
->gl_stat
;
916 dirs
.gl_lstat
= pglob
->gl_lstat
;
919 status
= glob (dirname
,
920 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
922 | GLOB_NOSORT
| GLOB_ONLYDIR
),
927 /* We have successfully globbed the preceding directory name.
928 For each name we found, call glob_in_dir on it and FILENAME,
929 appending the results to PGLOB. */
930 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
936 /* Make globbing interruptible in the bash shell. */
937 extern int interrupt_state
;
947 old_pathc
= pglob
->gl_pathc
;
948 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
949 ((flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
),
951 if (status
== GLOB_NOMATCH
)
952 /* No matches in this directory. Try the next. */
962 /* Stick the directory on the front of each name. */
963 if (prefix_array (dirs
.gl_pathv
[i
],
964 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
965 pglob
->gl_pathc
- old_pathc
))
973 flags
|= GLOB_MAGCHAR
;
975 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
976 But if we have not found any matching entry and the GLOB_NOCHECK
977 flag was set we must return the list consisting of the disrectory
978 names followed by the filename. */
979 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
982 if (flags
& GLOB_NOCHECK
)
984 size_t filename_len
= strlen (filename
) + 1;
986 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
992 /* This is an pessimistic guess about the size. */
994 = (char **) realloc (pglob
->gl_pathv
,
995 (newcount
+ dirs
.gl_pathc
+ 1)
997 if (pglob
->gl_pathv
== NULL
)
1000 return GLOB_NOSPACE
;
1003 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
1005 const char *dir
= dirs
.gl_pathv
[i
];
1006 size_t dir_len
= strlen (dir
);
1008 /* First check whether this really is a directory. */
1009 if (((flags
& GLOB_ALTDIRFUNC
)
1010 ? ((*pglob
->gl_stat
) (dir
, &st
) != 0
1011 || !S_ISDIR (st
.st_mode
))
1012 : (__stat64 (dir
, &st64
) != 0
1013 || !S_ISDIR (st64
.st_mode
))))
1014 /* No directory, ignore this entry. */
1017 pglob
->gl_pathv
[newcount
] = malloc (dir_len
+ 1
1019 if (pglob
->gl_pathv
[newcount
] == NULL
)
1023 return GLOB_NOSPACE
;
1027 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[newcount
],
1030 filename
, filename_len
);
1032 memcpy (pglob
->gl_pathv
[newcount
], dir
, dir_len
);
1033 pglob
->gl_pathv
[newcount
][dir_len
] = '/';
1034 memcpy (&pglob
->gl_pathv
[newcount
][dir_len
+ 1],
1035 filename
, filename_len
);
1041 pglob
->gl_pathv
[newcount
] = NULL
;
1042 pglob
->gl_flags
= flags
;
1044 /* Now we know how large the gl_pathv vector must be. */
1045 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
1047 * sizeof (char *)));
1048 if (new_pathv
!= NULL
)
1049 pglob
->gl_pathv
= new_pathv
;
1054 return GLOB_NOMATCH
;
1062 int old_pathc
= pglob
->gl_pathc
;
1064 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
1070 /* Stick the directory on the front of each name. */
1071 if (prefix_array (dirname
,
1072 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1073 pglob
->gl_pathc
- old_pathc
))
1076 return GLOB_NOSPACE
;
1081 if (flags
& GLOB_MARK
)
1083 /* Append slashes to directory names. */
1090 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1091 if (((flags
& GLOB_ALTDIRFUNC
)
1092 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1093 && S_ISDIR (st
.st_mode
))
1094 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1095 && S_ISDIR (st64
.st_mode
))))
1097 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1098 char *new = realloc (pglob
->gl_pathv
[i
], len
);
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
);
1121 #if !defined _LIBC || !defined GLOB_ONLY_P
1123 /* Free storage allocated in PGLOB by a previous `glob' call. */
1126 register glob_t
*pglob
;
1128 if (pglob
->gl_pathv
!= NULL
)
1131 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1132 if (pglob
->gl_pathv
[pglob
->gl_offs
+ i
] != NULL
)
1133 free ((__ptr_t
) pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1134 free ((__ptr_t
) pglob
->gl_pathv
);
1139 /* Do a collated comparison of A and B. */
1141 collated_compare (a
, b
)
1145 const char *const s1
= *(const char *const * const) a
;
1146 const char *const s2
= *(const char *const * const) b
;
1154 return strcoll (s1
, s2
);
1158 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1159 elements in place. Return nonzero if out of memory, zero if successful.
1160 A slash is inserted between DIRNAME and each elt of ARRAY,
1161 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1163 prefix_array (dirname
, array
, n
)
1164 const char *dirname
;
1169 size_t dirlen
= strlen (dirname
);
1170 #if defined __MSDOS__ || defined WINDOWS32
1172 # define DIRSEP_CHAR sep_char
1174 # define DIRSEP_CHAR '/'
1177 if (dirlen
== 1 && dirname
[0] == '/')
1178 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1179 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1181 #if defined __MSDOS__ || defined WINDOWS32
1182 else if (dirlen
> 1)
1184 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1185 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1187 else if (dirname
[dirlen
- 1] == ':')
1189 /* DIRNAME is "d:". Use `:' instead of `/'. */
1196 for (i
= 0; i
< n
; ++i
)
1198 size_t eltlen
= strlen (array
[i
]) + 1;
1199 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1203 free ((__ptr_t
) array
[--i
]);
1209 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1210 *endp
++ = DIRSEP_CHAR
;
1211 mempcpy (endp
, array
[i
], eltlen
);
1214 memcpy (new, dirname
, dirlen
);
1215 new[dirlen
] = DIRSEP_CHAR
;
1216 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1218 free ((__ptr_t
) array
[i
]);
1226 /* We must not compile this function twice. */
1227 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1228 /* Return nonzero if PATTERN contains any metacharacters.
1229 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1231 __glob_pattern_p (pattern
, quote
)
1232 const char *pattern
;
1235 register const char *p
;
1238 for (p
= pattern
; *p
!= '\0'; ++p
)
1246 if (quote
&& p
[1] != '\0')
1263 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1267 #endif /* !GLOB_ONLY_P */
1270 /* Like `glob', but PATTERN is a final pathname component,
1271 and matches are searched for in DIRECTORY.
1272 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1273 The GLOB_APPEND flag is assumed to be set (always appends). */
1275 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1276 const char *pattern
;
1277 const char *directory
;
1279 int (*errfunc
) __P ((const char *, int));
1282 __ptr_t stream
= NULL
;
1285 struct globlink
*next
;
1288 struct globlink
*names
= NULL
;
1293 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1294 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1296 /* We need not do any tests. The PATTERN contains no meta
1297 characters and we must not return an error therefore the
1298 result will always contain exactly one name. */
1299 flags
|= GLOB_NOCHECK
;
1302 else if (meta
== 0 &&
1303 ((flags
& GLOB_NOESCAPE
) || strchr(pattern
, '\\') == NULL
))
1305 /* Since we use the normal file functions we can also use stat()
1306 to verify the file is there. */
1311 size_t patlen
= strlen (pattern
);
1312 size_t dirlen
= strlen (directory
);
1313 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1315 # ifdef HAVE_MEMPCPY
1316 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1318 pattern
, patlen
+ 1);
1320 memcpy (fullname
, directory
, dirlen
);
1321 fullname
[dirlen
] = '/';
1322 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1324 if (((flags
& GLOB_ALTDIRFUNC
)
1325 ? (*pglob
->gl_stat
) (fullname
, &st
)
1326 : __stat64 (fullname
, &st64
)) == 0)
1327 /* We found this file to be existing. Now tell the rest
1328 of the function to copy this name into the result. */
1329 flags
|= GLOB_NOCHECK
;
1335 if (pattern
[0] == '\0')
1337 /* This is a special case for matching directories like in
1339 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1340 names
->name
= (char *) malloc (1);
1341 if (names
->name
== NULL
)
1343 names
->name
[0] = '\0';
1350 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1351 ? (*pglob
->gl_opendir
) (directory
)
1352 : (__ptr_t
) opendir (directory
));
1355 if (errno
!= ENOTDIR
1356 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1357 || (flags
& GLOB_ERR
)))
1358 return GLOB_ABORTED
;
1364 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1365 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1366 #if defined _AMIGA || defined VMS
1371 flags
|= GLOB_MAGCHAR
;
1377 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
1379 struct dirent64 d64
;
1381 if (flags
& GLOB_ALTDIRFUNC
)
1383 struct dirent
*d32
= (*pglob
->gl_readdir
) (stream
);
1386 CONVERT_DIRENT_DIRENT64 (&d64
, d32
);
1393 d
= __readdir64 ((DIR *) stream
);
1395 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1396 ? ((struct dirent
*)
1397 (*pglob
->gl_readdir
) (stream
))
1398 : __readdir ((DIR *) stream
));
1402 if (! REAL_DIR_ENTRY (d
))
1406 /* If we shall match only directories use the information
1407 provided by the dirent call if possible. */
1408 if ((flags
& GLOB_ONLYDIR
)
1409 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1415 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1417 struct globlink
*new = (struct globlink
*)
1418 __alloca (sizeof (struct globlink
));
1420 new->name
= (char *) malloc (len
+ 1);
1421 if (new->name
== NULL
)
1424 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1427 memcpy ((__ptr_t
) new->name
, name
, len
);
1428 new->name
[len
] = '\0';
1439 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1441 size_t len
= strlen (pattern
);
1443 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1445 names
->name
= (char *) malloc (len
+ 1);
1446 if (names
->name
== NULL
)
1449 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1451 memcpy (names
->name
, pattern
, len
);
1452 names
->name
[len
] = '\0';
1459 = (char **) realloc (pglob
->gl_pathv
,
1460 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1462 if (pglob
->gl_pathv
== NULL
)
1465 for (; names
!= NULL
; names
= names
->next
)
1466 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = names
->name
;
1467 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1469 pglob
->gl_flags
= flags
;
1475 if (flags
& GLOB_ALTDIRFUNC
)
1476 (*pglob
->gl_closedir
) (stream
);
1478 closedir ((DIR *) stream
);
1482 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1487 if (flags
& GLOB_ALTDIRFUNC
)
1488 (*pglob
->gl_closedir
) (stream
);
1490 closedir ((DIR *) stream
);
1493 while (names
!= NULL
)
1495 if (names
->name
!= NULL
)
1496 free ((__ptr_t
) names
->name
);
1497 names
= names
->next
;
1499 return GLOB_NOSPACE
;
1502 #endif /* Not ELIDE_CODE. */