home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / fileutil / rh / match.c < prev    next >
Encoding:
Text File  |  1990-02-12  |  7.7 KB  |  214 lines

  1. /* File-name wildcard pattern matching for GNU.
  2.    Copyright (C) 1985, 1988 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33. (C) 1988 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.  
  92. In other words, you are welcome to use, share and improve this program.
  93. You are forbidden to forbid anyone else to use, share and improve
  94. what you give them.   Help stamp out software-hoarding!  */
  95.  
  96. /* Match the pattern PATTERN against the string TEXT;
  97.    return 1 if it matches, 0 otherwise.
  98.  
  99.    A match means the entire string TEXT is used up in matching.
  100.  
  101.    In the pattern string, `*' matches any sequence of characters,
  102.    `?' matches any character, [SET] matches any character in the specified set,
  103.    [^SET] matches any character not in the specified set.
  104.  
  105.    A set is composed of characters or ranges; a range looks like
  106.    character hyphen character (as in 0-9 or A-Z).
  107.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  108.    Any other character in the pattern must be matched exactly.
  109.  
  110.    To suppress the special syntactic significance of any of `[]*?^-\',
  111.    and match the character exactly, precede it with a `\'.
  112.  
  113.    If DOT_SPECIAL is nonzero,
  114.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  115.  
  116. static int star_glob_match(char *pattern, char *text);
  117.  
  118. int glob_match(char *pattern, char *text, int dot_special)
  119. {
  120.   register char *p = pattern, *t = text;
  121.   register char c;
  122.  
  123.   while ((c = *p++))
  124.     {
  125.       switch (c)
  126.     {
  127.     case '?':
  128.       if (*t == 0 || (dot_special && t == text && *t == '.')) return 0;
  129.       else ++t;
  130.       break;
  131.  
  132.     case '\\':
  133.       if (*p++ != *t++) return 0;
  134.       break;
  135.  
  136.     case '*':
  137.       if (dot_special && t == text && *t == '.')
  138.         return 0;
  139.       return star_glob_match (p, t);
  140.  
  141.     case '[':
  142.       {
  143.         register char c1 = *t++;
  144.         register int invert = (*p == '^');
  145.  
  146.         if (invert) p++;
  147.  
  148.         c = *p++;
  149.         while (1)
  150.           {
  151.         register char cstart = c, cend = c;
  152.  
  153.         if (c == '\\')
  154.           {
  155.             cstart = *p++; cend = cstart;
  156.           }
  157.         c = *p++;
  158.         if (c == '-')
  159.           { cend = *p++; if (cend == '\\') cend = *p++; c = *p++; }
  160.         if (c1 >= cstart && c1 <= cend) goto match;
  161.         if (c == ']')
  162.           break;
  163.           }
  164.         if (!invert) return 0;
  165.         break;
  166.  
  167.       match:
  168.         /* Skip the rest of the [...] construct that already matched.  */
  169.         while (c != ']')
  170.           {
  171.             c = *p++;
  172.         if (c == '\\') p++;
  173.           }
  174.         if (invert) return 0;
  175.         break;
  176.       }
  177.  
  178.     default:
  179.       if (c != *t++) return 0;
  180.     }
  181.     }
  182.  
  183.   if (*t) return 0;
  184.   return 1;
  185. }
  186.  
  187. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  188.  
  189. static int star_glob_match(char *pattern, char *text)
  190. {
  191.   register char *p = pattern, *t = text;
  192.   register char c, c1;
  193.  
  194.   while ((c = *p++) == '?' || c == '*')
  195.     {
  196.       if (c == '?' && *t++ == 0)
  197.     return 0;
  198.     }
  199.  
  200.   if (c == 0)
  201.     return 1;
  202.  
  203.   if (c == '\\') c1 = *p;
  204.   else c1 = c;
  205.  
  206.   for (;;)
  207.     {
  208.       if ((c == '[' || *t == c1)
  209.           && glob_match (p - 1, t, 0))
  210.     return 1;
  211.       if (*t++ == 0) return 0;
  212.     }
  213. }
  214.