1 /* Copyright (C) 1991-2002,2003,2004,2005,2006 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
26 #include <sys/types.h>
30 /* Outcomment the following line for production quality code. */
31 /* #define NDEBUG 1 */
34 #include <stdio.h> /* Needed on stupid SunOS for assert. */
36 #if !defined _LIBC || !defined GLOB_ONLY_P
37 #if defined HAVE_UNISTD_H || defined _LIBC
40 # ifdef _POSIX_VERSION
50 # define __set_errno(val) errno = (val)
53 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
55 # define NAMLEN(dirent) strlen((dirent)->d_name)
57 # define dirent direct
58 # define NAMLEN(dirent) (dirent)->d_namlen
59 # ifdef HAVE_SYS_NDIR_H
60 # include <sys/ndir.h>
62 # ifdef HAVE_SYS_DIR_H
70 # endif /* HAVE_VMSDIR_H */
74 /* In GNU systems, <dirent.h> defines this macro for us. */
77 # define NAMLEN(d) _D_NAMLEN(d)
80 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
81 if the `d_type' member for `struct dirent' is available.
82 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
83 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
84 /* True if the directory entry D must be of type T. */
85 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
87 /* True if the directory entry D might be a symbolic link. */
88 # define DIRENT_MIGHT_BE_SYMLINK(d) \
89 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
91 /* True if the directory entry D might be a directory. */
92 # define DIRENT_MIGHT_BE_DIR(d) \
93 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
95 #else /* !HAVE_D_TYPE */
96 # define DIRENT_MUST_BE(d, t) false
97 # define DIRENT_MIGHT_BE_SYMLINK(d) true
98 # define DIRENT_MIGHT_BE_DIR(d) true
99 #endif /* HAVE_D_TYPE */
101 /* If the system has the `struct dirent64' type we use it internally. */
102 #if defined _LIBC && !defined COMPILE_GLOB64
103 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
104 # define CONVERT_D_NAMLEN(d64, d32)
106 # define CONVERT_D_NAMLEN(d64, d32) \
107 (d64)->d_namlen = (d32)->d_namlen;
110 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
111 # define CONVERT_D_INO(d64, d32)
113 # define CONVERT_D_INO(d64, d32) \
114 (d64)->d_ino = (d32)->d_ino;
117 # ifdef _DIRENT_HAVE_D_TYPE
118 # define CONVERT_D_TYPE(d64, d32) \
119 (d64)->d_type = (d32)->d_type;
121 # define CONVERT_D_TYPE(d64, d32)
124 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
125 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
126 CONVERT_D_NAMLEN (d64, d32) \
127 CONVERT_D_INO (d64, d32) \
128 CONVERT_D_TYPE (d64, d32)
132 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
133 /* Posix does not require that the d_ino field be present, and some
134 systems do not provide it. */
135 # define REAL_DIR_ENTRY(dp) 1
137 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
143 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
146 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
153 # define strdup(str) __strdup (str)
154 # define sysconf(id) __sysconf (id)
155 # define closedir(dir) __closedir (dir)
156 # define opendir(name) __opendir (name)
157 # define readdir(str) __readdir64 (str)
158 # define getpwnam_r(name, bufp, buf, len, res) \
159 __getpwnam_r (name, bufp, buf, len, res)
161 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
163 # define struct_stat64 struct stat64
165 # include "getlogin_r.h"
166 # include "mempcpy.h"
167 # include "stat-macros.h"
169 # define __stat64(fname, buf) stat (fname, buf)
170 # define struct_stat64 struct stat
171 # define __stat(fname, buf) stat (fname, buf)
172 # define __alloca alloca
173 # define __readdir readdir
174 # define __readdir64 readdir64
175 # define __glob_pattern_p glob_pattern_p
180 #ifdef _SC_GETPW_R_SIZE_MAX
181 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
183 # define GETPW_R_SIZE_MAX() (-1)
185 #ifdef _SC_LOGIN_NAME_MAX
186 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
188 # define GET_LOGIN_NAME_MAX() (-1)
191 static const char *next_brace_sub (const char *begin, int flags) __THROW;
193 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
195 static int glob_in_dir (const char *pattern, const char *directory,
196 int flags, int (*errfunc) (const char *, int),
199 #if !defined _LIBC || !defined GLOB_ONLY_P
200 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
201 static int collated_compare (const void *, const void *) __THROW;
204 /* Find the end of the sub-pattern in a brace expression. */
206 next_brace_sub (const char *cp, int flags)
208 unsigned int depth = 0;
210 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
218 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
225 return *cp != '\0' ? cp : NULL;
228 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
230 /* Do glob searching for PATTERN, placing results in PGLOB.
231 The bits defined above may be set in FLAGS.
232 If a directory cannot be opened or read and ERRFUNC is not nil,
233 it is called with the pathname that caused the error, and the
234 `errno' value from the failing call; if it returns non-zero
235 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
236 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
237 Otherwise, `glob' returns zero. */
239 #ifdef GLOB_ATTRIBUTE
242 glob (pattern, flags, errfunc, pglob)
245 int (*errfunc) (const char *, int);
248 const char *filename;
254 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
256 __set_errno (EINVAL);
260 if (!(flags & GLOB_DOOFFS))
261 /* Have to do this so `globfree' knows where to start freeing. It
262 also makes all the code that uses gl_offs simpler. */
265 if (flags & GLOB_BRACE)
269 if (flags & GLOB_NOESCAPE)
270 begin = strchr (pattern, '{');
282 if (*begin == '\\' && begin[1] != '\0')
284 else if (*begin == '{')
293 /* Allocate working buffer large enough for our work. Note that
294 we have at least an opening and closing brace. */
302 char onealt[strlen (pattern) - 1];
304 char *onealt = (char *) malloc (strlen (pattern) - 1);
307 if (!(flags & GLOB_APPEND))
310 pglob->gl_pathv = NULL;
316 /* We know the prefix for all sub-patterns. */
317 alt_start = mempcpy (onealt, pattern, begin - pattern);
319 /* Find the first sub-pattern and at the same time find the
320 rest after the closing brace. */
321 next = next_brace_sub (begin + 1, flags);
324 /* It is an illegal expression. */
328 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
331 /* Now find the end of the whole brace expression. */
335 rest = next_brace_sub (rest + 1, flags);
338 /* It is an illegal expression. */
342 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
345 /* Please note that we now can be sure the brace expression
347 rest_len = strlen (++rest) + 1;
349 /* We have a brace expression. BEGIN points to the opening {,
350 NEXT points past the terminator of the first element, and END
351 points past the final }. We will accumulate result names from
352 recursive runs for each brace alternative in the buffer using
355 if (!(flags & GLOB_APPEND))
357 /* This call is to set a new vector, so clear out the
358 vector so we can append to it. */
360 pglob->gl_pathv = NULL;
362 firstc = pglob->gl_pathc;
369 /* Construct the new glob expression. */
370 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
372 result = glob (onealt,
373 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
374 | GLOB_APPEND), errfunc, pglob);
376 /* If we got an error, return it. */
377 if (result && result != GLOB_NOMATCH)
382 if (!(flags & GLOB_APPEND))
391 /* We saw the last entry. */
395 next = next_brace_sub (p, flags);
396 assert (next != NULL);
403 if (pglob->gl_pathc != firstc)
404 /* We found some entries. */
406 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
411 /* Find the filename. */
412 filename = strrchr (pattern, '/');
413 #if defined __MSDOS__ || defined WINDOWS32
414 /* The case of "d:pattern". Since `:' is not allowed in
415 file names, we can safely assume that wherever it
416 happens in pattern, it signals the filename part. This
417 is so we could some day support patterns like "[a-z]:foo". */
418 if (filename == NULL)
419 filename = strchr (pattern, ':');
420 #endif /* __MSDOS__ || WINDOWS32 */
421 if (filename == NULL)
423 /* This can mean two things: a simple name or "~name". The latter
424 case is nothing but a notation for a directory. */
425 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
428 dirlen = strlen (pattern);
430 /* Set FILENAME to NULL as a special flag. This is ugly but
431 other solutions would require much more code. We test for
432 this special case below. */
446 else if (filename == pattern)
456 dirlen = filename - pattern;
457 #if defined __MSDOS__ || defined WINDOWS32
459 || (filename > pattern + 1 && filename[-1] == ':'))
464 drive_spec = (char *) __alloca (dirlen + 1);
465 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
466 /* For now, disallow wildcards in the drive spec, to
467 prevent infinite recursion in glob. */
468 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
470 /* If this is "d:pattern", we need to copy `:' to DIRNAME
471 as well. If it's "d:/pattern", don't remove the slash
472 from "d:/", since "d:" and "d:/" are not the same.*/
475 newp = (char *) __alloca (dirlen + 1);
476 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
480 if (filename[0] == '\0'
481 #if defined __MSDOS__ || defined WINDOWS32
482 && dirname[dirlen - 1] != ':'
483 && (dirlen < 3 || dirname[dirlen - 2] != ':'
484 || dirname[dirlen - 1] != '/')
487 /* "pattern/". Expand "pattern", appending slashes. */
489 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
491 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
492 | (flags & GLOB_MARK));
497 if (!(flags & GLOB_APPEND))
500 if (!(flags & GLOB_DOOFFS))
501 pglob->gl_pathv = NULL;
505 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
507 if (pglob->gl_pathv == NULL)
510 for (i = 0; i <= pglob->gl_offs; ++i)
511 pglob->gl_pathv[i] = NULL;
515 oldcount = pglob->gl_pathc + pglob->gl_offs;
518 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
520 if (dirname[1] == '\0' || dirname[1] == '/')
522 /* Look up home directory. */
523 const char *home_dir = getenv ("HOME");
525 if (home_dir == NULL || home_dir[0] == '\0')
529 if (home_dir == NULL || home_dir[0] == '\0')
530 home_dir = "c:/users/default"; /* poor default */
532 if (home_dir == NULL || home_dir[0] == '\0')
536 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
539 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
542 name = (char *) __alloca (buflen);
544 success = getlogin_r (name, buflen) == 0;
548 # if defined HAVE_GETPWNAM_R || defined _LIBC
549 long int pwbuflen = GETPW_R_SIZE_MAX ();
556 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
557 Try a moderate value. */
560 pwtmpbuf = (char *) __alloca (pwbuflen);
562 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
571 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
575 pwtmpbuf = (char *) __alloca (pwbuflen);
583 home_dir = p->pw_dir;
586 if (home_dir == NULL || home_dir[0] == '\0')
588 if (flags & GLOB_TILDE_CHECK)
591 home_dir = "~"; /* No luck. */
593 # endif /* WINDOWS32 */
595 /* Now construct the full directory. */
596 if (dirname[1] == '\0')
601 size_t home_len = strlen (home_dir);
602 newp = (char *) __alloca (home_len + dirlen);
603 mempcpy (mempcpy (newp, home_dir, home_len),
604 &dirname[1], dirlen);
608 # if !defined _AMIGA && !defined WINDOWS32
611 char *end_name = strchr (dirname, '/');
612 const char *user_name;
613 const char *home_dir;
615 if (end_name == NULL)
616 user_name = dirname + 1;
620 newp = (char *) __alloca (end_name - dirname);
621 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
626 /* Look up specific user's home directory. */
629 # if defined HAVE_GETPWNAM_R || defined _LIBC
630 long int buflen = GETPW_R_SIZE_MAX ();
637 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
641 pwtmpbuf = (char *) __alloca (buflen);
643 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
651 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
654 pwtmpbuf = __alloca (buflen);
659 p = getpwnam (user_name);
662 home_dir = p->pw_dir;
666 /* If we found a home directory use this. */
667 if (home_dir != NULL)
670 size_t home_len = strlen (home_dir);
671 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
672 newp = (char *) __alloca (home_len + rest_len + 1);
673 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
674 end_name, rest_len)) = '\0';
678 if (flags & GLOB_TILDE_CHECK)
679 /* We have to regard it as an error if we cannot find the
683 # endif /* Not Amiga && not WINDOWS32. */
685 #endif /* Not VMS. */
687 /* Now test whether we looked for "~" or "~NAME". In this case we
688 can give the answer now. */
689 if (filename == NULL)
694 /* Return the directory if we don't check for error or if it exists. */
695 if ((flags & GLOB_NOCHECK)
696 || (((flags & GLOB_ALTDIRFUNC)
697 ? ((*pglob->gl_stat) (dirname, &st) == 0
698 && S_ISDIR (st.st_mode))
699 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
701 int newcount = pglob->gl_pathc + pglob->gl_offs;
705 = (char **) realloc (pglob->gl_pathv,
706 (newcount + 1 + 1) * sizeof (char *));
707 if (new_gl_pathv == NULL)
710 free (pglob->gl_pathv);
711 pglob->gl_pathv = NULL;
715 pglob->gl_pathv = new_gl_pathv;
717 pglob->gl_pathv[newcount] = strdup (dirname);
718 if (pglob->gl_pathv[newcount] == NULL)
720 pglob->gl_pathv[++newcount] = NULL;
722 pglob->gl_flags = flags;
731 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
733 /* The directory name contains metacharacters, so we
734 have to glob for the directory, and then glob for
735 the pattern in each directory found. */
739 if ((flags & GLOB_ALTDIRFUNC) != 0)
741 /* Use the alternative access functions also in the recursive
743 dirs.gl_opendir = pglob->gl_opendir;
744 dirs.gl_readdir = pglob->gl_readdir;
745 dirs.gl_closedir = pglob->gl_closedir;
746 dirs.gl_stat = pglob->gl_stat;
747 dirs.gl_lstat = pglob->gl_lstat;
750 status = glob (dirname,
751 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
753 | GLOB_NOSORT | GLOB_ONLYDIR),
758 /* We have successfully globbed the preceding directory name.
759 For each name we found, call glob_in_dir on it and FILENAME,
760 appending the results to PGLOB. */
761 for (i = 0; i < dirs.gl_pathc; ++i)
767 /* Make globbing interruptible in the bash shell. */
768 extern int interrupt_state;
778 old_pathc = pglob->gl_pathc;
779 status = glob_in_dir (filename, dirs.gl_pathv[i],
780 ((flags | GLOB_APPEND)
781 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
783 if (status == GLOB_NOMATCH)
784 /* No matches in this directory. Try the next. */
795 /* Stick the directory on the front of each name. */
796 if (prefix_array (dirs.gl_pathv[i],
797 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
798 pglob->gl_pathc - old_pathc))
807 flags |= GLOB_MAGCHAR;
809 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
810 But if we have not found any matching entry and the GLOB_NOCHECK
811 flag was set we must return the input pattern itself. */
812 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
815 if (flags & GLOB_NOCHECK)
817 int newcount = pglob->gl_pathc + pglob->gl_offs;
820 new_gl_pathv = (char **) realloc (pglob->gl_pathv,
823 if (new_gl_pathv == NULL)
828 pglob->gl_pathv = new_gl_pathv;
830 pglob->gl_pathv[newcount] = __strdup (pattern);
831 if (pglob->gl_pathv[newcount] == NULL)
842 pglob->gl_pathv[newcount] = NULL;
843 pglob->gl_flags = flags;
856 int old_pathc = pglob->gl_pathc;
858 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
864 /* Stick the directory on the front of each name. */
865 if (prefix_array (dirname,
866 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
867 pglob->gl_pathc - old_pathc))
876 if (flags & GLOB_MARK)
878 /* Append slashes to directory names. */
883 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
884 if (((flags & GLOB_ALTDIRFUNC)
885 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
886 && S_ISDIR (st.st_mode))
887 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
888 && S_ISDIR (st64.st_mode))))
890 size_t len = strlen (pglob->gl_pathv[i]) + 2;
891 char *new = realloc (pglob->gl_pathv[i], len);
898 strcpy (&new[len - 2], "/");
899 pglob->gl_pathv[i] = new;
903 if (!(flags & GLOB_NOSORT))
905 /* Sort the vector. */
906 qsort (&pglob->gl_pathv[oldcount],
907 pglob->gl_pathc + pglob->gl_offs - oldcount,
908 sizeof (char *), collated_compare);
913 #if defined _LIBC && !defined glob
914 libc_hidden_def (glob)
918 #if !defined _LIBC || !defined GLOB_ONLY_P
920 /* Free storage allocated in PGLOB by a previous `glob' call. */
923 register glob_t *pglob;
925 if (pglob->gl_pathv != NULL)
928 for (i = 0; i < pglob->gl_pathc; ++i)
929 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
930 free (pglob->gl_pathv[pglob->gl_offs + i]);
931 free (pglob->gl_pathv);
932 pglob->gl_pathv = NULL;
935 #if defined _LIBC && !defined globfree
936 libc_hidden_def (globfree)
940 /* Do a collated comparison of A and B. */
942 collated_compare (const void *a, const void *b)
944 const char *const s1 = *(const char *const * const) a;
945 const char *const s2 = *(const char *const * const) b;
953 return strcoll (s1, s2);
957 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
958 elements in place. Return nonzero if out of memory, zero if successful.
959 A slash is inserted between DIRNAME and each elt of ARRAY,
960 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
962 prefix_array (const char *dirname, char **array, size_t n)
965 size_t dirlen = strlen (dirname);
966 #if defined __MSDOS__ || defined WINDOWS32
968 # define DIRSEP_CHAR sep_char
970 # define DIRSEP_CHAR '/'
973 if (dirlen == 1 && dirname[0] == '/')
974 /* DIRNAME is just "/", so normal prepending would get us "//foo".
975 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
977 #if defined __MSDOS__ || defined WINDOWS32
980 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
981 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
983 else if (dirname[dirlen - 1] == ':')
985 /* DIRNAME is "d:". Use `:' instead of `/'. */
992 for (i = 0; i < n; ++i)
994 size_t eltlen = strlen (array[i]) + 1;
995 char *new = (char *) malloc (dirlen + 1 + eltlen);
1004 char *endp = mempcpy (new, dirname, dirlen);
1005 *endp++ = DIRSEP_CHAR;
1006 mempcpy (endp, array[i], eltlen);
1016 /* We must not compile this function twice. */
1017 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1018 /* Return nonzero if PATTERN contains any metacharacters.
1019 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1021 __glob_pattern_p (pattern, quote)
1022 const char *pattern;
1025 register const char *p;
1028 for (p = pattern; *p != '\0'; ++p)
1036 if (quote && p[1] != '\0')
1053 weak_alias (__glob_pattern_p, glob_pattern_p)
1057 #endif /* !GLOB_ONLY_P */
1060 /* We put this in a separate function mainly to allow the memory
1061 allocated with alloca to be recycled. */
1062 #if !defined _LIBC || !defined GLOB_ONLY_P
1064 link_exists_p (const char *dir, size_t dirlen, const char *fname,
1065 glob_t *pglob, int flags)
1067 size_t fnamelen = strlen (fname);
1068 char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1);
1072 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1073 fname, fnamelen + 1);
1075 return (((flags & GLOB_ALTDIRFUNC)
1076 ? (*pglob->gl_stat) (fullname, &st)
1077 : __stat64 (fullname, &st64)) == 0);
1082 /* Like `glob', but PATTERN is a final pathname component,
1083 and matches are searched for in DIRECTORY.
1084 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1085 The GLOB_APPEND flag is assumed to be set (always appends). */
1087 glob_in_dir (const char *pattern, const char *directory, int flags,
1088 int (*errfunc) (const char *, int),
1091 size_t dirlen = strlen (directory);
1092 void *stream = NULL;
1095 struct globlink *next;
1098 struct globlink *names = NULL;
1103 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1104 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1106 /* We need not do any tests. The PATTERN contains no meta
1107 characters and we must not return an error therefore the
1108 result will always contain exactly one name. */
1109 flags |= GLOB_NOCHECK;
1112 else if (meta == 0 &&
1113 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1115 /* Since we use the normal file functions we can also use stat()
1116 to verify the file is there. */
1119 size_t patlen = strlen (pattern);
1120 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1122 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1124 pattern, patlen + 1);
1125 if (((flags & GLOB_ALTDIRFUNC)
1126 ? (*pglob->gl_stat) (fullname, &st)
1127 : __stat64 (fullname, &st64)) == 0)
1128 /* We found this file to be existing. Now tell the rest
1129 of the function to copy this name into the result. */
1130 flags |= GLOB_NOCHECK;
1136 if (pattern[0] == '\0')
1138 /* This is a special case for matching directories like in
1140 names = (struct globlink *) __alloca (sizeof (struct globlink));
1141 names->name = (char *) malloc (1);
1142 if (names->name == NULL)
1144 names->name[0] = '\0';
1151 stream = ((flags & GLOB_ALTDIRFUNC)
1152 ? (*pglob->gl_opendir) (directory)
1153 : opendir (directory));
1156 if (errno != ENOTDIR
1157 && ((errfunc != NULL && (*errfunc) (directory, errno))
1158 || (flags & GLOB_ERR)))
1159 return GLOB_ABORTED;
1165 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1166 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1167 #if defined _AMIGA || defined VMS
1172 flags |= GLOB_MAGCHAR;
1178 #if defined _LIBC && !defined COMPILE_GLOB64
1182 struct dirent64 d64;
1183 char room [offsetof (struct dirent64, d_name[0])
1188 if (flags & GLOB_ALTDIRFUNC)
1190 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1193 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1200 d = __readdir64 (stream);
1202 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1203 ? ((struct dirent *)
1204 (*pglob->gl_readdir) (stream))
1205 : __readdir (stream));
1209 if (! REAL_DIR_ENTRY (d))
1212 /* If we shall match only directories use the information
1213 provided by the dirent call if possible. */
1214 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1219 if (fnmatch (pattern, name, fnm_flags) == 0)
1221 /* If the file we found is a symlink we have to
1222 make sure the target file exists. */
1223 if (!DIRENT_MIGHT_BE_SYMLINK (d)
1224 || link_exists_p (directory, dirlen, name, pglob,
1227 struct globlink *new = (struct globlink *)
1228 __alloca (sizeof (struct globlink));
1230 new->name = (char *) malloc (len + 1);
1231 if (new->name == NULL)
1233 *((char *) mempcpy (new->name, name, len)) = '\0';
1244 if (nfound == 0 && (flags & GLOB_NOCHECK))
1246 size_t len = strlen (pattern);
1248 names = (struct globlink *) __alloca (sizeof (struct globlink));
1250 names->name = (char *) malloc (len + 1);
1251 if (names->name == NULL)
1253 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1258 char **new_gl_pathv;
1261 = (char **) realloc (pglob->gl_pathv,
1262 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1264 if (new_gl_pathv == NULL)
1266 pglob->gl_pathv = new_gl_pathv;
1268 for (; names != NULL; names = names->next)
1269 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1270 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1272 pglob->gl_flags = flags;
1278 if (flags & GLOB_ALTDIRFUNC)
1279 (*pglob->gl_closedir) (stream);
1285 return nfound == 0 ? GLOB_NOMATCH : 0;
1290 if (flags & GLOB_ALTDIRFUNC)
1291 (*pglob->gl_closedir) (stream);
1296 while (names != NULL)
1298 if (names->name != NULL)
1300 names = names->next;
1302 return GLOB_NOSPACE;