Commit 8396be0f authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement parse_nat, an error-checking variant of atoi.

parent e331aec3
......@@ -27,6 +27,7 @@ THE SOFTWARE.
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
......@@ -131,6 +132,26 @@ timeval_min_sec(struct timeval *d, time_t secs)
}
}
/* There's no good name for a positive int in C, call it nat. */
int
parse_nat(const char *string)
{
long l;
char *end;
l = strtol(string, &end, 0);
while(*end == ' ' || *end == '\t')
end++;
if(*end != '\0')
return -1;
if(l < 0 || l > INT_MAX)
return -1;
return (int)l;
}
int
parse_msec(const char *string)
{
......
......@@ -77,6 +77,7 @@ int timeval_compare(const struct timeval *s1, const struct timeval *s2)
ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
void timeval_min_sec(struct timeval *d, time_t secs);
int parse_nat(const char *string) ATTRIBUTE ((pure));
int parse_msec(const char *string) ATTRIBUTE ((pure));
void do_debugf(int level, const char *format, ...)
ATTRIBUTE ((format (printf, 2, 3))) COLD;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment