home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / GLOB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  14.6 KB  |  616 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. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/make/RCS/glob.c'v 3.58.0.1 90/07/17 00:59:14 tho Exp $
  33.  */
  34.  
  35. /* To whomever it may concern: I have never seen the code which most
  36.    Unix programs use to perform this function.  I wrote this from scratch
  37.    based on specifications for the pattern matching.  --RMS.  */
  38.  
  39. #ifdef MSDOS
  40.  
  41. #include <stdio.h>
  42. #include <sys/types.h>
  43. #include <malloc.h>
  44. #include "msd_dir.h"
  45. #define D_NAMLEN(d) ((d)->d_namlen)
  46.  
  47. #else /* not MSDOS */
  48.  
  49. #if defined(USGr3) || defined(DIRENT) || defined(__GNU_LIBRARY__)
  50. #include <dirent.h>
  51. #define direct dirent
  52. #define D_NAMLEN(d) strlen((d)->d_name)
  53. #else /* not USGr3 or DIRENT or __GNU_LIBRARY__ */
  54. #define D_NAMLEN(d) ((d)->d_namlen)
  55. #ifdef USG
  56. #ifdef SYSNDIR
  57. #include <sys/ndir.h>
  58. #else
  59. #include "ndir.h"        /* Get ndir.h from the Emacs distribution.  */
  60. #endif /* not SYSNDIR */
  61. #else /* not USG */
  62. #include <sys/dir.h>
  63. #endif /* USG */
  64. #endif /* USGr3 */
  65.  
  66. #endif /* not MSDOS */
  67.  
  68. #if    defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  72. #define index strchr
  73. #define rindex strrchr
  74. #else
  75.  
  76. #ifdef USG
  77. #include <string.h>
  78. #include <memory.h>
  79. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  80. #define index strchr
  81. #define rindex strrchr
  82. #else /* not USG */
  83. #include <strings.h>
  84. extern void bcopy ();
  85. #endif /* not USG */
  86.  
  87. extern char *malloc ();
  88. extern char *realloc ();
  89. extern void free ();
  90.  
  91. #ifndef NULL
  92. #define NULL 0
  93. #endif
  94. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  95.  
  96. #ifdef __GNUC__
  97. #define alloca __builtin_alloca
  98. #else /* Not GCC.  */
  99. #ifdef sparc
  100. #include <alloca.h>
  101. #else /* Not sparc.  */
  102. #ifndef MSDOS
  103. extern char *alloca ();
  104. #endif /* not MSDOS */
  105. #endif /* sparc.  */
  106. #endif /* GCC.  */
  107.  
  108. #ifdef MSDOS
  109. extern  int glob_pattern_p(char *);
  110. extern  int glob_match(char *,char *,int);
  111. static  int glob_match_after_star(char *,char *);
  112. extern  char * *glob_vector(char *,char *);
  113. static  char * *glob_dir_to_array(char *,char **);
  114. extern  char * *glob_filename(char *);
  115. #endif /* MSDOS */
  116.   
  117.  
  118. /* Nonzero if '*' and '?' do not match an initial '.' for glob_filename.  */
  119. int noglob_dot_filenames = 1;
  120.  
  121. static int glob_match_after_star ();
  122.  
  123. /* Return nonzero if PATTERN has any special globbing chars in it.  */
  124. int
  125. glob_pattern_p (pattern)
  126.      char *pattern;
  127. {
  128.   register char *p = pattern;
  129.   register char c;
  130.  
  131.   while ((c = *p++) != '\0')
  132.     switch (c)
  133.       {
  134.       case '?':
  135.       case '[':
  136.       case '*':
  137.     return 1;
  138.  
  139.       case '\\':
  140.     if (*p++ == '\0')
  141.       return 0;
  142.       }
  143.  
  144.   return 0;
  145. }
  146.  
  147. /* Match the pattern PATTERN against the string TEXT;
  148.    return 1 if it matches, 0 otherwise.
  149.  
  150.    A match means the entire string TEXT is used up in matching.
  151.  
  152.    In the pattern string, `*' matches any sequence of characters,
  153.    `?' matches any character, [SET] matches any character in the specified set,
  154.    [!SET] matches any character not in the specified set.
  155.  
  156.    A set is composed of characters or ranges; a range looks like
  157.    character hyphen character (as in 0-9 or A-Z).
  158.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  159.    Any other character in the pattern must be matched exactly.
  160.  
  161.    To suppress the special syntactic significance of any of `[]*?!-\',
  162.    and match the character exactly, precede it with a `\'.
  163.  
  164.    If DOT_SPECIAL is nonzero,
  165.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  166.  
  167. int
  168. glob_match (pattern, text, dot_special)
  169.      char *pattern, *text;
  170.      int dot_special;
  171. {
  172.   register char *p = pattern, *t = text;
  173.   register char c;
  174.  
  175.   while ((c = *p++) != '\0')
  176.     switch (c)
  177.       {
  178.       case '?':
  179.     if (*t == '\0' || (dot_special && t == text && *t == '.'))
  180.       return 0;
  181.     else
  182.       ++t;
  183.     break;
  184.  
  185.       case '\\':
  186.     if (*p++ != *t++)
  187.       return 0;
  188.     break;
  189.  
  190.       case '*':
  191.     if (dot_special && t == text && *t == '.')
  192.       return 0;
  193.     return glob_match_after_star (p, t);
  194.  
  195.       case '[':
  196.     {
  197.       register char c1 = *t++;
  198.       int invert;
  199.  
  200.       if (c1 == '\0')
  201.         return 0;
  202.  
  203.       invert = (*p == '!');
  204.  
  205.       if (invert)
  206.         p++;
  207.  
  208.       c = *p++;
  209.       while (1)
  210.         {
  211.           register char cstart = c, cend = c;
  212.  
  213.           if (c == '\\')
  214.         {
  215.           cstart = *p++;
  216.           cend = cstart;
  217.         }
  218.  
  219.           if (cstart == '\0')
  220.         return 0;    /* Missing ']'. */
  221.  
  222.           c = *p++;
  223.  
  224.           if (c == '-')
  225.         {
  226.           cend = *p++;
  227.           if (cend == '\\')
  228.             cend = *p++;
  229.           if (cend == '\0')
  230.             return 0;
  231.           c = *p++;
  232.         }
  233.           if (c1 >= cstart && c1 <= cend)
  234.         goto match;
  235.           if (c == ']')
  236.         break;
  237.         }
  238.       if (!invert)
  239.         return 0;
  240.       break;
  241.  
  242.     match:
  243.       /* Skip the rest of the [...] construct that already matched.  */
  244.       while (c != ']')
  245.         {
  246.           if (c == '\0')
  247.         return 0;
  248.           c = *p++;
  249.           if (c == '\0')
  250.         return 0;
  251.           if (c == '\\')
  252.         p++;
  253.         }
  254.       if (invert)
  255.         return 0;
  256.       break;
  257.     }
  258.  
  259.       default:
  260.     if (c != *t++)
  261.       return 0;
  262.       }
  263.  
  264.   return *t == '\0';
  265. }
  266.  
  267. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  268.  
  269. static int
  270. glob_match_after_star (pattern, text)
  271.      char *pattern, *text;
  272. {
  273.   register char *p = pattern, *t = text;
  274.   register char c, c1;
  275.  
  276.   while ((c = *p++) == '?' || c == '*')
  277.     if (c == '?' && *t++ == '\0')
  278.       return 0;
  279.  
  280.   if (c == '\0')
  281.     return 1;
  282.  
  283.   if (c == '\\')
  284.     c1 = *p;
  285.   else
  286.     c1 = c;
  287.  
  288.   --p;
  289.   while (1)
  290.     {
  291.       if ((c == '[' || *t == c1) && glob_match (p, t, 0))
  292.     return 1;
  293.       if (*t++ == '\0')
  294.     return 0;
  295.     }
  296. }
  297.  
  298. /* Return a vector of names of files in directory DIR
  299.    whose names match glob pattern PAT.
  300.    The names are not in any particular order.
  301.    Wildcards at the beginning of PAT do not match an initial period
  302.    if noglob_dot_filenames is nonzero.
  303.  
  304.    The vector is terminated by an element that is a null pointer.
  305.  
  306.    To free the space allocated, first free the vector's elements,
  307.    then free the vector.
  308.  
  309.    Return NULL if cannot get enough memory to hold the pointer
  310.    and the names.
  311.  
  312.    Return -1 if cannot access directory DIR.
  313.    Look in errno for more information.  */
  314.  
  315. char **
  316. glob_vector (pat, dir)
  317.      char *pat;
  318.      char *dir;
  319. {
  320.   struct globval
  321.   {
  322.     struct globval *next;
  323.     char *name;
  324.   };
  325.  
  326.   DIR *d;
  327.   register struct direct *dp;
  328.   struct globval *lastlink;
  329.   register struct globval *nextlink;
  330.   register char *nextname;
  331.   unsigned int count;
  332.   int lose;
  333.   register char **name_vector;
  334.   register unsigned int i;
  335.  
  336.   d = opendir (dir);
  337.   if (d == NULL)
  338.     return (char **) -1;
  339.  
  340.   lastlink = NULL;
  341.   count = 0;
  342.   lose = 0;
  343.  
  344.   /* Scan the directory, finding all names that match.
  345.      For each name that matches, allocate a struct globval
  346.      on the stack and store the name in it.
  347.      Chain those structs together; lastlink is the front of the chain.  */
  348.   while (1)
  349.     {
  350.       dp = readdir (d);
  351.       if (dp == NULL)
  352.     break;
  353. #ifdef MSDOS    /* dp->d_ino is always 0 in the MS-DOS implementation. */
  354.       if (glob_match (pat, dp->d_name, noglob_dot_filenames))
  355. #else
  356.       if (dp->d_ino != 0
  357.       && glob_match (pat, dp->d_name, noglob_dot_filenames))
  358. #endif /* MSDOS */
  359.     {
  360.       nextlink = (struct globval *) alloca (sizeof (struct globval));
  361.       nextlink->next = lastlink;
  362.       i = D_NAMLEN (dp) + 1;
  363.       nextname = (char *) malloc (i);
  364.       if (nextname == NULL)
  365.         {
  366.           lose = 1;
  367.           break;
  368.         }
  369.       lastlink = nextlink;
  370.       nextlink->name = nextname;
  371.       bcopy (dp->d_name, nextname, i);
  372.       count++;
  373.     }
  374.     }
  375.   closedir (d);
  376.  
  377.   if (!lose)
  378.     {
  379.       name_vector = (char **) malloc ((count + 1) * sizeof (char *));
  380.       lose |= name_vector == NULL;
  381.     }
  382.  
  383.   /* Have we run out of memory?  */
  384.   if (lose)
  385.     {
  386.       /* Here free the strings we have got.  */
  387.       while (lastlink)
  388.     {
  389.       free (lastlink->name);
  390.       lastlink = lastlink->next;
  391.     }
  392.       return NULL;
  393.     }
  394.  
  395.   /* Copy the name pointers from the linked list into the vector.  */
  396.   for (i = 0; i < count; ++i)
  397.     {
  398.       name_vector[i] = lastlink->name;
  399.       lastlink = lastlink->next;
  400.     }
  401.  
  402.   name_vector[count] = NULL;
  403.   return name_vector;
  404. }
  405.  
  406. /* Return a new array, replacing ARRAY, which is the concatenation
  407.    of each string in ARRAY to DIR.
  408.    Return NULL if out of memory.  */
  409.  
  410. static char **
  411. glob_dir_to_array (dir, array)
  412.      char *dir, **array;
  413. {
  414.   register unsigned int i, l;
  415.   int add_slash = 0;
  416.   char **result;
  417.  
  418.   l = strlen (dir);
  419.   if (l == 0)
  420.     return array;
  421.  
  422.   if (dir[l - 1] != '/')
  423.     add_slash++;
  424.  
  425.   for (i = 0; array[i] != NULL; i++)
  426.     ;
  427.  
  428.   result = (char **) malloc ((i + 1) * sizeof (char *));
  429.   if (result == NULL)
  430.     return NULL;
  431.  
  432.   for (i = 0; array[i] != NULL; i++)
  433.     {
  434.       result[i] = (char *) malloc (1 + l + add_slash + strlen (array[i]));
  435.       if (result[i] == NULL)
  436.     return NULL;
  437.       strcpy (result[i], dir);
  438.       if (add_slash)
  439.     result[i][l] = '/';
  440.       strcpy (result[i] + l + add_slash, array[i]);
  441.     }
  442.   result[i] = NULL;
  443.  
  444.   /* Free the input array.  */
  445.   for (i = 0; array[i] != NULL; i++)
  446.     free (array[i]);
  447.   free ((char *) array);
  448.  
  449.   return result;
  450. }
  451.  
  452. /* Do globbing on PATHNAME.  Return an array of pathnames that match,
  453.    marking the end of the array with a null-pointer as an element.
  454.    If no pathnames match, then the array is empty (first element is null).
  455.    If there isn't enough memory, then return NULL.
  456.    If a file system error occurs, return -1; `errno' has the error code.
  457.  
  458.    Wildcards at the beginning of PAT, or following a slash,
  459.    do not match an initial period if noglob_dot_filenames is nonzero.  */
  460.  
  461. char **
  462. glob_filename (pathname)
  463.      char *pathname;
  464. {
  465.   char **result;
  466.   unsigned int result_size;
  467.   char *directory_name, *filename;
  468.   unsigned int directory_len;
  469.  
  470.   result = (char **) malloc (sizeof (char *));
  471.   result_size = 1;
  472.   if (result == NULL)
  473.     return NULL;
  474.  
  475.   result[0] = NULL;
  476.  
  477.   /* Find the filename.  */
  478.   filename = rindex (pathname, '/');
  479.   if (filename == NULL)
  480.     {
  481.       filename = pathname;
  482.       directory_name = "";
  483.       directory_len = 0;
  484.     }
  485.   else
  486.     {
  487.       directory_len = (filename - pathname) + 1;
  488.       directory_name = (char *) alloca (directory_len + 1);
  489.       bcopy (pathname, directory_name, directory_len);
  490.       directory_name[directory_len] = '\0';
  491.       ++filename;
  492.     }
  493.  
  494.   /* If directory_name contains globbing characters, then we
  495.      have to expand the previous levels.  Just recurse. */
  496.   if (glob_pattern_p (directory_name))
  497.     {
  498.       char **directories;
  499.       register unsigned int i;
  500.  
  501.       if (directory_name[directory_len - 1] == '/')
  502.     directory_name[directory_len - 1] = '\0';
  503.  
  504.       directories = glob_filename (directory_name);
  505.       if (directories == NULL)
  506.     goto memory_error;
  507.       else if (directories == (char **) -1)
  508.     return (char **) -1;
  509.       else if (*directories == NULL)
  510.     {
  511.       free ((char *) directories);
  512.       return (char **) -1;
  513.     }
  514.  
  515.       /* We have successfully globbed the preceding directory name.
  516.      For each name in DIRECTORIES, call glob_vector on it and
  517.      FILENAME.  Concatenate the results together.  */
  518.       for (i = 0; directories[i] != NULL; i++)
  519.     {
  520.       char **temp_results = glob_vector (filename, directories[i]);
  521.       if (temp_results == NULL)
  522.         goto memory_error;
  523.       else if (temp_results == (char **) -1)
  524.         /* This filename is probably not a directory.  Ignore it.  */
  525.         ;
  526.       else
  527.         {
  528.           char **array = glob_dir_to_array (directories[i], temp_results);
  529.           register unsigned int l;
  530.  
  531.           l = 0;
  532.           while (array[l] != NULL)
  533.         ++l;
  534.  
  535.           result = (char **) realloc (result,
  536.                       (result_size + l) * sizeof (char *));
  537.           if (result == NULL)
  538.         goto memory_error;
  539.  
  540.           for (l = 0; array[l] != NULL; ++l)
  541.         result[result_size++ - 1] = array[l];
  542.           result[result_size - 1] = NULL;
  543.           free ((char *) array);
  544.         }
  545.     }
  546.       /* Free the directories.  */
  547.       for (i = 0; directories[i] != NULL; i++)
  548.     free (directories[i]);
  549.       free ((char *) directories);
  550.  
  551.       return result;
  552.     }
  553.  
  554.   /* If there is only a directory name, return it. */
  555.   if (*filename == '\0')
  556.     {
  557.       result = (char **) realloc ((char *) result, 2 * sizeof (char *));
  558.       if (result == NULL)
  559.     return NULL;
  560.       result[0] = (char *) malloc (directory_len + 1);
  561.       if (result[0] == NULL)
  562.     goto memory_error;
  563.       bcopy (directory_name, result[0], directory_len + 1);
  564.       result[1] = NULL;
  565.       return result;
  566.     }
  567.   else
  568.     {
  569.       /* Otherwise, just return what glob_vector
  570.      returns appended to the directory name. */
  571.       char **temp_results = glob_vector (filename,
  572.                      (directory_len == 0
  573.                       ? "." : directory_name));
  574.  
  575.       if (temp_results == NULL || temp_results == (char **) -1)
  576.     return temp_results;
  577.  
  578.       return glob_dir_to_array (directory_name, temp_results);
  579.     }
  580.  
  581. memory_error:;
  582.   if (result != NULL)
  583.     {
  584.       register unsigned int i;
  585.       for (i = 0; result[i] != NULL; ++i)
  586.     free (result[i]);
  587.       free ((char *) result);
  588.     }
  589.   return NULL;
  590. }
  591.  
  592. #ifdef TEST
  593.  
  594. main (argc, argv)
  595.      int argc;
  596.      char **argv;
  597. {
  598.   char **value;
  599.   int i, optind;
  600.  
  601.   for (optind = 1; optind < argc; optind++)
  602.     {
  603.       value = glob_filename (argv[optind]);
  604.       if (value == NULL)
  605.     puts ("virtual memory exhausted");
  606.       else if (value == (char **) -1)
  607.     perror (argv[optind]);
  608.       else
  609.     for (i = 0; value[i] != NULL; i++)
  610.       puts (value[i]);
  611.     }
  612.   exit (0);
  613. }
  614.  
  615. #endif /* TEST */
  616.