home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / tcggrep2.arj / GLOB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-24  |  20.2 KB  |  861 lines

  1. /* File-name wildcard pattern matching for GNU.
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program 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
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MODIFIED FOR MSDOS BY BARRY SCHWARTZ, AUGUST 1990 */
  19.  
  20. /* To whomever it may concern: I have never seen the code which most
  21.    Unix programs use to perform this function.  I wrote this from scratch
  22.    based on specifications for the pattern matching.  --RMS.  */
  23. /* modified for Turbo C Jim Segrave 00:08:11 Sat Nov 24 1990
  24.    added prototypes and the odd cast for compiles without warnings.
  25.    changed the appropriate include file names
  26. */   
  27. #include <sys/types.h>
  28.  
  29. #ifdef MSDOS
  30. #include "msd_dir.h"
  31. #include <stddef.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #define D_NAMLEN(d) ((d)->d_namlen)
  35. #ifndef USG
  36. #define USG
  37. #endif
  38. #ifndef STDC_HEADERS
  39. #define STDC_HEADERS
  40. #endif
  41. #else /* not MSDOS */
  42. #if defined(USGr3) || defined(DIRENT) || defined(__GNU_LIBRARY__)
  43. #include <dirent.h>
  44. #define direct dirent
  45. #define D_NAMLEN(d) strlen((d)->d_name)
  46. #else /* not USGr3 or DIRENT or __GNU_LIBRARY__ */
  47. #define D_NAMLEN(d) ((d)->d_namlen)
  48. #ifdef USG
  49. #ifdef SYSNDIR
  50. #include <sys/ndir.h>
  51. #else
  52. #include "ndir.h"        /* Get ndir.h from the Emacs distribution.  */
  53. #endif /* not SYSNDIR */
  54. #else /* not USG */
  55. #include <sys/dir.h>
  56. #endif /* USG */
  57. #endif /* USGr3 */
  58. #endif /* MSDOS */
  59.  
  60. #if    defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  64. #define index strchr
  65. #define rindex strrchr
  66. #else
  67.  
  68. #ifdef USG
  69. #include <string.h>
  70. #ifdef    __TURBOC__
  71. #  include    <mem.h>
  72. #else
  73. #  include    <memory.h>
  74. #endif
  75. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  76. #define index strchr
  77. #define rindex strrchr
  78. #else /* not USG */
  79. #include <strings.h>
  80. extern void bcopy ();
  81. #endif /* not USG */
  82.  
  83. extern char *malloc ();
  84. extern char *realloc ();
  85. extern void free ();
  86.  
  87. #ifndef NULL
  88. #define NULL 0
  89. #endif
  90. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  91.  
  92. #ifdef __GNUC__
  93. #  define alloca __builtin_alloca
  94. #else /* Not GCC.  */
  95. #  ifdef sparc
  96. #    include <alloca.h>
  97. #  else /* Not sparc.  */
  98. #    ifdef MSDOS
  99. #      ifdef __TURBOC__
  100. #        include <alloc.h>
  101. #      else
  102. #        include <malloc.h>
  103. #      endif
  104. #    else /* Not MSDOS.  */
  105.        extern char *alloca ();
  106. #    endif /* MSDOS.  */
  107. #  endif /* sparc.  */
  108. #endif /* GCC.  */
  109.  
  110. /* Nonzero if '*' and '?' do not match an initial '.' for glob_filename.  */
  111. int noglob_dot_filenames = 1;
  112.  
  113. #ifdef MSDOS
  114. /* Nonzero if '\\' is to be treated as equivalent to '/' rather than as
  115.  * an escape character. */
  116. int backslash_same_as_slash = 1;
  117. #endif
  118.  
  119. #ifdef    __TURBOC__
  120. #  include <dir.h>
  121.    int glob_pattern_p (char *pattern);
  122.    char **glob_filename (char *pathname);
  123.    int glob_match (char *pattern, char *text, int dot_special);
  124.    char **glob_vector (char *pat, char *dir);
  125.    static int glob_match_after_star (char *pattern, char *text);
  126.    static char **glob_dir_to_array (char *dir, char **array);
  127. #  define _MAX_DIR    MAXDIR   
  128. #else
  129.   static int glob_match_after_star ();
  130. #endif
  131.  
  132. /* Return nonzero if PATTERN has any special globbing chars in it.  */
  133.  
  134. int
  135. glob_pattern_p (pattern)
  136.      char *pattern;
  137. {
  138.   register char *p = pattern;
  139.   register char c;
  140.  
  141.   while ((c = *p++) != '\0')
  142.     switch (c)
  143.       {
  144.       case '?':
  145.       case '[':
  146.       case '*':
  147.     return 1;
  148.  
  149.       case '\\':
  150. #ifdef MSDOS
  151.     if (backslash_same_as_slash)
  152.       break;
  153. #endif
  154.     if (*p++ == '\0')
  155.       return 0;
  156.       }
  157.  
  158.   return 0;
  159. }
  160.  
  161.  
  162. /* Match the pattern PATTERN against the string TEXT;
  163.    return 1 if it matches, 0 otherwise.
  164.  
  165.    A match means the entire string TEXT is used up in matching.
  166.  
  167.    In the pattern string, `*' matches any sequence of characters,
  168.    `?' matches any character, [SET] matches any character in the specified set,
  169.    [!SET] matches any character not in the specified set.
  170.  
  171.    A set is composed of characters or ranges; a range looks like
  172.    character hyphen character (as in 0-9 or A-Z).
  173.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  174.    Any other character in the pattern must be matched exactly.
  175.  
  176.    To suppress the special syntactic significance of any of `[]*?!-\',
  177.    and match the character exactly, precede it with a `\'.
  178.  
  179.    If DOT_SPECIAL is nonzero,
  180.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  181.  
  182. int
  183. glob_match (pattern, text, dot_special)
  184.      char *pattern, *text;
  185.      int dot_special;
  186. {
  187.   register char *p = pattern, *t = text;
  188.   register char c;
  189.  
  190.   while ((c = *p++) != '\0')
  191.     switch (c)
  192.       {
  193.       case '?':
  194.     if (*t == '\0' || (dot_special && t == text && *t == '.'))
  195.       return 0;
  196.     else
  197.       ++t;
  198.     break;
  199.  
  200.       case '\\':
  201. #ifdef MSDOS
  202.     if (backslash_same_as_slash && c != *t++)
  203.       return 0;
  204. #endif
  205.     if (*p++ != *t++)
  206.       return 0;
  207.     break;
  208.  
  209.       case '*':
  210.     if (dot_special && t == text && *t == '.')
  211.       return 0;
  212.     return glob_match_after_star (p, t);
  213.  
  214.       case '[':
  215.     {
  216.       register char c1 = *t++;
  217.       int invert;
  218.  
  219.       if (c1 == '\0')
  220.         return 0;
  221.  
  222.       invert = (*p == '!');
  223.  
  224.       if (invert)
  225.         p++;
  226.  
  227.       c = *p++;
  228.       while (1)
  229.         {
  230.           register char cstart = c, cend = c;
  231.  
  232. #ifndef MSDOS
  233.           if (c == '\\')
  234.         {
  235.           cstart = *p++;
  236.           cend = cstart;
  237.         }
  238. #else
  239.           if (c == '\\' && !backslash_same_as_slash)
  240.         {
  241.           cstart = *p++;
  242.           cend = cstart;
  243.         }
  244. #endif
  245.  
  246.  
  247.           if (cstart == '\0')
  248.         return 0;    /* Missing ']'. */
  249.  
  250.           c = *p++;
  251.  
  252.           if (c == '-')
  253.         {
  254.           cend = *p++;
  255. #ifndef MSDOS
  256.           if (cend == '\\')
  257.             cend = *p++;
  258. #else
  259.           if (cend == '\\' && !backslash_same_as_slash)
  260.             cend = *p++;
  261. #endif
  262.           if (cend == '\0')
  263.             return 0;
  264.           c = *p++;
  265.         }
  266.           if (c1 >= cstart && c1 <= cend)
  267.         goto match;
  268.           if (c == ']')
  269.         break;
  270.         }
  271.       if (!invert)
  272.         return 0;
  273.       break;
  274.  
  275.     match:
  276.       /* Skip the rest of the [...] construct that already matched.  */
  277.       while (c != ']')
  278.         {
  279.           if (c == '\0')
  280.         return 0;
  281.           c = *p++;
  282.           if (c == '\0')
  283.         return 0;
  284. #ifndef MSDOS
  285.           if (c == '\\')
  286.         p++;
  287. #else
  288.           if (c == '\\' && !backslash_same_as_slash)
  289.         p++;
  290. #endif
  291.         }
  292.       if (invert)
  293.         return 0;
  294.       break;
  295.     }
  296.  
  297.       default:
  298.     if (c != *t++)
  299.       return 0;
  300.       }
  301.  
  302.   return *t == '\0';
  303. }
  304.  
  305. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  306.  
  307. static int
  308. glob_match_after_star (pattern, text)
  309.      char *pattern, *text;
  310. {
  311.   register char *p = pattern, *t = text;
  312.   register char c, c1;
  313.  
  314.   while ((c = *p++) == '?' || c == '*')
  315.     if (c == '?' && *t++ == '\0')
  316.       return 0;
  317.  
  318.   if (c == '\0')
  319.     return 1;
  320.  
  321. #ifndef MSDOS
  322.   if (c == '\\')
  323.     c1 = *p;
  324.   else
  325.     c1 = c;
  326. #else
  327.   if (c == '\\' && !backslash_same_as_slash)
  328.     c1 = *p;
  329.   else
  330.     c1 = c;
  331. #endif
  332.  
  333.   --p;
  334.   while (1)
  335.     {
  336.       if ((c == '[' || *t == c1) && glob_match (p, t, 0))
  337.     return 1;
  338.       if (*t++ == '\0')
  339.     return 0;
  340.     }
  341. }
  342.  
  343. /* Return a vector of names of files in directory DIR
  344.    whose names match glob pattern PAT.
  345.    The names are not in any particular order.
  346.    Wildcards at the beginning of PAT do not match an initial period
  347.    if noglob_dot_filenames is nonzero.
  348.  
  349.    The vector is terminated by an element that is a null pointer.
  350.  
  351.    To free the space allocated, first free the vector's elements,
  352.    then free the vector.
  353.  
  354.    Return NULL if cannot get enough memory to hold the pointer
  355.    and the names.
  356.  
  357.    Return -1 if cannot access directory DIR.
  358.    Look in errno for more information.  */
  359.  
  360. char **
  361. glob_vector (pat, dir)
  362.      char *pat;
  363.      char *dir;
  364. {
  365.   struct globval
  366.   {
  367.     struct globval *next;
  368.     char *name;
  369.   };
  370.  
  371.   DIR *d;
  372.   register struct direct *dp;
  373.   struct globval *lastlink;
  374.   register struct globval *nextlink;
  375.   register char *nextname;
  376.   unsigned int count;
  377.   int lose;
  378.   register char **name_vector;
  379.   register unsigned int i;
  380. #ifdef MSDOS
  381.   char *allocated_dir = NULL;
  382. #endif
  383.  
  384. #ifdef MSDOS
  385.   /* We have to get rid of trailing slashes, but only if this isn't
  386.    * a root directory */
  387.   count = strlen (dir);
  388.   if (backslash_same_as_slash)
  389.     i = count > 1 && (dir[count - 1] == '/' || dir[count - 1] == '\\');
  390.   else
  391.     i = count > 1 && dir[count - 1] == '/';
  392.   if (i && dir[count - 2] != ':')
  393.     {
  394.       allocated_dir = malloc (count);
  395.       if (allocated_dir == NULL)
  396.     return NULL;
  397.       bcopy (dir, allocated_dir, count);
  398.       allocated_dir[count - 1] = '\0';
  399.       dir = allocated_dir;
  400.     }
  401. #endif
  402.  
  403.   d = opendir (dir);
  404.   if (d == NULL)
  405.     {
  406. #ifdef MSDOS
  407.       free (allocated_dir);
  408. #endif
  409.       return (char **) -1;
  410.     }
  411.  
  412.   lastlink = NULL;
  413.   count = 0;
  414.   lose = 0;
  415.  
  416.   /* Scan the directory, finding all names that match.
  417.      For each name that matches, allocate a struct globval
  418.      on the stack and store the name in it.
  419.      Chain those structs together; lastlink is the front of the chain.  */
  420.   while (1)
  421.     {
  422.       dp = readdir (d);
  423.       if (dp == NULL)
  424.     break;
  425. #ifndef MSDOS
  426.       if (dp->d_ino != 0
  427.       && glob_match (pat, dp->d_name, noglob_dot_filenames))
  428. #else
  429.       if (glob_match (pat, dp->d_name, noglob_dot_filenames))
  430. #endif
  431.     {
  432. #ifndef MSDOS
  433.       nextlink = (struct globval *) alloca (sizeof (struct globval));
  434. #else
  435.       /* Under MSDOS our stack is small--use malloc instead of alloca */
  436.       nextlink = (struct globval *) malloc (sizeof (struct globval));
  437.       if (nextlink == NULL)
  438.         {
  439.           lose = 1;
  440.           break;
  441.         }
  442. #endif
  443.       nextlink->next = lastlink;
  444.       i = D_NAMLEN (dp) + 1;
  445.       nextname = (char *) malloc (i);
  446.       if (nextname == NULL)
  447.         {
  448.           lose = 1;
  449.           break;
  450.         }
  451.       lastlink = nextlink;
  452.       nextlink->name = nextname;
  453.       bcopy (dp->d_name, nextname, i);
  454.       count++;
  455.     }
  456.     }
  457.   closedir (d);
  458.  
  459.   if (!lose)
  460.     {
  461.       name_vector = (char **) malloc ((count + 1) * sizeof (char *));
  462.       lose |= name_vector == NULL;
  463.     }
  464.  
  465.   /* Have we run out of memory?  */
  466.   if (lose)
  467.     {
  468.       /* Here free the strings we have got.  */
  469.       while (lastlink)
  470.     {
  471. #ifndef MSDOS
  472.       free (lastlink->name);
  473.       lastlink = lastlink->next;
  474. #else
  475.       /* We used malloc to allocate so we have to free by hand */
  476.  
  477.       void *temp;
  478.  
  479.       free (lastlink->name);
  480.       temp = lastlink;
  481.       lastlink = lastlink->next;
  482.       free (temp);
  483. #endif
  484.     }
  485. #ifdef MSDOS
  486.       free (allocated_dir);
  487. #endif
  488.       return NULL;
  489.     }
  490.  
  491.   /* Copy the name pointers from the linked list into the vector.  */
  492.   for (i = 0; i < count; ++i)
  493.     {
  494. #ifndef MSDOS
  495.       name_vector[i] = lastlink->name;
  496.       lastlink = lastlink->next;
  497. #else
  498.       /* We used malloc to allocate so we have to free by hand */
  499.  
  500.       void *temp;
  501.  
  502.       name_vector[i] = lastlink->name;
  503.       temp = lastlink;
  504.       lastlink = lastlink->next;
  505.       free (temp);
  506. #endif
  507.     }
  508.  
  509. #ifdef MSDOS
  510.   free (allocated_dir);
  511. #endif
  512.  
  513.   name_vector[count] = NULL;
  514.   return name_vector;
  515. }
  516.  
  517. /* Return a new array, replacing ARRAY, which is the concatenation
  518.    of each string in ARRAY to DIR.
  519.    Return NULL if out of memory.  */
  520.  
  521. static char **
  522. glob_dir_to_array (dir, array)
  523.      char *dir, **array;
  524. {
  525.   register unsigned int i, l;
  526.   int add_slash = 0;
  527.   char **result;
  528.  
  529.   l = strlen (dir);
  530.   if (l == 0)
  531.     return array;
  532.  
  533. #ifndef MSDOS
  534.   if (dir[l - 1] != '/')
  535.     add_slash++;
  536. #else
  537.   /* Treat `drive:' as if it were a slash terminated directory;
  538.    * if we were to stick on a slash then we'd be making a mistake--
  539.    * we want `no slash' to mean the current directory on the given drive. */
  540.   if (dir[l - 1] != '/' && dir[l - 1] != ':')
  541.     if (!backslash_same_as_slash || dir[l - 1] != '\\')
  542.       add_slash++;
  543. #endif
  544.  
  545.   for (i = 0; array[i] != NULL; i++)
  546.     ;
  547.  
  548.   result = (char **) malloc ((i + 1) * sizeof (char *));
  549.   if (result == NULL)
  550.     return NULL;
  551.  
  552.   for (i = 0; array[i] != NULL; i++)
  553.     {
  554.       result[i] = (char *) malloc (1 + l + add_slash + strlen (array[i]));
  555.       if (result[i] == NULL)
  556.     return NULL;
  557.       strcpy (result[i], dir);
  558.       if (add_slash)
  559.     result[i][l] = '/';
  560.       strcpy (result[i] + l + add_slash, array[i]);
  561.     }
  562.   result[i] = NULL;
  563.  
  564.   /* Free the input array.  */
  565.   for (i = 0; array[i] != NULL; i++)
  566.     free (array[i]);
  567.   free ((char *) array);
  568.   return result;
  569. }
  570.  
  571. /* Do globbing on PATHNAME.  Return an array of pathnames that match,
  572.    marking the end of the array with a null-pointer as an element.
  573.    If no pathnames match, then the array is empty (first element is null).
  574.    If there isn't enough memory, then return NULL.
  575.    If a file system error occurs, return -1; `errno' has the error code.
  576.  
  577.    Wildcards at the beginning of PAT, or following a slash,
  578.    do not match an initial period if noglob_dot_filenames is nonzero.  */
  579.  
  580. char **
  581. glob_filename (pathname)
  582.      char *pathname;
  583. {
  584.   char **result;
  585.   unsigned int result_size;
  586.   char *directory_name, *filename;
  587.   unsigned int directory_len;
  588. #ifdef MSDOS
  589.   char *string_to_append_to_directory = "";
  590. #endif
  591.  
  592. #ifdef TEST_MESSAGES
  593.   printf("pathname = %s\n",pathname);
  594. #endif
  595.   result = (char **) malloc (sizeof (char *));
  596.   result_size = 1;
  597.   if (result == NULL)
  598.     return NULL;
  599.  
  600.   result[0] = NULL;
  601.  
  602.   /* Find the filename.  */
  603.   filename = rindex (pathname, '/');
  604. #ifdef MSDOS
  605.   if (backslash_same_as_slash)
  606.     {
  607.       char *fname;
  608.  
  609.       if (filename == NULL)
  610.     fname = rindex (pathname, '\\');
  611.       else
  612.     fname = rindex (filename + 1, '\\');
  613.       if (fname != NULL)
  614.     filename = fname;
  615.     }
  616.   if (filename == NULL && isalpha (pathname[0]) && pathname[1] == ':')
  617.     filename = pathname + 1;
  618. #endif
  619.   if (filename == NULL)
  620.     {
  621.       filename = pathname;
  622.       directory_name = "";
  623.       directory_len = 0;
  624.     }
  625.   else
  626.     {
  627.       directory_len = (int) ((filename - pathname) + 1);
  628.  
  629. #ifdef MSDOS
  630.       if (filename == pathname + 1 && *filename == ':')
  631.     {
  632.       /* The files are supposed to be in the current directory on the
  633.        * specified drive.  This creates minor problems.  No matter.
  634.        * If the current directory is root, append a '/'.  If the
  635.        * current directory is not root, append a './'.  If the
  636.        * drive is invalid, then no match is possible and so set
  637.        * errno to ENOENT and return -1 */
  638.  
  639.       static char working_dir[_MAX_DIR];
  640. /*      char *get_working_directory ();    */
  641.  
  642.       if (!get_working_directory (working_dir,
  643.                        tolower (pathname[0]) - 'a' + 1))
  644.         {
  645.           errno = ENOENT;
  646.           return (char **) -1;
  647.         }
  648.  
  649.           string_to_append_to_directory =
  650.         (working_dir[0] == '\0') ? "/" : "./";
  651.     }
  652. #endif
  653.  
  654. #ifndef MSDOS
  655.       directory_name = (char *) alloca (directory_len + 1);
  656. #else
  657.       /* Under MSDOS, stack space is limited, so use malloc instead of
  658.        * alloca */
  659.       directory_name =
  660.           (char *) malloc (directory_len + 1 +
  661.                             strlen (string_to_append_to_directory));
  662.       if (directory_name == NULL)
  663.     return NULL;
  664. #endif
  665.  
  666.       bcopy (pathname, directory_name, directory_len);
  667.       directory_name[directory_len] = '\0';
  668. #ifdef MSDOS
  669.       /* Append `/' or `./' if necessary */
  670.       strcat (directory_name, string_to_append_to_directory);
  671.       directory_len += strlen (string_to_append_to_directory);
  672. #endif
  673.       ++filename;
  674.     }
  675.  
  676. #ifdef TEST_MESSAGES
  677.   printf("  directory_name = %s\n",directory_name);
  678.   printf("  filename       = %s\n",filename);
  679. #endif
  680.  
  681.   /* If directory_name contains globbing characters, then we
  682.      have to expand the previous levels.  Just recurse. */
  683.   if (glob_pattern_p (directory_name))
  684.     {
  685.       char **directories;
  686.       register unsigned int i;
  687.  
  688.       if (directory_name[directory_len - 1] == '/')
  689.     directory_name[directory_len - 1] = '\0';
  690. #ifdef MSDOS
  691.       else if (backslash_same_as_slash
  692.                 && directory_name[directory_len - 1] == '\\')
  693.     directory_name[directory_len - 1] = '\0';
  694. #endif
  695.  
  696.       directories = glob_filename (directory_name);
  697.       if (directories == NULL)
  698.     goto memory_error;
  699.       else if (directories == (char **) -1)
  700.     {
  701. #ifdef MSDOS
  702.       free ((void *) directory_name);
  703. #endif
  704.           return (char **) -1;
  705.     }
  706.       else if (*directories == NULL)
  707.     {
  708.       free ((char *) directories);
  709. #ifdef MSDOS
  710.       free ((void *) directory_name);
  711. #endif
  712.       return (char **) -1;
  713.     }
  714.  
  715.       /* We have successfully globbed the preceding directory name.
  716.      For each name in DIRECTORIES, call glob_vector on it and
  717.      FILENAME.  Concatenate the results together.  */
  718.       for (i = 0; directories[i] != NULL; i++)
  719.     {
  720.       char **temp_results = glob_vector (filename, directories[i]);
  721.       if (temp_results == NULL)
  722.         goto memory_error;
  723.       else if (temp_results == (char **) -1)
  724.         /* This filename is probably not a directory.  Ignore it.  */
  725.         ;
  726.       else
  727.         {
  728.           char **array = glob_dir_to_array (directories[i], temp_results);
  729.           register unsigned int l;
  730.  
  731.           l = 0;
  732.           while (array[l] != NULL)
  733.         ++l;
  734.  
  735.           result = (char **) realloc (result,
  736.                       (result_size + l) * sizeof (char *));
  737.           if (result == NULL)
  738.         goto memory_error;
  739.  
  740.           for (l = 0; array[l] != NULL; ++l)
  741.         result[result_size++ - 1] = array[l];
  742.           result[result_size - 1] = NULL;
  743.           free ((char *) array);
  744.         }
  745.     }
  746.       /* Free the directories.  */
  747.       for (i = 0; directories[i] != NULL; i++)
  748.     free (directories[i]);
  749.       free ((char *) directories);
  750.  
  751. #ifdef MSDOS
  752.       free ((void *) directory_name);
  753. #endif
  754.       return result;
  755.     }
  756.  
  757.   /* If there is only a directory name, return it. */
  758.   if (*filename == '\0')
  759.     {
  760.       result = (char **) realloc ((char *) result, 2 * sizeof (char *));
  761.       if (result == NULL)
  762.     {
  763. #ifdef MSDOS
  764.       free ((void *) directory_name);
  765. #endif
  766.       return NULL;
  767.     }
  768.       result[0] = (char *) malloc (directory_len + 1);
  769.       if (result[0] == NULL)
  770.     goto memory_error;
  771.       bcopy (directory_name, result[0], directory_len + 1);
  772.       result[1] = NULL;
  773. #ifdef MSDOS
  774.       free ((void *) directory_name);
  775. #endif
  776.       return result;
  777.     }
  778.   else
  779.     {
  780.       /* Otherwise, just return what glob_vector
  781.      returns appended to the directory name. */
  782.       char **temp_results = glob_vector (filename,
  783.                      (directory_len == 0
  784.                       ? "." : directory_name));
  785.  
  786.       if (temp_results == NULL || temp_results == (char **) -1)
  787.     {
  788. #ifdef MSDOS
  789.       free ((void *) directory_name);
  790. #endif
  791.       return temp_results;
  792.     }
  793.  
  794. #ifndef MSDOS
  795.       return glob_dir_to_array (directory_name, temp_results);
  796. #else
  797.       temp_results = glob_dir_to_array (directory_name, temp_results);
  798.       free ((void *) directory_name);
  799.       return temp_results;
  800. #endif
  801.     }
  802.  
  803. memory_error:;
  804. #ifdef MSDOS
  805.   free ((void *) directory_name);
  806. #endif
  807.   if (result != NULL)
  808.     {
  809.       register unsigned int i;
  810.       for (i = 0; result[i] != NULL; ++i)
  811.     free (result[i]);
  812.       free ((char *) result);
  813.     }
  814.   return NULL;
  815. }
  816.  
  817. #ifdef TEST
  818.  
  819. main (argc, argv)
  820.      int argc;
  821.      char **argv;
  822. {
  823.   char **value;
  824.   int i, optind;
  825.  
  826.   optind = 1;
  827.  
  828. #ifdef MSDOS
  829.   if (optind < argc)
  830.     {
  831.       if (strcmp (argv[optind], "0") == 0)
  832.     {
  833.       ++optind;
  834.       backslash_same_as_slash = 0;
  835.       printf ("backslash_same_as_slash = 0\n");
  836.     }
  837.       else if (strcmp (argv[optind], "1") == 0)
  838.     {
  839.       ++optind;
  840.       backslash_same_as_slash = 1;
  841.       printf ("backslash_same_as_slash = 1\n");
  842.     }
  843.     }
  844. #endif
  845.  
  846.   for (; optind < argc; optind++)
  847.     {
  848.       value = glob_filename (argv[optind]);
  849.       if (value == NULL)
  850.     puts ("virtual memory exhausted");
  851.       else if (value == (char **) -1)
  852.     perror (argv[optind]);
  853.       else
  854.     for (i = 0; value[i] != NULL; i++)
  855.       puts (value[i]);
  856.     }
  857.   exit (0);
  858. }
  859.  
  860. #endif /* TEST */
  861.