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
70 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
79 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
81 # define NAMLEN(dirent) strlen((dirent)->d_name)
83 # define dirent direct
84 # define NAMLEN(dirent) (dirent)->d_namlen
85 # ifdef HAVE_SYS_NDIR_H
86 # include <sys/ndir.h>
88 # ifdef HAVE_SYS_DIR_H
97 /* In GNU systems, <dirent.h> defines this macro for us. */
100 #define NAMLEN(d) _D_NAMLEN(d)
104 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
105 /* Posix does not require that the d_ino field be present, and some
106 systems do not provide it. */
107 #define REAL_DIR_ENTRY(dp) 1
109 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
112 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
116 #else /* No standard headers. */
118 extern char *getenv ();
130 extern char *malloc (), *realloc ();
133 extern void qsort ();
134 extern void abort (), exit ();
136 #endif /* Standard headers. */
141 extern void bzero ();
144 extern void bcopy ();
147 #define memcpy(d, s, n) bcopy ((s), (d), (n))
148 #define strrchr rindex
149 /* memset is only used for zero here, but let's be paranoid. */
150 #define memset(s, better_be_zero, n) \
151 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
152 #endif /* Not ANSI_STRING. */
155 #define strcoll strcmp
159 #ifndef __GNU_LIBRARY__
168 /* These casts are the for sake of the broken Ultrix compiler,
169 which warns of illegal pointer combinations otherwise. */
171 return (char *) malloc (n);
172 return (char *) realloc (p, n);
174 #define realloc my_realloc
178 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
182 #define alloca(n) __builtin_alloca (n)
184 #if defined (sparc) || defined (HAVE_ALLOCA_H)
186 #else /* Not sparc or HAVE_ALLOCA_H. */
188 extern char *alloca ();
189 #endif /* Not _AIX. */
190 #endif /* sparc or HAVE_ALLOCA_H. */
193 #define __alloca alloca
197 #ifndef __GNU_LIBRARY__
199 #ifdef STAT_MACROS_BROKEN
203 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
209 #define size_t unsigned int
212 /* Some system header files erroneously define these.
213 We want our own definitions from <fnmatch.h> to take precedence. */
219 /* Some system header files erroneously define these.
220 We want our own definitions from <glob.h> to take precedence. */
231 static int glob_pattern_p __P ((const char *pattern, int quote));
232 static int glob_in_dir __P ((const char *pattern, const char *directory,
234 int (*errfunc) __P ((const char *, int)),
236 static int prefix_array __P ((const char *prefix, char **array, size_t n));
237 static int collated_compare __P ((const __ptr_t, const __ptr_t));
239 /* Do glob searching for PATTERN, placing results in PGLOB.
240 The bits defined above may be set in FLAGS.
241 If a directory cannot be opened or read and ERRFUNC is not nil,
242 it is called with the pathname that caused the error, and the
243 `errno' value from the failing call; if it returns non-zero
244 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
245 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
246 Otherwise, `glob' returns zero. */
248 glob (pattern, flags, errfunc, pglob)
251 int (*errfunc) __P ((const char *, int));
254 const char *filename;
260 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
266 if (flags & GLOB_BRACE)
268 const char *begin = strchr (pattern, '{');
273 const char *p, *end, *next;
274 unsigned int depth = 0;
276 /* Find the end of the brace expression, by counting braces.
277 While we're at it, notice the first comma at top brace level. */
285 if (depth == 0 && next == NULL)
296 return glob (pattern, flags &~ GLOB_BRACE, errfunc, pglob);
300 restlen = strlen (end) + 1;
304 /* We have a brace expression. BEGIN points to the opening {,
305 NEXT points past the terminator of the first element, and END
306 points past the final }. We will accumulate result names from
307 recursive runs for each brace alternative in the buffer using
310 if (!(flags & GLOB_APPEND))
312 /* This call is to set a new vector, so clear out the
313 vector so we can append to it. */
315 pglob->gl_pathv = NULL;
317 firstc = pglob->gl_pathc;
319 /* In this loop P points to the beginning of the current element
320 and NEXT points past its terminator. */
324 /* Construct a whole name that is one of the brace
325 alternatives in a temporary buffer. */
327 size_t bufsz = (begin - pattern) + (next - 1 - p) + restlen;
331 char *onealt = malloc (bufsz);
334 if (!(flags & GLOB_APPEND))
339 memcpy (onealt, pattern, begin - pattern);
340 memcpy (&onealt[begin - pattern], p, next - 1 - p);
341 memcpy (&onealt[(begin - pattern) + (next - 1 - p)],
343 result = glob (onealt,
344 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
345 GLOB_APPEND), errfunc, pglob);
350 /* If we got an error, return it. */
351 if (result && result != GLOB_NOMATCH)
353 if (!(flags & GLOB_APPEND))
358 /* Advance past this alternative and process the next. */
367 /* Found the next alternative. Loop to glob it. */
377 /* End of the brace expression. Break out of the loop. */
383 if (pglob->gl_pathc == firstc &&
384 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
389 /* Find the filename. */
390 filename = strrchr (pattern, '/');
391 if (filename == NULL)
394 dirname = (char *) ".";
397 else if (filename == pattern)
400 dirname = (char *) "/";
406 dirlen = filename - pattern;
407 dirname = (char *) __alloca (dirlen + 1);
408 memcpy (dirname, pattern, dirlen);
409 dirname[dirlen] = '\0';
413 if (filename[0] == '\0' && dirlen > 1)
414 /* "pattern/". Expand "pattern", appending slashes. */
416 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
418 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
422 if (!(flags & GLOB_APPEND))
425 pglob->gl_pathv = NULL;
428 oldcount = pglob->gl_pathc;
430 if ((flags & GLOB_TILDE) && dirname[0] == '~')
432 if (dirname[1] == '\0')
434 /* Look up home directory. */
435 dirname = getenv ("HOME");
436 if (dirname == NULL || dirname[0] == '\0')
438 extern char *getlogin __P ((void));
439 char *name = getlogin ();
442 struct passwd *p = getpwnam (name);
447 if (dirname == NULL || dirname[0] == '\0')
448 dirname = (char *) "~"; /* No luck. */
452 /* Look up specific user's home directory. */
453 struct passwd *p = getpwnam (dirname + 1);
459 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
461 /* The directory name contains metacharacters, so we
462 have to glob for the directory, and then glob for
463 the pattern in each directory found. */
467 status = glob (dirname,
468 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
474 /* We have successfully globbed the preceding directory name.
475 For each name we found, call glob_in_dir on it and FILENAME,
476 appending the results to PGLOB. */
477 for (i = 0; i < dirs.gl_pathc; ++i)
483 /* Make globbing interruptible in the bash shell. */
484 extern int interrupt_state;
495 oldcount = pglob->gl_pathc;
496 status = glob_in_dir (filename, dirs.gl_pathv[i],
497 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
499 if (status == GLOB_NOMATCH)
500 /* No matches in this directory. Try the next. */
510 /* Stick the directory on the front of each name. */
511 if (prefix_array (dirs.gl_pathv[i],
512 &pglob->gl_pathv[oldcount],
513 pglob->gl_pathc - oldcount))
521 flags |= GLOB_MAGCHAR;
523 if (pglob->gl_pathc == oldcount)
525 if (flags & GLOB_NOCHECK)
527 size_t len = strlen (pattern) + 1;
528 char *patcopy = (char *) malloc (len);
531 memcpy (patcopy, pattern, len);
534 = (char **) realloc (pglob->gl_pathv,
536 ((flags & GLOB_DOOFFS) ?
537 pglob->gl_offs : 0) +
540 if (pglob->gl_pathv == NULL)
546 if (flags & GLOB_DOOFFS)
547 while (pglob->gl_pathc < pglob->gl_offs)
548 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
550 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
551 pglob->gl_pathv[pglob->gl_pathc] = NULL;
552 pglob->gl_flags = flags;
559 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
565 /* Stick the directory on the front of each name. */
566 if (prefix_array (dirname,
567 &pglob->gl_pathv[oldcount],
568 pglob->gl_pathc - oldcount))
576 if (flags & GLOB_MARK)
578 /* Append slashes to directory names. */
581 for (i = oldcount; i < pglob->gl_pathc; ++i)
582 if (((flags & GLOB_ALTDIRFUNC) ?
583 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
584 __stat (pglob->gl_pathv[i], &st)) == 0 &&
585 S_ISDIR (st.st_mode))
587 size_t len = strlen (pglob->gl_pathv[i]) + 2;
588 char *new = realloc (pglob->gl_pathv[i], len);
594 strcpy (&new[len - 2], "/");
595 pglob->gl_pathv[i] = new;
599 if (!(flags & GLOB_NOSORT))
600 /* Sort the vector. */
601 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
602 pglob->gl_pathc - oldcount,
603 sizeof (char *), collated_compare);
609 /* Free storage allocated in PGLOB by a previous `glob' call. */
612 register glob_t *pglob;
614 if (pglob->gl_pathv != NULL)
617 for (i = 0; i < pglob->gl_pathc; ++i)
618 if (pglob->gl_pathv[i] != NULL)
619 free ((__ptr_t) pglob->gl_pathv[i]);
620 free ((__ptr_t) pglob->gl_pathv);
625 /* Do a collated comparison of A and B. */
627 collated_compare (a, b)
631 const char *const s1 = *(const char *const * const) a;
632 const char *const s2 = *(const char *const * const) b;
640 return strcoll (s1, s2);
644 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
645 elements in place. Return nonzero if out of memory, zero if successful.
646 A slash is inserted between DIRNAME and each elt of ARRAY,
647 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
649 prefix_array (dirname, array, n)
655 size_t dirlen = strlen (dirname);
657 if (dirlen == 1 && dirname[0] == '/')
658 /* DIRNAME is just "/", so normal prepending would get us "//foo".
659 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
662 for (i = 0; i < n; ++i)
664 size_t eltlen = strlen (array[i]) + 1;
665 char *new = (char *) malloc (dirlen + 1 + eltlen);
669 free ((__ptr_t) array[--i]);
673 memcpy (new, dirname, dirlen);
675 memcpy (&new[dirlen + 1], array[i], eltlen);
676 free ((__ptr_t) array[i]);
684 /* Return nonzero if PATTERN contains any metacharacters.
685 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
687 glob_pattern_p (pattern, quote)
691 register const char *p;
694 for (p = pattern; *p != '\0'; ++p)
702 if (quote && p[1] != '\0')
720 /* Like `glob', but PATTERN is a final pathname component,
721 and matches are searched for in DIRECTORY.
722 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
723 The GLOB_APPEND flag is assumed to be set (always appends). */
725 glob_in_dir (pattern, directory, flags, errfunc, pglob)
727 const char *directory;
729 int (*errfunc) __P ((const char *, int));
736 struct globlink *next;
739 struct globlink *names = NULL;
742 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
745 flags |= GLOB_NOCHECK;
749 flags |= GLOB_MAGCHAR;
751 stream = ((flags & GLOB_ALTDIRFUNC) ?
752 (*pglob->gl_opendir) (directory) :
753 (__ptr_t) opendir (directory));
756 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
765 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
766 (*pglob->gl_readdir) (stream) :
767 readdir ((DIR *) stream));
770 if (! REAL_DIR_ENTRY (d))
775 if (fnmatch (pattern, name,
776 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
777 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
780 = (struct globlink *) __alloca (sizeof (struct globlink));
783 = (char *) malloc (len + 1);
784 if (new->name == NULL)
786 memcpy ((__ptr_t) new->name, name, len);
787 new->name[len] = '\0';
795 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
796 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
797 flags |= GLOB_NOCHECK;
799 if (nfound == 0 && (flags & GLOB_NOCHECK))
801 size_t len = strlen (pattern);
803 names = (struct globlink *) __alloca (sizeof (struct globlink));
805 names->name = (char *) malloc (len + 1);
806 if (names->name == NULL)
808 memcpy (names->name, pattern, len);
809 names->name[len] = '\0';
813 = (char **) realloc (pglob->gl_pathv,
815 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
818 if (pglob->gl_pathv == NULL)
821 if (flags & GLOB_DOOFFS)
822 while (pglob->gl_pathc < pglob->gl_offs)
823 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
825 for (; names != NULL; names = names->next)
826 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
827 pglob->gl_pathv[pglob->gl_pathc] = NULL;
829 pglob->gl_flags = flags;
834 if (flags & GLOB_ALTDIRFUNC)
835 (*pglob->gl_closedir) (stream);
837 closedir ((DIR *) stream);
840 return nfound == 0 ? GLOB_NOMATCH : 0;
845 if (flags & GLOB_ALTDIRFUNC)
846 (*pglob->gl_closedir) (stream);
848 closedir ((DIR *) stream);
851 while (names != NULL)
853 if (names->name != NULL)
854 free ((__ptr_t) names->name);
860 #endif /* Not ELIDE_CODE. */