home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PAX20.ZIP / WILDMAT.C < prev   
C/C++ Source or Header  |  1990-11-12  |  5KB  |  208 lines

  1. /* $Source: /u/mark/src/pax/RCS/wildmat.c,v $
  2.  *
  3.  * $Revision: 2.0.0.4 $
  4.  *
  5.  * wildmat.c - simple regular expression pattern matching routines
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *     These routines provide simple UNIX style regular expression matching.
  10.  *    They were originally written by Rich Salz, the comp.sources.unix
  11.  *    moderator for inclusion in some of his software.  These routines
  12.  *    were released into the public domain and used by John Gilmore in
  13.  *    USTAR.
  14.  *
  15.  * AUTHORS
  16.  *
  17.  *     Mark H. Colburn, Open Systems Architects, Inc. (mark@minnetech.mn.org)
  18.  *     John Gilmore (gnu@hoptoad)
  19.  *     Rich Salz (rs@uunet.uu.net)
  20.  *
  21.  * COPYRIGHT
  22.  *
  23.  *    Copyright (c) 1989 Mark H. Colburn.  All rights reserved.
  24.  *
  25.  *    Redistribution and use in source and binary forms are permitted
  26.  *    provided that the above copyright notice and this paragraph are
  27.  *    duplicated in all such forms and that any documentation,
  28.  *    advertising materials, and other materials related to such
  29.  *    distribution and use acknowledge that the software was developed
  30.  *    by Mark H. Colburn.
  31.  *
  32.  *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  33.  *    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  34.  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  35.  *
  36.  * $Log:    wildmat.c,v $
  37.  * Revision 2.0.0.4  89/10/13  02:36:04  mark
  38.  * Beta Test Freeze
  39.  *
  40.  */
  41.  
  42. #ifndef lint
  43. static char        *ident = "$Id: wildmat.c,v 2.0.0.4 89/10/13 02:36:04 mark Exp Locker: mark $";
  44. static char        *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  45. #endif /* ! lint */
  46.  
  47.  
  48. /* Headers */
  49.  
  50. #include "pax.h"
  51.  
  52.  
  53. /* Function Prototypes */
  54.  
  55. #ifdef __STDC__
  56.  
  57. static int        star(char *, char *);
  58.  
  59. #else /* !__STDC__ */
  60.  
  61. static int         star();
  62.  
  63. #endif /* __STDC__ */
  64.  
  65.  
  66. /*
  67.  * star - handle trailing * in a regular expression
  68.  *
  69.  * DESCRIPTION
  70.  *
  71.  *    Star is used to match filename expansions containing a trailing
  72.  *    asterisk ('*').  Star call wildmat() to determine if the substring
  73.  *    passed to it is matches the regular expression.
  74.  *
  75.  * PARAMETERS
  76.  *
  77.  *     char *source     - The source string which is to be compared to the
  78.  *              regular expression pattern.
  79.  *     char *pattern     - The regular expression which we are supposed to
  80.  *              match to.
  81.  *
  82.  * RETURNS
  83.  *
  84.  *     Returns non-zero if the entire source string is completely matched by
  85.  *    the regular expression pattern, returns 0 otherwise. This is used to
  86.  *    see if *'s in a pattern matched the entire source string.
  87.  *
  88.  */
  89.  
  90. #ifdef __STDC__
  91.  
  92. static int
  93. star(char *source, char *pattern)
  94.  
  95. #else
  96.  
  97. static int
  98. star(source, pattern)
  99.     char               *source;        /* source operand */
  100.     char               *pattern;    /* regular expression to match */
  101.  
  102. #endif
  103. {
  104.     DBUG_ENTER("star");
  105.     while (!wildmat(pattern, source)) {
  106.     if (*++source == '\0') {
  107.         DBUG_RETURN(0);
  108.     }
  109.     }
  110.     DBUG_RETURN(1);
  111. }
  112.  
  113.  
  114. /*
  115.  * wildmat - match a regular expression
  116.  *
  117.  * DESCRIPTION
  118.  *
  119.  *    Wildmat attempts to match the string pointed to by source to the
  120.  *    regular expression pointed to by pattern.  The subset of regular
  121.  *    expression syntax which is supported is defined by POSIX P1003.2
  122.  *    FILENAME EXPANSION rules.
  123.  *
  124.  * PARAMETERS
  125.  *
  126.  *     char *pattern     - The regular expression which we are supposed to
  127.  *              match to.
  128.  *     char *source     - The source string which is to be compared to the
  129.  *              regular expression pattern.
  130.  *
  131.  * RETURNS
  132.  *
  133.  *     Returns non-zero if the source string matches the regular expression
  134.  *    pattern specified, returns 0 otherwise.
  135.  *
  136.  */
  137.  
  138. #ifdef __STDC__
  139.  
  140. int
  141. wildmat(char *pattern, char *source)
  142.  
  143. #else
  144.  
  145. int
  146. wildmat(pattern, source)
  147.     char               *pattern;    /* regular expression to match */
  148.     char               *source;        /* source operand */
  149.  
  150. #endif
  151. {
  152.     int                 last;        /* last character matched */
  153.     int                 matched;    /* !0 if a match occurred */
  154.     int                 reverse;    /* !0 if sense of match is reversed */
  155.  
  156.     DBUG_ENTER("wildmat");
  157.     for (; *pattern; source++, pattern++) {
  158.     switch (*pattern) {
  159.  
  160.     case '\\':
  161.         /* Literal match with following character */
  162.         pattern++;
  163.         /* FALLTHRU */
  164.  
  165.     default:
  166.         if (*source != *pattern) {
  167.         DBUG_RETURN(0);
  168.         }
  169.         continue;
  170.  
  171.     case '?':
  172.         /* Match anything. */
  173.         if (*source == '\0') {
  174.         DBUG_RETURN(0);
  175.         }
  176.         continue;
  177.  
  178.     case '*':
  179.         /* Trailing star matches everything. */
  180.         DBUG_RETURN(*++pattern ? star(source, pattern) : 1);
  181.  
  182.     case '[':
  183.         /* [^....] means inverse character class. */
  184.         if (reverse = pattern[1] == '^') {
  185.         pattern++;
  186.         }
  187.         for (last = 0400, matched = 0;
  188.          *++pattern && *pattern != ']'; last = *pattern) {
  189.         /* This next line requires a good C compiler. */
  190.         if (*pattern == '-'
  191.             ? *source <= *++pattern && *source >= last
  192.             : *source == *pattern) {
  193.             matched = 1;
  194.         }
  195.         }
  196.         if (matched == reverse) {
  197.         DBUG_RETURN(0);
  198.         }
  199.         continue;
  200.     }
  201.     }
  202.  
  203.     /*
  204.      * For "tar" use, matches that end at a slash also work. --hoptoad!gnu
  205.      */
  206.     DBUG_RETURN(*source == '\0' || *source == '/');
  207. }
  208.