wildmatch: make wildmatch's return value compatible with fnmatch
wildmatch returns non-zero if matched, zero otherwise. This patch makes it return zero if matches, non-zero otherwise, like fnmatch(). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Oct 15, 2012 at 13:25 UTC
3ae5396cf719000039c5d0d35f9bf934f647b030
2 files changed
+14
-11
test-wildmatch.c
+2
-2
@@ -4,9 +4,9 @@
4
int main(int argc, char **argv)
5
{
6
if (!strcmp(argv[1], "wildmatch"))
7
- return wildmatch(argv[3], argv[2]) ? 0 : 1;
7
+ return !!wildmatch(argv[3], argv[2]);
8
else if (!strcmp(argv[1], "iwildmatch"))
9
- return iwildmatch(argv[3], argv[2]) ? 0 : 1;
9
+ return !!iwildmatch(argv[3], argv[2]);
10
else if (!strcmp(argv[1], "fnmatch"))
11
return !!fnmatch(argv[3], argv[2], FNM_PATHNAME);
12
else
wildmatch.c
+12
-9
@@ -20,6 +20,9 @@ typedef unsigned char uchar;
20
21
#define FALSE 0
22
#define TRUE 1
23
+
24
+#define NOMATCH 1
25
+#define MATCH 0
26
#define ABORT_ALL -1
27
#define ABORT_TO_STARSTAR -2
28
@@ -78,12 +81,12 @@ static int dowild(const uchar *p, const uchar *text)
81
/* FALLTHROUGH */
82
default:
83
if (t_ch != p_ch)
81
- return FALSE;
84
+ return NOMATCH;
85
continue;
86
case '?':
87
/* Match anything but '/'. */
88
if (t_ch == '/')
86
- return FALSE;
89
+ return NOMATCH;
90
continue;
91
case '*':
92
if (*++p == '*') {
@@ -96,14 +99,14 @@ static int dowild(const uchar *p, const uchar *text)
99
* only if there are no more slash characters. */
100
if (!special) {
101
if (strchr((char*)text, '/') != NULL)
99
- return FALSE;
102
+ return NOMATCH;
103
}
101
- return TRUE;
104
+ return MATCH;
105
}
106
while (1) {
107
if (t_ch == '\0')
108
break;
106
- if ((matched = dowild(p, text)) != FALSE) {
109
+ if ((matched = dowild(p, text)) != NOMATCH) {
110
if (!special || matched != ABORT_TO_STARSTAR)
111
return matched;
112
} else if (!special && t_ch == '/')
@@ -202,18 +205,18 @@ static int dowild(const uchar *p, const uchar *text)
205
matched = TRUE;
206
} while (prev_ch = p_ch, (p_ch = *++p) != ']');
207
if (matched == special || t_ch == '/')
205
- return FALSE;
208
+ return NOMATCH;
209
continue;
210
}
211
}
212
210
- return *text ? FALSE : TRUE;
213
+ return *text ? NOMATCH : MATCH;
214
}
215
216
/* Match the "pattern" against the "text" string. */
217
int wildmatch(const char *pattern, const char *text)
218
{
216
- return dowild((const uchar*)pattern, (const uchar*)text) == TRUE;
219
+ return dowild((const uchar*)pattern, (const uchar*)text);
220
}
221
222
/* Match the "pattern" against the forced-to-lower-case "text" string. */
@@ -221,7 +224,7 @@ int iwildmatch(const char *pattern, const char *text)
224
{
225
int ret;
226
force_lower_case = 1;
224
- ret = dowild((const uchar*)pattern, (const uchar*)text) == TRUE;
227
+ ret = dowild((const uchar*)pattern, (const uchar*)text);
228
force_lower_case = 0;
229
return ret;
230
}