diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2011-05-14 16:06:08 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2011-05-14 16:06:08 +0000 |
commit | 060aa662da1355a03d3193d7e0dcd9b0bbc33ccc (patch) | |
tree | ca1f2c782e5a1569bba81bda878c94b56f4a3b58 /mandoc.c | |
parent | e0e21a567e51c014b019aee34241e8ae82a2eaca (diff) | |
download | mandoc-060aa662da1355a03d3193d7e0dcd9b0bbc33ccc.tar.gz |
Move roff.c's strtol into libmandoc.h for use by other parts of the code
(which will come).
Diffstat (limited to 'mandoc.c')
-rw-r--r-- | mandoc.c | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -23,6 +23,8 @@ #include <assert.h> #include <ctype.h> +#include <errno.h> +#include <limits.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -686,3 +688,35 @@ mandoc_getcontrol(const char *cp, int *ppos) *ppos = pos; return(1); } + +/* + * Convert a string to a long that may not be <0. + * If the string is invalid, or is less than 0, return -1. + */ +int +mandoc_strntou(const char *p, size_t sz, int base) +{ + char buf[32]; + char *ep; + long v; + + if (sz > 31) + return(-1); + + memcpy(buf, p, sz); + buf[sz] = '\0'; + + errno = 0; + v = strtol(buf, &ep, base); + + if (buf[0] == '\0' || *ep != '\0') + return(-1); + + if ((errno == ERANGE && + (v == LONG_MAX || v == LONG_MIN)) || + (v > INT_MAX || v < 0)) + return(-1); + + return((int)v); +} + |