1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to the Free
15 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18 /* AIX requires this to be the first thing in the file. */
19 #if defined _AIX && !defined __GNUC__
27 /* Enable GNU extensions in glob.h. */
29 # define _GNU_SOURCE 1
33 #include <sys/types.h>
36 /* Outcomment the following line for production quality code. */
37 /* #define NDEBUG 1 */
40 #include <stdio.h> /* Needed on stupid SunOS for assert. */
43 /* Comment out all this code if we are using the GNU C Library, and are not
44 actually compiling the library itself. This code is part of the GNU C
45 Library, but also included in many other GNU distributions. Compiling
46 and linking in this code is a waste when using the GNU C library
47 (especially if it is a shared library). Rather than having every GNU
48 program understand `configure --with-gnu-libc' and omit the object files,
49 it is simpler to just do this in the source for each such file. */
51 #define GLOB_INTERFACE_VERSION 1
52 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
53 # include <gnu-versions.h>
54 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
61 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
65 #if defined HAVE_UNISTD_H || defined _LIBC
68 # ifdef _POSIX_VERSION
74 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
78 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
82 # define __set_errno(val) errno = (val)
90 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
92 # define NAMLEN(dirent) strlen((dirent)->d_name)
94 # define dirent direct
95 # define NAMLEN(dirent) (dirent)->d_namlen
96 # ifdef HAVE_SYS_NDIR_H
97 # include <sys/ndir.h>
99 # ifdef HAVE_SYS_DIR_H
100 # include <sys/dir.h>
105 # ifdef HAVE_VMSDIR_H
107 # endif /* HAVE_VMSDIR_H */
111 /* In GNU systems, <dirent.h> defines this macro for us. */
114 # define NAMLEN(d) _D_NAMLEN(d)
117 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
118 if the `d_type' member for `struct dirent' is available. */
119 #ifdef _DIRENT_HAVE_D_TYPE
120 # define HAVE_D_TYPE 1
124 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
125 /* Posix does not require that the d_ino field be present, and some
126 systems do not provide it. */
127 # define REAL_DIR_ENTRY(dp) 1
129 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
132 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
136 #else /* No standard headers. */
138 extern char *getenv ();
140 # ifdef HAVE_STRING_H
144 # include <strings.h>
146 # ifdef HAVE_MEMORY_H
150 extern char *malloc (), *realloc ();
153 extern void qsort ();
154 extern void abort (), exit ();
156 #endif /* Standard headers. */
161 extern void bzero ();
164 extern void bcopy ();
167 # define memcpy(d, s, n) bcopy ((s), (d), (n))
168 # define strrchr rindex
169 /* memset is only used for zero here, but let's be paranoid. */
170 # define memset(s, better_be_zero, n) \
171 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
172 #endif /* Not ANSI_STRING. */
174 #if !defined HAVE_STRCOLL && !defined _LIBC
175 # define strcoll strcmp
178 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
179 # define HAVE_MEMPCPY 1
181 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
184 #ifndef __GNU_LIBRARY__
198 /* These casts are the for sake of the broken Ultrix compiler,
199 which warns of illegal pointer combinations otherwise. */
201 return (char *) malloc (n
);
202 return (char *) realloc (p
, n
);
204 # define realloc my_realloc
206 #endif /* __GNU_LIBRARY__ */
209 #if !defined __alloca && !defined __GNU_LIBRARY__
213 # define alloca(n) __builtin_alloca (n)
214 # else /* Not GCC. */
215 # ifdef HAVE_ALLOCA_H
217 # else /* Not HAVE_ALLOCA_H. */
222 extern char *alloca ();
223 # endif /* WINDOWS32 */
224 # endif /* Not _AIX. */
225 # endif /* sparc or HAVE_ALLOCA_H. */
228 # define __alloca alloca
232 #ifndef __GNU_LIBRARY__
234 # ifdef STAT_MACROS_BROKEN
238 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
244 # define strdup(str) __strdup (str)
245 # define sysconf(id) __sysconf (id)
246 # define closedir(dir) __closedir (dir)
247 # define opendir(name) __opendir (name)
248 # define readdir(str) __readdir (str)
249 # define getpwnam_r(name, bufp, buf, len, res) \
250 __getpwnam_r (name, bufp, buf, len, res)
252 # define __stat(fname, buf) __xstat (_STAT_VER, fname, buf)
256 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
258 # define size_t unsigned int
261 /* Some system header files erroneously define these.
262 We want our own definitions from <fnmatch.h> to take precedence. */
263 #ifndef __GNU_LIBRARY__
270 /* Some system header files erroneously define these.
271 We want our own definitions from <glob.h> to take precedence. */
272 #ifndef __GNU_LIBRARY__
279 # undef GLOB_NOESCAPE
284 #ifdef HAVE_GETLOGIN_R
285 extern int getlogin_r
__P ((char *, size_t));
287 extern char *getlogin
__P ((void));
291 #if __GNUC__ - 0 >= 2
294 const char *next_brace_sub
__P ((const char *begin
));
295 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
297 int (*errfunc
) (const char *, int),
299 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
300 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
302 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
303 int __glob_pattern_p
__P ((const char *pattern
, int quote
));
306 /* Find the end of the sub-pattern in a brace expression. We define
307 this as an inline function if the compiler permits. */
309 #if __GNUC__ - 0 >= 2
313 next_brace_sub (begin
)
316 unsigned int depth
= 0;
317 const char *cp
= begin
;
323 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
333 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
340 /* An incorrectly terminated brace expression. */
351 /* Do glob searching for PATTERN, placing results in PGLOB.
352 The bits defined above may be set in FLAGS.
353 If a directory cannot be opened or read and ERRFUNC is not nil,
354 it is called with the pathname that caused the error, and the
355 `errno' value from the failing call; if it returns non-zero
356 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
357 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
358 Otherwise, `glob' returns zero. */
360 glob (pattern
, flags
, errfunc
, pglob
)
363 int (*errfunc
) __P ((const char *, int));
366 const char *filename
;
372 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
374 __set_errno (EINVAL
);
378 if (flags
& GLOB_BRACE
)
380 const char *begin
= strchr (pattern
, '{');
383 /* Allocate working buffer large enough for our work. Note that
384 we have at least an opening and closing brace. */
392 char onealt
[strlen (pattern
) - 1];
394 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
397 if (!(flags
& GLOB_APPEND
))
403 /* We know the prefix for all sub-patterns. */
405 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
407 memcpy (onealt
, pattern
, begin
- pattern
);
408 alt_start
= &onealt
[begin
- pattern
];
411 /* Find the first sub-pattern and at the same time find the
412 rest after the closing brace. */
413 next
= next_brace_sub (begin
+ 1);
416 /* It is an illegal expression. */
420 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
423 /* Now find the end of the whole brace expression. */
427 rest
= next_brace_sub (rest
+ 1);
430 /* It is an illegal expression. */
434 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
437 /* Please note that we now can be sure the brace expression
439 rest_len
= strlen (++rest
) + 1;
441 /* We have a brace expression. BEGIN points to the opening {,
442 NEXT points past the terminator of the first element, and END
443 points past the final }. We will accumulate result names from
444 recursive runs for each brace alternative in the buffer using
447 if (!(flags
& GLOB_APPEND
))
449 /* This call is to set a new vector, so clear out the
450 vector so we can append to it. */
452 pglob
->gl_pathv
= NULL
;
454 firstc
= pglob
->gl_pathc
;
461 /* Construct the new glob expression. */
463 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
465 memcpy (alt_start
, p
, next
- p
);
466 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
469 result
= glob (onealt
,
470 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
471 | GLOB_APPEND
), errfunc
, pglob
);
473 /* If we got an error, return it. */
474 if (result
&& result
!= GLOB_NOMATCH
)
479 if (!(flags
& GLOB_APPEND
))
485 /* We saw the last entry. */
489 next
= next_brace_sub (p
);
490 assert (next
!= NULL
);
497 if (pglob
->gl_pathc
!= firstc
)
498 /* We found some entries. */
500 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
505 /* Find the filename. */
506 filename
= strrchr (pattern
, '/');
507 #if defined __MSDOS__ || defined WINDOWS32
508 /* The case of "d:pattern". Since `:' is not allowed in
509 file names, we can safely assume that wherever it
510 happens in pattern, it signals the filename part. This
511 is so we could some day support patterns like "[a-z]:foo". */
512 if (filename
== NULL
)
513 filename
= strchr (pattern
, ':');
514 #endif /* __MSDOS__ || WINDOWS32 */
515 if (filename
== NULL
)
517 /* This can mean two things: a simple name or "~name". The later
518 case is nothing but a notation for a directory. */
519 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
522 dirlen
= strlen (pattern
);
524 /* Set FILENAME to NULL as a special flag. This is ugly but
525 other solutions would require much more code. We test for
526 this special case below. */
540 else if (filename
== pattern
)
550 dirlen
= filename
- pattern
;
551 #if defined __MSDOS__ || defined WINDOWS32
553 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
558 drive_spec
= (char *) __alloca (dirlen
+ 1);
560 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
562 memcpy (drive_spec
, pattern
, dirlen
);
563 drive_spec
[dirlen
] = '\0';
565 /* For now, disallow wildcards in the drive spec, to
566 prevent infinite recursion in glob. */
567 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
569 /* If this is "d:pattern", we need to copy `:' to DIRNAME
570 as well. If it's "d:/pattern", don't remove the slash
571 from "d:/", since "d:" and "d:/" are not the same.*/
574 newp
= (char *) __alloca (dirlen
+ 1);
576 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
578 memcpy (newp
, pattern
, dirlen
);
584 if (filename
[0] == '\0'
585 #if defined __MSDOS__ || defined WINDOWS32
586 && dirname
[dirlen
- 1] != ':'
587 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
588 || dirname
[dirlen
- 1] != '/')
591 /* "pattern/". Expand "pattern", appending slashes. */
593 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
595 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
596 | (flags
& GLOB_MARK
));
601 if (!(flags
& GLOB_APPEND
))
604 pglob
->gl_pathv
= NULL
;
607 oldcount
= pglob
->gl_pathc
;
610 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
612 if (dirname
[1] == '\0' || dirname
[1] == '/')
614 /* Look up home directory. */
616 /* This isn't obvious, RTLs of DECC and VAXC know about "HOME" */
617 const char *home_dir
= getenv ("SYS$LOGIN");
619 const char *home_dir
= getenv ("HOME");
622 if (home_dir
== NULL
|| home_dir
[0] == '\0')
626 if (home_dir
== NULL
|| home_dir
[0] == '\0')
627 home_dir
= "c:/users/default"; /* poor default */
630 /* Again, this isn't obvious, if "HOME" isn't known "SYS$LOGIN" should be set */
631 if (home_dir
== NULL
|| home_dir
[0] == '\0')
632 home_dir
= "SYS$DISK:[]";
634 if (home_dir
== NULL
|| home_dir
[0] == '\0')
638 # if defined HAVE_GETLOGIN_R || defined _LIBC
639 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
642 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
645 name
= (char *) __alloca (buflen
);
647 success
= getlogin_r (name
, buflen
) >= 0;
649 success
= (name
= getlogin ()) != NULL
;
654 # if defined HAVE_GETPWNAM_R || defined _LIBC
655 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
661 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
662 Try a moderate value. */
664 pwtmpbuf
= (char *) __alloca (pwbuflen
);
666 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
675 pwtmpbuf
= (char *) __alloca (pwbuflen
);
682 home_dir
= p
->pw_dir
;
685 if (home_dir
== NULL
|| home_dir
[0] == '\0')
687 if (flags
& GLOB_TILDE_CHECK
)
690 home_dir
= "~"; /* No luck. */
693 # endif /* WINDOWS32 */
695 /* Now construct the full directory. */
696 if (dirname
[1] == '\0')
701 size_t home_len
= strlen (home_dir
);
702 newp
= (char *) __alloca (home_len
+ dirlen
);
704 mempcpy (mempcpy (newp
, home_dir
, home_len
),
705 &dirname
[1], dirlen
);
707 memcpy (newp
, home_dir
, home_len
);
708 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
713 # if !defined _AMIGA && !defined WINDOWS32 && !defined VMS
716 char *end_name
= strchr (dirname
, '/');
717 const char *user_name
;
718 const char *home_dir
;
720 if (end_name
== NULL
)
721 user_name
= dirname
+ 1;
725 newp
= (char *) __alloca (end_name
- dirname
);
727 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
730 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
731 newp
[end_name
- dirname
- 1] = '\0';
736 /* Look up specific user's home directory. */
739 # if defined HAVE_GETPWNAM_R || defined _LIBC
740 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
746 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
749 pwtmpbuf
= (char *) __alloca (buflen
);
751 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
759 pwtmpbuf
= __alloca (buflen
);
763 p
= getpwnam (user_name
);
766 home_dir
= p
->pw_dir
;
770 /* If we found a home directory use this. */
771 if (home_dir
!= NULL
)
774 size_t home_len
= strlen (home_dir
);
775 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
776 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
778 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
779 end_name
, rest_len
)) = '\0';
781 memcpy (newp
, home_dir
, home_len
);
782 memcpy (&newp
[home_len
], end_name
, rest_len
);
783 newp
[home_len
+ rest_len
] = '\0';
788 if (flags
& GLOB_TILDE_CHECK
)
789 /* We have to regard it as an error if we cannot find the
793 # endif /* Not Amiga && not WINDOWS32 && not VMS. */
795 #endif /* Not VMS. */
797 /* Now test whether we looked for "~" or "~NAME". In this case we
798 can give the answer now. */
799 if (filename
== NULL
)
803 /* Return the directory if we don't check for error or if it exists. */
804 if ((flags
& GLOB_NOCHECK
)
805 || (((flags
& GLOB_ALTDIRFUNC
)
806 ? (*pglob
->gl_stat
) (dirname
, &st
)
807 : __stat (dirname
, &st
)) == 0
808 && S_ISDIR (st
.st_mode
)))
811 = (char **) realloc (pglob
->gl_pathv
,
813 ((flags
& GLOB_DOOFFS
) ?
814 pglob
->gl_offs
: 0) +
817 if (pglob
->gl_pathv
== NULL
)
820 if (flags
& GLOB_DOOFFS
)
821 while (pglob
->gl_pathc
< pglob
->gl_offs
)
822 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
824 #if defined HAVE_STRDUP || defined _LIBC
825 pglob
->gl_pathv
[pglob
->gl_pathc
] = strdup (dirname
);
828 size_t len
= strlen (dirname
) + 1;
829 char *dircopy
= malloc (len
);
831 pglob
->gl_pathv
[pglob
->gl_pathc
] = memcpy (dircopy
, dirname
,
835 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
837 free (pglob
->gl_pathv
);
840 pglob
->gl_pathv
[++pglob
->gl_pathc
] = NULL
;
841 pglob
->gl_flags
= flags
;
850 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
852 /* The directory name contains metacharacters, so we
853 have to glob for the directory, and then glob for
854 the pattern in each directory found. */
858 status
= glob (dirname
,
859 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
))
860 | GLOB_NOSORT
| GLOB_ONLYDIR
),
865 /* We have successfully globbed the preceding directory name.
866 For each name we found, call glob_in_dir on it and FILENAME,
867 appending the results to PGLOB. */
868 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
874 /* Make globbing interruptible in the bash shell. */
875 extern int interrupt_state
;
886 old_pathc
= pglob
->gl_pathc
;
887 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
888 ((flags
| GLOB_APPEND
)
889 & ~(GLOB_NOCHECK
| GLOB_ERR
)),
891 if (status
== GLOB_NOMATCH
)
892 /* No matches in this directory. Try the next. */
902 /* Stick the directory on the front of each name. */
903 if (prefix_array (dirs
.gl_pathv
[i
],
904 &pglob
->gl_pathv
[old_pathc
],
905 pglob
->gl_pathc
- old_pathc
))
913 flags
|= GLOB_MAGCHAR
;
915 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
916 But if we have not found any matching entry and thie GLOB_NOCHECK
917 flag was set we must return the list consisting of the disrectory
918 names followed by the filename. */
919 if (pglob
->gl_pathc
== oldcount
)
922 if (flags
& GLOB_NOCHECK
)
924 size_t filename_len
= strlen (filename
) + 1;
928 /* This is an pessimistic guess about the size. */
930 = (char **) realloc (pglob
->gl_pathv
,
932 ((flags
& GLOB_DOOFFS
) ?
933 pglob
->gl_offs
: 0) +
936 if (pglob
->gl_pathv
== NULL
)
942 if (flags
& GLOB_DOOFFS
)
943 while (pglob
->gl_pathc
< pglob
->gl_offs
)
944 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
946 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
948 const char *dir
= dirs
.gl_pathv
[i
];
949 size_t dir_len
= strlen (dir
);
951 /* First check whether this really is a directory. */
952 if (((flags
& GLOB_ALTDIRFUNC
)
953 ? (*pglob
->gl_stat
) (dir
, &st
) : __stat (dir
, &st
)) != 0
954 || !S_ISDIR (st
.st_mode
))
955 /* No directory, ignore this entry. */
958 pglob
->gl_pathv
[pglob
->gl_pathc
] = malloc (dir_len
+ 1
960 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
968 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[pglob
->gl_pathc
],
971 filename
, filename_len
);
973 memcpy (pglob
->gl_pathv
[pglob
->gl_pathc
], dir
, dir_len
);
974 pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
] = '/';
975 memcpy (&pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
+ 1],
976 filename
, filename_len
);
981 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
982 pglob
->gl_flags
= flags
;
984 /* Now we know how large the gl_pathv vector must be. */
985 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
986 ((pglob
->gl_pathc
+ 1)
988 if (new_pathv
!= NULL
)
989 pglob
->gl_pathv
= new_pathv
;
999 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
1005 /* Stick the directory on the front of each name. */
1006 int ignore
= oldcount
;
1008 if ((flags
& GLOB_DOOFFS
) && ignore
< pglob
->gl_offs
)
1009 ignore
= pglob
->gl_offs
;
1011 if (prefix_array (dirname
,
1012 &pglob
->gl_pathv
[ignore
],
1013 pglob
->gl_pathc
- ignore
))
1016 return GLOB_NOSPACE
;
1021 if (flags
& GLOB_MARK
)
1023 /* Append slashes to directory names. */
1026 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
1027 if (((flags
& GLOB_ALTDIRFUNC
)
1028 ? (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
)
1029 : __stat (pglob
->gl_pathv
[i
], &st
)) == 0
1030 && S_ISDIR (st
.st_mode
))
1032 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1033 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1037 return GLOB_NOSPACE
;
1039 strcpy (&new[len
- 2], "/");
1040 pglob
->gl_pathv
[i
] = new;
1044 if (!(flags
& GLOB_NOSORT
))
1046 /* Sort the vector. */
1047 int non_sort
= oldcount
;
1049 if ((flags
& GLOB_DOOFFS
) && pglob
->gl_offs
> oldcount
)
1050 non_sort
= pglob
->gl_offs
;
1052 qsort ((__ptr_t
) &pglob
->gl_pathv
[non_sort
],
1053 pglob
->gl_pathc
- non_sort
,
1054 sizeof (char *), collated_compare
);
1061 /* Free storage allocated in PGLOB by a previous `glob' call. */
1064 register glob_t
*pglob
;
1066 if (pglob
->gl_pathv
!= NULL
)
1069 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1070 if (pglob
->gl_pathv
[i
] != NULL
)
1071 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
1072 free ((__ptr_t
) pglob
->gl_pathv
);
1077 /* Do a collated comparison of A and B. */
1079 collated_compare (a
, b
)
1083 const char *const s1
= *(const char *const * const) a
;
1084 const char *const s2
= *(const char *const * const) b
;
1092 return strcoll (s1
, s2
);
1096 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1097 elements in place. Return nonzero if out of memory, zero if successful.
1098 A slash is inserted between DIRNAME and each elt of ARRAY,
1099 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1101 prefix_array (dirname
, array
, n
)
1102 const char *dirname
;
1107 size_t dirlen
= strlen (dirname
);
1108 #if defined __MSDOS__ || defined WINDOWS32
1110 # define DIRSEP_CHAR sep_char
1112 # define DIRSEP_CHAR '/'
1115 if (dirlen
== 1 && dirname
[0] == '/')
1116 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1117 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1119 #if defined __MSDOS__ || defined WINDOWS32
1120 else if (dirlen
> 1)
1122 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1123 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1125 else if (dirname
[dirlen
- 1] == ':')
1127 /* DIRNAME is "d:". Use `:' instead of `/'. */
1134 for (i
= 0; i
< n
; ++i
)
1136 size_t eltlen
= strlen (array
[i
]) + 1;
1137 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1141 free ((__ptr_t
) array
[--i
]);
1147 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1148 *endp
++ = DIRSEP_CHAR
;
1149 mempcpy (endp
, array
[i
], eltlen
);
1152 memcpy (new, dirname
, dirlen
);
1153 new[dirlen
] = DIRSEP_CHAR
;
1154 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1156 free ((__ptr_t
) array
[i
]);
1164 /* We must not compile this function twice. */
1165 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1166 /* Return nonzero if PATTERN contains any metacharacters.
1167 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1169 __glob_pattern_p (pattern
, quote
)
1170 const char *pattern
;
1173 register const char *p
;
1176 for (p
= pattern
; *p
!= '\0'; ++p
)
1184 if (quote
&& p
[1] != '\0')
1201 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1206 /* Like `glob', but PATTERN is a final pathname component,
1207 and matches are searched for in DIRECTORY.
1208 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1209 The GLOB_APPEND flag is assumed to be set (always appends). */
1211 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1212 const char *pattern
;
1213 const char *directory
;
1215 int (*errfunc
) __P ((const char *, int));
1218 __ptr_t stream
= NULL
;
1222 struct globlink
*next
;
1225 struct globlink
*names
= NULL
;
1231 if (*directory
== 0)
1234 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1237 if (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
))
1238 /* We need not do any tests. The PATTERN contains no meta
1239 characters and we must not return an error therefore the
1240 result will always contain exactly one name. */
1241 flags
|= GLOB_NOCHECK
;
1244 /* Since we use the normal file functions we can also use stat()
1245 to verify the file is there. */
1247 size_t patlen
= strlen (pattern
);
1248 size_t dirlen
= strlen (directory
);
1249 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1251 # ifdef HAVE_MEMPCPY
1252 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1254 pattern
, patlen
+ 1);
1256 memcpy (fullname
, directory
, dirlen
);
1257 fullname
[dirlen
] = '/';
1258 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1260 if (((flags
& GLOB_ALTDIRFUNC
)
1261 ? (*pglob
->gl_stat
) (fullname
, &st
)
1262 : __stat (fullname
, &st
)) == 0)
1263 /* We found this file to be existing. Now tell the rest
1264 of the function to copy this name into the result. */
1265 flags
|= GLOB_NOCHECK
;
1272 if (pattern
[0] == '\0')
1274 /* This is a special case for matching directories like in
1276 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1277 names
->name
= (char *) malloc (1);
1278 if (names
->name
== NULL
)
1280 names
->name
[0] = '\0';
1287 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1288 ? (*pglob
->gl_opendir
) (directory
)
1289 : (__ptr_t
) opendir (directory
));
1292 if (errno
!= ENOTDIR
1293 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1294 || (flags
& GLOB_ERR
)))
1295 return GLOB_ABORTED
;
1301 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1302 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1303 #if defined HAVE_CASE_INSENSITIVE_FS
1308 flags
|= GLOB_MAGCHAR
;
1314 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1315 ? (*pglob
->gl_readdir
) (stream
)
1316 : readdir ((DIR *) stream
));
1319 if (! REAL_DIR_ENTRY (d
))
1323 /* If we shall match only directories use the information
1324 provided by the dirent call if possible. */
1325 if ((flags
& GLOB_ONLYDIR
)
1326 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1332 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1334 struct globlink
*new = (struct globlink
*)
1335 __alloca (sizeof (struct globlink
));
1337 new->name
= (char *) malloc (len
+ 1);
1338 if (new->name
== NULL
)
1341 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1344 memcpy ((__ptr_t
) new->name
, name
, len
);
1345 new->name
[len
] = '\0';
1356 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1358 size_t len
= strlen (pattern
);
1360 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1362 names
->name
= (char *) malloc (len
+ 1);
1363 if (names
->name
== NULL
)
1366 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1368 memcpy (names
->name
, pattern
, len
);
1369 names
->name
[len
] = '\0';
1376 = (char **) realloc (pglob
->gl_pathv
,
1378 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
1381 if (pglob
->gl_pathv
== NULL
)
1384 if (flags
& GLOB_DOOFFS
)
1385 while (pglob
->gl_pathc
< pglob
->gl_offs
)
1386 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
1388 for (; names
!= NULL
; names
= names
->next
)
1389 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
1390 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
1392 pglob
->gl_flags
= flags
;
1398 if (flags
& GLOB_ALTDIRFUNC
)
1399 (*pglob
->gl_closedir
) (stream
);
1401 closedir ((DIR *) stream
);
1405 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1410 if (flags
& GLOB_ALTDIRFUNC
)
1411 (*pglob
->gl_closedir
) (stream
);
1413 closedir ((DIR *) stream
);
1416 while (names
!= NULL
)
1418 if (names
->name
!= NULL
)
1419 free ((__ptr_t
) names
->name
);
1420 names
= names
->next
;
1422 return GLOB_NOSPACE
;
1425 #endif /* Not ELIDE_CODE. */