update from main archive 961016
[glibc.git] / posix / glob.c
blobb8820cee94630adfc53db9ed7c33c4f1b5de225d
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
18 /* AIX requires this to be the first thing in the file. */
19 #if defined (_AIX) && !defined (__GNUC__)
20 #pragma alloca
21 #endif
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 /* Enable GNU extensions in glob.h. */
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE 1
30 #endif
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #define GLOB_INTERFACE_VERSION 1
46 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
47 #include <gnu-versions.h>
48 #if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
49 #define ELIDE_CODE
50 #endif
51 #endif
53 #ifndef ELIDE_CODE
55 #ifdef STDC_HEADERS
56 #include <stddef.h>
57 #endif
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #ifndef POSIX
62 #ifdef _POSIX_VERSION
63 #define POSIX
64 #endif
65 #endif
66 #endif
68 #if !defined (_AMIGA) && !defined (VMS) && !defined(WIN32)
69 #include <pwd.h>
70 #endif
72 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
73 extern int errno;
74 #endif
75 #ifndef __set_errno
76 #define __set_errno(val) errno = (val)
77 #endif
79 #ifndef NULL
80 #define NULL 0
81 #endif
84 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
85 # include <dirent.h>
86 # define NAMLEN(dirent) strlen((dirent)->d_name)
87 #else
88 # define dirent direct
89 # define NAMLEN(dirent) (dirent)->d_namlen
90 # ifdef HAVE_SYS_NDIR_H
91 # include <sys/ndir.h>
92 # endif
93 # ifdef HAVE_SYS_DIR_H
94 # include <sys/dir.h>
95 # endif
96 # ifdef HAVE_NDIR_H
97 # include <ndir.h>
98 # endif
99 # ifdef HAVE_VMSDIR_H
100 # include "vmsdir.h"
101 # endif /* HAVE_VMSDIR_H */
102 #endif
105 /* In GNU systems, <dirent.h> defines this macro for us. */
106 #ifdef _D_NAMLEN
107 #undef NAMLEN
108 #define NAMLEN(d) _D_NAMLEN(d)
109 #endif
112 #if (defined (POSIX) || defined (WIN32)) && !defined (__GNU_LIBRARY__)
113 /* Posix does not require that the d_ino field be present, and some
114 systems do not provide it. */
115 #define REAL_DIR_ENTRY(dp) 1
116 #else
117 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
118 #endif /* POSIX */
120 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
121 #include <stdlib.h>
122 #include <string.h>
123 #define ANSI_STRING
124 #else /* No standard headers. */
126 extern char *getenv ();
128 #ifdef HAVE_STRING_H
129 #include <string.h>
130 #define ANSI_STRING
131 #else
132 #include <strings.h>
133 #endif
134 #ifdef HAVE_MEMORY_H
135 #include <memory.h>
136 #endif
138 extern char *malloc (), *realloc ();
139 extern void free ();
141 extern void qsort ();
142 extern void abort (), exit ();
144 #endif /* Standard headers. */
146 #ifndef ANSI_STRING
148 #ifndef bzero
149 extern void bzero ();
150 #endif
151 #ifndef bcopy
152 extern void bcopy ();
153 #endif
155 #define memcpy(d, s, n) bcopy ((s), (d), (n))
156 #define strrchr rindex
157 /* memset is only used for zero here, but let's be paranoid. */
158 #define memset(s, better_be_zero, n) \
159 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
160 #endif /* Not ANSI_STRING. */
162 #ifndef HAVE_STRCOLL
163 #define strcoll strcmp
164 #endif
167 #ifndef __GNU_LIBRARY__
168 #ifdef __GNUC__
169 __inline
170 #endif
171 #ifndef __SASC
172 #ifdef WIN32
173 static void *
174 #else
175 static char *
176 #endif
177 my_realloc (p, n)
178 char *p;
179 unsigned int n;
181 /* These casts are the for sake of the broken Ultrix compiler,
182 which warns of illegal pointer combinations otherwise. */
183 if (p == NULL)
184 return (char *) malloc (n);
185 return (char *) realloc (p, n);
187 #define realloc my_realloc
188 #endif /* __SASC */
189 #endif /* __GNU_LIBRARY__ */
192 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
194 #ifdef __GNUC__
195 #undef alloca
196 #define alloca(n) __builtin_alloca (n)
197 #else /* Not GCC. */
198 #ifdef HAVE_ALLOCA_H
199 #include <alloca.h>
200 #else /* Not HAVE_ALLOCA_H. */
201 #ifndef _AIX
202 #ifdef WIN32
203 #include <malloc.h>
204 #else
205 extern char *alloca ();
206 #endif /* WIN32 */
207 #endif /* Not _AIX. */
208 #endif /* sparc or HAVE_ALLOCA_H. */
209 #endif /* GCC. */
211 #define __alloca alloca
213 #endif
215 #ifndef __GNU_LIBRARY__
216 #define __stat stat
217 #ifdef STAT_MACROS_BROKEN
218 #undef S_ISDIR
219 #endif
220 #ifndef S_ISDIR
221 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
222 #endif
223 #endif
225 #ifndef STDC_HEADERS
226 #undef size_t
227 #define size_t unsigned int
228 #endif
230 /* Some system header files erroneously define these.
231 We want our own definitions from <fnmatch.h> to take precedence. */
232 #undef FNM_PATHNAME
233 #undef FNM_NOESCAPE
234 #undef FNM_PERIOD
235 #include <fnmatch.h>
237 /* Some system header files erroneously define these.
238 We want our own definitions from <glob.h> to take precedence. */
239 #undef GLOB_ERR
240 #undef GLOB_MARK
241 #undef GLOB_NOSORT
242 #undef GLOB_DOOFFS
243 #undef GLOB_NOCHECK
244 #undef GLOB_APPEND
245 #undef GLOB_NOESCAPE
246 #undef GLOB_PERIOD
247 #include <glob.h>
249 static int glob_pattern_p __P ((const char *pattern, int quote));
250 static int glob_in_dir __P ((const char *pattern, const char *directory,
251 int flags,
252 int (*errfunc) __P ((const char *, int)),
253 glob_t *pglob));
254 static int prefix_array __P ((const char *prefix, char **array, size_t n));
255 static int collated_compare __P ((const __ptr_t, const __ptr_t));
257 /* Do glob searching for PATTERN, placing results in PGLOB.
258 The bits defined above may be set in FLAGS.
259 If a directory cannot be opened or read and ERRFUNC is not nil,
260 it is called with the pathname that caused the error, and the
261 `errno' value from the failing call; if it returns non-zero
262 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
263 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
264 Otherwise, `glob' returns zero. */
266 glob (pattern, flags, errfunc, pglob)
267 const char *pattern;
268 int flags;
269 int (*errfunc) __P ((const char *, int));
270 glob_t *pglob;
272 const char *filename;
273 char *dirname;
274 size_t dirlen;
275 int status;
276 int oldcount;
278 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
280 __set_errno (EINVAL);
281 return -1;
284 if (flags & GLOB_BRACE)
286 const char *begin = strchr (pattern, '{');
287 if (begin != NULL)
289 int firstc;
290 size_t restlen;
291 const char *p, *end, *next;
292 unsigned int depth = 0;
294 /* Find the end of the brace expression, by counting braces.
295 While we're at it, notice the first comma at top brace level. */
296 end = begin + 1;
297 next = NULL;
298 while (1)
300 switch (*end++)
302 case ',':
303 if (depth == 0 && next == NULL)
304 next = end;
305 continue;
306 case '{':
307 ++depth;
308 continue;
309 case '}':
310 if (depth-- == 0)
311 break;
312 continue;
313 case '\0':
314 return glob (pattern, flags &~ GLOB_BRACE, errfunc, pglob);
316 break;
318 restlen = strlen (end) + 1;
319 if (next == NULL)
320 next = end;
322 /* We have a brace expression. BEGIN points to the opening {,
323 NEXT points past the terminator of the first element, and END
324 points past the final }. We will accumulate result names from
325 recursive runs for each brace alternative in the buffer using
326 GLOB_APPEND. */
328 if (!(flags & GLOB_APPEND))
330 /* This call is to set a new vector, so clear out the
331 vector so we can append to it. */
332 pglob->gl_pathc = 0;
333 pglob->gl_pathv = NULL;
335 firstc = pglob->gl_pathc;
337 /* In this loop P points to the beginning of the current element
338 and NEXT points past its terminator. */
339 p = begin + 1;
340 while (1)
342 /* Construct a whole name that is one of the brace
343 alternatives in a temporary buffer. */
344 int result;
345 size_t bufsz = (begin - pattern) + (next - 1 - p) + restlen;
346 #ifdef __GNUC__
347 char onealt[bufsz];
348 #else
349 char *onealt = malloc (bufsz);
350 if (onealt == NULL)
352 if (!(flags & GLOB_APPEND))
353 globfree (pglob);
354 return GLOB_NOSPACE;
356 #endif
357 memcpy (onealt, pattern, begin - pattern);
358 memcpy (&onealt[begin - pattern], p, next - 1 - p);
359 memcpy (&onealt[(begin - pattern) + (next - 1 - p)],
360 end, restlen);
361 result = glob (onealt,
362 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
363 GLOB_APPEND), errfunc, pglob);
364 #ifndef __GNUC__
365 free (onealt);
366 #endif
368 /* If we got an error, return it. */
369 if (result && result != GLOB_NOMATCH)
371 if (!(flags & GLOB_APPEND))
372 globfree (pglob);
373 return result;
376 /* Advance past this alternative and process the next. */
377 p = next;
378 depth = 0;
379 scan:
380 switch (*p++)
382 case ',':
383 if (depth == 0)
385 /* Found the next alternative. Loop to glob it. */
386 next = p;
387 continue;
389 goto scan;
390 case '{':
391 ++depth;
392 goto scan;
393 case '}':
394 if (depth-- == 0)
395 /* End of the brace expression. Break out of the loop. */
396 break;
397 goto scan;
401 if (pglob->gl_pathc == firstc &&
402 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
403 return GLOB_NOMATCH;
407 /* Find the filename. */
408 filename = strrchr (pattern, '/');
409 if (filename == NULL)
411 filename = pattern;
412 #ifdef _AMIGA
413 dirname = (char *) "";
414 #else
415 dirname = (char *) ".";
416 #endif
417 dirlen = 0;
419 else if (filename == pattern)
421 /* "/pattern". */
422 dirname = (char *) "/";
423 dirlen = 1;
424 ++filename;
426 else
428 dirlen = filename - pattern;
429 dirname = (char *) __alloca (dirlen + 1);
430 memcpy (dirname, pattern, dirlen);
431 dirname[dirlen] = '\0';
432 ++filename;
435 if (filename[0] == '\0' && dirlen > 1)
436 /* "pattern/". Expand "pattern", appending slashes. */
438 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
439 if (val == 0)
440 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
441 return val;
444 if (!(flags & GLOB_APPEND))
446 pglob->gl_pathc = 0;
447 pglob->gl_pathv = NULL;
450 oldcount = pglob->gl_pathc;
452 #ifndef VMS
453 if ((flags & GLOB_TILDE) && dirname[0] == '~')
455 if (dirname[1] == '\0')
457 /* Look up home directory. */
458 dirname = getenv ("HOME");
459 #ifdef _AMIGA
460 if (dirname == NULL || dirname[0] == '\0')
461 dirname = "SYS:";
462 #else
463 #ifdef WIN32
464 if (dirname == NULL || dirname[0] == '\0')
465 dirname = "c:/users/default"; /* poor default */
466 #else
467 if (dirname == NULL || dirname[0] == '\0')
469 extern char *getlogin __P ((void));
470 char *name = getlogin ();
471 if (name != NULL)
473 struct passwd *p = getpwnam (name);
474 if (p != NULL)
475 dirname = p->pw_dir;
478 if (dirname == NULL || dirname[0] == '\0')
479 dirname = (char *) "~"; /* No luck. */
480 #endif /* WIN32 */
481 #endif
483 else
485 #ifdef _AMIGA
486 if (dirname == NULL || dirname[0] == '\0')
487 dirname = "SYS:";
488 #else
489 #ifdef WIN32
490 if (dirname == NULL || dirname[0] == '\0')
491 dirname = "c:/users/default"; /* poor default */
492 #else
493 /* Look up specific user's home directory. */
494 struct passwd *p = getpwnam (dirname + 1);
495 if (p != NULL)
496 dirname = p->pw_dir;
497 #endif /* WIN32 */
498 #endif
501 #endif /* Not VMS. */
503 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
505 /* The directory name contains metacharacters, so we
506 have to glob for the directory, and then glob for
507 the pattern in each directory found. */
508 glob_t dirs;
509 register int i;
511 status = glob (dirname,
512 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
513 GLOB_NOSORT),
514 errfunc, &dirs);
515 if (status != 0)
516 return status;
518 /* We have successfully globbed the preceding directory name.
519 For each name we found, call glob_in_dir on it and FILENAME,
520 appending the results to PGLOB. */
521 for (i = 0; i < dirs.gl_pathc; ++i)
523 int oldcount;
525 #ifdef SHELL
527 /* Make globbing interruptible in the bash shell. */
528 extern int interrupt_state;
530 if (interrupt_state)
532 globfree (&dirs);
533 globfree (&files);
534 return GLOB_ABEND;
537 #endif /* SHELL. */
539 oldcount = pglob->gl_pathc;
540 status = glob_in_dir (filename, dirs.gl_pathv[i],
541 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
542 errfunc, pglob);
543 if (status == GLOB_NOMATCH)
544 /* No matches in this directory. Try the next. */
545 continue;
547 if (status != 0)
549 globfree (&dirs);
550 globfree (pglob);
551 return status;
554 /* Stick the directory on the front of each name. */
555 if (prefix_array (dirs.gl_pathv[i],
556 &pglob->gl_pathv[oldcount],
557 pglob->gl_pathc - oldcount))
559 globfree (&dirs);
560 globfree (pglob);
561 return GLOB_NOSPACE;
565 flags |= GLOB_MAGCHAR;
567 if (pglob->gl_pathc == oldcount)
568 /* No matches. */
569 if (flags & GLOB_NOCHECK)
571 size_t len = strlen (pattern) + 1;
572 char *patcopy = (char *) malloc (len);
573 if (patcopy == NULL)
574 return GLOB_NOSPACE;
575 memcpy (patcopy, pattern, len);
577 pglob->gl_pathv
578 = (char **) realloc (pglob->gl_pathv,
579 (pglob->gl_pathc +
580 ((flags & GLOB_DOOFFS) ?
581 pglob->gl_offs : 0) +
582 1 + 1) *
583 sizeof (char *));
584 if (pglob->gl_pathv == NULL)
586 free (patcopy);
587 return GLOB_NOSPACE;
590 if (flags & GLOB_DOOFFS)
591 while (pglob->gl_pathc < pglob->gl_offs)
592 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
594 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
595 pglob->gl_pathv[pglob->gl_pathc] = NULL;
596 pglob->gl_flags = flags;
598 else
599 return GLOB_NOMATCH;
601 else
603 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
604 if (status != 0)
605 return status;
607 if (dirlen > 0)
609 /* Stick the directory on the front of each name. */
610 if (prefix_array (dirname,
611 &pglob->gl_pathv[oldcount],
612 pglob->gl_pathc - oldcount))
614 globfree (pglob);
615 return GLOB_NOSPACE;
620 if (flags & GLOB_MARK)
622 /* Append slashes to directory names. */
623 int i;
624 struct stat st;
625 for (i = oldcount; i < pglob->gl_pathc; ++i)
626 if (((flags & GLOB_ALTDIRFUNC) ?
627 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
628 __stat (pglob->gl_pathv[i], &st)) == 0 &&
629 S_ISDIR (st.st_mode))
631 size_t len = strlen (pglob->gl_pathv[i]) + 2;
632 char *new = realloc (pglob->gl_pathv[i], len);
633 if (new == NULL)
635 globfree (pglob);
636 return GLOB_NOSPACE;
638 strcpy (&new[len - 2], "/");
639 pglob->gl_pathv[i] = new;
643 if (!(flags & GLOB_NOSORT))
644 /* Sort the vector. */
645 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
646 pglob->gl_pathc - oldcount,
647 sizeof (char *), collated_compare);
649 return 0;
653 /* Free storage allocated in PGLOB by a previous `glob' call. */
654 void
655 globfree (pglob)
656 register glob_t *pglob;
658 if (pglob->gl_pathv != NULL)
660 register int i;
661 for (i = 0; i < pglob->gl_pathc; ++i)
662 if (pglob->gl_pathv[i] != NULL)
663 free ((__ptr_t) pglob->gl_pathv[i]);
664 free ((__ptr_t) pglob->gl_pathv);
669 /* Do a collated comparison of A and B. */
670 static int
671 collated_compare (a, b)
672 const __ptr_t a;
673 const __ptr_t b;
675 const char *const s1 = *(const char *const * const) a;
676 const char *const s2 = *(const char *const * const) b;
678 if (s1 == s2)
679 return 0;
680 if (s1 == NULL)
681 return 1;
682 if (s2 == NULL)
683 return -1;
684 return strcoll (s1, s2);
688 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
689 elements in place. Return nonzero if out of memory, zero if successful.
690 A slash is inserted between DIRNAME and each elt of ARRAY,
691 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
692 static int
693 prefix_array (dirname, array, n)
694 const char *dirname;
695 char **array;
696 size_t n;
698 register size_t i;
699 size_t dirlen = strlen (dirname);
701 if (dirlen == 1 && dirname[0] == '/')
702 /* DIRNAME is just "/", so normal prepending would get us "//foo".
703 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
704 dirlen = 0;
706 for (i = 0; i < n; ++i)
708 size_t eltlen = strlen (array[i]) + 1;
709 char *new = (char *) malloc (dirlen + 1 + eltlen);
710 if (new == NULL)
712 while (i > 0)
713 free ((__ptr_t) array[--i]);
714 return 1;
717 memcpy (new, dirname, dirlen);
718 new[dirlen] = '/';
719 memcpy (&new[dirlen + 1], array[i], eltlen);
720 free ((__ptr_t) array[i]);
721 array[i] = new;
724 return 0;
728 /* Return nonzero if PATTERN contains any metacharacters.
729 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
730 static int
731 glob_pattern_p (pattern, quote)
732 const char *pattern;
733 int quote;
735 register const char *p;
736 int open = 0;
738 for (p = pattern; *p != '\0'; ++p)
739 switch (*p)
741 case '?':
742 case '*':
743 return 1;
745 case '\\':
746 if (quote && p[1] != '\0')
747 ++p;
748 break;
750 case '[':
751 open = 1;
752 break;
754 case ']':
755 if (open)
756 return 1;
757 break;
760 return 0;
764 /* Like `glob', but PATTERN is a final pathname component,
765 and matches are searched for in DIRECTORY.
766 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
767 The GLOB_APPEND flag is assumed to be set (always appends). */
768 static int
769 glob_in_dir (pattern, directory, flags, errfunc, pglob)
770 const char *pattern;
771 const char *directory;
772 int flags;
773 int (*errfunc) __P ((const char *, int));
774 glob_t *pglob;
776 __ptr_t stream;
778 struct globlink
780 struct globlink *next;
781 char *name;
783 struct globlink *names = NULL;
784 size_t nfound = 0;
786 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
788 stream = NULL;
789 flags |= GLOB_NOCHECK;
791 else
793 flags |= GLOB_MAGCHAR;
795 stream = ((flags & GLOB_ALTDIRFUNC) ?
796 (*pglob->gl_opendir) (directory) :
797 (__ptr_t) opendir (directory));
798 if (stream == NULL)
800 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
801 (flags & GLOB_ERR))
802 return GLOB_ABEND;
804 else
805 while (1)
807 const char *name;
808 size_t len;
809 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
810 (*pglob->gl_readdir) (stream) :
811 readdir ((DIR *) stream));
812 if (d == NULL)
813 break;
814 if (! REAL_DIR_ENTRY (d))
815 continue;
817 name = d->d_name;
819 if (fnmatch (pattern, name,
820 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
821 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
822 #ifdef _AMIGA
823 | FNM_CASEFOLD
824 #endif
825 ) == 0)
827 struct globlink *new
828 = (struct globlink *) __alloca (sizeof (struct globlink));
829 len = NAMLEN (d);
830 new->name
831 = (char *) malloc (len + 1);
832 if (new->name == NULL)
833 goto memory_error;
834 memcpy ((__ptr_t) new->name, name, len);
835 new->name[len] = '\0';
836 new->next = names;
837 names = new;
838 ++nfound;
843 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
844 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
845 flags |= GLOB_NOCHECK;
847 if (nfound == 0 && (flags & GLOB_NOCHECK))
849 size_t len = strlen (pattern);
850 nfound = 1;
851 names = (struct globlink *) __alloca (sizeof (struct globlink));
852 names->next = NULL;
853 names->name = (char *) malloc (len + 1);
854 if (names->name == NULL)
855 goto memory_error;
856 memcpy (names->name, pattern, len);
857 names->name[len] = '\0';
860 pglob->gl_pathv
861 = (char **) realloc (pglob->gl_pathv,
862 (pglob->gl_pathc +
863 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
864 nfound + 1) *
865 sizeof (char *));
866 if (pglob->gl_pathv == NULL)
867 goto memory_error;
869 if (flags & GLOB_DOOFFS)
870 while (pglob->gl_pathc < pglob->gl_offs)
871 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
873 for (; names != NULL; names = names->next)
874 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
875 pglob->gl_pathv[pglob->gl_pathc] = NULL;
877 pglob->gl_flags = flags;
879 if (stream != NULL)
881 int save = errno;
882 if (flags & GLOB_ALTDIRFUNC)
883 (*pglob->gl_closedir) (stream);
884 else
885 closedir ((DIR *) stream);
886 __set_errno (save);
888 return nfound == 0 ? GLOB_NOMATCH : 0;
890 memory_error:
892 int save = errno;
893 if (flags & GLOB_ALTDIRFUNC)
894 (*pglob->gl_closedir) (stream);
895 else
896 closedir ((DIR *) stream);
897 __set_errno (save);
899 while (names != NULL)
901 if (names->name != NULL)
902 free ((__ptr_t) names->name);
903 names = names->next;
905 return GLOB_NOSPACE;
908 #endif /* Not ELIDE_CODE. */