summaryrefslogtreecommitdiff
path: root/miniany/libc-hosted.c
diff options
context:
space:
mode:
Diffstat (limited to 'miniany/libc-hosted.c')
-rw-r--r--miniany/libc-hosted.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/miniany/libc-hosted.c b/miniany/libc-hosted.c
index c6c1e69..e599b1d 100644
--- a/miniany/libc-hosted.c
+++ b/miniany/libc-hosted.c
@@ -4,6 +4,7 @@
*/
#define _XOPEN_SOURCE 600
+#include <bsd/string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
@@ -27,3 +28,47 @@ int putnl( void )
{
return puts( "" );
}
+
+/* TODO: duplicate of functions in libc-freestanding.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;
+}