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>
32 /* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */
40 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
56 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
65 #if defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
67 #ifndef __GNU_LIBRARY__
68 #define D_NAMLEN(d) strlen((d)->d_name)
69 #else /* GNU C library. */
70 #define D_NAMLEN(d) ((d)->d_namlen)
71 #endif /* Not GNU C library. */
72 #else /* Not POSIX or HAVE_DIRENT_H. */
74 #define D_NAMLEN(d) ((d)->d_namlen)
75 #ifdef HAVE_SYS_NDIR_H
77 #endif /* HAVE_SYS_NDIR_H */
80 #endif /* HAVE_SYS_DIR_H */
83 #endif /* HAVE_NDIR_H */
84 #endif /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__. */
86 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
87 /* Posix does not require that the d_ino field be present, and some
88 systems do not provide it. */
89 #define REAL_DIR_ENTRY(dp) 1
91 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
94 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
98 #else /* No standard headers. */
110 extern char *malloc (), *realloc ();
113 extern void qsort ();
114 extern void abort (), exit ();
116 #endif /* Standard headers. */
121 extern void bzero ();
124 extern void bcopy ();
127 #define memcpy(d, s, n) bcopy ((s), (d), (n))
128 #define strrchr rindex
129 /* memset is only used for zero here, but let's be paranoid. */
130 #define memset(s, better_be_zero, n) \
131 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
132 #endif /* Not ANSI_STRING. */
135 #define strcoll strcmp
139 #ifndef __GNU_LIBRARY__
148 /* These casts are the for sake of the broken Ultrix compiler,
149 which warns of illegal pointer combinations otherwise. */
151 return (char *) malloc (n
);
152 return (char *) realloc (p
, n
);
154 #define realloc my_realloc
158 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
162 #define alloca(n) __builtin_alloca (n)
164 #if defined (sparc) || defined (HAVE_ALLOCA_H)
166 #else /* Not sparc or HAVE_ALLOCA_H. */
168 extern char *alloca ();
169 #endif /* Not _AIX. */
170 #endif /* sparc or HAVE_ALLOCA_H. */
173 #define __alloca alloca
177 #ifndef __GNU_LIBRARY__
178 #define __lstat lstat
182 #ifdef STAT_MACROS_BROKEN
186 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
192 #define size_t unsigned int
195 /* Some system header files erroneously define these.
196 We want our own definitions from <fnmatch.h> to take precedence. */
202 /* Some system header files erroneously define these.
203 We want our own definitions from <glob.h> to take precedence. */
214 __ptr_t (*__glob_opendir_hook
) __P ((const char *directory
));
215 const char *(*__glob_readdir_hook
) __P ((__ptr_t stream
));
216 void (*__glob_closedir_hook
) __P ((__ptr_t stream
));
218 static int glob_pattern_p
__P ((const char *pattern
, int quote
));
219 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
221 int (*errfunc
) __P ((const char *, int)),
223 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
224 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
226 /* Do glob searching for PATTERN, placing results in PGLOB.
227 The bits defined above may be set in FLAGS.
228 If a directory cannot be opened or read and ERRFUNC is not nil,
229 it is called with the pathname that caused the error, and the
230 `errno' value from the failing call; if it returns non-zero
231 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
232 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
233 Otherwise, `glob' returns zero. */
235 glob (pattern
, flags
, errfunc
, pglob
)
238 int (*errfunc
) __P ((const char *, int));
241 const char *filename
;
247 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
253 /* Find the filename. */
254 filename
= strrchr (pattern
, '/');
255 if (filename
== NULL
)
258 dirname
= (char *) ".";
261 else if (filename
== pattern
)
264 dirname
= (char *) "/";
270 dirlen
= filename
- pattern
;
271 dirname
= (char *) __alloca (dirlen
+ 1);
272 memcpy (dirname
, pattern
, dirlen
);
273 dirname
[dirlen
] = '\0';
277 if (filename
[0] == '\0' && dirlen
> 1)
278 /* "pattern/". Expand "pattern", appending slashes. */
280 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
282 pglob
->gl_flags
= (pglob
->gl_flags
& ~GLOB_MARK
) | (flags
& GLOB_MARK
);
286 if (!(flags
& GLOB_APPEND
))
289 pglob
->gl_pathv
= NULL
;
292 oldcount
= pglob
->gl_pathc
;
294 if (glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
296 /* The directory name contains metacharacters, so we
297 have to glob for the directory, and then glob for
298 the pattern in each directory found. */
302 status
= glob (dirname
,
303 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
)) |
309 /* We have successfully globbed the preceding directory name.
310 For each name we found, call glob_in_dir on it and FILENAME,
311 appending the results to PGLOB. */
312 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
318 /* Make globbing interruptible in the bash shell. */
319 extern int interrupt_state
;
330 oldcount
= pglob
->gl_pathc
;
331 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
332 (flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
,
334 if (status
== GLOB_NOMATCH
)
335 /* No matches in this directory. Try the next. */
345 /* Stick the directory on the front of each name. */
346 if (prefix_array (dirs
.gl_pathv
[i
],
347 &pglob
->gl_pathv
[oldcount
],
348 pglob
->gl_pathc
- oldcount
))
356 flags
|= GLOB_MAGCHAR
;
358 if (pglob
->gl_pathc
== oldcount
)
360 if (flags
& GLOB_NOCHECK
)
362 size_t len
= strlen (pattern
) + 1;
363 char *patcopy
= (char *) malloc (len
);
366 memcpy (patcopy
, pattern
, len
);
369 = (char **) realloc (pglob
->gl_pathv
,
371 ((flags
& GLOB_DOOFFS
) ?
372 pglob
->gl_offs
: 0) +
375 if (pglob
->gl_pathv
== NULL
)
381 if (flags
& GLOB_DOOFFS
)
382 while (pglob
->gl_pathc
< pglob
->gl_offs
)
383 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
385 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
386 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
387 pglob
->gl_flags
= flags
;
394 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
400 /* Stick the directory on the front of each name. */
401 if (prefix_array (dirname
,
402 &pglob
->gl_pathv
[oldcount
],
403 pglob
->gl_pathc
- oldcount
))
411 if (flags
& GLOB_MARK
)
413 /* Append slashes to directory names. */
416 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
417 if (__lstat (pglob
->gl_pathv
[i
], &st
) == 0 &&
418 S_ISDIR (st
.st_mode
))
420 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
421 char *new = realloc (pglob
->gl_pathv
[i
], len
);
427 strcpy (&new[len
- 2], "/");
428 pglob
->gl_pathv
[i
] = new;
432 if (!(flags
& GLOB_NOSORT
))
433 /* Sort the vector. */
434 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
435 pglob
->gl_pathc
- oldcount
,
436 sizeof (char *), collated_compare
);
442 /* Free storage allocated in PGLOB by a previous `glob' call. */
445 register glob_t
*pglob
;
447 if (pglob
->gl_pathv
!= NULL
)
450 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
451 if (pglob
->gl_pathv
[i
] != NULL
)
452 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
453 free ((__ptr_t
) pglob
->gl_pathv
);
458 /* Do a collated comparison of A and B. */
460 collated_compare (a
, b
)
464 const char *const s1
= *(const char *const * const) a
;
465 const char *const s2
= *(const char *const * const) b
;
473 return strcoll (s1
, s2
);
477 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
478 elements in place. Return nonzero if out of memory, zero if successful.
479 A slash is inserted between DIRNAME and each elt of ARRAY,
480 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
482 prefix_array (dirname
, array
, n
)
488 size_t dirlen
= strlen (dirname
);
490 if (dirlen
== 1 && dirname
[0] == '/')
491 /* DIRNAME is just "/", so normal prepending would get us "//foo".
492 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
495 for (i
= 0; i
< n
; ++i
)
497 size_t eltlen
= strlen (array
[i
]) + 1;
498 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
502 free ((__ptr_t
) array
[--i
]);
506 memcpy (new, dirname
, dirlen
);
508 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
509 free ((__ptr_t
) array
[i
]);
517 /* Return nonzero if PATTERN contains any metacharacters.
518 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
520 glob_pattern_p (pattern
, quote
)
524 register const char *p
;
527 for (p
= pattern
; *p
!= '\0'; ++p
)
553 /* Like `glob', but PATTERN is a final pathname component,
554 and matches are searched for in DIRECTORY.
555 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
556 The GLOB_APPEND flag is assumed to be set (always appends). */
558 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
560 const char *directory
;
562 int (*errfunc
) __P ((const char *, int));
569 struct globlink
*next
;
572 struct globlink
*names
= NULL
;
575 if (!glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
578 flags
|= GLOB_NOCHECK
;
582 flags
|= GLOB_MAGCHAR
;
584 stream
= (__glob_opendir_hook
? (*__glob_opendir_hook
) (directory
)
585 : (__ptr_t
) opendir (directory
));
588 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
)) ||
598 if (__glob_readdir_hook
)
600 name
= (*__glob_readdir_hook
) (stream
);
607 struct dirent
*d
= readdir ((DIR *) stream
);
610 if (! REAL_DIR_ENTRY (d
))
620 if (fnmatch (pattern
, name
,
621 (!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0) |
622 ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)) == 0)
625 = (struct globlink
*) __alloca (sizeof (struct globlink
));
629 = (char *) malloc (len
+ 1);
630 if (new->name
== NULL
)
632 memcpy ((__ptr_t
) new->name
, name
, len
);
633 new->name
[len
] = '\0';
641 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
643 size_t len
= strlen (pattern
);
645 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
647 names
->name
= (char *) malloc (len
+ 1);
648 if (names
->name
== NULL
)
650 memcpy (names
->name
, pattern
, len
);
651 names
->name
[len
] = '\0';
655 = (char **) realloc (pglob
->gl_pathv
,
657 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
660 if (pglob
->gl_pathv
== NULL
)
663 if (flags
& GLOB_DOOFFS
)
664 while (pglob
->gl_pathc
< pglob
->gl_offs
)
665 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
667 for (; names
!= NULL
; names
= names
->next
)
668 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
669 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
671 pglob
->gl_flags
= flags
;
676 if (__glob_closedir_hook
)
677 (*__glob_closedir_hook
) (stream
);
679 (void) closedir ((DIR *) stream
);
682 return nfound
== 0 ? GLOB_NOMATCH
: 0;
687 if (__glob_closedir_hook
)
688 (*__glob_closedir_hook
) (stream
);
690 (void) closedir ((DIR *) stream
);
693 while (names
!= NULL
)
695 if (names
->name
!= NULL
)
696 free ((__ptr_t
) names
->name
);
702 #endif /* _LIBC or not __GNU_LIBRARY__. */