summaryrefslogtreecommitdiff
path: root/miniany/libc-hosted.c
blob: e599b1db8125ccbc0b356899eb5b5abd7b810e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * include files for C library of the host. Currently only tested
 * with glibc 2.31.
 */

#define _XOPEN_SOURCE 600
#include <bsd/string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int putstring( char *s )
{
	printf( "%s", s );
	
	return 0;
}

int putint( int i )
{
	printf( "%d", i );

	return i;
}

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;
}