diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2017-06-10 21:26:24 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2017-06-10 21:26:24 +0200 |
commit | d6d1bdfefafff50b7b6d15d218c0a188570be541 (patch) | |
tree | 15ee8de727d0be5d126efda146b2879de0a72773 /src/libc/stdlib.c | |
parent | eea5bf4b859eb56c5772c58ca54937a90a10e7ee (diff) | |
download | abaos-d6d1bdfefafff50b7b6d15d218c0a188570be541.tar.gz abaos-d6d1bdfefafff50b7b6d15d218c0a188570be541.tar.bz2 |
some big renames into subdirs of aspects
updated README
removed size_t in sys/types.h and sys/types.h itself, size_t is in stddef.h
Diffstat (limited to 'src/libc/stdlib.c')
-rw-r--r-- | src/libc/stdlib.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c new file mode 100644 index 0000000..46bdce8 --- /dev/null +++ b/src/libc/stdlib.c @@ -0,0 +1,48 @@ +#include <stdbool.h> +#include "string.h" +#include "stdlib.h" +#include "stddef.h" + +static void strreverse( char *s ) +{ + char *end = s + strlen( s ) - 1; + + while( s < end ) { + // XOR swap; + *s ^= *end; + *end ^= *s; + *s ^= *end; + s++; + end--; + } +} + +char *itoa( int v, char *s, int base ) +{ + static char digit[] = "0123456789ABCDEF"; + bool sign = false; + char *p = s; + + if( base < 2 || base > 16 ) { + return NULL; + } + + if( v < 0 ) { + v = -v; + sign = true; + } + + do { + *p++ = digit[v % base]; + } while( ( v /= base ) > 0 ); + + if( sign ) { + *p++ = '-'; + } + *p = '\0'; + + strreverse( s ); + + return s; +} + |