err: Rewrite warnc() and errc() family functions to be standalone Do not depend on the system vwarn() and verr() functions to implement the *c() variants, as the system might actually lack any of the <err.h> BSD extensions.
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
diff --git a/COPYING b/COPYING
index 3e7c7cc..333ed40 100644
--- a/COPYING
+++ b/COPYING
@@ -81,7 +81,7 @@ Files:
src/fgetln.c
src/progname.c
Copyright:
- Copyright © 2005, 2008-2012 Guillem Jover <guillem@hadrons.org>
+ Copyright © 2005, 2008-2012, 2019 Guillem Jover <guillem@hadrons.org>
Copyright © 2005 Hector Garcia Alvarez
Copyright © 2005 Aurelien Jarno
Copyright © 2006 Robert Millan
diff --git a/include/bsd/err.h b/include/bsd/err.h
index ff37e55..9e83260 100644
--- a/include/bsd/err.h
+++ b/include/bsd/err.h
@@ -43,15 +43,15 @@
#include <stdarg.h>
__BEGIN_DECLS
-void warnc(int code, const char *format, ...)
- __printflike(2, 3);
void vwarnc(int code, const char *format, va_list ap)
__printflike(2, 0);
+void warnc(int code, const char *format, ...)
+ __printflike(2, 3);
-void errc(int status, int code, const char *format, ...)
- __printflike(3, 4) __dead2;
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;
__END_DECLS
#endif
diff --git a/src/err.c b/src/err.c
index 4e50510..45f1b61 100644
--- a/src/err.c
+++ b/src/err.c
@@ -1,6 +1,6 @@
/*
* Copyright © 2006 Robert Millan
- * Copyright © 2011 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2011, 2019 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,8 +26,21 @@
*/
#include <err.h>
-#include <errno.h>
+#include <string.h>
#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+vwarnc(int code, const char *format, va_list ap)
+{
+ fprintf(stderr, "%s: ", getprogname());
+ if (format) {
+ vfprintf(stderr, format, ap);
+ fprintf(stderr, ": ");
+ }
+ fprintf(stderr, "%s\n", strerror(code));
+}
void
warnc(int code, const char *format, ...)
@@ -40,13 +53,15 @@ warnc(int code, const char *format, ...)
}
void
-vwarnc(int code, const char *format, va_list ap)
+verrc(int status, int code, const char *format, va_list ap)
{
- int tmp = errno;
-
- errno = code;
- vwarn(format, ap);
- errno = tmp;
+ fprintf(stderr, "%s: ", getprogname());
+ if (format) {
+ vfprintf(stderr, format, ap);
+ fprintf(stderr, ": ");
+ }
+ fprintf(stderr, "%s\n", strerror(code));
+ exit(status);
}
void
@@ -58,10 +73,3 @@ errc(int status, int code, const char *format, ...)
verrc(status, code, format, ap);
va_end(ap);
}
-
-void
-verrc(int status, int code, const char *format, va_list ap)
-{
- errno = code;
- verr(status, format, ap);
-}