From 8f0b6d80b2e80a59b6e6d02d94daefd963a81ebf Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 12:29:06 +0200 Subject: [PATCH 1/2] libc: Implement strerror() --- libs/libc/include/string.h | 2 ++ libs/libc/src/string.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/libs/libc/include/string.h b/libs/libc/include/string.h index d9722ff7..e4379614 100644 --- a/libs/libc/include/string.h +++ b/libs/libc/include/string.h @@ -23,6 +23,8 @@ extern "C" deprecated("strcat is unsafe and should not be used; use strncat instead") char* strcat(char*, const char*); char* strncat(char*, const char*, size_t); + char* strerror(int); + #ifdef __cplusplus } #endif diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index 16fcb8d7..b9d32192 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -108,4 +109,20 @@ extern "C" // we return "m_start" + the amount of bytes that were transfered return (void*)(((size_t)start) + i); } + +#pragma GCC push_options +#pragma GCC diagnostic ignored "-Wwrite-strings" + + char* strerror(int errnum) + { + switch (errnum) + { + case EPERM: return "Operation not permitted"; + case EINVAL: return "Invalid argument"; + case ENOMEM: return "Out of memory"; + default: return 0; + } + } + +#pragma GCC pop_options } \ No newline at end of file -- 2.34.1 From 21e8ea14868844ff424c3e6f28dca41ee28e2842 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 12:29:19 +0200 Subject: [PATCH 2/2] apps: make memeater use strerror() --- apps/src/memeater.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/src/memeater.c b/apps/src/memeater.c index 8e683637..dcd6755c 100644 --- a/apps/src/memeater.c +++ b/apps/src/memeater.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #define CHUNK 4194304 // 4 MB @@ -14,6 +15,6 @@ int main() printf("Allocating 4 MB of memory... %lx\n", (unsigned long)allocated); sleep(1); } while ((allocated = malloc(CHUNK))); - printf("Out of memory. (errno=%d)\n", errno); + printf("Out of memory. (errno=%d, %s)\n", errno, strerror(errno)); printf("Press any key to restart.\n"); } \ No newline at end of file -- 2.34.1