From 871934a0f6fc24e9751688578687a63b53c530f3 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Wed, 23 Nov 2005 22:41:47 +0000 Subject: [PATCH] add strtol --- libunix/Makefile | 2 +- libunix/strtol.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 libunix/strtol.c diff --git a/libunix/Makefile b/libunix/Makefile index 0789970..f10b7dc 100644 --- a/libunix/Makefile +++ b/libunix/Makefile @@ -12,7 +12,7 @@ LIBRARY = libunix.a SOURCES = divsi3.S modsi3.S mulsi3.S udivsi3.S umodsi3.S free.c malloc.c \ memcpy.c memset.c printf.c putchar.c puts.c read.c sprintf.c \ strcpy.c strlen.c strncpy.c vsprintf.c write.c strcmp.c \ - strncmp.c + strncmp.c strtol.c HEADERS = diff --git a/libunix/strtol.c b/libunix/strtol.c new file mode 100644 index 0000000..f563191 --- /dev/null +++ b/libunix/strtol.c @@ -0,0 +1,67 @@ +#include + +static int my_isspace(int c) +{ + return (c == ' ') || (c == '\f') || (c == '\n') || + (c == '\r') || (c == '\t') || (c == '\v'); +} + +long int __strtol_internal (const char *nptr, char **endptr, int base, int group) +{ + long result = 0; + int sign; + + while(my_isspace(*nptr)) + nptr++; + + if (*nptr == '-') + { + nptr++; + sign = -1; + } else if (*nptr == '+') + { + nptr++; + sign = 1; + } else + sign = 1; + + if (base == 0) + { + if (*nptr == '0') + { + nptr++; + if (*nptr == 'x') + { + base = 16; + nptr++; + } else + base = 8; + } + else + base = 10; + } + + while (*nptr) + { + int digit; + + if ( (*nptr >= '0') && (*nptr <= '9') ) + digit = *nptr - '0'; + else if ( (*nptr >= 'a') && (*nptr <= 'z') ) + digit = *nptr - 'a' + 10; + else if ( (*nptr >= 'A') && (*nptr <= 'Z') ) + digit = *nptr - 'A' + 10; + else + break; + + if (digit >= base) + break; + + result = (result * base) + digit; + + nptr++; + } + if (endptr) + *endptr = (char*)nptr; + return sign * result; +}