Add support for L4Re Virtio Sockets
[src/app-framework-binder.git] / src / globmatch.c
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <ctype.h>
21
22 #include "globmatch.h"
23
24 /**
25  * Matches whether the string 'str' matches the pattern 'pat'
26  * and returns its matching score.
27  *
28  * @param pat the glob pattern
29  * @param str the string to match
30  * @return 0 if no match or number representing the matching score
31  */
32 static unsigned match(const char *pat, const char *str, int flags)
33 {
34         unsigned r, rs, rr;
35         char c, x;
36         int eq;
37
38         /* scan the prefix without glob */
39         r = 1;
40         while ((c = *pat++) != GLOB) {
41                 x = *str++;
42                 eq = (flags & FNM_CASEFOLD) ? (tolower(c) == tolower(x)) : (c == x);
43                 if (!eq)
44                         return 0; /* no match */
45                 if (!c)
46                         return r; /* match up to end */
47                 r++;
48         }
49
50         /* glob found */
51         c = *pat++;
52         if (!c) {
53                 /* not followed by pattern */
54                 if (flags & FNM_PATHNAME) {
55                         while(*str)
56                                 if (*str++ == '/')
57                                         return 0;
58                 }
59                 return r;
60         }
61
62         /* evaluate the best score for following pattern */
63         rs = 0;
64         while (*str) {
65                 x = *str++;
66                 eq = (flags & FNM_CASEFOLD) ? (tolower(c) == tolower(x)) : (c == x);
67                 if (eq) {
68                         /* first char matches, check remaining string */
69                         rr = match(pat, str, flags);
70                         if (rr > rs)
71                                 rs = rr;
72                 } else if ((flags & FNM_PATHNAME) && x == '/')
73                         return 0;
74         }
75
76         /* best score or not match if rs == 0 */
77         return rs ? rs + r : 0;
78 }
79
80 /**
81  * Matches whether the string 'str' matches the pattern 'pat'
82  * and returns its matching score.
83  *
84  * @param pat the glob pattern
85  * @param str the string to match
86  * @return 0 if no match or number representing the matching score
87  */
88 unsigned globmatch(const char *pat, const char *str)
89 {
90         return match(pat, str, 0);
91 }
92
93 /**
94  * Matches whether the string 'str' matches the pattern 'pat'
95  * and returns its matching score.
96  *
97  * @param pat the glob pattern
98  * @param str the string to match
99  * @return 0 if no match or number representing the matching score
100  */
101 unsigned globmatchi(const char *pat, const char *str)
102 {
103         return match(pat, str, FNM_CASEFOLD);
104 }
105
106 #if !WITH_FNMATCH
107 int fnmatch(const char *pattern, const char *string, int flags)
108 {
109         return !match(pattern, string, flags);
110 }
111 #endif