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 #if defined(_DIRENT_HAVE_D_TYPE) || defined(HAVE_STRUCT_DIRENT_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. */
231 #ifndef __GNU_LIBRARY__
233 # ifdef STAT_MACROS_BROKEN
237 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
243 # define strdup(str) __strdup (str)
244 # define sysconf(id) __sysconf (id)
245 # define closedir(dir) __closedir (dir)
246 # define opendir(name) __opendir (name)
247 # define readdir(str) __readdir (str)
248 # define getpwnam_r(name, bufp, buf, len, res) \
249 __getpwnam_r (name, bufp, buf, len, res)
251 # define __stat(fname, buf) __xstat (_STAT_VER, fname, buf)
255 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
257 # define size_t unsigned int
260 /* Some system header files erroneously define these.
261 We want our own definitions from <fnmatch.h> to take precedence. */
262 #ifndef __GNU_LIBRARY__
269 /* Some system header files erroneously define these.
270 We want our own definitions from <glob.h> to take precedence. */
271 #ifndef __GNU_LIBRARY__
278 # undef GLOB_NOESCAPE
283 #if !defined __alloca
284 # define __alloca alloca
291 #ifdef HAVE_GETLOGIN_R
292 extern int getlogin_r __P ((char *, size_t));
294 extern char *getlogin __P ((void));
298 #if __GNUC__ - 0 >= 2
301 const char *next_brace_sub __P ((const char *begin));
302 static int glob_in_dir __P ((const char *pattern, const char *directory,
304 int (*errfunc) (const char *, int),
306 static int prefix_array __P ((const char *prefix, char **array, size_t n));
307 static int collated_compare __P ((const __ptr_t, const __ptr_t));
309 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
310 int __glob_pattern_p __P ((const char *pattern, int quote));
313 /* Find the end of the sub-pattern in a brace expression. We define
314 this as an inline function if the compiler permits. */
316 #if __GNUC__ - 0 >= 2
320 next_brace_sub (begin)
323 unsigned int depth = 0;
324 const char *cp = begin;
330 if (*cp != ',' && *cp != '}' && *cp != '\0')
340 while (*cp != '\0' && (*cp != '}' || depth > 0))
347 /* An incorrectly terminated brace expression. */
358 /* Do glob searching for PATTERN, placing results in PGLOB.
359 The bits defined above may be set in FLAGS.
360 If a directory cannot be opened or read and ERRFUNC is not nil,
361 it is called with the pathname that caused the error, and the
362 `errno' value from the failing call; if it returns non-zero
363 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
364 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
365 Otherwise, `glob' returns zero. */
367 glob (pattern, flags, errfunc, pglob)
370 int (*errfunc) __P ((const char *, int));
373 const char *filename;
379 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
381 __set_errno (EINVAL);
385 /* POSIX requires all slashes to be matched. This means that with
386 a trailing slash we must match only directories. */
387 if (pattern[0] && pattern[strlen (pattern) - 1] == '/')
388 flags |= GLOB_ONLYDIR;
390 if (flags & GLOB_BRACE)
392 const char *begin = strchr (pattern, '{');
395 /* Allocate working buffer large enough for our work. Note that
396 we have at least an opening and closing brace. */
404 char onealt[strlen (pattern) - 1];
406 char *onealt = (char *) malloc (strlen (pattern) - 1);
409 if (!(flags & GLOB_APPEND))
415 /* We know the prefix for all sub-patterns. */
417 alt_start = mempcpy (onealt, pattern, begin - pattern);
419 memcpy (onealt, pattern, begin - pattern);
420 alt_start = &onealt[begin - pattern];
423 /* Find the first sub-pattern and at the same time find the
424 rest after the closing brace. */
425 next = next_brace_sub (begin + 1);
428 /* It is an illegal expression. */
432 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
435 /* Now find the end of the whole brace expression. */
439 rest = next_brace_sub (rest + 1);
442 /* It is an illegal expression. */
446 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
449 /* Please note that we now can be sure the brace expression
451 rest_len = strlen (++rest) + 1;
453 /* We have a brace expression. BEGIN points to the opening {,
454 NEXT points past the terminator of the first element, and END
455 points past the final }. We will accumulate result names from
456 recursive runs for each brace alternative in the buffer using
459 if (!(flags & GLOB_APPEND))
461 /* This call is to set a new vector, so clear out the
462 vector so we can append to it. */
464 pglob->gl_pathv = NULL;
466 firstc = pglob->gl_pathc;
473 /* Construct the new glob expression. */
475 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
477 memcpy (alt_start, p, next - p);
478 memcpy (&alt_start[next - p], rest, rest_len);
481 result = glob (onealt,
482 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
483 | GLOB_APPEND), errfunc, pglob);
485 /* If we got an error, return it. */
486 if (result && result != GLOB_NOMATCH)
491 if (!(flags & GLOB_APPEND))
497 /* We saw the last entry. */
501 next = next_brace_sub (p);
502 assert (next != NULL);
509 if (pglob->gl_pathc != firstc)
510 /* We found some entries. */
512 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
517 /* Find the filename. */
518 filename = strrchr (pattern, '/');
519 #if defined __MSDOS__ || defined WINDOWS32
520 /* The case of "d:pattern". Since `:' is not allowed in
521 file names, we can safely assume that wherever it
522 happens in pattern, it signals the filename part. This
523 is so we could some day support patterns like "[a-z]:foo". */
524 if (filename == NULL)
525 filename = strchr (pattern, ':');
526 #endif /* __MSDOS__ || WINDOWS32 */
527 if (filename == NULL)
529 /* This can mean two things: a simple name or "~name". The later
530 case is nothing but a notation for a directory. */
531 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
534 dirlen = strlen (pattern);
536 /* Set FILENAME to NULL as a special flag. This is ugly but
537 other solutions would require much more code. We test for
538 this special case below. */
552 else if (filename == pattern)
562 dirlen = filename - pattern;
563 #if defined __MSDOS__ || defined WINDOWS32
565 || (filename > pattern + 1 && filename[-1] == ':'))
570 drive_spec = (char *) __alloca (dirlen + 1);
572 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
574 memcpy (drive_spec, pattern, dirlen);
575 drive_spec[dirlen] = '\0';
577 /* For now, disallow wildcards in the drive spec, to
578 prevent infinite recursion in glob. */
579 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
581 /* If this is "d:pattern", we need to copy `:' to DIRNAME
582 as well. If it's "d:/pattern", don't remove the slash
583 from "d:/", since "d:" and "d:/" are not the same.*/
586 newp = (char *) __alloca (dirlen + 1);
588 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
590 memcpy (newp, pattern, dirlen);
596 if (filename[0] == '\0'
597 #if defined __MSDOS__ || defined WINDOWS32
598 && dirname[dirlen - 1] != ':'
599 && (dirlen < 3 || dirname[dirlen - 2] != ':'
600 || dirname[dirlen - 1] != '/')
603 /* "pattern/". Expand "pattern", appending slashes. */
605 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
607 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
608 | (flags & GLOB_MARK));
613 if (!(flags & GLOB_APPEND))
616 pglob->gl_pathv = NULL;
619 oldcount = pglob->gl_pathc;
622 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
624 if (dirname[1] == '\0' || dirname[1] == '/')
626 /* Look up home directory. */
628 /* This isn't obvious, RTLs of DECC and VAXC know about "HOME" */
629 const char *home_dir = getenv ("SYS$LOGIN");
631 const char *home_dir = getenv ("HOME");
634 if (home_dir == NULL || home_dir[0] == '\0')
638 if (home_dir == NULL || home_dir[0] == '\0')
639 home_dir = "c:/users/default"; /* poor default */
642 /* Again, this isn't obvious, if "HOME" isn't known "SYS$LOGIN" should be set */
643 if (home_dir == NULL || home_dir[0] == '\0')
644 home_dir = "SYS$DISK:[]";
646 if (home_dir == NULL || home_dir[0] == '\0')
650 # if defined HAVE_GETLOGIN_R || defined _LIBC
651 size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
654 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
657 name = (char *) __alloca (buflen);
659 success = getlogin_r (name, buflen) >= 0;
661 success = (name = getlogin ()) != NULL;
666 # if defined HAVE_GETPWNAM_R || defined _LIBC
667 size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
673 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
674 Try a moderate value. */
676 pwtmpbuf = (char *) __alloca (pwbuflen);
678 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
687 pwtmpbuf = (char *) __alloca (pwbuflen);
694 home_dir = p->pw_dir;
697 if (home_dir == NULL || home_dir[0] == '\0')
699 if (flags & GLOB_TILDE_CHECK)
702 home_dir = "~"; /* No luck. */
705 # endif /* WINDOWS32 */
707 /* Now construct the full directory. */
708 if (dirname[1] == '\0')
713 size_t home_len = strlen (home_dir);
714 newp = (char *) __alloca (home_len + dirlen);
716 mempcpy (mempcpy (newp, home_dir, home_len),
717 &dirname[1], dirlen);
719 memcpy (newp, home_dir, home_len);
720 memcpy (&newp[home_len], &dirname[1], dirlen);
725 # if !defined _AMIGA && !defined WINDOWS32 && !defined VMS
728 char *end_name = strchr (dirname, '/');
729 const char *user_name;
730 const char *home_dir;
732 if (end_name == NULL)
733 user_name = dirname + 1;
737 newp = (char *) __alloca (end_name - dirname);
739 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
742 memcpy (newp, dirname + 1, end_name - dirname);
743 newp[end_name - dirname - 1] = '\0';
748 /* Look up specific user's home directory. */
751 # if defined HAVE_GETPWNAM_R || defined _LIBC
752 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
758 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
761 pwtmpbuf = (char *) __alloca (buflen);
763 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
771 pwtmpbuf = __alloca (buflen);
775 p = getpwnam (user_name);
778 home_dir = p->pw_dir;
782 /* If we found a home directory use this. */
783 if (home_dir != NULL)
786 size_t home_len = strlen (home_dir);
787 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
788 newp = (char *) __alloca (home_len + rest_len + 1);
790 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
791 end_name, rest_len)) = '\0';
793 memcpy (newp, home_dir, home_len);
794 memcpy (&newp[home_len], end_name, rest_len);
795 newp[home_len + rest_len] = '\0';
800 if (flags & GLOB_TILDE_CHECK)
801 /* We have to regard it as an error if we cannot find the
805 # endif /* Not Amiga && not WINDOWS32 && not VMS. */
807 #endif /* Not VMS. */
809 /* Now test whether we looked for "~" or "~NAME". In this case we
810 can give the answer now. */
811 if (filename == NULL)
815 /* Return the directory if we don't check for error or if it exists. */
816 if ((flags & GLOB_NOCHECK)
817 || (((flags & GLOB_ALTDIRFUNC)
818 ? (*pglob->gl_stat) (dirname, &st)
819 : __stat (dirname, &st)) == 0
820 && S_ISDIR (st.st_mode)))
823 = (char **) realloc (pglob->gl_pathv,
825 ((flags & GLOB_DOOFFS) ?
826 pglob->gl_offs : 0) +
829 if (pglob->gl_pathv == NULL)
832 if (flags & GLOB_DOOFFS)
833 while (pglob->gl_pathc < pglob->gl_offs)
834 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
836 #if defined HAVE_STRDUP || defined _LIBC
837 pglob->gl_pathv[pglob->gl_pathc] = strdup (dirname);
840 size_t len = strlen (dirname) + 1;
841 char *dircopy = malloc (len);
843 pglob->gl_pathv[pglob->gl_pathc] = memcpy (dircopy, dirname,
847 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
849 free (pglob->gl_pathv);
852 pglob->gl_pathv[++pglob->gl_pathc] = NULL;
853 pglob->gl_flags = flags;
862 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
864 /* The directory name contains metacharacters, so we
865 have to glob for the directory, and then glob for
866 the pattern in each directory found. */
870 status = glob (dirname,
871 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE))
872 | GLOB_NOSORT | GLOB_ONLYDIR),
877 /* We have successfully globbed the preceding directory name.
878 For each name we found, call glob_in_dir on it and FILENAME,
879 appending the results to PGLOB. */
880 for (i = 0; i < dirs.gl_pathc; ++i)
886 /* Make globbing interruptible in the bash shell. */
887 extern int interrupt_state;
898 old_pathc = pglob->gl_pathc;
899 status = glob_in_dir (filename, dirs.gl_pathv[i],
900 ((flags | GLOB_APPEND)
901 & ~(GLOB_NOCHECK | GLOB_ERR)),
903 if (status == GLOB_NOMATCH)
904 /* No matches in this directory. Try the next. */
914 /* Stick the directory on the front of each name. */
915 if (prefix_array (dirs.gl_pathv[i],
916 &pglob->gl_pathv[old_pathc],
917 pglob->gl_pathc - old_pathc))
925 flags |= GLOB_MAGCHAR;
927 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
928 But if we have not found any matching entry and thie GLOB_NOCHECK
929 flag was set we must return the list consisting of the disrectory
930 names followed by the filename. */
931 if (pglob->gl_pathc == oldcount)
934 if (flags & GLOB_NOCHECK)
936 size_t filename_len = strlen (filename) + 1;
940 /* This is an pessimistic guess about the size. */
942 = (char **) realloc (pglob->gl_pathv,
944 ((flags & GLOB_DOOFFS) ?
945 pglob->gl_offs : 0) +
948 if (pglob->gl_pathv == NULL)
954 if (flags & GLOB_DOOFFS)
955 while (pglob->gl_pathc < pglob->gl_offs)
956 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
958 for (i = 0; i < dirs.gl_pathc; ++i)
960 const char *dir = dirs.gl_pathv[i];
961 size_t dir_len = strlen (dir);
963 /* First check whether this really is a directory. */
964 if (((flags & GLOB_ALTDIRFUNC)
965 ? (*pglob->gl_stat) (dir, &st) : __stat (dir, &st)) != 0
966 || !S_ISDIR (st.st_mode))
967 /* No directory, ignore this entry. */
970 pglob->gl_pathv[pglob->gl_pathc] = malloc (dir_len + 1
972 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
980 mempcpy (mempcpy (mempcpy (pglob->gl_pathv[pglob->gl_pathc],
983 filename, filename_len);
985 memcpy (pglob->gl_pathv[pglob->gl_pathc], dir, dir_len);
986 pglob->gl_pathv[pglob->gl_pathc][dir_len] = '/';
987 memcpy (&pglob->gl_pathv[pglob->gl_pathc][dir_len + 1],
988 filename, filename_len);
993 pglob->gl_pathv[pglob->gl_pathc] = NULL;
994 pglob->gl_flags = flags;
996 /* Now we know how large the gl_pathv vector must be. */
997 new_pathv = (char **) realloc (pglob->gl_pathv,
998 ((pglob->gl_pathc + 1)
1000 if (new_pathv != NULL)
1001 pglob->gl_pathv = new_pathv;
1004 return GLOB_NOMATCH;
1011 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
1017 /* Stick the directory on the front of each name. */
1018 size_t ignore = oldcount;
1020 if ((flags & GLOB_DOOFFS) && ignore < pglob->gl_offs)
1021 ignore = pglob->gl_offs;
1023 if (prefix_array (dirname,
1024 &pglob->gl_pathv[ignore],
1025 pglob->gl_pathc - ignore))
1028 return GLOB_NOSPACE;
1033 if (flags & GLOB_MARK)
1035 /* Append slashes to directory names. */
1038 for (i = oldcount; i < pglob->gl_pathc; ++i)
1039 if (((flags & GLOB_ALTDIRFUNC)
1040 ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
1041 : __stat (pglob->gl_pathv[i], &st)) == 0
1042 && S_ISDIR (st.st_mode))
1044 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1045 char *new = realloc (pglob->gl_pathv[i], len);
1049 return GLOB_NOSPACE;
1051 strcpy (&new[len - 2], "/");
1052 pglob->gl_pathv[i] = new;
1056 if (!(flags & GLOB_NOSORT))
1058 /* Sort the vector. */
1059 int non_sort = oldcount;
1061 if ((flags & GLOB_DOOFFS) && pglob->gl_offs > oldcount)
1062 non_sort = pglob->gl_offs;
1064 qsort ((__ptr_t) &pglob->gl_pathv[non_sort],
1065 pglob->gl_pathc - non_sort,
1066 sizeof (char *), collated_compare);
1073 /* Free storage allocated in PGLOB by a previous `glob' call. */
1076 register glob_t *pglob;
1078 if (pglob->gl_pathv != NULL)
1081 for (i = 0; i < pglob->gl_pathc; ++i)
1082 if (pglob->gl_pathv[i] != NULL)
1083 free ((__ptr_t) pglob->gl_pathv[i]);
1084 free ((__ptr_t) pglob->gl_pathv);
1089 /* Do a collated comparison of A and B. */
1091 collated_compare (a, b)
1095 const char *const s1 = *(const char *const * const) a;
1096 const char *const s2 = *(const char *const * const) b;
1104 return strcoll (s1, s2);
1108 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1109 elements in place. Return nonzero if out of memory, zero if successful.
1110 A slash is inserted between DIRNAME and each elt of ARRAY,
1111 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1113 prefix_array (dirname, array, n)
1114 const char *dirname;
1119 size_t dirlen = strlen (dirname);
1120 #if defined __MSDOS__ || defined WINDOWS32
1122 # define DIRSEP_CHAR sep_char
1124 # define DIRSEP_CHAR '/'
1127 if (dirlen == 1 && dirname[0] == '/')
1128 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1129 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1131 #if defined __MSDOS__ || defined WINDOWS32
1132 else if (dirlen > 1)
1134 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1135 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1137 else if (dirname[dirlen - 1] == ':')
1139 /* DIRNAME is "d:". Use `:' instead of `/'. */
1146 for (i = 0; i < n; ++i)
1148 size_t eltlen = strlen (array[i]) + 1;
1149 char *new = (char *) malloc (dirlen + 1 + eltlen);
1153 free ((__ptr_t) array[--i]);
1159 char *endp = (char *) mempcpy (new, dirname, dirlen);
1160 *endp++ = DIRSEP_CHAR;
1161 mempcpy (endp, array[i], eltlen);
1164 memcpy (new, dirname, dirlen);
1165 new[dirlen] = DIRSEP_CHAR;
1166 memcpy (&new[dirlen + 1], array[i], eltlen);
1168 free ((__ptr_t) array[i]);
1176 /* We must not compile this function twice. */
1177 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1178 /* Return nonzero if PATTERN contains any metacharacters.
1179 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1181 __glob_pattern_p (pattern, quote)
1182 const char *pattern;
1185 register const char *p;
1188 for (p = pattern; *p != '\0'; ++p)
1196 if (quote && p[1] != '\0')
1213 weak_alias (__glob_pattern_p, glob_pattern_p)
1218 /* Like `glob', but PATTERN is a final pathname component,
1219 and matches are searched for in DIRECTORY.
1220 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1221 The GLOB_APPEND flag is assumed to be set (always appends). */
1223 glob_in_dir (pattern, directory, flags, errfunc, pglob)
1224 const char *pattern;
1225 const char *directory;
1227 int (*errfunc) __P ((const char *, int));
1230 __ptr_t stream = NULL;
1234 struct globlink *next;
1237 struct globlink *names = NULL;
1243 if (*directory == 0)
1246 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1249 if (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))
1250 /* We need not do any tests. The PATTERN contains no meta
1251 characters and we must not return an error therefore the
1252 result will always contain exactly one name. */
1253 flags |= GLOB_NOCHECK;
1256 /* Since we use the normal file functions we can also use stat()
1257 to verify the file is there. */
1259 size_t patlen = strlen (pattern);
1260 size_t dirlen = strlen (directory);
1261 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1263 # ifdef HAVE_MEMPCPY
1264 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1266 pattern, patlen + 1);
1268 memcpy (fullname, directory, dirlen);
1269 fullname[dirlen] = '/';
1270 memcpy (&fullname[dirlen + 1], pattern, patlen + 1);
1272 if (((flags & GLOB_ALTDIRFUNC)
1273 ? (*pglob->gl_stat) (fullname, &st)
1274 : __stat (fullname, &st)) == 0)
1275 /* We found this file to be existing. Now tell the rest
1276 of the function to copy this name into the result. */
1277 flags |= GLOB_NOCHECK;
1284 if (pattern[0] == '\0')
1286 /* This is a special case for matching directories like in
1288 names = (struct globlink *) __alloca (sizeof (struct globlink));
1289 names->name = (char *) malloc (1);
1290 if (names->name == NULL)
1292 names->name[0] = '\0';
1299 stream = ((flags & GLOB_ALTDIRFUNC)
1300 ? (*pglob->gl_opendir) (directory)
1301 : (__ptr_t) opendir (directory));
1304 if (errno != ENOTDIR
1305 && ((errfunc != NULL && (*errfunc) (directory, errno))
1306 || (flags & GLOB_ERR)))
1307 return GLOB_ABORTED;
1313 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1314 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1315 #if defined HAVE_CASE_INSENSITIVE_FS
1320 flags |= GLOB_MAGCHAR;
1326 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1327 ? (*pglob->gl_readdir) (stream)
1328 : readdir ((DIR *) stream));
1331 if (! REAL_DIR_ENTRY (d))
1335 /* If we shall match only directories use the information
1336 provided by the dirent call if possible. */
1337 if ((flags & GLOB_ONLYDIR)
1338 && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR && d->d_type != DT_LNK)
1344 if (fnmatch (pattern, name, fnm_flags) == 0)
1346 struct globlink *new = (struct globlink *)
1347 __alloca (sizeof (struct globlink));
1349 new->name = (char *) malloc (len + 1);
1350 if (new->name == NULL)
1353 *((char *) mempcpy ((__ptr_t) new->name, name, len))
1356 memcpy ((__ptr_t) new->name, name, len);
1357 new->name[len] = '\0';
1368 if (nfound == 0 && (flags & GLOB_NOCHECK))
1370 size_t len = strlen (pattern);
1372 names = (struct globlink *) __alloca (sizeof (struct globlink));
1374 names->name = (char *) malloc (len + 1);
1375 if (names->name == NULL)
1378 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1380 memcpy (names->name, pattern, len);
1381 names->name[len] = '\0';
1388 = (char **) realloc (pglob->gl_pathv,
1390 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
1393 if (pglob->gl_pathv == NULL)
1396 if (flags & GLOB_DOOFFS)
1397 while (pglob->gl_pathc < pglob->gl_offs)
1398 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
1400 for (; names != NULL; names = names->next)
1401 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
1402 pglob->gl_pathv[pglob->gl_pathc] = NULL;
1404 pglob->gl_flags = flags;
1410 if (flags & GLOB_ALTDIRFUNC)
1411 (*pglob->gl_closedir) (stream);
1413 closedir ((DIR *) stream);
1417 return nfound == 0 ? GLOB_NOMATCH : 0;
1422 if (flags & GLOB_ALTDIRFUNC)
1423 (*pglob->gl_closedir) (stream);
1425 closedir ((DIR *) stream);
1428 while (names != NULL)
1430 if (names->name != NULL)
1431 free ((__ptr_t) names->name);
1432 names = names->next;
1434 return GLOB_NOSPACE;
1437 #endif /* Not ELIDE_CODE. */