summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-01-14 23:24:00 -0600
committerSamuel Holland <samuel@sholland.org>2018-01-15 00:02:54 -0600
commitb087b07c9649b7e4398093720ebcf96f205c2c49 (patch)
treeacebc421da0f1dcf3325178d6569bfd57d23abec
parent900ab571ba496f73b1e66204f8c66560afda29e6 (diff)
downloadgcompat-b087b07c9649b7e4398093720ebcf96f205c2c49.tar.gz
gcompat-b087b07c9649b7e4398093720ebcf96f205c2c49.tar.bz2
gcompat-b087b07c9649b7e4398093720ebcf96f205c2c49.tar.xz
gcompat-b087b07c9649b7e4398093720ebcf96f205c2c49.zip
error: Implement the error function
This follows the exceptionally-detailed functional description in the manual page. Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r--Makefile1
-rw-r--r--libgcompat/error.c27
2 files changed, 28 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index b5fda64..2ff1495 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@ LIBGCOMPAT_INCLUDE = \
libgcompat/alias.h
LIBGCOMPAT_SRC = \
libgcompat/dlfcn.c \
+ libgcompat/error.c \
libgcompat/execinfo.c \
libgcompat/gnulib.c \
libgcompat/grp.c \
diff --git a/libgcompat/error.c b/libgcompat/error.c
new file mode 100644
index 0000000..c759b19
--- /dev/null
+++ b/libgcompat/error.c
@@ -0,0 +1,27 @@
+#define _GNU_SOURCE /* program_invocation_name */
+#include <errno.h> /* program_invocation_name */
+#include <stdarg.h> /* va_list, va_start, va_end */
+#include <stdio.h> /* fflush, fputc, fputs, stderr, stdout, vfprintf */
+#include <string.h> /* strerror */
+
+/**
+ * Print an error message.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib-error-n.html
+ */
+void error(int status, int errnum, const char *format, ...)
+{
+ va_list ap;
+
+ fflush(stdout);
+ fputs(program_invocation_name, stderr);
+ fputs(": ", stderr);
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+ if (errnum != 0) {
+ fputs(": ", stderr);
+ fputs(strerror(errnum), stderr);
+ fputc('\n', stderr);
+ }
+}