]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
err: Add err(), warn(), errx() and warnx() familiy of functions
authorGuillem Jover <guillem@hadrons.org>
Tue, 6 Aug 2019 13:49:41 +0000 (15:49 +0200)
committerGuillem Jover <guillem@hadrons.org>
Thu, 8 Aug 2019 01:47:05 +0000 (03:47 +0200)
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.

include/bsd/err.h
src/err.c
src/libbsd.map

index 9e83260795598f348581c24c7a0f75c6d5c665b5..adf08f67c0203f94f7124355ff2c6bb5e46dfb67 100644 (file)
 #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
index 45f1b61ede620ebd1ae019c6f56a1cd5dd833b91..8f09972e96d67ea039353d34b16424c2387969dc 100644 (file)
--- 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
index ce360407000d7ce77341e491cd31243e8a5f6d86..caa5cd118f670e914d67dfce9a8a59404dbbd6ab 100644 (file)
@@ -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;