From 93613b99af676898e09f9ecf59b3d1f6e9af1035 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 11 Dec 2017 19:23:56 +0100 Subject: [PATCH] slprintf is not in BSD (it's theft from samba). --- src/battle.c | 1 + src/kernel/faction.c | 2 +- src/laws.c | 1 + src/move.c | 1 + src/util/bsdstring.c | 18 ------------------ src/util/bsdstring.h | 5 ----- src/util/bsdstring.test.c | 19 ------------------- src/util/strings.c | 18 ++++++++++++++++++ src/util/strings.h | 3 +++ src/util/strings.test.c | 19 +++++++++++++++++++ 10 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/battle.c b/src/battle.c index c12e6d46e..92a2c54c5 100644 --- a/src/battle.c +++ b/src/battle.c @@ -69,6 +69,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include #include #include diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 8da74e80e..f0010ddce 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -39,7 +39,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* util includes */ #include #include -#include #include #include #include @@ -50,6 +49,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include #include diff --git a/src/laws.c b/src/laws.c index f03f76730..873dc470f 100644 --- a/src/laws.c +++ b/src/laws.c @@ -90,6 +90,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include #include diff --git a/src/move.c b/src/move.c index 592ca606f..69a84c2cd 100644 --- a/src/move.c +++ b/src/move.c @@ -78,6 +78,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include diff --git a/src/util/bsdstring.c b/src/util/bsdstring.c index f59b0ddaa..1ac1246fa 100644 --- a/src/util/bsdstring.c +++ b/src/util/bsdstring.c @@ -1,9 +1,7 @@ #include -#include #include #include #include -#include #include #include "bsdstring.h" @@ -101,19 +99,3 @@ size_t strlcat(char *dst, const char *src, size_t siz) return (dlen + (s - src)); /* count does not include NUL */ } - -size_t slprintf(char * dst, size_t size, const char * format, ...) -{ - va_list args; - int result; - - va_start(args, format); - result = vsnprintf(dst, size, format, args); - va_end(args); - if (result < 0 || result >= (int)size) { - dst[size - 1] = '\0'; - return size; - } - - return (size_t)result; -} diff --git a/src/util/bsdstring.h b/src/util/bsdstring.h index 923cf60d8..463a043c8 100644 --- a/src/util/bsdstring.h +++ b/src/util/bsdstring.h @@ -9,7 +9,6 @@ int wrptr(char **ptr, size_t * size, int bytes); #undef HAVE_SLPRINTF #ifdef HAVE_BSDSTRING #define HAVE_STRLCAT -#define HAVE_SLPRINTF #define HAVE_STRLCPY #else #include @@ -24,10 +23,6 @@ char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const size_t strlcat(char *dst, const char *src, size_t siz); #endif -#ifndef HAVE_SLPRINTF -size_t slprintf(char * dst, size_t size, const char * format, ...); -#endif - #define WARN_STATIC_BUFFER_EX(foo) log_warning("%s: static buffer too small in %s:%d\n", (foo), __FILE__, __LINE__) #define WARN_STATIC_BUFFER() log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__) #define INFO_STATIC_BUFFER() log_info("static buffer too small in %s:%d\n", __FILE__, __LINE__) diff --git a/src/util/bsdstring.test.c b/src/util/bsdstring.test.c index d4276ef61..61453f893 100644 --- a/src/util/bsdstring.test.c +++ b/src/util/bsdstring.test.c @@ -42,29 +42,10 @@ static void test_strlcpy(CuTest * tc) errno = 0; } -static void test_slprintf(CuTest * tc) -{ - char buffer[32]; - - memset(buffer, 0x7f, sizeof(buffer)); - - CuAssertTrue(tc, slprintf(buffer, 4, "%s", "herpderp") > 3); - CuAssertStrEquals(tc, "her", buffer); - - CuAssertIntEquals(tc, 4, (int)slprintf(buffer, 8, "%s", "herp")); - CuAssertStrEquals(tc, "herp", buffer); - CuAssertIntEquals(tc, 0x7f, buffer[5]); - - CuAssertIntEquals(tc, 8, (int)slprintf(buffer, 8, "%s", "herpderp")); - CuAssertStrEquals(tc, "herpder", buffer); - CuAssertIntEquals(tc, 0x7f, buffer[8]); -} - CuSuite *get_bsdstring_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_strlcat); SUITE_ADD_TEST(suite, test_strlcpy); - SUITE_ADD_TEST(suite, test_slprintf); return suite; } diff --git a/src/util/strings.c b/src/util/strings.c index 55d4ca5fb..a63a694ce 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -22,10 +22,28 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "assert.h" /* libc includes */ +#include +#include #include #include #include +size_t str_slprintf(char * dst, size_t size, const char * format, ...) +{ + va_list args; + int result; + + va_start(args, format); + result = vsnprintf(dst, size, format, args); + va_end(args); + if (result < 0 || result >= (int)size) { + dst[size - 1] = '\0'; + return size; + } + + return (size_t)result; +} + char *set_string(char **s, const char *neu) { if (neu == NULL) { diff --git a/src/util/strings.h b/src/util/strings.h index f63299d93..9add04e23 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -29,6 +29,8 @@ extern "C" { const char *str_escape(const char *str, char *buffer, size_t len); char *set_string(char **s, const char *neu); unsigned int str_hash(const char *s); + size_t str_slprintf(char * dst, size_t size, const char * format, ...); + unsigned int jenkins_hash(unsigned int a); unsigned int wang_hash(unsigned int a); @@ -49,6 +51,7 @@ extern "C" { #define HASH1 JENKINS_HASH1 #define HASH2 JENKINS_HASH2 +#define slprintf str_slprintf #ifdef __cplusplus } diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 60a0b5d37..14d62200a 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -29,11 +29,30 @@ static void test_str_hash(CuTest * tc) CuAssertIntEquals(tc, 140703196, str_hash("Hodor")); } +static void test_str_slprintf(CuTest * tc) +{ + char buffer[32]; + + memset(buffer, 0x7f, sizeof(buffer)); + + CuAssertTrue(tc, slprintf(buffer, 4, "%s", "herpderp") > 3); + CuAssertStrEquals(tc, "her", buffer); + + CuAssertIntEquals(tc, 4, (int)str_slprintf(buffer, 8, "%s", "herp")); + CuAssertStrEquals(tc, "herp", buffer); + CuAssertIntEquals(tc, 0x7f, buffer[5]); + + CuAssertIntEquals(tc, 8, (int)str_slprintf(buffer, 8, "%s", "herpderp")); + CuAssertStrEquals(tc, "herpder", buffer); + CuAssertIntEquals(tc, 0x7f, buffer[8]); +} + CuSuite *get_strings_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_str_hash); SUITE_ADD_TEST(suite, test_str_escape); SUITE_ADD_TEST(suite, test_str_replace); + SUITE_ADD_TEST(suite, test_str_slprintf); return suite; }