diff options
author | dzwdz | 2022-10-19 14:23:14 +0200 |
---|---|---|
committer | dzwdz | 2022-10-19 14:23:14 +0200 |
commit | 3655115e5a3c50fc06afd801100a9d75d813fd2b (patch) | |
tree | c9d4f39608d19d0a4b0b886d70d9b86463a59d60 /src/user/lib/err.c | |
parent | 753d43d349bfde6b83cff650c9354c4a0442a3ae (diff) |
user/libc: BSD err.h, getprogname()
Diffstat (limited to 'src/user/lib/err.c')
-rw-r--r-- | src/user/lib/err.c | 36 |
1 files changed, 36 insertions, 0 deletions
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 <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +_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"); +} |