aboutsummaryrefslogtreecommitdiff
path: root/lib/errors
diff options
context:
space:
mode:
Diffstat (limited to 'lib/errors')
-rw-r--r--lib/errors/cerr.c14
-rw-r--r--lib/errors/cerrx.c14
-rw-r--r--lib/errors/err.c14
-rw-r--r--lib/errors/errx.c14
-rw-r--r--lib/errors/setprogname.c12
-rw-r--r--lib/errors/vwarn.c15
-rw-r--r--lib/errors/vwarnx.c12
-rw-r--r--lib/errors/warn.c12
-rw-r--r--lib/errors/warnx.c12
9 files changed, 119 insertions, 0 deletions
diff --git a/lib/errors/cerr.c b/lib/errors/cerr.c
new file mode 100644
index 0000000..bf5a98d
--- /dev/null
+++ b/lib/errors/cerr.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+cerr(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+ exit(code);
+}
diff --git a/lib/errors/cerrx.c b/lib/errors/cerrx.c
new file mode 100644
index 0000000..b614f47
--- /dev/null
+++ b/lib/errors/cerrx.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+cerrx(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+ exit(code);
+}
diff --git a/lib/errors/err.c b/lib/errors/err.c
new file mode 100644
index 0000000..906e661
--- /dev/null
+++ b/lib/errors/err.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+err(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
diff --git a/lib/errors/errx.c b/lib/errors/errx.c
new file mode 100644
index 0000000..1fd55af
--- /dev/null
+++ b/lib/errors/errx.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+errx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
diff --git a/lib/errors/setprogname.c b/lib/errors/setprogname.c
new file mode 100644
index 0000000..ab2de3e
--- /dev/null
+++ b/lib/errors/setprogname.c
@@ -0,0 +1,12 @@
+#include <string.h>
+
+#include "errors.h"
+
+const char *__mlib_errors_progname;
+
+void
+setprogname(const char *s)
+{
+ const char *p = strrchr(s, '/');
+ __mlib_errors_progname = p ? p + 1 : s;
+}
diff --git a/lib/errors/vwarn.c b/lib/errors/vwarn.c
new file mode 100644
index 0000000..bc6bd16
--- /dev/null
+++ b/lib/errors/vwarn.c
@@ -0,0 +1,15 @@
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "errors.h"
+
+void
+vwarn(const char *fmt, va_list ap)
+{
+ int save = errno;
+ fprintf(stderr, "%s: ", progname());
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, ": %s\n", strerror(save));
+}
diff --git a/lib/errors/vwarnx.c b/lib/errors/vwarnx.c
new file mode 100644
index 0000000..72d8fa7
--- /dev/null
+++ b/lib/errors/vwarnx.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "errors.h"
+
+void
+vwarnx(const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: ", progname());
+ vfprintf(stderr, fmt, ap);
+ fputc('\n', stderr);
+}
diff --git a/lib/errors/warn.c b/lib/errors/warn.c
new file mode 100644
index 0000000..78f3e36
--- /dev/null
+++ b/lib/errors/warn.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+
+#include "errors.h"
+
+void
+warn(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+}
diff --git a/lib/errors/warnx.c b/lib/errors/warnx.c
new file mode 100644
index 0000000..ea50299
--- /dev/null
+++ b/lib/errors/warnx.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+
+#include "errors.h"
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+}