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, '{');
271 const char *end = strchr (begin + 1, '}');
272 if (end != NULL && end != begin + 1)
274 size_t restlen = strlen (end + 1) + 1;
275 const char *p, *comma;
279 if (!(flags & GLOB_APPEND))
282 pglob->gl_pathv = NULL;
284 firstc = pglob->gl_pathc;
285 for (p = begin + 1;; p = comma + 1)
288 comma = strchr (p, ',');
290 comma = strchr (p, '\0');
291 if ((begin - pattern) + (comma - p) + 1 > bufsz)
293 if (bufsz * 2 < comma - p + 1)
296 bufsz = comma - p + 1;
297 buf = __alloca (bufsz);
299 memcpy (buf, pattern, begin - pattern);
300 memcpy (buf + (begin - pattern), p, comma - p);
301 memcpy (buf + (begin - pattern) + (comma - p), end, restlen);
302 result = glob (buf, ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
303 GLOB_APPEND), errfunc, pglob);
304 if (result && result != GLOB_NOMATCH)
309 if (pglob->gl_pathc == firstc &&
310 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
316 /* Find the filename. */
317 filename = strrchr (pattern, '/');
318 if (filename == NULL)
321 dirname = (char *) ".";
324 else if (filename == pattern)
327 dirname = (char *) "/";
333 dirlen = filename - pattern;
334 dirname = (char *) __alloca (dirlen + 1);
335 memcpy (dirname, pattern, dirlen);
336 dirname[dirlen] = '\0';
340 if (filename[0] == '\0' && dirlen > 1)
341 /* "pattern/". Expand "pattern", appending slashes. */
343 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
345 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
349 if (!(flags & GLOB_APPEND))
352 pglob->gl_pathv = NULL;
355 oldcount = pglob->gl_pathc;
357 if ((flags & GLOB_TILDE) && dirname[0] == '~')
359 if (dirname[1] == '\0')
361 /* Look up home directory. */
362 dirname = getenv ("HOME");
363 if (dirname == NULL || dirname[0] == '\0')
365 extern char *getlogin __P ((void));
366 char *name = getlogin ();
369 struct passwd *p = getpwnam (name);
374 if (dirname == NULL || dirname[0] == '\0')
375 dirname = (char *) "~"; /* No luck. */
379 /* Look up specific user's home directory. */
380 struct passwd *p = getpwnam (dirname + 1);
386 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
388 /* The directory name contains metacharacters, so we
389 have to glob for the directory, and then glob for
390 the pattern in each directory found. */
394 status = glob (dirname,
395 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
401 /* We have successfully globbed the preceding directory name.
402 For each name we found, call glob_in_dir on it and FILENAME,
403 appending the results to PGLOB. */
404 for (i = 0; i < dirs.gl_pathc; ++i)
410 /* Make globbing interruptible in the bash shell. */
411 extern int interrupt_state;
422 oldcount = pglob->gl_pathc;
423 status = glob_in_dir (filename, dirs.gl_pathv[i],
424 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
426 if (status == GLOB_NOMATCH)
427 /* No matches in this directory. Try the next. */
437 /* Stick the directory on the front of each name. */
438 if (prefix_array (dirs.gl_pathv[i],
439 &pglob->gl_pathv[oldcount],
440 pglob->gl_pathc - oldcount))
448 flags |= GLOB_MAGCHAR;
450 if (pglob->gl_pathc == oldcount)
452 if (flags & GLOB_NOCHECK)
454 size_t len = strlen (pattern) + 1;
455 char *patcopy = (char *) malloc (len);
458 memcpy (patcopy, pattern, len);
461 = (char **) realloc (pglob->gl_pathv,
463 ((flags & GLOB_DOOFFS) ?
464 pglob->gl_offs : 0) +
467 if (pglob->gl_pathv == NULL)
473 if (flags & GLOB_DOOFFS)
474 while (pglob->gl_pathc < pglob->gl_offs)
475 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
477 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
478 pglob->gl_pathv[pglob->gl_pathc] = NULL;
479 pglob->gl_flags = flags;
486 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
492 /* Stick the directory on the front of each name. */
493 if (prefix_array (dirname,
494 &pglob->gl_pathv[oldcount],
495 pglob->gl_pathc - oldcount))
503 if (flags & GLOB_MARK)
505 /* Append slashes to directory names. */
508 for (i = oldcount; i < pglob->gl_pathc; ++i)
509 if (((flags & GLOB_ALTDIRFUNC) ?
510 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
511 __stat (pglob->gl_pathv[i], &st)) == 0 &&
512 S_ISDIR (st.st_mode))
514 size_t len = strlen (pglob->gl_pathv[i]) + 2;
515 char *new = realloc (pglob->gl_pathv[i], len);
521 strcpy (&new[len - 2], "/");
522 pglob->gl_pathv[i] = new;
526 if (!(flags & GLOB_NOSORT))
527 /* Sort the vector. */
528 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
529 pglob->gl_pathc - oldcount,
530 sizeof (char *), collated_compare);
536 /* Free storage allocated in PGLOB by a previous `glob' call. */
539 register glob_t *pglob;
541 if (pglob->gl_pathv != NULL)
544 for (i = 0; i < pglob->gl_pathc; ++i)
545 if (pglob->gl_pathv[i] != NULL)
546 free ((__ptr_t) pglob->gl_pathv[i]);
547 free ((__ptr_t) pglob->gl_pathv);
552 /* Do a collated comparison of A and B. */
554 collated_compare (a, b)
558 const char *const s1 = *(const char *const * const) a;
559 const char *const s2 = *(const char *const * const) b;
567 return strcoll (s1, s2);
571 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
572 elements in place. Return nonzero if out of memory, zero if successful.
573 A slash is inserted between DIRNAME and each elt of ARRAY,
574 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
576 prefix_array (dirname, array, n)
582 size_t dirlen = strlen (dirname);
584 if (dirlen == 1 && dirname[0] == '/')
585 /* DIRNAME is just "/", so normal prepending would get us "//foo".
586 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
589 for (i = 0; i < n; ++i)
591 size_t eltlen = strlen (array[i]) + 1;
592 char *new = (char *) malloc (dirlen + 1 + eltlen);
596 free ((__ptr_t) array[--i]);
600 memcpy (new, dirname, dirlen);
602 memcpy (&new[dirlen + 1], array[i], eltlen);
603 free ((__ptr_t) array[i]);
611 /* Return nonzero if PATTERN contains any metacharacters.
612 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
614 glob_pattern_p (pattern, quote)
618 register const char *p;
621 for (p = pattern; *p != '\0'; ++p)
647 /* Like `glob', but PATTERN is a final pathname component,
648 and matches are searched for in DIRECTORY.
649 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
650 The GLOB_APPEND flag is assumed to be set (always appends). */
652 glob_in_dir (pattern, directory, flags, errfunc, pglob)
654 const char *directory;
656 int (*errfunc) __P ((const char *, int));
663 struct globlink *next;
666 struct globlink *names = NULL;
669 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
672 flags |= GLOB_NOCHECK;
676 flags |= GLOB_MAGCHAR;
678 stream = ((flags & GLOB_ALTDIRFUNC) ?
679 (*pglob->gl_opendir) (directory) :
680 (__ptr_t) opendir (directory));
683 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
692 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
693 (*pglob->gl_readdir) (stream) :
694 readdir ((DIR *) stream));
697 if (! REAL_DIR_ENTRY (d))
702 if (fnmatch (pattern, name,
703 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
704 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
707 = (struct globlink *) __alloca (sizeof (struct globlink));
710 = (char *) malloc (len + 1);
711 if (new->name == NULL)
713 memcpy ((__ptr_t) new->name, name, len);
714 new->name[len] = '\0';
722 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
723 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
724 flags |= GLOB_NOCHECK;
726 if (nfound == 0 && (flags & GLOB_NOCHECK))
728 size_t len = strlen (pattern);
730 names = (struct globlink *) __alloca (sizeof (struct globlink));
732 names->name = (char *) malloc (len + 1);
733 if (names->name == NULL)
735 memcpy (names->name, pattern, len);
736 names->name[len] = '\0';
740 = (char **) realloc (pglob->gl_pathv,
742 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
745 if (pglob->gl_pathv == NULL)
748 if (flags & GLOB_DOOFFS)
749 while (pglob->gl_pathc < pglob->gl_offs)
750 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
752 for (; names != NULL; names = names->next)
753 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
754 pglob->gl_pathv[pglob->gl_pathc] = NULL;
756 pglob->gl_flags = flags;
761 if (flags & GLOB_ALTDIRFUNC)
762 (*pglob->gl_closedir) (stream);
764 closedir ((DIR *) stream);
767 return nfound == 0 ? GLOB_NOMATCH : 0;
772 if (flags & GLOB_ALTDIRFUNC)
773 (*pglob->gl_closedir) (stream);
775 closedir ((DIR *) stream);
778 while (names != NULL)
780 if (names->name != NULL)
781 free ((__ptr_t) names->name);
787 #endif /* Not ELIDE_CODE. */