From 166ddd4673dc43e28659516b3f521ba357b9ab27 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Wed, 28 Nov 2012 19:05:41 +0800 Subject: [PATCH 1/1] Usermode/libc - Cleanup of source --- Usermode/Libraries/libc.so_src/Makefile | 4 +- .../libc.so_src/{fileIO.c => stdio.c} | 0 Usermode/Libraries/libc.so_src/stdlib.c | 124 ---------------- Usermode/Libraries/libc.so_src/strtoi.c | 135 ++++++++++++++++++ Usermode/Libraries/libc.so_src/stub.c | 2 +- 5 files changed, 138 insertions(+), 127 deletions(-) rename Usermode/Libraries/libc.so_src/{fileIO.c => stdio.c} (100%) create mode 100644 Usermode/Libraries/libc.so_src/strtoi.c diff --git a/Usermode/Libraries/libc.so_src/Makefile b/Usermode/Libraries/libc.so_src/Makefile index d2c3190a..b890d482 100644 --- a/Usermode/Libraries/libc.so_src/Makefile +++ b/Usermode/Libraries/libc.so_src/Makefile @@ -10,8 +10,8 @@ LDFLAGS += -soname libc.so -Map map.txt INCFILES := stdio.h stdlib.h -OBJ = stub.o heap.o stdlib.o env.o fileIO.o string.o select.o rand.o -OBJ += perror.o scanf.o signals.o +OBJ = stub.o heap.o stdlib.o env.o stdio.o string.o select.o rand.o +OBJ += perror.o scanf.o signals.o strtoi.o OBJ += arch/$(ARCHDIR).ao # signals.o DEPFILES := $(OBJ:%.o=%.d) diff --git a/Usermode/Libraries/libc.so_src/fileIO.c b/Usermode/Libraries/libc.so_src/stdio.c similarity index 100% rename from Usermode/Libraries/libc.so_src/fileIO.c rename to Usermode/Libraries/libc.so_src/stdio.c diff --git a/Usermode/Libraries/libc.so_src/stdlib.c b/Usermode/Libraries/libc.so_src/stdlib.c index e15f49b4..6e81c654 100644 --- a/Usermode/Libraries/libc.so_src/stdlib.c +++ b/Usermode/Libraries/libc.so_src/stdlib.c @@ -5,14 +5,8 @@ #include #include #include -#include -#include -#include #include "lib.h" -#define _stdout 1 -#define _stdin 0 - extern void *_crt0_exit_handler; // === PROTOTYPES === @@ -71,124 +65,6 @@ EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void } } -EXPORT unsigned long long strtoull(const char *str, char **end, int base) -{ - long long ret = 0; - - if( !str || base < 0 || base > 36 || base == 1 ) { - if(end) - *end = (char*)str; - errno = EINVAL; - return 0; - } - - while( isspace(*str) ) - str++; - - if( base == 0 || base == 16 ) { - if( *str == '0' && str[1] == 'x' ) { - str += 2; - base = 16; - } - } - - if( base == 0 && *str == '0' ) { - str ++; - base = 8; - } - - if( base == 0 ) - base = 10; - - while( *str ) - { - int next = -1; - if( base <= 10 ) { - if( '0' <= *str && *str <= '0'+base-1 ) - next = *str - '0'; - } - else { - if( '0' <= *str && *str <= '9' ) - next = *str - '0'; - if( 'A' <= *str && *str <= 'A'+base-10-1 ) - next = *str - 'A'; - if( 'a' <= *str && *str <= 'a'+base-10-1 ) - next = *str - 'a'; - } - if( next < 0 ) - break; - ret *= base; - ret += next; - str ++; - } - - if(end) - *end = (char*)str; - return ret; -} - -EXPORT unsigned long strtoul(const char *ptr, char **end, int base) -{ - unsigned long long tmp = strtoull(ptr, end, base); - - if( tmp > ULONG_MAX ) { - errno = ERANGE; - return ULONG_MAX; - } - - return tmp; -} - -EXPORT long long strtoll(const char *str, char **end, int base) -{ - int neg = 0; - unsigned long long ret; - - if( !str ) { - errno = EINVAL; - return 0; - } - - while( isspace(*str) ) - str++; - - // Check for negative (or positive) sign - if(*str == '-' || *str == '+') { - neg = (*str == '-'); - str++; - } - - ret = strtoull(str, end, base); - - if( neg ) - return -ret; - else - return ret; -} - -EXPORT long strtol(const char *str, char **end, int base) -{ - long long tmp = strtoll(str, end, base); - if( tmp > LONG_MAX || tmp < LONG_MIN ) { - errno = ERANGE; - return (tmp > LONG_MAX) ? LONG_MAX : LONG_MIN; - } - return tmp; -} - -/** - * \fn EXPORT int atoi(const char *str) - * \brief Convert a string to an integer - */ -EXPORT int atoi(const char *str) -{ - long long tmp = strtoll(str, NULL, 0); - if( tmp > INT_MAX || tmp < INT_MIN ) { - errno = ERANGE; - return (tmp > INT_MAX) ? INT_MAX : INT_MIN; - } - return tmp; -} int abs(int j) { return j < 0 ? -j : j; } long int labs(long int j) { return j < 0 ? -j : j; } diff --git a/Usermode/Libraries/libc.so_src/strtoi.c b/Usermode/Libraries/libc.so_src/strtoi.c new file mode 100644 index 00000000..53756dc8 --- /dev/null +++ b/Usermode/Libraries/libc.so_src/strtoi.c @@ -0,0 +1,135 @@ +/* + * Acess2 C Library + * - By John Hodge (thePowersGang) + * + * strtoi.c + * - strto[u][l]l/atoi implimentation + */ +#include +#include +#include +#include +#include "lib.h" + +EXPORT unsigned long long strtoull(const char *str, char **end, int base) +{ + long long ret = 0; + + if( !str || base < 0 || base > 36 || base == 1 ) { + if(end) + *end = (char*)str; + errno = EINVAL; + return 0; + } + + // Trim leading spaces + while( isspace(*str) ) + str++; + + // Handle base detection for hex + if( base == 0 || base == 16 ) { + if( *str == '0' && str[1] == 'x' ) { + str += 2; + base = 16; + } + } + + // Handle base detection for octal + if( base == 0 && *str == '0' ) { + str ++; + base = 8; + } + + // Fall back on decimal when unknown + if( base == 0 ) + base = 10; + + while( *str ) + { + int next = -1; + if( base <= 10 ) { + if( '0' <= *str && *str <= '0'+base-1 ) + next = *str - '0'; + } + else { + if( '0' <= *str && *str <= '9' ) + next = *str - '0'; + if( 'A' <= *str && *str <= 'A'+base-10-1 ) + next = *str - 'A'; + if( 'a' <= *str && *str <= 'a'+base-10-1 ) + next = *str - 'a'; + } + if( next < 0 ) + break; + ret *= base; + ret += next; + str ++; + } + + if(end) + *end = (char*)str; + return ret; +} + +EXPORT unsigned long strtoul(const char *ptr, char **end, int base) +{ + unsigned long long tmp = strtoull(ptr, end, base); + + if( tmp > ULONG_MAX ) { + errno = ERANGE; + return ULONG_MAX; + } + + return tmp; +} + +EXPORT long long strtoll(const char *str, char **end, int base) +{ + int neg = 0; + unsigned long long ret; + + if( !str ) { + errno = EINVAL; + return 0; + } + + while( isspace(*str) ) + str++; + + // Check for negative (or positive) sign + if(*str == '-' || *str == '+') { + neg = (*str == '-'); + str++; + } + + ret = strtoull(str, end, base); + + if( neg ) + return -ret; + else + return ret; +} + +EXPORT long strtol(const char *str, char **end, int base) +{ + long long tmp = strtoll(str, end, base); + if( tmp > LONG_MAX || tmp < LONG_MIN ) { + errno = ERANGE; + return (tmp > LONG_MAX) ? LONG_MAX : LONG_MIN; + } + return tmp; +} + +/** + * \fn EXPORT int atoi(const char *str) + * \brief Convert a string to an integer + */ +EXPORT int atoi(const char *str) +{ + long long tmp = strtoll(str, NULL, 0); + if( tmp > INT_MAX || tmp < INT_MIN ) { + errno = ERANGE; + return (tmp > INT_MAX) ? INT_MAX : INT_MIN; + } + return tmp; +} diff --git a/Usermode/Libraries/libc.so_src/stub.c b/Usermode/Libraries/libc.so_src/stub.c index 7cde4c1b..20e796ee 100644 --- a/Usermode/Libraries/libc.so_src/stub.c +++ b/Usermode/Libraries/libc.so_src/stub.c @@ -4,7 +4,7 @@ #include "stdio_int.h" #include "lib.h" #include -#include +#include #define USE_CPUID 0 -- 2.20.1