diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 01:58:42 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 03:48:50 +0200 |
| commit | d4c8a8062ff722d7165f4b5faefe0f9b73d3738a (patch) | |
| tree | 3282708835e37a96082cc346e71162de3e1ec924 | |
| parent | c4a3d54ac9f28d4690a88d9abcc262e6ffb9e381 (diff) | |
| download | slcl-d4c8a8062ff722d7165f4b5faefe0f9b73d3738a.tar.gz | |
Split wildcard_cmp into its own component
Future commits will make use of this function outside handler.c.
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | handler.c | 43 | ||||
| -rw-r--r-- | main.c | 1 | ||||
| -rw-r--r-- | wildcard_cmp.c | 44 | ||||
| -rw-r--r-- | wildcard_cmp.h | 6 |
6 files changed, 55 insertions, 43 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 83c25b1..1b4a381 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(${PROJECT_NAME} main.c page.c server.c + wildcard_cmp.c ) target_compile_options(${PROJECT_NAME} PRIVATE -Wall) target_compile_definitions(${PROJECT_NAME} PRIVATE _FILE_OFFSET_BITS=64) @@ -20,7 +20,8 @@ OBJECTS = \ jwt.o \ main.o \ page.o \ - server.o + server.o \ + wildcard_cmp.o \ all: $(PROJECT) @@ -3,6 +3,7 @@ #include "handler.h" #include "http.h" #include "server.h" +#include "wildcard_cmp.h" #include <errno.h> #include <stdbool.h> #include <stddef.h> @@ -47,48 +48,6 @@ static int on_write(const void *const buf, const size_t n, void *const user) return server_write(buf, n, c->c); } -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; -} - static int on_payload(const struct http_payload *const p, struct http_response *const r, void *const user) { @@ -6,6 +6,7 @@ #include "hex.h" #include "http.h" #include "page.h" +#include "wildcard_cmp.h" #include <openssl/err.h> #include <openssl/rand.h> #include <dynstr.h> 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; +} diff --git a/wildcard_cmp.h b/wildcard_cmp.h new file mode 100644 index 0000000..9024dbc --- /dev/null +++ b/wildcard_cmp.h @@ -0,0 +1,6 @@ +#ifndef WILDCARD_CMP_H +#define WILDCARD_CMP_H + +int wildcard_cmp(const char *s, const char *p); + +#endif /* WILDCARD_CMP_H */ |
