1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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__)
27 /* Enable GNU extensions in glob.h. */
33 #include <sys/types.h>
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #define GLOB_INTERFACE_VERSION 1
46 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
47 #include <gnu-versions.h>
48 #if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
70 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
79 #if defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
81 #ifndef __GNU_LIBRARY__
82 #define D_NAMLEN(d) strlen((d)->d_name)
83 #else /* GNU C library. */
84 #define D_NAMLEN(d) ((d)->d_namlen)
85 #endif /* Not GNU C library. */
86 #else /* Not POSIX or HAVE_DIRENT_H. */
88 #define D_NAMLEN(d) ((d)->d_namlen)
89 #ifdef HAVE_SYS_NDIR_H
91 #endif /* HAVE_SYS_NDIR_H */
94 #endif /* HAVE_SYS_DIR_H */
97 #endif /* HAVE_NDIR_H */
98 #endif /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__. */
100 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
101 /* Posix does not require that the d_ino field be present, and some
102 systems do not provide it. */
103 #define REAL_DIR_ENTRY(dp) 1
105 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
108 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
112 #else /* No standard headers. */
114 extern char *getenv ();
126 extern char *malloc (), *realloc ();
129 extern void qsort ();
130 extern void abort (), exit ();
132 #endif /* Standard headers. */
137 extern void bzero ();
140 extern void bcopy ();
143 #define memcpy(d, s, n) bcopy ((s), (d), (n))
144 #define strrchr rindex
145 /* memset is only used for zero here, but let's be paranoid. */
146 #define memset(s, better_be_zero, n) \
147 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
148 #endif /* Not ANSI_STRING. */
151 #define strcoll strcmp
155 #ifndef __GNU_LIBRARY__
164 /* These casts are the for sake of the broken Ultrix compiler,
165 which warns of illegal pointer combinations otherwise. */
167 return (char *) malloc (n
);
168 return (char *) realloc (p
, n
);
170 #define realloc my_realloc
174 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
178 #define alloca(n) __builtin_alloca (n)
180 #if defined (sparc) || defined (HAVE_ALLOCA_H)
182 #else /* Not sparc or HAVE_ALLOCA_H. */
184 extern char *alloca ();
185 #endif /* Not _AIX. */
186 #endif /* sparc or HAVE_ALLOCA_H. */
189 #define __alloca alloca
193 #ifndef __GNU_LIBRARY__
195 #ifdef STAT_MACROS_BROKEN
199 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
205 #define size_t unsigned int
208 /* Some system header files erroneously define these.
209 We want our own definitions from <fnmatch.h> to take precedence. */
215 /* Some system header files erroneously define these.
216 We want our own definitions from <glob.h> to take precedence. */
227 static int glob_pattern_p
__P ((const char *pattern
, int quote
));
228 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
230 int (*errfunc
) __P ((const char *, int)),
232 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
233 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
235 /* Do glob searching for PATTERN, placing results in PGLOB.
236 The bits defined above may be set in FLAGS.
237 If a directory cannot be opened or read and ERRFUNC is not nil,
238 it is called with the pathname that caused the error, and the
239 `errno' value from the failing call; if it returns non-zero
240 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
241 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
242 Otherwise, `glob' returns zero. */
244 glob (pattern
, flags
, errfunc
, pglob
)
247 int (*errfunc
) __P ((const char *, int));
250 const char *filename
;
256 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
262 if (flags
& GLOB_BRACE
)
264 const char *begin
= strchr (pattern
, '{');
267 const char *end
= strchr (begin
+ 1, '}');
268 if (end
!= NULL
&& end
!= begin
+ 1)
270 size_t restlen
= strlen (end
+ 1) + 1;
271 const char *p
, *comma
;
275 if (!(flags
& GLOB_APPEND
))
278 pglob
->gl_pathv
= NULL
;
280 firstc
= pglob
->gl_pathc
;
281 for (p
= begin
+ 1;; p
= comma
+ 1)
284 comma
= strchr (p
, ',');
286 comma
= strchr (p
, '\0');
287 if ((begin
- pattern
) + (comma
- p
) + 1 > bufsz
)
289 if (bufsz
* 2 < comma
- p
+ 1)
292 bufsz
= comma
- p
+ 1;
293 buf
= __alloca (bufsz
);
295 memcpy (buf
, pattern
, begin
- pattern
);
296 memcpy (buf
+ (begin
- pattern
), p
, comma
- p
);
297 memcpy (buf
+ (begin
- pattern
) + (comma
- p
), end
, restlen
);
298 result
= glob (buf
, ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
)) |
299 GLOB_APPEND
), errfunc
, pglob
);
300 if (result
&& result
!= GLOB_NOMATCH
)
305 if (pglob
->gl_pathc
== firstc
&&
306 !(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
312 /* Find the filename. */
313 filename
= strrchr (pattern
, '/');
314 if (filename
== NULL
)
317 dirname
= (char *) ".";
320 else if (filename
== pattern
)
323 dirname
= (char *) "/";
329 dirlen
= filename
- pattern
;
330 dirname
= (char *) __alloca (dirlen
+ 1);
331 memcpy (dirname
, pattern
, dirlen
);
332 dirname
[dirlen
] = '\0';
336 if (filename
[0] == '\0' && dirlen
> 1)
337 /* "pattern/". Expand "pattern", appending slashes. */
339 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
341 pglob
->gl_flags
= (pglob
->gl_flags
& ~GLOB_MARK
) | (flags
& GLOB_MARK
);
345 if (!(flags
& GLOB_APPEND
))
348 pglob
->gl_pathv
= NULL
;
351 oldcount
= pglob
->gl_pathc
;
353 if ((flags
& GLOB_TILDE
) && dirname
[0] == '~')
355 if (dirname
[1] == '\0')
357 /* Look up home directory. */
358 dirname
= getenv ("HOME");
359 if (dirname
== NULL
|| dirname
[0] == '\0')
361 extern char *getlogin
__P ((void));
362 char *name
= getlogin ();
365 struct passwd
*p
= getpwnam (name
);
370 if (dirname
== NULL
|| dirname
[0] == '\0')
371 dirname
= (char *) "~"; /* No luck. */
375 /* Look up specific user's home directory. */
376 struct passwd
*p
= getpwnam (dirname
+ 1);
382 if (glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
384 /* The directory name contains metacharacters, so we
385 have to glob for the directory, and then glob for
386 the pattern in each directory found. */
390 status
= glob (dirname
,
391 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
)) |
397 /* We have successfully globbed the preceding directory name.
398 For each name we found, call glob_in_dir on it and FILENAME,
399 appending the results to PGLOB. */
400 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
406 /* Make globbing interruptible in the bash shell. */
407 extern int interrupt_state
;
418 oldcount
= pglob
->gl_pathc
;
419 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
420 (flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
,
422 if (status
== GLOB_NOMATCH
)
423 /* No matches in this directory. Try the next. */
433 /* Stick the directory on the front of each name. */
434 if (prefix_array (dirs
.gl_pathv
[i
],
435 &pglob
->gl_pathv
[oldcount
],
436 pglob
->gl_pathc
- oldcount
))
444 flags
|= GLOB_MAGCHAR
;
446 if (pglob
->gl_pathc
== oldcount
)
448 if (flags
& GLOB_NOCHECK
)
450 size_t len
= strlen (pattern
) + 1;
451 char *patcopy
= (char *) malloc (len
);
454 memcpy (patcopy
, pattern
, len
);
457 = (char **) realloc (pglob
->gl_pathv
,
459 ((flags
& GLOB_DOOFFS
) ?
460 pglob
->gl_offs
: 0) +
463 if (pglob
->gl_pathv
== NULL
)
469 if (flags
& GLOB_DOOFFS
)
470 while (pglob
->gl_pathc
< pglob
->gl_offs
)
471 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
473 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
474 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
475 pglob
->gl_flags
= flags
;
482 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
488 /* Stick the directory on the front of each name. */
489 if (prefix_array (dirname
,
490 &pglob
->gl_pathv
[oldcount
],
491 pglob
->gl_pathc
- oldcount
))
499 if (flags
& GLOB_MARK
)
501 /* Append slashes to directory names. */
504 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
505 if (((flags
& GLOB_ALTDIRFUNC
) ?
506 (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) :
507 __stat (pglob
->gl_pathv
[i
], &st
)) == 0 &&
508 S_ISDIR (st
.st_mode
))
510 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
511 char *new = realloc (pglob
->gl_pathv
[i
], len
);
517 strcpy (&new[len
- 2], "/");
518 pglob
->gl_pathv
[i
] = new;
522 if (!(flags
& GLOB_NOSORT
))
523 /* Sort the vector. */
524 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
525 pglob
->gl_pathc
- oldcount
,
526 sizeof (char *), collated_compare
);
532 /* Free storage allocated in PGLOB by a previous `glob' call. */
535 register glob_t
*pglob
;
537 if (pglob
->gl_pathv
!= NULL
)
540 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
541 if (pglob
->gl_pathv
[i
] != NULL
)
542 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
543 free ((__ptr_t
) pglob
->gl_pathv
);
548 /* Do a collated comparison of A and B. */
550 collated_compare (a
, b
)
554 const char *const s1
= *(const char *const * const) a
;
555 const char *const s2
= *(const char *const * const) b
;
563 return strcoll (s1
, s2
);
567 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
568 elements in place. Return nonzero if out of memory, zero if successful.
569 A slash is inserted between DIRNAME and each elt of ARRAY,
570 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
572 prefix_array (dirname
, array
, n
)
578 size_t dirlen
= strlen (dirname
);
580 if (dirlen
== 1 && dirname
[0] == '/')
581 /* DIRNAME is just "/", so normal prepending would get us "//foo".
582 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
585 for (i
= 0; i
< n
; ++i
)
587 size_t eltlen
= strlen (array
[i
]) + 1;
588 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
592 free ((__ptr_t
) array
[--i
]);
596 memcpy (new, dirname
, dirlen
);
598 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
599 free ((__ptr_t
) array
[i
]);
607 /* Return nonzero if PATTERN contains any metacharacters.
608 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
610 glob_pattern_p (pattern
, quote
)
614 register const char *p
;
617 for (p
= pattern
; *p
!= '\0'; ++p
)
643 /* Like `glob', but PATTERN is a final pathname component,
644 and matches are searched for in DIRECTORY.
645 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
646 The GLOB_APPEND flag is assumed to be set (always appends). */
648 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
650 const char *directory
;
652 int (*errfunc
) __P ((const char *, int));
659 struct globlink
*next
;
662 struct globlink
*names
= NULL
;
665 if (!glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
668 flags
|= GLOB_NOCHECK
;
672 flags
|= GLOB_MAGCHAR
;
674 stream
= ((flags
& GLOB_ALTDIRFUNC
) ?
675 (*pglob
->gl_opendir
) (directory
) :
676 (__ptr_t
) opendir (directory
));
679 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
)) ||
688 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
) ?
689 (*pglob
->gl_readdir
) (stream
) :
690 readdir ((DIR *) stream
));
693 if (! REAL_DIR_ENTRY (d
))
702 if (fnmatch (pattern
, name
,
703 (!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0) |
704 ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)) == 0)
707 = (struct globlink
*) __alloca (sizeof (struct globlink
));
711 = (char *) malloc (len
+ 1);
712 if (new->name
== NULL
)
714 memcpy ((__ptr_t
) new->name
, name
, len
);
715 new->name
[len
] = '\0';
723 if (nfound
== 0 && (flags
& GLOB_NOMAGIC
) &&
724 ! glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
725 flags
|= GLOB_NOCHECK
;
727 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
729 size_t len
= strlen (pattern
);
731 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
733 names
->name
= (char *) malloc (len
+ 1);
734 if (names
->name
== NULL
)
736 memcpy (names
->name
, pattern
, len
);
737 names
->name
[len
] = '\0';
741 = (char **) realloc (pglob
->gl_pathv
,
743 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
746 if (pglob
->gl_pathv
== NULL
)
749 if (flags
& GLOB_DOOFFS
)
750 while (pglob
->gl_pathc
< pglob
->gl_offs
)
751 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
753 for (; names
!= NULL
; names
= names
->next
)
754 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
755 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
757 pglob
->gl_flags
= flags
;
762 if (flags
& GLOB_ALTDIRFUNC
)
763 (*pglob
->gl_closedir
) (stream
);
765 closedir ((DIR *) stream
);
768 return nfound
== 0 ? GLOB_NOMATCH
: 0;
773 if (flags
& GLOB_ALTDIRFUNC
)
774 (*pglob
->gl_closedir
) (stream
);
776 closedir ((DIR *) stream
);
779 while (names
!= NULL
)
781 if (names
->name
!= NULL
)
782 free ((__ptr_t
) names
->name
);
788 #endif /* Not ELIDE_CODE. */