Usermode - Moved include files to each library's directory
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / stdlib.c
index 850d269..191f526 100644 (file)
 #define _stdout        1\r
 #define _stdin 0\r
 \r
+extern void    *_crt0_exit_handler;\r
+\r
 // === PROTOTYPES ===\r
 EXPORT int     atoi(const char *str);\r
 EXPORT void    exit(int status);\r
 \r
+// === GLOBALS ===\r
+void   (*g_stdlib_exithandler)(void);\r
+\r
 // === CODE ===\r
+void atexit(void (*__func)(void))\r
+{\r
+       g_stdlib_exithandler = __func;\r
+       // TODO: Replace with meta-function to allow multiple atexit() handlers\r
+       _crt0_exit_handler = __func;    \r
+}\r
+\r
 /**\r
  * \fn EXPORT void exit(int status)\r
  * \brief Exit\r
  */\r
 EXPORT void exit(int status)\r
 {\r
+       if( g_stdlib_exithandler )\r
+               g_stdlib_exithandler();\r
        _exit(status);\r
 }\r
 \r
@@ -35,6 +49,10 @@ EXPORT void exit(int status)
 EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))\r
 {\r
         int    i, j, min;\r
+       // With 0 items, there's nothing to do and with 1 its already sorted\r
+       if(nmemb == 0 || nmemb == 1)    return;\r
+       \r
+       // SORT!\r
        for( i = 0; i < (nmemb-1); i++ )\r
        {\r
                min = i;\r

UCC git Repository :: git.ucc.asn.au