commit 55ac2338fcf5c3f2f3f812fd53063ce58aa38a49
parent cbb82666e00815aa2dbcc3f144460f3003b46b70
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 1 Jun 2021 18:21:08 +0200
portability and standards: add BSD-like err() and errx() functions
These are BSD functions.
- HaikuOS now compiles without having to use libbsd.
- Tested on SerenityOS (for fun), which doesn't have these functions (yet).
With a small change to support wcwidth() sfeed works on SerenityOS.
Diffstat:
14 files changed, 49 insertions(+), 12 deletions(-)
diff --git a/README b/README
@@ -117,7 +117,8 @@ OS tested
- FreeBSD
- DragonFlyBSD
- Windows (cygwin gcc, mingw).
-- HaikuOS (using libbsd).
+- HaikuOS
+- SerenityOS
- FreeDOS (djgpp).
- FUZIX (sdcc -mz80).
diff --git a/sfeed.c b/sfeed.c
@@ -1,7 +1,6 @@
#include <sys/types.h>
#include <ctype.h>
-#include <err.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
diff --git a/sfeed_atom.c b/sfeed_atom.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
diff --git a/sfeed_frames.c b/sfeed_frames.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/sfeed_gopher.c b/sfeed_gopher.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/sfeed_html.c b/sfeed_html.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/sfeed_mbox.c b/sfeed_mbox.c
@@ -1,4 +1,3 @@
-#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/sfeed_opml_import.c b/sfeed_opml_import.c
@@ -1,5 +1,4 @@
#include <ctype.h>
-#include <err.h>
#include <stdio.h>
#include <strings.h>
diff --git a/sfeed_plain.c b/sfeed_plain.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c
@@ -1,6 +1,5 @@
#include <sys/types.h>
-#include <err.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
diff --git a/sfeed_web.c b/sfeed_web.c
@@ -1,5 +1,4 @@
#include <ctype.h>
-#include <err.h>
#include <stdio.h>
#include <strings.h>
diff --git a/sfeed_xmlenc.c b/sfeed_xmlenc.c
@@ -1,5 +1,4 @@
#include <ctype.h>
-#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
diff --git a/util.c b/util.c
@@ -1,5 +1,6 @@
#include <ctype.h>
#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -7,6 +8,44 @@
#include "util.h"
+/* print to stderr, print error message of errno and exit().
+ Unlike BSD err() it does not prefix __progname */
+__dead void
+err(int exitstatus, const char *fmt, ...)
+{
+ va_list ap;
+ int saved_errno;
+
+ saved_errno = errno;
+
+ if (fmt) {
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, ": ");
+ }
+ fprintf(stderr, "%s\n", strerror(saved_errno));
+
+ exit(exitstatus);
+}
+
+/* print to stderr and exit().
+ Unlike BSD errx() it does not prefix __progname */
+__dead void
+errx(int exitstatus, const char *fmt, ...)
+{
+ va_list ap;
+
+ if (fmt) {
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ }
+ fputs("\n", stderr);
+
+ exit(exitstatus);
+}
+
/* check if string has a non-empty scheme / protocol part */
int
uri_hasscheme(const char *s)
diff --git a/util.h b/util.h
@@ -38,6 +38,14 @@ enum {
FieldLast
};
+/* hint for compilers and static analyzers that a function exits */
+#ifndef __dead
+#define __dead
+#endif
+
+__dead void err(int, const char *, ...);
+__dead void errx(int, const char *, ...);
+
int uri_format(char *, size_t, struct uri *);
int uri_hasscheme(const char *);
int uri_makeabs(struct uri *, struct uri *, struct uri *);