2 * AcessOS Basic C Library
\r
6 * \todo Move half of these to stdio
\r
8 #include <acess/sys.h>
\r
16 extern void *_crt0_exit_handler;
\r
18 // === PROTOTYPES ===
\r
19 EXPORT int atoi(const char *str);
\r
20 EXPORT void exit(int status);
\r
23 void (*g_stdlib_exithandler)(void);
\r
26 void atexit(void (*__func)(void))
\r
28 g_stdlib_exithandler = __func;
\r
29 // TODO: Replace with meta-function to allow multiple atexit() handlers
\r
30 _crt0_exit_handler = __func;
\r
34 * \fn EXPORT void exit(int status)
\r
37 EXPORT void exit(int status)
\r
39 if( g_stdlib_exithandler )
\r
40 g_stdlib_exithandler();
\r
45 * \fn EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
\r
46 * \brief Sort an array
\r
47 * \note Uses a selection sort
\r
49 EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
\r
52 // With 0 items, there's nothing to do and with 1 its already sorted
\r
53 if(nmemb == 0 || nmemb == 1) return;
\r
56 for( i = 0; i < (nmemb-1); i++ )
\r
59 for( j = i+1; j < nmemb; j++ )
\r
61 if(compar(base+size*j, base + size*min) < 0) {
\r
67 memcpy(swap, base+size*i, size);
\r
68 memcpy(base+size*i, base+size*min, size);
\r
69 memcpy(base+size*i, swap, size);
\r
75 * \fn EXPORT int atoi(const char *str)
\r
76 * \brief Convert a string to an integer
\r
78 EXPORT int atoi(const char *str)
\r
86 while(*str == ' ' || *str == '\t') str++;
\r
88 // Check for negative
\r
99 while( ('0' <= *str && *str <= '9')
\r
100 || ('A' <= *str && *str <= 'F' )
\r
101 || ('a' <= *str && *str <= 'f' )
\r
107 } else if (*str <= 'F') {
\r
108 ret += *str - 'A' + 10;
\r
110 ret += *str - 'a' + 10;
\r
116 while( '0' <= *str && *str <= '7' )
\r
125 while( '0' <= *str && *str <= '9' )
\r
133 // Negate if needed
\r
134 if(neg) ret = -ret;
\r