added slprintf and tests for the bsdstring functions

This commit is contained in:
Enno Rehling 2012-05-29 10:45:46 -07:00
parent 131efac393
commit 330bc9c13b
8 changed files with 116 additions and 44 deletions

View File

@ -5,6 +5,8 @@
#include <iniparser/iniparser.c>
#include <mt19937ar.c>
#include <util/bsdstring.c>
#include <util/console.c>
#include <util/attrib.c>
#include <util/base36.c>
@ -33,10 +35,5 @@
#include <util/xml.c>
#ifndef HAVE_INLINE
#include <util/bsdstring.c>
#include <util/strings.c>
#endif
#ifdef __GNUC__
#include <util/strncpy.c>
#endif

View File

@ -6,6 +6,7 @@
#include "tests_test.c"
#include <util/base36_test.c>
#include <util/bsdstring_test.c>
#include <util/functions_test.c>
#include <util/quicklist_test.c>
#include <util/umlaut_test.c>
@ -49,6 +50,7 @@ int RunAllTests(void)
CuSuiteAddSuite(suite, get_tests_suite());
/* util */
CuSuiteAddSuite(suite, get_base36_suite());
CuSuiteAddSuite(suite, get_bsdstring_suite());
CuSuiteAddSuite(suite, get_quicklist_suite());
CuSuiteAddSuite(suite, get_functions_suite());
CuSuiteAddSuite(suite, get_umlaut_suite());

View File

@ -125,6 +125,7 @@
<ClCompile Include="util\base36.c" />
<ClCompile Include="util\base36_test.c" />
<ClCompile Include="util\bsdstring.c" />
<ClCompile Include="util\bsdstring_test.c" />
<ClCompile Include="util\console.c" />
<ClCompile Include="util\critbit.c" />
<ClCompile Include="util\crmessage.c" />

View File

@ -208,5 +208,8 @@
<ClCompile Include="util\umlaut_test.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util\bsdstring_test.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -2,12 +2,11 @@
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#ifndef HAVE_INLINE
#include "bsdstring.h"
#endif
INLINE_FUNCTION int wrptr(char **ptr, size_t * size, int bytes)
int wrptr(char **ptr, size_t * size, int bytes)
{
assert(bytes >= 0 || !"you're not using snprintf right, maybe?");
@ -29,8 +28,9 @@ INLINE_FUNCTION int wrptr(char **ptr, size_t * size, int bytes)
return ENAMETOOLONG;
}
#if !defined(HAVE_STRLCPY)
INLINE_FUNCTION size_t strlcpy(char *dst, const char *src, size_t siz)
#ifndef HAVE_STRLCPY
#define HAVE_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t siz)
{ /* copied from OpenBSD source code */
register char *d = dst;
register const char *s = src;
@ -53,8 +53,11 @@ INLINE_FUNCTION size_t strlcpy(char *dst, const char *src, size_t siz)
return (s - src - 1); /* count does not include NUL */
}
#endif
INLINE_FUNCTION size_t strlcat(char *dst, const char *src, size_t siz)
#ifndef HAVE_STRLCAT
#define HAVE_STRLCAT
size_t strlcat(char *dst, const char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
@ -81,3 +84,23 @@ INLINE_FUNCTION size_t strlcat(char *dst, const char *src, size_t siz)
return (dlen + (s - src)); /* count does not include NUL */
}
#endif
#ifndef HAVE_SLPRINTF
#define HAVE_SLPRINTF
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);
if (result<0 || result>=(int)size) {
dst[size-1]='\0';
return size;
}
va_start(args, format);
va_end(args);
return (size_t)result;
}
#endif

View File

@ -1,19 +1,20 @@
#ifndef UTIL_BSDSTRING_H
#define UTIL_BSDSTRING_H
#ifdef HAVE_INLINE
# include "bsdstring.c"
#else
extern size_t strlcpy(char *dst, const char *src, size_t siz);
extern size_t strlcat(char *dst, const char *src, size_t siz);
extern int wrptr(char **ptr, size_t * size, int bytes);
#ifndef HAVE_STRLCPY
extern size_t strlcpy(char *dst, const char *src, size_t siz);
#endif
#ifndef HAVE_STRLCAT
extern size_t strlcat(char *dst, const char *src, size_t siz);
#endif
#ifndef HAVE_SLPRINTF
extern size_t slprintf(char * dst, size_t size, const char * format, ...);
#endif
#define WARN_STATIC_BUFFER() log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__)
#if !defined(HAVE_STRLPRINTF)
# define HAVE_STRLPRINTF
# define slprintf snprintf
#endif
#endif

68
src/util/bsdstring_test.c Normal file
View File

@ -0,0 +1,68 @@
#include <cutest/CuTest.h>
#include "bsdstring.h"
#include <string.h>
static void test_strlcat(CuTest * tc)
{
char buffer[32];
memset(buffer, -2, sizeof(buffer));
buffer[0] = '\0';
CuAssertIntEquals(tc, 4, strlcat(buffer, "herp", 4));
CuAssertStrEquals(tc, "her", buffer);
buffer[0] = '\0';
CuAssertIntEquals(tc, 4, strlcat(buffer, "herp", 8));
CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, -2, buffer[5]);
CuAssertIntEquals(tc, 8, strlcat(buffer, "derp", 8));
CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, -2, buffer[8]);
}
static void test_strlcpy(CuTest * tc)
{
char buffer[32];
memset(buffer, -2, sizeof(buffer));
CuAssertIntEquals(tc, 4, strlcpy(buffer, "herp", 4));
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 4, strlcpy(buffer, "herp", 8));
CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, -2, buffer[5]);
CuAssertIntEquals(tc, 8, strlcpy(buffer, "herpderp", 8));
CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, -2, buffer[8]);
}
static void test_slprintf(CuTest * tc)
{
char buffer[32];
memset(buffer, -2, sizeof(buffer));
CuAssertTrue(tc, slprintf(buffer, 4, "%s", "herpderp")>3);
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 4, slprintf(buffer, 8, "%s", "herp"));
CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, -2, buffer[5]);
CuAssertIntEquals(tc, 8, slprintf(buffer, 8, "%s", "herpderp"));
CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, -2, 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;
}

View File

@ -1,23 +0,0 @@
/*
* Faster replacement for ISO-C strncpy, does not pad with zeros
*/
#include <stddef.h>
char *strncpy(char *to, const char *from, size_t size)
{
char *t = to, *f = (char *)from;
int copied = 0;
while (copied < size) {
*t = *f;
if (*f == '\0')
break;
t++;
f++;
copied++;
}
return to;
}