]> mj.ucw.cz Git - libucw.git/blob - lib/patmatch.c
Adding all files developed during last week...
[libucw.git] / lib / patmatch.c
1 /*
2  *      Sherlock Library -- Shell-Like Pattern Matching (currently only '?' and '*')
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "lib.h"
11
12 int
13 match_pattern(byte *p, byte *s)
14 {
15   while (*p)
16     {
17       if (*p == '?' && *s)
18         p++, s++;
19       else if (*p == '*')
20         {
21           int z = p[1];
22
23           if (!z)
24             return 1;
25           while (s = strchr(s, z))
26             {
27               if (match_pattern(p+1, s))
28                 return 1;
29               s++;
30             }
31           return 0;
32         }
33       else if (*p++ != *s++)
34         return 0;
35     }
36   return !*s;
37 }