home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / flistfrontend / src / strwcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-06  |  2.7 KB  |  113 lines

  1. #ifndef NO_IDENT
  2. static char *Id = "$Id: strwcmp.c,v 1.4 1995/06/06 13:41:18 tom Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Title:    strwcmp.c
  7.  * Author:    Thomas E. Dickey
  8.  * Created:    24 May 1984
  9.  * Last update:
  10.  *        18 Feb 1995    prototyped
  11.  *        14 Sep 1985    Don't pass ALL/ONE as arguments.  Also, test for
  12.  *                pathname-wildcard "..." which matches 0 or more
  13.  *                tokens delimited by '.'
  14.  *        24 May 1984
  15.  *
  16.  * Function:    This procedure performs wild-card comparison on a string,
  17.  *        a la VMS: the first argument may contain wild-card characters
  18.  *        <ALL> and <ONE>, which respectively match any sequence and
  19.  *        any single character in the second argument.  Characters
  20.  *        other than <ALL> or <ONE> in the first argument must match
  21.  *        exactly in the second.
  22.  *
  23.  * Parameters:    wild_    => string containing wildcard characters
  24.  *        tame_    => string to be tested for match.
  25.  *
  26.  * Returns:    1 (TRUE) if no match, else 0 (FALSE, or EQUAL).
  27.  */
  28.  
  29. #include <string.h>
  30.  
  31. #include "strutils.h"
  32.  
  33. #define    EQUAL        0
  34. #define    ANY        '*'
  35. #define    ONE        '%'
  36. #define    DOT        '.'
  37. #define    ELLIPSIS    "..."
  38.  
  39. int
  40. strwcmp (char *wild_, char *tame_)
  41. {
  42.     while (*wild_ && *tame_)
  43.     {
  44.         if (*wild_ == ANY)
  45.         {
  46.         while (*wild_ == ANY)    wild_++;
  47.         if (*wild_)
  48.         {
  49.             while (*tame_)
  50.             {
  51.             if (strwcmp (wild_, tame_))
  52.                 tame_++;
  53.             else
  54.                 return (EQUAL);
  55.             }
  56.             return (! EQUAL);
  57.         }
  58.         else    /* Match remainder of string    */
  59.             return (EQUAL);
  60.         }
  61.         else if (*wild_ == ONE)
  62.         wild_++, tame_++;
  63.         else if (*wild_ == DOT)
  64.         {
  65.         /*
  66.          * If we have an ellipsis, we may match any number of directory
  67.          * names, separated by '.', e.g., [...] matches
  68.          *    [name]
  69.          *    [.name]
  70.          *    [name.name]
  71.          */
  72.         if (! strncmp (wild_, ELLIPSIS, sizeof(ELLIPSIS)-1))
  73.         {
  74.             wild_ += sizeof(ELLIPSIS)-1;
  75.             if (*tame_ == DOT)
  76.                 tame_++;
  77.             /*
  78.              * The inputs to this routine *must* be syntactically
  79.              * correct.  VMS permits an ellipsis only within brackets.
  80.              * Therefore, we can safely (?) test the last character
  81.              * before the current scanning position:
  82.              */
  83.             else if (tame_[-1] != '.' && tame_[-1] != '[')
  84.                 return (! EQUAL);
  85.  
  86.             /*
  87.              * Fall-thru to scan matches against an ellipsis:
  88.              */
  89.             while (*tame_ && (*tame_ != ']'))
  90.             {
  91.             if (strwcmp (wild_, tame_))
  92.             {
  93.                 while ((*tame_ != DOT) && (*tame_ != ']'))
  94.                 tame_++;
  95.                 if (*tame_ == DOT)    tame_++;
  96.             }
  97.             else
  98.                 return (EQUAL);
  99.             }
  100.             return (strwcmp (wild_, tame_));
  101.         }
  102.         else if (*wild_++ != *tame_++)
  103.             return (! EQUAL);
  104.         }
  105.         else if (*wild_++ != *tame_++)
  106.         return (! EQUAL);
  107.     }
  108.  
  109.     if (*wild_ == ANY)
  110.         return (wild_[1] != '\0');
  111.     return (*wild_ || *tame_); /* Equal if both end at the same point */
  112. }
  113.