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 not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, 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
55 #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
59 #if defined HAVE_UNISTD_H || defined _LIBC
68 #if !defined (_AMIGA) && !defined (VMS) && !defined(WIN32)
72 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
76 #define __set_errno(val) errno = (val)
84 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
86 # define NAMLEN(dirent) strlen((dirent)->d_name)
88 # define dirent direct
89 # define NAMLEN(dirent) (dirent)->d_namlen
90 # ifdef HAVE_SYS_NDIR_H
91 # include <sys/ndir.h>
93 # ifdef HAVE_SYS_DIR_H
101 # endif /* HAVE_VMSDIR_H */
105 /* In GNU systems, <dirent.h> defines this macro for us. */
108 #define NAMLEN(d) _D_NAMLEN(d)
112 #if (defined (POSIX) || defined (WIN32)) && !defined (__GNU_LIBRARY__)
113 /* Posix does not require that the d_ino field be present, and some
114 systems do not provide it. */
115 #define REAL_DIR_ENTRY(dp) 1
117 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
120 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
124 #else /* No standard headers. */
126 extern char *getenv ();
138 extern char *malloc (), *realloc ();
141 extern void qsort ();
142 extern void abort (), exit ();
144 #endif /* Standard headers. */
149 extern void bzero ();
152 extern void bcopy ();
155 #define memcpy(d, s, n) bcopy ((s), (d), (n))
156 #define strrchr rindex
157 /* memset is only used for zero here, but let's be paranoid. */
158 #define memset(s, better_be_zero, n) \
159 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
160 #endif /* Not ANSI_STRING. */
163 #define strcoll strcmp
167 #ifndef __GNU_LIBRARY__
181 /* These casts are the for sake of the broken Ultrix compiler,
182 which warns of illegal pointer combinations otherwise. */
184 return (char *) malloc (n
);
185 return (char *) realloc (p
, n
);
187 #define realloc my_realloc
189 #endif /* __GNU_LIBRARY__ */
192 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
196 #define alloca(n) __builtin_alloca (n)
200 #else /* Not HAVE_ALLOCA_H. */
205 extern char *alloca ();
207 #endif /* Not _AIX. */
208 #endif /* sparc or HAVE_ALLOCA_H. */
211 #define __alloca alloca
215 #ifndef __GNU_LIBRARY__
217 #ifdef STAT_MACROS_BROKEN
221 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
225 #if !(defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
227 #define size_t unsigned int
230 /* Some system header files erroneously define these.
231 We want our own definitions from <fnmatch.h> to take precedence. */
237 /* Some system header files erroneously define these.
238 We want our own definitions from <glob.h> to take precedence. */
249 static int glob_pattern_p
__P ((const char *pattern
, int quote
));
250 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
252 int (*errfunc
) __P ((const char *, int)),
254 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
255 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
257 /* Do glob searching for PATTERN, placing results in PGLOB.
258 The bits defined above may be set in FLAGS.
259 If a directory cannot be opened or read and ERRFUNC is not nil,
260 it is called with the pathname that caused the error, and the
261 `errno' value from the failing call; if it returns non-zero
262 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
263 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
264 Otherwise, `glob' returns zero. */
266 glob (pattern
, flags
, errfunc
, pglob
)
269 int (*errfunc
) __P ((const char *, int));
272 const char *filename
;
278 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
280 __set_errno (EINVAL
);
284 if (flags
& GLOB_BRACE
)
286 const char *begin
= strchr (pattern
, '{');
291 const char *p
, *end
, *next
;
292 unsigned int depth
= 0;
294 /* Find the end of the brace expression, by counting braces.
295 While we're at it, notice the first comma at top brace level. */
303 if (depth
== 0 && next
== NULL
)
314 return glob (pattern
, flags
&~ GLOB_BRACE
, errfunc
, pglob
);
318 restlen
= strlen (end
) + 1;
322 /* We have a brace expression. BEGIN points to the opening {,
323 NEXT points past the terminator of the first element, and END
324 points past the final }. We will accumulate result names from
325 recursive runs for each brace alternative in the buffer using
328 if (!(flags
& GLOB_APPEND
))
330 /* This call is to set a new vector, so clear out the
331 vector so we can append to it. */
333 pglob
->gl_pathv
= NULL
;
335 firstc
= pglob
->gl_pathc
;
337 /* In this loop P points to the beginning of the current element
338 and NEXT points past its terminator. */
342 /* Construct a whole name that is one of the brace
343 alternatives in a temporary buffer. */
345 size_t bufsz
= (begin
- pattern
) + (next
- 1 - p
) + restlen
;
349 char *onealt
= malloc (bufsz
);
352 if (!(flags
& GLOB_APPEND
))
357 memcpy (onealt
, pattern
, begin
- pattern
);
358 memcpy (&onealt
[begin
- pattern
], p
, next
- 1 - p
);
359 memcpy (&onealt
[(begin
- pattern
) + (next
- 1 - p
)],
361 result
= glob (onealt
,
362 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
)) |
363 GLOB_APPEND
), errfunc
, pglob
);
368 /* If we got an error, return it. */
369 if (result
&& result
!= GLOB_NOMATCH
)
371 if (!(flags
& GLOB_APPEND
))
376 /* Advance past this alternative and process the next. */
385 /* Found the next alternative. Loop to glob it. */
395 /* End of the brace expression. Break out of the loop. */
401 if (pglob
->gl_pathc
== firstc
&&
402 !(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
407 /* Find the filename. */
408 filename
= strrchr (pattern
, '/');
409 if (filename
== NULL
)
413 dirname
= (char *) "";
415 dirname
= (char *) ".";
419 else if (filename
== pattern
)
422 dirname
= (char *) "/";
428 dirlen
= filename
- pattern
;
429 dirname
= (char *) __alloca (dirlen
+ 1);
430 memcpy (dirname
, pattern
, dirlen
);
431 dirname
[dirlen
] = '\0';
435 if (filename
[0] == '\0' && dirlen
> 1)
436 /* "pattern/". Expand "pattern", appending slashes. */
438 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
440 pglob
->gl_flags
= (pglob
->gl_flags
& ~GLOB_MARK
) | (flags
& GLOB_MARK
);
444 if (!(flags
& GLOB_APPEND
))
447 pglob
->gl_pathv
= NULL
;
450 oldcount
= pglob
->gl_pathc
;
453 if ((flags
& GLOB_TILDE
) && dirname
[0] == '~')
455 if (dirname
[1] == '\0')
457 /* Look up home directory. */
458 dirname
= getenv ("HOME");
460 if (dirname
== NULL
|| dirname
[0] == '\0')
464 if (dirname
== NULL
|| dirname
[0] == '\0')
465 dirname
= "c:/users/default"; /* poor default */
467 if (dirname
== NULL
|| dirname
[0] == '\0')
469 extern char *getlogin
__P ((void));
470 extern int getlogin_r
__P ((char *, size_t));
473 #if defined HAVE_GETLOGIN_R || defined _LIBC
474 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
478 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
481 name
= __alloca (buflen
);
483 success
= getlogin_r (name
, buflen
) >= 0;
486 success
= (name
= getlogin ()) != NULL
;
490 #if defined HAVE_GETPWNAM_R || defined _LIBC
491 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
493 struct passwd pwbuf
, *p
;
495 pwtmpbuf
= __alloca (pwbuflen
);
497 success
= (__getpwnam_r (name
, &pwbuf
, pwtmpbuf
,
500 struct passwd
*p
= getpwnam (name
);
507 if (dirname
== NULL
|| dirname
[0] == '\0')
508 dirname
= (char *) "~"; /* No luck. */
515 if (dirname
== NULL
|| dirname
[0] == '\0')
519 if (dirname
== NULL
|| dirname
[0] == '\0')
520 dirname
= "c:/users/default"; /* poor default */
522 /* Look up specific user's home directory. */
523 #if defined HAVE_GETPWNAM_R || defined _LIBC
524 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
525 char *pwtmpbuf
= __alloca (buflen
);
526 struct passwd pwbuf
, *p
;
527 if (__getpwnam_r (dirname
+ 1, &pwbuf
, pwtmpbuf
, buflen
, &p
) >= 0)
530 struct passwd
*p
= getpwnam (dirname
+ 1);
538 #endif /* Not VMS. */
540 if (glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
542 /* The directory name contains metacharacters, so we
543 have to glob for the directory, and then glob for
544 the pattern in each directory found. */
548 status
= glob (dirname
,
549 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
)) |
555 /* We have successfully globbed the preceding directory name.
556 For each name we found, call glob_in_dir on it and FILENAME,
557 appending the results to PGLOB. */
558 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
564 /* Make globbing interruptible in the bash shell. */
565 extern int interrupt_state
;
576 oldcount
= pglob
->gl_pathc
;
577 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
578 (flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
,
580 if (status
== GLOB_NOMATCH
)
581 /* No matches in this directory. Try the next. */
591 /* Stick the directory on the front of each name. */
592 if (prefix_array (dirs
.gl_pathv
[i
],
593 &pglob
->gl_pathv
[oldcount
],
594 pglob
->gl_pathc
- oldcount
))
602 flags
|= GLOB_MAGCHAR
;
604 if (pglob
->gl_pathc
== oldcount
)
606 if (flags
& GLOB_NOCHECK
)
608 size_t len
= strlen (pattern
) + 1;
609 char *patcopy
= (char *) malloc (len
);
612 memcpy (patcopy
, pattern
, len
);
615 = (char **) realloc (pglob
->gl_pathv
,
617 ((flags
& GLOB_DOOFFS
) ?
618 pglob
->gl_offs
: 0) +
621 if (pglob
->gl_pathv
== NULL
)
627 if (flags
& GLOB_DOOFFS
)
628 while (pglob
->gl_pathc
< pglob
->gl_offs
)
629 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
631 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
632 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
633 pglob
->gl_flags
= flags
;
640 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
646 /* Stick the directory on the front of each name. */
647 if (prefix_array (dirname
,
648 &pglob
->gl_pathv
[oldcount
],
649 pglob
->gl_pathc
- oldcount
))
657 if (flags
& GLOB_MARK
)
659 /* Append slashes to directory names. */
662 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
663 if (((flags
& GLOB_ALTDIRFUNC
) ?
664 (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) :
665 __stat (pglob
->gl_pathv
[i
], &st
)) == 0 &&
666 S_ISDIR (st
.st_mode
))
668 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
669 char *new = realloc (pglob
->gl_pathv
[i
], len
);
675 strcpy (&new[len
- 2], "/");
676 pglob
->gl_pathv
[i
] = new;
680 if (!(flags
& GLOB_NOSORT
))
681 /* Sort the vector. */
682 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
683 pglob
->gl_pathc
- oldcount
,
684 sizeof (char *), collated_compare
);
690 /* Free storage allocated in PGLOB by a previous `glob' call. */
693 register glob_t
*pglob
;
695 if (pglob
->gl_pathv
!= NULL
)
698 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
699 if (pglob
->gl_pathv
[i
] != NULL
)
700 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
701 free ((__ptr_t
) pglob
->gl_pathv
);
706 /* Do a collated comparison of A and B. */
708 collated_compare (a
, b
)
712 const char *const s1
= *(const char *const * const) a
;
713 const char *const s2
= *(const char *const * const) b
;
721 return strcoll (s1
, s2
);
725 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
726 elements in place. Return nonzero if out of memory, zero if successful.
727 A slash is inserted between DIRNAME and each elt of ARRAY,
728 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
730 prefix_array (dirname
, array
, n
)
736 size_t dirlen
= strlen (dirname
);
738 if (dirlen
== 1 && dirname
[0] == '/')
739 /* DIRNAME is just "/", so normal prepending would get us "//foo".
740 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
743 for (i
= 0; i
< n
; ++i
)
745 size_t eltlen
= strlen (array
[i
]) + 1;
746 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
750 free ((__ptr_t
) array
[--i
]);
754 memcpy (new, dirname
, dirlen
);
756 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
757 free ((__ptr_t
) array
[i
]);
765 /* Return nonzero if PATTERN contains any metacharacters.
766 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
768 glob_pattern_p (pattern
, quote
)
772 register const char *p
;
775 for (p
= pattern
; *p
!= '\0'; ++p
)
783 if (quote
&& p
[1] != '\0')
801 /* Like `glob', but PATTERN is a final pathname component,
802 and matches are searched for in DIRECTORY.
803 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
804 The GLOB_APPEND flag is assumed to be set (always appends). */
806 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
808 const char *directory
;
810 int (*errfunc
) __P ((const char *, int));
817 struct globlink
*next
;
820 struct globlink
*names
= NULL
;
823 if (!glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
826 flags
|= GLOB_NOCHECK
;
830 flags
|= GLOB_MAGCHAR
;
832 stream
= ((flags
& GLOB_ALTDIRFUNC
) ?
833 (*pglob
->gl_opendir
) (directory
) :
834 (__ptr_t
) opendir (directory
));
837 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
)) ||
846 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
) ?
847 (*pglob
->gl_readdir
) (stream
) :
848 readdir ((DIR *) stream
));
851 if (! REAL_DIR_ENTRY (d
))
856 if (fnmatch (pattern
, name
,
857 (!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0) |
858 ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
865 = (struct globlink
*) __alloca (sizeof (struct globlink
));
868 = (char *) malloc (len
+ 1);
869 if (new->name
== NULL
)
871 memcpy ((__ptr_t
) new->name
, name
, len
);
872 new->name
[len
] = '\0';
880 if (nfound
== 0 && (flags
& GLOB_NOMAGIC
) &&
881 ! glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
882 flags
|= GLOB_NOCHECK
;
884 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
886 size_t len
= strlen (pattern
);
888 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
890 names
->name
= (char *) malloc (len
+ 1);
891 if (names
->name
== NULL
)
893 memcpy (names
->name
, pattern
, len
);
894 names
->name
[len
] = '\0';
898 = (char **) realloc (pglob
->gl_pathv
,
900 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
903 if (pglob
->gl_pathv
== NULL
)
906 if (flags
& GLOB_DOOFFS
)
907 while (pglob
->gl_pathc
< pglob
->gl_offs
)
908 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
910 for (; names
!= NULL
; names
= names
->next
)
911 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
912 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
914 pglob
->gl_flags
= flags
;
919 if (flags
& GLOB_ALTDIRFUNC
)
920 (*pglob
->gl_closedir
) (stream
);
922 closedir ((DIR *) stream
);
925 return nfound
== 0 ? GLOB_NOMATCH
: 0;
930 if (flags
& GLOB_ALTDIRFUNC
)
931 (*pglob
->gl_closedir
) (stream
);
933 closedir ((DIR *) stream
);
936 while (names
!= NULL
)
938 if (names
->name
!= NULL
)
939 free ((__ptr_t
) names
->name
);
945 #endif /* Not ELIDE_CODE. */