1 /* Copyright (c) 2000, 2003, 2004 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    Without limiting anything contained in the foregoing, this file,
16    which is part of C Driver for MySQL (Connector/C), is also subject to the
17    Universal FOSS Exception, version 1.0, a copy of which can be found at
18    http://oss.oracle.com/licenses/universal-foss-exception.
19 
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License, version 2.0, for more details.
24 
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
28 
29 /* Funktions for comparing with wild-cards */
30 
31 #include "mysys_priv.h"
32 
33 	/* Test if a string is "comparable" to a wild-card string */
34 	/* returns 0 if the strings are "comparable" */
35 
36 char wild_many='*';
37 char wild_one='?';
38 char wild_prefix=0; /* QQ this can potentially cause a SIGSEGV */
39 
wild_compare(register const char * str,register const char * wildstr,pbool str_is_pattern)40 int wild_compare(register const char *str, register const char *wildstr,
41                  pbool str_is_pattern)
42 {
43   char cmp;
44   DBUG_ENTER("wild_compare");
45 
46   while (*wildstr)
47   {
48     while (*wildstr && *wildstr != wild_many && *wildstr != wild_one)
49     {
50       if (*wildstr == wild_prefix && wildstr[1])
51       {
52 	wildstr++;
53         if (str_is_pattern && *str++ != wild_prefix)
54           DBUG_RETURN(1);
55       }
56       if (*wildstr++ != *str++)
57         DBUG_RETURN(1);
58     }
59     if (! *wildstr )
60       DBUG_RETURN(*str != 0);
61     if (*wildstr++ == wild_one)
62     {
63       if (! *str || (str_is_pattern && *str == wild_many))
64         DBUG_RETURN(1);                     /* One char; skip */
65       if (*str++ == wild_prefix && str_is_pattern && *str)
66         str++;
67     }
68     else
69     {						/* Found '*' */
70       while (str_is_pattern && *str == wild_many)
71         str++;
72       for (; *wildstr ==  wild_many || *wildstr == wild_one; wildstr++)
73         if (*wildstr == wild_many)
74         {
75           while (str_is_pattern && *str == wild_many)
76             str++;
77         }
78         else
79         {
80           if (str_is_pattern && *str == wild_prefix && str[1])
81             str+=2;
82           else if (! *str++)
83             DBUG_RETURN (1);
84         }
85       if (!*wildstr)
86         DBUG_RETURN(0);		/* '*' as last char: OK */
87       if ((cmp= *wildstr) == wild_prefix && wildstr[1] && !str_is_pattern)
88         cmp=wildstr[1];
89       for (;;str++)
90       {
91         while (*str && *str != cmp)
92           str++;
93         if (!*str)
94           DBUG_RETURN (1);
95 	if (wild_compare(str,wildstr,str_is_pattern) == 0)
96           DBUG_RETURN (0);
97       }
98       /* We will never come here */
99     }
100   }
101   DBUG_RETURN (*str != 0);
102 } /* wild_compare */
103