home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / stuff2.arj / MATCHSTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  1.8 KB  |  66 lines

  1. /*
  2. Stuff 2.0.
  3.  
  4. Checksum: 2711816906 (verify with "brik")
  5.  
  6. I claim no copyright over the contents of this file  -- Rahul Dhesi
  7. */
  8.  
  9. #include <stddef.h>     /* NULL */
  10. #include <string.h>
  11. #include "stuff.h"
  12.  
  13. #if 1
  14. int tolower (char c)
  15. {
  16.    if (c >= 'A' && c <= 'Z')
  17.       return (c - ('A' - 'a'));
  18.    else
  19.       return (c);
  20. }
  21.  
  22. char *strlwr (char *str)
  23. {
  24.    char *p;
  25.    for (p = str;  *p;  p++)
  26.       *p = tolower(*p);
  27.    return (str);
  28. }
  29. #else
  30. #include <ctype.h>
  31. #endif
  32.  
  33. /*
  34. matchstr() compares a pattern with a string using * and ? wildcards.
  35.  
  36. Originally written by Jeff Damens of Columbia University Center for
  37. Computing Activities.  Taken from the source code for C-Kermit version
  38. 4C.
  39. */
  40.  
  41. int matchstr (register char *string, register char *pattern)
  42. {
  43.    char *psave,*ssave;        /* back up pointers for failure */
  44.    psave = ssave = NULL;
  45.    while (1) {
  46.       for (;
  47.          tolower(*pattern) == tolower(*string);
  48.          pattern++,string++                        )  /* skip first */
  49.  
  50.       if (*string == '\0')
  51.          return(1);                          /* end of strings, succeed */
  52.       if (*string != '\0' && *pattern == '?') {
  53.          pattern++;                             /* '?', let it match */
  54.          string++;
  55.       } else if (*pattern == '*') {             /* '*' ... */
  56.          psave = ++pattern;                     /* remember where we saw it */
  57.          ssave = string;                        /* let it match 0 chars */
  58.       } else if (ssave != NULL && *ssave != '\0') {   /* if not at end  */
  59.          /* ...have seen a star */
  60.          string = ++ssave;                      /* skip 1 char from string */
  61.          pattern = psave;                       /* and back up pattern */
  62.       } else
  63.          return(0);                             /* otherwise just fail */
  64.    }
  65. }
  66.