X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibc.so_src%2Fstdlib.c;h=e05ddc2d4ed07316e1958507402cd992f91df514;hb=d8d31a4ec9a28eb8de493146ce75e8238e8e13b1;hp=f5acadbd215a5c0074c0048e0276652b01d98a05;hpb=91dd38c34820c03311738439125675d59bf9e3f1;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libc.so_src/stdlib.c b/Usermode/Libraries/libc.so_src/stdlib.c index f5acadbd..e05ddc2d 100644 --- a/Usermode/Libraries/libc.so_src/stdlib.c +++ b/Usermode/Libraries/libc.so_src/stdlib.c @@ -2,92 +2,82 @@ * AcessOS Basic C Library * stdlib.c */ -/** - * \todo Move half of these to stdio - */ #include #include #include #include "lib.h" -#define _stdout 1 -#define _stdin 0 +extern void _stdio_cleanup(void); -// === IMPORTS === -extern int fprintfv(FILE *fp, const char *format, va_list args); +#define MAX_ATEXIT_HANDLERS 64 // Standard defines >=32 // === PROTOTYPES === EXPORT int atoi(const char *str); EXPORT void exit(int status); +// === GLOBALS === +typedef void (*stdlib_exithandler_t)(void); +stdlib_exithandler_t g_stdlib_exithandlers[MAX_ATEXIT_HANDLERS]; + int g_stdlib_num_exithandlers; + // === CODE === +void _call_atexit_handlers(void) +{ + int i; + for( i = g_stdlib_num_exithandlers; i --; ) + g_stdlib_exithandlers[i](); + _stdio_cleanup(); +} + +EXPORT void atexit(void (*__func)(void)) +{ + if( g_stdlib_num_exithandlers < MAX_ATEXIT_HANDLERS ) + { + g_stdlib_exithandlers[g_stdlib_num_exithandlers++] = __func; + } +} + /** * \fn EXPORT void exit(int status) * \brief Exit */ EXPORT void exit(int status) { + _call_atexit_handlers(); _exit(status); } /** + * \fn EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) + * \brief Sort an array + * \note Uses a selection sort */ -EXPORT int atoi(const char *str) +EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) { - int neg = 0; - int ret = 0; - - // NULL Check - if(!str) return 0; + size_t i, j, min; + // With 0 items, there's nothing to do and with 1 its already sorted + if(nmemb == 0 || nmemb == 1) return; - while(*str == ' ' || *str == '\t') str++; - - // Check for negative - if(*str == '-') { - neg = 1; - str++; - } - - if(*str == '0') { - str ++; - if(*str == 'x') { - str++; - // Hex - while( ('0' <= *str && *str <= '9') - || ('A' <= *str && *str <= 'F' ) - || ('a' <= *str && *str <= 'f' ) - ) - { - ret *= 16; - if(*str <= '9') { - ret += *str - '0'; - } else if (*str <= 'F') { - ret += *str - 'A' + 10; - } else { - ret += *str - 'a' + 10; - } - str++; - } - } else { - // Octal - while( '0' <= *str && *str <= '7' ) - { - ret *= 8; - ret += *str - '0'; - str++; + // SORT! + for( i = 0; i < (nmemb-1); i++ ) + { + min = i; + for( j = i+1; j < nmemb; j++ ) + { + if(compar(base+size*j, base + size*min) < 0) { + min = j; } } - } else { - // Decimal - while( '0' <= *str && *str <= '9' ) - { - ret *= 10; - ret += *str - '0'; - str++; + if (i != min) { + char swap[size]; + memcpy(swap, base+size*i, size); + memcpy(base+size*i, base+size*min, size); + memcpy(base+size*i, swap, size); } } - - // Negate if needed - if(neg) ret = -ret; - return ret; } + + +int abs(int j) { return j < 0 ? -j : j; } +long int labs(long int j) { return j < 0 ? -j : j; } +