Merge branch 'master' of github.com:thepowersgang/acess2
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / stdlib.c
index 733cb91..6e81c65 100644 (file)
@@ -2,16 +2,12 @@
  * AcessOS Basic C Library\r
  * stdlib.c\r
  */\r
-/**\r
- * \todo Move half of these to stdio\r
- */\r
 #include <acess/sys.h>\r
 #include <stdlib.h>\r
 #include <stdio.h>\r
 #include "lib.h"\r
 \r
-#define _stdout        1\r
-#define _stdin 0\r
+extern void    *_crt0_exit_handler;\r
 \r
 // === PROTOTYPES ===\r
 EXPORT int     atoi(const char *str);\r
@@ -24,6 +20,8 @@ void  (*g_stdlib_exithandler)(void);
 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
@@ -32,6 +30,8 @@ void atexit(void (*__func)(void))
  */\r
 EXPORT void exit(int status)\r
 {\r
+       if( g_stdlib_exithandler )\r
+               g_stdlib_exithandler();\r
        _exit(status);\r
 }\r
 \r
@@ -42,7 +42,7 @@ EXPORT void exit(int status)
  */\r
 EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))\r
 {\r
-        int    i, j, min;\r
+       size_t  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
@@ -65,66 +65,7 @@ EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void
        }\r
 }\r
 \r
-/**\r
- * \fn EXPORT int atoi(const char *str)\r
- * \brief Convert a string to an integer\r
- */\r
-EXPORT int atoi(const char *str)\r
-{\r
-        int    neg = 0;\r
-        int    ret = 0;\r
-       \r
-       // NULL Check\r
-       if(!str)        return 0;\r
-       \r
-       while(*str == ' ' || *str == '\t')      str++;\r
-       \r
-       // Check for negative\r
-       if(*str == '-') {\r
-               neg = 1;\r
-               str++;\r
-       }\r
-       \r
-       if(*str == '0') {\r
-               str ++;\r
-               if(*str == 'x') {\r
-                       str++;\r
-                       // Hex\r
-                       while( ('0' <= *str && *str <= '9')\r
-                               || ('A' <= *str && *str <= 'F' )\r
-                               || ('a' <= *str && *str <= 'f' )\r
-                               )\r
-                       {\r
-                               ret *= 16;\r
-                               if(*str <= '9') {\r
-                                       ret += *str - '0';\r
-                               } else if (*str <= 'F') {\r
-                                       ret += *str - 'A' + 10;\r
-                               } else {\r
-                                       ret += *str - 'a' + 10;\r
-                               }\r
-                               str++;\r
-                       }\r
-               } else {\r
-                       // Octal\r
-                       while( '0' <= *str && *str <= '7' )\r
-                       {\r
-                               ret *= 8;\r
-                               ret += *str - '0';\r
-                               str++;\r
-                       }\r
-               }\r
-       } else {\r
-               // Decimal\r
-               while( '0' <= *str && *str <= '9' )\r
-               {\r
-                       ret *= 10;\r
-                       ret += *str - '0';\r
-                       str++;\r
-               }\r
-       }\r
-       \r
-       // Negate if needed\r
-       if(neg) ret = -ret;\r
-       return ret;\r
-}\r
+\r
+int abs(int j) { return j < 0 ? -j : j; }\r
+long int labs(long int j) { return j < 0 ? -j : j; }\r
+\r

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