forked from github/server
move that errno-preserving atoi to strings.c
This commit is contained in:
parent
c2570b1e58
commit
01edb1e204
|
@ -54,13 +54,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <kernel/unit.h>
|
||||
|
||||
/* from libutil */
|
||||
#include <util/base36.h>
|
||||
#include <util/goodies.h>
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/param.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/strings.h>
|
||||
|
||||
/* from libc */
|
||||
#include <assert.h>
|
||||
|
@ -180,7 +180,7 @@ int destroy_cmd(unit * u, struct order *ord)
|
|||
s = gettoken(token, sizeof(token));
|
||||
|
||||
if (s && *s) {
|
||||
n = atoi10((const char *)s);
|
||||
n = str_atoi(s);
|
||||
|
||||
if (n <= 0) {
|
||||
n = INT_MAX;
|
||||
|
|
|
@ -23,14 +23,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
int atoi10(const char *str)
|
||||
{
|
||||
int i = atoi(str);
|
||||
errno = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
int atoi36(const char *str)
|
||||
{
|
||||
|
|
|
@ -22,10 +22,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include "strings.h"
|
||||
|
||||
/* libc includes */
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef HAVE_LIBBSD
|
||||
|
@ -34,6 +35,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
int str_atoi(const char *str)
|
||||
{
|
||||
int e = errno;
|
||||
int i = atoi(str);
|
||||
errno = e;
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t str_strlcpy(char *dst, const char *src, size_t len)
|
||||
{
|
||||
#ifdef HAVE_BSDSTRING
|
||||
|
|
|
@ -27,6 +27,7 @@ extern "C" {
|
|||
|
||||
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value);
|
||||
int str_hash(const char *s);
|
||||
int str_atoi(const char *s);
|
||||
size_t str_slprintf(char * dst, size_t size, const char * format, ...);
|
||||
size_t str_strlcpy(char *dst, const char *src, size_t len);
|
||||
size_t str_strlcat(char *dst, const char *src, size_t len);
|
||||
|
|
|
@ -71,6 +71,27 @@ static void test_str_hash(CuTest * tc)
|
|||
CuAssertIntEquals(tc, 140703196, str_hash("Hodor"));
|
||||
}
|
||||
|
||||
static void test_str_atoi(CuTest * tc)
|
||||
{
|
||||
errno = 0;
|
||||
CuAssertIntEquals(tc, 0, str_atoi("0"));
|
||||
CuAssertIntEquals(tc, 4, str_atoi("4"));
|
||||
CuAssertIntEquals(tc, 42, str_atoi("42"));
|
||||
CuAssertIntEquals(tc, -4, str_atoi("-4"));
|
||||
CuAssertIntEquals(tc, 0, errno);
|
||||
CuAssertIntEquals(tc, 4, str_atoi("4a"));
|
||||
CuAssertIntEquals(tc, 8, str_atoi("08"));
|
||||
CuAssertIntEquals(tc, 0, str_atoi("0x8"));
|
||||
CuAssertIntEquals(tc, 0, str_atoi("a"));
|
||||
CuAssertIntEquals(tc, 0, errno);
|
||||
errno = ERANGE;
|
||||
CuAssertIntEquals(tc, 0, str_atoi("a"));
|
||||
CuAssertIntEquals(tc, ERANGE, errno);
|
||||
errno = EINVAL;
|
||||
CuAssertIntEquals(tc, 0, str_atoi("a"));
|
||||
CuAssertIntEquals(tc, EINVAL, errno);
|
||||
}
|
||||
|
||||
static void test_str_slprintf(CuTest * tc)
|
||||
{
|
||||
char buffer[32];
|
||||
|
@ -157,6 +178,7 @@ static void test_sbstring(CuTest * tc)
|
|||
CuSuite *get_strings_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_str_atoi);
|
||||
SUITE_ADD_TEST(suite, test_str_hash);
|
||||
SUITE_ADD_TEST(suite, test_str_escape);
|
||||
SUITE_ADD_TEST(suite, test_str_escape_ex);
|
||||
|
|
Loading…
Reference in New Issue