1 /* Copyright (C) 1991,92,93,94,95,96,97,98 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. */
29 # define _GNU_SOURCE 1
33 #include <sys/types.h>
36 /* Outcomment the following line for production quality code. */
37 /* #define NDEBUG 1 */
40 #include <stdio.h> /* Needed on stupid SunOS for assert. */
43 /* Comment out all this code if we are using the GNU C Library, and are not
44 actually compiling the library itself. This code is part of the GNU C
45 Library, but also included in many other GNU distributions. Compiling
46 and linking in this code is a waste when using the GNU C library
47 (especially if it is a shared library). Rather than having every GNU
48 program understand `configure --with-gnu-libc' and omit the object files,
49 it is simpler to just do this in the source for each such file. */
51 #define GLOB_INTERFACE_VERSION 1
52 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
53 # include <gnu-versions.h>
54 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
61 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
65 #if defined HAVE_UNISTD_H || defined _LIBC
68 # ifdef _POSIX_VERSION
74 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
78 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
82 # define __set_errno(val) errno = (val)
90 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
92 # define NAMLEN(dirent) strlen((dirent)->d_name)
94 # define dirent direct
95 # define NAMLEN(dirent) (dirent)->d_namlen
96 # ifdef HAVE_SYS_NDIR_H
97 # include <sys/ndir.h>
99 # ifdef HAVE_SYS_DIR_H
100 # include <sys/dir.h>
105 # ifdef HAVE_VMSDIR_H
107 # endif /* HAVE_VMSDIR_H */
111 /* In GNU systems, <dirent.h> defines this macro for us. */
114 # define NAMLEN(d) _D_NAMLEN(d)
117 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
118 if the `d_type' member for `struct dirent' is available. */
119 #ifdef _DIRENT_HAVE_D_TYPE
120 # define HAVE_D_TYPE 1
124 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
125 /* Posix does not require that the d_ino field be present, and some
126 systems do not provide it. */
127 # define REAL_DIR_ENTRY(dp) 1
129 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
132 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
136 #else /* No standard headers. */
138 extern char *getenv ();
140 # ifdef HAVE_STRING_H
144 # include <strings.h>
146 # ifdef HAVE_MEMORY_H
150 extern char *malloc (), *realloc ();
153 extern void qsort ();
154 extern void abort (), exit ();
156 #endif /* Standard headers. */
161 extern void bzero ();
164 extern void bcopy ();
167 # define memcpy(d, s, n) bcopy ((s), (d), (n))
168 # define strrchr rindex
169 /* memset is only used for zero here, but let's be paranoid. */
170 # define memset(s, better_be_zero, n) \
171 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
172 #endif /* Not ANSI_STRING. */
174 #if !defined HAVE_STRCOLL && !defined _LIBC
175 # define strcoll strcmp
178 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
179 # define HAVE_MEMPCPY 1
180 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
184 #ifndef __GNU_LIBRARY__
198 /* These casts are the for sake of the broken Ultrix compiler,
199 which warns of illegal pointer combinations otherwise. */
201 return (char *) malloc (n
);
202 return (char *) realloc (p
, n
);
204 # define realloc my_realloc
206 #endif /* __GNU_LIBRARY__ */
209 #if !defined __alloca && !defined __GNU_LIBRARY__
213 # define alloca(n) __builtin_alloca (n)
214 # else /* Not GCC. */
215 # ifdef HAVE_ALLOCA_H
217 # else /* Not HAVE_ALLOCA_H. */
222 extern char *alloca ();
223 # endif /* WINDOWS32 */
224 # endif /* Not _AIX. */
225 # endif /* sparc or HAVE_ALLOCA_H. */
228 # define __alloca alloca
232 #ifndef __GNU_LIBRARY__
234 # ifdef STAT_MACROS_BROKEN
238 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
243 # define sysconf(id) __sysconf (id)
244 # define closedir(dir) __closedir (dir)
245 # define opendir(name) __opendir (name)
246 # define readdir(str) __readdir (str)
249 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
251 # define size_t unsigned int
254 /* Some system header files erroneously define these.
255 We want our own definitions from <fnmatch.h> to take precedence. */
256 #ifndef __GNU_LIBRARY__
263 /* Some system header files erroneously define these.
264 We want our own definitions from <glob.h> to take precedence. */
265 #ifndef __GNU_LIBRARY__
272 # undef GLOB_NOESCAPE
278 #if __GNUC__ - 0 >= 2
281 const char *next_brace_sub
__P ((const char *begin
));
282 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
284 int (*errfunc
) __P ((const char *, int)),
286 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
287 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
290 /* Find the end of the sub-pattern in a brace expression. We define
291 this as an inline function if the compiler permits. */
293 #if __GNUC__ - 0 >= 2
297 next_brace_sub (begin
)
300 unsigned int depth
= 0;
301 const char *cp
= begin
;
307 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
317 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
324 /* An incorrectly terminated brace expression. */
335 /* Do glob searching for PATTERN, placing results in PGLOB.
336 The bits defined above may be set in FLAGS.
337 If a directory cannot be opened or read and ERRFUNC is not nil,
338 it is called with the pathname that caused the error, and the
339 `errno' value from the failing call; if it returns non-zero
340 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
341 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
342 Otherwise, `glob' returns zero. */
344 glob (pattern
, flags
, errfunc
, pglob
)
347 int (*errfunc
) __P ((const char *, int));
350 const char *filename
;
356 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
358 __set_errno (EINVAL
);
362 if (flags
& GLOB_BRACE
)
364 const char *begin
= strchr (pattern
, '{');
367 /* Allocate working buffer large enough for our work. Note that
368 we have at least an opening and closing brace. */
376 char onealt
[strlen (pattern
) - 1];
378 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
381 if (!(flags
& GLOB_APPEND
))
387 /* We know the prefix for all sub-patterns. */
389 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
391 memcpy (onealt
, pattern
, begin
- pattern
);
392 alt_start
= &onealt
[begin
- pattern
];
395 /* Find the first sub-pattern and at the same time find the
396 rest after the closing brace. */
397 next
= next_brace_sub (begin
+ 1);
400 /* It is an illegal expression. */
404 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
407 /* Now find the end of the whole brace expression. */
411 rest
= next_brace_sub (rest
+ 1);
414 /* It is an illegal expression. */
418 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
421 /* Please note that we now can be sure the brace expression
423 rest_len
= strlen (++rest
) + 1;
425 /* We have a brace expression. BEGIN points to the opening {,
426 NEXT points past the terminator of the first element, and END
427 points past the final }. We will accumulate result names from
428 recursive runs for each brace alternative in the buffer using
431 if (!(flags
& GLOB_APPEND
))
433 /* This call is to set a new vector, so clear out the
434 vector so we can append to it. */
436 pglob
->gl_pathv
= NULL
;
438 firstc
= pglob
->gl_pathc
;
445 /* Construct the new glob expression. */
447 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
449 memcpy (alt_start
, p
, next
- p
);
450 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
453 result
= glob (onealt
,
454 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
455 | GLOB_APPEND
), errfunc
, pglob
);
457 /* If we got an error, return it. */
458 if (result
&& result
!= GLOB_NOMATCH
)
463 if (!(flags
& GLOB_APPEND
))
469 /* We saw the last entry. */
473 next
= next_brace_sub (p
);
474 assert (next
!= NULL
);
481 if (pglob
->gl_pathc
!= firstc
)
482 /* We found some entries. */
484 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
489 /* Find the filename. */
490 filename
= strrchr (pattern
, '/');
491 if (filename
== NULL
)
493 /* This can mean two things: a simple name or "~name". The later
494 case is nothing but a notation for a directory. */
495 if ((flags
& GLOB_TILDE
) && pattern
[0] == '~')
498 dirlen
= strlen (pattern
);
506 dirname
= (char *) "";
508 dirname
= (char *) ".";
513 else if (filename
== pattern
)
516 dirname
= (char *) "/";
522 dirlen
= filename
- pattern
;
523 dirname
= (char *) __alloca (dirlen
+ 1);
525 *((char *) mempcpy (dirname
, pattern
, dirlen
)) = '\0';
527 memcpy (dirname
, pattern
, dirlen
);
528 dirname
[dirlen
] = '\0';
532 if (filename
[0] == '\0' && dirlen
> 1)
533 /* "pattern/". Expand "pattern", appending slashes. */
535 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
537 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
538 | (flags
& GLOB_MARK
));
543 if (!(flags
& GLOB_APPEND
))
546 pglob
->gl_pathv
= NULL
;
549 oldcount
= pglob
->gl_pathc
;
552 if ((flags
& GLOB_TILDE
) && dirname
[0] == '~')
554 if (dirname
[1] == '\0' || dirname
[1] == '/')
556 /* Look up home directory. */
557 char *home_dir
= getenv ("HOME");
559 if (home_dir
== NULL
|| home_dir
[0] == '\0')
563 if (home_dir
== NULL
|| home_dir
[0] == '\0')
564 home_dir
= "c:/users/default"; /* poor default */
566 if (home_dir
== NULL
|| home_dir
[0] == '\0')
569 # if defined HAVE_GETLOGIN_R || defined _LIBC
570 extern int getlogin_r
__P ((char *, size_t));
571 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
575 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
578 name
= (char *) __alloca (buflen
);
580 success
= getlogin_r (name
, buflen
) >= 0;
582 extern char *getlogin
__P ((void));
585 success
= (name
= getlogin ()) != NULL
;
589 # if defined HAVE_GETPWNAM_R || defined _LIBC
590 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
592 struct passwd pwbuf
, *p
;
594 pwtmpbuf
= (char *) __alloca (pwbuflen
);
596 success
= (__getpwnam_r (name
, &pwbuf
, pwtmpbuf
,
599 struct passwd
*p
= getpwnam (name
);
603 home_dir
= p
->pw_dir
;
606 if (home_dir
== NULL
|| home_dir
[0] == '\0')
607 home_dir
= (char *) "~"; /* No luck. */
608 # endif /* WINDOWS32 */
610 /* Now construct the full directory. */
611 if (dirname
[1] == '\0')
616 size_t home_len
= strlen (home_dir
);
617 newp
= (char *) __alloca (home_len
+ dirlen
);
619 mempcpy (mempcpy (newp
, home_dir
, home_len
),
620 &dirname
[1], dirlen
);
622 memcpy (newp
, home_dir
, home_len
);
623 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
628 # if !defined _AMIGA && !defined WINDOWS32
631 char *end_name
= strchr (dirname
, '/');
635 if (end_name
== NULL
)
636 user_name
= dirname
+ 1;
639 user_name
= (char *) __alloca (end_name
- dirname
);
641 *((char *) mempcpy (user_name
, dirname
+ 1, end_name
- dirname
))
644 memcpy (user_name
, dirname
+ 1, end_name
- dirname
);
645 user_name
[end_name
- dirname
- 1] = '\0';
649 /* Look up specific user's home directory. */
651 # if defined HAVE_GETPWNAM_R || defined _LIBC
652 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
653 char *pwtmpbuf
= (char *) __alloca (buflen
);
654 struct passwd pwbuf
, *p
;
655 if (__getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) >= 0)
656 home_dir
= p
->pw_dir
;
660 struct passwd
*p
= getpwnam (user_name
);
662 home_dir
= p
->pw_dir
;
667 /* If we found a home directory use this. */
668 if (home_dir
!= NULL
)
671 size_t home_len
= strlen (home_dir
);
672 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
673 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
675 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
676 end_name
, rest_len
)) = '\0';
678 memcpy (newp
, home_dir
, home_len
);
679 memcpy (&newp
[home_len
], end_name
, rest_len
);
680 newp
[home_len
+ rest_len
] = '\0';
685 # endif /* Not Amiga && not WINDOWS32. */
687 #endif /* Not VMS. */
689 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
691 /* The directory name contains metacharacters, so we
692 have to glob for the directory, and then glob for
693 the pattern in each directory found. */
697 status
= glob (dirname
,
698 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
))
699 | GLOB_NOSORT
| GLOB_ONLYDIR
),
704 /* We have successfully globbed the preceding directory name.
705 For each name we found, call glob_in_dir on it and FILENAME,
706 appending the results to PGLOB. */
707 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
713 /* Make globbing interruptible in the bash shell. */
714 extern int interrupt_state
;
725 old_pathc
= pglob
->gl_pathc
;
726 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
727 ((flags
| GLOB_APPEND
)
728 & ~(GLOB_NOCHECK
| GLOB_ERR
)),
730 if (status
== GLOB_NOMATCH
)
731 /* No matches in this directory. Try the next. */
741 /* Stick the directory on the front of each name. */
742 if (prefix_array (dirs
.gl_pathv
[i
],
743 &pglob
->gl_pathv
[old_pathc
],
744 pglob
->gl_pathc
- old_pathc
))
752 flags
|= GLOB_MAGCHAR
;
754 if (pglob
->gl_pathc
== oldcount
)
756 if (flags
& GLOB_NOCHECK
)
758 size_t len
= strlen (pattern
) + 1;
759 char *patcopy
= (char *) malloc (len
);
762 memcpy (patcopy
, pattern
, len
);
765 = (char **) realloc (pglob
->gl_pathv
,
767 ((flags
& GLOB_DOOFFS
) ?
768 pglob
->gl_offs
: 0) +
771 if (pglob
->gl_pathv
== NULL
)
777 if (flags
& GLOB_DOOFFS
)
778 while (pglob
->gl_pathc
< pglob
->gl_offs
)
779 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
781 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
782 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
783 pglob
->gl_flags
= flags
;
790 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
796 /* Stick the directory on the front of each name. */
797 int ignore
= oldcount
;
799 if ((flags
& GLOB_DOOFFS
) && ignore
< pglob
->gl_offs
)
800 ignore
= pglob
->gl_offs
;
802 if (prefix_array (dirname
,
803 &pglob
->gl_pathv
[ignore
],
804 pglob
->gl_pathc
- ignore
))
812 if (flags
& GLOB_MARK
)
814 /* Append slashes to directory names. */
817 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
818 if (((flags
& GLOB_ALTDIRFUNC
)
819 ? (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
)
820 : __stat (pglob
->gl_pathv
[i
], &st
)) == 0
821 && S_ISDIR (st
.st_mode
))
823 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
824 char *new = realloc (pglob
->gl_pathv
[i
], len
);
830 strcpy (&new[len
- 2], "/");
831 pglob
->gl_pathv
[i
] = new;
835 if (!(flags
& GLOB_NOSORT
))
837 /* Sort the vector. */
838 int non_sort
= oldcount
;
840 if ((flags
& GLOB_DOOFFS
) && pglob
->gl_offs
> oldcount
)
841 non_sort
= pglob
->gl_offs
;
843 qsort ((__ptr_t
) &pglob
->gl_pathv
[non_sort
],
844 pglob
->gl_pathc
- non_sort
,
845 sizeof (char *), collated_compare
);
852 /* Free storage allocated in PGLOB by a previous `glob' call. */
855 register glob_t
*pglob
;
857 if (pglob
->gl_pathv
!= NULL
)
860 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
861 if (pglob
->gl_pathv
[i
] != NULL
)
862 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
863 free ((__ptr_t
) pglob
->gl_pathv
);
868 /* Do a collated comparison of A and B. */
870 collated_compare (a
, b
)
874 const char *const s1
= *(const char *const * const) a
;
875 const char *const s2
= *(const char *const * const) b
;
883 return strcoll (s1
, s2
);
887 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
888 elements in place. Return nonzero if out of memory, zero if successful.
889 A slash is inserted between DIRNAME and each elt of ARRAY,
890 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
892 prefix_array (dirname
, array
, n
)
898 size_t dirlen
= strlen (dirname
);
900 if (dirlen
== 1 && dirname
[0] == '/')
901 /* DIRNAME is just "/", so normal prepending would get us "//foo".
902 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
905 for (i
= 0; i
< n
; ++i
)
907 size_t eltlen
= strlen (array
[i
]) + 1;
908 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
912 free ((__ptr_t
) array
[--i
]);
918 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
920 mempcpy (endp
, array
[i
], eltlen
);
923 memcpy (new, dirname
, dirlen
);
925 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
927 free ((__ptr_t
) array
[i
]);
935 /* Return nonzero if PATTERN contains any metacharacters.
936 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
938 __glob_pattern_p (pattern
, quote
)
942 register const char *p
;
945 for (p
= pattern
; *p
!= '\0'; ++p
)
953 if (quote
&& p
[1] != '\0')
970 weak_alias (__glob_pattern_p
, glob_pattern_p
)
974 /* Like `glob', but PATTERN is a final pathname component,
975 and matches are searched for in DIRECTORY.
976 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
977 The GLOB_APPEND flag is assumed to be set (always appends). */
979 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
981 const char *directory
;
983 int (*errfunc
) __P ((const char *, int));
986 __ptr_t stream
= NULL
;
990 struct globlink
*next
;
993 struct globlink
*names
= NULL
;
998 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1001 if (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
))
1002 /* We need not do any tests. The PATTERN contains no meta
1003 characters and we must not return an error therefore the
1004 result will always contain exactly the one name. */
1005 flags
|= GLOB_NOCHECK
;
1008 /* Since we use the normal file functions we can also use stat()
1009 to verify the file is there. */
1011 size_t patlen
= strlen (pattern
);
1012 size_t dirlen
= strlen (directory
);
1013 char *fullname
= __alloca (dirlen
+ 1 + patlen
+ 1);
1015 # ifdef HAVE_MEMPCPY
1016 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1018 pattern
, patlen
+ 1);
1020 memcpy (fullname
, directory
, dirlen
);
1021 fullname
[dirlen
] = '/';
1022 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1024 if (((flags
& GLOB_ALTDIRFUNC
)
1025 ? (*pglob
->gl_stat
) (fullname
, &st
)
1026 : __stat (fullname
, &st
)) == 0)
1027 /* We found this file to be existing. No tell the rest
1028 of the function to copy this name into the result. */
1029 flags
|= GLOB_NOCHECK
;
1036 if (pattern
[0] == '\0')
1038 /* This is a special case for matching directories like in
1040 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1041 names
->name
= (char *) malloc (1);
1042 if (names
->name
== NULL
)
1044 names
->name
[0] = '\0';
1051 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1052 ? (*pglob
->gl_opendir
) (directory
)
1053 : (__ptr_t
) opendir (directory
));
1056 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1057 || (flags
& GLOB_ERR
))
1058 return GLOB_ABORTED
;
1064 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1065 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1066 #if defined _AMIGA || defined VMS
1071 flags
|= GLOB_MAGCHAR
;
1077 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1078 ? (*pglob
->gl_readdir
) (stream
)
1079 : readdir ((DIR *) stream
));
1082 if (! REAL_DIR_ENTRY (d
))
1086 /* If we shall match only directories use the information
1087 provided by the dirent call if possible. */
1088 if ((flags
& GLOB_ONLYDIR
)
1089 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1095 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1097 struct globlink
*new = (struct globlink
*)
1098 __alloca (sizeof (struct globlink
));
1100 new->name
= (char *) malloc (len
+ 1);
1101 if (new->name
== NULL
)
1104 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1107 memcpy ((__ptr_t
) new->name
, name
, len
);
1108 new->name
[len
] = '\0';
1119 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1121 size_t len
= strlen (pattern
);
1123 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1125 names
->name
= (char *) malloc (len
+ 1);
1126 if (names
->name
== NULL
)
1129 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1131 memcpy (names
->name
, pattern
, len
);
1132 names
->name
[len
] = '\0';
1139 = (char **) realloc (pglob
->gl_pathv
,
1141 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
1144 if (pglob
->gl_pathv
== NULL
)
1147 if (flags
& GLOB_DOOFFS
)
1148 while (pglob
->gl_pathc
< pglob
->gl_offs
)
1149 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
1151 for (; names
!= NULL
; names
= names
->next
)
1152 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
1153 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
1155 pglob
->gl_flags
= flags
;
1160 if (flags
& GLOB_ALTDIRFUNC
)
1161 (*pglob
->gl_closedir
) (stream
);
1163 closedir ((DIR *) stream
);
1166 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1171 if (flags
& GLOB_ALTDIRFUNC
)
1172 (*pglob
->gl_closedir
) (stream
);
1174 closedir ((DIR *) stream
);
1177 while (names
!= NULL
)
1179 if (names
->name
!= NULL
)
1180 free ((__ptr_t
) names
->name
);
1181 names
= names
->next
;
1183 return GLOB_NOSPACE
;
1186 #endif /* Not ELIDE_CODE. */