1 /* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 /* AIX requires this to be the first thing in the file. */
20 #if defined _AIX && !defined __GNUC__
28 /* Enable GNU extensions in glob.h. */
30 # define _GNU_SOURCE 1
34 #include <sys/types.h>
37 /* Outcomment the following line for production quality code. */
38 /* #define NDEBUG 1 */
41 #include <stdio.h> /* Needed on stupid SunOS for assert. */
44 /* Comment out all this code if we are using the GNU C Library, and are not
45 actually compiling the library itself. This code is part of the GNU C
46 Library, but also included in many other GNU distributions. Compiling
47 and linking in this code is a waste when using the GNU C library
48 (especially if it is a shared library). Rather than having every GNU
49 program understand `configure --with-gnu-libc' and omit the object files,
50 it is simpler to just do this in the source for each such file. */
52 #define GLOB_INTERFACE_VERSION 1
53 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
54 # include <gnu-versions.h>
55 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
61 #if !defined _LIBC || !defined GLOB_ONLY_P
63 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
67 #if defined HAVE_UNISTD_H || defined _LIBC
70 # ifdef _POSIX_VERSION
76 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
80 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
84 # define __set_errno(val) errno = (val)
92 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
94 # define NAMLEN(dirent) strlen((dirent)->d_name)
96 # define dirent direct
97 # define NAMLEN(dirent) (dirent)->d_namlen
98 # ifdef HAVE_SYS_NDIR_H
99 # include <sys/ndir.h>
101 # ifdef HAVE_SYS_DIR_H
102 # include <sys/dir.h>
107 # ifdef HAVE_VMSDIR_H
109 # endif /* HAVE_VMSDIR_H */
113 /* In GNU systems, <dirent.h> defines this macro for us. */
116 # define NAMLEN(d) _D_NAMLEN(d)
119 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
120 if the `d_type' member for `struct dirent' is available. */
121 #ifdef _DIRENT_HAVE_D_TYPE
122 # define HAVE_D_TYPE 1
126 # define HAVE_DIRENT64 1
129 /* If the system has the `struct dirent64' type we use it internally. */
130 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
131 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
132 # define CONVERT_D_NAMLEN(d64, d32)
134 # define CONVERT_D_NAMLEN(d64, d32) \
135 (d64)->d_namlen = (d32)->d_namlen;
138 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
139 # define CONVERT_D_INO(d64, d32)
141 # define CONVERT_D_INO(d64, d32) \
142 (d64)->d_ino = (d32)->d_ino;
146 # define CONVERT_D_TYPE(d64, d32) \
147 (d64)->d_type = (d32)->d_type;
149 # define CONVERT_D_TYPE(d64, d32)
152 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
153 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
154 CONVERT_D_NAMLEN (d64, d32) \
155 CONVERT_D_INO (d64, d32) \
156 CONVERT_D_TYPE (d64, d32)
160 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
161 /* Posix does not require that the d_ino field be present, and some
162 systems do not provide it. */
163 # define REAL_DIR_ENTRY(dp) 1
165 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
168 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
172 #else /* No standard headers. */
174 extern char *getenv ();
176 # ifdef HAVE_STRING_H
180 # include <strings.h>
182 # ifdef HAVE_MEMORY_H
186 extern char *malloc (), *realloc ();
189 extern void qsort ();
190 extern void abort (), exit ();
192 #endif /* Standard headers. */
197 extern void bzero ();
200 extern void bcopy ();
203 # define memcpy(d, s, n) bcopy ((s), (d), (n))
204 # define strrchr rindex
205 /* memset is only used for zero here, but let's be paranoid. */
206 # define memset(s, better_be_zero, n) \
207 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
208 #endif /* Not ANSI_STRING. */
210 #if !defined HAVE_STRCOLL && !defined _LIBC
211 # define strcoll strcmp
214 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
215 # define HAVE_MEMPCPY 1
217 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
220 #ifndef __GNU_LIBRARY__
234 /* These casts are the for sake of the broken Ultrix compiler,
235 which warns of illegal pointer combinations otherwise. */
237 return (char *) malloc (n
);
238 return (char *) realloc (p
, n
);
240 # define realloc my_realloc
242 #endif /* __GNU_LIBRARY__ */
245 #if !defined __alloca && !defined __GNU_LIBRARY__
249 # define alloca(n) __builtin_alloca (n)
250 # else /* Not GCC. */
251 # ifdef HAVE_ALLOCA_H
253 # else /* Not HAVE_ALLOCA_H. */
258 extern char *alloca ();
259 # endif /* WINDOWS32 */
260 # endif /* Not _AIX. */
261 # endif /* sparc or HAVE_ALLOCA_H. */
264 # define __alloca alloca
268 #ifndef __GNU_LIBRARY__
270 # ifdef STAT_MACROS_BROKEN
274 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
280 # define strdup(str) __strdup (str)
281 # define sysconf(id) __sysconf (id)
282 # define closedir(dir) __closedir (dir)
283 # define opendir(name) __opendir (name)
284 # define readdir(str) __readdir64 (str)
285 # define getpwnam_r(name, bufp, buf, len, res) \
286 __getpwnam_r (name, bufp, buf, len, res)
288 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
290 # define HAVE_STAT64 1
294 # define __stat64(fname, buf) __stat (fname, buf)
295 /* This is the variable name we are using. */
299 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
301 # define size_t unsigned int
304 /* Some system header files erroneously define these.
305 We want our own definitions from <fnmatch.h> to take precedence. */
306 #ifndef __GNU_LIBRARY__
313 /* Some system header files erroneously define these.
314 We want our own definitions from <glob.h> to take precedence. */
315 #ifndef __GNU_LIBRARY__
322 # undef GLOB_NOESCAPE
327 #ifdef HAVE_GETLOGIN_R
328 extern int getlogin_r
__P ((char *, size_t));
330 extern char *getlogin
__P ((void));
334 #if __GNUC__ - 0 >= 2
337 const char *next_brace_sub
__P ((const char *begin
));
339 #endif /* GLOB_ONLY_P */
341 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
343 int (*errfunc
) (const char *, int),
346 #if !defined _LIBC || !defined GLOB_ONLY_P
347 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
348 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
351 /* Find the end of the sub-pattern in a brace expression. We define
352 this as an inline function if the compiler permits. */
354 #if __GNUC__ - 0 >= 2
361 unsigned int depth
= 0;
362 while (*cp
!= '\0' && (*cp
!= '}' || depth
--) && (*cp
!= ',' || depth
))
365 return *cp
!= '\0' ? cp
: NULL
;
368 #endif /* !GLOB_ONLY_P */
370 /* Do glob searching for PATTERN, placing results in PGLOB.
371 The bits defined above may be set in FLAGS.
372 If a directory cannot be opened or read and ERRFUNC is not nil,
373 it is called with the pathname that caused the error, and the
374 `errno' value from the failing call; if it returns non-zero
375 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
376 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
377 Otherwise, `glob' returns zero. */
379 glob (pattern
, flags
, errfunc
, pglob
)
382 int (*errfunc
) __P ((const char *, int));
385 const char *filename
;
391 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
393 __set_errno (EINVAL
);
397 if (!(flags
& GLOB_DOOFFS
))
398 /* Have to do this so `globfree' knows where to start freeing. It
399 also makes all the code that uses gl_offs simpler. */
402 if (flags
& GLOB_BRACE
)
404 const char *begin
= strchr (pattern
, '{');
407 /* Allocate working buffer large enough for our work. Note that
408 we have at least an opening and closing brace. */
416 char onealt
[strlen (pattern
) - 1];
418 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
421 if (!(flags
& GLOB_APPEND
))
427 /* We know the prefix for all sub-patterns. */
429 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
431 memcpy (onealt
, pattern
, begin
- pattern
);
432 alt_start
= &onealt
[begin
- pattern
];
435 /* Find the first sub-pattern and at the same time find the
436 rest after the closing brace. */
437 next
= next_brace_sub (begin
+ 1);
440 /* It is an illegal expression. */
444 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
447 /* Now find the end of the whole brace expression. */
451 rest
= next_brace_sub (rest
+ 1);
454 /* It is an illegal expression. */
458 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
461 /* Please note that we now can be sure the brace expression
463 rest_len
= strlen (++rest
) + 1;
465 /* We have a brace expression. BEGIN points to the opening {,
466 NEXT points past the terminator of the first element, and END
467 points past the final }. We will accumulate result names from
468 recursive runs for each brace alternative in the buffer using
471 if (!(flags
& GLOB_APPEND
))
473 /* This call is to set a new vector, so clear out the
474 vector so we can append to it. */
476 pglob
->gl_pathv
= NULL
;
478 firstc
= pglob
->gl_pathc
;
485 /* Construct the new glob expression. */
487 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
489 memcpy (alt_start
, p
, next
- p
);
490 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
493 result
= glob (onealt
,
494 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
495 | GLOB_APPEND
), errfunc
, pglob
);
497 /* If we got an error, return it. */
498 if (result
&& result
!= GLOB_NOMATCH
)
503 if (!(flags
& GLOB_APPEND
))
509 /* We saw the last entry. */
513 next
= next_brace_sub (p
);
514 assert (next
!= NULL
);
521 if (pglob
->gl_pathc
!= firstc
)
522 /* We found some entries. */
524 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
529 /* Find the filename. */
530 filename
= strrchr (pattern
, '/');
531 #if defined __MSDOS__ || defined WINDOWS32
532 /* The case of "d:pattern". Since `:' is not allowed in
533 file names, we can safely assume that wherever it
534 happens in pattern, it signals the filename part. This
535 is so we could some day support patterns like "[a-z]:foo". */
536 if (filename
== NULL
)
537 filename
= strchr (pattern
, ':');
538 #endif /* __MSDOS__ || WINDOWS32 */
539 if (filename
== NULL
)
541 /* This can mean two things: a simple name or "~name". The latter
542 case is nothing but a notation for a directory. */
543 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
546 dirlen
= strlen (pattern
);
548 /* Set FILENAME to NULL as a special flag. This is ugly but
549 other solutions would require much more code. We test for
550 this special case below. */
564 else if (filename
== pattern
)
574 dirlen
= filename
- pattern
;
575 #if defined __MSDOS__ || defined WINDOWS32
577 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
582 drive_spec
= (char *) __alloca (dirlen
+ 1);
584 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
586 memcpy (drive_spec
, pattern
, dirlen
);
587 drive_spec
[dirlen
] = '\0';
589 /* For now, disallow wildcards in the drive spec, to
590 prevent infinite recursion in glob. */
591 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
593 /* If this is "d:pattern", we need to copy `:' to DIRNAME
594 as well. If it's "d:/pattern", don't remove the slash
595 from "d:/", since "d:" and "d:/" are not the same.*/
598 newp
= (char *) __alloca (dirlen
+ 1);
600 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
602 memcpy (newp
, pattern
, dirlen
);
608 if (filename
[0] == '\0'
609 #if defined __MSDOS__ || defined WINDOWS32
610 && dirname
[dirlen
- 1] != ':'
611 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
612 || dirname
[dirlen
- 1] != '/')
615 /* "pattern/". Expand "pattern", appending slashes. */
617 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
619 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
620 | (flags
& GLOB_MARK
));
625 if (!(flags
& GLOB_APPEND
))
628 if (!(flags
& GLOB_DOOFFS
))
629 pglob
->gl_pathv
= NULL
;
633 pglob
->gl_pathv
= (char **) malloc ((pglob
->gl_offs
+ 1)
635 if (pglob
->gl_pathv
== NULL
)
638 for (i
= 0; i
<= pglob
->gl_offs
; ++i
)
639 pglob
->gl_pathv
[i
] = NULL
;
643 oldcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
646 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
648 if (dirname
[1] == '\0' || dirname
[1] == '/')
650 /* Look up home directory. */
651 const char *home_dir
= getenv ("HOME");
653 if (home_dir
== NULL
|| home_dir
[0] == '\0')
657 if (home_dir
== NULL
|| home_dir
[0] == '\0')
658 home_dir
= "c:/users/default"; /* poor default */
660 if (home_dir
== NULL
|| home_dir
[0] == '\0')
664 # if defined HAVE_GETLOGIN_R || defined _LIBC
665 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
668 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
671 name
= (char *) __alloca (buflen
);
673 success
= getlogin_r (name
, buflen
) >= 0;
675 success
= (name
= getlogin ()) != NULL
;
680 # if defined HAVE_GETPWNAM_R || defined _LIBC
681 long int pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
687 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
688 Try a moderate value. */
690 pwtmpbuf
= (char *) __alloca (pwbuflen
);
692 while (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
701 pwtmpbuf
= (char *) __alloca (pwbuflen
);
708 home_dir
= p
->pw_dir
;
711 if (home_dir
== NULL
|| home_dir
[0] == '\0')
713 if (flags
& GLOB_TILDE_CHECK
)
716 home_dir
= "~"; /* No luck. */
718 # endif /* WINDOWS32 */
720 /* Now construct the full directory. */
721 if (dirname
[1] == '\0')
726 size_t home_len
= strlen (home_dir
);
727 newp
= (char *) __alloca (home_len
+ dirlen
);
729 mempcpy (mempcpy (newp
, home_dir
, home_len
),
730 &dirname
[1], dirlen
);
732 memcpy (newp
, home_dir
, home_len
);
733 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
738 # if !defined _AMIGA && !defined WINDOWS32
741 char *end_name
= strchr (dirname
, '/');
742 const char *user_name
;
743 const char *home_dir
;
745 if (end_name
== NULL
)
746 user_name
= dirname
+ 1;
750 newp
= (char *) __alloca (end_name
- dirname
);
752 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
755 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
756 newp
[end_name
- dirname
- 1] = '\0';
761 /* Look up specific user's home directory. */
764 # if defined HAVE_GETPWNAM_R || defined _LIBC
765 long int buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
771 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
774 pwtmpbuf
= (char *) __alloca (buflen
);
776 while (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) != 0)
784 pwtmpbuf
= __alloca (buflen
);
788 p
= getpwnam (user_name
);
791 home_dir
= p
->pw_dir
;
795 /* If we found a home directory use this. */
796 if (home_dir
!= NULL
)
799 size_t home_len
= strlen (home_dir
);
800 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
801 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
803 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
804 end_name
, rest_len
)) = '\0';
806 memcpy (newp
, home_dir
, home_len
);
807 memcpy (&newp
[home_len
], end_name
, rest_len
);
808 newp
[home_len
+ rest_len
] = '\0';
813 if (flags
& GLOB_TILDE_CHECK
)
814 /* We have to regard it as an error if we cannot find the
818 # endif /* Not Amiga && not WINDOWS32. */
820 #endif /* Not VMS. */
822 /* Now test whether we looked for "~" or "~NAME". In this case we
823 can give the answer now. */
824 if (filename
== NULL
)
831 /* Return the directory if we don't check for error or if it exists. */
832 if ((flags
& GLOB_NOCHECK
)
833 || (((flags
& GLOB_ALTDIRFUNC
)
834 ? ((*pglob
->gl_stat
) (dirname
, &st
) == 0
835 && S_ISDIR (st
.st_mode
))
836 : (__stat64 (dirname
, &st64
) == 0 && S_ISDIR (st64
.st_mode
)))))
838 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
841 = (char **) realloc (pglob
->gl_pathv
,
842 (newcount
+ 1 + 1) * sizeof (char *));
843 if (pglob
->gl_pathv
== NULL
)
846 #if defined HAVE_STRDUP || defined _LIBC
847 pglob
->gl_pathv
[newcount
] = strdup (dirname
);
850 size_t len
= strlen (dirname
) + 1;
851 char *dircopy
= malloc (len
);
853 pglob
->gl_pathv
[newcount
] = memcpy (dircopy
, dirname
, len
);
856 if (pglob
->gl_pathv
[newcount
] == NULL
)
858 free (pglob
->gl_pathv
);
861 pglob
->gl_pathv
[++newcount
] = NULL
;
863 pglob
->gl_flags
= flags
;
872 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
874 /* The directory name contains metacharacters, so we
875 have to glob for the directory, and then glob for
876 the pattern in each directory found. */
880 if ((flags
& GLOB_ALTDIRFUNC
) != 0)
882 /* Use the alternative access functions also in the recursive
884 dirs
.gl_opendir
= pglob
->gl_opendir
;
885 dirs
.gl_readdir
= pglob
->gl_readdir
;
886 dirs
.gl_closedir
= pglob
->gl_closedir
;
887 dirs
.gl_stat
= pglob
->gl_stat
;
888 dirs
.gl_lstat
= pglob
->gl_lstat
;
891 status
= glob (dirname
,
892 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
894 | GLOB_NOSORT
| GLOB_ONLYDIR
),
899 /* We have successfully globbed the preceding directory name.
900 For each name we found, call glob_in_dir on it and FILENAME,
901 appending the results to PGLOB. */
902 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
908 /* Make globbing interruptible in the bash shell. */
909 extern int interrupt_state
;
919 old_pathc
= pglob
->gl_pathc
;
920 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
921 ((flags
| GLOB_APPEND
) & ~GLOB_NOCHECK
),
923 if (status
== GLOB_NOMATCH
)
924 /* No matches in this directory. Try the next. */
934 /* Stick the directory on the front of each name. */
935 if (prefix_array (dirs
.gl_pathv
[i
],
936 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
937 pglob
->gl_pathc
- old_pathc
))
945 flags
|= GLOB_MAGCHAR
;
947 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
948 But if we have not found any matching entry and the GLOB_NOCHECK
949 flag was set we must return the list consisting of the disrectory
950 names followed by the filename. */
951 if (pglob
->gl_pathc
+ pglob
->gl_offs
== oldcount
)
954 if (flags
& GLOB_NOCHECK
)
956 size_t filename_len
= strlen (filename
) + 1;
958 int newcount
= pglob
->gl_pathc
+ pglob
->gl_offs
;
964 /* This is an pessimistic guess about the size. */
966 = (char **) realloc (pglob
->gl_pathv
,
967 (newcount
+ dirs
.gl_pathc
+ 1)
969 if (pglob
->gl_pathv
== NULL
)
975 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
977 const char *dir
= dirs
.gl_pathv
[i
];
978 size_t dir_len
= strlen (dir
);
980 /* First check whether this really is a directory. */
981 if (((flags
& GLOB_ALTDIRFUNC
)
982 ? ((*pglob
->gl_stat
) (dir
, &st
) != 0
983 || !S_ISDIR (st
.st_mode
))
984 : (__stat64 (dir
, &st64
) != 0
985 || !S_ISDIR (st64
.st_mode
))))
986 /* No directory, ignore this entry. */
989 pglob
->gl_pathv
[newcount
] = malloc (dir_len
+ 1
991 if (pglob
->gl_pathv
[newcount
] == NULL
)
999 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[newcount
],
1002 filename
, filename_len
);
1004 memcpy (pglob
->gl_pathv
[newcount
], dir
, dir_len
);
1005 pglob
->gl_pathv
[newcount
][dir_len
] = '/';
1006 memcpy (&pglob
->gl_pathv
[newcount
][dir_len
+ 1],
1007 filename
, filename_len
);
1013 pglob
->gl_pathv
[newcount
] = NULL
;
1014 pglob
->gl_flags
= flags
;
1016 /* Now we know how large the gl_pathv vector must be. */
1017 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
1019 * sizeof (char *)));
1020 if (new_pathv
!= NULL
)
1021 pglob
->gl_pathv
= new_pathv
;
1026 return GLOB_NOMATCH
;
1034 int old_pathc
= pglob
->gl_pathc
;
1036 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
1042 /* Stick the directory on the front of each name. */
1043 if (prefix_array (dirname
,
1044 &pglob
->gl_pathv
[old_pathc
+ pglob
->gl_offs
],
1045 pglob
->gl_pathc
- old_pathc
))
1048 return GLOB_NOSPACE
;
1053 if (flags
& GLOB_MARK
)
1055 /* Append slashes to directory names. */
1062 for (i
= oldcount
; i
< pglob
->gl_pathc
+ pglob
->gl_offs
; ++i
)
1063 if (((flags
& GLOB_ALTDIRFUNC
)
1064 ? ((*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
) == 0
1065 && S_ISDIR (st
.st_mode
))
1066 : (__stat64 (pglob
->gl_pathv
[i
], &st64
) == 0
1067 && S_ISDIR (st64
.st_mode
))))
1069 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
1070 char *new = realloc (pglob
->gl_pathv
[i
], len
);
1074 return GLOB_NOSPACE
;
1076 strcpy (&new[len
- 2], "/");
1077 pglob
->gl_pathv
[i
] = new;
1081 if (!(flags
& GLOB_NOSORT
))
1083 /* Sort the vector. */
1084 qsort ((__ptr_t
) &pglob
->gl_pathv
[oldcount
],
1085 pglob
->gl_pathc
+ pglob
->gl_offs
- oldcount
,
1086 sizeof (char *), collated_compare
);
1093 #if !defined _LIBC || !defined GLOB_ONLY_P
1095 /* Free storage allocated in PGLOB by a previous `glob' call. */
1098 register glob_t
*pglob
;
1100 if (pglob
->gl_pathv
!= NULL
)
1103 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1104 if (pglob
->gl_pathv
[pglob
->gl_offs
+ i
] != NULL
)
1105 free ((__ptr_t
) pglob
->gl_pathv
[pglob
->gl_offs
+ i
]);
1106 free ((__ptr_t
) pglob
->gl_pathv
);
1111 /* Do a collated comparison of A and B. */
1113 collated_compare (a
, b
)
1117 const char *const s1
= *(const char *const * const) a
;
1118 const char *const s2
= *(const char *const * const) b
;
1126 return strcoll (s1
, s2
);
1130 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1131 elements in place. Return nonzero if out of memory, zero if successful.
1132 A slash is inserted between DIRNAME and each elt of ARRAY,
1133 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1135 prefix_array (dirname
, array
, n
)
1136 const char *dirname
;
1141 size_t dirlen
= strlen (dirname
);
1142 #if defined __MSDOS__ || defined WINDOWS32
1144 # define DIRSEP_CHAR sep_char
1146 # define DIRSEP_CHAR '/'
1149 if (dirlen
== 1 && dirname
[0] == '/')
1150 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1151 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1153 #if defined __MSDOS__ || defined WINDOWS32
1154 else if (dirlen
> 1)
1156 if (dirname
[dirlen
- 1] == '/' && dirname
[dirlen
- 2] == ':')
1157 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1159 else if (dirname
[dirlen
- 1] == ':')
1161 /* DIRNAME is "d:". Use `:' instead of `/'. */
1168 for (i
= 0; i
< n
; ++i
)
1170 size_t eltlen
= strlen (array
[i
]) + 1;
1171 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1175 free ((__ptr_t
) array
[--i
]);
1181 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1182 *endp
++ = DIRSEP_CHAR
;
1183 mempcpy (endp
, array
[i
], eltlen
);
1186 memcpy (new, dirname
, dirlen
);
1187 new[dirlen
] = DIRSEP_CHAR
;
1188 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1190 free ((__ptr_t
) array
[i
]);
1198 /* We must not compile this function twice. */
1199 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1200 /* Return nonzero if PATTERN contains any metacharacters.
1201 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1203 __glob_pattern_p (pattern
, quote
)
1204 const char *pattern
;
1207 register const char *p
;
1210 for (p
= pattern
; *p
!= '\0'; ++p
)
1218 if (quote
&& p
[1] != '\0')
1235 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1239 #endif /* !GLOB_ONLY_P */
1242 /* Like `glob', but PATTERN is a final pathname component,
1243 and matches are searched for in DIRECTORY.
1244 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1245 The GLOB_APPEND flag is assumed to be set (always appends). */
1247 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1248 const char *pattern
;
1249 const char *directory
;
1251 int (*errfunc
) __P ((const char *, int));
1254 __ptr_t stream
= NULL
;
1257 struct globlink
*next
;
1260 struct globlink
*names
= NULL
;
1265 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1266 if (meta
== 0 && (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
1268 /* We need not do any tests. The PATTERN contains no meta
1269 characters and we must not return an error therefore the
1270 result will always contain exactly one name. */
1271 flags
|= GLOB_NOCHECK
;
1274 else if (meta
== 0 &&
1275 ((flags
& GLOB_NOESCAPE
) || strchr(pattern
, '\\') == NULL
))
1277 /* Since we use the normal file functions we can also use stat()
1278 to verify the file is there. */
1283 size_t patlen
= strlen (pattern
);
1284 size_t dirlen
= strlen (directory
);
1285 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1287 # ifdef HAVE_MEMPCPY
1288 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1290 pattern
, patlen
+ 1);
1292 memcpy (fullname
, directory
, dirlen
);
1293 fullname
[dirlen
] = '/';
1294 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1296 if (((flags
& GLOB_ALTDIRFUNC
)
1297 ? (*pglob
->gl_stat
) (fullname
, &st
)
1298 : __stat64 (fullname
, &st64
)) == 0)
1299 /* We found this file to be existing. Now tell the rest
1300 of the function to copy this name into the result. */
1301 flags
|= GLOB_NOCHECK
;
1307 if (pattern
[0] == '\0')
1309 /* This is a special case for matching directories like in
1311 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1312 names
->name
= (char *) malloc (1);
1313 if (names
->name
== NULL
)
1315 names
->name
[0] = '\0';
1322 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1323 ? (*pglob
->gl_opendir
) (directory
)
1324 : (__ptr_t
) opendir (directory
));
1327 if (errno
!= ENOTDIR
1328 && ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1329 || (flags
& GLOB_ERR
)))
1330 return GLOB_ABORTED
;
1336 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1337 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1338 #if defined _AMIGA || defined VMS
1343 flags
|= GLOB_MAGCHAR
;
1349 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
1351 struct dirent64 d64
;
1353 if (flags
& GLOB_ALTDIRFUNC
)
1355 struct dirent
*d32
= (*pglob
->gl_readdir
) (stream
);
1358 CONVERT_DIRENT_DIRENT64 (&d64
, d32
);
1365 d
= __readdir64 ((DIR *) stream
);
1367 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1368 ? ((struct dirent
*)
1369 (*pglob
->gl_readdir
) (stream
))
1370 : __readdir ((DIR *) stream
));
1374 if (! REAL_DIR_ENTRY (d
))
1378 /* If we shall match only directories use the information
1379 provided by the dirent call if possible. */
1380 if ((flags
& GLOB_ONLYDIR
)
1381 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1387 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1389 struct globlink
*new = (struct globlink
*)
1390 __alloca (sizeof (struct globlink
));
1392 new->name
= (char *) malloc (len
+ 1);
1393 if (new->name
== NULL
)
1396 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1399 memcpy ((__ptr_t
) new->name
, name
, len
);
1400 new->name
[len
] = '\0';
1411 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1413 size_t len
= strlen (pattern
);
1415 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1417 names
->name
= (char *) malloc (len
+ 1);
1418 if (names
->name
== NULL
)
1421 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1423 memcpy (names
->name
, pattern
, len
);
1424 names
->name
[len
] = '\0';
1431 = (char **) realloc (pglob
->gl_pathv
,
1432 (pglob
->gl_pathc
+ pglob
->gl_offs
+ nfound
+ 1)
1434 if (pglob
->gl_pathv
== NULL
)
1437 for (; names
!= NULL
; names
= names
->next
)
1438 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = names
->name
;
1439 pglob
->gl_pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
1441 pglob
->gl_flags
= flags
;
1447 if (flags
& GLOB_ALTDIRFUNC
)
1448 (*pglob
->gl_closedir
) (stream
);
1450 closedir ((DIR *) stream
);
1454 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1459 if (flags
& GLOB_ALTDIRFUNC
)
1460 (*pglob
->gl_closedir
) (stream
);
1462 closedir ((DIR *) stream
);
1465 while (names
!= NULL
)
1467 if (names
->name
!= NULL
)
1468 free ((__ptr_t
) names
->name
);
1469 names
= names
->next
;
1471 return GLOB_NOSPACE
;
1474 #endif /* Not ELIDE_CODE. */