Fri Jul 19 15:45:20 1996 Ulrich Drepper <drepper@cygnus.com>
[glibc.git] / posix / glob.c
blobd6635fc9d09a587e9b456547472c670626568eb9
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)
69 #include <pwd.h>
70 #endif
72 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
73 extern int errno;
74 #endif
76 #ifndef NULL
77 #define NULL 0
78 #endif
81 #if defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
82 # include <dirent.h>
83 # define NAMLEN(dirent) strlen((dirent)->d_name)
84 #else
85 # define dirent direct
86 # define NAMLEN(dirent) (dirent)->d_namlen
87 # ifdef HAVE_SYS_NDIR_H
88 # include <sys/ndir.h>
89 # endif
90 # ifdef HAVE_SYS_DIR_H
91 # include <sys/dir.h>
92 # endif
93 # ifdef HAVE_NDIR_H
94 # include <ndir.h>
95 # endif
96 # ifdef HAVE_VMSDIR_H
97 # include "vmsdir.h"
98 # endif /* HAVE_VMSDIR_H */
99 #endif
102 /* In GNU systems, <dirent.h> defines this macro for us. */
103 #ifdef _D_NAMLEN
104 #undef NAMLEN
105 #define NAMLEN(d) _D_NAMLEN(d)
106 #endif
109 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
110 /* Posix does not require that the d_ino field be present, and some
111 systems do not provide it. */
112 #define REAL_DIR_ENTRY(dp) 1
113 #else
114 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
115 #endif /* POSIX */
117 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
118 #include <stdlib.h>
119 #include <string.h>
120 #define ANSI_STRING
121 #else /* No standard headers. */
123 extern char *getenv ();
125 #ifdef HAVE_STRING_H
126 #include <string.h>
127 #define ANSI_STRING
128 #else
129 #include <strings.h>
130 #endif
131 #ifdef HAVE_MEMORY_H
132 #include <memory.h>
133 #endif
135 extern char *malloc (), *realloc ();
136 extern void free ();
138 extern void qsort ();
139 extern void abort (), exit ();
141 #endif /* Standard headers. */
143 #ifndef ANSI_STRING
145 #ifndef bzero
146 extern void bzero ();
147 #endif
148 #ifndef bcopy
149 extern void bcopy ();
150 #endif
152 #define memcpy(d, s, n) bcopy ((s), (d), (n))
153 #define strrchr rindex
154 /* memset is only used for zero here, but let's be paranoid. */
155 #define memset(s, better_be_zero, n) \
156 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
157 #endif /* Not ANSI_STRING. */
159 #ifndef HAVE_STRCOLL
160 #define strcoll strcmp
161 #endif
164 #ifndef __GNU_LIBRARY__
165 #ifdef __GNUC__
166 __inline
167 #endif
168 #ifndef __SASC
169 static char *
170 my_realloc (p, n)
171 char *p;
172 unsigned int n;
174 /* These casts are the for sake of the broken Ultrix compiler,
175 which warns of illegal pointer combinations otherwise. */
176 if (p == NULL)
177 return (char *) malloc (n);
178 return (char *) realloc (p, n);
180 #define realloc my_realloc
181 #endif /* __SASC */
182 #endif /* __GNU_LIBRARY__ */
185 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
187 #ifdef __GNUC__
188 #undef alloca
189 #define alloca(n) __builtin_alloca (n)
190 #else /* Not GCC. */
191 #ifdef HAVE_ALLOCA_H
192 #include <alloca.h>
193 #else /* Not HAVE_ALLOCA_H. */
194 #ifndef _AIX
195 extern char *alloca ();
196 #endif /* Not _AIX. */
197 #endif /* sparc or HAVE_ALLOCA_H. */
198 #endif /* GCC. */
200 #define __alloca alloca
202 #endif
204 #ifndef __GNU_LIBRARY__
205 #define __stat stat
206 #ifdef STAT_MACROS_BROKEN
207 #undef S_ISDIR
208 #endif
209 #ifndef S_ISDIR
210 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
211 #endif
212 #endif
214 #ifndef STDC_HEADERS
215 #undef size_t
216 #define size_t unsigned int
217 #endif
219 /* Some system header files erroneously define these.
220 We want our own definitions from <fnmatch.h> to take precedence. */
221 #undef FNM_PATHNAME
222 #undef FNM_NOESCAPE
223 #undef FNM_PERIOD
224 #include <fnmatch.h>
226 /* Some system header files erroneously define these.
227 We want our own definitions from <glob.h> to take precedence. */
228 #undef GLOB_ERR
229 #undef GLOB_MARK
230 #undef GLOB_NOSORT
231 #undef GLOB_DOOFFS
232 #undef GLOB_NOCHECK
233 #undef GLOB_APPEND
234 #undef GLOB_NOESCAPE
235 #undef GLOB_PERIOD
236 #include <glob.h>
238 static int glob_pattern_p __P ((const char *pattern, int quote));
239 static int glob_in_dir __P ((const char *pattern, const char *directory,
240 int flags,
241 int (*errfunc) __P ((const char *, int)),
242 glob_t *pglob));
243 static int prefix_array __P ((const char *prefix, char **array, size_t n));
244 static int collated_compare __P ((const __ptr_t, const __ptr_t));
246 /* Do glob searching for PATTERN, placing results in PGLOB.
247 The bits defined above may be set in FLAGS.
248 If a directory cannot be opened or read and ERRFUNC is not nil,
249 it is called with the pathname that caused the error, and the
250 `errno' value from the failing call; if it returns non-zero
251 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
252 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
253 Otherwise, `glob' returns zero. */
255 glob (pattern, flags, errfunc, pglob)
256 const char *pattern;
257 int flags;
258 int (*errfunc) __P ((const char *, int));
259 glob_t *pglob;
261 const char *filename;
262 char *dirname;
263 size_t dirlen;
264 int status;
265 int oldcount;
267 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
269 errno = EINVAL;
270 return -1;
273 if (flags & GLOB_BRACE)
275 const char *begin = strchr (pattern, '{');
276 if (begin != NULL)
278 int firstc;
279 size_t restlen;
280 const char *p, *end, *next;
281 unsigned int depth = 0;
283 /* Find the end of the brace expression, by counting braces.
284 While we're at it, notice the first comma at top brace level. */
285 end = begin + 1;
286 next = NULL;
287 while (1)
289 switch (*end++)
291 case ',':
292 if (depth == 0 && next == NULL)
293 next = end;
294 continue;
295 case '{':
296 ++depth;
297 continue;
298 case '}':
299 if (depth-- == 0)
300 break;
301 continue;
302 case '\0':
303 return glob (pattern, flags &~ GLOB_BRACE, errfunc, pglob);
305 break;
307 restlen = strlen (end) + 1;
308 if (next == NULL)
309 next = end;
311 /* We have a brace expression. BEGIN points to the opening {,
312 NEXT points past the terminator of the first element, and END
313 points past the final }. We will accumulate result names from
314 recursive runs for each brace alternative in the buffer using
315 GLOB_APPEND. */
317 if (!(flags & GLOB_APPEND))
319 /* This call is to set a new vector, so clear out the
320 vector so we can append to it. */
321 pglob->gl_pathc = 0;
322 pglob->gl_pathv = NULL;
324 firstc = pglob->gl_pathc;
326 /* In this loop P points to the beginning of the current element
327 and NEXT points past its terminator. */
328 p = begin + 1;
329 while (1)
331 /* Construct a whole name that is one of the brace
332 alternatives in a temporary buffer. */
333 int result;
334 size_t bufsz = (begin - pattern) + (next - 1 - p) + restlen;
335 #ifdef __GNUC__
336 char onealt[bufsz];
337 #else
338 char *onealt = malloc (bufsz);
339 if (onealt == NULL)
341 if (!(flags & GLOB_APPEND))
342 globfree (pglob);
343 return GLOB_NOSPACE;
345 #endif
346 memcpy (onealt, pattern, begin - pattern);
347 memcpy (&onealt[begin - pattern], p, next - 1 - p);
348 memcpy (&onealt[(begin - pattern) + (next - 1 - p)],
349 end, restlen);
350 result = glob (onealt,
351 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
352 GLOB_APPEND), errfunc, pglob);
353 #ifndef __GNUC__
354 free (onealt);
355 #endif
357 /* If we got an error, return it. */
358 if (result && result != GLOB_NOMATCH)
360 if (!(flags & GLOB_APPEND))
361 globfree (pglob);
362 return result;
365 /* Advance past this alternative and process the next. */
366 p = next;
367 depth = 0;
368 scan:
369 switch (*p++)
371 case ',':
372 if (depth == 0)
374 /* Found the next alternative. Loop to glob it. */
375 next = p;
376 continue;
378 goto scan;
379 case '{':
380 ++depth;
381 goto scan;
382 case '}':
383 if (depth-- == 0)
384 /* End of the brace expression. Break out of the loop. */
385 break;
386 goto scan;
390 if (pglob->gl_pathc == firstc &&
391 !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
392 return GLOB_NOMATCH;
396 /* Find the filename. */
397 filename = strrchr (pattern, '/');
398 if (filename == NULL)
400 filename = pattern;
401 #ifdef _AMIGA
402 dirname = (char *) "";
403 #else
404 dirname = (char *) ".";
405 #endif
406 dirlen = 0;
408 else if (filename == pattern)
410 /* "/pattern". */
411 dirname = (char *) "/";
412 dirlen = 1;
413 ++filename;
415 else
417 dirlen = filename - pattern;
418 dirname = (char *) __alloca (dirlen + 1);
419 memcpy (dirname, pattern, dirlen);
420 dirname[dirlen] = '\0';
421 ++filename;
424 if (filename[0] == '\0' && dirlen > 1)
425 /* "pattern/". Expand "pattern", appending slashes. */
427 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
428 if (val == 0)
429 pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
430 return val;
433 if (!(flags & GLOB_APPEND))
435 pglob->gl_pathc = 0;
436 pglob->gl_pathv = NULL;
439 oldcount = pglob->gl_pathc;
441 #ifndef VMS
442 if ((flags & GLOB_TILDE) && dirname[0] == '~')
444 if (dirname[1] == '\0')
446 /* Look up home directory. */
447 dirname = getenv ("HOME");
448 #ifdef _AMIGA
449 if (dirname == NULL || dirname[0] == '\0')
450 dirname = "SYS:";
451 #else
452 if (dirname == NULL || dirname[0] == '\0')
454 extern char *getlogin __P ((void));
455 char *name = getlogin ();
456 if (name != NULL)
458 struct passwd *p = getpwnam (name);
459 if (p != NULL)
460 dirname = p->pw_dir;
463 if (dirname == NULL || dirname[0] == '\0')
464 dirname = (char *) "~"; /* No luck. */
465 #endif
467 else
469 #ifdef _AMIGA
470 if (dirname == NULL || dirname[0] == '\0')
471 dirname = "SYS:";
472 #else
473 /* Look up specific user's home directory. */
474 struct passwd *p = getpwnam (dirname + 1);
475 if (p != NULL)
476 dirname = p->pw_dir;
477 #endif
480 #endif /* Not VMS. */
482 if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
484 /* The directory name contains metacharacters, so we
485 have to glob for the directory, and then glob for
486 the pattern in each directory found. */
487 glob_t dirs;
488 register int i;
490 status = glob (dirname,
491 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
492 GLOB_NOSORT),
493 errfunc, &dirs);
494 if (status != 0)
495 return status;
497 /* We have successfully globbed the preceding directory name.
498 For each name we found, call glob_in_dir on it and FILENAME,
499 appending the results to PGLOB. */
500 for (i = 0; i < dirs.gl_pathc; ++i)
502 int oldcount;
504 #ifdef SHELL
506 /* Make globbing interruptible in the bash shell. */
507 extern int interrupt_state;
509 if (interrupt_state)
511 globfree (&dirs);
512 globfree (&files);
513 return GLOB_ABEND;
516 #endif /* SHELL. */
518 oldcount = pglob->gl_pathc;
519 status = glob_in_dir (filename, dirs.gl_pathv[i],
520 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
521 errfunc, pglob);
522 if (status == GLOB_NOMATCH)
523 /* No matches in this directory. Try the next. */
524 continue;
526 if (status != 0)
528 globfree (&dirs);
529 globfree (pglob);
530 return status;
533 /* Stick the directory on the front of each name. */
534 if (prefix_array (dirs.gl_pathv[i],
535 &pglob->gl_pathv[oldcount],
536 pglob->gl_pathc - oldcount))
538 globfree (&dirs);
539 globfree (pglob);
540 return GLOB_NOSPACE;
544 flags |= GLOB_MAGCHAR;
546 if (pglob->gl_pathc == oldcount)
547 /* No matches. */
548 if (flags & GLOB_NOCHECK)
550 size_t len = strlen (pattern) + 1;
551 char *patcopy = (char *) malloc (len);
552 if (patcopy == NULL)
553 return GLOB_NOSPACE;
554 memcpy (patcopy, pattern, len);
556 pglob->gl_pathv
557 = (char **) realloc (pglob->gl_pathv,
558 (pglob->gl_pathc +
559 ((flags & GLOB_DOOFFS) ?
560 pglob->gl_offs : 0) +
561 1 + 1) *
562 sizeof (char *));
563 if (pglob->gl_pathv == NULL)
565 free (patcopy);
566 return GLOB_NOSPACE;
569 if (flags & GLOB_DOOFFS)
570 while (pglob->gl_pathc < pglob->gl_offs)
571 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
573 pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
574 pglob->gl_pathv[pglob->gl_pathc] = NULL;
575 pglob->gl_flags = flags;
577 else
578 return GLOB_NOMATCH;
580 else
582 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
583 if (status != 0)
584 return status;
586 if (dirlen > 0)
588 /* Stick the directory on the front of each name. */
589 if (prefix_array (dirname,
590 &pglob->gl_pathv[oldcount],
591 pglob->gl_pathc - oldcount))
593 globfree (pglob);
594 return GLOB_NOSPACE;
599 if (flags & GLOB_MARK)
601 /* Append slashes to directory names. */
602 int i;
603 struct stat st;
604 for (i = oldcount; i < pglob->gl_pathc; ++i)
605 if (((flags & GLOB_ALTDIRFUNC) ?
606 (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
607 __stat (pglob->gl_pathv[i], &st)) == 0 &&
608 S_ISDIR (st.st_mode))
610 size_t len = strlen (pglob->gl_pathv[i]) + 2;
611 char *new = realloc (pglob->gl_pathv[i], len);
612 if (new == NULL)
614 globfree (pglob);
615 return GLOB_NOSPACE;
617 strcpy (&new[len - 2], "/");
618 pglob->gl_pathv[i] = new;
622 if (!(flags & GLOB_NOSORT))
623 /* Sort the vector. */
624 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
625 pglob->gl_pathc - oldcount,
626 sizeof (char *), collated_compare);
628 return 0;
632 /* Free storage allocated in PGLOB by a previous `glob' call. */
633 void
634 globfree (pglob)
635 register glob_t *pglob;
637 if (pglob->gl_pathv != NULL)
639 register int i;
640 for (i = 0; i < pglob->gl_pathc; ++i)
641 if (pglob->gl_pathv[i] != NULL)
642 free ((__ptr_t) pglob->gl_pathv[i]);
643 free ((__ptr_t) pglob->gl_pathv);
648 /* Do a collated comparison of A and B. */
649 static int
650 collated_compare (a, b)
651 const __ptr_t a;
652 const __ptr_t b;
654 const char *const s1 = *(const char *const * const) a;
655 const char *const s2 = *(const char *const * const) b;
657 if (s1 == s2)
658 return 0;
659 if (s1 == NULL)
660 return 1;
661 if (s2 == NULL)
662 return -1;
663 return strcoll (s1, s2);
667 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
668 elements in place. Return nonzero if out of memory, zero if successful.
669 A slash is inserted between DIRNAME and each elt of ARRAY,
670 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
671 static int
672 prefix_array (dirname, array, n)
673 const char *dirname;
674 char **array;
675 size_t n;
677 register size_t i;
678 size_t dirlen = strlen (dirname);
680 if (dirlen == 1 && dirname[0] == '/')
681 /* DIRNAME is just "/", so normal prepending would get us "//foo".
682 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
683 dirlen = 0;
685 for (i = 0; i < n; ++i)
687 size_t eltlen = strlen (array[i]) + 1;
688 char *new = (char *) malloc (dirlen + 1 + eltlen);
689 if (new == NULL)
691 while (i > 0)
692 free ((__ptr_t) array[--i]);
693 return 1;
696 memcpy (new, dirname, dirlen);
697 new[dirlen] = '/';
698 memcpy (&new[dirlen + 1], array[i], eltlen);
699 free ((__ptr_t) array[i]);
700 array[i] = new;
703 return 0;
707 /* Return nonzero if PATTERN contains any metacharacters.
708 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
709 static int
710 glob_pattern_p (pattern, quote)
711 const char *pattern;
712 int quote;
714 register const char *p;
715 int open = 0;
717 for (p = pattern; *p != '\0'; ++p)
718 switch (*p)
720 case '?':
721 case '*':
722 return 1;
724 case '\\':
725 if (quote && p[1] != '\0')
726 ++p;
727 break;
729 case '[':
730 open = 1;
731 break;
733 case ']':
734 if (open)
735 return 1;
736 break;
739 return 0;
743 /* Like `glob', but PATTERN is a final pathname component,
744 and matches are searched for in DIRECTORY.
745 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
746 The GLOB_APPEND flag is assumed to be set (always appends). */
747 static int
748 glob_in_dir (pattern, directory, flags, errfunc, pglob)
749 const char *pattern;
750 const char *directory;
751 int flags;
752 int (*errfunc) __P ((const char *, int));
753 glob_t *pglob;
755 __ptr_t stream;
757 struct globlink
759 struct globlink *next;
760 char *name;
762 struct globlink *names = NULL;
763 size_t nfound = 0;
765 if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
767 stream = NULL;
768 flags |= GLOB_NOCHECK;
770 else
772 flags |= GLOB_MAGCHAR;
774 stream = ((flags & GLOB_ALTDIRFUNC) ?
775 (*pglob->gl_opendir) (directory) :
776 (__ptr_t) opendir (directory));
777 if (stream == NULL)
779 if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
780 (flags & GLOB_ERR))
781 return GLOB_ABEND;
783 else
784 while (1)
786 const char *name;
787 size_t len;
788 struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
789 (*pglob->gl_readdir) (stream) :
790 readdir ((DIR *) stream));
791 if (d == NULL)
792 break;
793 if (! REAL_DIR_ENTRY (d))
794 continue;
796 name = d->d_name;
798 if (fnmatch (pattern, name,
799 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
800 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
801 #ifdef _AMIGA
802 | FNM_CASEFOLD
803 #endif
804 ) == 0)
806 struct globlink *new
807 = (struct globlink *) __alloca (sizeof (struct globlink));
808 len = NAMLEN (d);
809 new->name
810 = (char *) malloc (len + 1);
811 if (new->name == NULL)
812 goto memory_error;
813 memcpy ((__ptr_t) new->name, name, len);
814 new->name[len] = '\0';
815 new->next = names;
816 names = new;
817 ++nfound;
822 if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
823 ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
824 flags |= GLOB_NOCHECK;
826 if (nfound == 0 && (flags & GLOB_NOCHECK))
828 size_t len = strlen (pattern);
829 nfound = 1;
830 names = (struct globlink *) __alloca (sizeof (struct globlink));
831 names->next = NULL;
832 names->name = (char *) malloc (len + 1);
833 if (names->name == NULL)
834 goto memory_error;
835 memcpy (names->name, pattern, len);
836 names->name[len] = '\0';
839 pglob->gl_pathv
840 = (char **) realloc (pglob->gl_pathv,
841 (pglob->gl_pathc +
842 ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
843 nfound + 1) *
844 sizeof (char *));
845 if (pglob->gl_pathv == NULL)
846 goto memory_error;
848 if (flags & GLOB_DOOFFS)
849 while (pglob->gl_pathc < pglob->gl_offs)
850 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
852 for (; names != NULL; names = names->next)
853 pglob->gl_pathv[pglob->gl_pathc++] = names->name;
854 pglob->gl_pathv[pglob->gl_pathc] = NULL;
856 pglob->gl_flags = flags;
858 if (stream != NULL)
860 int save = errno;
861 if (flags & GLOB_ALTDIRFUNC)
862 (*pglob->gl_closedir) (stream);
863 else
864 closedir ((DIR *) stream);
865 errno = save;
867 return nfound == 0 ? GLOB_NOMATCH : 0;
869 memory_error:
871 int save = errno;
872 if (flags & GLOB_ALTDIRFUNC)
873 (*pglob->gl_closedir) (stream);
874 else
875 closedir ((DIR *) stream);
876 errno = save;
878 while (names != NULL)
880 if (names->name != NULL)
881 free ((__ptr_t) names->name);
882 names = names->next;
884 return GLOB_NOSPACE;
887 #endif /* Not ELIDE_CODE. */