1/* glob.c -- file-name wildcard pattern matching for Bash.
2
3   Copyright (C) 1985-2005 Free Software Foundation, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2, or (at your option)
8   any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.  */
18
19/* To whomever it may concern: I have never seen the code which most
20   Unix programs use to perform this function.  I wrote this from scratch
21   based on specifications for the pattern matching.  --RMS.  */
22
23#include <config.h>
24
25#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
26  #pragma alloca
27#endif /* _AIX && RISC6000 && !__GNUC__ */
28
29#include "bashtypes.h"
30
31#if defined (HAVE_UNISTD_H)
32#  include <unistd.h>
33#endif
34
35#include "bashansi.h"
36#include "posixdir.h"
37#include "posixstat.h"
38#include "shmbutil.h"
39#include "xmalloc.h"
40
41#include "filecntl.h"
42#if !defined (F_OK)
43#  define F_OK 0
44#endif
45
46#include "stdc.h"
47#include "memalloc.h"
48#include "quit.h"
49
50#include "glob.h"
51#include "strmatch.h"
52
53#if !defined (HAVE_BCOPY) && !defined (bcopy)
54#  define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))
55#endif /* !HAVE_BCOPY && !bcopy */
56
57#if !defined (NULL)
58#  if defined (__STDC__)
59#    define NULL ((void *) 0)
60#  else
61#    define NULL 0x0
62#  endif /* __STDC__ */
63#endif /* !NULL */
64
65#if !defined (FREE)
66#  define FREE(x)	if (x) free (x)
67#endif
68
69/* Don't try to alloca() more than this much memory for `struct globval'
70   in glob_vector() */
71#ifndef ALLOCA_MAX
72#  define ALLOCA_MAX	100000
73#endif
74
75extern void throw_to_top_level __P((void));
76extern int sh_eaccess __P((char *, int));
77
78extern int extended_glob;
79
80/* Global variable which controls whether or not * matches .*.
81   Non-zero means don't match .*.  */
82int noglob_dot_filenames = 1;
83
84/* Global variable which controls whether or not filename globbing
85   is done without regard to case. */
86int glob_ignore_case = 0;
87
88/* Global variable to return to signify an error in globbing. */
89char *glob_error_return;
90
91/* Some forward declarations. */
92static int skipname __P((char *, char *));
93#if HANDLE_MULTIBYTE
94static int mbskipname __P((char *, char *));
95#endif
96#if HANDLE_MULTIBYTE
97static void udequote_pathname __P((char *));
98static void wdequote_pathname __P((char *));
99#else
100#  define dequote_pathname udequote_pathname
101#endif
102static void dequote_pathname __P((char *));
103static int glob_testdir __P((char *));
104static char **glob_dir_to_array __P((char *, char **, int));
105
106/* Compile `glob_loop.c' for single-byte characters. */
107#define CHAR	unsigned char
108#define INT	int
109#define L(CS)	CS
110#define INTERNAL_GLOB_PATTERN_P internal_glob_pattern_p
111#include "glob_loop.c"
112
113/* Compile `glob_loop.c' again for multibyte characters. */
114#if HANDLE_MULTIBYTE
115
116#define CHAR	wchar_t
117#define INT	wint_t
118#define L(CS)	L##CS
119#define INTERNAL_GLOB_PATTERN_P internal_glob_wpattern_p
120#include "glob_loop.c"
121
122#endif /* HANDLE_MULTIBYTE */
123
124/* And now a function that calls either the single-byte or multibyte version
125   of internal_glob_pattern_p. */
126int
127glob_pattern_p (pattern)
128     const char *pattern;
129{
130#if HANDLE_MULTIBYTE
131  size_t n;
132  wchar_t *wpattern;
133  int r;
134
135  if (MB_CUR_MAX == 1)
136    return (internal_glob_pattern_p ((unsigned char *)pattern));
137
138  /* Convert strings to wide chars, and call the multibyte version. */
139  n = xdupmbstowcs (&wpattern, NULL, pattern);
140  if (n == (size_t)-1)
141    /* Oops.  Invalid multibyte sequence.  Try it as single-byte sequence. */
142    return (internal_glob_pattern_p ((unsigned char *)pattern));
143
144  r = internal_glob_wpattern_p (wpattern);
145  free (wpattern);
146
147  return r;
148#else
149  return (internal_glob_pattern_p (pattern));
150#endif
151}
152
153/* Return 1 if DNAME should be skipped according to PAT.  Mostly concerned
154   with matching leading `.'. */
155
156static int
157skipname (pat, dname)
158     char *pat;
159     char *dname;
160{
161  /* If a leading dot need not be explicitly matched, and the pattern
162     doesn't start with a `.', don't match `.' or `..' */
163  if (noglob_dot_filenames == 0 && pat[0] != '.' &&
164	(pat[0] != '\\' || pat[1] != '.') &&
165	(dname[0] == '.' &&
166	  (dname[1] == '\0' || (dname[1] == '.' && dname[2] == '\0'))))
167    return 1;
168
169  /* If a dot must be explicity matched, check to see if they do. */
170  else if (noglob_dot_filenames && dname[0] == '.' && pat[0] != '.' &&
171	(pat[0] != '\\' || pat[1] != '.'))
172    return 1;
173
174  return 0;
175}
176
177#if HANDLE_MULTIBYTE
178/* Return 1 if DNAME should be skipped according to PAT.  Handles multibyte
179   characters in PAT and DNAME.  Mostly concerned with matching leading `.'. */
180
181static int
182mbskipname (pat, dname)
183     char *pat, *dname;
184{
185  int ret;
186  wchar_t *pat_wc, *dn_wc;
187  size_t pat_n, dn_n;
188
189  pat_n = xdupmbstowcs (&pat_wc, NULL, pat);
190  dn_n = xdupmbstowcs (&dn_wc, NULL, dname);
191
192  ret = 0;
193  if (pat_n != (size_t)-1 && dn_n !=(size_t)-1)
194    {
195      /* If a leading dot need not be explicitly matched, and the
196	 pattern doesn't start with a `.', don't match `.' or `..' */
197      if (noglob_dot_filenames == 0 && pat_wc[0] != L'.' &&
198	    (pat_wc[0] != L'\\' || pat_wc[1] != L'.') &&
199	    (dn_wc[0] == L'.' &&
200	      (dn_wc[1] == L'\0' || (dn_wc[1] == L'.' && dn_wc[2] == L'\0'))))
201	ret = 1;
202
203      /* If a leading dot must be explicity matched, check to see if the
204	 pattern and dirname both have one. */
205     else if (noglob_dot_filenames && dn_wc[0] == L'.' &&
206	   pat_wc[0] != L'.' &&
207	   (pat_wc[0] != L'\\' || pat_wc[1] != L'.'))
208	ret = 1;
209    }
210
211  FREE (pat_wc);
212  FREE (dn_wc);
213
214  return ret;
215}
216#endif /* HANDLE_MULTIBYTE */
217
218/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
219static void
220udequote_pathname (pathname)
221     char *pathname;
222{
223  register int i, j;
224
225  for (i = j = 0; pathname && pathname[i]; )
226    {
227      if (pathname[i] == '\\')
228	i++;
229
230      pathname[j++] = pathname[i++];
231
232      if (pathname[i - 1] == 0)
233	break;
234    }
235  pathname[j] = '\0';
236}
237
238#if HANDLE_MULTIBYTE
239/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
240static void
241wdequote_pathname (pathname)
242     char *pathname;
243{
244  mbstate_t ps;
245  size_t len, n;
246  wchar_t *wpathname;
247  int i, j;
248  wchar_t *orig_wpathname;
249
250  len = strlen (pathname);
251  /* Convert the strings into wide characters.  */
252  n = xdupmbstowcs (&wpathname, NULL, pathname);
253  if (n == (size_t) -1)
254    /* Something wrong. */
255    return;
256  orig_wpathname = wpathname;
257
258  for (i = j = 0; wpathname && wpathname[i]; )
259    {
260      if (wpathname[i] == L'\\')
261	i++;
262
263      wpathname[j++] = wpathname[i++];
264
265      if (wpathname[i - 1] == L'\0')
266	break;
267    }
268  wpathname[j] = L'\0';
269
270  /* Convert the wide character string into unibyte character set. */
271  memset (&ps, '\0', sizeof(mbstate_t));
272  n = wcsrtombs(pathname, (const wchar_t **)&wpathname, len, &ps);
273  pathname[len] = '\0';
274
275  /* Can't just free wpathname here; wcsrtombs changes it in many cases. */
276  free (orig_wpathname);
277}
278
279static void
280dequote_pathname (pathname)
281     char *pathname;
282{
283  if (MB_CUR_MAX > 1)
284    wdequote_pathname (pathname);
285  else
286    udequote_pathname (pathname);
287}
288#endif /* HANDLE_MULTIBYTE */
289
290/* Test whether NAME exists. */
291
292#if defined (HAVE_LSTAT)
293#  define GLOB_TESTNAME(name)  (lstat (name, &finfo))
294#else /* !HAVE_LSTAT */
295#  if !defined (AFS)
296#    define GLOB_TESTNAME(name)  (sh_eaccess (nextname, F_OK))
297#  else /* AFS */
298#    define GLOB_TESTNAME(name)  (access (nextname, F_OK))
299#  endif /* AFS */
300#endif /* !HAVE_LSTAT */
301
302/* Return 0 if DIR is a directory, -1 otherwise. */
303static int
304glob_testdir (dir)
305     char *dir;
306{
307  struct stat finfo;
308
309  if (stat (dir, &finfo) < 0)
310    return (-1);
311
312  if (S_ISDIR (finfo.st_mode) == 0)
313    return (-1);
314
315  return (0);
316}
317
318/* Return a vector of names of files in directory DIR
319   whose names match glob pattern PAT.
320   The names are not in any particular order.
321   Wildcards at the beginning of PAT do not match an initial period.
322
323   The vector is terminated by an element that is a null pointer.
324
325   To free the space allocated, first free the vector's elements,
326   then free the vector.
327
328   Return 0 if cannot get enough memory to hold the pointer
329   and the names.
330
331   Return -1 if cannot access directory DIR.
332   Look in errno for more information.  */
333
334char **
335glob_vector (pat, dir, flags)
336     char *pat;
337     char *dir;
338     int flags;
339{
340  struct globval
341    {
342      struct globval *next;
343      char *name;
344    };
345
346  DIR *d;
347  register struct dirent *dp;
348  struct globval *lastlink;
349  register struct globval *nextlink;
350  register char *nextname, *npat;
351  unsigned int count;
352  int lose, skip;
353  register char **name_vector;
354  register unsigned int i;
355  int mflags;		/* Flags passed to strmatch (). */
356  int nalloca;
357  struct globval *firstmalloc, *tmplink;
358
359  lastlink = 0;
360  count = lose = skip = 0;
361
362  firstmalloc = 0;
363  nalloca = 0;
364
365  /* If PAT is empty, skip the loop, but return one (empty) filename. */
366  if (pat == 0 || *pat == '\0')
367    {
368      if (glob_testdir (dir) < 0)
369	return ((char **) &glob_error_return);
370
371      nextlink = (struct globval *)alloca (sizeof (struct globval));
372      if (nextlink == NULL)
373	return ((char **) NULL);
374
375      nextlink->next = (struct globval *)0;
376      nextname = (char *) malloc (1);
377      if (nextname == 0)
378	lose = 1;
379      else
380	{
381	  lastlink = nextlink;
382	  nextlink->name = nextname;
383	  nextname[0] = '\0';
384	  count = 1;
385	}
386
387      skip = 1;
388    }
389
390  /* If the filename pattern (PAT) does not contain any globbing characters,
391     we can dispense with reading the directory, and just see if there is
392     a filename `DIR/PAT'.  If there is, and we can access it, just make the
393     vector to return and bail immediately. */
394  if (skip == 0 && glob_pattern_p (pat) == 0)
395    {
396      int dirlen;
397      struct stat finfo;
398
399      if (glob_testdir (dir) < 0)
400	return ((char **) &glob_error_return);
401
402      dirlen = strlen (dir);
403      nextname = (char *)malloc (dirlen + strlen (pat) + 2);
404      npat = (char *)malloc (strlen (pat) + 1);
405      if (nextname == 0 || npat == 0)
406	lose = 1;
407      else
408	{
409	  strcpy (npat, pat);
410	  dequote_pathname (npat);
411
412	  strcpy (nextname, dir);
413	  nextname[dirlen++] = '/';
414	  strcpy (nextname + dirlen, npat);
415
416	  if (GLOB_TESTNAME (nextname) >= 0)
417	    {
418	      free (nextname);
419	      nextlink = (struct globval *)alloca (sizeof (struct globval));
420	      if (nextlink)
421		{
422		  nextlink->next = (struct globval *)0;
423		  lastlink = nextlink;
424		  nextlink->name = npat;
425		  count = 1;
426		}
427	      else
428		lose = 1;
429	    }
430	  else
431	    {
432	      free (nextname);
433	      free (npat);
434	    }
435	}
436
437      skip = 1;
438    }
439
440  if (skip == 0)
441    {
442      /* Open the directory, punting immediately if we cannot.  If opendir
443	 is not robust (i.e., it opens non-directories successfully), test
444	 that DIR is a directory and punt if it's not. */
445#if defined (OPENDIR_NOT_ROBUST)
446      if (glob_testdir (dir) < 0)
447	return ((char **) &glob_error_return);
448#endif
449
450      d = opendir (dir);
451      if (d == NULL)
452	return ((char **) &glob_error_return);
453
454      /* Compute the flags that will be passed to strmatch().  We don't
455	 need to do this every time through the loop. */
456      mflags = (noglob_dot_filenames ? FNM_PERIOD : 0) | FNM_PATHNAME;
457
458#ifdef FNM_CASEFOLD
459      if (glob_ignore_case)
460	mflags |= FNM_CASEFOLD;
461#endif
462
463      if (extended_glob)
464	mflags |= FNM_EXTMATCH;
465
466      /* Scan the directory, finding all names that match.
467	 For each name that matches, allocate a struct globval
468	 on the stack and store the name in it.
469	 Chain those structs together; lastlink is the front of the chain.  */
470      while (1)
471	{
472	  /* Make globbing interruptible in the shell. */
473	  if (interrupt_state || terminating_signal)
474	    {
475	      lose = 1;
476	      break;
477	    }
478
479	  dp = readdir (d);
480	  if (dp == NULL)
481	    break;
482
483	  /* If this directory entry is not to be used, try again. */
484	  if (REAL_DIR_ENTRY (dp) == 0)
485	    continue;
486
487#if 0
488	  if (dp->d_name == 0 || *dp->d_name == 0)
489	    continue;
490#endif
491
492#if HANDLE_MULTIBYTE
493	  if (MB_CUR_MAX > 1 && mbskipname (pat, dp->d_name))
494	    continue;
495	  else
496#endif
497	  if (skipname (pat, dp->d_name))
498	    continue;
499
500	  if (strmatch (pat, dp->d_name, mflags) != FNM_NOMATCH)
501	    {
502	      if (nalloca < ALLOCA_MAX)
503		{
504		  nextlink = (struct globval *) alloca (sizeof (struct globval));
505		  nalloca += sizeof (struct globval);
506		}
507	      else
508		{
509		  nextlink = (struct globval *) malloc (sizeof (struct globval));
510		  if (firstmalloc == 0)
511		    firstmalloc = nextlink;
512		}
513	      nextname = (char *) malloc (D_NAMLEN (dp) + 1);
514	      if (nextlink == 0 || nextname == 0)
515		{
516		  lose = 1;
517		  break;
518		}
519	      nextlink->next = lastlink;
520	      lastlink = nextlink;
521	      nextlink->name = nextname;
522	      bcopy (dp->d_name, nextname, D_NAMLEN (dp) + 1);
523	      ++count;
524	    }
525	}
526
527      (void) closedir (d);
528    }
529
530  if (lose == 0)
531    {
532      name_vector = (char **) malloc ((count + 1) * sizeof (char *));
533      lose |= name_vector == NULL;
534    }
535
536  /* Have we run out of memory?	 */
537  if (lose)
538    {
539      tmplink = 0;
540
541      /* Here free the strings we have got.  */
542      while (lastlink)
543	{
544	  /* Since we build the list in reverse order, the first N entries
545	     will be allocated with malloc, if firstmalloc is set, from
546	     lastlink to firstmalloc. */
547	  if (firstmalloc)
548	    {
549	      if (lastlink == firstmalloc)
550		firstmalloc = 0;
551	      tmplink = lastlink;
552	    }
553	  else
554	    tmplink = 0;
555	  free (lastlink->name);
556	  lastlink = lastlink->next;
557	  FREE (tmplink);
558	}
559
560      QUIT;
561
562      return ((char **)NULL);
563    }
564
565  /* Copy the name pointers from the linked list into the vector.  */
566  for (tmplink = lastlink, i = 0; i < count; ++i)
567    {
568      name_vector[i] = tmplink->name;
569      tmplink = tmplink->next;
570    }
571
572  name_vector[count] = NULL;
573
574  /* If we allocated some of the struct globvals, free them now. */
575  if (firstmalloc)
576    {
577      tmplink = 0;
578      while (lastlink)
579	{
580	  tmplink = lastlink;
581	  if (lastlink == firstmalloc)
582	    lastlink = firstmalloc = 0;
583	  else
584	    lastlink = lastlink->next;
585	  free (tmplink);
586	}
587    }
588
589  return (name_vector);
590}
591
592/* Return a new array which is the concatenation of each string in ARRAY
593   to DIR.  This function expects you to pass in an allocated ARRAY, and
594   it takes care of free()ing that array.  Thus, you might think of this
595   function as side-effecting ARRAY.  This should handle GX_MARKDIRS. */
596static char **
597glob_dir_to_array (dir, array, flags)
598     char *dir, **array;
599     int flags;
600{
601  register unsigned int i, l;
602  int add_slash;
603  char **result, *new;
604  struct stat sb;
605
606  l = strlen (dir);
607  if (l == 0)
608    {
609      if (flags & GX_MARKDIRS)
610	for (i = 0; array[i]; i++)
611	  {
612	    if ((stat (array[i], &sb) == 0) && S_ISDIR (sb.st_mode))
613	      {
614		l = strlen (array[i]);
615		new = (char *)realloc (array[i], l + 2);
616		if (new == 0)
617		  return NULL;
618		new[l] = '/';
619		new[l+1] = '\0';
620		array[i] = new;
621	      }
622	  }
623      return (array);
624    }
625
626  add_slash = dir[l - 1] != '/';
627
628  i = 0;
629  while (array[i] != NULL)
630    ++i;
631
632  result = (char **) malloc ((i + 1) * sizeof (char *));
633  if (result == NULL)
634    return (NULL);
635
636  for (i = 0; array[i] != NULL; i++)
637    {
638      /* 3 == 1 for NUL, 1 for slash at end of DIR, 1 for GX_MARKDIRS */
639      result[i] = (char *) malloc (l + strlen (array[i]) + 3);
640
641      if (result[i] == NULL)
642	return (NULL);
643
644      strcpy (result[i], dir);
645      if (add_slash)
646	result[i][l] = '/';
647      strcpy (result[i] + l + add_slash, array[i]);
648      if (flags & GX_MARKDIRS)
649	{
650	  if ((stat (result[i], &sb) == 0) && S_ISDIR (sb.st_mode))
651	    {
652	      size_t rlen;
653	      rlen = strlen (result[i]);
654	      result[i][rlen] = '/';
655	      result[i][rlen+1] = '\0';
656	    }
657	}
658    }
659  result[i] = NULL;
660
661  /* Free the input array.  */
662  for (i = 0; array[i] != NULL; i++)
663    free (array[i]);
664  free ((char *) array);
665
666  return (result);
667}
668
669/* Do globbing on PATHNAME.  Return an array of pathnames that match,
670   marking the end of the array with a null-pointer as an element.
671   If no pathnames match, then the array is empty (first element is null).
672   If there isn't enough memory, then return NULL.
673   If a file system error occurs, return -1; `errno' has the error code.  */
674char **
675glob_filename (pathname, flags)
676     char *pathname;
677     int flags;
678{
679  char **result;
680  unsigned int result_size;
681  char *directory_name, *filename;
682  unsigned int directory_len;
683  int free_dirname;			/* flag */
684
685  result = (char **) malloc (sizeof (char *));
686  result_size = 1;
687  if (result == NULL)
688    return (NULL);
689
690  result[0] = NULL;
691
692  directory_name = NULL;
693
694  /* Find the filename.  */
695  filename = strrchr (pathname, '/');
696  if (filename == NULL)
697    {
698      filename = pathname;
699      directory_name = "";
700      directory_len = 0;
701      free_dirname = 0;
702    }
703  else
704    {
705      directory_len = (filename - pathname) + 1;
706      directory_name = (char *) malloc (directory_len + 1);
707
708      if (directory_name == 0)		/* allocation failed? */
709	return (NULL);
710
711      bcopy (pathname, directory_name, directory_len);
712      directory_name[directory_len] = '\0';
713      ++filename;
714      free_dirname = 1;
715    }
716
717  /* If directory_name contains globbing characters, then we
718     have to expand the previous levels.  Just recurse. */
719  if (glob_pattern_p (directory_name))
720    {
721      char **directories;
722      register unsigned int i;
723
724      if (directory_name[directory_len - 1] == '/')
725	directory_name[directory_len - 1] = '\0';
726
727      directories = glob_filename (directory_name, flags & ~GX_MARKDIRS);
728
729      if (free_dirname)
730	{
731	  free (directory_name);
732	  directory_name = NULL;
733	}
734
735      if (directories == NULL)
736	goto memory_error;
737      else if (directories == (char **)&glob_error_return)
738	{
739	  free ((char *) result);
740	  return ((char **) &glob_error_return);
741	}
742      else if (*directories == NULL)
743	{
744	  free ((char *) directories);
745	  free ((char *) result);
746	  return ((char **) &glob_error_return);
747	}
748
749      /* We have successfully globbed the preceding directory name.
750	 For each name in DIRECTORIES, call glob_vector on it and
751	 FILENAME.  Concatenate the results together.  */
752      for (i = 0; directories[i] != NULL; ++i)
753	{
754	  char **temp_results;
755
756	  /* Scan directory even on a NULL pathname.  That way, `*h/'
757	     returns only directories ending in `h', instead of all
758	     files ending in `h' with a `/' appended. */
759	  temp_results = glob_vector (filename, directories[i], flags & ~GX_MARKDIRS);
760
761	  /* Handle error cases. */
762	  if (temp_results == NULL)
763	    goto memory_error;
764	  else if (temp_results == (char **)&glob_error_return)
765	    /* This filename is probably not a directory.  Ignore it.  */
766	    ;
767	  else
768	    {
769	      char **array;
770	      register unsigned int l;
771
772	      array = glob_dir_to_array (directories[i], temp_results, flags);
773	      l = 0;
774	      while (array[l] != NULL)
775		++l;
776
777	      result =
778		(char **)realloc (result, (result_size + l) * sizeof (char *));
779
780	      if (result == NULL)
781		goto memory_error;
782
783	      for (l = 0; array[l] != NULL; ++l)
784		result[result_size++ - 1] = array[l];
785
786	      result[result_size - 1] = NULL;
787
788	      /* Note that the elements of ARRAY are not freed.  */
789	      free ((char *) array);
790	    }
791	}
792      /* Free the directories.  */
793      for (i = 0; directories[i]; i++)
794	free (directories[i]);
795
796      free ((char *) directories);
797
798      return (result);
799    }
800
801  /* If there is only a directory name, return it. */
802  if (*filename == '\0')
803    {
804      result = (char **) realloc ((char *) result, 2 * sizeof (char *));
805      if (result == NULL)
806	return (NULL);
807      /* Handle GX_MARKDIRS here. */
808      result[0] = (char *) malloc (directory_len + 1);
809      if (result[0] == NULL)
810	goto memory_error;
811      bcopy (directory_name, result[0], directory_len + 1);
812      if (free_dirname)
813	free (directory_name);
814      result[1] = NULL;
815      return (result);
816    }
817  else
818    {
819      char **temp_results;
820
821      /* There are no unquoted globbing characters in DIRECTORY_NAME.
822	 Dequote it before we try to open the directory since there may
823	 be quoted globbing characters which should be treated verbatim. */
824      if (directory_len > 0)
825	dequote_pathname (directory_name);
826
827      /* We allocated a small array called RESULT, which we won't be using.
828	 Free that memory now. */
829      free (result);
830
831      /* Just return what glob_vector () returns appended to the
832	 directory name. */
833      temp_results = glob_vector (filename,
834				  (directory_len == 0 ? "." : directory_name),
835				  flags & ~GX_MARKDIRS);
836
837      if (temp_results == NULL || temp_results == (char **)&glob_error_return)
838	{
839	  if (free_dirname)
840	    free (directory_name);
841	  return (temp_results);
842	}
843
844      result = glob_dir_to_array (directory_name, temp_results, flags);
845      if (free_dirname)
846	free (directory_name);
847      return (result);
848    }
849
850  /* We get to memory_error if the program has run out of memory, or
851     if this is the shell, and we have been interrupted. */
852 memory_error:
853  if (result != NULL)
854    {
855      register unsigned int i;
856      for (i = 0; result[i] != NULL; ++i)
857	free (result[i]);
858      free ((char *) result);
859    }
860
861  if (free_dirname && directory_name)
862    free (directory_name);
863
864  QUIT;
865
866  return (NULL);
867}
868
869#if defined (TEST)
870
871main (argc, argv)
872     int argc;
873     char **argv;
874{
875  unsigned int i;
876
877  for (i = 1; i < argc; ++i)
878    {
879      char **value = glob_filename (argv[i], 0);
880      if (value == NULL)
881	puts ("Out of memory.");
882      else if (value == &glob_error_return)
883	perror (argv[i]);
884      else
885	for (i = 0; value[i] != NULL; i++)
886	  puts (value[i]);
887    }
888
889  exit (0);
890}
891#endif	/* TEST.  */
892