From 28bde216c4fe9df6d4ae46ef46db0d3ad808ee5a Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 13 Jan 2023 21:08:10 +0100 Subject: [PATCH] libc: Implement perror() --- libc/include/stdio.h | 3 +++ libc/src/stdio.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 3726a8ca..a5a1d5df 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -58,6 +58,9 @@ extern "C" /* Output a message to the console. */ int console_write(const char* msg, size_t size); + /* Write an error message to standard error. */ + void perror(const char* s); + #ifdef __cplusplus } #endif diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 8881c675..276518a8 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -70,6 +70,14 @@ extern "C" return 0; } + + void perror(const char* s) + { + // FIXME: Output to stderr when available. + int err = errno; + if (s && *s) printf("%s: ", s); + printf("%s\n", strerror(err)); + } } void debug_log_impl(const char* format, va_list ap)