From 3655115e5a3c50fc06afd801100a9d75d813fd2b Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 19 Oct 2022 14:23:14 +0200 Subject: user/libc: BSD err.h, getprogname() --- src/user/lib/err.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/user/lib/err.c (limited to 'src/user/lib/err.c') diff --git a/src/user/lib/err.c b/src/user/lib/err.c new file mode 100644 index 0000000..7684b6d --- /dev/null +++ b/src/user/lib/err.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +_Noreturn void err(int ret, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + vwarn(fmt, args); + va_end(args); + exit(ret); +} + +_Noreturn void errx(int ret, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + vwarnx(fmt, args); + va_end(args); + exit(ret); +} + +void vwarn(const char *fmt, va_list args) { + fprintf(stderr, "%s: ", getprogname()); + if (fmt) { + vfprintf(stderr, fmt, args); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(errno)); +} + +void vwarnx(const char *fmt, va_list args) { + fprintf(stderr, "%s: ", getprogname()); + if (fmt) vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); +} -- cgit v1.2.3