summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-22 17:32:16 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-22 17:32:16 -0400
commit088672836ad3774c7ad89e2a4956531e063b6771 (patch)
treeb0d04534386652b27c669a2e00ca1009968f3f06
initial commit
-rw-r--r--err.h121
1 files changed, 121 insertions, 0 deletions
diff --git a/err.h b/err.h
new file mode 100644
index 0000000..1119e0d
--- /dev/null
+++ b/err.h
@@ -0,0 +1,121 @@
+/*
+Copyright (c) 2022 Jakob Kaivo <jkk@jkk.org>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef ERR_H
+#define ERR_H
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static inline void vwarnc(int code, const char *fmt, va_list args);
+{
+ fprintf(stderr, "%s: ", getprogname());
+ if (fmt) {
+ vfprintf(stderr, fmt, args);
+ if (code) {
+ fprintf(stderr, ": ");
+ }
+ }
+ if (code) {
+ fprintf(stderr, "%s", strerror(code));
+ }
+ fprintf(stderr, "\n");
+}
+
+static inline void vwarn(const char *fmt, va_list args)
+{
+ vwarnc(errno, fmt, args);
+}
+
+static inline void vwarnx(const char *fmt, va_list args)
+{
+ vwarnc(0, fmt, args);
+}
+
+static inline void verrc(int eval, int code, const char *fmt, va_list args)
+{
+ vwarnc(code, fmt, args);
+ exit(eval);
+}
+
+static inline void verr(int eval, const char *fmt, va_list args)
+{
+ verrc(eval, errno, fmt, args);
+}
+
+static inline void verrx(int eval, const char *fmt, va_list args)
+{
+ verrc(eval, 0, fmt, args);
+}
+
+static inline void err(int eval, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verr(eval, fmt, ap);
+ va_end();
+}
+
+static inline void errc(int eval, int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrc(eval, fmt, ap);
+ va_end();
+}
+
+static inline void errx(int eval, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrx(eval, fmt, ap);
+ va_end();
+}
+
+static inline void warn(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(eval, fmt, ap);
+ va_end();
+}
+
+static inline void warnc(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnc(eval, fmt, ap);
+ va_end();
+}
+
+static inline void warnx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(eval, fmt, ap);
+ va_end();
+}
+
+#endif