summaryrefslogtreecommitdiff
path: root/lib/wildmatch.c
diff options
context:
space:
mode:
authorWayne Davison <wayned@samba.org>2003-07-05 00:03:36 +0000
committerWayne Davison <wayned@samba.org>2003-07-05 00:03:36 +0000
commit446ee5b1105cbb419bcccf8c09ad3514d3d66037 (patch)
treebd8e56d478de3bd033938ac757d1d1b489564257 /lib/wildmatch.c
parentd3e6fd39108f68f75ca6cca39a787269865a5358 (diff)
downloadrsync-446ee5b1105cbb419bcccf8c09ad3514d3d66037.tar.gz
A new wildcard-matching routine to replace the fnmatch code.
Diffstat (limited to 'lib/wildmatch.c')
-rw-r--r--lib/wildmatch.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/wildmatch.c b/lib/wildmatch.c
new file mode 100644
index 00000000..bce9545e
--- /dev/null
+++ b/lib/wildmatch.c
@@ -0,0 +1,96 @@
+/*
+** Do shell-style pattern matching for ?, \, [], and * characters.
+** It is 8bit clean.
+**
+** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
+** Rich $alz is now <rsalz@bbn.com>.
+**
+** Modified by Wayne Davison to special-case '/' matching and to fix
+** the character-class code.
+*/
+
+#include "rsync.h"
+
+/* What character marks an inverted character class? */
+#define NEGATE_CLASS '!'
+
+#define false 0
+#define true 1
+
+/* Look for pattern "p" in the "text" string. */
+int
+wildmatch(const char *p, const char *text)
+{
+ int matched, special;
+ char ch, prev;
+
+ for ( ; (ch = *p) != '\0'; text++, p++) {
+ if (*text == '\0' && ch != '*')
+ return false;
+ switch (ch) {
+ case '\\':
+ /* Literal match with following character. Note that the test
+ * in "default" handles the p[1] == '\0' failure case. */
+ ch = *++p;
+ /* FALLTHROUGH */
+ default:
+ if (*text != ch)
+ return false;
+ continue;
+ case '?':
+ /* Match anything but '/'. */
+ if (*text == '/')
+ return false;
+ continue;
+ case '*':
+ if (*++p == '*') {
+ while (*++p == '*') {}
+ special = true;
+ }
+ else
+ special = false;
+ if (*p == '\0') {
+ /* Trailing "**" matches everything. */
+ return special? true : strchr(text, '/') == 0;
+ }
+ for ( ; *text; text++) {
+ if (wildmatch(p, text))
+ return true;
+ if (!special && *text == '/')
+ return false;
+ }
+ return false;
+ case '[':
+ special = *++p == NEGATE_CLASS ? true : false;
+ if (special) {
+ /* Inverted character class. */
+ p++;
+ }
+ prev = 0;
+ matched = false;
+ ch = *p;
+ if (ch == ']' || ch == '-') {
+ if (*text == ch)
+ matched = true;
+ prev = ch;
+ ch = *++p;
+ }
+ for ( ; ch != ']'; prev = ch, ch = *++p) {
+ if (!ch)
+ return false;
+ if (ch == '-' && prev && p[1] && p[1] != ']') {
+ if (*text <= *++p && *text >= prev)
+ matched = true;
+ ch = 0;
+ }
+ else if (*text == ch)
+ matched = true;
+ }
+ if (matched == special)
+ return false;
+ continue;
+ }
+ }
+
+ return *text == '\0';
+}