home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / pax / 1 / wildmat.c < prev   
Encoding:
C/C++ Source or Header  |  1989-01-07  |  4.8 KB  |  195 lines

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