1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999 Free
2 Software Foundation, Inc.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the Free
16 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
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
62 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
66 #if defined HAVE_UNISTD_H || defined _LIBC
69 # ifdef _POSIX_VERSION
75 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
79 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
83 # define __set_errno(val) errno = (val)
91 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
93 # define NAMLEN(dirent) strlen((dirent)->d_name)
95 # define dirent direct
96 # define NAMLEN(dirent) (dirent)->d_namlen
97 # ifdef HAVE_SYS_NDIR_H
98 # include <sys/ndir.h>
100 # ifdef HAVE_SYS_DIR_H
101 # include <sys/dir.h>
106 # ifdef HAVE_VMSDIR_H
108 # endif /* HAVE_VMSDIR_H */
112 /* In GNU systems, <dirent.h> defines this macro for us. */
115 # define NAMLEN(d) _D_NAMLEN(d)
118 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
119 if the `d_type' member for `struct dirent' is available. */
120 #ifdef _DIRENT_HAVE_D_TYPE
121 # define HAVE_D_TYPE 1
125 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
126 /* Posix does not require that the d_ino field be present, and some
127 systems do not provide it. */
128 # define REAL_DIR_ENTRY(dp) 1
130 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
133 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
137 #else /* No standard headers. */
139 extern char *getenv ();
141 # ifdef HAVE_STRING_H
145 # include <strings.h>
147 # ifdef HAVE_MEMORY_H
151 extern char *malloc (), *realloc ();
154 extern void qsort ();
155 extern void abort (), exit ();
157 #endif /* Standard headers. */
162 extern void bzero ();
165 extern void bcopy ();
168 # define memcpy(d, s, n) bcopy ((s), (d), (n))
169 # define strrchr rindex
170 /* memset is only used for zero here, but let's be paranoid. */
171 # define memset(s, better_be_zero, n) \
172 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
173 #endif /* Not ANSI_STRING. */
175 #if !defined HAVE_STRCOLL && !defined _LIBC
176 # define strcoll strcmp
179 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
180 # define HAVE_MEMPCPY 1
182 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
185 #if !defined __GNU_LIBRARY__ && !defined __DJGPP__
192 my_realloc (void *p, unsigned int n)
200 /* These casts are the for sake of the broken Ultrix compiler,
201 which warns of illegal pointer combinations otherwise. */
203 return (char *) malloc (n);
204 return (char *) realloc (p, n);
206 # define realloc my_realloc
208 #endif /* __GNU_LIBRARY__ || __DJGPP__ */
211 #if !defined __alloca && !defined __GNU_LIBRARY__
215 # define alloca(n) __builtin_alloca (n)
216 # else /* Not GCC. */
217 # ifdef HAVE_ALLOCA_H
219 # else /* Not HAVE_ALLOCA_H. */
224 extern char *alloca ();
225 # endif /* WINDOWS32 */
226 # endif /* Not _AIX. */
227 # endif /* sparc or HAVE_ALLOCA_H. */
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 #if !defined __alloca
285 # define __alloca alloca
292 #ifdef HAVE_GETLOGIN_R
293 extern int getlogin_r __P ((char *, size_t));
295 extern char *getlogin __P ((void));
299 #if __GNUC__ - 0 >= 2
302 const char *next_brace_sub __P ((const char *begin));
303 static int glob_in_dir __P ((const char *pattern, const char *directory,
305 int (*errfunc) (const char *, int),
307 static int prefix_array __P ((const char *prefix, char **array, size_t n));
308 static int collated_compare __P ((const __ptr_t, const __ptr_t));
310 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
311 int __glob_pattern_p __P ((const char *pattern, int quote));
314 /* Find the end of the sub-pattern in a brace expression. We define
315 this as an inline function if the compiler permits. */
317 #if __GNUC__ - 0 >= 2
321 next_brace_sub (begin)
324 unsigned int depth = 0;
325 const char *cp = begin;
331 if (*cp != ',' && *cp != '}' && *cp != '\0')
341 while (*cp != '\0' && (*cp != '}' || depth > 0))
348 /* An incorrectly terminated brace expression. */
359 /* Do glob searching for PATTERN, placing results in PGLOB.
360 The bits defined above may be set in FLAGS.
361 If a directory cannot be opened or read and ERRFUNC is not nil,
362 it is called with the pathname that caused the error, and the
363 `errno' value from the failing call; if it returns non-zero
364 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
365 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
366 Otherwise, `glob' returns zero. */
368 glob (pattern, flags, errfunc, pglob)
371 int (*errfunc) __P ((const char *, int));
374 const char *filename;
380 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
382 __set_errno (EINVAL);
386 if (flags & GLOB_BRACE)
388 const char *begin = strchr (pattern, '{');
391 /* Allocate working buffer large enough for our work. Note that
392 we have at least an opening and closing brace. */
400 char onealt[strlen (pattern) - 1];
402 char *onealt = (char *) malloc (strlen (pattern) - 1);
405 if (!(flags & GLOB_APPEND))
411 /* We know the prefix for all sub-patterns. */
413 alt_start = mempcpy (onealt, pattern, begin - pattern);
415 memcpy (onealt, pattern, begin - pattern);
416 alt_start = &onealt[begin - pattern];
419 /* Find the first sub-pattern and at the same time find the
420 rest after the closing brace. */
421 next = next_brace_sub (begin + 1);
424 /* It is an illegal expression. */
428 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
431 /* Now find the end of the whole brace expression. */
435 rest = next_brace_sub (rest + 1);
438 /* It is an illegal expression. */
442 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
445 /* Please note that we now can be sure the brace expression
447 rest_len = strlen (++rest) + 1;
449 /* We have a brace expression. BEGIN points to the opening {,
450 NEXT points past the terminator of the first element, and END
451 points past the final }. We will accumulate result names from
452 recursive runs for each brace alternative in the buffer using
455 if (!(flags & GLOB_APPEND))
457 /* This call is to set a new vector, so clear out the
458 vector so we can append to it. */
460 pglob->gl_pathv = NULL;
462 firstc = pglob->gl_pathc;
469 /* Construct the new glob expression. */
471 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
473 memcpy (alt_start, p, next - p);
474 memcpy (&alt_start[next - p], rest, rest_len);
477 result = glob (onealt,
478 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
479 | GLOB_APPEND), errfunc, pglob);
481 /* If we got an error, return it. */
482 if (result && result != GLOB_NOMATCH)
487 if (!(flags & GLOB_APPEND))
493 /* We saw the last entry. */
497 next = next_brace_sub (p);
498 assert (next != NULL);
505 if (pglob->gl_pathc != firstc)
506 /* We found some entries. */
508 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
513 /* Find the filename. */
514 filename = strrchr (pattern, '/');
515 #if defined __MSDOS__ || defined WINDOWS32
516 /* The case of "d:pattern". Since `:' is not allowed in
517 file names, we can safely assume that wherever it
518 happens in pattern, it signals the filename part. This
519 is so we could some day support patterns like "[a-z]:foo". */
520 if (filename == NULL)
521 filename = strchr (pattern, ':');
522 #endif /* __MSDOS__ || WINDOWS32 */
523 if (filename == NULL)
525 /* This can mean two things: a simple name or "~name". The later
526 case is nothing but a notation for a directory. */
527 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
530 dirlen = strlen (pattern);
532 /* Set FILENAME to NULL as a special flag. This is ugly but
533 other solutions would require much more code. We test for
534 this special case below. */
548 else if (filename == pattern)
558 dirlen = filename - pattern;
559 #if defined __MSDOS__ || defined WINDOWS32
561 || (filename > pattern + 1 && filename[-1] == ':'))
566 drive_spec = (char *) __alloca (dirlen + 1);
568 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
570 memcpy (drive_spec, pattern, dirlen);
571 drive_spec[dirlen] = '\0';
573 /* For now, disallow wildcards in the drive spec, to
574 prevent infinite recursion in glob. */
575 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
577 /* If this is "d:pattern", we need to copy `:' to DIRNAME
578 as well. If it's "d:/pattern", don't remove the slash
579 from "d:/", since "d:" and "d:/" are not the same.*/
582 newp = (char *) __alloca (dirlen + 1);
584 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
586 memcpy (newp, pattern, dirlen);
592 if (filename[0] == '\0'
593 #if defined __MSDOS__ || defined WINDOWS32
594 && dirname[dirlen - 1] != ':'
595 && (dirlen < 3 || dirname[dirlen - 2] != ':'
596 || dirname[dirlen - 1] != '/')
599 /* "pattern/". Expand "pattern", appending slashes. */
601 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
603 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
604 | (flags & GLOB_MARK));
609 if (!(flags & GLOB_APPEND))
612 pglob->gl_pathv = NULL;
615 oldcount = pglob->gl_pathc;
618 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
620 if (dirname[1] == '\0' || dirname[1] == '/')
622 /* Look up home directory. */
624 /* This isn't obvious, RTLs of DECC and VAXC know about "HOME" */
625 const char *home_dir = getenv ("SYS$LOGIN");
627 const char *home_dir = getenv ("HOME");
630 if (home_dir == NULL || home_dir[0] == '\0')
634 if (home_dir == NULL || home_dir[0] == '\0')
635 home_dir = "c:/users/default"; /* poor default */
638 /* Again, this isn't obvious, if "HOME" isn't known "SYS$LOGIN" should be set */
639 if (home_dir == NULL || home_dir[0] == '\0')
640 home_dir = "SYS$DISK:[]";
642 if (home_dir == NULL || home_dir[0] == '\0')
646 # if defined HAVE_GETLOGIN_R || defined _LIBC
647 size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
650 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
653 name = (char *) __alloca (buflen);
655 success = getlogin_r (name, buflen) >= 0;
657 success = (name = getlogin ()) != NULL;
662 # if defined HAVE_GETPWNAM_R || defined _LIBC
663 size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
669 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
670 Try a moderate value. */
672 pwtmpbuf = (char *) __alloca (pwbuflen);
674 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
683 pwtmpbuf = (char *) __alloca (pwbuflen);
690 home_dir = p->pw_dir;
693 if (home_dir == NULL || home_dir[0] == '\0')
695 if (flags & GLOB_TILDE_CHECK)
698 home_dir = "~"; /* No luck. */
701 # endif /* WINDOWS32 */
703 /* Now construct the full directory. */
704 if (dirname[1] == '\0')
709 size_t home_len = strlen (home_dir);
710 newp = (char *) __alloca (home_len + dirlen);
712 mempcpy (mempcpy (newp, home_dir, home_len),
713 &dirname[1], dirlen);
715 memcpy (newp, home_dir, home_len);
716 memcpy (&newp[home_len], &dirname[1], dirlen);
721 # if !defined _AMIGA && !defined WINDOWS32 && !defined VMS
724 char *end_name = strchr (dirname, '/');
725 const char *user_name;
726 const char *home_dir;
728 if (end_name == NULL)
729 user_name = dirname + 1;
733 newp = (char *) __alloca (end_name - dirname);
735 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
738 memcpy (newp, dirname + 1, end_name - dirname);
739 newp[end_name - dirname - 1] = '\0';
744 /* Look up specific user's home directory. */
747 # if defined HAVE_GETPWNAM_R || defined _LIBC
748 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
754 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
757 pwtmpbuf = (char *) __alloca (buflen);
759 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
767 pwtmpbuf = __alloca (buflen);
771 p = getpwnam (user_name);
774 home_dir = p->pw_dir;
778 /* If we found a home directory use this. */
779 if (home_dir != NULL)
782 size_t home_len = strlen (home_dir);
783 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
784 newp = (char *) __alloca (home_len + rest_len + 1);
786 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
787 end_name, rest_len)) = '\0';
789 memcpy (newp, home_dir, home_len);
790 memcpy (&newp[home_len], end_name, rest_len);
791 newp[home_len + rest_len] = '\0';
796 if (flags & GLOB_TILDE_CHECK)
797 /* We have to regard it as an error if we cannot find the
801 # endif /* Not Amiga && not WINDOWS32 && not VMS. */
803 #endif /* Not VMS. */
805 /* Now test whether we looked for "~" or "~NAME". In this case we
806 can give the answer now. */
807 if (filename == NULL)
811 /* Return the directory if we don't check for error or if it exists. */
812 if ((flags & GLOB_NOCHECK)
813 || (((flags & GLOB_ALTDIRFUNC)
814 ? (*pglob->gl_stat) (dirname, &st)
815 : __stat (dirname, &st)) == 0
816 && S_ISDIR (st.st_mode)))
819 = (char **) realloc (pglob->gl_pathv,
821 ((flags & GLOB_DOOFFS) ?
822 pglob->gl_offs : 0) +
825 if (pglob->gl_pathv == NULL)
828 if (flags & GLOB_DOOFFS)
829 while (pglob->gl_pathc < pglob->gl_offs)
830 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
832 #if defined HAVE_STRDUP || defined _LIBC
833 pglob->gl_pathv[pglob->gl_pathc] = strdup (dirname);
836 size_t len = strlen (dirname) + 1;
837 char *dircopy = malloc (len);
839 pglob->gl_pathv[pglob->gl_pathc] = memcpy (dircopy, dirname,
843 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
845 free (pglob->gl_pathv);
848 pglob->gl_pathv[++pglob->gl_pathc] = NULL;
849 pglob->gl_flags = flags;
858 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
860 /* The directory name contains metacharacters, so we
861 have to glob for the directory, and then glob for
862 the pattern in each directory found. */
866 status = glob (dirname,
867 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE))
868 | GLOB_NOSORT | GLOB_ONLYDIR),
873 /* We have successfully globbed the preceding directory name.
874 For each name we found, call glob_in_dir on it and FILENAME,
875 appending the results to PGLOB. */
876 for (i = 0; i < dirs.gl_pathc; ++i)
882 /* Make globbing interruptible in the bash shell. */
883 extern int interrupt_state;
894 old_pathc = pglob->gl_pathc;
895 status = glob_in_dir (filename, dirs.gl_pathv[i],
896 ((flags | GLOB_APPEND)
897 & ~(GLOB_NOCHECK | GLOB_ERR)),
899 if (status == GLOB_NOMATCH)
900 /* No matches in this directory. Try the next. */
910 /* Stick the directory on the front of each name. */
911 if (prefix_array (dirs.gl_pathv[i],
912 &pglob->gl_pathv[old_pathc],
913 pglob->gl_pathc - old_pathc))
921 flags |= GLOB_MAGCHAR;
923 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
924 But if we have not found any matching entry and thie GLOB_NOCHECK
925 flag was set we must return the list consisting of the disrectory
926 names followed by the filename. */
927 if (pglob->gl_pathc == oldcount)
930 if (flags & GLOB_NOCHECK)
932 size_t filename_len = strlen (filename) + 1;
936 /* This is an pessimistic guess about the size. */
938 = (char **) realloc (pglob->gl_pathv,
940 ((flags & GLOB_DOOFFS) ?
941 pglob->gl_offs : 0) +
944 if (pglob->gl_pathv == NULL)
950 if (flags & GLOB_DOOFFS)
951 while (pglob->gl_pathc < pglob->gl_offs)
952 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
954 for (i = 0; i < dirs.gl_pathc; ++i)
956 const char *dir = dirs.gl_pathv[i];
957 size_t dir_len = strlen (dir);
959 /* First check whether this really is a directory. */
960 if (((flags & GLOB_ALTDIRFUNC)
961 ? (*pglob->gl_stat) (dir, &st) : __stat (dir, &st)) != 0
962 || !S_ISDIR (st.st_mode))
963 /* No directory, ignore this entry. */
966 pglob->gl_pathv[pglob->gl_pathc] = malloc (dir_len + 1
968 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
976 mempcpy (mempcpy (mempcpy (pglob->gl_pathv[pglob->gl_pathc],
979 filename, filename_len);
981 memcpy (pglob->gl_pathv[pglob->gl_pathc], dir, dir_len);
982 pglob->gl_pathv[pglob->gl_pathc][dir_len] = '/';
983 memcpy (&pglob->gl_pathv[pglob->gl_pathc][dir_len + 1],
984 filename, filename_len);
989 pglob->gl_pathv[pglob->gl_pathc] = NULL;
990 pglob->gl_flags = flags;
992 /* Now we know how large the gl_pathv vector must be. */
993 new_pathv = (char **) realloc (pglob->gl_pathv,
994 ((pglob->gl_pathc + 1)
996 if (new_pathv != NULL)
997 pglob->gl_pathv = new_pathv;
1000 return GLOB_NOMATCH;
1007 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
1013 /* Stick the directory on the front of each name. */
1014 int ignore = oldcount;
1016 if ((flags & GLOB_DOOFFS) && ignore < pglob->gl_offs)
1017 ignore = pglob->gl_offs;
1019 if (prefix_array (dirname,
1020 &pglob->gl_pathv[ignore],
1021 pglob->gl_pathc - ignore))
1024 return GLOB_NOSPACE;
1029 if (flags & GLOB_MARK)
1031 /* Append slashes to directory names. */
1034 for (i = oldcount; i < pglob->gl_pathc; ++i)
1035 if (((flags & GLOB_ALTDIRFUNC)
1036 ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
1037 : __stat (pglob->gl_pathv[i], &st)) == 0
1038 && S_ISDIR (st.st_mode))
1040 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1041 char *new = realloc (pglob->gl_pathv[i], len);
1045 return GLOB_NOSPACE;
1047 strcpy (&new[len - 2], "/");
1048 pglob->gl_pathv[i] = new;
1052 if (!(flags & GLOB_NOSORT))
1054 /* Sort the vector. */
1055 int non_sort = oldcount;
1057 if ((flags & GLOB_DOOFFS) && pglob->gl_offs > oldcount)
1058 non_sort = pglob->gl_offs;
1060 qsort ((__ptr_t) &pglob->gl_pathv[non_sort],
1061 pglob->gl_pathc - non_sort,
1062 sizeof (char *), collated_compare);
1069 /* Free storage allocated in PGLOB by a previous `glob' call. */
1072 register glob_t *pglob;
1074 if (pglob->gl_pathv != NULL)
1077 for (i = 0; i < pglob->gl_pathc; ++i)
1078 if (pglob->gl_pathv[i] != NULL)
1079 free ((__ptr_t) pglob->gl_pathv[i]);
1080 free ((__ptr_t) pglob->gl_pathv);
1085 /* Do a collated comparison of A and B. */
1087 collated_compare (a, b)
1091 const char *const s1 = *(const char *const * const) a;
1092 const char *const s2 = *(const char *const * const) b;
1100 return strcoll (s1, s2);
1104 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1105 elements in place. Return nonzero if out of memory, zero if successful.
1106 A slash is inserted between DIRNAME and each elt of ARRAY,
1107 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1109 prefix_array (dirname, array, n)
1110 const char *dirname;
1115 size_t dirlen = strlen (dirname);
1116 #if defined __MSDOS__ || defined WINDOWS32
1118 # define DIRSEP_CHAR sep_char
1120 # define DIRSEP_CHAR '/'
1123 if (dirlen == 1 && dirname[0] == '/')
1124 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1125 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1127 #if defined __MSDOS__ || defined WINDOWS32
1128 else if (dirlen > 1)
1130 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1131 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1133 else if (dirname[dirlen - 1] == ':')
1135 /* DIRNAME is "d:". Use `:' instead of `/'. */
1142 for (i = 0; i < n; ++i)
1144 size_t eltlen = strlen (array[i]) + 1;
1145 char *new = (char *) malloc (dirlen + 1 + eltlen);
1149 free ((__ptr_t) array[--i]);
1155 char *endp = (char *) mempcpy (new, dirname, dirlen);
1156 *endp++ = DIRSEP_CHAR;
1157 mempcpy (endp, array[i], eltlen);
1160 memcpy (new, dirname, dirlen);
1161 new[dirlen] = DIRSEP_CHAR;
1162 memcpy (&new[dirlen + 1], array[i], eltlen);
1164 free ((__ptr_t) array[i]);
1172 /* We must not compile this function twice. */
1173 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1174 /* Return nonzero if PATTERN contains any metacharacters.
1175 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1177 __glob_pattern_p (pattern, quote)
1178 const char *pattern;
1181 register const char *p;
1184 for (p = pattern; *p != '\0'; ++p)
1192 if (quote && p[1] != '\0')
1209 weak_alias (__glob_pattern_p, glob_pattern_p)
1214 /* Like `glob', but PATTERN is a final pathname component,
1215 and matches are searched for in DIRECTORY.
1216 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1217 The GLOB_APPEND flag is assumed to be set (always appends). */
1219 glob_in_dir (pattern, directory, flags, errfunc, pglob)
1220 const char *pattern;
1221 const char *directory;
1223 int (*errfunc) __P ((const char *, int));
1226 __ptr_t stream = NULL;
1230 struct globlink *next;
1233 struct globlink *names = NULL;
1239 if (*directory == 0)
1242 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1245 if (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))
1246 /* We need not do any tests. The PATTERN contains no meta
1247 characters and we must not return an error therefore the
1248 result will always contain exactly one name. */
1249 flags |= GLOB_NOCHECK;
1252 /* Since we use the normal file functions we can also use stat()
1253 to verify the file is there. */
1255 size_t patlen = strlen (pattern);
1256 size_t dirlen = strlen (directory);
1257 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1259 # ifdef HAVE_MEMPCPY
1260 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1262 pattern, patlen + 1);
1264 memcpy (fullname, directory, dirlen);
1265 fullname[dirlen] = '/';
1266 memcpy (&fullname[dirlen + 1], pattern, patlen + 1);
1268 if (((flags & GLOB_ALTDIRFUNC)
1269 ? (*pglob->gl_stat) (fullname, &st)
1270 : __stat (fullname, &st)) == 0)
1271 /* We found this file to be existing. Now tell the rest
1272 of the function to copy this name into the result. */
1273 flags |= GLOB_NOCHECK;
1280 if (pattern[0] == '\0')
1282 /* This is a special case for matching directories like in
1284 names = (struct globlink *) __alloca (sizeof (struct globlink));
1285 names->name = (char *) malloc (1);
1286 if (names->name == NULL)
1288 names->name[0] = '\0';
1295 stream = ((flags & GLOB_ALTDIRFUNC)
1296 ? (*pglob->gl_opendir) (directory)
1297 : (__ptr_t) opendir (directory));
1300 if (errno != ENOTDIR
1301 && ((errfunc != NULL && (*errfunc) (directory, errno))
1302 || (flags & GLOB_ERR)))
1303 return GLOB_ABORTED;
1309 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1310 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1311 #if defined HAVE_CASE_INSENSITIVE_FS
1316 flags |= GLOB_MAGCHAR;
1322 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1323 ? (*pglob->gl_readdir) (stream)
1324 : readdir ((DIR *) stream));
1327 if (! REAL_DIR_ENTRY (d))
1331 /* If we shall match only directories use the information
1332 provided by the dirent call if possible. */
1333 if ((flags & GLOB_ONLYDIR)
1334 && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
1340 if (fnmatch (pattern, name, fnm_flags) == 0)
1342 struct globlink *new = (struct globlink *)
1343 __alloca (sizeof (struct globlink));
1345 new->name = (char *) malloc (len + 1);
1346 if (new->name == NULL)
1349 *((char *) mempcpy ((__ptr_t) new->name, name, len))
1352 memcpy ((__ptr_t) new->name, name, len);
1353 new->name[len] = '\0';
1364 if (nfound == 0 && (flags & GLOB_NOCHECK))
1366 size_t len = strlen (pattern);
1368 names = (struct globlink *) __alloca (sizeof (struct globlink));
1370 names->name = (char *) malloc (len + 1);
1371 if (names->name == NULL)
1374 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1376 memcpy (names->name, pattern, len);
1377 names->name[len] = '\0';
1384 = (char **) realloc (pglob->gl_pathv,
1386 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
1389 if (pglob->gl_pathv == NULL)
1392 if (flags & GLOB_DOOFFS)
1393 while (pglob->gl_pathc < pglob->gl_offs)
1394 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
1396 for (; names != NULL; names = names->next)
1397 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
1398 pglob->gl_pathv[pglob->gl_pathc] = NULL;
1400 pglob->gl_flags = flags;
1406 if (flags & GLOB_ALTDIRFUNC)
1407 (*pglob->gl_closedir) (stream);
1409 closedir ((DIR *) stream);
1413 return nfound == 0 ? GLOB_NOMATCH : 0;
1418 if (flags & GLOB_ALTDIRFUNC)
1419 (*pglob->gl_closedir) (stream);
1421 closedir ((DIR *) stream);
1424 while (names != NULL)
1426 if (names->name != NULL)
1427 free ((__ptr_t) names->name);
1428 names = names->next;
1430 return GLOB_NOSPACE;
1433 #endif /* Not ELIDE_CODE. */