summaryrefslogtreecommitdiff
path: root/src/user/lib/std/stdio.c
blob: 3b24da1dd7282b60dc1649691ce936293f7ffbce (plain) (blame)
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
#include <stdlib.h>

void printk_int(int number) {
	if (number == 0) {
		printk("0");
		return;
	}
	int negative = 0;
	if (number < 0) {
		negative = 1;
		number = 0 - number;
	}
	int order = 0, temp = number, i;
	char numbers[] = "0123456789";
	while (temp > 0) {
		order++;
		temp /= 10;
	}

	char *s, *r;
	s = malloc(order + (negative ? 2 : 1));
	if (negative) {
		s[0] = '-';
		r = s + 1;
	} else {
		r = s;
	}

	for (i = order; i > 0; i--) {
		r[i - 1] = numbers[number % 10];
		number /= 10;
	}
	r[order] = 0;
	printk(s);
	free(s);
}

void printk_hex(unsigned v) {
	char s[11] = {'0', 'x', 0};

	int i;

	char hexdigits[] = "0123456789ABCDEF";

	for (i = 0; i < 8; i++) {
		s[i + 2] = (hexdigits[v >> 28]);
		v = v << 4;
	}
	s[11] = 0;
	printk(s);
}