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