/* * libutil - C utility functions * * Written in 2020 by Lucas * * To the extent possible under law, the author(s) have dedicated all * copyright and related and neighboring rights to this software to the * public domain worldwide. This software is distributed without any * warranty. * * You should have received a copy of the CC0 Public Domain Dedication * along with this software. If not, see * . */ /* * Inspired by OpenBSD's `err.h' header, found at * http://man.openbsd.org/err.3 * http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/gen/verr.c */ #include #include #include #include #include #include "err.h" extern const char *__progname; void err(int eval, const char *fmt, ...) { va_list ap; int saved_errno = errno; fprintf(stderr, "%s: ", __progname); if (fmt != NULL) { va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, ": "); } fprintf(stderr, "%s\n", strerror(saved_errno)); exit(eval); } void errx(int eval, const char *fmt, ...) { va_list ap; fprintf(stderr, "%s: ", __progname); if (fmt != NULL) { va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); } fprintf(stderr, "\n"); exit(eval); } void warn(const char *fmt, ...) { va_list ap; int saved_errno = errno; fprintf(stderr, "%s: ", __progname); if (fmt != NULL) { va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, ": "); } fprintf(stderr, "%s\n", strerror(saved_errno)); } void warnx(const char *fmt, ...) { va_list ap; fprintf(stderr, "%s: ", __progname); if (fmt != NULL) { va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); } fprintf(stderr, "\n"); }