summaryrefslogtreecommitdiff
path: root/openbsd-compat/err.h
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/err.h')
-rw-r--r--openbsd-compat/err.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/openbsd-compat/err.h b/openbsd-compat/err.h
new file mode 100644
index 0000000..394c7bb
--- /dev/null
+++ b/openbsd-compat/err.h
@@ -0,0 +1,85 @@
1/*
2 * Public domain
3 * err.h compatibility shim
4 */
5
6#ifndef _COMPAT_ERR_H
7#define _COMPAT_ERR_H
8
9#if !defined(HAVE_ERR_H)
10
11#include <errno.h>
12#include <stdarg.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16
17#if defined(_MSC_VER)
18__declspec(noreturn)
19#else
20__attribute__((noreturn))
21#endif
22static inline void
23err(int eval, const char *fmt, ...)
24{
25 int sverrno = errno;
26 va_list ap;
27
28 va_start(ap, fmt);
29 if (fmt != NULL) {
30 vfprintf(stderr, fmt, ap);
31 fprintf(stderr, ": ");
32 }
33 va_end(ap);
34 fprintf(stderr, "%s\n", strerror(sverrno));
35 exit(eval);
36}
37
38#if defined(_MSC_VER)
39__declspec(noreturn)
40#else
41__attribute__((noreturn))
42#endif
43static inline void
44errx(int eval, const char *fmt, ...)
45{
46 va_list ap;
47
48 va_start(ap, fmt);
49 if (fmt != NULL)
50 vfprintf(stderr, fmt, ap);
51 va_end(ap);
52 fprintf(stderr, "\n");
53 exit(eval);
54}
55
56static inline void
57warn(const char *fmt, ...)
58{
59 int sverrno = errno;
60 va_list ap;
61
62 va_start(ap, fmt);
63 if (fmt != NULL) {
64 vfprintf(stderr, fmt, ap);
65 fprintf(stderr, ": ");
66 }
67 va_end(ap);
68 fprintf(stderr, "%s\n", strerror(sverrno));
69}
70
71static inline void
72warnx(const char *fmt, ...)
73{
74 va_list ap;
75
76 va_start(ap, fmt);
77 if (fmt != NULL)
78 vfprintf(stderr, fmt, ap);
79 va_end(ap);
80 fprintf(stderr, "\n");
81}
82
83#endif /* !defined(HAVE_ERR_H) */
84
85#endif /* _COMPAT_ERR_H */