From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/options/glibc/generic/err.cpp | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/mlibc/options/glibc/generic/err.cpp (limited to 'lib/mlibc/options/glibc/generic/err.cpp') diff --git a/lib/mlibc/options/glibc/generic/err.cpp b/lib/mlibc/options/glibc/generic/err.cpp new file mode 100644 index 0000000..042c1d8 --- /dev/null +++ b/lib/mlibc/options/glibc/generic/err.cpp @@ -0,0 +1,64 @@ +#include + +#include +#include +#include + +// va_list + +void vwarn(const char *fmt, va_list params) { + fprintf(stderr, "%s: ", program_invocation_short_name); + if (fmt) { + vfprintf(stderr, fmt, params); + fwrite(": ", 1, 2, stderr); + } + perror(NULL); +} + +void vwarnx(const char *fmt, va_list params) { + fprintf(stderr, "%s: ", program_invocation_short_name); + if (fmt) { + vfprintf(stderr, fmt, params); + } + putc('\n', stderr); +} + +__attribute__((__noreturn__)) void verr(int status, const char *fmt, va_list params) { + vwarn(fmt, params); + exit(status); +} + +__attribute__((__noreturn__)) void verrx(int status, const char *fmt, va_list params) { + vwarnx(fmt, params); + exit(status); +} + +// variadic + +void warn(const char *fmt, ...) { + va_list params; + va_start(params, fmt); + vwarn(fmt, params); + va_end(params); +} + +void warnx(const char *fmt, ...) { + va_list params; + va_start(params, fmt); + vwarnx(fmt, params); + va_end(params); +} + +__attribute__((__noreturn__)) void err(int status, const char *fmt, ...) { + va_list params; + va_start(params, fmt); + verr(status, fmt, params); + va_end(params); +} + +__attribute__((__noreturn__)) void errx(int status, const char *fmt, ...) { + va_list params; + va_start(params, fmt); + verrx(status, fmt, params); + va_end(params); +} -- cgit v1.2.3