1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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__)
28 #include <sys/types.h>
32 /* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */
40 #define GLOB_INTERFACE_VERSION 1
41 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
42 #include <gnu-versions.h>
43 #if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
65 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
74 #if defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
76 #ifndef __GNU_LIBRARY__
77 #define D_NAMLEN(d) strlen((d)->d_name)
78 #else /* GNU C library. */
79 #define D_NAMLEN(d) ((d)->d_namlen)
80 #endif /* Not GNU C library. */
81 #else /* Not POSIX or HAVE_DIRENT_H. */
83 #define D_NAMLEN(d) ((d)->d_namlen)
84 #ifdef HAVE_SYS_NDIR_H
86 #endif /* HAVE_SYS_NDIR_H */
89 #endif /* HAVE_SYS_DIR_H */
92 #endif /* HAVE_NDIR_H */
93 #endif /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__. */
95 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
96 /* Posix does not require that the d_ino field be present, and some
97 systems do not provide it. */
98 #define REAL_DIR_ENTRY(dp) 1
100 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
103 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
107 #else /* No standard headers. */
109 extern char *getenv ();
121 extern char *malloc (), *realloc ();
124 extern void qsort ();
125 extern void abort (), exit ();
127 #endif /* Standard headers. */
132 extern void bzero ();
135 extern void bcopy ();
138 #define memcpy(d, s, n) bcopy ((s), (d), (n))
139 #define strrchr rindex
140 /* memset is only used for zero here, but let's be paranoid. */
141 #define memset(s, better_be_zero, n) \
142 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
143 #endif /* Not ANSI_STRING. */
146 #define strcoll strcmp
150 #ifndef __GNU_LIBRARY__
159 /* These casts are the for sake of the broken Ultrix compiler,
160 which warns of illegal pointer combinations otherwise. */
162 return (char *) malloc (n);
163 return (char *) realloc (p, n);
165 #define realloc my_realloc
169 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
173 #define alloca(n) __builtin_alloca (n)
175 #if defined (sparc) || defined (HAVE_ALLOCA_H)
177 #else /* Not sparc or HAVE_ALLOCA_H. */
179 extern char *alloca ();
180 #endif /* Not _AIX. */
181 #endif /* sparc or HAVE_ALLOCA_H. */
184 #define __alloca alloca
188 #ifndef __GNU_LIBRARY__
190 #ifdef STAT_MACROS_BROKEN
194 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
200 #define size_t unsigned int
203 /* Some system header files erroneously define these.
204 We want our own definitions from <fnmatch.h> to take precedence. */
210 /* Some system header files erroneously define these.
211 We want our own definitions from <glob.h> to take precedence. */
222 static int glob_pattern_p __P ((const char *pattern, int quote));
223 static int glob_in_dir __P ((const char *pattern, const char *directory,
225 int (*errfunc) __P ((const char *, int)),
227 static int prefix_array __P ((const char *prefix, char **array, size_t n));
228 static int collated_compare __P ((const __ptr_t, const __ptr_t));
230 /* Do glob searching for PATTERN, placing results in PGLOB.
231 The bits defined above may be set in FLAGS.
232 If a directory cannot be opened or read and ERRFUNC is not nil,
233 it is called with the pathname that caused the error, and the
234 `errno' value from the failing call; if it returns non-zero
235 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
236 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
237 Otherwise, `glob' returns zero. */
239 glob (pattern, flags, errfunc, pglob)
242 int (*errfunc) __P ((const char *, int));
245 const char *filename;
251 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
257 if (flags & GLOB_BRACE)
259 const char *begin = strchr (pattern, '{');
262 const char *end = strchr (begin + 1, '}');
263 if (end != NULL && end != begin + 1)
265 size_t restlen = strlen (end + 1) + 1;
266 const char *p, *comma;
270 if (!(flags & GLOB_APPEND))
273 pglob->gl_pathv = NULL;
275 firstc = pglob->gl_pathc;
276 for (p = begin + 1;; p = comma + 1)
279 comma = strchr (p, ',');
281 comma = strchr (p, '\0');
282 if ((begin - pattern) + (comma - p) + 1 > bufsz)
284 if (bufsz * 2 < comma - p + 1)
287 bufsz = comma - p + 1;
288 buf = __alloca (bufsz);
290 memcpy (buf, pattern, begin - pattern);
291 memcpy (buf + (begin - pattern), p, comma - p);
292 memcpy (buf + (begin - pattern) + (comma - p), end, restlen);
293 result = glob (buf, (flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC) |
294 GLOB_APPEND), errfunc, pglob);
295 if (result && result != GLOB_NOMATCH)
300 if (pglob->gl_pathc == firstc &&
301 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
307 /* Find the filename. */
308 filename = strrchr (pattern, '/');
309 if (filename == NULL)
312 dirname = (char *) ".";
315 else if (filename == pattern)
318 dirname = (char *) "/";
324 dirlen = filename - pattern;
325 dirname = (char *) __alloca (dirlen + 1);
326 memcpy (dirname, pattern, dirlen);
327 dirname[dirlen] = '\0';
331 if (filename[0] == '\0' && dirlen > 1)
332 /* "pattern/". Expand "pattern", appending slashes. */
334 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
336 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
340 if (!(flags & GLOB_APPEND))
343 pglob->gl_pathv = NULL;
346 oldcount = pglob->gl_pathc;
348 if ((flags & GLOB_TILDE) && dirname[0] == '~')
350 if (dirname[1] == '\0')
352 /* Look up home directory. */
353 dirname = getenv ("HOME");
354 if (dirname == NULL || dirname[0] == '\0')
356 extern char *getlogin ();
357 char *name = getlogin ();
360 struct passwd *p = getpwnam (name);
365 if (dirname == NULL || dirname[0] == '\0')
366 dirname = (char *) "~"; /* No luck. */
370 /* Look up specific user's home directory. */
371 struct passwd *p = getpwnam (dirname + 1);
377 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
379 /* The directory name contains metacharacters, so we
380 have to glob for the directory, and then glob for
381 the pattern in each directory found. */
385 status = glob (dirname,
386 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
392 /* We have successfully globbed the preceding directory name.
393 For each name we found, call glob_in_dir on it and FILENAME,
394 appending the results to PGLOB. */
395 for (i = 0; i < dirs.gl_pathc; ++i)
401 /* Make globbing interruptible in the bash shell. */
402 extern int interrupt_state;
413 oldcount = pglob->gl_pathc;
414 status = glob_in_dir (filename, dirs.gl_pathv[i],
415 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
417 if (status == GLOB_NOMATCH)
418 /* No matches in this directory. Try the next. */
428 /* Stick the directory on the front of each name. */
429 if (prefix_array (dirs.gl_pathv[i],
430 &pglob->gl_pathv[oldcount],
431 pglob->gl_pathc - oldcount))
439 flags |= GLOB_MAGCHAR;
441 if (pglob->gl_pathc == oldcount)
443 if (flags & GLOB_NOCHECK)
445 size_t len = strlen (pattern) + 1;
446 char *patcopy = (char *) malloc (len);
449 memcpy (patcopy, pattern, len);
452 = (char **) realloc (pglob->gl_pathv,
454 ((flags & GLOB_DOOFFS) ?
455 pglob->gl_offs : 0) +
458 if (pglob->gl_pathv == NULL)
464 if (flags & GLOB_DOOFFS)
465 while (pglob->gl_pathc < pglob->gl_offs)
466 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
468 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
469 pglob->gl_pathv[pglob->gl_pathc] = NULL;
470 pglob->gl_flags = flags;
477 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
483 /* Stick the directory on the front of each name. */
484 if (prefix_array (dirname,
485 &pglob->gl_pathv[oldcount],
486 pglob->gl_pathc - oldcount))
494 if (flags & GLOB_MARK)
496 /* Append slashes to directory names. */
499 for (i = oldcount; i < pglob->gl_pathc; ++i)
500 if (((flags & GLOB_ALTDIRFUNC) ?
501 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
502 __stat (pglob->gl_pathv[i], &st)) == 0 &&
503 S_ISDIR (st.st_mode))
505 size_t len = strlen (pglob->gl_pathv[i]) + 2;
506 char *new = realloc (pglob->gl_pathv[i], len);
512 strcpy (&new[len - 2], "/");
513 pglob->gl_pathv[i] = new;
517 if (!(flags & GLOB_NOSORT))
518 /* Sort the vector. */
519 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
520 pglob->gl_pathc - oldcount,
521 sizeof (char *), collated_compare);
527 /* Free storage allocated in PGLOB by a previous `glob' call. */
530 register glob_t *pglob;
532 if (pglob->gl_pathv != NULL)
535 for (i = 0; i < pglob->gl_pathc; ++i)
536 if (pglob->gl_pathv[i] != NULL)
537 free ((__ptr_t) pglob->gl_pathv[i]);
538 free ((__ptr_t) pglob->gl_pathv);
543 /* Do a collated comparison of A and B. */
545 collated_compare (a, b)
549 const char *const s1 = *(const char *const * const) a;
550 const char *const s2 = *(const char *const * const) b;
558 return strcoll (s1, s2);
562 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
563 elements in place. Return nonzero if out of memory, zero if successful.
564 A slash is inserted between DIRNAME and each elt of ARRAY,
565 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
567 prefix_array (dirname, array, n)
573 size_t dirlen = strlen (dirname);
575 if (dirlen == 1 && dirname[0] == '/')
576 /* DIRNAME is just "/", so normal prepending would get us "//foo".
577 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
580 for (i = 0; i < n; ++i)
582 size_t eltlen = strlen (array[i]) + 1;
583 char *new = (char *) malloc (dirlen + 1 + eltlen);
587 free ((__ptr_t) array[--i]);
591 memcpy (new, dirname, dirlen);
593 memcpy (&new[dirlen + 1], array[i], eltlen);
594 free ((__ptr_t) array[i]);
602 /* Return nonzero if PATTERN contains any metacharacters.
603 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
605 glob_pattern_p (pattern, quote)
609 register const char *p;
612 for (p = pattern; *p != '\0'; ++p)
638 /* Like `glob', but PATTERN is a final pathname component,
639 and matches are searched for in DIRECTORY.
640 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
641 The GLOB_APPEND flag is assumed to be set (always appends). */
643 glob_in_dir (pattern, directory, flags, errfunc, pglob)
645 const char *directory;
647 int (*errfunc) __P ((const char *, int));
654 struct globlink *next;
657 struct globlink *names = NULL;
660 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
663 flags |= GLOB_NOCHECK;
667 flags |= GLOB_MAGCHAR;
669 stream = ((flags & GLOB_ALTDIRFUNC) ?
670 (*pglob->gl_opendir) (directory) :
671 opendir (directory));
674 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
683 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
684 (*pglob->gl_readdir) (stream) :
688 if (! REAL_DIR_ENTRY (d))
697 if (fnmatch (pattern, name,
698 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
699 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
702 = (struct globlink *) __alloca (sizeof (struct globlink));
706 = (char *) malloc (len + 1);
707 if (new->name == NULL)
709 memcpy ((__ptr_t) new->name, name, len);
710 new->name[len] = '\0';
718 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
719 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
720 flags |= GLOB_NOCHECK;
722 if (nfound == 0 && (flags & GLOB_NOCHECK))
724 size_t len = strlen (pattern);
726 names = (struct globlink *) __alloca (sizeof (struct globlink));
728 names->name = (char *) malloc (len + 1);
729 if (names->name == NULL)
731 memcpy (names->name, pattern, len);
732 names->name[len] = '\0';
736 = (char **) realloc (pglob->gl_pathv,
738 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
741 if (pglob->gl_pathv == NULL)
744 if (flags & GLOB_DOOFFS)
745 while (pglob->gl_pathc < pglob->gl_offs)
746 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
748 for (; names != NULL; names = names->next)
749 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
750 pglob->gl_pathv[pglob->gl_pathc] = NULL;
752 pglob->gl_flags = flags;
757 if (flags & GLOB_ALTDIRFUNC)
758 (*pglob->gl_closedir) (stream);
763 return nfound == 0 ? GLOB_NOMATCH : 0;
768 if (flags & GLOB_ALTDIRFUNC)
769 (*pglob->gl_closedir) (stream);
774 while (names != NULL)
776 if (names->name != NULL)
777 free ((__ptr_t) names->name);
783 #endif /* Not ELIDE_CODE. */