X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibc.so_src%2Fstdlib.c;h=191f5261bd7d588e5e711670941b59fe39ccf682;hb=71b1271511ad01f042d2e51673dd57cce1b0fd9c;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..191f5261 100644 --- a/Usermode/Libraries/libc.so_src/stdlib.c +++ b/Usermode/Libraries/libc.so_src/stdlib.c @@ -13,24 +13,67 @@ #define _stdout 1 #define _stdin 0 -// === IMPORTS === -extern int fprintfv(FILE *fp, const char *format, va_list args); +extern void *_crt0_exit_handler; // === PROTOTYPES === EXPORT int atoi(const char *str); EXPORT void exit(int status); +// === GLOBALS === +void (*g_stdlib_exithandler)(void); + // === CODE === +void atexit(void (*__func)(void)) +{ + g_stdlib_exithandler = __func; + // TODO: Replace with meta-function to allow multiple atexit() handlers + _crt0_exit_handler = __func; +} + /** * \fn EXPORT void exit(int status) * \brief Exit */ EXPORT void exit(int status) { + if( g_stdlib_exithandler ) + g_stdlib_exithandler(); _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 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) +{ + int i, j, min; + // With 0 items, there's nothing to do and with 1 its already sorted + if(nmemb == 0 || nmemb == 1) return; + + // 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; + } + } + 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); + } + } +} + +/** + * \fn EXPORT int atoi(const char *str) + * \brief Convert a string to an integer */ EXPORT int atoi(const char *str) {