aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mandoc.c
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2011-05-14 16:06:08 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2011-05-14 16:06:08 +0000
commitbc80934db614d28c7b53a0b013dd68c4e6234c7e (patch)
treebcfd50f5fc85c0ca8c328e04521d96dcd6ff8e67 /mandoc.c
parentf9f5e761b11d13e40b541734e962b5931fa8f5d9 (diff)
downloadmandoc-bc80934db614d28c7b53a0b013dd68c4e6234c7e.tar.gz
mandoc-bc80934db614d28c7b53a0b013dd68c4e6234c7e.tar.zst
mandoc-bc80934db614d28c7b53a0b013dd68c4e6234c7e.zip
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.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/mandoc.c b/mandoc.c
index c0ffa585..249a0dea 100644
--- a/mandoc.c
+++ b/mandoc.c
@@ -1,4 +1,4 @@
-/* $Id: mandoc.c,v 1.49 2011/04/30 10:18:24 kristaps Exp $ */
+/* $Id: mandoc.c,v 1.50 2011/05/14 16:06:09 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -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);
+}
+