CeDOS - Commit 037adfa8

stdlib: Add atoi function
Celina Sophie Kalus
Wed, 06 Dec 2023 00:42:55 +0100
2 files changed, 25 insertions(+), 0 deletions(-)
A libcedos/stdlib.c

@@ -0,0 +1,23 @@

+#include "stdlib.h" + +int atoi (const char * str) { + int res = 0; + int sign = 1; + int i = 0; + + if (str[0] == '+') { + sign = 1; + i = 1; + } else if (str[0] == '-') { + sign == -1; + i = 1; + } else if (str[0] < '0' || str[0] > '9') { + return 0; + } + + while (str[i] >= '0' && str[i] <= '9') { + res = 10 * res + (str[i++] - '0'); + } + + return res; +}
M libcedos/stdlib.hlibcedos/stdlib.h

@@ -41,4 +41,6 @@ */

void free(void* ptr); +int atoi (const char * str); + #endif