7dfd7f5347bff59fc6195e3ba88d07f764d304ce
[platform/upstream/glibc.git] / posix / glob.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2
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.
7
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.
12
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.  */
17
18 /* AIX requires this to be the first thing in the file.  */
19 #if defined (_AIX) && !defined (__GNUC__)
20  #pragma alloca
21 #endif
22
23 #ifdef  HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 /* Enable GNU extensions in glob.h.  */
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE     1
30 #endif
31
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36
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.  */
44
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
52
53 #ifndef ELIDE_CODE
54
55 #ifdef  STDC_HEADERS
56 #include <stddef.h>
57 #endif
58
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
67
68 #ifndef _AMIGA
69 #include <pwd.h>
70 #endif
71
72 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
73 extern int errno;
74 #endif
75
76 #ifndef NULL
77 #define NULL    0
78 #endif
79
80
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 #endif
97
98
99 /* In GNU systems, <dirent.h> defines this macro for us.  */
100 #ifdef _D_NAMLEN
101 #undef NAMLEN
102 #define NAMLEN(d) _D_NAMLEN(d)
103 #endif
104
105
106 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
107 /* Posix does not require that the d_ino field be present, and some
108    systems do not provide it. */
109 #define REAL_DIR_ENTRY(dp) 1
110 #else
111 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
112 #endif /* POSIX */
113
114 #if     (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__))
115 #include <stdlib.h>
116 #include <string.h>
117 #define ANSI_STRING
118 #else   /* No standard headers.  */
119
120 extern char *getenv ();
121
122 #ifdef HAVE_STRING_H
123 #include <string.h>
124 #define ANSI_STRING
125 #else
126 #include <strings.h>
127 #endif
128 #ifdef  HAVE_MEMORY_H
129 #include <memory.h>
130 #endif
131
132 extern char *malloc (), *realloc ();
133 extern void free ();
134
135 extern void qsort ();
136 extern void abort (), exit ();
137
138 #endif  /* Standard headers.  */
139
140 #ifndef ANSI_STRING
141
142 #ifndef bzero
143 extern void bzero ();
144 #endif
145 #ifndef bcopy
146 extern void bcopy ();
147 #endif
148
149 #define memcpy(d, s, n) bcopy ((s), (d), (n))
150 #define strrchr rindex
151 /* memset is only used for zero here, but let's be paranoid.  */
152 #define memset(s, better_be_zero, n) \
153   ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
154 #endif  /* Not ANSI_STRING.  */
155
156 #ifndef HAVE_STRCOLL
157 #define strcoll strcmp
158 #endif
159
160
161 #ifndef __GNU_LIBRARY__
162 #ifdef  __GNUC__
163 __inline
164 #endif
165 #ifndef __SASC
166 static char *
167 my_realloc (p, n)
168      char *p;
169      unsigned int n;
170 {
171   /* These casts are the for sake of the broken Ultrix compiler,
172      which warns of illegal pointer combinations otherwise.  */
173   if (p == NULL)
174     return (char *) malloc (n);
175   return (char *) realloc (p, n);
176 }
177 #define realloc my_realloc
178 #endif /* __SASC */
179 #endif /* __GNU_LIBRARY__ */
180
181
182 #if     !defined(__alloca) && !defined(__GNU_LIBRARY__)
183
184 #ifdef  __GNUC__
185 #undef  alloca
186 #define alloca(n)       __builtin_alloca (n)
187 #else   /* Not GCC.  */
188 #if     defined (sparc) || defined (HAVE_ALLOCA_H)
189 #include <alloca.h>
190 #else   /* Not sparc or HAVE_ALLOCA_H.  */
191 #ifndef _AIX
192 extern char *alloca ();
193 #endif  /* Not _AIX.  */
194 #endif  /* sparc or HAVE_ALLOCA_H.  */
195 #endif  /* GCC.  */
196
197 #define __alloca        alloca
198
199 #endif
200
201 #ifndef __GNU_LIBRARY__
202 #define __stat stat
203 #ifdef STAT_MACROS_BROKEN
204 #undef S_ISDIR
205 #endif
206 #ifndef S_ISDIR
207 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
208 #endif
209 #endif
210
211 #ifndef STDC_HEADERS
212 #undef  size_t
213 #define size_t  unsigned int
214 #endif
215
216 /* Some system header files erroneously define these.
217    We want our own definitions from <fnmatch.h> to take precedence.  */
218 #undef  FNM_PATHNAME
219 #undef  FNM_NOESCAPE
220 #undef  FNM_PERIOD
221 #include <fnmatch.h>
222
223 /* Some system header files erroneously define these.
224    We want our own definitions from <glob.h> to take precedence.  */
225 #undef  GLOB_ERR
226 #undef  GLOB_MARK
227 #undef  GLOB_NOSORT
228 #undef  GLOB_DOOFFS
229 #undef  GLOB_NOCHECK
230 #undef  GLOB_APPEND
231 #undef  GLOB_NOESCAPE
232 #undef  GLOB_PERIOD
233 #include <glob.h>
234 \f
235 static int glob_pattern_p __P ((const char *pattern, int quote));
236 static int glob_in_dir __P ((const char *pattern, const char *directory,
237                              int flags,
238                              int (*errfunc) __P ((const char *, int)),
239                              glob_t *pglob));
240 static int prefix_array __P ((const char *prefix, char **array, size_t n));
241 static int collated_compare __P ((const __ptr_t, const __ptr_t));
242
243 /* Do glob searching for PATTERN, placing results in PGLOB.
244    The bits defined above may be set in FLAGS.
245    If a directory cannot be opened or read and ERRFUNC is not nil,
246    it is called with the pathname that caused the error, and the
247    `errno' value from the failing call; if it returns non-zero
248    `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
249    If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
250    Otherwise, `glob' returns zero.  */
251 int
252 glob (pattern, flags, errfunc, pglob)
253      const char *pattern;
254      int flags;
255      int (*errfunc) __P ((const char *, int));
256      glob_t *pglob;
257 {
258   const char *filename;
259   char *dirname;
260   size_t dirlen;
261   int status;
262   int oldcount;
263
264   if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
265     {
266       errno = EINVAL;
267       return -1;
268     }
269
270   if (flags & GLOB_BRACE)
271     {
272       const char *begin = strchr (pattern, '{');
273       if (begin != NULL)
274         {
275           int firstc;
276           size_t restlen;
277           const char *p, *end, *next;
278           unsigned int depth = 0;
279
280           /* Find the end of the brace expression, by counting braces.
281              While we're at it, notice the first comma at top brace level.  */
282           end = begin + 1;
283           next = NULL;
284           while (1)
285             {
286               switch (*end++)
287                 {
288                 case ',':
289                   if (depth == 0 && next == NULL)
290                     next = end;
291                   continue;
292                 case '{':
293                   ++depth;
294                   continue;
295                 case '}':
296                   if (depth-- == 0)
297                     break;
298                   continue;
299                 case '\0':
300                   return glob (pattern, flags &~ GLOB_BRACE, errfunc, pglob);
301                 }
302               break;
303             }
304           restlen = strlen (end) + 1;
305           if (next == NULL)
306             next = end;
307
308           /* We have a brace expression.  BEGIN points to the opening {,
309              NEXT points past the terminator of the first element, and END
310              points past the final }.  We will accumulate result names from
311              recursive runs for each brace alternative in the buffer using
312              GLOB_APPEND.  */
313
314           if (!(flags & GLOB_APPEND))
315             {
316               /* This call is to set a new vector, so clear out the
317                  vector so we can append to it.  */
318               pglob->gl_pathc = 0;
319               pglob->gl_pathv = NULL;
320             }
321           firstc = pglob->gl_pathc;
322
323           /* In this loop P points to the beginning of the current element
324              and NEXT points past its terminator.  */
325           p = begin + 1;
326           while (1)
327             {
328               /* Construct a whole name that is one of the brace
329                  alternatives in a temporary buffer.  */
330               int result;
331               size_t bufsz = (begin - pattern) + (next - 1 - p) + restlen;
332 #ifdef __GNUC__
333               char onealt[bufsz];
334 #else
335               char *onealt = malloc (bufsz);
336               if (onealt == NULL)
337                 {
338                   if (!(flags & GLOB_APPEND))
339                     globfree (pglob);
340                   return GLOB_NOSPACE;
341                 }
342 #endif
343               memcpy (onealt, pattern, begin - pattern);
344               memcpy (&onealt[begin - pattern], p, next - 1 - p);
345               memcpy (&onealt[(begin - pattern) + (next - 1 - p)],
346                       end, restlen);
347               result = glob (onealt,
348                              ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC)) |
349                               GLOB_APPEND), errfunc, pglob);
350 #ifndef __GNUC__
351               free (onealt);
352 #endif
353
354               /* If we got an error, return it.  */
355               if (result && result != GLOB_NOMATCH)
356                 {
357                   if (!(flags & GLOB_APPEND))
358                     globfree (pglob);
359                   return result;
360                 }
361
362               /* Advance past this alternative and process the next.  */
363               p = next;
364               depth = 0;
365             scan:
366               switch (*p++)
367                 {
368                 case ',':
369                   if (depth == 0)
370                     {
371                       /* Found the next alternative.  Loop to glob it.  */
372                       next = p;
373                       continue;
374                     }
375                   goto scan;
376                 case '{':
377                   ++depth;
378                   goto scan;
379                 case '}':
380                   if (depth-- == 0)
381                     /* End of the brace expression.  Break out of the loop.  */
382                     break;
383                   goto scan;
384                 }
385             }
386
387           if (pglob->gl_pathc == firstc &&
388               !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
389             return GLOB_NOMATCH;
390         }
391     }
392
393   /* Find the filename.  */
394   filename = strrchr (pattern, '/');
395   if (filename == NULL)
396     {
397       filename = pattern;
398 #ifdef _AMIGA
399       dirname = (char *) "";
400 #else
401       dirname = (char *) ".";
402 #endif
403       dirlen = 0;
404     }
405   else if (filename == pattern)
406     {
407       /* "/pattern".  */
408       dirname = (char *) "/";
409       dirlen = 1;
410       ++filename;
411     }
412   else
413     {
414       dirlen = filename - pattern;
415       dirname = (char *) __alloca (dirlen + 1);
416       memcpy (dirname, pattern, dirlen);
417       dirname[dirlen] = '\0';
418       ++filename;
419     }
420
421   if (filename[0] == '\0' && dirlen > 1)
422     /* "pattern/".  Expand "pattern", appending slashes.  */
423     {
424       int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
425       if (val == 0)
426         pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
427       return val;
428     }
429
430   if (!(flags & GLOB_APPEND))
431     {
432       pglob->gl_pathc = 0;
433       pglob->gl_pathv = NULL;
434     }
435
436   oldcount = pglob->gl_pathc;
437
438   if ((flags & GLOB_TILDE) && dirname[0] == '~')
439     {
440       if (dirname[1] == '\0')
441         {
442           /* Look up home directory.  */
443           dirname = getenv ("HOME");
444 #ifdef _AMIGA
445           if (dirname == NULL || dirname[0] == '\0')
446             dirname = "SYS:";
447 #else
448           if (dirname == NULL || dirname[0] == '\0')
449             {
450               extern char *getlogin __P ((void));
451               char *name = getlogin ();
452               if (name != NULL)
453                 {
454                   struct passwd *p = getpwnam (name);
455                   if (p != NULL)
456                     dirname = p->pw_dir;
457                 }
458             }
459           if (dirname == NULL || dirname[0] == '\0')
460             dirname = (char *) "~"; /* No luck.  */
461 #endif
462         }
463       else
464         {
465 #ifdef _AMIGA
466           if (dirname == NULL || dirname[0] == '\0')
467             dirname = "SYS:";
468 #else
469           /* Look up specific user's home directory.  */
470           struct passwd *p = getpwnam (dirname + 1);
471           if (p != NULL)
472             dirname = p->pw_dir;
473 #endif
474         }
475     }
476
477   if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
478     {
479       /* The directory name contains metacharacters, so we
480          have to glob for the directory, and then glob for
481          the pattern in each directory found.  */
482       glob_t dirs;
483       register int i;
484
485       status = glob (dirname,
486                      ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
487                       GLOB_NOSORT),
488                      errfunc, &dirs);
489       if (status != 0)
490         return status;
491
492       /* We have successfully globbed the preceding directory name.
493          For each name we found, call glob_in_dir on it and FILENAME,
494          appending the results to PGLOB.  */
495       for (i = 0; i < dirs.gl_pathc; ++i)
496         {
497           int oldcount;
498
499 #ifdef  SHELL
500           {
501             /* Make globbing interruptible in the bash shell. */
502             extern int interrupt_state;
503
504             if (interrupt_state)
505               {
506                 globfree (&dirs);
507                 globfree (&files);
508                 return GLOB_ABEND;
509               }
510           }
511 #endif /* SHELL.  */
512
513           oldcount = pglob->gl_pathc;
514           status = glob_in_dir (filename, dirs.gl_pathv[i],
515                                 (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
516                                 errfunc, pglob);
517           if (status == GLOB_NOMATCH)
518             /* No matches in this directory.  Try the next.  */
519             continue;
520
521           if (status != 0)
522             {
523               globfree (&dirs);
524               globfree (pglob);
525               return status;
526             }
527
528           /* Stick the directory on the front of each name.  */
529           if (prefix_array (dirs.gl_pathv[i],
530                             &pglob->gl_pathv[oldcount],
531                             pglob->gl_pathc - oldcount))
532             {
533               globfree (&dirs);
534               globfree (pglob);
535               return GLOB_NOSPACE;
536             }
537         }
538
539       flags |= GLOB_MAGCHAR;
540
541       if (pglob->gl_pathc == oldcount)
542         /* No matches.  */
543         if (flags & GLOB_NOCHECK)
544           {
545             size_t len = strlen (pattern) + 1;
546             char *patcopy = (char *) malloc (len);
547             if (patcopy == NULL)
548               return GLOB_NOSPACE;
549             memcpy (patcopy, pattern, len);
550
551             pglob->gl_pathv
552               = (char **) realloc (pglob->gl_pathv,
553                                    (pglob->gl_pathc +
554                                     ((flags & GLOB_DOOFFS) ?
555                                      pglob->gl_offs : 0) +
556                                     1 + 1) *
557                                    sizeof (char *));
558             if (pglob->gl_pathv == NULL)
559               {
560                 free (patcopy);
561                 return GLOB_NOSPACE;
562               }
563
564             if (flags & GLOB_DOOFFS)
565               while (pglob->gl_pathc < pglob->gl_offs)
566                 pglob->gl_pathv[pglob->gl_pathc++] = NULL;
567
568             pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
569             pglob->gl_pathv[pglob->gl_pathc] = NULL;
570             pglob->gl_flags = flags;
571           }
572         else
573           return GLOB_NOMATCH;
574     }
575   else
576     {
577       status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
578       if (status != 0)
579         return status;
580
581       if (dirlen > 0)
582         {
583           /* Stick the directory on the front of each name.  */
584           if (prefix_array (dirname,
585                             &pglob->gl_pathv[oldcount],
586                             pglob->gl_pathc - oldcount))
587             {
588               globfree (pglob);
589               return GLOB_NOSPACE;
590             }
591         }
592     }
593
594   if (flags & GLOB_MARK)
595     {
596       /* Append slashes to directory names.  */
597       int i;
598       struct stat st;
599       for (i = oldcount; i < pglob->gl_pathc; ++i)
600         if (((flags & GLOB_ALTDIRFUNC) ?
601              (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
602              __stat (pglob->gl_pathv[i], &st)) == 0 &&
603             S_ISDIR (st.st_mode))
604           {
605             size_t len = strlen (pglob->gl_pathv[i]) + 2;
606             char *new = realloc (pglob->gl_pathv[i], len);
607             if (new == NULL)
608               {
609                 globfree (pglob);
610                 return GLOB_NOSPACE;
611               }
612             strcpy (&new[len - 2], "/");
613             pglob->gl_pathv[i] = new;
614           }
615     }
616
617   if (!(flags & GLOB_NOSORT))
618     /* Sort the vector.  */
619     qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
620            pglob->gl_pathc - oldcount,
621            sizeof (char *), collated_compare);
622
623   return 0;
624 }
625
626
627 /* Free storage allocated in PGLOB by a previous `glob' call.  */
628 void
629 globfree (pglob)
630      register glob_t *pglob;
631 {
632   if (pglob->gl_pathv != NULL)
633     {
634       register int i;
635       for (i = 0; i < pglob->gl_pathc; ++i)
636         if (pglob->gl_pathv[i] != NULL)
637           free ((__ptr_t) pglob->gl_pathv[i]);
638       free ((__ptr_t) pglob->gl_pathv);
639     }
640 }
641
642
643 /* Do a collated comparison of A and B.  */
644 static int
645 collated_compare (a, b)
646      const __ptr_t a;
647      const __ptr_t b;
648 {
649   const char *const s1 = *(const char *const * const) a;
650   const char *const s2 = *(const char *const * const) b;
651
652   if (s1 == s2)
653     return 0;
654   if (s1 == NULL)
655     return 1;
656   if (s2 == NULL)
657     return -1;
658   return strcoll (s1, s2);
659 }
660
661
662 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
663    elements in place.  Return nonzero if out of memory, zero if successful.
664    A slash is inserted between DIRNAME and each elt of ARRAY,
665    unless DIRNAME is just "/".  Each old element of ARRAY is freed.  */
666 static int
667 prefix_array (dirname, array, n)
668      const char *dirname;
669      char **array;
670      size_t n;
671 {
672   register size_t i;
673   size_t dirlen = strlen (dirname);
674
675   if (dirlen == 1 && dirname[0] == '/')
676     /* DIRNAME is just "/", so normal prepending would get us "//foo".
677        We want "/foo" instead, so don't prepend any chars from DIRNAME.  */
678     dirlen = 0;
679
680   for (i = 0; i < n; ++i)
681     {
682       size_t eltlen = strlen (array[i]) + 1;
683       char *new = (char *) malloc (dirlen + 1 + eltlen);
684       if (new == NULL)
685         {
686           while (i > 0)
687             free ((__ptr_t) array[--i]);
688           return 1;
689         }
690
691       memcpy (new, dirname, dirlen);
692       new[dirlen] = '/';
693       memcpy (&new[dirlen + 1], array[i], eltlen);
694       free ((__ptr_t) array[i]);
695       array[i] = new;
696     }
697
698   return 0;
699 }
700
701
702 /* Return nonzero if PATTERN contains any metacharacters.
703    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
704 static int
705 glob_pattern_p (pattern, quote)
706      const char *pattern;
707      int quote;
708 {
709   register const char *p;
710   int open = 0;
711
712   for (p = pattern; *p != '\0'; ++p)
713     switch (*p)
714       {
715       case '?':
716       case '*':
717         return 1;
718
719       case '\\':
720         if (quote && p[1] != '\0')
721           ++p;
722         break;
723
724       case '[':
725         open = 1;
726         break;
727
728       case ']':
729         if (open)
730           return 1;
731         break;
732       }
733
734   return 0;
735 }
736
737
738 /* Like `glob', but PATTERN is a final pathname component,
739    and matches are searched for in DIRECTORY.
740    The GLOB_NOSORT bit in FLAGS is ignored.  No sorting is ever done.
741    The GLOB_APPEND flag is assumed to be set (always appends).  */
742 static int
743 glob_in_dir (pattern, directory, flags, errfunc, pglob)
744      const char *pattern;
745      const char *directory;
746      int flags;
747      int (*errfunc) __P ((const char *, int));
748      glob_t *pglob;
749 {
750   __ptr_t stream;
751
752   struct globlink
753     {
754       struct globlink *next;
755       char *name;
756     };
757   struct globlink *names = NULL;
758   size_t nfound = 0;
759
760   if (!glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
761     {
762       stream = NULL;
763       flags |= GLOB_NOCHECK;
764     }
765   else
766     {
767       flags |= GLOB_MAGCHAR;
768
769       stream = ((flags & GLOB_ALTDIRFUNC) ?
770                 (*pglob->gl_opendir) (directory) :
771                 (__ptr_t) opendir (directory));
772       if (stream == NULL)
773         {
774           if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
775               (flags & GLOB_ERR))
776             return GLOB_ABEND;
777         }
778       else
779         while (1)
780           {
781             const char *name;
782             size_t len;
783             struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
784                                 (*pglob->gl_readdir) (stream) :
785                                 readdir ((DIR *) stream));
786             if (d == NULL)
787               break;
788             if (! REAL_DIR_ENTRY (d))
789               continue;
790
791             name = d->d_name;
792
793             if (fnmatch (pattern, name,
794                          (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
795                          ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
796 #ifdef _AMIGA
797                          | FNM_CASEFOLD
798 #endif
799                          ) == 0)
800               {
801                 struct globlink *new
802                   = (struct globlink *) __alloca (sizeof (struct globlink));
803                 len = NAMLEN (d);
804                 new->name
805                   = (char *) malloc (len + 1);
806                 if (new->name == NULL)
807                   goto memory_error;
808                 memcpy ((__ptr_t) new->name, name, len);
809                 new->name[len] = '\0';
810                 new->next = names;
811                 names = new;
812                 ++nfound;
813               }
814           }
815     }
816
817   if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
818       ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
819     flags |= GLOB_NOCHECK;
820
821   if (nfound == 0 && (flags & GLOB_NOCHECK))
822     {
823       size_t len = strlen (pattern);
824       nfound = 1;
825       names = (struct globlink *) __alloca (sizeof (struct globlink));
826       names->next = NULL;
827       names->name = (char *) malloc (len + 1);
828       if (names->name == NULL)
829         goto memory_error;
830       memcpy (names->name, pattern, len);
831       names->name[len] = '\0';
832     }
833
834   pglob->gl_pathv
835     = (char **) realloc (pglob->gl_pathv,
836                          (pglob->gl_pathc +
837                           ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
838                           nfound + 1) *
839                          sizeof (char *));
840   if (pglob->gl_pathv == NULL)
841     goto memory_error;
842
843   if (flags & GLOB_DOOFFS)
844     while (pglob->gl_pathc < pglob->gl_offs)
845       pglob->gl_pathv[pglob->gl_pathc++] = NULL;
846
847   for (; names != NULL; names = names->next)
848     pglob->gl_pathv[pglob->gl_pathc++] = names->name;
849   pglob->gl_pathv[pglob->gl_pathc] = NULL;
850
851   pglob->gl_flags = flags;
852
853   if (stream != NULL)
854     {
855       int save = errno;
856       if (flags & GLOB_ALTDIRFUNC)
857         (*pglob->gl_closedir) (stream);
858       else
859         closedir ((DIR *) stream);
860       errno = save;
861     }
862   return nfound == 0 ? GLOB_NOMATCH : 0;
863
864  memory_error:
865   {
866     int save = errno;
867     if (flags & GLOB_ALTDIRFUNC)
868       (*pglob->gl_closedir) (stream);
869     else
870       closedir ((DIR *) stream);
871     errno = save;
872   }
873   while (names != NULL)
874     {
875       if (names->name != NULL)
876         free ((__ptr_t) names->name);
877       names = names->next;
878     }
879   return GLOB_NOSPACE;
880 }
881
882 #endif  /* Not ELIDE_CODE.  */
883