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. */
22 #include <sys/types.h>
31 libc_hidden_proto(memcpy)
32 libc_hidden_proto(strcat)
33 libc_hidden_proto(strcmp)
34 libc_hidden_proto(strlen)
35 libc_hidden_proto(strrchr)
36 libc_hidden_proto(strcoll)
37 libc_hidden_proto(opendir)
38 libc_hidden_proto(closedir)
39 libc_hidden_proto(fnmatch)
40 libc_hidden_proto(qsort)
41 libc_hidden_proto(lstat)
43 extern __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
44 extern void (*__glob_closedir_hook) __P ((__ptr_t stream));
45 extern const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
48 static int glob_in_dir __P ((const char *pattern, const char *directory,
50 int (*errfunc) __P ((const char *, int)),
52 static int prefix_array __P ((const char *prefix, char **array, size_t n,
54 static int collated_compare __P ((const __ptr_t, const __ptr_t));
57 extern int __glob_pattern_p(const char *pattern, int quote) attribute_hidden;
58 libc_hidden_proto(readdir64)
59 #define __readdir readdir64
61 extern int __glob (__const char *__restrict __pattern, int __flags,
62 int (*__errfunc) (__const char *, int),
63 glob_t *__restrict __pglob) __THROW attribute_hidden;
64 extern void __globfree (glob_t *__pglob) __THROW attribute_hidden;
65 #define __readdir readdir
66 libc_hidden_proto(readdir)
67 /* Return nonzero if PATTERN contains any metacharacters.
68 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
69 int attribute_hidden __glob_pattern_p(const char *pattern, int quote)
74 for (p = pattern; *p != '\0'; ++p)
82 if (quote && p[1] != '\0')
98 strong_alias(__glob_pattern_p,glob_pattern_p)
102 /* Do glob searching for PATTERN, placing results in PGLOB.
103 The bits defined above may be set in FLAGS.
104 If a directory cannot be opened or read and ERRFUNC is not nil,
105 it is called with the pathname that caused the error, and the
106 `errno' value from the failing call; if it returns non-zero
107 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
108 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
109 Otherwise, `glob' returns zero. */
111 __glob (pattern, flags, errfunc, pglob)
114 int (*errfunc) __P ((const char *, int));
117 const char *filename;
123 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
129 /* Find the filename. */
130 filename = strrchr (pattern, '/');
131 if (filename == NULL)
134 dirname = (char *) ".";
137 else if (filename == pattern)
140 dirname = (char *) "/";
146 dirlen = filename - pattern;
147 dirname = (char *) alloca (dirlen + 1);
148 memcpy (dirname, pattern, dirlen);
149 dirname[dirlen] = '\0';
153 if (filename[0] == '\0' && dirlen > 1)
154 /* "pattern/". Expand "pattern", appending slashes. */
156 int val = __glob (dirname, flags | GLOB_MARK, errfunc, pglob);
158 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
162 if (!(flags & GLOB_APPEND))
165 pglob->gl_pathv = NULL;
168 oldcount = pglob->gl_pathc;
170 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
172 /* The directory name contains metacharacters, so we
173 have to glob for the directory, and then glob for
174 the pattern in each directory found. */
178 status = __glob (dirname,
179 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
185 /* We have successfully globbed the preceding directory name.
186 For each name we found, call glob_in_dir on it and FILENAME,
187 appending the results to PGLOB. */
188 for (i = 0; i < dirs.gl_pathc; ++i)
194 /* Make globbing interruptible in the bash shell. */
195 extern int interrupt_state;
206 oldcount = pglob->gl_pathc;
207 status = glob_in_dir (filename, dirs.gl_pathv[i],
208 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
210 if (status == GLOB_NOMATCH)
211 /* No matches in this directory. Try the next. */
221 /* Stick the directory on the front of each name. */
222 if (prefix_array (dirs.gl_pathv[i],
223 &pglob->gl_pathv[oldcount],
224 pglob->gl_pathc - oldcount,
233 flags |= GLOB_MAGCHAR;
235 if (pglob->gl_pathc == oldcount)
238 if (flags & GLOB_NOCHECK)
240 size_t len = strlen (pattern) + 1;
241 char *patcopy = (char *) malloc (len);
244 memcpy (patcopy, pattern, len);
247 = (char **) realloc (pglob->gl_pathv,
249 ((flags & GLOB_DOOFFS) ?
250 pglob->gl_offs : 0) +
253 if (pglob->gl_pathv == NULL)
259 if (flags & GLOB_DOOFFS)
260 while (pglob->gl_pathc < pglob->gl_offs)
261 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
263 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
264 pglob->gl_pathv[pglob->gl_pathc] = NULL;
265 pglob->gl_flags = flags;
275 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
281 /* Stick the directory on the front of each name. */
282 if (prefix_array (dirname,
283 &pglob->gl_pathv[oldcount],
284 pglob->gl_pathc - oldcount,
293 if (flags & GLOB_MARK)
295 /* Append slashes to directory names. glob_in_dir has already
296 allocated the extra character for us. */
299 for (i = oldcount; i < pglob->gl_pathc; ++i)
300 if (lstat (pglob->gl_pathv[i], &st) == 0 &&
301 S_ISDIR (st.st_mode))
302 strcat (pglob->gl_pathv[i], "/");
305 if (!(flags & GLOB_NOSORT))
306 /* Sort the vector. */
307 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
308 pglob->gl_pathc - oldcount,
309 sizeof (char *), (__compar_fn_t)collated_compare);
314 strong_alias(__glob64,glob64)
316 strong_alias(__glob,glob)
320 /* Free storage allocated in PGLOB by a previous `glob' call. */
321 void attribute_hidden
323 register glob_t *pglob;
325 if (pglob->gl_pathv != NULL)
327 register int i = pglob->gl_flags & GLOB_DOOFFS? pglob->gl_offs : 0;
328 for (; i < pglob->gl_pathc; ++i)
329 if (pglob->gl_pathv[i] != NULL)
330 free ((__ptr_t) pglob->gl_pathv[i]);
331 free ((__ptr_t) pglob->gl_pathv);
335 strong_alias(__globfree64,globfree64)
337 strong_alias(__globfree,globfree)
341 /* Do a collated comparison of A and B. */
343 collated_compare (a, b)
347 const char *const s1 = *(const char *const *) a;
348 const char *const s2 = *(const char *const *) b;
356 return strcoll (s1, s2);
360 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
361 elements in place. Return nonzero if out of memory, zero if successful.
362 A slash is inserted between DIRNAME and each elt of ARRAY,
363 unless DIRNAME is just "/". Each old element of ARRAY is freed.
364 If ADD_SLASH is non-zero, allocate one character more than
365 necessary, so that a slash can be appended later. */
367 prefix_array (dirname, array, n, add_slash)
374 size_t dirlen = strlen (dirname);
376 if (dirlen == 1 && dirname[0] == '/')
377 /* DIRNAME is just "/", so normal prepending would get us "//foo".
378 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
381 for (i = 0; i < n; ++i)
383 size_t eltlen = strlen (array[i]) + 1;
384 char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0));
388 free ((__ptr_t) array[--i]);
392 memcpy (new, dirname, dirlen);
394 memcpy (&new[dirlen + 1], array[i], eltlen);
395 free ((__ptr_t) array[i]);
403 /* Like `glob', but PATTERN is a final pathname component,
404 and matches are searched for in DIRECTORY.
405 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
406 The GLOB_APPEND flag is assumed to be set (always appends). */
408 glob_in_dir (pattern, directory, flags, errfunc, pglob)
410 const char *directory;
412 int (*errfunc) __P ((const char *, int));
419 struct globlink *next;
422 struct globlink *names = NULL;
426 stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
427 : (__ptr_t) opendir (directory));
430 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
435 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
438 flags |= GLOB_MAGCHAR;
445 if (__glob_readdir_hook)
447 name = (*__glob_readdir_hook) (stream);
454 struct dirent *d = __readdir ((DIR *) stream);
457 if (! (d->d_ino != 0))
460 #ifdef _DIRENT_HAVE_D_NAMLEN
467 if ((!meta && strcmp (pattern, name) == 0)
468 || fnmatch (pattern, name,
469 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
470 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
473 = (struct globlink *) alloca (sizeof (struct globlink));
477 = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
478 if (new->name == NULL)
480 memcpy ((__ptr_t) new->name, name, len);
481 new->name[len] = '\0';
490 if (nfound == 0 && (flags & GLOB_NOCHECK))
492 size_t len = strlen (pattern);
494 names = (struct globlink *) alloca (sizeof (struct globlink));
496 names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1);
497 if (names->name == NULL)
499 memcpy (names->name, pattern, len);
500 names->name[len] = '\0';
504 = (char **) realloc (pglob->gl_pathv,
506 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
509 if (pglob->gl_pathv == NULL)
512 if (flags & GLOB_DOOFFS)
513 while (pglob->gl_pathc < pglob->gl_offs)
514 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
516 for (; names != NULL; names = names->next)
517 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
518 pglob->gl_pathv[pglob->gl_pathc] = NULL;
520 pglob->gl_flags = flags;
524 if (__glob_closedir_hook)
525 (*__glob_closedir_hook) (stream);
527 (void) closedir ((DIR *) stream);
530 return nfound == 0 ? GLOB_NOMATCH : 0;
535 if (__glob_closedir_hook)
536 (*__glob_closedir_hook) (stream);
538 (void) closedir ((DIR *) stream);
541 while (names != NULL)
543 if (names->name != NULL)
544 free ((__ptr_t) names->name);