1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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)
118 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
119 /* Posix does not require that the d_ino field be present, and some
120 systems do not provide it. */
121 # define REAL_DIR_ENTRY(dp) 1
123 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
126 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
130 #else /* No standard headers. */
132 extern char *getenv ();
134 # ifdef HAVE_STRING_H
138 # include <strings.h>
140 # ifdef HAVE_MEMORY_H
144 extern char *malloc (), *realloc ();
147 extern void qsort ();
148 extern void abort (), exit ();
150 #endif /* Standard headers. */
155 extern void bzero ();
158 extern void bcopy ();
161 # define memcpy(d, s, n) bcopy ((s), (d), (n))
162 # define strrchr rindex
163 /* memset is only used for zero here, but let's be paranoid. */
164 # define memset(s, better_be_zero, n) \
165 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
166 #endif /* Not ANSI_STRING. */
168 #if !defined HAVE_STRCOLL && !defined _LIBC
169 # define strcoll strcmp
173 #ifndef __GNU_LIBRARY__
187 /* These casts are the for sake of the broken Ultrix compiler,
188 which warns of illegal pointer combinations otherwise. */
190 return (char *) malloc (n);
191 return (char *) realloc (p, n);
193 # define realloc my_realloc
195 #endif /* __GNU_LIBRARY__ */
198 #if !defined __alloca && !defined __GNU_LIBRARY__
202 # define alloca(n) __builtin_alloca (n)
203 # else /* Not GCC. */
204 # ifdef HAVE_ALLOCA_H
206 # else /* Not HAVE_ALLOCA_H. */
211 extern char *alloca ();
212 # endif /* WINDOWS32 */
213 # endif /* Not _AIX. */
214 # endif /* sparc or HAVE_ALLOCA_H. */
217 # define __alloca alloca
221 #ifndef __GNU_LIBRARY__
223 # ifdef STAT_MACROS_BROKEN
227 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
231 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
233 # define size_t unsigned int
236 /* Some system header files erroneously define these.
237 We want our own definitions from <fnmatch.h> to take precedence. */
243 /* Some system header files erroneously define these.
244 We want our own definitions from <glob.h> to take precedence. */
256 #if __GNUC__ - 0 >= 2
259 const char *next_brace_sub __P ((const char *begin));
260 static int glob_in_dir __P ((const char *pattern, const char *directory,
262 int (*errfunc) __P ((const char *, int)),
264 static int prefix_array __P ((const char *prefix, char **array, size_t n));
265 static int collated_compare __P ((const __ptr_t, const __ptr_t));
268 /* Find the end of the sub-pattern in a brace expression. We define
269 this as an inline function if the compiler permits. */
271 #if __GNUC__ - 0 >= 2
275 next_brace_sub (begin)
278 unsigned int depth = 0;
279 const char *cp = begin;
285 if (*cp != ',' && *cp != '}' && *cp != '\0')
295 while (*cp != '\0' && (*cp != '}' || depth > 0))
302 /* An incorrectly terminated brace expression. */
313 /* Do glob searching for PATTERN, placing results in PGLOB.
314 The bits defined above may be set in FLAGS.
315 If a directory cannot be opened or read and ERRFUNC is not nil,
316 it is called with the pathname that caused the error, and the
317 `errno' value from the failing call; if it returns non-zero
318 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
319 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
320 Otherwise, `glob' returns zero. */
322 glob (pattern, flags, errfunc, pglob)
325 int (*errfunc) __P ((const char *, int));
328 const char *filename;
334 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
336 __set_errno (EINVAL);
340 if (flags & GLOB_BRACE)
342 const char *begin = strchr (pattern, '{');
345 /* Allocate working buffer large enough for our work. Note that
346 we have at least an opening and closing brace. */
354 char onealt[strlen (pattern) - 1];
356 char *onealt = (char *) malloc (strlen (pattern) - 1);
359 if (!(flags & GLOB_APPEND))
365 /* We know the prefix for all sub-patterns. */
366 memcpy (onealt, pattern, begin - pattern);
367 alt_start = &onealt[begin - pattern];
369 /* Find the first sub-pattern and at the same time find the
370 rest after the closing brace. */
371 next = next_brace_sub (begin + 1);
374 /* It is an illegal expression. */
378 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
381 /* Now find the end of the whole brace expression. */
385 rest = next_brace_sub (rest + 1);
388 /* It is an illegal expression. */
392 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
395 /* Please note that we now can be sure the brace expression
397 rest_len = strlen (++rest) + 1;
399 /* We have a brace expression. BEGIN points to the opening {,
400 NEXT points past the terminator of the first element, and END
401 points past the final }. We will accumulate result names from
402 recursive runs for each brace alternative in the buffer using
405 if (!(flags & GLOB_APPEND))
407 /* This call is to set a new vector, so clear out the
408 vector so we can append to it. */
410 pglob->gl_pathv = NULL;
412 firstc = pglob->gl_pathc;
419 /* Construct the new glob expression. */
420 memcpy (alt_start, p, next - p);
421 memcpy (&alt_start[next - p], rest, rest_len);
423 result = glob (onealt,
424 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
425 | GLOB_APPEND), errfunc, pglob);
427 /* If we got an error, return it. */
428 if (result && result != GLOB_NOMATCH)
433 if (!(flags & GLOB_APPEND))
439 /* We saw the last entry. */
443 next = next_brace_sub (p);
444 assert (next != NULL);
451 if (pglob->gl_pathc != firstc)
452 /* We found some entries. */
454 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
459 /* Find the filename. */
460 filename = strrchr (pattern, '/');
461 if (filename == NULL)
465 dirname = (char *) "";
467 dirname = (char *) ".";
471 else if (filename == pattern)
474 dirname = (char *) "/";
480 dirlen = filename - pattern;
481 dirname = (char *) __alloca (dirlen + 1);
482 memcpy (dirname, pattern, dirlen);
483 dirname[dirlen] = '\0';
487 if (filename[0] == '\0' && dirlen > 1)
488 /* "pattern/". Expand "pattern", appending slashes. */
490 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
492 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
496 if (!(flags & GLOB_APPEND))
499 pglob->gl_pathv = NULL;
502 oldcount = pglob->gl_pathc;
505 if ((flags & GLOB_TILDE) && dirname[0] == '~')
507 if (dirname[1] == '\0' || dirname[1] == '/')
509 /* Look up home directory. */
510 char *home_dir = getenv ("HOME");
512 if (home_dir == NULL || home_dir[0] == '\0')
516 if (home_dir == NULL || home_dir[0] == '\0')
517 home_dir = "c:/users/default"; /* poor default */
519 if (home_dir == NULL || home_dir[0] == '\0')
522 # if defined HAVE_GETLOGIN_R || defined _LIBC
523 extern int getlogin_r __P ((char *, size_t));
524 size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
528 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
531 name = (char *) __alloca (buflen);
533 success = getlogin_r (name, buflen) >= 0;
535 extern char *getlogin __P ((void));
538 success = (name = getlogin ()) != NULL;
542 # if defined HAVE_GETPWNAM_R || defined _LIBC
543 size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
545 struct passwd pwbuf, *p;
547 pwtmpbuf = (char *) __alloca (pwbuflen);
549 success = (__getpwnam_r (name, &pwbuf, pwtmpbuf,
552 struct passwd *p = getpwnam (name);
556 home_dir = p->pw_dir;
559 if (home_dir == NULL || home_dir[0] == '\0')
560 home_dir = (char *) "~"; /* No luck. */
561 # endif /* WINDOWS32 */
563 /* Now construct the full directory. */
564 if (dirname[1] == '\0')
569 size_t home_len = strlen (home_dir);
570 newp = (char *) __alloca (home_len + dirlen);
571 memcpy (newp, home_dir, home_len);
572 memcpy (&newp[home_len], &dirname[1], dirlen);
576 # if !defined _AMIGA && !defined WINDOWS32
579 char *end_name = strchr (dirname, '/');
583 if (end_name == NULL)
584 user_name = dirname + 1;
587 user_name = (char *) __alloca (end_name - dirname);
588 memcpy (user_name, dirname + 1, end_name - dirname);
589 user_name[end_name - dirname - 1] = '\0';
592 /* Look up specific user's home directory. */
594 # if defined HAVE_GETPWNAM_R || defined _LIBC
595 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
596 char *pwtmpbuf = (char *) __alloca (buflen);
597 struct passwd pwbuf, *p;
598 if (__getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) >= 0)
599 home_dir = p->pw_dir;
603 struct passwd *p = getpwnam (user_name);
605 home_dir = p->pw_dir;
610 /* If we found a home directory use this. */
611 if (home_dir != NULL)
614 size_t home_len = strlen (home_dir);
615 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
616 newp = (char *) __alloca (home_len + rest_len + 1);
617 memcpy (newp, home_dir, home_len);
618 memcpy (&newp[home_len], end_name, rest_len);
619 newp[home_len + rest_len] = '\0';
623 # endif /* Not Amiga && not WINDOWS32. */
625 #endif /* Not VMS. */
627 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
629 /* The directory name contains metacharacters, so we
630 have to glob for the directory, and then glob for
631 the pattern in each directory found. */
635 status = glob (dirname,
636 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
642 /* We have successfully globbed the preceding directory name.
643 For each name we found, call glob_in_dir on it and FILENAME,
644 appending the results to PGLOB. */
645 for (i = 0; i < dirs.gl_pathc; ++i)
651 /* Make globbing interruptible in the bash shell. */
652 extern int interrupt_state;
663 oldcount = pglob->gl_pathc;
664 status = glob_in_dir (filename, dirs.gl_pathv[i],
665 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
667 if (status == GLOB_NOMATCH)
668 /* No matches in this directory. Try the next. */
678 /* Stick the directory on the front of each name. */
679 if (prefix_array (dirs.gl_pathv[i],
680 &pglob->gl_pathv[oldcount],
681 pglob->gl_pathc - oldcount))
689 flags |= GLOB_MAGCHAR;
691 if (pglob->gl_pathc == oldcount)
693 if (flags & GLOB_NOCHECK)
695 size_t len = strlen (pattern) + 1;
696 char *patcopy = (char *) malloc (len);
699 memcpy (patcopy, pattern, len);
702 = (char **) realloc (pglob->gl_pathv,
704 ((flags & GLOB_DOOFFS) ?
705 pglob->gl_offs : 0) +
708 if (pglob->gl_pathv == NULL)
714 if (flags & GLOB_DOOFFS)
715 while (pglob->gl_pathc < pglob->gl_offs)
716 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
718 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
719 pglob->gl_pathv[pglob->gl_pathc] = NULL;
720 pglob->gl_flags = flags;
727 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
733 /* Stick the directory on the front of each name. */
734 if (prefix_array (dirname,
735 &pglob->gl_pathv[oldcount],
736 pglob->gl_pathc - oldcount))
744 if (flags & GLOB_MARK)
746 /* Append slashes to directory names. */
749 for (i = oldcount; i < pglob->gl_pathc; ++i)
750 if (((flags & GLOB_ALTDIRFUNC) ?
751 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
752 __stat (pglob->gl_pathv[i], &st)) == 0 &&
753 S_ISDIR (st.st_mode))
755 size_t len = strlen (pglob->gl_pathv[i]) + 2;
756 char *new = realloc (pglob->gl_pathv[i], len);
762 strcpy (&new[len - 2], "/");
763 pglob->gl_pathv[i] = new;
767 if (!(flags & GLOB_NOSORT))
768 /* Sort the vector. */
769 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
770 pglob->gl_pathc - oldcount,
771 sizeof (char *), collated_compare);
777 /* Free storage allocated in PGLOB by a previous `glob' call. */
780 register glob_t *pglob;
782 if (pglob->gl_pathv != NULL)
785 for (i = 0; i < pglob->gl_pathc; ++i)
786 if (pglob->gl_pathv[i] != NULL)
787 free ((__ptr_t) pglob->gl_pathv[i]);
788 free ((__ptr_t) pglob->gl_pathv);
793 /* Do a collated comparison of A and B. */
795 collated_compare (a, b)
799 const char *const s1 = *(const char *const * const) a;
800 const char *const s2 = *(const char *const * const) b;
808 return strcoll (s1, s2);
812 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
813 elements in place. Return nonzero if out of memory, zero if successful.
814 A slash is inserted between DIRNAME and each elt of ARRAY,
815 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
817 prefix_array (dirname, array, n)
823 size_t dirlen = strlen (dirname);
825 if (dirlen == 1 && dirname[0] == '/')
826 /* DIRNAME is just "/", so normal prepending would get us "//foo".
827 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
830 for (i = 0; i < n; ++i)
832 size_t eltlen = strlen (array[i]) + 1;
833 char *new = (char *) malloc (dirlen + 1 + eltlen);
837 free ((__ptr_t) array[--i]);
841 memcpy (new, dirname, dirlen);
843 memcpy (&new[dirlen + 1], array[i], eltlen);
844 free ((__ptr_t) array[i]);
852 /* Return nonzero if PATTERN contains any metacharacters.
853 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
855 __glob_pattern_p (pattern, quote)
859 register const char *p;
862 for (p = pattern; *p != '\0'; ++p)
870 if (quote && p[1] != '\0')
887 weak_alias (__glob_pattern_p, glob_pattern_p)
891 /* Like `glob', but PATTERN is a final pathname component,
892 and matches are searched for in DIRECTORY.
893 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
894 The GLOB_APPEND flag is assumed to be set (always appends). */
896 glob_in_dir (pattern, directory, flags, errfunc, pglob)
898 const char *directory;
900 int (*errfunc) __P ((const char *, int));
907 struct globlink *next;
910 struct globlink *names = NULL;
913 if (!__glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
915 /* We must check whether the file in this directory exists. */
916 stream = ((flags & GLOB_ALTDIRFUNC) ?
917 (*pglob->gl_opendir) (directory) :
918 (__ptr_t) opendir (directory));
921 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
928 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
929 (*pglob->gl_readdir) (stream) :
930 readdir ((DIR *) stream));
933 if (! REAL_DIR_ENTRY (d))
936 if (strcmp (pattern, d->d_name) == 0)
938 size_t len = NAMLEN (d);
940 (struct globlink *) __alloca (sizeof (struct globlink));
941 names->name = (char *) malloc (len + 1);
942 if (names->name == NULL)
944 memcpy ((__ptr_t) names->name, pattern, len);
945 names->name[len] = '\0';
954 flags |= GLOB_MAGCHAR;
956 stream = ((flags & GLOB_ALTDIRFUNC) ?
957 (*pglob->gl_opendir) (directory) :
958 (__ptr_t) opendir (directory));
961 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
970 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
971 (*pglob->gl_readdir) (stream) :
972 readdir ((DIR *) stream));
975 if (! REAL_DIR_ENTRY (d))
980 if (fnmatch (pattern, name,
981 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
982 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
989 = (struct globlink *) __alloca (sizeof (struct globlink));
992 = (char *) malloc (len + 1);
993 if (new->name == NULL)
995 memcpy ((__ptr_t) new->name, name, len);
996 new->name[len] = '\0';
1004 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
1005 ! __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
1006 flags |= GLOB_NOCHECK;
1008 if (nfound == 0 && (flags & GLOB_NOCHECK))
1010 size_t len = strlen (pattern);
1012 names = (struct globlink *) __alloca (sizeof (struct globlink));
1014 names->name = (char *) malloc (len + 1);
1015 if (names->name == NULL)
1017 memcpy (names->name, pattern, len);
1018 names->name[len] = '\0';
1024 = (char **) realloc (pglob->gl_pathv,
1026 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
1029 if (pglob->gl_pathv == NULL)
1032 if (flags & GLOB_DOOFFS)
1033 while (pglob->gl_pathc < pglob->gl_offs)
1034 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
1036 for (; names != NULL; names = names->next)
1037 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
1038 pglob->gl_pathv[pglob->gl_pathc] = NULL;
1040 pglob->gl_flags = flags;
1046 if (flags & GLOB_ALTDIRFUNC)
1047 (*pglob->gl_closedir) (stream);
1049 closedir ((DIR *) stream);
1052 return nfound == 0 ? GLOB_NOMATCH : 0;
1057 if (flags & GLOB_ALTDIRFUNC)
1058 (*pglob->gl_closedir) (stream);
1060 closedir ((DIR *) stream);
1063 while (names != NULL)
1065 if (names->name != NULL)
1066 free ((__ptr_t) names->name);
1067 names = names->next;
1069 return GLOB_NOSPACE;
1072 #endif /* Not ELIDE_CODE. */