From ade01b532e7efd5b3331d7114cfa9f9afd716398 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Thu, 20 May 2021 11:11:24 +0200 Subject: [PATCH] stdlib: add atoi, atol and atoll --- CMakeLists.txt | 3 +++ STATUS | 2 +- include/stdlib.h | 11 +++++++++++ src/libc/stdlib/atoi.c | 6 ++++++ src/libc/stdlib/atol.c | 6 ++++++ src/libc/stdlib/atoll.c | 6 ++++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/libc/stdlib/atoi.c create mode 100644 src/libc/stdlib/atol.c create mode 100644 src/libc/stdlib/atoll.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 79f43ff..523ee83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,9 @@ set(SOURCES src/libc/stdio/printf.c # stdlib src/libc/stdlib/abs.c + src/libc/stdlib/atoi.c + src/libc/stdlib/atol.c + src/libc/stdlib/atoll.c src/libc/stdlib/calloc.c src/libc/stdlib/div.c src/libc/stdlib/labs.c diff --git a/STATUS b/STATUS index a7f1d24..3238b46 100644 --- a/STATUS +++ b/STATUS @@ -98,7 +98,7 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested 7.20 7.20.1.1 atof: TODO - 7.20.1.2 atoi, atol, atoll: TODO + 7.20.1.2 atoi, atol, atoll: DONE 7.20.1.3 strtod, strtof, strtold: TODO 7.20.1.4 strtol, strtoul, strtoll, strtoull: DONE 7.20.2 Pseudo-random sequence generation functions: TODO diff --git a/include/stdlib.h b/include/stdlib.h index db5cc60..63e8d91 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -63,6 +63,17 @@ div_t div(int __num, int __denom); ldiv_t ldiv(long int __num, long int __denom); lldiv_t lldiv(long long int __num, long long int __denom); +/* Simplified numeric conversion functions. */ + +/* ASCII to int. */ +extern int atoi(char const *__ptr); + +/* ASCII to long int. */ +extern long int atol(char const *__ptr); + +/* ASCII to long long int. */ +extern long long int atoll(char const *__ptr); + /* Numeric conversion functions. */ /* Parse a long int from a string. */ diff --git a/src/libc/stdlib/atoi.c b/src/libc/stdlib/atoi.c new file mode 100644 index 0000000..1fe38c8 --- /dev/null +++ b/src/libc/stdlib/atoi.c @@ -0,0 +1,6 @@ +#include + +int atoi(char const *ptr) +{ + return (int)strtol(ptr, NULL, 10); +} diff --git a/src/libc/stdlib/atol.c b/src/libc/stdlib/atol.c new file mode 100644 index 0000000..6195e5a --- /dev/null +++ b/src/libc/stdlib/atol.c @@ -0,0 +1,6 @@ +#include + +long int atol(char const *ptr) +{ + return strtol(ptr, NULL, 10); +} diff --git a/src/libc/stdlib/atoll.c b/src/libc/stdlib/atoll.c new file mode 100644 index 0000000..54816e3 --- /dev/null +++ b/src/libc/stdlib/atoll.c @@ -0,0 +1,6 @@ +#include + +long long int atoll(char const *ptr) +{ + return strtoll(ptr, NULL, 10); +}