err: Add err(), warn(), errx() and warnx() familiy of functions Some systems such as Windows or musl-libc based ones do not have these BSD extensions. In addition libbsd itself is making use of the warnx() functions, so we better provide these interfaces in case they are missing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
diff --git a/include/bsd/err.h b/include/bsd/err.h
index 9e83260..adf08f6 100644
--- a/include/bsd/err.h
+++ b/include/bsd/err.h
@@ -29,11 +29,15 @@
#include <sys/cdefs.h>
#if __has_include_next(<err.h>)
#include_next <err.h>
+#else
+#define LIBBSD_NEED_ERR_H_FUNCS
#endif
#else
#include <bsd/sys/cdefs.h>
#if __has_include(<err.h>)
#include <err.h>
+#else
+#define LIBBSD_NEED_ERR_H_FUNCS
#endif
#endif
@@ -52,6 +56,26 @@ void verrc(int status, int code, const char *format, va_list ap)
__printflike(3, 0) __dead2;
void errc(int status, int code, const char *format, ...)
__printflike(3, 4) __dead2;
+
+#ifdef LIBBSD_NEED_ERR_H_FUNCS
+void vwarn(const char *format, va_list ap)
+ __printflike(1, 0);
+void vwarnx(const char *format, va_list ap)
+ __printflike(1, 0);
+void warn(const char *format, ...)
+ __printflike(1, 2);
+void warnx(const char *format, ...)
+ __printflike(1, 2);
+
+void verr(int status, const char *format, va_list ap)
+ __printflike(2, 0) __dead2;
+void verrx(int status, const char *format, va_list ap)
+ __printflike(2, 0) __dead2;
+void err(int status, const char *format, ...)
+ __printflike(2, 3) __dead2;
+void errx(int status, const char *format, ...)
+ __printflike(2, 3) __dead2;
+#endif
__END_DECLS
#endif
diff --git a/src/err.c b/src/err.c
index 45f1b61..8f09972 100644
--- a/src/err.c
+++ b/src/err.c
@@ -26,6 +26,9 @@
*/
#include <err.h>
+#ifdef LIBBSD_NEED_ERR_H_FUNCS
+#include <errno.h>
+#endif
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -73,3 +76,76 @@ errc(int status, int code, const char *format, ...)
verrc(status, code, format, ap);
va_end(ap);
}
+
+#ifdef LIBBSD_NEED_ERR_H_FUNCS
+void
+vwarn(const char *format, va_list ap)
+{
+ vwarnc(errno, format, ap);
+}
+
+void
+warn(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vwarnc(errno, format, ap);
+ va_end(ap);
+}
+
+void
+vwarnx(const char *format, va_list ap)
+{
+ fprintf(stderr, "%s: ", getprogname());
+ if (format)
+ vfprintf(stderr, format, ap);
+ fprintf(stderr, "\n");
+}
+
+void
+warnx(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vwarnx(format, ap);
+ va_end(ap);
+}
+
+void
+verr(int status, const char *format, va_list ap)
+{
+ verrc(status, errno, format, ap);
+}
+
+void
+err(int status, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ verrc(status, errno, format, ap);
+ va_end(ap);
+}
+
+void
+verrx(int eval, const char *format, va_list ap)
+{
+ fprintf(stderr, "%s: ", getprogname());
+ if (format)
+ vfprintf(stderr, format, ap);
+ fprintf(stderr, "\n");
+ exit(eval);
+}
+
+void
+errx(int eval, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ verrx(eval, format, ap);
+ va_end(ap);
+}
+#endif
diff --git a/src/libbsd.map b/src/libbsd.map
index ce36040..caa5cd1 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -168,3 +168,16 @@ LIBBSD_0.9.1 {
strnvis_netbsd;
strnunvis_netbsd;
} LIBBSD_0.9;
+
+LIBBSD_0.10.0 {
+ /* These BSD extensions are available on GNU systems, but not on other
+ * systems such as Windows or musl libc based ones. */
+ vwarn;
+ vwarnx;
+ warn;
+ warnx;
+ verr;
+ verrx;
+ err;
+ errx;
+} LIBBSD_0.9.1;