diff --git a/src/kernel/libroot/posix/stdlib/atoi.c b/src/kernel/libroot/posix/stdlib/atoi.c index 038c4c08fc..66eac93af0 100644 --- a/src/kernel/libroot/posix/stdlib/atoi.c +++ b/src/kernel/libroot/posix/stdlib/atoi.c @@ -1,86 +1,54 @@ -/* -** Copyright 2001, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ +/* + * Copyright (c) 2002, OpenBeOS Project. + * All rights reserved. + * Distributed under the terms of the OpenBeOS license. + * + * + * atoi.c: + * implements the standard C library functions: + * atoi, atoui, atol, atoul, atoll + * (these are all just wrappers around calls to the strto[u]l[l] functions) + * + * + * Author(s): + * Daniel Reinhold (danielre@users.sf.net) + * + */ + #include -#include -static int hexval(char c) + +int +atoi(const char *num) { - if (c >= '0' && c <= '9') - return c - '0'; - else if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - else if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - - return 0; + return (int) strtol(num, NULL, 10); } -int atoi(const char *num) -{ - int value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - return value; +unsigned int +atoui(const char *num) +{ + return (unsigned int) strtoul(num, NULL, 10); } -unsigned int atoui(const char *num) -{ - int value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - return value; +long +atol(const char *num) +{ + return strtol(num, NULL, 10); } -long atol(const char *num) -{ - int value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - return value; +unsigned long +atoul(const char *num) +{ + return strtoul(num, NULL, 10); } -unsigned long atoul(const char *num) -{ - int value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - return value; +long long int +atoll(const char *num) +{ + return strtoll(num, NULL, 10); } +