aboutsummaryrefslogtreecommitdiff
path: root/src/mlibc/stdlib/atoi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mlibc/stdlib/atoi.c')
-rw-r--r--src/mlibc/stdlib/atoi.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/mlibc/stdlib/atoi.c b/src/mlibc/stdlib/atoi.c
new file mode 100644
index 0000000..34f9991
--- /dev/null
+++ b/src/mlibc/stdlib/atoi.c
@@ -0,0 +1,23 @@
+#include <mlibc/math.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <mlibc/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, (s_str - i) - 1);
+ }
+
+ if (negative) ret *= -1;
+ return ret;
+} \ No newline at end of file