1
0
Fork 0

Split wildcard_cmp into its own component

Future commits will make use of this function outside handler.c.
This commit is contained in:
Xavier Del Campo Romero 2023-06-06 01:58:42 +02:00
parent 3f64235123
commit 0759122e4c
Signed by untrusted user: xavi
GPG Key ID: 84FF3612A9BF43F2
4 changed files with 53 additions and 43 deletions

View File

@ -20,7 +20,8 @@ OBJECTS = \
jwt.o \
main.o \
page.o \
server.o
server.o \
wildcard_cmp.o \
all: $(PROJECT)

View File

@ -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)
{

44
wildcard_cmp.c Normal file
View File

@ -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;
}

6
wildcard_cmp.h Normal file
View File

@ -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 */