1 /* Copyright (C) 1991, 1992 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__)
31 #include <sys/types.h>
37 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
45 #if defined (USGr3) && !defined (DIRENT)
48 #if defined (Xenix) && !defined (SYSNDIR)
52 #if defined (POSIX) || defined (DIRENT) || defined (__GNU_LIBRARY__)
54 #ifndef __GNU_LIBRARY__
55 #define D_NAMLEN(d) strlen((d)->d_name)
58 #define D_NAMLEN(d) ((d)->d_namlen)
60 #else /* not POSIX or DIRENT */
62 #define D_NAMLEN(d) ((d)->d_namlen)
64 #if defined (USG) && !defined (sgi)
69 #endif /* not SYSNDIR */
73 #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
75 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
76 /* Posix does not require that the d_ino field be present, and some
77 systems do not provide it. */
78 #define REAL_DIR_ENTRY(dp) 1
80 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
87 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__) \
92 #else /* No standard headers. */
108 #else /* Not NeXT. */
116 extern void bzero ();
119 extern void bcopy ();
126 extern char *malloc (), *realloc ();
128 extern void qsort ();
130 #endif /* Standard headers. */
133 #define memcpy(d, s, n) bcopy((s), (d), (n))
134 #define strrchr rindex
135 /* memset is only used for zero here, but let's be paranoid. */
136 #define memset(s, better_be_zero, n) \
137 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
138 #endif /* Not ANSI_STRING. */
141 #define strcoll strcmp
145 #ifndef __GNU_LIBRARY__
156 return realloc (p, n);
158 #define realloc my_realloc
162 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
166 #define alloca(n) __builtin_alloca (n)
168 #if defined (sparc) || defined (HAVE_ALLOCA_H)
170 #else /* Not sparc or HAVE_ALLOCA_H. */
172 extern char *alloca ();
173 #endif /* Not _AIX. */
174 #endif /* sparc or HAVE_ALLOCA_H. */
177 #define __alloca alloca
183 #define size_t unsigned int
186 __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
187 const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
188 void (*__glob_closedir_hook) __P ((__ptr_t stream));
190 static int glob_pattern_p __P ((const char *pattern, int quote));
191 static int glob_in_dir __P ((const char *pattern, const char *directory,
193 int (*errfunc) __P ((const char *, int)),
195 static int prefix_array __P ((const char *prefix, char **array, size_t n));
196 static int collated_compare __P ((const __ptr_t, const __ptr_t));
198 /* Do glob searching for PATTERN, placing results in PGLOB.
199 The bits defined above may be set in FLAGS.
200 If a directory cannot be opened or read and ERRFUNC is not nil,
201 it is called with the pathname that caused the error, and the
202 `errno' value from the failing call; if it returns non-zero
203 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
204 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
205 Otherwise, `glob' returns zero. */
207 glob (pattern, flags, errfunc, pglob)
210 int (*errfunc) __P ((const char *, int));
213 const char *filename;
219 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
225 /* Find the filename. */
226 filename = strrchr (pattern, '/');
227 if (filename == NULL)
230 dirname = (char *) ".";
233 else if (filename == pattern)
236 dirname = (char *) "/";
242 dirlen = filename - pattern;
243 dirname = (char *) __alloca (dirlen + 1);
244 memcpy (dirname, pattern, dirlen);
245 dirname[dirlen] = '\0';
249 if (filename[0] == '\0' && dirlen > 1)
250 /* "pattern/". Expand "pattern", appending slashes. */
252 int ret = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
254 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
258 if (!(flags & GLOB_APPEND))
261 pglob->gl_pathv = NULL;
264 oldcount = pglob->gl_pathc;
266 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
268 /* The directory name contains metacharacters, so we
269 have to glob for the directory, and then glob for
270 the pattern in each directory found. */
274 status = glob (dirname,
275 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
281 /* We have successfully globbed the preceding directory name.
282 For each name we found, call glob_in_dir on it and FILENAME,
283 appending the results to PGLOB. */
284 for (i = 0; i < dirs.gl_pathc; ++i)
290 /* Make globbing interruptible in the bash shell. */
291 extern int interrupt_state;
302 oldcount = pglob->gl_pathc;
303 status = glob_in_dir (filename, dirs.gl_pathv[i],
304 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
306 if (status == GLOB_NOMATCH)
307 /* No matches in this directory. Try the next. */
317 /* Stick the directory on the front of each name. */
318 if (prefix_array (dirs.gl_pathv[i],
319 &pglob->gl_pathv[oldcount],
320 pglob->gl_pathc - oldcount))
328 flags |= GLOB_MAGCHAR;
330 if (pglob->gl_pathc == oldcount)
332 if (flags & GLOB_NOCHECK)
334 const size_t len = strlen (pattern) + 1;
335 char *patcopy = (char *) malloc (len);
338 memcpy (patcopy, pattern, len);
341 = (char **) realloc (pglob->gl_pathv,
343 ((flags & GLOB_DOOFFS) ?
344 pglob->gl_offs : 0) +
347 if (pglob->gl_pathv == NULL)
353 if (flags & GLOB_DOOFFS)
354 while (pglob->gl_pathc < pglob->gl_offs)
355 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
357 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
358 pglob->gl_flags = flags;
365 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
371 /* Stick the directory on the front of each name. */
372 if (prefix_array (dirname,
373 &pglob->gl_pathv[oldcount],
374 pglob->gl_pathc - oldcount))
381 if (!(flags & GLOB_NOSORT))
382 qsort ((__ptr_t) & pglob->gl_pathv[oldcount],
383 pglob->gl_pathc - oldcount,
384 sizeof (char *), collated_compare);
391 /* Free storage allocated in PGLOB by a previous `glob' call. */
394 register glob_t *pglob;
396 if (pglob->gl_pathv != NULL)
399 for (i = 0; i < pglob->gl_pathc; ++i)
400 if (pglob->gl_pathv[i] != NULL)
401 free ((__ptr_t) pglob->gl_pathv[i]);
402 free ((__ptr_t) pglob->gl_pathv);
407 /* Do a collated comparison of A and B. */
409 collated_compare (a, b)
413 const char *const s1 = *(const char *const * const) a;
414 const char *const s2 = *(const char *const * const) b;
422 return strcoll (s1, s2);
426 /* Prepend PREFIX to each of N members of ARRAY, replacing ARRAY's
427 elements in place. Return nonzero if out of memory, zero if successful.
428 A slash is inserted between PREFIX and each elt of ARRAY.
429 Each old element of ARRAY is freed. */
431 prefix_array (prefix, array, n)
437 const size_t prelen = strlen (prefix);
439 for (i = 0; i < n; ++i)
441 const size_t eltlen = strlen (array[i]) + 1;
442 char *new = (char *) malloc (prelen + 1 + eltlen);
446 free ((__ptr_t) array[--i]);
450 memcpy (new, prefix, prelen);
452 memcpy (&new[prelen + 1], array[i], eltlen);
453 free ((__ptr_t) array[i]);
461 /* Return nonzero if PATTERN contains any metacharacters.
462 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
464 glob_pattern_p (pattern, quote)
468 register const char *p;
471 for (p = pattern; *p != '\0'; ++p)
497 /* Like `glob', but PATTERN is a final pathname component,
498 and matches are searched for in DIRECTORY.
499 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
500 The GLOB_APPEND flag is assumed to be set (always appends). */
502 glob_in_dir (pattern, directory, flags, errfunc, pglob)
504 const char *directory;
506 int (*errfunc) __P ((const char *, int));
513 struct globlink *next;
516 struct globlink *names = NULL;
519 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
522 flags |= GLOB_NOCHECK;
526 flags |= GLOB_MAGCHAR;
528 stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
529 : (__ptr_t) opendir (directory));
532 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
542 if (__glob_readdir_hook)
544 name = (*__glob_readdir_hook) (stream);
551 struct dirent *d = readdir ((DIR *) stream);
554 if (! REAL_DIR_ENTRY (d))
564 if (fnmatch (pattern, name,
565 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
566 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
569 = (struct globlink *) __alloca (sizeof (struct globlink));
573 = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
574 if (new->name == NULL)
576 memcpy ((__ptr_t) new->name, name, len);
577 if (flags & GLOB_MARK)
578 new->name[len++] = '/';
579 new->name[len] = '\0';
587 if (nfound == 0 && (flags & GLOB_NOCHECK))
589 size_t len = strlen (pattern);
591 names = (struct globlink *) __alloca (sizeof (struct globlink));
593 names->name = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
594 if (names->name == NULL)
596 memcpy (names->name, pattern, len);
597 if (flags & GLOB_MARK)
598 names->name[len++] = '/';
599 names->name[len] = '\0';
603 = (char **) realloc (pglob->gl_pathv,
605 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
608 if (pglob->gl_pathv == NULL)
611 if (flags & GLOB_DOOFFS)
612 while (pglob->gl_pathc < pglob->gl_offs)
613 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
615 for (; names != NULL; names = names->next)
616 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
617 pglob->gl_pathv[pglob->gl_pathc] = NULL;
619 pglob->gl_flags = flags;
624 if (__glob_closedir_hook)
625 (*__glob_closedir_hook) (stream);
627 (void) closedir ((DIR *) stream);
630 return nfound == 0 ? GLOB_NOMATCH : 0;
635 if (__glob_closedir_hook)
636 (*__glob_closedir_hook) (stream);
638 (void) closedir ((DIR *) stream);
641 while (names != NULL)
643 if (names->name != NULL)
644 free ((__ptr_t) names->name);