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