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
14 License along with this library; see the file COPYING.LIB. If not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
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
));
303 /* these compilers like prototypes */
304 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
305 int __glob_pattern_p (const char *pattern
, int quote
);
309 /* Find the end of the sub-pattern in a brace expression. We define
310 this as an inline function if the compiler permits. */
312 #if __GNUC__ - 0 >= 2
316 next_brace_sub (begin
)
319 unsigned int depth
= 0;
320 const char *cp
= begin
;
326 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
336 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
343 /* An incorrectly terminated brace expression. */
354 /* Do glob searching for PATTERN, placing results in PGLOB.
355 The bits defined above may be set in FLAGS.
356 If a directory cannot be opened or read and ERRFUNC is not nil,
357 it is called with the pathname that caused the error, and the
358 `errno' value from the failing call; if it returns non-zero
359 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
360 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
361 Otherwise, `glob' returns zero. */
363 glob (pattern
, flags
, errfunc
, pglob
)
366 int (*errfunc
) __P ((const char *, int));
369 const char *filename
;
375 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
377 __set_errno (EINVAL
);
381 if (flags
& GLOB_BRACE
)
383 const char *begin
= strchr (pattern
, '{');
386 /* Allocate working buffer large enough for our work. Note that
387 we have at least an opening and closing brace. */
395 char onealt
[strlen (pattern
) - 1];
397 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
400 if (!(flags
& GLOB_APPEND
))
406 /* We know the prefix for all sub-patterns. */
408 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
410 memcpy (onealt
, pattern
, begin
- pattern
);
411 alt_start
= &onealt
[begin
- pattern
];
414 /* Find the first sub-pattern and at the same time find the
415 rest after the closing brace. */
416 next
= next_brace_sub (begin
+ 1);
419 /* It is an illegal expression. */
423 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
426 /* Now find the end of the whole brace expression. */
430 rest
= next_brace_sub (rest
+ 1);
433 /* It is an illegal expression. */
437 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
440 /* Please note that we now can be sure the brace expression
442 rest_len
= strlen (++rest
) + 1;
444 /* We have a brace expression. BEGIN points to the opening {,
445 NEXT points past the terminator of the first element, and END
446 points past the final }. We will accumulate result names from
447 recursive runs for each brace alternative in the buffer using
450 if (!(flags
& GLOB_APPEND
))
452 /* This call is to set a new vector, so clear out the
453 vector so we can append to it. */
455 pglob
->gl_pathv
= NULL
;
457 firstc
= pglob
->gl_pathc
;
464 /* Construct the new glob expression. */
466 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
468 memcpy (alt_start
, p
, next
- p
);
469 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
472 result
= glob (onealt
,
473 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
474 | GLOB_APPEND
), errfunc
, pglob
);
476 /* If we got an error, return it. */
477 if (result
&& result
!= GLOB_NOMATCH
)
482 if (!(flags
& GLOB_APPEND
))
488 /* We saw the last entry. */
492 next
= next_brace_sub (p
);
493 assert (next
!= NULL
);
500 if (pglob
->gl_pathc
!= firstc
)
501 /* We found some entries. */
503 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
508 /* Find the filename. */
509 filename
= strrchr (pattern
, '/');
510 #if defined __MSDOS__ || defined WINDOWS32
511 /* The case of "d:pattern". Since `:' is not allowed in
512 file names, we can safely assume that wherever it
513 happens in pattern, it signals the filename part. This
514 is so we could some day support patterns like "[a-z]:foo". */
515 if (filename
== NULL
)
516 filename
= strchr (pattern
, ':');
517 #endif /* __MSDOS__ || WINDOWS32 */
518 if (filename
== NULL
)
520 /* This can mean two things: a simple name or "~name". The later
521 case is nothing but a notation for a directory. */
522 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
525 dirlen
= strlen (pattern
);
527 /* Set FILENAME to NULL as a special flag. This is ugly but
528 other solutions would require much more code. We test for
529 this special case below. */
543 else if (filename
== pattern
)
553 dirlen
= filename
- pattern
;
554 #if defined __MSDOS__ || defined WINDOWS32
556 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
561 drive_spec
= (char *) __alloca (dirlen
+ 1);
563 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
565 memcpy (drive_spec
, pattern
, dirlen
);
566 drive_spec
[dirlen
] = '\0';
568 /* For now, disallow wildcards in the drive spec, to
569 prevent infinite recursion in glob. */
570 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
572 /* If this is "d:pattern", we need to copy `:' to DIRNAME
573 as well. If it's "d:/pattern", don't remove the slash
574 from "d:/", since "d:" and "d:/" are not the same.*/
577 newp
= (char *) __alloca (dirlen
+ 1);
579 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
581 memcpy (newp
, pattern
, dirlen
);
587 if (filename
[0] == '\0'
588 #if defined __MSDOS__ || defined WINDOWS32
589 && dirname
[dirlen
- 1] != ':'
590 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
591 || dirname
[dirlen
- 1] != '/')
594 /* "pattern/". Expand "pattern", appending slashes. */
596 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
598 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
599 | (flags
& GLOB_MARK
));
604 if (!(flags
& GLOB_APPEND
))
607 pglob
->gl_pathv
= NULL
;
610 oldcount
= pglob
->gl_pathc
;
613 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
615 if (dirname
[1] == '\0' || dirname
[1] == '/')
617 /* Look up home directory. */
619 /* This isn't obvious, RTLs of DECC and VAXC know about "HOME" */
620 const char *home_dir
= getenv ("SYS$LOGIN");
622 const char *home_dir
= getenv ("HOME");
625 if (home_dir
== NULL
|| home_dir
[0] == '\0')
629 if (home_dir
== NULL
|| home_dir
[0] == '\0')
630 home_dir
= "c:/users/default"; /* poor default */
633 /* Again, this isn't obvious, if "HOME" isn't known "SYS$LOGIN" should be set */
634 if (home_dir
== NULL
|| home_dir
[0] == '\0')
635 home_dir
= "SYS$DISK:[]";
637 if (home_dir
== NULL
|| home_dir
[0] == '\0')
641 # if defined HAVE_GETLOGIN_R || defined _LIBC
642 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
645 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
648 name
= (char *) __alloca (buflen
);
650 success
= getlogin_r (name
, buflen
) >= 0;
652 success
= (name
= getlogin ()) != NULL
;
657 # if defined HAVE_GETPWNAM_R || defined _LIBC
658 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
664 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
665 Try a moderate value. */
667 pwtmpbuf
= (char *) __alloca (pwbuflen
);
669 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
678 pwtmpbuf
= (char *) __alloca (pwbuflen
);
685 home_dir
= p
->pw_dir
;
688 if (home_dir
== NULL
|| home_dir
[0] == '\0')
690 if (flags
& GLOB_TILDE_CHECK
)
693 home_dir
= "~"; /* No luck. */
696 # endif /* WINDOWS32 */
698 /* Now construct the full directory. */
699 if (dirname
[1] == '\0')
704 size_t home_len
= strlen (home_dir
);
705 newp
= (char *) __alloca (home_len
+ dirlen
);
707 mempcpy (mempcpy (newp
, home_dir
, home_len
),
708 &dirname
[1], dirlen
);
710 memcpy (newp
, home_dir
, home_len
);
711 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
716 # if !defined _AMIGA && !defined WINDOWS32 && !defined VMS
719 char *end_name
= strchr (dirname
, '/');
720 const char *user_name
;
721 const char *home_dir
;
723 if (end_name
== NULL
)
724 user_name
= dirname
+ 1;
728 newp
= (char *) __alloca (end_name
- dirname
);
730 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
733 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
734 newp
[end_name
- dirname
- 1] = '\0';
739 /* Look up specific user's home directory. */
742 # if defined HAVE_GETPWNAM_R || defined _LIBC
743 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
749 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
752 pwtmpbuf
= (char *) __alloca (buflen
);
754 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
762 pwtmpbuf
= __alloca (buflen
);
766 p
= getpwnam (user_name
);
769 home_dir
= p
->pw_dir
;
773 /* If we found a home directory use this. */
774 if (home_dir
!= NULL
)
777 size_t home_len
= strlen (home_dir
);
778 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
779 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
781 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
782 end_name
, rest_len
)) = '\0';
784 memcpy (newp
, home_dir
, home_len
);
785 memcpy (&newp
[home_len
], end_name
, rest_len
);
786 newp
[home_len
+ rest_len
] = '\0';
791 if (flags
& GLOB_TILDE_CHECK
)
792 /* We have to regard it as an error if we cannot find the
796 # endif /* Not Amiga && not WINDOWS32 && not VMS. */
798 #endif /* Not VMS. */
800 /* Now test whether we looked for "~" or "~NAME". In this case we
801 can give the answer now. */
802 if (filename
== NULL
)
806 /* Return the directory if we don't check for error or if it exists. */
807 if ((flags
& GLOB_NOCHECK
)
808 || (((flags
& GLOB_ALTDIRFUNC
)
809 ? (*pglob
->gl_stat
) (dirname
, &st
)
810 : __stat (dirname
, &st
)) == 0
811 && S_ISDIR (st
.st_mode
)))
814 = (char **) realloc (pglob
->gl_pathv
,
816 ((flags
& GLOB_DOOFFS
) ?
817 pglob
->gl_offs
: 0) +
820 if (pglob
->gl_pathv
== NULL
)
823 if (flags
& GLOB_DOOFFS
)
824 while (pglob
->gl_pathc
< pglob
->gl_offs
)
825 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
827 #if defined HAVE_STRDUP || defined _LIBC
828 pglob
->gl_pathv
[pglob
->gl_pathc
] = strdup (dirname
);
831 size_t len
= strlen (dirname
) + 1;
832 char *dircopy
= malloc (len
);
834 pglob
->gl_pathv
[pglob
->gl_pathc
] = memcpy (dircopy
, dirname
,
838 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
840 free (pglob
->gl_pathv
);
843 pglob
->gl_pathv
[++pglob
->gl_pathc
] = NULL
;
844 pglob
->gl_flags
= flags
;
853 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
855 /* The directory name contains metacharacters, so we
856 have to glob for the directory, and then glob for
857 the pattern in each directory found. */
861 status
= glob (dirname
,
862 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
))
863 | GLOB_NOSORT
| GLOB_ONLYDIR
),
868 /* We have successfully globbed the preceding directory name.
869 For each name we found, call glob_in_dir on it and FILENAME,
870 appending the results to PGLOB. */
871 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
877 /* Make globbing interruptible in the bash shell. */
878 extern int interrupt_state
;
889 old_pathc
= pglob
->gl_pathc
;
890 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
891 ((flags
| GLOB_APPEND
)
892 & ~(GLOB_NOCHECK
| GLOB_ERR
)),
894 if (status
== GLOB_NOMATCH
)
895 /* No matches in this directory. Try the next. */
905 /* Stick the directory on the front of each name. */
906 if (prefix_array (dirs
.gl_pathv
[i
],
907 &pglob
->gl_pathv
[old_pathc
],
908 pglob
->gl_pathc
- old_pathc
))
916 flags
|= GLOB_MAGCHAR
;
918 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
919 But if we have not found any matching entry and thie GLOB_NOCHECK
920 flag was set we must return the list consisting of the disrectory
921 names followed by the filename. */
922 if (pglob
->gl_pathc
== oldcount
)
925 if (flags
& GLOB_NOCHECK
)
927 size_t filename_len
= strlen (filename
) + 1;
931 /* This is an pessimistic guess about the size. */
933 = (char **) realloc (pglob
->gl_pathv
,
935 ((flags
& GLOB_DOOFFS
) ?
936 pglob
->gl_offs
: 0) +
939 if (pglob
->gl_pathv
== NULL
)
945 if (flags
& GLOB_DOOFFS
)
946 while (pglob
->gl_pathc
< pglob
->gl_offs
)
947 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
949 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
951 const char *dir
= dirs
.gl_pathv
[i
];
952 size_t dir_len
= strlen (dir
);
954 /* First check whether this really is a directory. */
955 if (((flags
& GLOB_ALTDIRFUNC
)
956 ? (*pglob
->gl_stat
) (dir
, &st
) : __stat (dir
, &st
)) != 0
957 || !S_ISDIR (st
.st_mode
))
958 /* No directory, ignore this entry. */
961 pglob
->gl_pathv
[pglob
->gl_pathc
] = malloc (dir_len
+ 1
963 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
971 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[pglob
->gl_pathc
],
974 filename
, filename_len
);
976 memcpy (pglob
->gl_pathv
[pglob
->gl_pathc
], dir
, dir_len
);
977 pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
] = '/';
978 memcpy (&pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
+ 1],
979 filename
, filename_len
);
984 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
985 pglob
->gl_flags
= flags
;
987 /* Now we know how large the gl_pathv vector must be. */
988 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
989 ((pglob
->gl_pathc
+ 1)
991 if (new_pathv
!= NULL
)
992 pglob
->gl_pathv
= new_pathv
;
1002 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
1008 /* Stick the directory on the front of each name. */
1009 int ignore
= oldcount
;
1011 if ((flags
& GLOB_DOOFFS
) && ignore
< pglob
->gl_offs
)
1012 ignore
= pglob
->gl_offs
;
1014 if (prefix_array (dirname
,
1015 &pglob
->gl_pathv
[ignore
],
1016 pglob
->gl_pathc
- ignore
))
1019 return GLOB_NOSPACE
;
1024 if (flags
& GLOB_MARK
)
1026 /* Append slashes to directory names. */
1029 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
1030 if (((flags
& GLOB_ALTDIRFUNC
)
1031 ? (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
)
1032 : __stat (pglob
->gl_pathv
[i
], &st
)) == 0
1033 && S_ISDIR (st
.st_mode
))
1035 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1036 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1040 return GLOB_NOSPACE
;
1042 strcpy (&new[len
- 2], "/");
1043 pglob
->gl_pathv
[i
] = new;
1047 if (!(flags
& GLOB_NOSORT
))
1049 /* Sort the vector. */
1050 int non_sort
= oldcount
;
1052 if ((flags
& GLOB_DOOFFS
) && pglob
->gl_offs
> oldcount
)
1053 non_sort
= pglob
->gl_offs
;
1055 qsort ((__ptr_t
) &pglob
->gl_pathv
[non_sort
],
1056 pglob
->gl_pathc
- non_sort
,
1057 sizeof (char *), collated_compare
);
1064 /* Free storage allocated in PGLOB by a previous `glob' call. */
1067 register glob_t
*pglob
;
1069 if (pglob
->gl_pathv
!= NULL
)
1072 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1073 if (pglob
->gl_pathv
[i
] != NULL
)
1074 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
1075 free ((__ptr_t
) pglob
->gl_pathv
);
1080 /* Do a collated comparison of A and B. */
1082 collated_compare (a
, b
)
1086 const char *const s1
= *(const char *const * const) a
;
1087 const char *const s2
= *(const char *const * const) b
;
1095 return strcoll (s1
, s2
);
1099 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1100 elements in place. Return nonzero if out of memory, zero if successful.
1101 A slash is inserted between DIRNAME and each elt of ARRAY,
1102 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1104 prefix_array (dirname
, array
, n
)
1105 const char *dirname
;
1110 size_t dirlen
= strlen (dirname
);
1111 #if defined __MSDOS__ || defined WINDOWS32
1113 # define DIRSEP_CHAR sep_char
1115 # define DIRSEP_CHAR '/'
1118 if (dirlen
== 1 && dirname
[0] == '/')
1119 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1120 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1122 #if defined __MSDOS__ || defined WINDOWS32
1123 else if (dirlen
> 1)
1125 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1126 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1128 else if (dirname
[dirlen
- 1] == ':')
1130 /* DIRNAME is "d:". Use `:' instead of `/'. */
1137 for (i
= 0; i
< n
; ++i
)
1139 size_t eltlen
= strlen (array
[i
]) + 1;
1140 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1144 free ((__ptr_t
) array
[--i
]);
1150 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1151 *endp
++ = DIRSEP_CHAR
;
1152 mempcpy (endp
, array
[i
], eltlen
);
1155 memcpy (new, dirname
, dirlen
);
1156 new[dirlen
] = DIRSEP_CHAR
;
1157 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1159 free ((__ptr_t
) array
[i
]);
1167 /* We must not compile this function twice. */
1168 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1169 /* Return nonzero if PATTERN contains any metacharacters.
1170 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1172 __glob_pattern_p (pattern
, quote
)
1173 const char *pattern
;
1176 register const char *p
;
1179 for (p
= pattern
; *p
!= '\0'; ++p
)
1187 if (quote
&& p
[1] != '\0')
1204 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1209 /* Like `glob', but PATTERN is a final pathname component,
1210 and matches are searched for in DIRECTORY.
1211 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1212 The GLOB_APPEND flag is assumed to be set (always appends). */
1214 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1215 const char *pattern
;
1216 const char *directory
;
1218 int (*errfunc
) __P ((const char *, int));
1221 __ptr_t stream
= NULL
;
1225 struct globlink
*next
;
1228 struct globlink
*names
= NULL
;
1234 if (*directory
== 0)
1237 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1240 if (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
))
1241 /* We need not do any tests. The PATTERN contains no meta
1242 characters and we must not return an error therefore the
1243 result will always contain exactly one name. */
1244 flags
|= GLOB_NOCHECK
;
1247 /* Since we use the normal file functions we can also use stat()
1248 to verify the file is there. */
1250 size_t patlen
= strlen (pattern
);
1251 size_t dirlen
= strlen (directory
);
1252 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1254 # ifdef HAVE_MEMPCPY
1255 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1257 pattern
, patlen
+ 1);
1259 memcpy (fullname
, directory
, dirlen
);
1260 fullname
[dirlen
] = '/';
1261 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1263 if (((flags
& GLOB_ALTDIRFUNC
)
1264 ? (*pglob
->gl_stat
) (fullname
, &st
)
1265 : __stat (fullname
, &st
)) == 0)
1266 /* We found this file to be existing. Now tell the rest
1267 of the function to copy this name into the result. */
1268 flags
|= GLOB_NOCHECK
;
1275 if (pattern
[0] == '\0')
1277 /* This is a special case for matching directories like in
1279 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1280 names
->name
= (char *) malloc (1);
1281 if (names
->name
== NULL
)
1283 names
->name
[0] = '\0';
1290 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1291 ? (*pglob
->gl_opendir
) (directory
)
1292 : (__ptr_t
) opendir (directory
));
1295 if (errno
!= ENOTDIR
1296 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1297 || (flags
& GLOB_ERR
)))
1298 return GLOB_ABORTED
;
1304 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1305 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1306 #if defined _AMIGA || defined VMS
1311 flags
|= GLOB_MAGCHAR
;
1317 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1318 ? (*pglob
->gl_readdir
) (stream
)
1319 : readdir ((DIR *) stream
));
1322 if (! REAL_DIR_ENTRY (d
))
1326 /* If we shall match only directories use the information
1327 provided by the dirent call if possible. */
1328 if ((flags
& GLOB_ONLYDIR
)
1329 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1335 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1337 struct globlink
*new = (struct globlink
*)
1338 __alloca (sizeof (struct globlink
));
1340 new->name
= (char *) malloc (len
+ 1);
1341 if (new->name
== NULL
)
1344 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1347 memcpy ((__ptr_t
) new->name
, name
, len
);
1348 new->name
[len
] = '\0';
1359 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1361 size_t len
= strlen (pattern
);
1363 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1365 names
->name
= (char *) malloc (len
+ 1);
1366 if (names
->name
== NULL
)
1369 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1371 memcpy (names
->name
, pattern
, len
);
1372 names
->name
[len
] = '\0';
1379 = (char **) realloc (pglob
->gl_pathv
,
1381 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
1384 if (pglob
->gl_pathv
== NULL
)
1387 if (flags
& GLOB_DOOFFS
)
1388 while (pglob
->gl_pathc
< pglob
->gl_offs
)
1389 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
1391 for (; names
!= NULL
; names
= names
->next
)
1392 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
1393 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
1395 pglob
->gl_flags
= flags
;
1401 if (flags
& GLOB_ALTDIRFUNC
)
1402 (*pglob
->gl_closedir
) (stream
);
1404 closedir ((DIR *) stream
);
1408 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1413 if (flags
& GLOB_ALTDIRFUNC
)
1414 (*pglob
->gl_closedir
) (stream
);
1416 closedir ((DIR *) stream
);
1419 while (names
!= NULL
)
1421 if (names
->name
!= NULL
)
1422 free ((__ptr_t
) names
->name
);
1423 names
= names
->next
;
1425 return GLOB_NOSPACE
;
1428 #endif /* Not ELIDE_CODE. */