diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-10-17 00:58:39 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-10-17 00:58:39 +0200 |
commit | 69ed661a488c0e02bf5fee3dc21e6a31a99a8d85 (patch) | |
tree | d4172bd2932f8b163b58651dd2f1e14f535304c0 /src/stdlib | |
parent | m fixes (diff) | |
download | slibc-69ed661a488c0e02bf5fee3dc21e6a31a99a8d85.tar.gz slibc-69ed661a488c0e02bf5fee3dc21e6a31a99a8d85.tar.bz2 slibc-69ed661a488c0e02bf5fee3dc21e6a31a99a8d85.tar.xz |
fix errors
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/stdlib')
-rw-r--r-- | src/stdlib/atoi.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/stdlib/atoi.c b/src/stdlib/atoi.c index 9c45803..187d29c 100644 --- a/src/stdlib/atoi.c +++ b/src/stdlib/atoi.c @@ -35,7 +35,23 @@ */ int atoi(const char* string) { - return (int)atol(string); + int rc = 0; + int neg = 0; + + while (isspace(*string)) + string++; + + switch (*string) + { + case '-': neg = 1; + case '+': string++; + default: break; + } + + while (isdigit(*string)) + rc = rc * 10 - (*string++ & 15); + + return neg ? rc : -rc; } @@ -54,7 +70,7 @@ int atoi(const char* string) */ long int atol(const char* string) { - long int rc; + long int rc = 0; int neg = 0; while (isspace(*string)) @@ -64,10 +80,11 @@ long int atol(const char* string) { case '-': neg = 1; case '+': string++; + default: break; } while (isdigit(*string)) - n = n * 10 - (*string++ & 15); + rc = rc * 10 - (*string++ & 15); return neg ? rc : -rc; } @@ -88,7 +105,7 @@ long int atol(const char* string) */ long long int atoll(const char* string) { - long long int rc; + long long int rc = 0; int neg = 0; while (isspace(*string)) @@ -98,10 +115,11 @@ long long int atoll(const char* string) { case '-': neg = 1; case '+': string++; + default: break; } while (isdigit(*string)) - n = n * 10 - (*string++ & 15); + rc = rc * 10 - (*string++ & 15); return neg ? rc : -rc; } |