From ee7558a9b7900047033644241f0d7e6f62813e8c Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 12:42:25 +0200 Subject: [PATCH 1/2] Add a perror() function --- libs/libc/include/stdio.h | 1 + libs/libc/src/stdio.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index f2528129..adea55f3 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -35,6 +35,7 @@ extern "C" int vsprintf(char*, const char*, va_list); int vsnprintf(char*, size_t, const char*, va_list); int puts(const char*); + void perror(const char*); #ifdef __cplusplus } diff --git a/libs/libc/src/stdio.cpp b/libs/libc/src/stdio.cpp index eecf7621..a59357ad 100644 --- a/libs/libc/src/stdio.cpp +++ b/libs/libc/src/stdio.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -78,4 +79,9 @@ extern "C" va_end(ap); return written; } + void perror(const char* s) // FIXME: Print to stderr, whenever we have an stderr. + { + if (s && *s) { printf("%s: ", s); } + printf("%s\n", strerror(errno)); + } } \ No newline at end of file From e76d90364210d66a54b6e62450a8060fb0e84197 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Oct 2022 12:42:46 +0200 Subject: [PATCH 2/2] apps: make memeater use perror --- apps/src/memeater.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/src/memeater.c b/apps/src/memeater.c index dcd6755c..a1c26c71 100644 --- a/apps/src/memeater.c +++ b/apps/src/memeater.c @@ -15,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, %s)\n", errno, strerror(errno)); + perror("malloc"); printf("Press any key to restart.\n"); } \ No newline at end of file