aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/stdlib/atoi.c
blob: 2d58d84854bb571a6c072d971267c621aaee3e71 (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
#include <kernel/lib/math.h>
#include <stddef.h>
#include <stdint.h>
#include <kernel/lib/string.h>

int atoi(char *str)
{
	size_t s_str = strlen(str);
	if (!s_str)
		return 0;

	uint8_t negative = 0;
	if (str[0] == '-')
		negative = 1;

	size_t i = 0;
	if (negative)
		i++;

	int ret = 0;
	for (; i < s_str; i++) {
		ret += (str[i] - '0') * pow(10, (int)((s_str - i) - 1));
	}

	if (negative)
		ret *= -1;
	return ret;
}