From 1d33649516f2046e9fe692d42fffeeec96a2b858 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 17 Apr 2012 10:15:29 +0100 Subject: [PATCH] [libc] Allow strtoul() to interpret negative numbers Signed-off-by: Michael Brown --- src/arch/i386/core/runtime.c | 1 + src/core/misc.c | 12 ++++++++++++ src/core/strtoull.c | 12 ++++++++++++ src/include/stdlib.h | 4 ---- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/arch/i386/core/runtime.c b/src/arch/i386/core/runtime.c index efa501e6..fcfec060 100644 --- a/src/arch/i386/core/runtime.c +++ b/src/arch/i386/core/runtime.c @@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include #include #include +#include #include #include #include diff --git a/src/core/misc.c b/src/core/misc.c index 8f56e1fb..11342481 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -35,8 +35,17 @@ int inet_aton ( const char *cp, struct in_addr *inp ) { unsigned long strtoul ( const char *p, char **endp, int base ) { unsigned long ret = 0; + int negative = 0; unsigned int charval; + while ( isspace ( *p ) ) + p++; + + if ( *p == '-' ) { + negative = 1; + p++; + } + base = strtoul_base ( &p, base ); while ( 1 ) { @@ -47,6 +56,9 @@ unsigned long strtoul ( const char *p, char **endp, int base ) { p++; } + if ( negative ) + ret = -ret; + if ( endp ) *endp = ( char * ) p; diff --git a/src/core/strtoull.c b/src/core/strtoull.c index b1ceeb45..00986eef 100644 --- a/src/core/strtoull.c +++ b/src/core/strtoull.c @@ -29,8 +29,17 @@ FILE_LICENCE ( GPL2_OR_LATER ); */ unsigned long long strtoull ( const char *p, char **endp, int base ) { unsigned long long ret = 0; + int negative = 0; unsigned int charval; + while ( isspace ( *p ) ) + p++; + + if ( *p == '-' ) { + negative = 1; + p++; + } + base = strtoul_base ( &p, base ); while ( 1 ) { @@ -41,6 +50,9 @@ unsigned long long strtoull ( const char *p, char **endp, int base ) { p++; } + if ( negative ) + ret = -ret; + if ( endp ) *endp = ( char * ) p; diff --git a/src/include/stdlib.h b/src/include/stdlib.h index 19a7c8e0..3d30858f 100644 --- a/src/include/stdlib.h +++ b/src/include/stdlib.h @@ -5,7 +5,6 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include #include -#include /***************************************************************************** * @@ -18,9 +17,6 @@ static inline int strtoul_base ( const char **pp, int base ) { const char *p = *pp; - while ( isspace ( *p ) ) - p++; - if ( base == 0 ) { base = 10; if ( *p == '0' ) {