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>
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself. This code is part of the GNU C
33 Library, but also included in many other GNU distributions. Compiling
34 and linking in this code is a waste when using the GNU C library
35 (especially if it is a shared library). Rather than having every GNU
36 program understand `configure --with-gnu-libc' and omit the object files,
37 it is simpler to just do this in the source for each such file. */
39 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
55 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
64 #if defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
66 #ifndef __GNU_LIBRARY__
67 #define D_NAMLEN(d) strlen((d)->d_name)
68 #else /* GNU C library. */
69 #define D_NAMLEN(d) ((d)->d_namlen)
70 #endif /* Not GNU C library. */
71 #else /* Not POSIX or HAVE_DIRENT_H. */
73 #define D_NAMLEN(d) ((d)->d_namlen)
74 #ifdef HAVE_SYS_NDIR_H
76 #endif /* HAVE_SYS_NDIR_H */
79 #endif /* HAVE_SYS_DIR_H */
82 #endif /* HAVE_NDIR_H */
83 #endif /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__. */
85 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
86 /* Posix does not require that the d_ino field be present, and some
87 systems do not provide it. */
88 #define REAL_DIR_ENTRY(dp) 1
90 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
93 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
97 #else /* No standard headers. */
109 extern char *malloc (), *realloc ();
112 extern void qsort ();
113 extern void abort (), exit ();
115 #endif /* Standard headers. */
120 extern void bzero ();
123 extern void bcopy ();
126 #define memcpy(d, s, n) bcopy ((s), (d), (n))
127 #define strrchr rindex
128 /* memset is only used for zero here, but let's be paranoid. */
129 #define memset(s, better_be_zero, n) \
130 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
131 #endif /* Not ANSI_STRING. */
134 #define strcoll strcmp
138 #ifndef __GNU_LIBRARY__
147 /* These casts are the for sake of the broken Ultrix compiler,
148 which warns of illegal pointer combinations otherwise. */
150 return (char *) malloc (n);
151 return (char *) realloc (p, n);
153 #define realloc my_realloc
157 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
161 #define alloca(n) __builtin_alloca (n)
163 #if defined (sparc) || defined (HAVE_ALLOCA_H)
165 #else /* Not sparc or HAVE_ALLOCA_H. */
167 extern char *alloca ();
168 #endif /* Not _AIX. */
169 #endif /* sparc or HAVE_ALLOCA_H. */
172 #define __alloca alloca
178 #define size_t unsigned int
181 /* Some system header files erroneously define these.
182 We want our own definitions from <fnmatch.h> to take precedence. */
188 /* Some system header files erroneously define these.
189 We want our own definitions from <glob.h> to take precedence. */
200 __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
201 const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
202 void (*__glob_closedir_hook) __P ((__ptr_t stream));
204 static int glob_pattern_p __P ((const char *pattern, int quote));
205 static int glob_in_dir __P ((const char *pattern, const char *directory,
207 int (*errfunc) __P ((const char *, int)),
209 static int prefix_array __P ((const char *prefix, char **array, size_t n));
210 static int collated_compare __P ((const __ptr_t, const __ptr_t));
212 /* Do glob searching for PATTERN, placing results in PGLOB.
213 The bits defined above may be set in FLAGS.
214 If a directory cannot be opened or read and ERRFUNC is not nil,
215 it is called with the pathname that caused the error, and the
216 `errno' value from the failing call; if it returns non-zero
217 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
218 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
219 Otherwise, `glob' returns zero. */
221 glob (pattern, flags, errfunc, pglob)
224 int (*errfunc) __P ((const char *, int));
227 const char *filename;
233 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
239 /* Find the filename. */
240 filename = strrchr (pattern, '/');
241 if (filename == NULL)
244 dirname = (char *) ".";
247 else if (filename == pattern)
250 dirname = (char *) "/";
256 dirlen = filename - pattern;
257 dirname = (char *) __alloca (dirlen + 1);
258 memcpy (dirname, pattern, dirlen);
259 dirname[dirlen] = '\0';
263 if (filename[0] == '\0' && dirlen > 1)
264 /* "pattern/". Expand "pattern", appending slashes. */
266 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
268 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
272 if (!(flags & GLOB_APPEND))
275 pglob->gl_pathv = NULL;
278 oldcount = pglob->gl_pathc;
280 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
282 /* The directory name contains metacharacters, so we
283 have to glob for the directory, and then glob for
284 the pattern in each directory found. */
288 status = glob (dirname,
289 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
295 /* We have successfully globbed the preceding directory name.
296 For each name we found, call glob_in_dir on it and FILENAME,
297 appending the results to PGLOB. */
298 for (i = 0; i < dirs.gl_pathc; ++i)
304 /* Make globbing interruptible in the bash shell. */
305 extern int interrupt_state;
316 oldcount = pglob->gl_pathc;
317 status = glob_in_dir (filename, dirs.gl_pathv[i],
318 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
320 if (status == GLOB_NOMATCH)
321 /* No matches in this directory. Try the next. */
331 /* Stick the directory on the front of each name. */
332 if (prefix_array (dirs.gl_pathv[i],
333 &pglob->gl_pathv[oldcount],
334 pglob->gl_pathc - oldcount))
342 flags |= GLOB_MAGCHAR;
344 if (pglob->gl_pathc == oldcount)
346 if (flags & GLOB_NOCHECK)
348 size_t len = strlen (pattern) + 1;
349 char *patcopy = (char *) malloc (len);
352 memcpy (patcopy, pattern, len);
355 = (char **) realloc (pglob->gl_pathv,
357 ((flags & GLOB_DOOFFS) ?
358 pglob->gl_offs : 0) +
361 if (pglob->gl_pathv == NULL)
367 if (flags & GLOB_DOOFFS)
368 while (pglob->gl_pathc < pglob->gl_offs)
369 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
371 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
372 pglob->gl_pathv[pglob->gl_pathc] = NULL;
373 pglob->gl_flags = flags;
380 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
386 /* Stick the directory on the front of each name. */
387 if (prefix_array (dirname,
388 &pglob->gl_pathv[oldcount],
389 pglob->gl_pathc - oldcount))
397 if (!(flags & GLOB_NOSORT))
398 /* Sort the vector. */
399 qsort ((__ptr_t) & pglob->gl_pathv[oldcount],
400 pglob->gl_pathc - oldcount,
401 sizeof (char *), collated_compare);
407 /* Free storage allocated in PGLOB by a previous `glob' call. */
410 register glob_t *pglob;
412 if (pglob->gl_pathv != NULL)
415 for (i = 0; i < pglob->gl_pathc; ++i)
416 if (pglob->gl_pathv[i] != NULL)
417 free ((__ptr_t) pglob->gl_pathv[i]);
418 free ((__ptr_t) pglob->gl_pathv);
423 /* Do a collated comparison of A and B. */
425 collated_compare (a, b)
429 const char *const s1 = *(const char *const * const) a;
430 const char *const s2 = *(const char *const * const) b;
438 return strcoll (s1, s2);
442 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
443 elements in place. Return nonzero if out of memory, zero if successful.
444 A slash is inserted between DIRNAME and each elt of ARRAY,
445 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
447 prefix_array (dirname, array, n)
453 size_t dirlen = strlen (dirname);
455 if (dirlen == 1 && dirname[0] == '/')
456 /* DIRNAME is just "/", so normal prepending would get us "//foo".
457 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
460 for (i = 0; i < n; ++i)
462 size_t eltlen = strlen (array[i]) + 1;
463 char *new = (char *) malloc (dirlen + 1 + eltlen);
467 free ((__ptr_t) array[--i]);
471 memcpy (new, dirname, dirlen);
473 memcpy (&new[dirlen + 1], array[i], eltlen);
474 free ((__ptr_t) array[i]);
482 /* Return nonzero if PATTERN contains any metacharacters.
483 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
485 glob_pattern_p (pattern, quote)
489 register const char *p;
492 for (p = pattern; *p != '\0'; ++p)
518 /* Like `glob', but PATTERN is a final pathname component,
519 and matches are searched for in DIRECTORY.
520 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
521 The GLOB_APPEND flag is assumed to be set (always appends). */
523 glob_in_dir (pattern, directory, flags, errfunc, pglob)
525 const char *directory;
527 int (*errfunc) __P ((const char *, int));
534 struct globlink *next;
537 struct globlink *names = NULL;
540 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
543 flags |= GLOB_NOCHECK;
547 flags |= GLOB_MAGCHAR;
549 stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
550 : (__ptr_t) opendir (directory));
553 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
563 if (__glob_readdir_hook)
565 name = (*__glob_readdir_hook) (stream);
572 struct dirent *d = readdir ((DIR *) stream);
575 if (! REAL_DIR_ENTRY (d))
585 if (fnmatch (pattern, name,
586 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
587 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
590 = (struct globlink *) __alloca (sizeof (struct globlink));
594 = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
595 if (new->name == NULL)
597 memcpy ((__ptr_t) new->name, name, len);
598 if (flags & GLOB_MARK)
599 new->name[len++] = '/';
600 new->name[len] = '\0';
608 if (nfound == 0 && (flags & GLOB_NOCHECK))
610 size_t len = strlen (pattern);
612 names = (struct globlink *) __alloca (sizeof (struct globlink));
614 names->name = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
615 if (names->name == NULL)
617 memcpy (names->name, pattern, len);
618 if (flags & GLOB_MARK)
619 names->name[len++] = '/';
620 names->name[len] = '\0';
624 = (char **) realloc (pglob->gl_pathv,
626 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
629 if (pglob->gl_pathv == NULL)
632 if (flags & GLOB_DOOFFS)
633 while (pglob->gl_pathc < pglob->gl_offs)
634 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
636 for (; names != NULL; names = names->next)
637 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
638 pglob->gl_pathv[pglob->gl_pathc] = NULL;
640 pglob->gl_flags = flags;
645 if (__glob_closedir_hook)
646 (*__glob_closedir_hook) (stream);
648 (void) closedir ((DIR *) stream);
651 return nfound == 0 ? GLOB_NOMATCH : 0;
656 if (__glob_closedir_hook)
657 (*__glob_closedir_hook) (stream);
659 (void) closedir ((DIR *) stream);
662 while (names != NULL)
664 if (names->name != NULL)
665 free ((__ptr_t) names->name);
671 #endif /* _LIBC or not __GNU_LIBRARY__. */