slprintf is not in BSD (it's theft from samba).

This commit is contained in:
Enno Rehling 2017-12-11 19:23:56 +01:00
parent af7cc02388
commit 93613b99af
10 changed files with 44 additions and 43 deletions

View File

@ -69,6 +69,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/lists.h> #include <util/lists.h>
#include <util/log.h> #include <util/log.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/strings.h>
#include <selist.h> #include <selist.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>

View File

@ -39,7 +39,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h> #include <util/gamedata.h>
#include <util/goodies.h> #include <util/goodies.h>
@ -50,6 +49,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/password.h> #include <util/password.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/strings.h>
#include <util/variant.h> #include <util/variant.h>
#include <selist.h> #include <selist.h>

View File

@ -90,6 +90,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/password.h> #include <util/password.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/strings.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <util/unicode.h> #include <util/unicode.h>

View File

@ -78,6 +78,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/parser.h> #include <util/parser.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/strings.h>
#include <storage.h> #include <storage.h>

View File

@ -1,9 +1,7 @@
#include <platform.h> #include <platform.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h>
#include <limits.h> #include <limits.h>
#include "bsdstring.h" #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 */ 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;
}

View File

@ -9,7 +9,6 @@ int wrptr(char **ptr, size_t * size, int bytes);
#undef HAVE_SLPRINTF #undef HAVE_SLPRINTF
#ifdef HAVE_BSDSTRING #ifdef HAVE_BSDSTRING
#define HAVE_STRLCAT #define HAVE_STRLCAT
#define HAVE_SLPRINTF
#define HAVE_STRLCPY #define HAVE_STRLCPY
#else #else
#include <string.h> #include <string.h>
@ -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); size_t strlcat(char *dst, const char *src, size_t siz);
#endif #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_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 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__) #define INFO_STATIC_BUFFER() log_info("static buffer too small in %s:%d\n", __FILE__, __LINE__)

View File

@ -42,29 +42,10 @@ static void test_strlcpy(CuTest * tc)
errno = 0; 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 *get_bsdstring_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_strlcat); SUITE_ADD_TEST(suite, test_strlcat);
SUITE_ADD_TEST(suite, test_strlcpy); SUITE_ADD_TEST(suite, test_strlcpy);
SUITE_ADD_TEST(suite, test_slprintf);
return suite; return suite;
} }

View File

@ -22,10 +22,28 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "assert.h" #include "assert.h"
/* libc includes */ /* libc includes */
#include <stdarg.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
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) char *set_string(char **s, const char *neu)
{ {
if (neu == NULL) { if (neu == NULL) {

View File

@ -29,6 +29,8 @@ extern "C" {
const char *str_escape(const char *str, char *buffer, size_t len); const char *str_escape(const char *str, char *buffer, size_t len);
char *set_string(char **s, const char *neu); char *set_string(char **s, const char *neu);
unsigned int str_hash(const char *s); 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 jenkins_hash(unsigned int a);
unsigned int wang_hash(unsigned int a); unsigned int wang_hash(unsigned int a);
@ -49,6 +51,7 @@ extern "C" {
#define HASH1 JENKINS_HASH1 #define HASH1 JENKINS_HASH1
#define HASH2 JENKINS_HASH2 #define HASH2 JENKINS_HASH2
#define slprintf str_slprintf
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -29,11 +29,30 @@ static void test_str_hash(CuTest * tc)
CuAssertIntEquals(tc, 140703196, str_hash("Hodor")); 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 *get_strings_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_str_hash); SUITE_ADD_TEST(suite, test_str_hash);
SUITE_ADD_TEST(suite, test_str_escape); SUITE_ADD_TEST(suite, test_str_escape);
SUITE_ADD_TEST(suite, test_str_replace); SUITE_ADD_TEST(suite, test_str_replace);
SUITE_ADD_TEST(suite, test_str_slprintf);
return suite; return suite;
} }