aboutsummaryrefslogtreecommitdiffstats
path: root/src/stdlib.c
blob: ec67ff357082f5ab8bd05b9bdccafe632bdc7a50 (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
75
76
77
78
79
80
81
#include <stdlib.h>

#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>

extern char *itoa(int, char *, int);

void _exit(int code)
{
	(void)code;
	for (;;);
}

char *snprintf(char *buf, unsigned int max, const char *format, ...)
{
	va_list args;
	va_start(args, format);

	if (buf == 0 || max == 0)
		return 0;

	static char nbuf[32];
	unsigned int off = 0;
	for (unsigned int i = 0; format[i] != '\0' && off < max; i++) {
		if (format[i] == '%') {
			i++;
			nbuf[0] = '\0';
			switch (format[i]) {
			case 'd':
				itoa(va_arg(args, int), nbuf, 10);
				break;
			case 'u':
				itoa(va_arg(args, unsigned int), nbuf, 10);
				break;
			case 'x':
			case 'p':
				itoa(va_arg(args, unsigned int), nbuf, 16);
				break;
			case 'f':
				itoa((int)va_arg(args, double), nbuf, 10);
				continue;
				break;
			default:
				buf[off++] = format[i];
				nbuf[0] = '\0';
				break;
			}
			if (nbuf[0] != '\0') {
				for (unsigned int j = 0; off < max && j < 32 &&
					nbuf[j] != '\0'; off++, j++)
					buf[off] = nbuf[j];
			}
		} else {
			buf[off++] = format[i];
		}
	}


	buf[off >= max ? max - 1 : off] = '\0';
	va_end(args);
	return buf;
}

float strtof(const char *s, char **endptr)
{
	(void)s;
	(void)endptr;
	return 0.0f;
}

int atoi(const char *s)
{
	int n = 0;
	for (unsigned int i = 0; isdigit(s[i]); i++) {
		n *= 10;
		n += s[i] - '0';
	}
	return n;
}