This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/msgidlib/patmat.c

64 lines
1.5 KiB
C
Raw Normal View History

2003-03-26 16:12:22 +00:00
/*
* PATMAT.C - Pattern matching. Taken from sh sources
*/
/*
2003-04-03 14:10:24 +00:00
* Written by Alexander S. Aganichev.
2003-03-26 16:12:22 +00:00
*
2003-04-03 14:10:24 +00:00
* Released to public domain.
2003-03-26 16:12:22 +00:00
*/
/*
* Returns true if the pattern matches the string.
*/
2003-04-03 14:10:24 +00:00
int patmat (char *tag, char *mask)
2003-03-26 16:12:22 +00:00
{
2003-04-03 14:10:24 +00:00
if (tag && mask) {
while (*tag && *mask)
if ((*tag == *mask) || (*mask == '?')) {
++ tag;
++ mask;
} else if (*mask == '*') {
while ((*mask == '*') || (*mask == '?')) {
if (*mask == '?') {
if (*tag)
++ tag;
else
return 0;
}
++ mask;
}
if (*mask) {
while (*tag && !patmat (tag, mask))
++ tag;
if (*tag)
return 1;
} else
return 1;
} else
return 0;
while (*mask == '*')
++ mask;
if (*mask)
return 0;
return (*tag) ? 0 : 1;
}
return 0;
2003-03-26 16:12:22 +00:00
}
#ifdef TEST
#include <stdio.h>
int main(void)
{
printf("patmat(\"abcdefghi\", \"*ghi\"): %d\n", patmat("abcdefghi", "*ghi"));
printf("patmat(\"abcdefghi\", \"??c??f*\"): %d\n", patmat("abcdefghi", "??c??f*"));
printf("patmat(\"abcdefghi\", \"*dh*\"): %d\n", patmat("abcdefghi", "*dh*"));
printf("patmat(\"abcdefghi\", \"*def\"): %d\n", patmat("abcdefghi", "*def"));
return 0;
}
#endif