aboutsummaryrefslogtreecommitdiffstats
path: root/main/musl/1002-implement-FNM_CASEFOLD-gnu-extension.patch
blob: 21a4308b5a1c095f31266948b016a14fcb4c8d95 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
From bfc8d9df41d16bb8beb3625286576c44f10cb898 Mon Sep 17 00:00:00 2001
From: Nagy Szabolcs <nsz@port70.net>
Date: Fri, 10 Oct 2014 12:09:03 +0200
Subject: [PATCH] implement FNM_CASEFOLD gnu extension

---
 src/regex/fnmatch.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/regex/fnmatch.c b/src/regex/fnmatch.c
index 4df10a3..7f6b65f 100644
--- a/src/regex/fnmatch.c
+++ b/src/regex/fnmatch.c
@@ -97,7 +97,13 @@ escaped:
 	return pat[0];
 }
 
-static int match_bracket(const char *p, int k)
+static int casefold(int k)
+{
+	int c = towupper(k);
+	return c == k ? towlower(k) : c;
+}
+
+static int match_bracket(const char *p, int k, int kfold)
 {
 	wchar_t wc;
 	int inv = 0;
@@ -119,7 +125,10 @@ static int match_bracket(const char *p, int k)
 			wchar_t wc2;
 			int l = mbtowc(&wc2, p+1, 4);
 			if (l < 0) return 0;
-			if (wc<=wc2 && (unsigned)k-wc <= wc2-wc) return !inv;
+			if (wc <= wc2)
+				if ((unsigned)k-wc <= wc2-wc ||
+				    (unsigned)kfold-wc <= wc2-wc)
+					return !inv;
 			p += l-1;
 			continue;
 		}
@@ -132,7 +141,9 @@ static int match_bracket(const char *p, int k)
 				char buf[16];
 				memcpy(buf, p0, p-1-p0);
 				buf[p-1-p0] = 0;
-				if (iswctype(k, wctype(buf))) return !inv;
+				if (iswctype(k, wctype(buf)) ||
+				    iswctype(kfold, wctype(buf)))
+					return !inv;
 			}
 			continue;
 		}
@@ -143,7 +154,7 @@ static int match_bracket(const char *p, int k)
 			if (l < 0) return 0;
 			p += l-1;
 		}
-		if (wc==k) return !inv;
+		if (wc==k || wc==kfold) return !inv;
 	}
 	return inv;
 }
@@ -153,7 +164,7 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
 	const char *p, *ptail, *endpat;
 	const char *s, *stail, *endstr;
 	size_t pinc, sinc, tailcnt=0;
-	int c, k;
+	int c, k, kfold;
 
 	if (flags & FNM_PERIOD) {
 		if (*str == '.' && *pat != '.')
@@ -173,10 +184,11 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
 				return (c==END) ? 0 : FNM_NOMATCH;
 			str += sinc;
 			n -= sinc;
+			kfold = flags & FNM_CASEFOLD ? casefold(k) : k;
 			if (c == BRACKET) {
-				if (!match_bracket(pat, k))
+				if (!match_bracket(pat, k, kfold))
 					return FNM_NOMATCH;
-			} else if (c != QUESTION && k != c) {
+			} else if (c != QUESTION && k != c && kfold != c) {
 				return FNM_NOMATCH;
 			}
 			pat+=pinc;
@@ -233,10 +245,11 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
 			break;
 		}
 		s += sinc;
+		kfold = flags & FNM_CASEFOLD ? casefold(k) : k;
 		if (c == BRACKET) {
-			if (!match_bracket(p-pinc, k))
+			if (!match_bracket(p-pinc, k, kfold))
 				return FNM_NOMATCH;
-		} else if (c != QUESTION && k != c) {
+		} else if (c != QUESTION && k != c && kfold != c) {
 			return FNM_NOMATCH;
 		}
 	}
@@ -261,10 +274,11 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
 			k = str_next(s, endstr-s, &sinc);
 			if (!k)
 				return FNM_NOMATCH;
+			kfold = flags & FNM_CASEFOLD ? casefold(k) : k;
 			if (c == BRACKET) {
-				if (!match_bracket(p-pinc, k))
+				if (!match_bracket(p-pinc, k, kfold))
 					break;
-			} else if (c != QUESTION && k != c) {
+			} else if (c != QUESTION && k != c && kfold != c) {
 				break;
 			}
 			s += sinc;
-- 
2.1.0