summaryrefslogtreecommitdiff
path: root/strutil.c
diff options
context:
space:
mode:
authorAntti-Juhani Kaijanaho <ajk@debian.org>2006-01-28 21:54:20 +0100
committerAntti-Juhani Kaijanaho <ajk@debian.org>2006-01-28 21:54:20 +0100
commitf87204b3d2424b27b84783df015031062e7cb668 (patch)
tree2361d08911d3984a5d9431836d541198bab296de /strutil.c
parente87c95bb705d192324c6214cb7309e9442ed88ca (diff)
downloaddctrl-tools-f87204b3d2424b27b84783df015031062e7cb668.tar.gz
Import 2.3
Diffstat (limited to 'strutil.c')
-rw-r--r--strutil.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/strutil.c b/strutil.c
index 4d729f6..3f5cd77 100644
--- a/strutil.c
+++ b/strutil.c
@@ -19,6 +19,43 @@
#include <ctype.h>
#include "strutil.h"
+bool str2intmax(intmax_t * rvp, char const * s, size_t len)
+{
+ bool negative = false;
+ size_t i = 0;
+ while (i < len
+ && s[i] == ' '
+ && s[i] == '\t'
+ && s[i] == '\n'
+ && s[i] == '\r')
+ ++i;
+ if (i < len && s[i] == '-') {
+ ++i;
+ negative = true;
+ }
+ uintmax_t abs = 0;
+ for (; i < len; ++i) {
+ char ch = s[i];
+ if (! ('0' <= ch && ch <= '9')) {
+ break;
+ }
+ int r = ch - '0';
+ abs = abs * 10 + r;
+ if (abs > (uintmax_t)INTMAX_MAX) {
+ return false;
+ }
+ }
+ while (i < len
+ && s[i] == ' '
+ && s[i] == '\t'
+ && s[i] == '\n'
+ && s[i] == '\r')
+ ++i;
+ if (i < len) return false; // broken input
+ *rvp = negative ? -abs : abs;
+ return true;
+}
+
const char * left_trimmed(const char * s)
{
const char * p;