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 #define strrchr __strrchr
19 #define strcoll __strcoll
25 #include <sys/types.h>
34 extern __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
35 extern void (*__glob_closedir_hook) __P ((__ptr_t stream));
36 extern const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
39 static int glob_in_dir __P ((const char *pattern, const char *directory,
41 int (*errfunc) __P ((const char *, int)),
43 static int prefix_array __P ((const char *prefix, char **array, size_t n,
45 static int collated_compare __P ((const __ptr_t, const __ptr_t));
48 extern int glob_pattern_p(const char *pattern, int quote);
50 /* Return nonzero if PATTERN contains any metacharacters.
51 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
52 int glob_pattern_p(const char *pattern, int quote)
57 for (p = pattern; *p != '\0'; ++p)
65 if (quote && p[1] != '\0')
84 /* Do glob searching for PATTERN, placing results in PGLOB.
85 The bits defined above may be set in FLAGS.
86 If a directory cannot be opened or read and ERRFUNC is not nil,
87 it is called with the pathname that caused the error, and the
88 `errno' value from the failing call; if it returns non-zero
89 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
90 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
91 Otherwise, `glob' returns zero. */
93 glob (pattern, flags, errfunc, pglob)
96 int (*errfunc) __P ((const char *, int));
105 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
111 /* Find the filename. */
112 filename = strrchr (pattern, '/');
113 if (filename == NULL)
116 dirname = (char *) ".";
119 else if (filename == pattern)
122 dirname = (char *) "/";
128 dirlen = filename - pattern;
129 dirname = (char *) alloca (dirlen + 1);
130 __memcpy (dirname, pattern, dirlen);
131 dirname[dirlen] = '\0';
135 if (filename[0] == '\0' && dirlen > 1)
136 /* "pattern/". Expand "pattern", appending slashes. */
138 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
140 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
144 if (!(flags & GLOB_APPEND))
147 pglob->gl_pathv = NULL;
150 oldcount = pglob->gl_pathc;
152 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
154 /* The directory name contains metacharacters, so we
155 have to glob for the directory, and then glob for
156 the pattern in each directory found. */
160 status = glob (dirname,
161 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
167 /* We have successfully globbed the preceding directory name.
168 For each name we found, call glob_in_dir on it and FILENAME,
169 appending the results to PGLOB. */
170 for (i = 0; i < dirs.gl_pathc; ++i)
176 /* Make globbing interruptible in the bash shell. */
177 extern int interrupt_state;
188 oldcount = pglob->gl_pathc;
189 status = glob_in_dir (filename, dirs.gl_pathv[i],
190 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
192 if (status == GLOB_NOMATCH)
193 /* No matches in this directory. Try the next. */
203 /* Stick the directory on the front of each name. */
204 if (prefix_array (dirs.gl_pathv[i],
205 &pglob->gl_pathv[oldcount],
206 pglob->gl_pathc - oldcount,
215 flags |= GLOB_MAGCHAR;
217 if (pglob->gl_pathc == oldcount)
220 if (flags & GLOB_NOCHECK)
222 size_t len = __strlen (pattern) + 1;
223 char *patcopy = (char *) malloc (len);
226 __memcpy (patcopy, pattern, len);
229 = (char **) realloc (pglob->gl_pathv,
231 ((flags & GLOB_DOOFFS) ?
232 pglob->gl_offs : 0) +
235 if (pglob->gl_pathv == NULL)
241 if (flags & GLOB_DOOFFS)
242 while (pglob->gl_pathc < pglob->gl_offs)
243 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
245 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
246 pglob->gl_pathv[pglob->gl_pathc] = NULL;
247 pglob->gl_flags = flags;
257 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
263 /* Stick the directory on the front of each name. */
264 if (prefix_array (dirname,
265 &pglob->gl_pathv[oldcount],
266 pglob->gl_pathc - oldcount,
275 if (flags & GLOB_MARK)
277 /* Append slashes to directory names. glob_in_dir has already
278 allocated the extra character for us. */
281 for (i = oldcount; i < pglob->gl_pathc; ++i)
282 if (lstat (pglob->gl_pathv[i], &st) == 0 &&
283 S_ISDIR (st.st_mode))
284 __strcat (pglob->gl_pathv[i], "/");
287 if (!(flags & GLOB_NOSORT))
288 /* Sort the vector. */
289 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
290 pglob->gl_pathc - oldcount,
291 sizeof (char *), (__compar_fn_t)collated_compare);
297 /* Free storage allocated in PGLOB by a previous `glob' call. */
300 register glob_t *pglob;
302 if (pglob->gl_pathv != NULL)
304 register int i = pglob->gl_flags & GLOB_DOOFFS? pglob->gl_offs : 0;
305 for (; i < pglob->gl_pathc; ++i)
306 if (pglob->gl_pathv[i] != NULL)
307 free ((__ptr_t) pglob->gl_pathv[i]);
308 free ((__ptr_t) pglob->gl_pathv);
313 /* Do a collated comparison of A and B. */
315 collated_compare (a, b)
319 const char *const s1 = *(const char *const *) a;
320 const char *const s2 = *(const char *const *) b;
328 return strcoll (s1, s2);
332 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
333 elements in place. Return nonzero if out of memory, zero if successful.
334 A slash is inserted between DIRNAME and each elt of ARRAY,
335 unless DIRNAME is just "/". Each old element of ARRAY is freed.
336 If ADD_SLASH is non-zero, allocate one character more than
337 necessary, so that a slash can be appended later. */
339 prefix_array (dirname, array, n, add_slash)
346 size_t dirlen = __strlen (dirname);
348 if (dirlen == 1 && dirname[0] == '/')
349 /* DIRNAME is just "/", so normal prepending would get us "//foo".
350 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
353 for (i = 0; i < n; ++i)
355 size_t eltlen = __strlen (array[i]) + 1;
356 char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0));
360 free ((__ptr_t) array[--i]);
364 __memcpy (new, dirname, dirlen);
366 __memcpy (&new[dirlen + 1], array[i], eltlen);
367 free ((__ptr_t) array[i]);
375 /* Like `glob', but PATTERN is a final pathname component,
376 and matches are searched for in DIRECTORY.
377 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
378 The GLOB_APPEND flag is assumed to be set (always appends). */
380 glob_in_dir (pattern, directory, flags, errfunc, pglob)
382 const char *directory;
384 int (*errfunc) __P ((const char *, int));
391 struct globlink *next;
394 struct globlink *names = NULL;
398 stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
399 : (__ptr_t) opendir (directory));
402 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
407 meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
410 flags |= GLOB_MAGCHAR;
417 if (__glob_readdir_hook)
419 name = (*__glob_readdir_hook) (stream);
426 struct dirent *d = readdir ((DIR *) stream);
429 if (! (d->d_ino != 0))
432 #ifdef _DIRENT_HAVE_D_NAMLEN
439 if ((!meta && __strcmp (pattern, name) == 0)
440 || fnmatch (pattern, name,
441 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
442 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
445 = (struct globlink *) alloca (sizeof (struct globlink));
447 len = __strlen (name);
449 = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
450 if (new->name == NULL)
452 __memcpy ((__ptr_t) new->name, name, len);
453 new->name[len] = '\0';
462 if (nfound == 0 && (flags & GLOB_NOCHECK))
464 size_t len = __strlen (pattern);
466 names = (struct globlink *) alloca (sizeof (struct globlink));
468 names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1);
469 if (names->name == NULL)
471 __memcpy (names->name, pattern, len);
472 names->name[len] = '\0';
476 = (char **) realloc (pglob->gl_pathv,
478 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
481 if (pglob->gl_pathv == NULL)
484 if (flags & GLOB_DOOFFS)
485 while (pglob->gl_pathc < pglob->gl_offs)
486 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
488 for (; names != NULL; names = names->next)
489 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
490 pglob->gl_pathv[pglob->gl_pathc] = NULL;
492 pglob->gl_flags = flags;
496 if (__glob_closedir_hook)
497 (*__glob_closedir_hook) (stream);
499 (void) closedir ((DIR *) stream);
502 return nfound == 0 ? GLOB_NOMATCH : 0;
507 if (__glob_closedir_hook)
508 (*__glob_closedir_hook) (stream);
510 (void) closedir ((DIR *) stream);
513 while (names != NULL)
515 if (names->name != NULL)
516 free ((__ptr_t) names->name);