1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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>
36 /* Outcomment the following line for production quality code. */
37 /* #define NDEBUG 1 */
41 /* Comment out all this code if we are using the GNU C Library, and are not
42 actually compiling the library itself. This code is part of the GNU C
43 Library, but also included in many other GNU distributions. Compiling
44 and linking in this code is a waste when using the GNU C library
45 (especially if it is a shared library). Rather than having every GNU
46 program understand `configure --with-gnu-libc' and omit the object files,
47 it is simpler to just do this in the source for each such file. */
49 #define GLOB_INTERFACE_VERSION 1
50 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
51 #include <gnu-versions.h>
52 #if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
59 #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
63 #if defined HAVE_UNISTD_H || defined _LIBC
72 #if !defined (_AMIGA) && !defined (VMS) && !defined(WIN32)
76 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
80 #define __set_errno(val) errno = (val)
88 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
90 # define NAMLEN(dirent) strlen((dirent)->d_name)
92 # define dirent direct
93 # define NAMLEN(dirent) (dirent)->d_namlen
94 # ifdef HAVE_SYS_NDIR_H
95 # include <sys/ndir.h>
97 # ifdef HAVE_SYS_DIR_H
103 # ifdef HAVE_VMSDIR_H
105 # endif /* HAVE_VMSDIR_H */
109 /* In GNU systems, <dirent.h> defines this macro for us. */
112 #define NAMLEN(d) _D_NAMLEN(d)
116 #if (defined (POSIX) || defined (WIN32)) && !defined (__GNU_LIBRARY__)
117 /* Posix does not require that the d_ino field be present, and some
118 systems do not provide it. */
119 #define REAL_DIR_ENTRY(dp) 1
121 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
124 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
128 #else /* No standard headers. */
130 extern char *getenv ();
142 extern char *malloc (), *realloc ();
145 extern void qsort ();
146 extern void abort (), exit ();
148 #endif /* Standard headers. */
153 extern void bzero ();
156 extern void bcopy ();
159 #define memcpy(d, s, n) bcopy ((s), (d), (n))
160 #define strrchr rindex
161 /* memset is only used for zero here, but let's be paranoid. */
162 #define memset(s, better_be_zero, n) \
163 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
164 #endif /* Not ANSI_STRING. */
166 #if !defined HAVE_STRCOLL && !defined _LIBC
167 #define strcoll strcmp
171 #ifndef __GNU_LIBRARY__
185 /* These casts are the for sake of the broken Ultrix compiler,
186 which warns of illegal pointer combinations otherwise. */
188 return (char *) malloc (n
);
189 return (char *) realloc (p
, n
);
191 #define realloc my_realloc
193 #endif /* __GNU_LIBRARY__ */
196 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
200 #define alloca(n) __builtin_alloca (n)
204 #else /* Not HAVE_ALLOCA_H. */
209 extern char *alloca ();
211 #endif /* Not _AIX. */
212 #endif /* sparc or HAVE_ALLOCA_H. */
215 #define __alloca alloca
219 #ifndef __GNU_LIBRARY__
221 #ifdef STAT_MACROS_BROKEN
225 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
229 #if !(defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
231 #define size_t unsigned int
234 /* Some system header files erroneously define these.
235 We want our own definitions from <fnmatch.h> to take precedence. */
241 /* Some system header files erroneously define these.
242 We want our own definitions from <glob.h> to take precedence. */
253 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
255 int (*errfunc
) __P ((const char *, int)),
257 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
258 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
261 /* Find the end of the sub-pattern in a brace expression. We define
262 this as an inline function if the compiler permits. */
264 #if __GNUC__ - 0 >= 2
268 next_brace_sub (const char *begin
)
270 unsigned int depth
= 0;
271 const char *cp
= begin
;
277 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
287 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
294 /* An incorrectly terminated brace expression. */
305 /* Do glob searching for PATTERN, placing results in PGLOB.
306 The bits defined above may be set in FLAGS.
307 If a directory cannot be opened or read and ERRFUNC is not nil,
308 it is called with the pathname that caused the error, and the
309 `errno' value from the failing call; if it returns non-zero
310 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
311 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
312 Otherwise, `glob' returns zero. */
314 glob (pattern
, flags
, errfunc
, pglob
)
317 int (*errfunc
) __P ((const char *, int));
320 const char *filename
;
326 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
328 __set_errno (EINVAL
);
332 if (flags
& GLOB_BRACE
)
334 const char *begin
= strchr (pattern
, '{');
337 /* Allocate working buffer large enough for our work. Note that
338 we have at least an opening and closing brace. */
346 char onealt
[strlen (pattern
) - 1];
348 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
351 if (!(flags
& GLOB_APPEND
))
357 /* We know the prefix for all sub-patterns. */
358 memcpy (onealt
, pattern
, begin
- pattern
);
359 alt_start
= &onealt
[begin
- pattern
];
361 /* Find the first sub-pattern and at the same time find the
362 rest after the closing brace. */
363 next
= next_brace_sub (begin
+ 1);
366 /* It is an illegal expression. */
370 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
373 /* Now find the end of the whole brace expression. */
377 rest
= next_brace_sub (rest
+ 1);
380 /* It is an illegal expression. */
384 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
387 /* Please note that we now can be sure the brace expression
389 rest_len
= strlen (++rest
) + 1;
391 /* We have a brace expression. BEGIN points to the opening {,
392 NEXT points past the terminator of the first element, and END
393 points past the final }. We will accumulate result names from
394 recursive runs for each brace alternative in the buffer using
397 if (!(flags
& GLOB_APPEND
))
399 /* This call is to set a new vector, so clear out the
400 vector so we can append to it. */
402 pglob
->gl_pathv
= NULL
;
404 firstc
= pglob
->gl_pathc
;
411 /* Construct the new glob expression. */
412 memcpy (alt_start
, p
, next
- p
);
413 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
415 result
= glob (onealt
,
416 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
417 | GLOB_APPEND
), errfunc
, pglob
);
419 /* If we got an error, return it. */
420 if (result
&& result
!= GLOB_NOMATCH
)
425 if (!(flags
& GLOB_APPEND
))
431 /* We saw the last entry. */
435 next
= next_brace_sub (p
);
436 assert (next
!= NULL
);
443 if (pglob
->gl_pathc
!= firstc
)
444 /* We found some entries. */
446 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
451 /* Find the filename. */
452 filename
= strrchr (pattern
, '/');
453 if (filename
== NULL
)
457 dirname
= (char *) "";
459 dirname
= (char *) ".";
463 else if (filename
== pattern
)
466 dirname
= (char *) "/";
472 dirlen
= filename
- pattern
;
473 dirname
= (char *) __alloca (dirlen
+ 1);
474 memcpy (dirname
, pattern
, dirlen
);
475 dirname
[dirlen
] = '\0';
479 if (filename
[0] == '\0' && dirlen
> 1)
480 /* "pattern/". Expand "pattern", appending slashes. */
482 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
484 pglob
->gl_flags
= (pglob
->gl_flags
& ~GLOB_MARK
) | (flags
& GLOB_MARK
);
488 if (!(flags
& GLOB_APPEND
))
491 pglob
->gl_pathv
= NULL
;
494 oldcount
= pglob
->gl_pathc
;
497 if ((flags
& GLOB_TILDE
) && dirname
[0] == '~')
499 if (dirname
[1] == '\0' || dirname
[1] == '/')
501 /* Look up home directory. */
502 char *home_dir
= getenv ("HOME");
504 if (home_dir
== NULL
|| home_dir
[0] == '\0')
508 if (home_dir
== NULL
|| home_dir
[0] == '\0')
509 home_dir
= "c:/users/default"; /* poor default */
511 if (home_dir
== NULL
|| home_dir
[0] == '\0')
513 extern char *getlogin
__P ((void));
514 extern int getlogin_r
__P ((char *, size_t));
517 #if defined HAVE_GETLOGIN_R || defined _LIBC
518 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
522 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
525 name
= __alloca (buflen
);
527 success
= getlogin_r (name
, buflen
) >= 0;
530 success
= (name
= getlogin ()) != NULL
;
534 #if defined HAVE_GETPWNAM_R || defined _LIBC
535 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
537 struct passwd pwbuf
, *p
;
539 pwtmpbuf
= __alloca (pwbuflen
);
541 success
= (__getpwnam_r (name
, &pwbuf
, pwtmpbuf
,
544 struct passwd
*p
= getpwnam (name
);
548 home_dir
= p
->pw_dir
;
551 if (home_dir
== NULL
|| home_dir
[0] == '\0')
552 home_dir
= (char *) "~"; /* No luck. */
555 /* Now construct the full directory. */
556 if (dirname
[1] == '\0')
561 size_t home_len
= strlen (home_dir
);
562 newp
= __alloca (home_len
+ dirlen
);
563 memcpy (newp
, home_dir
, home_len
);
564 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
568 #if !defined _AMIGA && !defined WIN32
571 char *end_name
= strchr (dirname
, '/');
575 if (end_name
== NULL
)
576 user_name
= dirname
+ 1;
579 user_name
= __alloca (end_name
- dirname
);
580 memcpy (user_name
, dirname
+ 1, end_name
- dirname
);
581 user_name
[end_name
- dirname
- 1] = '\0';
584 /* Look up specific user's home directory. */
586 #if defined HAVE_GETPWNAM_R || defined _LIBC
587 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
588 char *pwtmpbuf
= __alloca (buflen
);
589 struct passwd pwbuf
, *p
;
590 if (__getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) >= 0)
591 home_dir
= p
->pw_dir
;
595 struct passwd
*p
= getpwnam (user_name
);
597 home_dir
= p
->pw_dir
;
602 /* If we found a home directory use this. */
603 if (home_dir
!= NULL
)
606 size_t home_len
= strlen (home_dir
);
607 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
608 newp
= __alloca (home_len
+ rest_len
+ 1);
609 memcpy (newp
, home_dir
, home_len
);
610 memcpy (&newp
[home_len
], end_name
, rest_len
);
611 newp
[home_len
+ rest_len
] = '\0';
615 #endif /* Not Amiga && not Win32. */
617 #endif /* Not VMS. */
619 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
621 /* The directory name contains metacharacters, so we
622 have to glob for the directory, and then glob for
623 the pattern in each directory found. */
627 status
= glob (dirname
,
628 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
)) |
634 /* We have successfully globbed the preceding directory name.
635 For each name we found, call glob_in_dir on it and FILENAME,
636 appending the results to PGLOB. */
637 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
643 /* Make globbing interruptible in the bash shell. */
644 extern int interrupt_state
;
655 oldcount
= pglob
->gl_pathc
;
656 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
657 (flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
,
659 if (status
== GLOB_NOMATCH
)
660 /* No matches in this directory. Try the next. */
670 /* Stick the directory on the front of each name. */
671 if (prefix_array (dirs
.gl_pathv
[i
],
672 &pglob
->gl_pathv
[oldcount
],
673 pglob
->gl_pathc
- oldcount
))
681 flags
|= GLOB_MAGCHAR
;
683 if (pglob
->gl_pathc
== oldcount
)
685 if (flags
& GLOB_NOCHECK
)
687 size_t len
= strlen (pattern
) + 1;
688 char *patcopy
= (char *) malloc (len
);
691 memcpy (patcopy
, pattern
, len
);
694 = (char **) realloc (pglob
->gl_pathv
,
696 ((flags
& GLOB_DOOFFS
) ?
697 pglob
->gl_offs
: 0) +
700 if (pglob
->gl_pathv
== NULL
)
706 if (flags
& GLOB_DOOFFS
)
707 while (pglob
->gl_pathc
< pglob
->gl_offs
)
708 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
710 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
711 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
712 pglob
->gl_flags
= flags
;
719 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
725 /* Stick the directory on the front of each name. */
726 if (prefix_array (dirname
,
727 &pglob
->gl_pathv
[oldcount
],
728 pglob
->gl_pathc
- oldcount
))
736 if (flags
& GLOB_MARK
)
738 /* Append slashes to directory names. */
741 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
742 if (((flags
& GLOB_ALTDIRFUNC
) ?
743 (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) :
744 __stat (pglob
->gl_pathv
[i
], &st
)) == 0 &&
745 S_ISDIR (st
.st_mode
))
747 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
748 char *new = realloc (pglob
->gl_pathv
[i
], len
);
754 strcpy (&new[len
- 2], "/");
755 pglob
->gl_pathv
[i
] = new;
759 if (!(flags
& GLOB_NOSORT
))
760 /* Sort the vector. */
761 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
762 pglob
->gl_pathc
- oldcount
,
763 sizeof (char *), collated_compare
);
769 /* Free storage allocated in PGLOB by a previous `glob' call. */
772 register glob_t
*pglob
;
774 if (pglob
->gl_pathv
!= NULL
)
777 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
778 if (pglob
->gl_pathv
[i
] != NULL
)
779 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
780 free ((__ptr_t
) pglob
->gl_pathv
);
785 /* Do a collated comparison of A and B. */
787 collated_compare (a
, b
)
791 const char *const s1
= *(const char *const * const) a
;
792 const char *const s2
= *(const char *const * const) b
;
800 return strcoll (s1
, s2
);
804 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
805 elements in place. Return nonzero if out of memory, zero if successful.
806 A slash is inserted between DIRNAME and each elt of ARRAY,
807 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
809 prefix_array (dirname
, array
, n
)
815 size_t dirlen
= strlen (dirname
);
817 if (dirlen
== 1 && dirname
[0] == '/')
818 /* DIRNAME is just "/", so normal prepending would get us "//foo".
819 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
822 for (i
= 0; i
< n
; ++i
)
824 size_t eltlen
= strlen (array
[i
]) + 1;
825 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
829 free ((__ptr_t
) array
[--i
]);
833 memcpy (new, dirname
, dirlen
);
835 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
836 free ((__ptr_t
) array
[i
]);
844 /* Return nonzero if PATTERN contains any metacharacters.
845 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
847 __glob_pattern_p (pattern
, quote
)
851 register const char *p
;
854 for (p
= pattern
; *p
!= '\0'; ++p
)
862 if (quote
&& p
[1] != '\0')
879 weak_alias (__glob_pattern_p
, glob_pattern_p
)
883 /* Like `glob', but PATTERN is a final pathname component,
884 and matches are searched for in DIRECTORY.
885 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
886 The GLOB_APPEND flag is assumed to be set (always appends). */
888 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
890 const char *directory
;
892 int (*errfunc
) __P ((const char *, int));
899 struct globlink
*next
;
902 struct globlink
*names
= NULL
;
905 if (!__glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
908 flags
|= GLOB_NOCHECK
;
912 flags
|= GLOB_MAGCHAR
;
914 stream
= ((flags
& GLOB_ALTDIRFUNC
) ?
915 (*pglob
->gl_opendir
) (directory
) :
916 (__ptr_t
) opendir (directory
));
919 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
)) ||
928 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
) ?
929 (*pglob
->gl_readdir
) (stream
) :
930 readdir ((DIR *) stream
));
933 if (! REAL_DIR_ENTRY (d
))
938 if (fnmatch (pattern
, name
,
939 (!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0) |
940 ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
947 = (struct globlink
*) __alloca (sizeof (struct globlink
));
950 = (char *) malloc (len
+ 1);
951 if (new->name
== NULL
)
953 memcpy ((__ptr_t
) new->name
, name
, len
);
954 new->name
[len
] = '\0';
962 if (nfound
== 0 && (flags
& GLOB_NOMAGIC
) &&
963 ! __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
)))
964 flags
|= GLOB_NOCHECK
;
966 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
968 size_t len
= strlen (pattern
);
970 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
972 names
->name
= (char *) malloc (len
+ 1);
973 if (names
->name
== NULL
)
975 memcpy (names
->name
, pattern
, len
);
976 names
->name
[len
] = '\0';
980 = (char **) realloc (pglob
->gl_pathv
,
982 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
985 if (pglob
->gl_pathv
== NULL
)
988 if (flags
& GLOB_DOOFFS
)
989 while (pglob
->gl_pathc
< pglob
->gl_offs
)
990 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
992 for (; names
!= NULL
; names
= names
->next
)
993 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
994 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
996 pglob
->gl_flags
= flags
;
1001 if (flags
& GLOB_ALTDIRFUNC
)
1002 (*pglob
->gl_closedir
) (stream
);
1004 closedir ((DIR *) stream
);
1007 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1012 if (flags
& GLOB_ALTDIRFUNC
)
1013 (*pglob
->gl_closedir
) (stream
);
1015 closedir ((DIR *) stream
);
1018 while (names
!= NULL
)
1020 if (names
->name
!= NULL
)
1021 free ((__ptr_t
) names
->name
);
1022 names
= names
->next
;
1024 return GLOB_NOSPACE
;
1027 #endif /* Not ELIDE_CODE. */