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.
This commit is contained in:
Hiltjo Posthuma 2021-06-01 18:21:08 +02:00
parent cbb82666e0
commit 55ac2338fc
14 changed files with 49 additions and 12 deletions

3
README
View File

@ -117,7 +117,8 @@ OS tested
- FreeBSD
- DragonFlyBSD
- Windows (cygwin gcc, mingw).
- HaikuOS (using libbsd).
- HaikuOS
- SerenityOS
- FreeDOS (djgpp).
- FUZIX (sdcc -mz80).

View File

@ -1,7 +1,6 @@
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,4 +1,3 @@
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,5 +1,4 @@
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <strings.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>

View File

@ -1,6 +1,5 @@
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

View File

@ -1,5 +1,4 @@
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <strings.h>

View File

@ -1,5 +1,4 @@
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

39
util.c
View File

@ -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)

8
util.h
View File

@ -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 *);