1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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
181 # 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)
244 # define strdup(str) __strdup (str)
245 # define sysconf(id) __sysconf (id)
246 # define closedir(dir) __closedir (dir)
247 # define opendir(name) __opendir (name)
248 # define readdir(str) __readdir (str)
249 # define getpwnam_r(name, bufp, buf, len, res) \
250 __getpwnam_r (name, bufp, buf, len, res)
252 # define __stat(fname, buf) __xstat (_STAT_VER, fname, buf)
256 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
258 # define size_t unsigned int
261 /* Some system header files erroneously define these.
262 We want our own definitions from <fnmatch.h> to take precedence. */
263 #ifndef __GNU_LIBRARY__
270 /* Some system header files erroneously define these.
271 We want our own definitions from <glob.h> to take precedence. */
272 #ifndef __GNU_LIBRARY__
279 # undef GLOB_NOESCAPE
284 #ifdef HAVE_GETLOGIN_R
285 extern int getlogin_r
__P ((char *, size_t));
287 extern char *getlogin
__P ((void));
291 #if __GNUC__ - 0 >= 2
294 const char *next_brace_sub
__P ((const char *begin
));
295 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
297 int (*errfunc
) (const char *, int),
299 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
300 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
303 /* Find the end of the sub-pattern in a brace expression. We define
304 this as an inline function if the compiler permits. */
306 #if __GNUC__ - 0 >= 2
310 next_brace_sub (begin
)
313 unsigned int depth
= 0;
314 const char *cp
= begin
;
320 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
330 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
337 /* An incorrectly terminated brace expression. */
348 /* Do glob searching for PATTERN, placing results in PGLOB.
349 The bits defined above may be set in FLAGS.
350 If a directory cannot be opened or read and ERRFUNC is not nil,
351 it is called with the pathname that caused the error, and the
352 `errno' value from the failing call; if it returns non-zero
353 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
354 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
355 Otherwise, `glob' returns zero. */
357 glob (pattern
, flags
, errfunc
, pglob
)
360 int (*errfunc
) __P ((const char *, int));
363 const char *filename
;
369 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
371 __set_errno (EINVAL
);
375 if (flags
& GLOB_BRACE
)
377 const char *begin
= strchr (pattern
, '{');
380 /* Allocate working buffer large enough for our work. Note that
381 we have at least an opening and closing brace. */
389 char onealt
[strlen (pattern
) - 1];
391 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
394 if (!(flags
& GLOB_APPEND
))
400 /* We know the prefix for all sub-patterns. */
402 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
404 memcpy (onealt
, pattern
, begin
- pattern
);
405 alt_start
= &onealt
[begin
- pattern
];
408 /* Find the first sub-pattern and at the same time find the
409 rest after the closing brace. */
410 next
= next_brace_sub (begin
+ 1);
413 /* It is an illegal expression. */
417 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
420 /* Now find the end of the whole brace expression. */
424 rest
= next_brace_sub (rest
+ 1);
427 /* It is an illegal expression. */
431 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
434 /* Please note that we now can be sure the brace expression
436 rest_len
= strlen (++rest
) + 1;
438 /* We have a brace expression. BEGIN points to the opening {,
439 NEXT points past the terminator of the first element, and END
440 points past the final }. We will accumulate result names from
441 recursive runs for each brace alternative in the buffer using
444 if (!(flags
& GLOB_APPEND
))
446 /* This call is to set a new vector, so clear out the
447 vector so we can append to it. */
449 pglob
->gl_pathv
= NULL
;
451 firstc
= pglob
->gl_pathc
;
458 /* Construct the new glob expression. */
460 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
462 memcpy (alt_start
, p
, next
- p
);
463 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
466 result
= glob (onealt
,
467 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
468 | GLOB_APPEND
), errfunc
, pglob
);
470 /* If we got an error, return it. */
471 if (result
&& result
!= GLOB_NOMATCH
)
476 if (!(flags
& GLOB_APPEND
))
482 /* We saw the last entry. */
486 next
= next_brace_sub (p
);
487 assert (next
!= NULL
);
494 if (pglob
->gl_pathc
!= firstc
)
495 /* We found some entries. */
497 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
502 /* Find the filename. */
503 filename
= strrchr (pattern
, '/');
504 #if defined __MSDOS__ || defined WINDOWS32
505 /* The case of "d:pattern". Since `:' is not allowed in
506 file names, we can safely assume that wherever it
507 happens in pattern, it signals the filename part. This
508 is so we could some day support patterns like "[a-z]:foo". */
509 if (filename
== NULL
)
510 filename
= strchr (pattern
, ':');
511 #endif /* __MSDOS__ || WINDOWS32 */
512 if (filename
== NULL
)
514 /* This can mean two things: a simple name or "~name". The later
515 case is nothing but a notation for a directory. */
516 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
519 dirlen
= strlen (pattern
);
521 /* Set FILENAME to NULL as a special flag. This is ugly but
522 other solutions would require much more code. We test for
523 this special case below. */
537 else if (filename
== pattern
)
547 dirlen
= filename
- pattern
;
548 #if defined __MSDOS__ || defined WINDOWS32
550 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
555 drive_spec
= (char *) __alloca (dirlen
+ 1);
557 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
559 memcpy (drive_spec
, pattern
, dirlen
);
560 drive_spec
[dirlen
] = '\0';
562 /* For now, disallow wildcards in the drive spec, to
563 prevent infinite recursion in glob. */
564 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
566 /* If this is "d:pattern", we need to copy `:' to DIRNAME
567 as well. If it's "d:/pattern", don't remove the slash
568 from "d:/", since "d:" and "d:/" are not the same.*/
571 newp
= (char *) __alloca (dirlen
+ 1);
573 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
575 memcpy (newp
, pattern
, dirlen
);
581 if (filename
[0] == '\0'
582 #if defined __MSDOS__ || defined WINDOWS32
583 && dirname
[dirlen
- 1] != ':'
584 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
585 || dirname
[dirlen
- 1] != '/')
588 /* "pattern/". Expand "pattern", appending slashes. */
590 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
592 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
593 | (flags
& GLOB_MARK
));
598 if (!(flags
& GLOB_APPEND
))
601 pglob
->gl_pathv
= NULL
;
604 oldcount
= pglob
->gl_pathc
;
607 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
609 if (dirname
[1] == '\0' || dirname
[1] == '/')
611 /* Look up home directory. */
612 const char *home_dir
= getenv ("HOME");
614 if (home_dir
== NULL
|| home_dir
[0] == '\0')
618 if (home_dir
== NULL
|| home_dir
[0] == '\0')
619 home_dir
= "c:/users/default"; /* poor default */
621 if (home_dir
== NULL
|| home_dir
[0] == '\0')
625 # if defined HAVE_GETLOGIN_R || defined _LIBC
626 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
629 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
632 name
= (char *) __alloca (buflen
);
634 success
= getlogin_r (name
, buflen
) >= 0;
636 success
= (name
= getlogin ()) != NULL
;
641 # if defined HAVE_GETPWNAM_R || defined _LIBC
642 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
648 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
649 Try a moderate value. */
651 pwtmpbuf
= (char *) __alloca (pwbuflen
);
653 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
662 pwtmpbuf
= (char *) __alloca (pwbuflen
);
669 home_dir
= p
->pw_dir
;
672 if (home_dir
== NULL
|| home_dir
[0] == '\0')
674 if (flags
& GLOB_TILDE_CHECK
)
677 home_dir
= "~"; /* No luck. */
679 # endif /* WINDOWS32 */
681 /* Now construct the full directory. */
682 if (dirname
[1] == '\0')
687 size_t home_len
= strlen (home_dir
);
688 newp
= (char *) __alloca (home_len
+ dirlen
);
690 mempcpy (mempcpy (newp
, home_dir
, home_len
),
691 &dirname
[1], dirlen
);
693 memcpy (newp
, home_dir
, home_len
);
694 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
699 # if !defined _AMIGA && !defined WINDOWS32
702 char *end_name
= strchr (dirname
, '/');
703 const char *user_name
;
704 const char *home_dir
;
706 if (end_name
== NULL
)
707 user_name
= dirname
+ 1;
711 newp
= (char *) __alloca (end_name
- dirname
);
713 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
716 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
717 newp
[end_name
- dirname
- 1] = '\0';
722 /* Look up specific user's home directory. */
725 # if defined HAVE_GETPWNAM_R || defined _LIBC
726 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
732 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
735 pwtmpbuf
= (char *) __alloca (buflen
);
737 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
745 pwtmpbuf
= __alloca (buflen
);
749 p
= getpwnam (user_name
);
752 home_dir
= p
->pw_dir
;
756 /* If we found a home directory use this. */
757 if (home_dir
!= NULL
)
760 size_t home_len
= strlen (home_dir
);
761 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
762 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
764 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
765 end_name
, rest_len
)) = '\0';
767 memcpy (newp
, home_dir
, home_len
);
768 memcpy (&newp
[home_len
], end_name
, rest_len
);
769 newp
[home_len
+ rest_len
] = '\0';
774 if (flags
& GLOB_TILDE_CHECK
)
775 /* We have to regard it as an error if we cannot find the
779 # endif /* Not Amiga && not WINDOWS32. */
781 #endif /* Not VMS. */
783 /* Now test whether we looked for "~" or "~NAME". In this case we
784 can give the answer now. */
785 if (filename
== NULL
)
789 /* Return the directory if we don't check for error or if it exists. */
790 if ((flags
& GLOB_NOCHECK
)
791 || (((flags
& GLOB_ALTDIRFUNC
)
792 ? (*pglob
->gl_stat
) (dirname
, &st
)
793 : __stat (dirname
, &st
)) == 0
794 && S_ISDIR (st
.st_mode
)))
797 = (char **) realloc (pglob
->gl_pathv
,
799 ((flags
& GLOB_DOOFFS
) ?
800 pglob
->gl_offs
: 0) +
803 if (pglob
->gl_pathv
== NULL
)
806 if (flags
& GLOB_DOOFFS
)
807 while (pglob
->gl_pathc
< pglob
->gl_offs
)
808 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
810 #if defined HAVE_STRDUP || defined _LIBC
811 pglob
->gl_pathv
[pglob
->gl_pathc
] = strdup (dirname
);
814 size_t len
= strlen (dirname
) + 1;
815 char *dircopy
= malloc (len
);
817 pglob
->gl_pathv
[pglob
->gl_pathc
] = memcpy (dircopy
, dirname
,
821 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
823 free (pglob
->gl_pathv
);
826 pglob
->gl_pathv
[++pglob
->gl_pathc
] = NULL
;
827 pglob
->gl_flags
= flags
;
836 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
838 /* The directory name contains metacharacters, so we
839 have to glob for the directory, and then glob for
840 the pattern in each directory found. */
844 status
= glob (dirname
,
845 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
))
846 | GLOB_NOSORT
| GLOB_ONLYDIR
),
851 /* We have successfully globbed the preceding directory name.
852 For each name we found, call glob_in_dir on it and FILENAME,
853 appending the results to PGLOB. */
854 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
860 /* Make globbing interruptible in the bash shell. */
861 extern int interrupt_state
;
872 old_pathc
= pglob
->gl_pathc
;
873 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
874 ((flags
| GLOB_APPEND
)
875 & ~(GLOB_NOCHECK
| GLOB_ERR
)),
877 if (status
== GLOB_NOMATCH
)
878 /* No matches in this directory. Try the next. */
888 /* Stick the directory on the front of each name. */
889 if (prefix_array (dirs
.gl_pathv
[i
],
890 &pglob
->gl_pathv
[old_pathc
],
891 pglob
->gl_pathc
- old_pathc
))
899 flags
|= GLOB_MAGCHAR
;
901 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
902 But if we have not found any matching entry and thie GLOB_NOCHECK
903 flag was set we must return the list consisting of the disrectory
904 names followed by the filename. */
905 if (pglob
->gl_pathc
== oldcount
)
908 if (flags
& GLOB_NOCHECK
)
910 size_t filename_len
= strlen (filename
) + 1;
914 /* This is an pessimistic guess about the size. */
916 = (char **) realloc (pglob
->gl_pathv
,
918 ((flags
& GLOB_DOOFFS
) ?
919 pglob
->gl_offs
: 0) +
922 if (pglob
->gl_pathv
== NULL
)
928 if (flags
& GLOB_DOOFFS
)
929 while (pglob
->gl_pathc
< pglob
->gl_offs
)
930 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
932 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
934 const char *dir
= dirs
.gl_pathv
[i
];
935 size_t dir_len
= strlen (dir
);
937 /* First check whether this really is a directory. */
938 if (((flags
& GLOB_ALTDIRFUNC
)
939 ? (*pglob
->gl_stat
) (dir
, &st
) : __stat (dir
, &st
)) != 0
940 || !S_ISDIR (st
.st_mode
))
941 /* No directory, ignore this entry. */
944 pglob
->gl_pathv
[pglob
->gl_pathc
] = malloc (dir_len
+ 1
946 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
954 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[pglob
->gl_pathc
],
957 filename
, filename_len
);
959 memcpy (pglob
->gl_pathv
[pglob
->gl_pathc
], dir
, dir_len
);
960 pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
] = '/';
961 memcpy (&pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
+ 1],
962 filename
, filename_len
);
967 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
968 pglob
->gl_flags
= flags
;
970 /* Now we know how large the gl_pathv vector must be. */
971 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
972 ((pglob
->gl_pathc
+ 1)
974 if (new_pathv
!= NULL
)
975 pglob
->gl_pathv
= new_pathv
;
985 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
991 /* Stick the directory on the front of each name. */
992 int ignore
= oldcount
;
994 if ((flags
& GLOB_DOOFFS
) && ignore
< pglob
->gl_offs
)
995 ignore
= pglob
->gl_offs
;
997 if (prefix_array (dirname
,
998 &pglob
->gl_pathv
[ignore
],
999 pglob
->gl_pathc
- ignore
))
1002 return GLOB_NOSPACE
;
1007 if (flags
& GLOB_MARK
)
1009 /* Append slashes to directory names. */
1012 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
1013 if (((flags
& GLOB_ALTDIRFUNC
)
1014 ? (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
)
1015 : __stat (pglob
->gl_pathv
[i
], &st
)) == 0
1016 && S_ISDIR (st
.st_mode
))
1018 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1019 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1023 return GLOB_NOSPACE
;
1025 strcpy (&new[len
- 2], "/");
1026 pglob
->gl_pathv
[i
] = new;
1030 if (!(flags
& GLOB_NOSORT
))
1032 /* Sort the vector. */
1033 int non_sort
= oldcount
;
1035 if ((flags
& GLOB_DOOFFS
) && pglob
->gl_offs
> oldcount
)
1036 non_sort
= pglob
->gl_offs
;
1038 qsort ((__ptr_t
) &pglob
->gl_pathv
[non_sort
],
1039 pglob
->gl_pathc
- non_sort
,
1040 sizeof (char *), collated_compare
);
1047 /* Free storage allocated in PGLOB by a previous `glob' call. */
1050 register glob_t
*pglob
;
1052 if (pglob
->gl_pathv
!= NULL
)
1055 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1056 if (pglob
->gl_pathv
[i
] != NULL
)
1057 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
1058 free ((__ptr_t
) pglob
->gl_pathv
);
1063 /* Do a collated comparison of A and B. */
1065 collated_compare (a
, b
)
1069 const char *const s1
= *(const char *const * const) a
;
1070 const char *const s2
= *(const char *const * const) b
;
1078 return strcoll (s1
, s2
);
1082 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1083 elements in place. Return nonzero if out of memory, zero if successful.
1084 A slash is inserted between DIRNAME and each elt of ARRAY,
1085 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1087 prefix_array (dirname
, array
, n
)
1088 const char *dirname
;
1093 size_t dirlen
= strlen (dirname
);
1094 #if defined __MSDOS__ || defined WINDOWS32
1096 # define DIRSEP_CHAR sep_char
1098 # define DIRSEP_CHAR '/'
1101 if (dirlen
== 1 && dirname
[0] == '/')
1102 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1103 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1105 #if defined __MSDOS__ || defined WINDOWS32
1106 else if (dirlen
> 1)
1108 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1109 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1111 else if (dirname
[dirlen
- 1] == ':')
1113 /* DIRNAME is "d:". Use `:' instead of `/'. */
1120 for (i
= 0; i
< n
; ++i
)
1122 size_t eltlen
= strlen (array
[i
]) + 1;
1123 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1127 free ((__ptr_t
) array
[--i
]);
1133 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1134 *endp
++ = DIRSEP_CHAR
;
1135 mempcpy (endp
, array
[i
], eltlen
);
1138 memcpy (new, dirname
, dirlen
);
1139 new[dirlen
] = DIRSEP_CHAR
;
1140 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1142 free ((__ptr_t
) array
[i
]);
1150 /* We must not compile this function twice. */
1151 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1152 /* Return nonzero if PATTERN contains any metacharacters.
1153 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1155 __glob_pattern_p (pattern
, quote
)
1156 const char *pattern
;
1159 register const char *p
;
1162 for (p
= pattern
; *p
!= '\0'; ++p
)
1170 if (quote
&& p
[1] != '\0')
1187 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1192 /* Like `glob', but PATTERN is a final pathname component,
1193 and matches are searched for in DIRECTORY.
1194 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1195 The GLOB_APPEND flag is assumed to be set (always appends). */
1197 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1198 const char *pattern
;
1199 const char *directory
;
1201 int (*errfunc
) __P ((const char *, int));
1204 __ptr_t stream
= NULL
;
1208 struct globlink
*next
;
1211 struct globlink
*names
= NULL
;
1216 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1219 if (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
))
1220 /* We need not do any tests. The PATTERN contains no meta
1221 characters and we must not return an error therefore the
1222 result will always contain exactly one name. */
1223 flags
|= GLOB_NOCHECK
;
1226 /* Since we use the normal file functions we can also use stat()
1227 to verify the file is there. */
1229 size_t patlen
= strlen (pattern
);
1230 size_t dirlen
= strlen (directory
);
1231 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1233 # ifdef HAVE_MEMPCPY
1234 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1236 pattern
, patlen
+ 1);
1238 memcpy (fullname
, directory
, dirlen
);
1239 fullname
[dirlen
] = '/';
1240 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1242 if (((flags
& GLOB_ALTDIRFUNC
)
1243 ? (*pglob
->gl_stat
) (fullname
, &st
)
1244 : __stat (fullname
, &st
)) == 0)
1245 /* We found this file to be existing. Now tell the rest
1246 of the function to copy this name into the result. */
1247 flags
|= GLOB_NOCHECK
;
1254 if (pattern
[0] == '\0')
1256 /* This is a special case for matching directories like in
1258 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1259 names
->name
= (char *) malloc (1);
1260 if (names
->name
== NULL
)
1262 names
->name
[0] = '\0';
1269 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1270 ? (*pglob
->gl_opendir
) (directory
)
1271 : (__ptr_t
) opendir (directory
));
1274 if (errno
!= ENOTDIR
1275 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1276 || (flags
& GLOB_ERR
)))
1277 return GLOB_ABORTED
;
1283 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1284 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1285 #if defined _AMIGA || defined VMS
1290 flags
|= GLOB_MAGCHAR
;
1296 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1297 ? (*pglob
->gl_readdir
) (stream
)
1298 : readdir ((DIR *) stream
));
1301 if (! REAL_DIR_ENTRY (d
))
1305 /* If we shall match only directories use the information
1306 provided by the dirent call if possible. */
1307 if ((flags
& GLOB_ONLYDIR
)
1308 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1314 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1316 struct globlink
*new = (struct globlink
*)
1317 __alloca (sizeof (struct globlink
));
1319 new->name
= (char *) malloc (len
+ 1);
1320 if (new->name
== NULL
)
1323 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1326 memcpy ((__ptr_t
) new->name
, name
, len
);
1327 new->name
[len
] = '\0';
1338 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1340 size_t len
= strlen (pattern
);
1342 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1344 names
->name
= (char *) malloc (len
+ 1);
1345 if (names
->name
== NULL
)
1348 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1350 memcpy (names
->name
, pattern
, len
);
1351 names
->name
[len
] = '\0';
1358 = (char **) realloc (pglob
->gl_pathv
,
1360 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
1363 if (pglob
->gl_pathv
== NULL
)
1366 if (flags
& GLOB_DOOFFS
)
1367 while (pglob
->gl_pathc
< pglob
->gl_offs
)
1368 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
1370 for (; names
!= NULL
; names
= names
->next
)
1371 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
1372 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
1374 pglob
->gl_flags
= flags
;
1380 if (flags
& GLOB_ALTDIRFUNC
)
1381 (*pglob
->gl_closedir
) (stream
);
1383 closedir ((DIR *) stream
);
1387 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1392 if (flags
& GLOB_ALTDIRFUNC
)
1393 (*pglob
->gl_closedir
) (stream
);
1395 closedir ((DIR *) stream
);
1398 while (names
!= NULL
)
1400 if (names
->name
!= NULL
)
1401 free ((__ptr_t
) names
->name
);
1402 names
= names
->next
;
1404 return GLOB_NOSPACE
;
1407 #endif /* Not ELIDE_CODE. */