From 62130ca44b316e64391c21807b5241f749b6d431 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 31 Dec 2017 20:07:40 +0100 Subject: [PATCH] add a strncat function to sbstring, use it for create_order. eliminate bsdstring.h use. --- src/kernel/order.c | 41 ++++++++++++++++++----------------------- src/util/strings.c | 13 +++++++++++++ src/util/strings.h | 1 + src/util/strings.test.c | 6 ++++++ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index 4cdc20e9f..8defd05c9 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -19,7 +19,6 @@ #include "keyword.h" #include -#include #include #include #include @@ -235,50 +234,46 @@ order *create_order(keyword_t kwd, const struct locale * lang, order *ord; char zBuffer[DISPLAYSIZE]; if (params) { - char *bufp = zBuffer; - int bytes; - size_t size = sizeof(zBuffer) - 1; + sbstring sbs; va_list marker; + char *tok; assert(lang); va_start(marker, params); + sbs_init(&sbs, zBuffer, sizeof(zBuffer)); while (*params) { - if (*params == '%') { - int i; - const char *s; - ++params; - switch (*params) { + int i; + const char *s; + tok = strchr(params, '%'); + if (tok) { + if (tok != params) { + sbs_strncat(&sbs, params, tok - params); + } + switch (tok[1]) { case 's': s = va_arg(marker, const char *); assert(s); - bytes = (int)str_strlcpy(bufp, s, size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, s); break; case 'd': i = va_arg(marker, int); - bytes = (int)str_strlcpy(bufp, itoa10(i), size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, itoa10(i)); break; case 'i': i = va_arg(marker, int); - bytes = (int)str_strlcpy(bufp, itoa36(i), size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, itoa36(i)); break; default: assert(!"unknown format-character in create_order"); } + params = tok + 2; } - else if (size > 0) { - *bufp++ = *params; - --size; + else { + sbs_strcat(&sbs, params); + break; } - ++params; } va_end(marker); - *bufp = 0; } else { zBuffer[0] = 0; diff --git a/src/util/strings.c b/src/util/strings.c index 656a7729b..d3a1b2eec 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -249,6 +249,19 @@ void sbs_init(struct sbstring *sbs, char *buffer, size_t size) buffer[0] = '\0'; } +void sbs_strncat(struct sbstring *sbs, const char *str, size_t size) +{ + size_t len; + assert(sbs); + len = sbs->size - (sbs->end - sbs->begin) - 1; + if (len < size) { + size = len; + } + memcpy(sbs->end, str, size); + sbs->end[size] = '\0'; + sbs->end += size; +} + void sbs_strcat(struct sbstring *sbs, const char *str) { size_t len; diff --git a/src/util/strings.h b/src/util/strings.h index a9fa46d3a..76bc5a12a 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -46,6 +46,7 @@ extern "C" { void sbs_init(struct sbstring *sbs, char *buffer, size_t size); void sbs_strcat(struct sbstring *sbs, const char *str); + void sbs_strncat(struct sbstring *sbs, const char *str, size_t size); void sbs_strcpy(struct sbstring *sbs, const char *str); /* benchmark for units: diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 8d80183b7..89c880a70 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -109,6 +109,12 @@ static void test_sbstring(CuTest * tc) sbs_strcat(&sbs, "12345678901234567890"); CuAssertStrEquals(tc, "123456789012345", sbs.begin); CuAssertPtrEquals(tc, buffer, sbs.begin); + sbs_strcpy(&sbs, "1234567890"); + CuAssertStrEquals(tc, "1234567890", sbs.begin); + sbs_strncat(&sbs, "1234567890", 4); + CuAssertStrEquals(tc, "12345678901234", sbs.begin); + sbs_strncat(&sbs, "567890", 6); + CuAssertStrEquals(tc, "123456789012345", sbs.begin); } CuSuite *get_strings_suite(void)