summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/xstrcmp.c
blob: 3d9502cae72a8200bdc4a6550378d4f41179bbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
**  xstrcmp() - compares strings using DOS wildcards
**              'mask' may contain '*' and '?'
**               returns 1 if 's' matches 'mask', otherwise 0
**               public domain by Steffen Offermann 1991
*/


int xstrcmp (char *mask, char *s)
{
      while (*mask)
      {
            switch (*mask)
            {
            case '?':
                  if (!*s)
                        return (0);
                  s++;
                  mask++;
                  break;

            case '*':
                  while (*mask == '*')
                        mask++;
                  if (!*mask)
                        return ( 1 );
                  if (*mask == '?')
                        break;
                  while (*s != *mask)
                  {
                        if (!*s)
                              return (0);
                        s++;
                  }
                  s++;
                  mask++;
                  break;

            default:
                  if (*s != *mask)
                        return (0);
                  s++;
                  mask++;
            }
      }

      if (!*s && *mask)
            return (0);
      return ( 1 );
}

#ifdef TEST

#include <stdio.h>

void main(int argc, char *argv[])
{
      if (3 != argc)
      {
            puts("Usage: XSTRCMP string_1 string_2");
            return;
      }
      printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
            xstrcmp(argv[1], argv[2]));
}

#endif /* TEST */