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