9 #if defined(sun) && !defined(__svr4__)
13 /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Library General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with this library; see the file COPYING.LIB. If
27 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
28 Cambridge, MA 02139, USA. */
30 /* AIX requires this to be the first thing in the file. */
31 #if defined (_AIX) && !defined (__GNUC__)
36 #include <sys/types.h>
40 /* Comment out all this code if we are using the GNU C Library, and are not
41 actually compiling the library itself. This code is part of the GNU C
42 Library, but also included in many other GNU distributions. Compiling
43 and linking in this code is a waste when using the GNU C library
44 (especially if it is a shared library). Rather than having every GNU
45 program understand `configure --with-gnu-libc' and omit the object files,
46 it is simpler to just do this in the source for each such file. */
48 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
64 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
72 #if defined (POSIX) || defined (DIRENT) || defined (__GNU_LIBRARY__) || defined(HAVE_DIRENT_H)
74 #ifndef __GNU_LIBRARY__
75 #define D_NAMLEN(d) strlen((d)->d_name)
76 #else /* GNU C library. */
77 #define D_NAMLEN(d) ((d)->d_namlen)
78 #endif /* Not GNU C library. */
79 #else /* Not POSIX or DIRENT. */
81 #define D_NAMLEN(d) ((d)->d_namlen)
91 #endif /* POSIX or DIRENT or __GNU_LIBRARY__. */
93 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
94 /* Posix does not require that the d_ino field be present, and some
95 systems do not provide it. */
96 #define REAL_DIR_ENTRY(dp) 1
98 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
101 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
105 #else /* No standard headers. */
117 extern char *malloc (), *realloc ();
120 extern void qsort ();
121 extern void abort (), exit ();
123 #endif /* Standard headers. */
128 extern void bzero ();
131 extern void bcopy ();
134 #define memcpy(d, s, n) bcopy ((s), (d), (n))
135 #define strrchr rindex
136 /* memset is only used for zero here, but let's be paranoid. */
137 #define memset(s, better_be_zero, n) \
138 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
139 #endif /* Not ANSI_STRING. */
142 #define strcoll strcmp
146 #ifndef __GNU_LIBRARY__
155 /* These casts are the for sake of the broken Ultrix compiler,
156 which warns of illegal pointer combinations otherwise. */
158 return (char *) malloc (n);
159 return (char *) realloc (p, n);
161 #define realloc my_realloc
165 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
169 #define alloca(n) __builtin_alloca (n)
171 #if defined (sparc) || defined (HAVE_ALLOCA_H)
173 #else /* Not sparc or HAVE_ALLOCA_H. */
175 extern char *alloca ();
176 #endif /* Not _AIX. */
177 #endif /* sparc or HAVE_ALLOCA_H. */
180 #define __alloca alloca
186 #define size_t unsigned int
189 /* Some system header files erroneously define these.
190 We want our own definitions from <fnmatch.h> to take precedence. */
195 /* Some system header files erroneously define these.
196 We want our own definitions from <glob.h> to take precedence. */
207 __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
208 const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
209 void (*__glob_closedir_hook) __P ((__ptr_t stream));
211 static int glob_pattern_p __P ((const char *pattern, int quote));
212 static int glob_in_dir __P ((const char *pattern, const char *directory,
214 int (*errfunc) __P ((const char *, int)),
216 static int prefix_array __P ((const char *prefix, char **array, size_t n));
217 static int collated_compare __P ((const __ptr_t, const __ptr_t));
219 /* Do glob searching for PATTERN, placing results in PGLOB.
220 The bits defined above may be set in FLAGS.
221 If a directory cannot be opened or read and ERRFUNC is not nil,
222 it is called with the pathname that caused the error, and the
223 `errno' value from the failing call; if it returns non-zero
224 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
225 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
226 Otherwise, `glob' returns zero. */
228 glob (pattern, flags, errfunc, pglob)
231 int (*errfunc) __P ((const char *, int));
234 const char *filename;
240 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
246 /* Find the filename. */
247 filename = strrchr (pattern, '/');
248 if (filename == NULL)
251 dirname = (char *) ".";
254 else if (filename == pattern)
257 dirname = (char *) "/";
263 dirlen = filename - pattern;
264 dirname = (char *) __alloca (dirlen + 1);
265 memcpy (dirname, pattern, dirlen);
266 dirname[dirlen] = '\0';
270 if (filename[0] == '\0' && dirlen > 1)
271 /* "pattern/". Expand "pattern", appending slashes. */
273 int ret = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
275 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
279 if (!(flags & GLOB_APPEND))
282 pglob->gl_pathv = NULL;
285 oldcount = pglob->gl_pathc;
287 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
289 /* The directory name contains metacharacters, so we
290 have to glob for the directory, and then glob for
291 the pattern in each directory found. */
295 status = glob (dirname,
296 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
302 /* We have successfully globbed the preceding directory name.
303 For each name we found, call glob_in_dir on it and FILENAME,
304 appending the results to PGLOB. */
305 for (i = 0; i < dirs.gl_pathc; ++i)
311 /* Make globbing interruptible in the bash shell. */
312 extern int interrupt_state;
323 oldcount = pglob->gl_pathc;
324 status = glob_in_dir (filename, dirs.gl_pathv[i],
325 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
327 if (status == GLOB_NOMATCH)
328 /* No matches in this directory. Try the next. */
338 /* Stick the directory on the front of each name. */
339 if (prefix_array (dirs.gl_pathv[i],
340 &pglob->gl_pathv[oldcount],
341 pglob->gl_pathc - oldcount))
349 flags |= GLOB_MAGCHAR;
351 if (pglob->gl_pathc == oldcount)
353 if (flags & GLOB_NOCHECK)
355 size_t len = strlen (pattern) + 1;
356 char *patcopy = (char *) malloc (len);
359 memcpy (patcopy, pattern, len);
362 = (char **) realloc (pglob->gl_pathv,
364 ((flags & GLOB_DOOFFS) ?
365 pglob->gl_offs : 0) +
368 if (pglob->gl_pathv == NULL)
374 if (flags & GLOB_DOOFFS)
375 while (pglob->gl_pathc < pglob->gl_offs)
376 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
378 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
379 pglob->gl_pathv[pglob->gl_pathc] = NULL;
380 pglob->gl_flags = flags;
387 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
393 /* Stick the directory on the front of each name. */
394 if (prefix_array (dirname,
395 &pglob->gl_pathv[oldcount],
396 pglob->gl_pathc - oldcount))
404 if (!(flags & GLOB_NOSORT))
405 /* Sort the vector. */
406 qsort ((__ptr_t) & pglob->gl_pathv[oldcount],
407 pglob->gl_pathc - oldcount,
408 sizeof (char *), collated_compare);
414 /* Free storage allocated in PGLOB by a previous `glob' call. */
417 register glob_t *pglob;
419 if (pglob->gl_pathv != NULL)
422 for (i = 0; i < pglob->gl_pathc; ++i)
423 if (pglob->gl_pathv[i] != NULL)
424 free ((__ptr_t) pglob->gl_pathv[i]);
425 free ((__ptr_t) pglob->gl_pathv);
430 /* Do a collated comparison of A and B. */
432 collated_compare (a, b)
436 const char *const s1 = *(const char *const * const) a;
437 const char *const s2 = *(const char *const * const) b;
445 return strcoll (s1, s2);
449 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
450 elements in place. Return nonzero if out of memory, zero if successful.
451 A slash is inserted between DIRNAME and each elt of ARRAY,
452 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
454 prefix_array (dirname, array, n)
460 size_t dirlen = strlen (dirname);
462 if (dirlen == 1 && dirname[0] == '/')
463 /* DIRNAME is just "/", so normal prepending would get us "//foo".
464 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
467 for (i = 0; i < n; ++i)
469 size_t eltlen = strlen (array[i]) + 1;
470 char *new = (char *) malloc (dirlen + 1 + eltlen);
474 free ((__ptr_t) array[--i]);
478 memcpy (new, dirname, dirlen);
480 memcpy (&new[dirlen + 1], array[i], eltlen);
481 free ((__ptr_t) array[i]);
489 /* Return nonzero if PATTERN contains any metacharacters.
490 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
492 glob_pattern_p (pattern, quote)
496 register const char *p;
499 for (p = pattern; *p != '\0'; ++p)
525 /* Like `glob', but PATTERN is a final pathname component,
526 and matches are searched for in DIRECTORY.
527 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
528 The GLOB_APPEND flag is assumed to be set (always appends). */
530 glob_in_dir (pattern, directory, flags, errfunc, pglob)
532 const char *directory;
534 int (*errfunc) __P ((const char *, int));
541 struct globlink *next;
544 struct globlink *names = NULL;
547 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
550 flags |= GLOB_NOCHECK;
554 flags |= GLOB_MAGCHAR;
556 stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
557 : (__ptr_t) opendir (directory));
560 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
570 if (__glob_readdir_hook)
572 name = (*__glob_readdir_hook) (stream);
579 struct dirent *d = readdir ((DIR *) stream);
582 if (! REAL_DIR_ENTRY (d))
592 if (fnmatch (pattern, name,
593 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
594 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
597 = (struct globlink *) __alloca (sizeof (struct globlink));
601 = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
602 if (new->name == NULL)
604 memcpy ((__ptr_t) new->name, name, len);
605 if (flags & GLOB_MARK)
606 new->name[len++] = '/';
607 new->name[len] = '\0';
615 if (nfound == 0 && (flags & GLOB_NOCHECK))
617 size_t len = strlen (pattern);
619 names = (struct globlink *) __alloca (sizeof (struct globlink));
621 names->name = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
622 if (names->name == NULL)
624 memcpy (names->name, pattern, len);
625 if (flags & GLOB_MARK)
626 names->name[len++] = '/';
627 names->name[len] = '\0';
631 = (char **) realloc (pglob->gl_pathv,
633 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
636 if (pglob->gl_pathv == NULL)
639 if (flags & GLOB_DOOFFS)
640 while (pglob->gl_pathc < pglob->gl_offs)
641 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
643 for (; names != NULL; names = names->next)
644 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
645 pglob->gl_pathv[pglob->gl_pathc] = NULL;
647 pglob->gl_flags = flags;
652 if (__glob_closedir_hook)
653 (*__glob_closedir_hook) (stream);
655 (void) closedir ((DIR *) stream);
658 return nfound == 0 ? GLOB_NOMATCH : 0;
663 if (__glob_closedir_hook)
664 (*__glob_closedir_hook) (stream);
666 (void) closedir ((DIR *) stream);
669 while (names != NULL)
671 if (names->name != NULL)
672 free ((__ptr_t) names->name);
678 #endif /* _LIBC or not __GNU_LIBRARY__. */