libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-10-25 23:46:40
Commit5d1e02b66752dbeb487fe7ab960e583bb4f77508
Parent61eea26beeb3172fc96f283518773fbcb423a6a9

error: Add error

Diffstat

A src/lib/error.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/lib/error.h | 43 +++++++++++++++++++++++++++++++++++++++++++

2 files changed, 101 insertions, 0 deletions

diff --git a/src/lib/error.c b/src/lib/error.c
new file mode 100644
index 0000000..6b17188
--- /dev/null
+++ b/src/lib/error.c
@@ -0,0 +1,58 @@
+// error.c, version 1.0.1
+// Error source file for OMKOV lib
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+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:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *A0 = NULL;
+
+/* Prints an error message and exits. */
+_Noreturn void error(int status, const char *format, ...) {
+	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
+	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
+	fputc('\n', stderr); exit(status);
+}
+
+/* Prints a warning message. */
+void warn(const char *format, ...) {
+	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
+	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
+	fputc('\n', stderr); return;
+}
+
+/* Shorthand for strerror(errno). */
+char *serr(void) { return strerror(errno); }
diff --git a/src/lib/error.h b/src/lib/error.h
new file mode 100644
index 0000000..d0d4585
--- /dev/null
+++ b/src/lib/error.h
@@ -0,0 +1,43 @@
+// error.h, version 1.0.1
+// Error header file for OMKOV lib
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+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:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#ifndef OMKOV_LIB_ERROR_H_38W06M3W
+#define OMKOV_LIB_ERROR_H_38W06M3W
+
+extern char *A0;
+
+extern _Noreturn void error(int status, const char *format, ...);
+extern void warn(const char *format, ...);
+
+extern char *serr(void);
+
+#endif // OMKOV_LIB_ERROR_H_38W06M3W