standalone: fix w/h confusion
[pcsx_rearmed.git] / deps / libretro-common / compat / compat_fnmatch.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (compat_fnmatch.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <stddef.h>
24
25 #include <compat/fnmatch.h>
26
27 /* Implemnentation of fnmatch(3) so it can be
28  * distributed to non *nix platforms.
29  *
30  * No flags are implemented ATM.
31  * We don't use them. Add flags as needed. */
32
33 int rl_fnmatch(const char *pattern, const char *string, int flags)
34 {
35    int rv;
36    const char *c = NULL;
37    int charmatch = 0;
38
39    for (c = pattern; *c != '\0'; c++)
40    {
41       /* String ended before pattern */
42       if ((*c != '*') && (*string == '\0'))
43          return FNM_NOMATCH;
44
45       switch (*c)
46       {
47          /* Match any number of unknown chars */
48          case '*':
49             /* Find next node in the pattern
50              * ignoring multiple asterixes
51              */
52             do {
53                c++;
54                if (*c == '\0')
55                   return 0;
56             } while (*c == '*');
57
58             /* Match the remaining pattern
59              * ignoring more and more characters. */
60             do {
61                /* We reached the end of the string without a
62                 * match. There is a way to optimize this by
63                 * calculating the minimum chars needed to
64                 * match the remaining pattern but I don't
65                 * think it is worth the work ATM.
66                 */
67                if (*string == '\0')
68                   return FNM_NOMATCH;
69
70                rv = rl_fnmatch(c, string, flags);
71                string++;
72             } while (rv != 0);
73
74             return 0;
75             /* Match char from list */
76          case '[':
77             charmatch = 0;
78             for (c++; *c != ']'; c++)
79             {
80                /* Bad format */
81                if (*c == '\0')
82                   return FNM_NOMATCH;
83
84                /* Match already found */
85                if (charmatch)
86                   continue;
87
88                if (*c == *string)
89                   charmatch = 1;
90             }
91
92             /* No match in list */
93             if (!charmatch)
94                return FNM_NOMATCH;
95
96             string++;
97             break;
98             /* Has any character */
99          case '?':
100             string++;
101             break;
102             /* Match following character verbatim */
103          case '\\':
104             c++;
105             /* Dangling escape at end of pattern.
106              * FIXME: Was c == '\0' (makes no sense).
107              * Not sure if c == NULL or *c == '\0'
108              * is intended. Assuming *c due to c++ right before. */
109             if (*c == '\0')
110                return FNM_NOMATCH;
111          default:
112             if (*c != *string)
113                return FNM_NOMATCH;
114             string++;
115       }
116    }
117
118    /* End of string and end of pattend */
119    if (*string == '\0')
120       return 0;
121    return FNM_NOMATCH;
122 }