diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2013-02-06 17:29:11 +0100 |
---|---|---|
committer | Kozlov Dmitry <xeb@mail.ru> | 2013-02-06 21:09:01 +0400 |
commit | a068ff609288b61f86b37b02f06755f84c5aaca7 (patch) | |
tree | 30357f8b926c01ceda91ff9564bf67d63c88fad1 /accel-pppd/utils.c | |
parent | 3565aac70d801a08b82aa3b4ed268b3470488d57 (diff) | |
download | accel-ppp-a068ff609288b61f86b37b02f06755f84c5aaca7.tar.gz accel-ppp-a068ff609288b61f86b37b02f06755f84c5aaca7.zip |
utils: Add function for strict str to int conversion
Define the u_readlong() function as a wrapper for strtol(). It performs
stricter checks on the input value and let the caller specify an interval
of acceptable values.
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd/utils.c')
-rw-r--r-- | accel-pppd/utils.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/accel-pppd/utils.c b/accel-pppd/utils.c index 491e6507..a6a76c93 100644 --- a/accel-pppd/utils.c +++ b/accel-pppd/utils.c @@ -1,4 +1,6 @@ +#include <errno.h> #include <stdio.h> +#include <stdlib.h> #include "triton.h" #include "utils.h" @@ -9,3 +11,22 @@ void __export u_inet_ntoa(in_addr_t addr, char *str) { sprintf(str, "%i.%i.%i.%i", addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff); } + +int __export u_readlong(long int *dst, const char *src, + long int min, long int max) +{ + char *src_stop = NULL; + long int rv; + + if (dst == NULL || src == NULL || src[0] == '\0') + return -1; + + errno = 0; + rv = strtol(src, &src_stop, 0); + if (errno != 0 || *src_stop != '\0' || rv < min || rv > max) { + return -1; + } else { + *dst = rv; + return 0; + } +} |