summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Kacur <jkacur@redhat.com>2009-12-23 14:43:54 +0100
committerJohn Kacur <jkacur@redhat.com>2009-12-23 14:43:54 +0100
commitaee58a288f0ab71c0eafaec6b86f26b94e012af2 (patch)
tree6eeb3eb8c885bfc4c3b14f4a8a169f1cdf6e1f44
parent3115847ca0f16757a1283e58d15db5d652df28ce (diff)
downloadrt-tests-aee58a288f0ab71c0eafaec6b86f26b94e012af2.tar.gz
rt-tests: Add error routines to the library
Add error routines, similar to those found in Advanced Programming in the UNIX Environment 2nd ed. for use by all rt test programs Signed-off-by: John Kacur <jkacur@redhat.com>
-rw-r--r--src/lib/error.c49
-rw-r--r--src/lib/error.h14
2 files changed, 63 insertions, 0 deletions
diff --git a/src/lib/error.c b/src/lib/error.c
new file mode 100644
index 0000000..d5ad2aa
--- /dev/null
+++ b/src/lib/error.c
@@ -0,0 +1,49 @@
+#include "error.h"
+
+/* Print an error message, plus a message for err and exit with error err */
+void err_exit(int err, char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ err_doit(err, fmt, ap);
+ va_end(ap);
+ exit(err);
+}
+
+/* print an error message and return */
+void err_msg(char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ err_doit(0, fmt, ap);
+ va_end(ap);
+ return;
+}
+
+/* Print an error message, plus a message for err, and return */
+void err_msg_n(int err, char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ err_doit(err, fmt, ap);
+ va_end(ap);
+ return;
+}
+
+/* print an error message and quit */
+void err_quit(char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ err_doit(0, fmt, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void err_doit(int err, const char *fmt, va_list ap)
+{
+ if (err)
+ fprintf(stderr, "%s\n", strerror(err));
+ vfprintf(stderr, fmt, ap);
+ return;
+}
diff --git a/src/lib/error.h b/src/lib/error.h
new file mode 100644
index 0000000..90d6e94
--- /dev/null
+++ b/src/lib/error.h
@@ -0,0 +1,14 @@
+#ifndef __ERROR_H
+#define __ERROR_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+void err_exit(int err, char *fmt, ...);
+void err_msg(char *fmt, ...);
+void err_msg_n(int err, char *fmt, ...);
+void err_quit(char *fmt, ...);
+void err_doit(int err, const char *fmt, va_list ap);
+
+#endif /* __ERROR_H */