aboutsummaryrefslogtreecommitdiff
path: root/wildcard_cmp.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-06-06 01:58:42 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-06-06 03:48:50 +0200
commitd4c8a8062ff722d7165f4b5faefe0f9b73d3738a (patch)
tree3282708835e37a96082cc346e71162de3e1ec924 /wildcard_cmp.c
parentc4a3d54ac9f28d4690a88d9abcc262e6ffb9e381 (diff)
downloadslcl-d4c8a8062ff722d7165f4b5faefe0f9b73d3738a.tar.gz
Split wildcard_cmp into its own component
Future commits will make use of this function outside handler.c.
Diffstat (limited to 'wildcard_cmp.c')
-rw-r--r--wildcard_cmp.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/wildcard_cmp.c b/wildcard_cmp.c
new file mode 100644
index 0000000..8f7c7c1
--- /dev/null
+++ b/wildcard_cmp.c
@@ -0,0 +1,44 @@
+#include "wildcard_cmp.h"
+#include <string.h>
+
+static int wildcard_cmp(const char *s, const char *p)
+{
+ while (*p && *s)
+ {
+ const char *const wc = strchr(p, '*');
+
+ if (!wc)
+ return strcmp(s, p);
+
+ const size_t n = wc - p;
+
+ if (n)
+ {
+ const int r = strncmp(s, p, n);
+
+ if (r)
+ return r;
+
+ p += n;
+ s += n;
+ }
+ else if (*(wc + 1) == *s)
+ {
+ p = wc + 1;
+ s += n;
+ }
+ else if (*(wc + 1) == '*')
+ p++;
+ else
+ {
+ s++;
+ p += n;
+ }
+ }
+
+ while (*p)
+ if (*p++ != '*')
+ return -1;
+
+ return 0;
+}