diff options
Diffstat (limited to 'miniany/libc-freestanding.c')
-rw-r--r-- | miniany/libc-freestanding.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/miniany/libc-freestanding.c b/miniany/libc-freestanding.c index b4e48f8..8b1a378 100644 --- a/miniany/libc-freestanding.c +++ b/miniany/libc-freestanding.c @@ -6,6 +6,8 @@ * */ +#include <stddef.h> + int strlen( char *s ) { char *p; @@ -139,6 +141,72 @@ int putchar( int c ) return c; } +static void strreverse( char *s ) +{ + char *end = s + strlen( s ) - 1; + + while( s < end ) { + *s ^= *end; + *end ^= *s; + *s ^= *end; + s++; + end--; + } +} + +char *itoa( int v, char *s, int base ) +{ + static char digit[] = "0123456789ABCDEF"; + int sign = 0; + char *p = s; + + if( base < 2 || base > 16 ) { + return NULL; + } + + if( v < 0 ) { + v = -v; + sign = 1; + } + + do { + *p++ = digit[v % base]; + } while( ( v /= base ) > 0 ); + + if( sign ) { + *p++ = '-'; + } + *p = '\0'; + + strreverse( s ); + + return s; +} + +int putint( int i ) +{ + char buf[17]; + + itoa( i, buf, 10 ); + print_string( buf ); + + return i; +} + +int putstring( char *s ) +{ + print_string( s ); + + return 0; +} + +int putnl( ) +{ + putchar( '\n' ); + + return 0; +} + void exit( int status ) { syscall1( SYSCALL_EXIT, status ); |