diff --git a/src/kernel/build.c b/src/kernel/build.c index 71d5543fd..4ad7815c8 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -54,13 +54,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* from libutil */ -#include #include #include #include #include #include #include +#include /* from libc */ #include @@ -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; diff --git a/src/util/base36.c b/src/util/base36.c index e68fd76f6..3b5543e88 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -23,14 +23,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include - -int atoi10(const char *str) -{ - int i = atoi(str); - errno = 0; - return i; -} int atoi36(const char *str) { diff --git a/src/util/strings.c b/src/util/strings.c index ef351ab1f..e85d8ca56 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -22,10 +22,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "strings.h" /* libc includes */ +#include #include +#include #include #include -#include #include #ifdef HAVE_LIBBSD @@ -34,6 +35,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #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 diff --git a/src/util/strings.h b/src/util/strings.h index bd6f0de58..6524f5970 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -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); diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 2205e538b..211ec65cc 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -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);