]> granicus.if.org Git - libevent/commitdiff
Fall back to sscanf if we have no other way to implement strtoll
authorNick Mathewson <nickm@torproject.org>
Mon, 24 Jan 2011 23:22:32 +0000 (18:22 -0500)
committerNick Mathewson <nickm@torproject.org>
Mon, 24 Jan 2011 23:22:32 +0000 (18:22 -0500)
evutil.c

index 1d2c0fc921fd1ba7ffb8531feb70d3b834a8e061..227cc5b4549f3414f6ccb6c0b238b77c231092f4 100644 (file)
--- a/evutil.c
+++ b/evutil.c
@@ -351,6 +351,8 @@ evutil_strtoll(const char *s, char **endptr, int base)
        r = (ev_int64_t) _atoi64(s);
        while (isspace(*s))
                ++s;
+       if (*s == '-')
+               ++s;
        while (isdigit(*s))
                ++s;
        if (endptr)
@@ -358,6 +360,36 @@ evutil_strtoll(const char *s, char **endptr, int base)
        return r;
 #elif defined(WIN32)
        return (ev_int64_t) _strtoi64(s, endptr, base);
+#elif defined(_EVENT_SIZEOF_LONG_LONG) && _EVENT_SIZEOF_LONG_LONG == 8
+       long long r;
+       int n;
+       if (base != 10 && base != 16)
+               return 0;
+       if (base == 10) {
+               n = sscanf(s, "%lld", &r);
+       } else {
+               unsigned long long ru=0;
+               n = sscanf(s, "%llx", &ru);
+               if (ru > EV_INT64_MAX)
+                       return 0;
+               r = (long long) ru;
+       }
+       if (n != 1)
+               return 0;
+       while (EVUTIL_ISSPACE(*s))
+               ++s;
+       if (*s == '-')
+               ++s;
+       if (base == 10) {
+               while (EVUTIL_ISDIGIT(*s))
+                       ++s;
+       } else {
+               while (EVUTIL_ISXDIGIT(*s))
+                       ++s;
+       }
+       if (endptr)
+               *endptr = (char*) s;
+       return r;
 #else
 #error "I don't know how to parse 64-bit integers."
 #endif