"Fossies" - the Fresh Open Source Software Archive

Member "coda-6.9.5/coda-src/asr/wildmat.c" (26 Jun 2007, 3685 Bytes) of package /linux/misc/old/coda-6.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wildmat.c" see the Fossies "Dox" file reference documentation.

    1 /* BLURB gpl
    2 
    3                            Coda File System
    4                               Release 6
    5 
    6           Copyright (c) 1987-2003 Carnegie Mellon University
    7                   Additional copyrights listed below
    8 
    9 This  code  is  distributed "AS IS" without warranty of any kind under
   10 the terms of the GNU General Public Licence Version 2, as shown in the
   11 file  LICENSE.  The  technical and financial  contributors to Coda are
   12 listed in the file CREDITS.
   13 
   14                         Additional copyrights
   15                            none currently
   16 
   17 #*/
   18 
   19 
   20 
   21 
   22 
   23 /*
   24 **  Do shell-style pattern matching for ?, \, [], and * characters.
   25 **  Might not be robust in face of malformed patterns; e.g., "foo[a-"
   26 **  could cause a segmentation violation.  It is 8bit clean.
   27 **
   28 **  Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
   29 **  Rich $alz is now <rsalz@bbn.com>.
   30 **  Special thanks to Lars Mathiesen <thorinn@diku.dk> for the ABORT code.
   31 **  This can greatly speed up failing wildcard patterns.  For example:
   32 **  pattern: -*-*-*-*-*-*-12-*-*-*-m-*-*-*
   33 **  text 1:  -adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1
   34 **  text 2:  -adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1
   35 **  Text 1 matches with 51 calls, while text 2 fails with 54 calls.  Without
   36 **  the ABORT, then it takes 22310 calls to fail.  Ugh.
   37 */
   38 
   39 #define TRUE        1
   40 #define FALSE       0
   41 #define ABORT       -1
   42 
   43 #define NEGATE_CLASS    '^'
   44 
   45 /* Forward declaration. */
   46 static int DoMatch(register char *text, register char *p);
   47 
   48 /*
   49 **  See if the text matches the p, which has an implied leading asterisk.
   50 */
   51 static int
   52 Star(register char *text, char * p)
   53 {
   54     register int    ret;
   55 
   56     do {
   57     ret = DoMatch(text++, p);
   58     } while (ret == FALSE);
   59     return ret;
   60 }
   61 
   62 
   63 /*
   64 **  Match text and p, return TRUE, FALSE, or ABORT.
   65 */
   66 static int
   67 DoMatch(register char *text, register char *p)
   68 {
   69     register int     last;
   70     register int     matched;
   71     register int     reverse;
   72 
   73     for ( ; *p; text++, p++) {
   74     if (*text == '\0' && *p != '*')
   75         return ABORT;
   76     switch (*p) {
   77     case '\\':
   78         /* Literal match with following character. */
   79         p++;
   80         /* FALLTHROUGH */
   81     default:
   82         if (*text != *p)
   83         return FALSE;
   84         continue;
   85     case '?':
   86         /* Match anything. */
   87         continue;
   88     case '*':
   89         /* Trailing star matches everything. */
   90         return *++p ? Star(text, p) : TRUE;
   91     case '[':
   92         reverse = (p[1] == NEGATE_CLASS);
   93         if (reverse)
   94         /* Inverted character class. */
   95         p++;
   96         for (last = 0400, matched = FALSE; *p && *p != ']'; last = *p, p++) {
   97         /* This next line requires a good C compiler. */
   98           
   99         if (*p == '-' ? (p++, *text <= *p && *text >= last) : *text == *p)
  100             matched = TRUE;
  101         }
  102         if (matched == reverse)
  103         return FALSE;
  104         continue;
  105     }
  106     }
  107 
  108     return *text == '\0';
  109 }
  110 
  111 
  112 /*
  113 **  User-level routine.  Returns TRUE or FALSE.
  114 */
  115 int
  116 wildmat(char *text, char * p)
  117 {
  118     return DoMatch(text, p) == TRUE;
  119 }
  120 
  121 
  122 
  123 #ifdef  TEST
  124 #include <stdio.h>
  125 /* Yes, we use gets not fgets.  Sue me. */
  126 extern char *gets();
  127 
  128 
  129 main()
  130 {
  131     char     p[80];
  132     char     text[80];
  133 
  134     printf("Wildmat tester.  Enter pattern, then strings to test.\n");
  135     printf("A blank line gets prompts for a new pattern; a blank pattern\n");
  136     printf("exits the program.\n\n");
  137 
  138     for ( ; ; ) {
  139     printf("Enter pattern:  ");
  140     (void)fflush(stdout);
  141     if (gets(p) == NULL || p[0] == '\n')
  142         break;
  143     for ( ; ; ) {
  144         printf("Enter text:  ");
  145         (void)fflush(stdout);
  146         if (gets(text) == NULL)
  147         exit(0);
  148         if (text[0] == '\0')
  149         /* Blank line; go back and get a new pattern. */
  150         break;
  151         printf("      %s\n", wildmat(text, p) ? "YES" : "NO");
  152     }
  153     }
  154 
  155     exit(0);
  156     /* NOTREACHED */
  157 }
  158 #endif  /* TEST */