Skip to content

Commit 3ae5396

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f1cf7b7 commit 3ae5396

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

test-wildmatch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
int main(int argc, char **argv)
55
{
66
if (!strcmp(argv[1], "wildmatch"))
7-
return wildmatch(argv[3], argv[2]) ? 0 : 1;
7+
return !!wildmatch(argv[3], argv[2]);
88
else if (!strcmp(argv[1], "iwildmatch"))
9-
return iwildmatch(argv[3], argv[2]) ? 0 : 1;
9+
return !!iwildmatch(argv[3], argv[2]);
1010
else if (!strcmp(argv[1], "fnmatch"))
1111
return !!fnmatch(argv[3], argv[2], FNM_PATHNAME);
1212
else

wildmatch.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ typedef unsigned char uchar;
2020

2121
#define FALSE 0
2222
#define TRUE 1
23+
24+
#define NOMATCH 1
25+
#define MATCH 0
2326
#define ABORT_ALL -1
2427
#define ABORT_TO_STARSTAR -2
2528

@@ -78,12 +81,12 @@ static int dowild(const uchar *p, const uchar *text)
7881
/* FALLTHROUGH */
7982
default:
8083
if (t_ch != p_ch)
81-
return FALSE;
84+
return NOMATCH;
8285
continue;
8386
case '?':
8487
/* Match anything but '/'. */
8588
if (t_ch == '/')
86-
return FALSE;
89+
return NOMATCH;
8790
continue;
8891
case '*':
8992
if (*++p == '*') {
@@ -96,14 +99,14 @@ static int dowild(const uchar *p, const uchar *text)
9699
* only if there are no more slash characters. */
97100
if (!special) {
98101
if (strchr((char*)text, '/') != NULL)
99-
return FALSE;
102+
return NOMATCH;
100103
}
101-
return TRUE;
104+
return MATCH;
102105
}
103106
while (1) {
104107
if (t_ch == '\0')
105108
break;
106-
if ((matched = dowild(p, text)) != FALSE) {
109+
if ((matched = dowild(p, text)) != NOMATCH) {
107110
if (!special || matched != ABORT_TO_STARSTAR)
108111
return matched;
109112
} else if (!special && t_ch == '/')
@@ -202,26 +205,26 @@ static int dowild(const uchar *p, const uchar *text)
202205
matched = TRUE;
203206
} while (prev_ch = p_ch, (p_ch = *++p) != ']');
204207
if (matched == special || t_ch == '/')
205-
return FALSE;
208+
return NOMATCH;
206209
continue;
207210
}
208211
}
209212

210-
return *text ? FALSE : TRUE;
213+
return *text ? NOMATCH : MATCH;
211214
}
212215

213216
/* Match the "pattern" against the "text" string. */
214217
int wildmatch(const char *pattern, const char *text)
215218
{
216-
return dowild((const uchar*)pattern, (const uchar*)text) == TRUE;
219+
return dowild((const uchar*)pattern, (const uchar*)text);
217220
}
218221

219222
/* Match the "pattern" against the forced-to-lower-case "text" string. */
220223
int iwildmatch(const char *pattern, const char *text)
221224
{
222225
int ret;
223226
force_lower_case = 1;
224-
ret = dowild((const uchar*)pattern, (const uchar*)text) == TRUE;
227+
ret = dowild((const uchar*)pattern, (const uchar*)text);
225228
force_lower_case = 0;
226229
return ret;
227230
}

0 commit comments

Comments
 (0)