1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, 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. */
33 #include <sys/types.h>
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #define GLOB_INTERFACE_VERSION 1
46 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
47 #include <gnu-versions.h>
48 #if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
68 #if !defined (_AMIGA) && !defined (VMS) && !defined(WIN32)
72 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
76 #define __set_errno(val) errno = (val)
84 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
86 # define NAMLEN(dirent) strlen((dirent)->d_name)
88 # define dirent direct
89 # define NAMLEN(dirent) (dirent)->d_namlen
90 # ifdef HAVE_SYS_NDIR_H
91 # include <sys/ndir.h>
93 # ifdef HAVE_SYS_DIR_H
101 # endif /* HAVE_VMSDIR_H */
105 /* In GNU systems, <dirent.h> defines this macro for us. */
108 #define NAMLEN(d) _D_NAMLEN(d)
112 #if (defined (POSIX) || defined (WIN32)) && !defined (__GNU_LIBRARY__)
113 /* Posix does not require that the d_ino field be present, and some
114 systems do not provide it. */
115 #define REAL_DIR_ENTRY(dp) 1
117 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
120 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
124 #else /* No standard headers. */
126 extern char *getenv ();
138 extern char *malloc (), *realloc ();
141 extern void qsort ();
142 extern void abort (), exit ();
144 #endif /* Standard headers. */
149 extern void bzero ();
152 extern void bcopy ();
155 #define memcpy(d, s, n) bcopy ((s), (d), (n))
156 #define strrchr rindex
157 /* memset is only used for zero here, but let's be paranoid. */
158 #define memset(s, better_be_zero, n) \
159 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
160 #endif /* Not ANSI_STRING. */
163 #define strcoll strcmp
167 #ifndef __GNU_LIBRARY__
181 /* These casts are the for sake of the broken Ultrix compiler,
182 which warns of illegal pointer combinations otherwise. */
184 return (char *) malloc (n);
185 return (char *) realloc (p, n);
187 #define realloc my_realloc
189 #endif /* __GNU_LIBRARY__ */
192 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
196 #define alloca(n) __builtin_alloca (n)
200 #else /* Not HAVE_ALLOCA_H. */
205 extern char *alloca ();
207 #endif /* Not _AIX. */
208 #endif /* sparc or HAVE_ALLOCA_H. */
211 #define __alloca alloca
215 #ifndef __GNU_LIBRARY__
217 #ifdef STAT_MACROS_BROKEN
221 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
227 #define size_t unsigned int
230 /* Some system header files erroneously define these.
231 We want our own definitions from <fnmatch.h> to take precedence. */
237 /* Some system header files erroneously define these.
238 We want our own definitions from <glob.h> to take precedence. */
249 static int glob_pattern_p __P ((const char *pattern, int quote));
250 static int glob_in_dir __P ((const char *pattern, const char *directory,
252 int (*errfunc) __P ((const char *, int)),
254 static int prefix_array __P ((const char *prefix, char **array, size_t n));
255 static int collated_compare __P ((const __ptr_t, const __ptr_t));
257 /* Do glob searching for PATTERN, placing results in PGLOB.
258 The bits defined above may be set in FLAGS.
259 If a directory cannot be opened or read and ERRFUNC is not nil,
260 it is called with the pathname that caused the error, and the
261 `errno' value from the failing call; if it returns non-zero
262 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
263 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
264 Otherwise, `glob' returns zero. */
266 glob (pattern, flags, errfunc, pglob)
269 int (*errfunc) __P ((const char *, int));
272 const char *filename;
278 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
280 __set_errno (EINVAL);
284 if (flags & GLOB_BRACE)
286 const char *begin = strchr (pattern, '{');
291 const char *p, *end, *next;
292 unsigned int depth = 0;
294 /* Find the end of the brace expression, by counting braces.
295 While we're at it, notice the first comma at top brace level. */
303 if (depth == 0 && next == NULL)
314 return glob (pattern, flags &~ GLOB_BRACE, errfunc, pglob);
318 restlen = strlen (end) + 1;
322 /* We have a brace expression. BEGIN points to the opening {,
323 NEXT points past the terminator of the first element, and END
324 points past the final }. We will accumulate result names from
325 recursive runs for each brace alternative in the buffer using
328 if (!(flags & GLOB_APPEND))
330 /* This call is to set a new vector, so clear out the
331 vector so we can append to it. */
333 pglob->gl_pathv = NULL;
335 firstc = pglob->gl_pathc;
337 /* In this loop P points to the beginning of the current element
338 and NEXT points past its terminator. */
342 /* Construct a whole name that is one of the brace
343 alternatives in a temporary buffer. */
345 size_t bufsz = (begin - pattern) + (next - 1 - p) + restlen;
349 char *onealt = malloc (bufsz);
352 if (!(flags & GLOB_APPEND))
357 memcpy (onealt, pattern, begin - pattern);
358 memcpy (&onealt[begin - pattern], p, next - 1 - p);
359 memcpy (&onealt[(begin - pattern) + (next - 1 - p)],
361 result = glob (onealt,
362 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
363 GLOB_APPEND), errfunc, pglob);
368 /* If we got an error, return it. */
369 if (result && result != GLOB_NOMATCH)
371 if (!(flags & GLOB_APPEND))
376 /* Advance past this alternative and process the next. */
385 /* Found the next alternative. Loop to glob it. */
395 /* End of the brace expression. Break out of the loop. */
401 if (pglob->gl_pathc == firstc &&
402 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
407 /* Find the filename. */
408 filename = strrchr (pattern, '/');
409 if (filename == NULL)
413 dirname = (char *) "";
415 dirname = (char *) ".";
419 else if (filename == pattern)
422 dirname = (char *) "/";
428 dirlen = filename - pattern;
429 dirname = (char *) __alloca (dirlen + 1);
430 memcpy (dirname, pattern, dirlen);
431 dirname[dirlen] = '\0';
435 if (filename[0] == '\0' && dirlen > 1)
436 /* "pattern/". Expand "pattern", appending slashes. */
438 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
440 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
444 if (!(flags & GLOB_APPEND))
447 pglob->gl_pathv = NULL;
450 oldcount = pglob->gl_pathc;
453 if ((flags & GLOB_TILDE) && dirname[0] == '~')
455 if (dirname[1] == '\0')
457 /* Look up home directory. */
458 dirname = getenv ("HOME");
460 if (dirname == NULL || dirname[0] == '\0')
464 if (dirname == NULL || dirname[0] == '\0')
465 dirname = "c:/users/default"; /* poor default */
467 if (dirname == NULL || dirname[0] == '\0')
469 extern char *getlogin __P ((void));
470 char *name = getlogin ();
473 struct passwd *p = getpwnam (name);
478 if (dirname == NULL || dirname[0] == '\0')
479 dirname = (char *) "~"; /* No luck. */
486 if (dirname == NULL || dirname[0] == '\0')
490 if (dirname == NULL || dirname[0] == '\0')
491 dirname = "c:/users/default"; /* poor default */
493 /* Look up specific user's home directory. */
494 struct passwd *p = getpwnam (dirname + 1);
501 #endif /* Not VMS. */
503 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
505 /* The directory name contains metacharacters, so we
506 have to glob for the directory, and then glob for
507 the pattern in each directory found. */
511 status = glob (dirname,
512 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
518 /* We have successfully globbed the preceding directory name.
519 For each name we found, call glob_in_dir on it and FILENAME,
520 appending the results to PGLOB. */
521 for (i = 0; i < dirs.gl_pathc; ++i)
527 /* Make globbing interruptible in the bash shell. */
528 extern int interrupt_state;
539 oldcount = pglob->gl_pathc;
540 status = glob_in_dir (filename, dirs.gl_pathv[i],
541 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
543 if (status == GLOB_NOMATCH)
544 /* No matches in this directory. Try the next. */
554 /* Stick the directory on the front of each name. */
555 if (prefix_array (dirs.gl_pathv[i],
556 &pglob->gl_pathv[oldcount],
557 pglob->gl_pathc - oldcount))
565 flags |= GLOB_MAGCHAR;
567 if (pglob->gl_pathc == oldcount)
569 if (flags & GLOB_NOCHECK)
571 size_t len = strlen (pattern) + 1;
572 char *patcopy = (char *) malloc (len);
575 memcpy (patcopy, pattern, len);
578 = (char **) realloc (pglob->gl_pathv,
580 ((flags & GLOB_DOOFFS) ?
581 pglob->gl_offs : 0) +
584 if (pglob->gl_pathv == NULL)
590 if (flags & GLOB_DOOFFS)
591 while (pglob->gl_pathc < pglob->gl_offs)
592 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
594 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
595 pglob->gl_pathv[pglob->gl_pathc] = NULL;
596 pglob->gl_flags = flags;
603 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
609 /* Stick the directory on the front of each name. */
610 if (prefix_array (dirname,
611 &pglob->gl_pathv[oldcount],
612 pglob->gl_pathc - oldcount))
620 if (flags & GLOB_MARK)
622 /* Append slashes to directory names. */
625 for (i = oldcount; i < pglob->gl_pathc; ++i)
626 if (((flags & GLOB_ALTDIRFUNC) ?
627 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
628 __stat (pglob->gl_pathv[i], &st)) == 0 &&
629 S_ISDIR (st.st_mode))
631 size_t len = strlen (pglob->gl_pathv[i]) + 2;
632 char *new = realloc (pglob->gl_pathv[i], len);
638 strcpy (&new[len - 2], "/");
639 pglob->gl_pathv[i] = new;
643 if (!(flags & GLOB_NOSORT))
644 /* Sort the vector. */
645 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
646 pglob->gl_pathc - oldcount,
647 sizeof (char *), collated_compare);
653 /* Free storage allocated in PGLOB by a previous `glob' call. */
656 register glob_t *pglob;
658 if (pglob->gl_pathv != NULL)
661 for (i = 0; i < pglob->gl_pathc; ++i)
662 if (pglob->gl_pathv[i] != NULL)
663 free ((__ptr_t) pglob->gl_pathv[i]);
664 free ((__ptr_t) pglob->gl_pathv);
669 /* Do a collated comparison of A and B. */
671 collated_compare (a, b)
675 const char *const s1 = *(const char *const * const) a;
676 const char *const s2 = *(const char *const * const) b;
684 return strcoll (s1, s2);
688 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
689 elements in place. Return nonzero if out of memory, zero if successful.
690 A slash is inserted between DIRNAME and each elt of ARRAY,
691 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
693 prefix_array (dirname, array, n)
699 size_t dirlen = strlen (dirname);
701 if (dirlen == 1 && dirname[0] == '/')
702 /* DIRNAME is just "/", so normal prepending would get us "//foo".
703 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
706 for (i = 0; i < n; ++i)
708 size_t eltlen = strlen (array[i]) + 1;
709 char *new = (char *) malloc (dirlen + 1 + eltlen);
713 free ((__ptr_t) array[--i]);
717 memcpy (new, dirname, dirlen);
719 memcpy (&new[dirlen + 1], array[i], eltlen);
720 free ((__ptr_t) array[i]);
728 /* Return nonzero if PATTERN contains any metacharacters.
729 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
731 glob_pattern_p (pattern, quote)
735 register const char *p;
738 for (p = pattern; *p != '\0'; ++p)
746 if (quote && p[1] != '\0')
764 /* Like `glob', but PATTERN is a final pathname component,
765 and matches are searched for in DIRECTORY.
766 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
767 The GLOB_APPEND flag is assumed to be set (always appends). */
769 glob_in_dir (pattern, directory, flags, errfunc, pglob)
771 const char *directory;
773 int (*errfunc) __P ((const char *, int));
780 struct globlink *next;
783 struct globlink *names = NULL;
786 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
789 flags |= GLOB_NOCHECK;
793 flags |= GLOB_MAGCHAR;
795 stream = ((flags & GLOB_ALTDIRFUNC) ?
796 (*pglob->gl_opendir) (directory) :
797 (__ptr_t) opendir (directory));
800 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
809 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
810 (*pglob->gl_readdir) (stream) :
811 readdir ((DIR *) stream));
814 if (! REAL_DIR_ENTRY (d))
819 if (fnmatch (pattern, name,
820 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
821 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
828 = (struct globlink *) __alloca (sizeof (struct globlink));
831 = (char *) malloc (len + 1);
832 if (new->name == NULL)
834 memcpy ((__ptr_t) new->name, name, len);
835 new->name[len] = '\0';
843 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
844 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
845 flags |= GLOB_NOCHECK;
847 if (nfound == 0 && (flags & GLOB_NOCHECK))
849 size_t len = strlen (pattern);
851 names = (struct globlink *) __alloca (sizeof (struct globlink));
853 names->name = (char *) malloc (len + 1);
854 if (names->name == NULL)
856 memcpy (names->name, pattern, len);
857 names->name[len] = '\0';
861 = (char **) realloc (pglob->gl_pathv,
863 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
866 if (pglob->gl_pathv == NULL)
869 if (flags & GLOB_DOOFFS)
870 while (pglob->gl_pathc < pglob->gl_offs)
871 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
873 for (; names != NULL; names = names->next)
874 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
875 pglob->gl_pathv[pglob->gl_pathc] = NULL;
877 pglob->gl_flags = flags;
882 if (flags & GLOB_ALTDIRFUNC)
883 (*pglob->gl_closedir) (stream);
885 closedir ((DIR *) stream);
888 return nfound == 0 ? GLOB_NOMATCH : 0;
893 if (flags & GLOB_ALTDIRFUNC)
894 (*pglob->gl_closedir) (stream);
896 closedir ((DIR *) stream);
899 while (names != NULL)
901 if (names->name != NULL)
902 free ((__ptr_t) names->name);
908 #endif /* Not ELIDE_CODE. */