home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / snippets / xstrcmp.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  2KB  |  71 lines

  1. /*
  2. **  xstrcmp() - compares strings using DOS wildcards
  3. **              'mask' may contain '*' and '?'
  4. **               returns 1 if 's' matches 'mask', otherwise 0
  5. **               public domain by Steffen Offermann 1991
  6. */
  7.  
  8.  
  9. int xstrcmp (char *mask, char *s)
  10. {
  11.       while (*mask)
  12.       {
  13.             switch (*mask)
  14.             {
  15.             case '?':
  16.                   if (!*s)
  17.                         return 0;
  18.                   s++;
  19.                   mask++;
  20.                   break;
  21.  
  22.             case '*':
  23.                   while (*mask == '*')
  24.                         mask++;
  25.                   if (!*mask)
  26.                         return 1;
  27.                   if (*mask == '?')
  28.                         break;
  29.                   while (*s != *mask)
  30.                   {
  31.                         if (!*s)
  32.                               return 0;
  33.                         s++;
  34.                   }
  35.                   s++;
  36.                   mask++;
  37.                   break;
  38.  
  39.             default:
  40.                   if (*s != *mask)
  41.                         return 0;
  42.                   s++;
  43.                   mask++;
  44.             }
  45.       }
  46.  
  47.       if (!*s && *mask)
  48.             return 0;
  49.       if (*s)
  50.             return 0;
  51.       return 1;
  52. }
  53.  
  54. #ifdef TEST
  55.  
  56. #include <stdio.h>
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.       if (3 != argc)
  61.       {
  62.             puts("Usage: XSTRCMP mask string");
  63.             return -1;
  64.       }
  65.       printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
  66.             xstrcmp(argv[1], argv[2]));
  67.       return 0;
  68. }
  69.  
  70. #endif /* TEST */
  71.