From 5751e17f985d4048d47791898b6c7977baa16ffa Mon Sep 17 00:00:00 2001 From: Ivan Polyakov Date: Wed, 19 Oct 2022 22:01:49 +0300 Subject: [PATCH] c code rewritten to c99 without gnu extensions --- .clangd | 2 +- c/keyval.c | 5 +++-- c/query.c | 8 ++++---- c/url.c | 10 +++++----- c/utils.c | 29 +++++++++++++++++++++++++++++ c/utils.h | 4 ++++ config.mk | 2 +- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/.clangd b/.clangd index 2a1dc75..e343b1b 100644 --- a/.clangd +++ b/.clangd @@ -13,7 +13,7 @@ CompileFlags: If: PathMatch: [.*\.c, .*\.h] CompileFlags: - Add: [-std=gnu99] + Add: [-std=c99] --- diff --git a/c/keyval.c b/c/keyval.c index 23d99cb..0a17d06 100644 --- a/c/keyval.c +++ b/c/keyval.c @@ -2,6 +2,7 @@ /* Copyright 2022 Ivan Polyakov */ #include "keyval.h" +#include "utils.h" #include #include #include @@ -57,8 +58,8 @@ int rpd_keyval_insert(rpd_keyval *keyval, const char *key, const char *value) keyval->size++; } - item->key = strdup(key); - item->val = strdup(value); + item->key = rpd_strdup(key); + item->val = rpd_strdup(value); return item->key && item->val ? 0 : 2; } diff --git a/c/query.c b/c/query.c index 1219e98..7515bd8 100644 --- a/c/query.c +++ b/c/query.c @@ -17,7 +17,7 @@ int rpd_query_parse(rpd_keyval *dest, const char *src) return 0; } - char *query = strdup(src); + char *query = rpd_strdup(src); if (!query) return 1; @@ -28,7 +28,7 @@ int rpd_query_parse(rpd_keyval *dest, const char *src) char *tokens = query, *p = query; int i = 0; - while ((p = strsep(&tokens, "&\n")) && i < len) { + while ((p = rpd_strsep(&tokens, "&\n")) && i < len) { char *param, *val; if (*p == '=') @@ -37,8 +37,8 @@ int rpd_query_parse(rpd_keyval *dest, const char *src) val = strtok(NULL, "="); if (param) { - dest->items[i].key = strdup(param); - dest->items[i].val = val ? strdup(val) : NULL; + dest->items[i].key = rpd_strdup(param); + dest->items[i].val = val ? rpd_strdup(val) : NULL; i++; } } diff --git a/c/url.c b/c/url.c index e7b5046..9cf99cf 100644 --- a/c/url.c +++ b/c/url.c @@ -23,11 +23,11 @@ int rpd_url_parse(rpd_url *dest, const char *src) i = 0; char *tmp, *token; - tmp = strdup(src); - while ((token = strsep(&tmp, "/"))) { + tmp = rpd_strdup(src); + while ((token = rpd_strsep(&tmp, "/"))) { if (!strlen(token)) continue; - dest->parts[i] = strdup(token); + dest->parts[i] = rpd_strdup(token); i++; } free(tmp); @@ -65,7 +65,7 @@ int rpd_url_params_parse_keys(rpd_keyval *dest, const rpd_url *tpl) for (int i = 0; i < tpl->parts_len; i++) { if (*tpl->parts[i] == ':') { - dest->items[dest->size].key = strdup(tpl->parts[i] + 1); + dest->items[dest->size].key = rpd_strdup(tpl->parts[i] + 1); dest->size++; } } @@ -78,7 +78,7 @@ int rpd_url_params_parse_vals(rpd_keyval *dest, const rpd_url *url, const rpd_ur int i = 0, j = 0; while (i < tpl->parts_len) { if (*tpl->parts[i] == ':') { - dest->items[j++].val = strdup(url->parts[i]); + dest->items[j++].val = rpd_strdup(url->parts[i]); } i++; } diff --git a/c/utils.c b/c/utils.c index fb2762b..174ed2a 100644 --- a/c/utils.c +++ b/c/utils.c @@ -2,6 +2,8 @@ /* Copyright 2022 Ivan Polyakov */ #include "utils.h" +#include +#include int count_char_entries(const char *str, char ch) { @@ -14,3 +16,30 @@ int count_char_entries(const char *str, char ch) } return cnt; } + +char *rpd_strdup(const char *src) +{ + size_t size = strlen(src) + 1; + char *dest = (char *) malloc(sizeof(char) * size); + if (!dest) { + return NULL; + } + return memcpy(dest, src, size); +} + +char *rpd_strsep(char **str, const char *sep) +{ + char *s = *str, *end; + if (!s) { + return NULL; + } + + end = s + strcspn(s, sep); + if (*end) { + *end++ = 0; + } else { + end = 0; + } + *str = end; + return s; +} diff --git a/c/utils.h b/c/utils.h index 2ca2d9c..53c6430 100644 --- a/c/utils.h +++ b/c/utils.h @@ -6,4 +6,8 @@ int count_char_entries(const char *str, char ch); +char *rpd_strdup(const char *src); + +char *rpd_strsep(char **str, const char *sep); + #endif /* RAPIDA_UTILS_H_ENTRY */ diff --git a/config.mk b/config.mk index c28750c..9f9c983 100644 --- a/config.mk +++ b/config.mk @@ -3,7 +3,7 @@ VERSION=0.2.2 PREFIX=/usr/local CC=gcc -CFLAGS=-std=gnu99 -pedantic -Iinclude +CFLAGS=-std=c99 -pedantic -Iinclude CXX=c++ CXXFLAGS=-pedantic -Iinclude CXXSTD=-ansi