• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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 License
14 along with this library; see the file COPYING.LIB.  If not, write to the Free
15 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
16 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 /* Outcomment the following line for production quality code.  */
37 /* #define NDEBUG 1 */
38 #include <assert.h>
39 
40 #include <stdio.h>		/* Needed on stupid SunOS for assert.  */
41 
42 
43 /* Comment out all this code if we are using the GNU C Library, and are not
44    actually compiling the library itself.  This code is part of the GNU C
45    Library, but also included in many other GNU distributions.  Compiling
46    and linking in this code is a waste when using the GNU C library
47    (especially if it is a shared library).  Rather than having every GNU
48    program understand `configure --with-gnu-libc' and omit the object files,
49    it is simpler to just do this in the source for each such file.  */
50 
51 #define GLOB_INTERFACE_VERSION 1
52 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
53 # include <gnu-versions.h>
54 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
55 #  define ELIDE_CODE
56 # endif
57 #endif
58 
59 #ifndef ELIDE_CODE
60 
61 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
62 # include <stddef.h>
63 #endif
64 
65 #if defined HAVE_UNISTD_H || defined _LIBC
66 # include <unistd.h>
67 # ifndef POSIX
68 #  ifdef _POSIX_VERSION
69 #   define POSIX
70 #  endif
71 # endif
72 #endif
73 
74 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
75 # include <pwd.h>
76 #endif
77 
78 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
79 extern int errno;
80 #endif
81 #ifndef __set_errno
82 # define __set_errno(val) errno = (val)
83 #endif
84 
85 #ifndef	NULL
86 # define NULL	0
87 #endif
88 
89 
90 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
91 # include <dirent.h>
92 # define NAMLEN(dirent) strlen((dirent)->d_name)
93 #else
94 # define dirent direct
95 # define NAMLEN(dirent) (dirent)->d_namlen
96 # ifdef HAVE_SYS_NDIR_H
97 #  include <sys/ndir.h>
98 # endif
99 # ifdef HAVE_SYS_DIR_H
100 #  include <sys/dir.h>
101 # endif
102 # ifdef HAVE_NDIR_H
103 #  include <ndir.h>
104 # endif
105 # ifdef HAVE_VMSDIR_H
106 #  include "vmsdir.h"
107 # endif /* HAVE_VMSDIR_H */
108 #endif
109 
110 
111 /* In GNU systems, <dirent.h> defines this macro for us.  */
112 #ifdef _D_NAMLEN
113 # undef NAMLEN
114 # define NAMLEN(d) _D_NAMLEN(d)
115 #endif
116 
117 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
118    if the `d_type' member for `struct dirent' is available.  */
119 #ifdef _DIRENT_HAVE_D_TYPE
120 # define HAVE_D_TYPE	1
121 #endif
122 
123 
124 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
125 /* Posix does not require that the d_ino field be present, and some
126    systems do not provide it. */
127 # define REAL_DIR_ENTRY(dp) 1
128 #else
129 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
130 #endif /* POSIX */
131 
132 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
133 # include <stdlib.h>
134 # include <string.h>
135 # define	ANSI_STRING
136 #else	/* No standard headers.  */
137 
138 extern char *getenv ();
139 
140 # ifdef HAVE_STRING_H
141 #  include <string.h>
142 #  define ANSI_STRING
143 # else
144 #  include <strings.h>
145 # endif
146 # ifdef	HAVE_MEMORY_H
147 #  include <memory.h>
148 # endif
149 
150 extern char *malloc (), *realloc ();
151 extern void free ();
152 
153 extern void qsort ();
154 extern void abort (), exit ();
155 
156 #endif	/* Standard headers.  */
157 
158 #ifndef	ANSI_STRING
159 
160 # ifndef bzero
161 extern void bzero ();
162 # endif
163 # ifndef bcopy
164 extern void bcopy ();
165 # endif
166 
167 # define memcpy(d, s, n)	bcopy ((s), (d), (n))
168 # define strrchr	rindex
169 /* memset is only used for zero here, but let's be paranoid.  */
170 # define memset(s, better_be_zero, n) \
171   ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
172 #endif	/* Not ANSI_STRING.  */
173 
174 #if !defined HAVE_STRCOLL && !defined _LIBC
175 # define strcoll	strcmp
176 #endif
177 
178 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
179 # define HAVE_MEMPCPY	1
180 # undef  mempcpy
181 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
182 #endif
183 
184 #ifndef	__GNU_LIBRARY__
185 # ifdef	__GNUC__
186 __inline
187 # endif
188 # ifndef __SASC
189 #  ifdef WINDOWS32
190 static void *
my_realloc(void * p,unsigned int n)191 my_realloc (void *p, unsigned int n)
192 #  else
193 static char *
194 my_realloc (p, n)
195      char *p;
196      unsigned int n;
197 # endif
198 {
199   /* These casts are the for sake of the broken Ultrix compiler,
200      which warns of illegal pointer combinations otherwise.  */
201   if (p == NULL)
202     return (char *) malloc (n);
203   return (char *) realloc (p, n);
204 }
205 # define	realloc	my_realloc
206 # endif /* __SASC */
207 #endif /* __GNU_LIBRARY__ */
208 
209 
210 #if !defined __alloca && !defined __GNU_LIBRARY__
211 
212 # ifdef	__GNUC__
213 #  undef alloca
214 #  define alloca(n)	__builtin_alloca (n)
215 # else	/* Not GCC.  */
216 #  ifdef HAVE_ALLOCA_H
217 #   include <alloca.h>
218 #  else	/* Not HAVE_ALLOCA_H.  */
219 #   ifndef _AIX
220 #    ifdef WINDOWS32
221 #     include <malloc.h>
222 #    else
223 extern char *alloca ();
224 #    endif /* WINDOWS32 */
225 #   endif /* Not _AIX.  */
226 #  endif /* sparc or HAVE_ALLOCA_H.  */
227 # endif	/* GCC.  */
228 
229 # define __alloca	alloca
230 
231 #endif
232 
233 #ifndef __GNU_LIBRARY__
234 # define __stat stat
235 # ifdef STAT_MACROS_BROKEN
236 #  undef S_ISDIR
237 # endif
238 # ifndef S_ISDIR
239 #  define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
240 # endif
241 #endif
242 
243 #ifdef _LIBC
244 # undef strdup
245 # define strdup(str) __strdup (str)
246 # define sysconf(id) __sysconf (id)
247 # define closedir(dir) __closedir (dir)
248 # define opendir(name) __opendir (name)
249 # define readdir(str) __readdir (str)
250 # define getpwnam_r(name, bufp, buf, len, res) \
251    __getpwnam_r (name, bufp, buf, len, res)
252 # ifndef __stat
253 #  define __stat(fname, buf) __xstat (_STAT_VER, fname, buf)
254 # endif
255 #endif
256 
257 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
258 # undef	size_t
259 # define size_t	unsigned int
260 #endif
261 
262 /* Some system header files erroneously define these.
263    We want our own definitions from <fnmatch.h> to take precedence.  */
264 #ifndef __GNU_LIBRARY__
265 # undef	FNM_PATHNAME
266 # undef	FNM_NOESCAPE
267 # undef	FNM_PERIOD
268 #endif
269 #include <fnmatch.h>
270 
271 /* Some system header files erroneously define these.
272    We want our own definitions from <glob.h> to take precedence.  */
273 #ifndef __GNU_LIBRARY__
274 # undef	GLOB_ERR
275 # undef	GLOB_MARK
276 # undef	GLOB_NOSORT
277 # undef	GLOB_DOOFFS
278 # undef	GLOB_NOCHECK
279 # undef	GLOB_APPEND
280 # undef	GLOB_NOESCAPE
281 # undef	GLOB_PERIOD
282 #endif
283 #include <glob.h>
284 
285 #ifdef HAVE_GETLOGIN_R
286 extern int getlogin_r __P ((char *, size_t));
287 #else
288 extern char *getlogin __P ((void));
289 #endif
290 
291 static
292 #if __GNUC__ - 0 >= 2
293 inline
294 #endif
295 const char *next_brace_sub __P ((const char *begin));
296 static int glob_in_dir __P ((const char *pattern, const char *directory,
297 			     int flags,
298 			     int (*errfunc) (const char *, int),
299 			     glob_t *pglob));
300 static int prefix_array __P ((const char *prefix, char **array, size_t n));
301 static int collated_compare __P ((const __ptr_t, const __ptr_t));
302 
303 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
304 int __glob_pattern_p __P ((const char *pattern, int quote));
305 #endif
306 
307 /* Find the end of the sub-pattern in a brace expression.  We define
308    this as an inline function if the compiler permits.  */
309 static
310 #if __GNUC__ - 0 >= 2
311 inline
312 #endif
313 const char *
next_brace_sub(begin)314 next_brace_sub (begin)
315      const char *begin;
316 {
317   unsigned int depth = 0;
318   const char *cp = begin;
319 
320   while (1)
321     {
322       if (depth == 0)
323 	{
324 	  if (*cp != ',' && *cp != '}' && *cp != '\0')
325 	    {
326 	      if (*cp == '{')
327 		++depth;
328 	      ++cp;
329 	      continue;
330 	    }
331 	}
332       else
333 	{
334 	  while (*cp != '\0' && (*cp != '}' || depth > 0))
335 	    {
336 	      if (*cp == '}')
337 		--depth;
338 	      ++cp;
339 	    }
340 	  if (*cp == '\0')
341 	    /* An incorrectly terminated brace expression.  */
342 	    return NULL;
343 
344 	  continue;
345 	}
346       break;
347     }
348 
349   return cp;
350 }
351 
352 /* Do glob searching for PATTERN, placing results in PGLOB.
353    The bits defined above may be set in FLAGS.
354    If a directory cannot be opened or read and ERRFUNC is not nil,
355    it is called with the pathname that caused the error, and the
356    `errno' value from the failing call; if it returns non-zero
357    `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
358    If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
359    Otherwise, `glob' returns zero.  */
360 int
glob(pattern,flags,errfunc,pglob)361 glob (pattern, flags, errfunc, pglob)
362      const char *pattern;
363      int flags;
364      int (*errfunc) __P ((const char *, int));
365      glob_t *pglob;
366 {
367   const char *filename;
368   const char *dirname;
369   size_t dirlen;
370   int status;
371   int oldcount;
372 
373   if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
374     {
375       __set_errno (EINVAL);
376       return -1;
377     }
378 
379   if (flags & GLOB_BRACE)
380     {
381       const char *begin = strchr (pattern, '{');
382       if (begin != NULL)
383 	{
384 	  /* Allocate working buffer large enough for our work.  Note that
385 	    we have at least an opening and closing brace.  */
386 	  int firstc;
387 	  char *alt_start;
388 	  const char *p;
389 	  const char *next;
390 	  const char *rest;
391 	  size_t rest_len;
392 #ifdef __GNUC__
393 	  char onealt[strlen (pattern) - 1];
394 #else
395 	  char *onealt = (char *) malloc (strlen (pattern) - 1);
396 	  if (onealt == NULL)
397 	    {
398 	      if (!(flags & GLOB_APPEND))
399 		globfree (pglob);
400 	      return GLOB_NOSPACE;
401 	    }
402 #endif
403 
404 	  /* We know the prefix for all sub-patterns.  */
405 #ifdef HAVE_MEMPCPY
406 	  alt_start = mempcpy (onealt, pattern, begin - pattern);
407 #else
408 	  memcpy (onealt, pattern, begin - pattern);
409 	  alt_start = &onealt[begin - pattern];
410 #endif
411 
412 	  /* Find the first sub-pattern and at the same time find the
413 	     rest after the closing brace.  */
414 	  next = next_brace_sub (begin + 1);
415 	  if (next == NULL)
416 	    {
417 	      /* It is an illegal expression.  */
418 #ifndef __GNUC__
419 	      free (onealt);
420 #endif
421 	      return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
422 	    }
423 
424 	  /* Now find the end of the whole brace expression.  */
425 	  rest = next;
426 	  while (*rest != '}')
427 	    {
428 	      rest = next_brace_sub (rest + 1);
429 	      if (rest == NULL)
430 		{
431 		  /* It is an illegal expression.  */
432 #ifndef __GNUC__
433 		  free (onealt);
434 #endif
435 		  return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
436 		}
437 	    }
438 	  /* Please note that we now can be sure the brace expression
439 	     is well-formed.  */
440 	  rest_len = strlen (++rest) + 1;
441 
442 	  /* We have a brace expression.  BEGIN points to the opening {,
443 	     NEXT points past the terminator of the first element, and END
444 	     points past the final }.  We will accumulate result names from
445 	     recursive runs for each brace alternative in the buffer using
446 	     GLOB_APPEND.  */
447 
448 	  if (!(flags & GLOB_APPEND))
449 	    {
450 	      /* This call is to set a new vector, so clear out the
451 		 vector so we can append to it.  */
452 	      pglob->gl_pathc = 0;
453 	      pglob->gl_pathv = NULL;
454 	    }
455 	  firstc = pglob->gl_pathc;
456 
457 	  p = begin + 1;
458 	  while (1)
459 	    {
460 	      int result;
461 
462 	      /* Construct the new glob expression.  */
463 #ifdef HAVE_MEMPCPY
464 	      mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
465 #else
466 	      memcpy (alt_start, p, next - p);
467 	      memcpy (&alt_start[next - p], rest, rest_len);
468 #endif
469 
470 	      result = glob (onealt,
471 			     ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
472 			      | GLOB_APPEND), errfunc, pglob);
473 
474 	      /* If we got an error, return it.  */
475 	      if (result && result != GLOB_NOMATCH)
476 		{
477 #ifndef __GNUC__
478 		  free (onealt);
479 #endif
480 		  if (!(flags & GLOB_APPEND))
481 		    globfree (pglob);
482 		  return result;
483 		}
484 
485 	      if (*next == '}')
486 		/* We saw the last entry.  */
487 		break;
488 
489 	      p = next + 1;
490 	      next = next_brace_sub (p);
491 	      assert (next != NULL);
492 	    }
493 
494 #ifndef __GNUC__
495 	  free (onealt);
496 #endif
497 
498 	  if (pglob->gl_pathc != firstc)
499 	    /* We found some entries.  */
500 	    return 0;
501 	  else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
502 	    return GLOB_NOMATCH;
503 	}
504     }
505 
506   /* Find the filename.  */
507   filename = strrchr (pattern, '/');
508 #if defined __MSDOS__ || defined WINDOWS32
509   /* The case of "d:pattern".  Since `:' is not allowed in
510      file names, we can safely assume that wherever it
511      happens in pattern, it signals the filename part.  This
512      is so we could some day support patterns like "[a-z]:foo".  */
513   if (filename == NULL)
514     filename = strchr (pattern, ':');
515 #endif /* __MSDOS__ || WINDOWS32 */
516   if (filename == NULL)
517     {
518       /* This can mean two things: a simple name or "~name".  The later
519 	 case is nothing but a notation for a directory.  */
520       if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
521 	{
522 	  dirname = pattern;
523 	  dirlen = strlen (pattern);
524 
525 	  /* Set FILENAME to NULL as a special flag.  This is ugly but
526 	     other solutions would require much more code.  We test for
527 	     this special case below.  */
528 	  filename = NULL;
529 	}
530       else
531 	{
532 	  filename = pattern;
533 #ifdef _AMIGA
534 	  dirname = "";
535 #else
536 	  dirname = ".";
537 #endif
538 	  dirlen = 0;
539 	}
540     }
541   else if (filename == pattern)
542     {
543       /* "/pattern".  */
544       dirname = "/";
545       dirlen = 1;
546       ++filename;
547     }
548   else
549     {
550       char *newp;
551       dirlen = filename - pattern;
552 #if defined __MSDOS__ || defined WINDOWS32
553       if (*filename == ':'
554 	  || (filename > pattern + 1 && filename[-1] == ':'))
555 	{
556 	  char *drive_spec;
557 
558 	  ++dirlen;
559 	  drive_spec = (char *) __alloca (dirlen + 1);
560 #ifdef HAVE_MEMPCPY
561 	  *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
562 #else
563 	  memcpy (drive_spec, pattern, dirlen);
564 	  drive_spec[dirlen] = '\0';
565 #endif
566 	  /* For now, disallow wildcards in the drive spec, to
567 	     prevent infinite recursion in glob.  */
568 	  if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
569 	    return GLOB_NOMATCH;
570 	  /* If this is "d:pattern", we need to copy `:' to DIRNAME
571 	     as well.  If it's "d:/pattern", don't remove the slash
572 	     from "d:/", since "d:" and "d:/" are not the same.*/
573 	}
574 #endif
575       newp = (char *) __alloca (dirlen + 1);
576 #ifdef HAVE_MEMPCPY
577       *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
578 #else
579       memcpy (newp, pattern, dirlen);
580       newp[dirlen] = '\0';
581 #endif
582       dirname = newp;
583       ++filename;
584 
585       if (filename[0] == '\0'
586 #if defined __MSDOS__ || defined WINDOWS32
587           && dirname[dirlen - 1] != ':'
588 	  && (dirlen < 3 || dirname[dirlen - 2] != ':'
589 	      || dirname[dirlen - 1] != '/')
590 #endif
591 	  && dirlen > 1)
592 	/* "pattern/".  Expand "pattern", appending slashes.  */
593 	{
594 	  int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
595 	  if (val == 0)
596 	    pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
597 			       | (flags & GLOB_MARK));
598 	  return val;
599 	}
600     }
601 
602   if (!(flags & GLOB_APPEND))
603     {
604       pglob->gl_pathc = 0;
605       pglob->gl_pathv = NULL;
606     }
607 
608   oldcount = pglob->gl_pathc;
609 
610 #ifndef VMS
611   if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
612     {
613       if (dirname[1] == '\0' || dirname[1] == '/')
614 	{
615 	  /* Look up home directory.  */
616 #ifdef VMS
617 /* This isn't obvious, RTLs of DECC and VAXC know about "HOME" */
618           const char *home_dir = getenv ("SYS$LOGIN");
619 #else
620           const char *home_dir = getenv ("HOME");
621 #endif
622 # ifdef _AMIGA
623 	  if (home_dir == NULL || home_dir[0] == '\0')
624 	    home_dir = "SYS:";
625 # else
626 #  ifdef WINDOWS32
627 	  if (home_dir == NULL || home_dir[0] == '\0')
628             home_dir = "c:/users/default"; /* poor default */
629 #  else
630 #   ifdef VMS
631 /* Again, this isn't obvious, if "HOME" isn't known "SYS$LOGIN" should be set */
632 	  if (home_dir == NULL || home_dir[0] == '\0')
633 	    home_dir = "SYS$DISK:[]";
634 #   else
635 	  if (home_dir == NULL || home_dir[0] == '\0')
636 	    {
637 	      int success;
638 	      char *name;
639 #   if defined HAVE_GETLOGIN_R || defined _LIBC
640 	      size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
641 
642 	      if (buflen == 0)
643 		/* `sysconf' does not support _SC_LOGIN_NAME_MAX.  Try
644 		   a moderate value.  */
645 		buflen = 20;
646 	      name = (char *) __alloca (buflen);
647 
648 	      success = getlogin_r (name, buflen) >= 0;
649 #   else
650 	      success = (name = getlogin ()) != NULL;
651 #   endif
652 	      if (success)
653 		{
654 		  struct passwd *p;
655 #   if defined HAVE_GETPWNAM_R || defined _LIBC
656 		  size_t pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
657 		  char *pwtmpbuf;
658 		  struct passwd pwbuf;
659 		  int save = errno;
660 
661 		  if (pwbuflen == -1)
662 		    /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
663 		       Try a moderate value.  */
664 		    pwbuflen = 1024;
665 		  pwtmpbuf = (char *) __alloca (pwbuflen);
666 
667 		  while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
668 			 != 0)
669 		    {
670 		      if (errno != ERANGE)
671 			{
672 			  p = NULL;
673 			  break;
674 			}
675 		      pwbuflen *= 2;
676 		      pwtmpbuf = (char *) __alloca (pwbuflen);
677 		      __set_errno (save);
678 		    }
679 #   else
680 		  p = getpwnam (name);
681 #   endif
682 		  if (p != NULL)
683 		    home_dir = p->pw_dir;
684 		}
685 	    }
686 	  if (home_dir == NULL || home_dir[0] == '\0')
687 	    {
688 	      if (flags & GLOB_TILDE_CHECK)
689 		return GLOB_NOMATCH;
690 	      else
691 		home_dir = "~"; /* No luck.  */
692 	    }
693 #   endif /* VMS */
694 #  endif /* WINDOWS32 */
695 # endif
696 	  /* Now construct the full directory.  */
697 	  if (dirname[1] == '\0')
698 	    dirname = home_dir;
699 	  else
700 	    {
701 	      char *newp;
702 	      size_t home_len = strlen (home_dir);
703 	      newp = (char *) __alloca (home_len + dirlen);
704 # ifdef HAVE_MEMPCPY
705 	      mempcpy (mempcpy (newp, home_dir, home_len),
706 		       &dirname[1], dirlen);
707 # else
708 	      memcpy (newp, home_dir, home_len);
709 	      memcpy (&newp[home_len], &dirname[1], dirlen);
710 # endif
711 	      dirname = newp;
712 	    }
713 	}
714 # if !defined _AMIGA && !defined WINDOWS32 && !defined VMS
715       else
716 	{
717 	  char *end_name = strchr (dirname, '/');
718 	  const char *user_name;
719 	  const char *home_dir;
720 
721 	  if (end_name == NULL)
722 	    user_name = dirname + 1;
723 	  else
724 	    {
725 	      char *newp;
726 	      newp = (char *) __alloca (end_name - dirname);
727 # ifdef HAVE_MEMPCPY
728 	      *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
729 		= '\0';
730 # else
731 	      memcpy (newp, dirname + 1, end_name - dirname);
732 	      newp[end_name - dirname - 1] = '\0';
733 # endif
734 	      user_name = newp;
735 	    }
736 
737 	  /* Look up specific user's home directory.  */
738 	  {
739 	    struct passwd *p;
740 #  if defined HAVE_GETPWNAM_R || defined _LIBC
741 	    size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
742 	    char *pwtmpbuf;
743 	    struct passwd pwbuf;
744 	    int save = errno;
745 
746 	    if (buflen == -1)
747 	      /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.  Try a
748 		 moderate value.  */
749 	      buflen = 1024;
750 	    pwtmpbuf = (char *) __alloca (buflen);
751 
752 	    while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
753 	      {
754 		if (errno != ERANGE)
755 		  {
756 		    p = NULL;
757 		    break;
758 		  }
759 		buflen *= 2;
760 		pwtmpbuf = __alloca (buflen);
761 		__set_errno (save);
762 	      }
763 #  else
764 	    p = getpwnam (user_name);
765 #  endif
766 	    if (p != NULL)
767 	      home_dir = p->pw_dir;
768 	    else
769 	      home_dir = NULL;
770 	  }
771 	  /* If we found a home directory use this.  */
772 	  if (home_dir != NULL)
773 	    {
774 	      char *newp;
775 	      size_t home_len = strlen (home_dir);
776 	      size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
777 	      newp = (char *) __alloca (home_len + rest_len + 1);
778 #  ifdef HAVE_MEMPCPY
779 	      *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
780 				  end_name, rest_len)) = '\0';
781 #  else
782 	      memcpy (newp, home_dir, home_len);
783 	      memcpy (&newp[home_len], end_name, rest_len);
784 	      newp[home_len + rest_len] = '\0';
785 #  endif
786 	      dirname = newp;
787 	    }
788 	  else
789 	    if (flags & GLOB_TILDE_CHECK)
790 	      /* We have to regard it as an error if we cannot find the
791 		 home directory.  */
792 	      return GLOB_NOMATCH;
793 	}
794 # endif	/* Not Amiga && not WINDOWS32 && not VMS.  */
795     }
796 #endif	/* Not VMS.  */
797 
798   /* Now test whether we looked for "~" or "~NAME".  In this case we
799      can give the answer now.  */
800   if (filename == NULL)
801     {
802       struct stat st;
803 
804       /* Return the directory if we don't check for error or if it exists.  */
805       if ((flags & GLOB_NOCHECK)
806 	  || (((flags & GLOB_ALTDIRFUNC)
807 	       ? (*pglob->gl_stat) (dirname, &st)
808 	       : __stat (dirname, &st)) == 0
809 	      && S_ISDIR (st.st_mode)))
810 	{
811 	  pglob->gl_pathv
812 	    = (char **) realloc (pglob->gl_pathv,
813 				 (pglob->gl_pathc +
814 				  ((flags & GLOB_DOOFFS) ?
815 				   pglob->gl_offs : 0) +
816 				  1 + 1) *
817 				 sizeof (char *));
818 	  if (pglob->gl_pathv == NULL)
819 	    return GLOB_NOSPACE;
820 
821 	  if (flags & GLOB_DOOFFS)
822 	    while (pglob->gl_pathc < pglob->gl_offs)
823 	      pglob->gl_pathv[pglob->gl_pathc++] = NULL;
824 
825 #if defined HAVE_STRDUP || defined _LIBC
826 	  pglob->gl_pathv[pglob->gl_pathc] = strdup (dirname);
827 #else
828 	  {
829 	    size_t len = strlen (dirname) + 1;
830 	    char *dircopy = malloc (len);
831 	    if (dircopy != NULL)
832 	      pglob->gl_pathv[pglob->gl_pathc] = memcpy (dircopy, dirname,
833 							 len);
834 	  }
835 #endif
836 	  if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
837 	    {
838 	      free (pglob->gl_pathv);
839 	      return GLOB_NOSPACE;
840 	    }
841 	  pglob->gl_pathv[++pglob->gl_pathc] = NULL;
842 	  pglob->gl_flags = flags;
843 
844 	  return 0;
845 	}
846 
847       /* Not found.  */
848       return GLOB_NOMATCH;
849     }
850 
851   if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
852     {
853       /* The directory name contains metacharacters, so we
854 	 have to glob for the directory, and then glob for
855 	 the pattern in each directory found.  */
856       glob_t dirs;
857       register int i;
858 
859       status = glob (dirname,
860 		     ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE))
861 		      | GLOB_NOSORT | GLOB_ONLYDIR),
862 		     errfunc, &dirs);
863       if (status != 0)
864 	return status;
865 
866       /* We have successfully globbed the preceding directory name.
867 	 For each name we found, call glob_in_dir on it and FILENAME,
868 	 appending the results to PGLOB.  */
869       for (i = 0; i < dirs.gl_pathc; ++i)
870 	{
871 	  int old_pathc;
872 
873 #ifdef	SHELL
874 	  {
875 	    /* Make globbing interruptible in the bash shell. */
876 	    extern int interrupt_state;
877 
878 	    if (interrupt_state)
879 	      {
880 		globfree (&dirs);
881 		globfree (&files);
882 		return GLOB_ABORTED;
883 	      }
884 	  }
885 #endif /* SHELL.  */
886 
887 	  old_pathc = pglob->gl_pathc;
888 	  status = glob_in_dir (filename, dirs.gl_pathv[i],
889 				((flags | GLOB_APPEND)
890 				 & ~(GLOB_NOCHECK | GLOB_ERR)),
891 				errfunc, pglob);
892 	  if (status == GLOB_NOMATCH)
893 	    /* No matches in this directory.  Try the next.  */
894 	    continue;
895 
896 	  if (status != 0)
897 	    {
898 	      globfree (&dirs);
899 	      globfree (pglob);
900 	      return status;
901 	    }
902 
903 	  /* Stick the directory on the front of each name.  */
904 	  if (prefix_array (dirs.gl_pathv[i],
905 			    &pglob->gl_pathv[old_pathc],
906 			    pglob->gl_pathc - old_pathc))
907 	    {
908 	      globfree (&dirs);
909 	      globfree (pglob);
910 	      return GLOB_NOSPACE;
911 	    }
912 	}
913 
914       flags |= GLOB_MAGCHAR;
915 
916       /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
917 	 But if we have not found any matching entry and thie GLOB_NOCHECK
918 	 flag was set we must return the list consisting of the disrectory
919 	 names followed by the filename.  */
920       if (pglob->gl_pathc == oldcount)
921 	{
922 	  /* No matches.  */
923 	  if (flags & GLOB_NOCHECK)
924 	    {
925 	      size_t filename_len = strlen (filename) + 1;
926 	      char **new_pathv;
927 	      struct stat st;
928 
929 	      /* This is an pessimistic guess about the size.  */
930 	      pglob->gl_pathv
931 		= (char **) realloc (pglob->gl_pathv,
932 				     (pglob->gl_pathc +
933 				      ((flags & GLOB_DOOFFS) ?
934 				       pglob->gl_offs : 0) +
935 				      dirs.gl_pathc + 1) *
936 				     sizeof (char *));
937 	      if (pglob->gl_pathv == NULL)
938 		{
939 		  globfree (&dirs);
940 		  return GLOB_NOSPACE;
941 		}
942 
943 	      if (flags & GLOB_DOOFFS)
944 		while (pglob->gl_pathc < pglob->gl_offs)
945 		  pglob->gl_pathv[pglob->gl_pathc++] = NULL;
946 
947 	      for (i = 0; i < dirs.gl_pathc; ++i)
948 		{
949 		  const char *dir = dirs.gl_pathv[i];
950 		  size_t dir_len = strlen (dir);
951 
952 		  /* First check whether this really is a directory.  */
953 		  if (((flags & GLOB_ALTDIRFUNC)
954 		       ? (*pglob->gl_stat) (dir, &st) : __stat (dir, &st)) != 0
955 		      || !S_ISDIR (st.st_mode))
956 		    /* No directory, ignore this entry.  */
957 		    continue;
958 
959 		  pglob->gl_pathv[pglob->gl_pathc] = malloc (dir_len + 1
960 							     + filename_len);
961 		  if (pglob->gl_pathv[pglob->gl_pathc] == NULL)
962 		    {
963 		      globfree (&dirs);
964 		      globfree (pglob);
965 		      return GLOB_NOSPACE;
966 		    }
967 
968 #ifdef HAVE_MEMPCPY
969 		  mempcpy (mempcpy (mempcpy (pglob->gl_pathv[pglob->gl_pathc],
970 					     dir, dir_len),
971 				    "/", 1),
972 			   filename, filename_len);
973 #else
974 		  memcpy (pglob->gl_pathv[pglob->gl_pathc], dir, dir_len);
975 		  pglob->gl_pathv[pglob->gl_pathc][dir_len] = '/';
976 		  memcpy (&pglob->gl_pathv[pglob->gl_pathc][dir_len + 1],
977 			  filename, filename_len);
978 #endif
979 		  ++pglob->gl_pathc;
980 		}
981 
982 	      pglob->gl_pathv[pglob->gl_pathc] = NULL;
983 	      pglob->gl_flags = flags;
984 
985 	      /* Now we know how large the gl_pathv vector must be.  */
986 	      new_pathv = (char **) realloc (pglob->gl_pathv,
987 					     ((pglob->gl_pathc + 1)
988 					      * sizeof (char *)));
989 	      if (new_pathv != NULL)
990 		pglob->gl_pathv = new_pathv;
991 	    }
992 	  else
993 	    return GLOB_NOMATCH;
994 	}
995 
996       globfree (&dirs);
997     }
998   else
999     {
1000       status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
1001       if (status != 0)
1002 	return status;
1003 
1004       if (dirlen > 0)
1005 	{
1006 	  /* Stick the directory on the front of each name.  */
1007 	  int ignore = oldcount;
1008 
1009 	  if ((flags & GLOB_DOOFFS) && ignore < pglob->gl_offs)
1010 	    ignore = pglob->gl_offs;
1011 
1012 	  if (prefix_array (dirname,
1013 			    &pglob->gl_pathv[ignore],
1014 			    pglob->gl_pathc - ignore))
1015 	    {
1016 	      globfree (pglob);
1017 	      return GLOB_NOSPACE;
1018 	    }
1019 	}
1020     }
1021 
1022   if (flags & GLOB_MARK)
1023     {
1024       /* Append slashes to directory names.  */
1025       int i;
1026       struct stat st;
1027       for (i = oldcount; i < pglob->gl_pathc; ++i)
1028 	if (((flags & GLOB_ALTDIRFUNC)
1029 	     ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
1030 	     : __stat (pglob->gl_pathv[i], &st)) == 0
1031 	    && S_ISDIR (st.st_mode))
1032 	  {
1033  	    size_t len = strlen (pglob->gl_pathv[i]) + 2;
1034 	    char *new = realloc (pglob->gl_pathv[i], len);
1035  	    if (new == NULL)
1036 	      {
1037 		globfree (pglob);
1038 		return GLOB_NOSPACE;
1039 	      }
1040 	    strcpy (&new[len - 2], "/");
1041 	    pglob->gl_pathv[i] = new;
1042 	  }
1043     }
1044 
1045   if (!(flags & GLOB_NOSORT))
1046     {
1047       /* Sort the vector.  */
1048       int non_sort = oldcount;
1049 
1050       if ((flags & GLOB_DOOFFS) && pglob->gl_offs > oldcount)
1051 	non_sort = pglob->gl_offs;
1052 
1053       qsort ((__ptr_t) &pglob->gl_pathv[non_sort],
1054 	     pglob->gl_pathc - non_sort,
1055 	     sizeof (char *), collated_compare);
1056     }
1057 
1058   return 0;
1059 }
1060 
1061 
1062 /* Free storage allocated in PGLOB by a previous `glob' call.  */
1063 void
globfree(pglob)1064 globfree (pglob)
1065      register glob_t *pglob;
1066 {
1067   if (pglob->gl_pathv != NULL)
1068     {
1069       register int i;
1070       for (i = 0; i < pglob->gl_pathc; ++i)
1071 	if (pglob->gl_pathv[i] != NULL)
1072 	  free ((__ptr_t) pglob->gl_pathv[i]);
1073       free ((__ptr_t) pglob->gl_pathv);
1074     }
1075 }
1076 
1077 
1078 /* Do a collated comparison of A and B.  */
1079 static int
collated_compare(a,b)1080 collated_compare (a, b)
1081      const __ptr_t a;
1082      const __ptr_t b;
1083 {
1084   const char *const s1 = *(const char *const * const) a;
1085   const char *const s2 = *(const char *const * const) b;
1086 
1087   if (s1 == s2)
1088     return 0;
1089   if (s1 == NULL)
1090     return 1;
1091   if (s2 == NULL)
1092     return -1;
1093   return strcoll (s1, s2);
1094 }
1095 
1096 
1097 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1098    elements in place.  Return nonzero if out of memory, zero if successful.
1099    A slash is inserted between DIRNAME and each elt of ARRAY,
1100    unless DIRNAME is just "/".  Each old element of ARRAY is freed.  */
1101 static int
prefix_array(dirname,array,n)1102 prefix_array (dirname, array, n)
1103      const char *dirname;
1104      char **array;
1105      size_t n;
1106 {
1107   register size_t i;
1108   size_t dirlen = strlen (dirname);
1109 #if defined __MSDOS__ || defined WINDOWS32
1110   int sep_char = '/';
1111 # define DIRSEP_CHAR sep_char
1112 #else
1113 # define DIRSEP_CHAR '/'
1114 #endif
1115 
1116   if (dirlen == 1 && dirname[0] == '/')
1117     /* DIRNAME is just "/", so normal prepending would get us "//foo".
1118        We want "/foo" instead, so don't prepend any chars from DIRNAME.  */
1119     dirlen = 0;
1120 #if defined __MSDOS__ || defined WINDOWS32
1121   else if (dirlen > 1)
1122     {
1123       if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1124 	/* DIRNAME is "d:/".  Don't prepend the slash from DIRNAME.  */
1125 	--dirlen;
1126       else if (dirname[dirlen - 1] == ':')
1127 	{
1128 	  /* DIRNAME is "d:".  Use `:' instead of `/'.  */
1129 	  --dirlen;
1130 	  sep_char = ':';
1131 	}
1132     }
1133 #endif
1134 
1135   for (i = 0; i < n; ++i)
1136     {
1137       size_t eltlen = strlen (array[i]) + 1;
1138       char *new = (char *) malloc (dirlen + 1 + eltlen);
1139       if (new == NULL)
1140 	{
1141 	  while (i > 0)
1142 	    free ((__ptr_t) array[--i]);
1143 	  return 1;
1144 	}
1145 
1146 #ifdef HAVE_MEMPCPY
1147       {
1148 	char *endp = (char *) mempcpy (new, dirname, dirlen);
1149 	*endp++ = DIRSEP_CHAR;
1150 	mempcpy (endp, array[i], eltlen);
1151       }
1152 #else
1153       memcpy (new, dirname, dirlen);
1154       new[dirlen] = DIRSEP_CHAR;
1155       memcpy (&new[dirlen + 1], array[i], eltlen);
1156 #endif
1157       free ((__ptr_t) array[i]);
1158       array[i] = new;
1159     }
1160 
1161   return 0;
1162 }
1163 
1164 
1165 /* We must not compile this function twice.  */
1166 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1167 /* Return nonzero if PATTERN contains any metacharacters.
1168    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
1169 int
__glob_pattern_p(pattern,quote)1170 __glob_pattern_p (pattern, quote)
1171      const char *pattern;
1172      int quote;
1173 {
1174   register const char *p;
1175   int open = 0;
1176 
1177   for (p = pattern; *p != '\0'; ++p)
1178     switch (*p)
1179       {
1180       case '?':
1181       case '*':
1182 	return 1;
1183 
1184       case '\\':
1185 	if (quote && p[1] != '\0')
1186 	  ++p;
1187 	break;
1188 
1189       case '[':
1190 	open = 1;
1191 	break;
1192 
1193       case ']':
1194 	if (open)
1195 	  return 1;
1196 	break;
1197       }
1198 
1199   return 0;
1200 }
1201 # ifdef _LIBC
1202 weak_alias (__glob_pattern_p, glob_pattern_p)
1203 # endif
1204 #endif
1205 
1206 
1207 /* Like `glob', but PATTERN is a final pathname component,
1208    and matches are searched for in DIRECTORY.
1209    The GLOB_NOSORT bit in FLAGS is ignored.  No sorting is ever done.
1210    The GLOB_APPEND flag is assumed to be set (always appends).  */
1211 static int
1212 glob_in_dir (pattern, directory, flags, errfunc, pglob)
1213      const char *pattern;
1214      const char *directory;
1215      int flags;
1216      int (*errfunc) __P ((const char *, int));
1217      glob_t *pglob;
1218 {
1219   __ptr_t stream = NULL;
1220 
1221   struct globlink
1222     {
1223       struct globlink *next;
1224       char *name;
1225     };
1226   struct globlink *names = NULL;
1227   size_t nfound;
1228   int meta;
1229   int save;
1230 
1231 #ifdef VMS
1232   if (*directory == 0)
1233     directory = "[]";
1234 #endif
1235   meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1236   if (meta == 0)
1237     {
1238       if (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))
1239 	/* We need not do any tests.  The PATTERN contains no meta
1240 	   characters and we must not return an error therefore the
1241 	   result will always contain exactly one name.  */
1242 	flags |= GLOB_NOCHECK;
1243       else
1244 	{
1245 	  /* Since we use the normal file functions we can also use stat()
1246 	     to verify the file is there.  */
1247 	  struct stat st;
1248 	  size_t patlen = strlen (pattern);
1249 	  size_t dirlen = strlen (directory);
1250 	  char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1251 
1252 # ifdef HAVE_MEMPCPY
1253 	  mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1254 			    "/", 1),
1255 		   pattern, patlen + 1);
1256 # else
1257 	  memcpy (fullname, directory, dirlen);
1258 	  fullname[dirlen] = '/';
1259 	  memcpy (&fullname[dirlen + 1], pattern, patlen + 1);
1260 # endif
1261 	  if (((flags & GLOB_ALTDIRFUNC)
1262 	       ? (*pglob->gl_stat) (fullname, &st)
1263 	       : __stat (fullname, &st)) == 0)
1264 	    /* We found this file to be existing.  Now tell the rest
1265 	       of the function to copy this name into the result.  */
1266 	    flags |= GLOB_NOCHECK;
1267 	}
1268 
1269       nfound = 0;
1270     }
1271   else
1272     {
1273       if (pattern[0] == '\0')
1274 	{
1275 	  /* This is a special case for matching directories like in
1276 	     "*a/".  */
1277 	  names = (struct globlink *) __alloca (sizeof (struct globlink));
1278 	  names->name = (char *) malloc (1);
1279 	  if (names->name == NULL)
1280 	    goto memory_error;
1281 	  names->name[0] = '\0';
1282 	  names->next = NULL;
1283 	  nfound = 1;
1284 	  meta = 0;
1285 	}
1286       else
1287 	{
1288 	  stream = ((flags & GLOB_ALTDIRFUNC)
1289 		    ? (*pglob->gl_opendir) (directory)
1290 		    : (__ptr_t) opendir (directory));
1291 	  if (stream == NULL)
1292 	    {
1293 	      if (errno != ENOTDIR
1294 		  && ((errfunc != NULL && (*errfunc) (directory, errno))
1295 		      || (flags & GLOB_ERR)))
1296 		return GLOB_ABORTED;
1297 	      nfound = 0;
1298 	      meta = 0;
1299 	    }
1300 	  else
1301 	    {
1302 	      int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1303 			       | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1304 #if defined HAVE_CASE_INSENSITIVE_FS
1305 				   | FNM_CASEFOLD
1306 #endif
1307 				   );
1308 	      nfound = 0;
1309 	      flags |= GLOB_MAGCHAR;
1310 
1311 	      while (1)
1312 		{
1313 		  const char *name;
1314 		  size_t len;
1315 		  struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1316 				      ? (*pglob->gl_readdir) (stream)
1317 				      : readdir ((DIR *) stream));
1318 		  if (d == NULL)
1319 		    break;
1320 		  if (! REAL_DIR_ENTRY (d))
1321 		    continue;
1322 
1323 #ifdef HAVE_D_TYPE
1324 		  /* If we shall match only directories use the information
1325 		     provided by the dirent call if possible.  */
1326 		  if ((flags & GLOB_ONLYDIR)
1327 		      && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
1328 		    continue;
1329 #endif
1330 
1331 		  name = d->d_name;
1332 
1333 		  if (fnmatch (pattern, name, fnm_flags) == 0)
1334 		    {
1335 		      struct globlink *new = (struct globlink *)
1336 			__alloca (sizeof (struct globlink));
1337 		      len = NAMLEN (d);
1338 		      new->name = (char *) malloc (len + 1);
1339 		      if (new->name == NULL)
1340 			goto memory_error;
1341 #ifdef HAVE_MEMPCPY
1342 		      *((char *) mempcpy ((__ptr_t) new->name, name, len))
1343 			= '\0';
1344 #else
1345 		      memcpy ((__ptr_t) new->name, name, len);
1346 		      new->name[len] = '\0';
1347 #endif
1348 		      new->next = names;
1349 		      names = new;
1350 		      ++nfound;
1351 		    }
1352 		}
1353 	    }
1354 	}
1355     }
1356 
1357   if (nfound == 0 && (flags & GLOB_NOCHECK))
1358     {
1359       size_t len = strlen (pattern);
1360       nfound = 1;
1361       names = (struct globlink *) __alloca (sizeof (struct globlink));
1362       names->next = NULL;
1363       names->name = (char *) malloc (len + 1);
1364       if (names->name == NULL)
1365 	goto memory_error;
1366 #ifdef HAVE_MEMPCPY
1367       *((char *) mempcpy (names->name, pattern, len)) = '\0';
1368 #else
1369       memcpy (names->name, pattern, len);
1370       names->name[len] = '\0';
1371 #endif
1372     }
1373 
1374   if (nfound != 0)
1375     {
1376       pglob->gl_pathv
1377 	= (char **) realloc (pglob->gl_pathv,
1378 			     (pglob->gl_pathc +
1379 			      ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
1380 			      nfound + 1) *
1381 			     sizeof (char *));
1382       if (pglob->gl_pathv == NULL)
1383 	goto memory_error;
1384 
1385       if (flags & GLOB_DOOFFS)
1386 	while (pglob->gl_pathc < pglob->gl_offs)
1387 	  pglob->gl_pathv[pglob->gl_pathc++] = NULL;
1388 
1389       for (; names != NULL; names = names->next)
1390 	pglob->gl_pathv[pglob->gl_pathc++] = names->name;
1391       pglob->gl_pathv[pglob->gl_pathc] = NULL;
1392 
1393       pglob->gl_flags = flags;
1394     }
1395 
1396   save = errno;
1397   if (stream != NULL)
1398     {
1399       if (flags & GLOB_ALTDIRFUNC)
1400 	(*pglob->gl_closedir) (stream);
1401       else
1402 	closedir ((DIR *) stream);
1403     }
1404   __set_errno (save);
1405 
1406   return nfound == 0 ? GLOB_NOMATCH : 0;
1407 
1408  memory_error:
1409   {
1410     int save = errno;
1411     if (flags & GLOB_ALTDIRFUNC)
1412       (*pglob->gl_closedir) (stream);
1413     else
1414       closedir ((DIR *) stream);
1415     __set_errno (save);
1416   }
1417   while (names != NULL)
1418     {
1419       if (names->name != NULL)
1420 	free ((__ptr_t) names->name);
1421       names = names->next;
1422     }
1423   return GLOB_NOSPACE;
1424 }
1425 
1426 #endif	/* Not ELIDE_CODE.  */
1427