2 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* AIX requires this to be the first thing in the file. */
20 #if defined _AIX && !defined __GNUC__
29 #define __alloca alloca
31 #define NAMLEN(_d) NLENGTH(_d)
33 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
34 /* Posix does not require that the d_ino field be present, and some
35 systems do not provide it. */
36 # define REAL_DIR_ENTRY(dp) 1
38 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
43 # define __set_errno(val) errno = (val)
46 /* Outcomment the following line for production quality code. */
47 /* #define NDEBUG 1 */
49 #define GLOB_INTERFACE_VERSION 1
51 static inline const char *next_brace_sub __P ((const char *begin));
52 static int glob_in_dir __P ((const char *pattern, const char *directory,
54 int (*errfunc) (const char *, int),
56 static int prefix_array __P ((const char *prefix, char **array, size_t n));
57 static int collated_compare __P ((const __ptr_t, const __ptr_t));
60 /* Find the end of the sub-pattern in a brace expression. We define
61 this as an inline function if the compiler permits. */
62 static inline const char *
63 next_brace_sub (const char *begin)
65 unsigned int depth = 0;
66 const char *cp = begin;
72 if (*cp != ',' && *cp != '}' && *cp != '\0')
82 while (*cp != '\0' && (*cp != '}' || depth > 0))
89 /* An incorrectly terminated brace expression. */
100 static int __glob_pattern_p (const char *pattern, int quote);
102 /* Do glob searching for PATTERN, placing results in PGLOB.
103 The bits defined above may be set in FLAGS.
104 If a directory cannot be opened or read and ERRFUNC is not nil,
105 it is called with the pathname that caused the error, and the
106 `errno' value from the failing call; if it returns non-zero
107 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
108 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
109 Otherwise, `glob' returns zero. */
111 glob (const char *pattern, int flags,
112 int (*errfunc) __P ((const char *, int)), glob_t *pglob)
114 const char *filename;
120 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
122 __set_errno (EINVAL);
126 if (flags & GLOB_BRACE)
128 const char *begin = strchr (pattern, '{');
131 /* Allocate working buffer large enough for our work. Note that
132 we have at least an opening and closing brace. */
140 char onealt[strlen (pattern) - 1];
142 char *onealt = (char *) xmalloc (strlen (pattern) - 1);
145 if (!(flags & GLOB_APPEND))
151 /* We know the prefix for all sub-patterns. */
153 alt_start = mempcpy (onealt, pattern, begin - pattern);
155 memcpy (onealt, pattern, begin - pattern);
156 alt_start = &onealt[begin - pattern];
159 /* Find the first sub-pattern and at the same time find the
160 rest after the closing brace. */
161 next = next_brace_sub (begin + 1);
164 /* It is an illegal expression. */
168 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
171 /* Now find the end of the whole brace expression. */
175 rest = next_brace_sub (rest + 1);
178 /* It is an illegal expression. */
182 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
185 /* Please note that we now can be sure the brace expression
187 rest_len = strlen (++rest) + 1;
189 /* We have a brace expression. BEGIN points to the opening {,
190 NEXT points past the terminator of the first element, and END
191 points past the final }. We will accumulate result names from
192 recursive runs for each brace alternative in the buffer using
195 if (!(flags & GLOB_APPEND))
197 /* This call is to set a new vector, so clear out the
198 vector so we can append to it. */
200 pglob->gl_pathv = NULL;
202 firstc = pglob->gl_pathc;
209 /* Construct the new glob expression. */
211 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
213 memcpy (alt_start, p, next - p);
214 memcpy (&alt_start[next - p], rest, rest_len);
217 result = glob (onealt,
218 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
219 | GLOB_APPEND), errfunc, pglob);
221 /* If we got an error, return it. */
222 if (result && result != GLOB_NOMATCH)
227 if (!(flags & GLOB_APPEND))
233 /* We saw the last entry. */
237 next = next_brace_sub (p);
238 assert (next != NULL);
245 if (pglob->gl_pathc != firstc)
246 /* We found some entries. */
248 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
253 /* Find the filename. */
254 filename = strrchr (pattern, '/');
255 #if defined __MSDOS__ || defined WINDOWS32
256 /* The case of "d:pattern". Since `:' is not allowed in
257 file names, we can safely assume that wherever it
258 happens in pattern, it signals the filename part. This
259 is so we could some day support patterns like "[a-z]:foo". */
260 if (filename == NULL)
261 filename = strchr (pattern, ':');
262 #endif /* __MSDOS__ || WINDOWS32 */
263 if (filename == NULL)
265 /* This can mean two things: a simple name or "~name". The latter
266 case is nothing but a notation for a directory. */
267 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
270 dirlen = strlen (pattern);
272 /* Set FILENAME to NULL as a special flag. This is ugly but
273 other solutions would require much more code. We test for
274 this special case below. */
288 else if (filename == pattern)
298 dirlen = filename - pattern;
299 #if defined __MSDOS__ || defined WINDOWS32
301 || (filename > pattern + 1 && filename[-1] == ':'))
306 drive_spec = (char *) __alloca (dirlen + 1);
308 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
310 memcpy (drive_spec, pattern, dirlen);
311 drive_spec[dirlen] = '\0';
313 /* For now, disallow wildcards in the drive spec, to
314 prevent infinite recursion in glob. */
315 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
317 /* If this is "d:pattern", we need to copy `:' to DIRNAME
318 as well. If it's "d:/pattern", don't remove the slash
319 from "d:/", since "d:" and "d:/" are not the same.*/
322 newp = (char *) __alloca (dirlen + 1);
324 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
326 memcpy (newp, pattern, dirlen);
332 if (filename[0] == '\0'
333 #if defined __MSDOS__ || defined WINDOWS32
334 && dirname[dirlen - 1] != ':'
335 && (dirlen < 3 || dirname[dirlen - 2] != ':'
336 || dirname[dirlen - 1] != '/')
339 /* "pattern/". Expand "pattern", appending slashes. */
341 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
343 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
344 | (flags & GLOB_MARK));
349 if (!(flags & GLOB_APPEND))
352 pglob->gl_pathv = NULL;
355 oldcount = pglob->gl_pathc;
358 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
360 if (dirname[1] == '\0' || dirname[1] == '/')
362 /* Look up home directory. */
363 const char *home_dir = getenv ("HOME");
365 if (home_dir == NULL || home_dir[0] == '\0')
369 if (home_dir == NULL || home_dir[0] == '\0')
370 home_dir = "c:/users/default"; /* poor default */
372 if (home_dir == NULL || home_dir[0] == '\0')
376 # if defined HAVE_GETLOGIN_R || defined _LIBC
377 size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
380 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
383 name = (char *) __alloca (buflen);
385 success = getlogin_r (name, buflen) >= 0;
387 success = (name = getlogin ()) != NULL;
392 # if defined HAVE_GETPWNAM_R || defined _LIBC
393 size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
399 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
400 Try a moderate value. */
402 pwtmpbuf = (char *) __alloca (pwbuflen);
404 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
413 pwtmpbuf = (char *) __alloca (pwbuflen);
420 home_dir = p->pw_dir;
423 if (home_dir == NULL || home_dir[0] == '\0')
425 if (flags & GLOB_TILDE_CHECK)
428 home_dir = "~"; /* No luck. */
430 # endif /* WINDOWS32 */
432 /* Now construct the full directory. */
433 if (dirname[1] == '\0')
438 size_t home_len = strlen (home_dir);
439 newp = (char *) __alloca (home_len + dirlen);
441 mempcpy (mempcpy (newp, home_dir, home_len),
442 &dirname[1], dirlen);
444 memcpy (newp, home_dir, home_len);
445 memcpy (&newp[home_len], &dirname[1], dirlen);
450 # if !defined _AMIGA && !defined WINDOWS32
453 char *end_name = strchr (dirname, '/');
454 const char *user_name;
455 const char *home_dir;
457 if (end_name == NULL)
458 user_name = dirname + 1;
462 newp = (char *) __alloca (end_name - dirname);
464 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
467 memcpy (newp, dirname + 1, end_name - dirname);
468 newp[end_name - dirname - 1] = '\0';
473 /* Look up specific user's home directory. */
476 # if defined HAVE_GETPWNAM_R || defined _LIBC
477 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
483 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
486 pwtmpbuf = (char *) __alloca (buflen);
488 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
496 pwtmpbuf = __alloca (buflen);
500 p = getpwnam (user_name);
503 home_dir = p->pw_dir;
507 /* If we found a home directory use this. */
508 if (home_dir != NULL)
511 size_t home_len = strlen (home_dir);
512 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
513 newp = (char *) __alloca (home_len + rest_len + 1);
515 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
516 end_name, rest_len)) = '\0';
518 memcpy (newp, home_dir, home_len);
519 memcpy (&newp[home_len], end_name, rest_len);
520 newp[home_len + rest_len] = '\0';
525 if (flags & GLOB_TILDE_CHECK)
526 /* We have to regard it as an error if we cannot find the
530 # endif /* Not Amiga && not WINDOWS32. */
532 #endif /* Not VMS. */
534 /* Now test whether we looked for "~" or "~NAME". In this case we
535 can give the answer now. */
536 if (filename == NULL)
540 /* Return the directory if we don't check for error or if it exists. */
541 if ((flags & GLOB_NOCHECK)
542 || (((flags & GLOB_ALTDIRFUNC)
543 ? (*pglob->gl_stat) (dirname, &st)
544 : __stat (dirname, &st)) == 0
545 && S_ISDIR (st.st_mode)))
548 = (char **) xrealloc (pglob->gl_pathv,
550 ((flags & GLOB_DOOFFS) ?
551 pglob->gl_offs : 0) +
554 if (pglob->gl_pathv == NULL)
557 if (flags & GLOB_DOOFFS)
558 while (pglob->gl_pathc < pglob->gl_offs)
559 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
561 #if defined HAVE_STRDUP || defined _LIBC
562 pglob->gl_pathv[pglob->gl_pathc] = xstrdup (dirname);
565 size_t len = strlen (dirname) + 1;
566 char *dircopy = xmalloc (len);
568 pglob->gl_pathv[pglob->gl_pathc] = memcpy (dircopy, dirname,
572 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
574 free (pglob->gl_pathv);
577 pglob->gl_pathv[++pglob->gl_pathc] = NULL;
578 pglob->gl_flags = flags;
587 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
589 /* The directory name contains metacharacters, so we
590 have to glob for the directory, and then glob for
591 the pattern in each directory found. */
595 if ((flags & GLOB_ALTDIRFUNC) != 0)
597 /* Use the alternative access functions also in the recursive
599 dirs.gl_opendir = pglob->gl_opendir;
600 dirs.gl_readdir = pglob->gl_readdir;
601 dirs.gl_closedir = pglob->gl_closedir;
602 dirs.gl_stat = pglob->gl_stat;
603 dirs.gl_lstat = pglob->gl_lstat;
606 status = glob (dirname,
607 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
609 | GLOB_NOSORT | GLOB_ONLYDIR),
614 /* We have successfully globbed the preceding directory name.
615 For each name we found, call glob_in_dir on it and FILENAME,
616 appending the results to PGLOB. */
617 for (i = 0; i < dirs.gl_pathc; ++i)
623 /* Make globbing interruptible in the bash shell. */
624 extern int interrupt_state;
635 old_pathc = pglob->gl_pathc;
636 status = glob_in_dir (filename, dirs.gl_pathv[i],
637 ((flags | GLOB_APPEND)
638 & ~(GLOB_NOCHECK | GLOB_ERR)),
640 if (status == GLOB_NOMATCH)
641 /* No matches in this directory. Try the next. */
651 /* Stick the directory on the front of each name. */
652 if (prefix_array (dirs.gl_pathv[i],
653 &pglob->gl_pathv[old_pathc],
654 pglob->gl_pathc - old_pathc))
662 flags |= GLOB_MAGCHAR;
664 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
665 But if we have not found any matching entry and thie GLOB_NOCHECK
666 flag was set we must return the list consisting of the disrectory
667 names followed by the filename. */
668 if (pglob->gl_pathc == oldcount)
671 if (flags & GLOB_NOCHECK)
673 size_t filename_len = strlen (filename) + 1;
677 /* This is an pessimistic guess about the size. */
679 = (char **) xrealloc (pglob->gl_pathv,
681 ((flags & GLOB_DOOFFS) ?
682 pglob->gl_offs : 0) +
685 if (pglob->gl_pathv == NULL)
691 if (flags & GLOB_DOOFFS)
692 while (pglob->gl_pathc < pglob->gl_offs)
693 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
695 for (i = 0; i < dirs.gl_pathc; ++i)
697 const char *dir = dirs.gl_pathv[i];
698 size_t dir_len = strlen (dir);
700 /* First check whether this really is a directory. */
701 if (((flags & GLOB_ALTDIRFUNC)
702 ? (*pglob->gl_stat) (dir, &st) : __stat (dir, &st)) != 0
703 || !S_ISDIR (st.st_mode))
704 /* No directory, ignore this entry. */
707 pglob->gl_pathv[pglob->gl_pathc] = xmalloc (dir_len + 1
709 if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
717 mempcpy (mempcpy (mempcpy (pglob->gl_pathv[pglob->gl_pathc],
720 filename, filename_len);
722 memcpy (pglob->gl_pathv[pglob->gl_pathc], dir, dir_len);
723 pglob->gl_pathv[pglob->gl_pathc][dir_len] = '/';
724 memcpy (&pglob->gl_pathv[pglob->gl_pathc][dir_len + 1],
725 filename, filename_len);
730 pglob->gl_pathv[pglob->gl_pathc] = NULL;
731 pglob->gl_flags = flags;
733 /* Now we know how large the gl_pathv vector must be. */
734 new_pathv = (char **) xrealloc (pglob->gl_pathv,
735 ((pglob->gl_pathc + 1)
737 if (new_pathv != NULL)
738 pglob->gl_pathv = new_pathv;
748 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
754 /* Stick the directory on the front of each name. */
755 int ignore = oldcount;
757 if ((flags & GLOB_DOOFFS) && ignore < pglob->gl_offs)
758 ignore = pglob->gl_offs;
760 if (prefix_array (dirname,
761 &pglob->gl_pathv[ignore],
762 pglob->gl_pathc - ignore))
770 if (flags & GLOB_MARK)
772 /* Append slashes to directory names. */
775 for (i = oldcount; i < pglob->gl_pathc; ++i)
776 if (((flags & GLOB_ALTDIRFUNC)
777 ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
778 : __stat (pglob->gl_pathv[i], &st)) == 0
779 && S_ISDIR (st.st_mode))
781 size_t len = strlen (pglob->gl_pathv[i]) + 2;
782 char *new = xrealloc (pglob->gl_pathv[i], len);
788 strcpy (&new[len - 2], "/");
789 pglob->gl_pathv[i] = new;
793 if (!(flags & GLOB_NOSORT))
795 /* Sort the vector. */
796 int non_sort = oldcount;
798 if ((flags & GLOB_DOOFFS) && pglob->gl_offs > oldcount)
799 non_sort = pglob->gl_offs;
801 qsort ((__ptr_t) &pglob->gl_pathv[non_sort],
802 pglob->gl_pathc - non_sort,
803 sizeof (char *), collated_compare);
810 /* Free storage allocated in PGLOB by a previous `glob' call. */
812 globfree (glob_t *pglob)
814 if (pglob->gl_pathv != NULL)
817 for (i = 0; i < pglob->gl_pathc; ++i)
818 if (pglob->gl_pathv[i] != NULL)
819 free ((__ptr_t) pglob->gl_pathv[i]);
820 free ((__ptr_t) pglob->gl_pathv);
825 /* Do a collated comparison of A and B. */
827 collated_compare (const __ptr_t a, const __ptr_t b)
829 const char *const s1 = *(const char *const * const) a;
830 const char *const s2 = *(const char *const * const) b;
838 return strcoll (s1, s2);
842 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
843 elements in place. Return nonzero if out of memory, zero if successful.
844 A slash is inserted between DIRNAME and each elt of ARRAY,
845 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
847 prefix_array (const char *dirname, char **array, size_t n)
850 size_t dirlen = strlen (dirname);
851 #if defined __MSDOS__ || defined WINDOWS32
853 # define DIRSEP_CHAR sep_char
855 # define DIRSEP_CHAR '/'
858 if (dirlen == 1 && dirname[0] == '/')
859 /* DIRNAME is just "/", so normal prepending would get us "//foo".
860 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
862 #if defined __MSDOS__ || defined WINDOWS32
865 if (dirname[dirlen - 1] == '/')
866 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
868 else if (dirname[dirlen - 1] == ':')
870 /* DIRNAME is "d:". Use `:' instead of `/'. */
877 for (i = 0; i < n; ++i)
879 size_t eltlen = strlen (array[i]) + 1;
880 char *new = (char *) xmalloc (dirlen + 1 + eltlen);
884 free ((__ptr_t) array[--i]);
890 char *endp = (char *) mempcpy (new, dirname, dirlen);
891 *endp++ = DIRSEP_CHAR;
892 mempcpy (endp, array[i], eltlen);
895 memcpy (new, dirname, dirlen);
896 new[dirlen] = DIRSEP_CHAR;
897 memcpy (&new[dirlen + 1], array[i], eltlen);
899 free ((__ptr_t) array[i]);
907 /* We must not compile this function twice. */
908 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
909 /* Return nonzero if PATTERN contains any metacharacters.
910 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
912 __glob_pattern_p (const char *pattern, int quote)
914 register const char *p;
917 for (p = pattern; *p != '\0'; ++p)
925 if (quote && p[1] != '\0')
942 weak_alias (__glob_pattern_p, glob_pattern_p)
947 /* Like `glob', but PATTERN is a final pathname component,
948 and matches are searched for in DIRECTORY.
949 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
950 The GLOB_APPEND flag is assumed to be set (always appends). */
952 glob_in_dir (const char *pattern, const char *directory, int flags,
953 int (*errfunc) __P ((const char *, int)), glob_t *pglob)
955 __ptr_t stream = NULL;
959 struct globlink *next;
962 struct globlink *names = NULL;
967 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
970 if (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))
971 /* We need not do any tests. The PATTERN contains no meta
972 characters and we must not return an error therefore the
973 result will always contain exactly one name. */
974 flags |= GLOB_NOCHECK;
977 /* Since we use the normal file functions we can also use stat()
978 to verify the file is there. */
980 size_t patlen = strlen (pattern);
981 size_t dirlen = strlen (directory);
982 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
985 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
987 pattern, patlen + 1);
989 memcpy (fullname, directory, dirlen);
990 fullname[dirlen] = '/';
991 memcpy (&fullname[dirlen + 1], pattern, patlen + 1);
993 if (((flags & GLOB_ALTDIRFUNC)
994 ? (*pglob->gl_stat) (fullname, &st)
995 : __stat (fullname, &st)) == 0)
996 /* We found this file to be existing. Now tell the rest
997 of the function to copy this name into the result. */
998 flags |= GLOB_NOCHECK;
1005 if (pattern[0] == '\0')
1007 /* This is a special case for matching directories like in
1009 names = (struct globlink *) __alloca (sizeof (struct globlink));
1010 names->name = (char *) xmalloc (1);
1011 if (names->name == NULL)
1013 names->name[0] = '\0';
1020 stream = ((flags & GLOB_ALTDIRFUNC)
1021 ? (*pglob->gl_opendir) (directory)
1022 : (__ptr_t) opendir (directory));
1025 if (errno != ENOTDIR
1026 && ((errfunc != NULL && (*errfunc) (directory, errno))
1027 || (flags & GLOB_ERR)))
1028 return GLOB_ABORTED;
1034 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1035 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1036 #if defined _AMIGA || defined VMS
1041 flags |= GLOB_MAGCHAR;
1047 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1048 ? (*pglob->gl_readdir) (stream)
1049 : readdir ((DIR *) stream));
1052 if (! REAL_DIR_ENTRY (d))
1056 /* If we shall match only directories use the information
1057 provided by the dirent call if possible. */
1058 if ((flags & GLOB_ONLYDIR)
1059 && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
1065 if (fnmatch (pattern, name, fnm_flags) == 0)
1067 struct globlink *new = (struct globlink *)
1068 __alloca (sizeof (struct globlink));
1070 new->name = (char *) xmalloc (len + 1);
1071 if (new->name == NULL)
1074 *((char *) mempcpy ((__ptr_t) new->name, name, len))
1077 memcpy ((__ptr_t) new->name, name, len);
1078 new->name[len] = '\0';
1089 if (nfound == 0 && (flags & GLOB_NOCHECK))
1091 size_t len = strlen (pattern);
1093 names = (struct globlink *) __alloca (sizeof (struct globlink));
1095 names->name = (char *) xmalloc (len + 1);
1096 if (names->name == NULL)
1099 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1101 memcpy (names->name, pattern, len);
1102 names->name[len] = '\0';
1109 = (char **) xrealloc (pglob->gl_pathv,
1111 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
1114 if (pglob->gl_pathv == NULL)
1117 if (flags & GLOB_DOOFFS)
1118 while (pglob->gl_pathc < pglob->gl_offs)
1119 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
1121 for (; names != NULL; names = names->next)
1122 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
1123 pglob->gl_pathv[pglob->gl_pathc] = NULL;
1125 pglob->gl_flags = flags;
1131 if (flags & GLOB_ALTDIRFUNC)
1132 (*pglob->gl_closedir) (stream);
1134 closedir ((DIR *) stream);
1138 return nfound == 0 ? GLOB_NOMATCH : 0;
1143 if (flags & GLOB_ALTDIRFUNC)
1144 (*pglob->gl_closedir) (stream);
1146 closedir ((DIR *) stream);
1149 while (names != NULL)
1151 if (names->name != NULL)
1152 free ((__ptr_t) names->name);
1153 names = names->next;
1155 return GLOB_NOSPACE;