From b78cec4f6d35cdad3b63896e86fc0a34176e221c Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 23 May 2021 17:44:37 +0200 Subject: [PATCH] string: add and test strerror (DONE) --- CMakeLists.txt | 1 + STATUS | 2 +- src/libc/string/strerror.c | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/libc/string/strerror.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ffd8e2..cb2fc6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,6 +141,7 @@ set(SOURCES src/libc/string/strcmp.c src/libc/string/strcpy.c src/libc/string/strdup.c + src/libc/string/strerror.c src/libc/string/strlen.c src/libc/string/strncat.c src/libc/string/strncmp.c diff --git a/STATUS b/STATUS index 7fb0a2c..92d272c 100644 --- a/STATUS +++ b/STATUS @@ -129,7 +129,7 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested ! 7.21.5.7 strstr: TODO ! 7.21.5.8 strtok: TODO 7.21.6.1 memset: DONE -! 7.21.6.2 strerror: TODO + 7.21.6.2 strerror: DONE 7.21.6.3 strlen: DONE Extensions: ! - strnlen: TODO diff --git a/src/libc/string/strerror.c b/src/libc/string/strerror.c new file mode 100644 index 0000000..0a91180 --- /dev/null +++ b/src/libc/string/strerror.c @@ -0,0 +1,15 @@ +#include +#include + +static char *errno_strings [] = { + [0] = "Success", + [EDOM] = "Numerical argument out of domain", + [EILSEQ] = "Invalid or incomplete multibyte or wide character", + [ERANGE] = "Numerical result out of range", +}; + +char *strerror(int e) +{ + int count = sizeof errno_strings / sizeof errno_strings[0]; + return (e >= 0 && e < count) ? errno_strings[e] : ""; +}