diff options
Diffstat (limited to 'src/string.c')
-rw-r--r-- | src/string.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c index 878efeb..3a52ab2 100644 --- a/src/string.c +++ b/src/string.c @@ -6,7 +6,10 @@ */ #include "string.h" +#include "log.h" +#include <errno.h> +#include <stdlib.h> #include <string.h> char *stpecpy(char *dst, char *end, const char *src) @@ -23,3 +26,24 @@ char *stpecpy(char *dst, char *end, const char *src) end[-1] = '\0'; return end; } + +int string_to_int(const char *src, int *result) +{ + char *endptr = NULL; + + errno = 0; + long ret = strtol(src, &endptr, 10); + + if (errno) { + log_errno("strtol"); + return -1; + } + + if (endptr == src || *endptr != '\0') { + log_err("Invalid number: %s\n", src); + return -1; + } + + *result = (int)ret; + return 0; +} |