1: /*
   2:  * Do shell-style pattern matching for '?', '\', '[..]', and '*' wildcards.
   3:  * Returns 1 if match, 0 if not.
   4:  */
   5: 
   6: #include <stdio.h>
   7: 
   8: int
   9: match(s, p)
  10: char *s, *p;
  11: {
  12:     int matched, reverse;
  13:     char first, last;
  14: 
  15:     for (; *p != '\0'; s++, p++) {
  16:         switch (*p) {
  17:             case '?':   /* match any one character */
  18:                 if (*s == '\0')
  19:                     return(0);
  20:                 break;
  21:             case '*':   /* match everything */
  22:                 while (*p == '*')
  23:                     p++;
  24: 
  25:                     /* if last char in pattern */
  26:                 if (*p == '\0')
  27:                     return(1);
  28: 
  29:                     /* search for next char in pattern */
  30:                 matched = 0;
  31:                 while (*s != '\0') {
  32:                     if (*s == *p) {
  33:                         matched = 1;
  34:                         break;
  35:                     }
  36:                     s++;
  37:                 }
  38:                 if (!matched)
  39:                     return(0);
  40:                 break;
  41:             case '[':    /* match range of characters */
  42:                 first = '\0';
  43:                 matched = 0;
  44:                 reverse = 0;
  45:                 while (*++p != ']') {
  46:                     if (*p == '^') {
  47:                         reverse = 1;
  48:                         p++;
  49:                     }
  50:                     first = *p;
  51:                     if (first == ']' || first == '\0')
  52:                         return(0);
  53: 
  54:                     /* if 2nd char is '-' */
  55:                     if (*(p + 1) == '-') {
  56:                         p++;
  57:                     /* set last to 3rd char ... */
  58:                         last = *++p;
  59:                         if (last == ']' || last == '\0')
  60:                             return(0);
  61:                     /* test the range of values */
  62:                         if (*s >= first && *s <= last) {
  63:                             matched = 1;
  64:                             p++;
  65:                             break;
  66:                         }
  67:                         return(0);
  68:                     }
  69:                     if (*s == *p)
  70:                         matched = 1;
  71:                 }
  72:                 if (matched && reverse)
  73:                     return(0);
  74:                 if (!matched)
  75:                     return(0);
  76:                 break;
  77:             case '\\':  /* Literal match with next character */
  78:                 p++;
  79:                 /* fall thru */
  80:             default:
  81:                 if (*s != *p)
  82:                     return(0);
  83:                 break;
  84:         }
  85:     }
  86:                     /* string ended prematurely ? */
  87:     if (*s != '\0')
  88:         return(0);
  89:     else
  90:         return(1);
  91: }
Last modified: 1992-06-24
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2044
Valid CSS Valid XHTML 1.0 Strict