From 55ac2338fcf5c3f2f3f812fd53063ce58aa38a49 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Tue, 1 Jun 2021 18:21:08 +0200 Subject: 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. --- README | 3 ++- sfeed.c | 1 - sfeed_atom.c | 1 - sfeed_frames.c | 1 - sfeed_gopher.c | 1 - sfeed_html.c | 1 - sfeed_mbox.c | 1 - sfeed_opml_import.c | 1 - sfeed_plain.c | 1 - sfeed_twtxt.c | 1 - sfeed_web.c | 1 - sfeed_xmlenc.c | 1 - util.c | 39 +++++++++++++++++++++++++++++++++++++++ util.h | 8 ++++++++ 14 files changed, 49 insertions(+), 12 deletions(-) diff --git a/README b/README index 2fef8b2..2877912 100644 --- 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 index 086031a..645342c 100644 --- a/sfeed.c +++ b/sfeed.c @@ -1,7 +1,6 @@ #include #include -#include #include #include #include diff --git a/sfeed_atom.c b/sfeed_atom.c index 8935db5..66698e8 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_frames.c b/sfeed_frames.c index dc225d9..89838cc 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_gopher.c b/sfeed_gopher.c index 28dcb9d..606e452 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_html.c b/sfeed_html.c index 9a03901..d8abda7 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_mbox.c b/sfeed_mbox.c index b6d3705..a734504 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/sfeed_opml_import.c b/sfeed_opml_import.c index 6eef0ca..0844b5c 100644 --- a/sfeed_opml_import.c +++ b/sfeed_opml_import.c @@ -1,5 +1,4 @@ #include -#include #include #include diff --git a/sfeed_plain.c b/sfeed_plain.c index 1b11a71..28b36e6 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c index 2b171ba..b7d7fc4 100644 --- a/sfeed_twtxt.c +++ b/sfeed_twtxt.c @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/sfeed_web.c b/sfeed_web.c index a715731..2d77d4c 100644 --- a/sfeed_web.c +++ b/sfeed_web.c @@ -1,5 +1,4 @@ #include -#include #include #include diff --git a/sfeed_xmlenc.c b/sfeed_xmlenc.c index a5ad2b6..c6a43d4 100644 --- a/sfeed_xmlenc.c +++ b/sfeed_xmlenc.c @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/util.c b/util.c index e1ff9bd..c43bdfc 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -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 index 15d6702..1570d67 100644 --- 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 *); -- cgit v1.2.3