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.
This commit is contained in:
Guillem Jover 2019-08-06 15:37:43 +02:00
parent f34a5f71d9
commit 9628798d7d
3 changed files with 28 additions and 20 deletions

View File

@ -81,7 +81,7 @@ Files:
src/fgetln.c src/fgetln.c
src/progname.c src/progname.c
Copyright: 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 Hector Garcia Alvarez
Copyright © 2005 Aurelien Jarno Copyright © 2005 Aurelien Jarno
Copyright © 2006 Robert Millan Copyright © 2006 Robert Millan

View File

@ -43,15 +43,15 @@
#include <stdarg.h> #include <stdarg.h>
__BEGIN_DECLS __BEGIN_DECLS
void warnc(int code, const char *format, ...)
__printflike(2, 3);
void vwarnc(int code, const char *format, va_list ap) void vwarnc(int code, const char *format, va_list ap)
__printflike(2, 0); __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) void verrc(int status, int code, const char *format, va_list ap)
__printflike(3, 0) __dead2; __printflike(3, 0) __dead2;
void errc(int status, int code, const char *format, ...)
__printflike(3, 4) __dead2;
__END_DECLS __END_DECLS
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* Copyright © 2006 Robert Millan * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -26,8 +26,21 @@
*/ */
#include <err.h> #include <err.h>
#include <errno.h> #include <string.h>
#include <stdarg.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 void
warnc(int code, const char *format, ...) warnc(int code, const char *format, ...)
@ -40,13 +53,15 @@ warnc(int code, const char *format, ...)
} }
void void
vwarnc(int code, const char *format, va_list ap) verrc(int status, int code, const char *format, va_list ap)
{ {
int tmp = errno; fprintf(stderr, "%s: ", getprogname());
if (format) {
errno = code; vfprintf(stderr, format, ap);
vwarn(format, ap); fprintf(stderr, ": ");
errno = tmp; }
fprintf(stderr, "%s\n", strerror(code));
exit(status);
} }
void void
@ -58,10 +73,3 @@ errc(int status, int code, const char *format, ...)
verrc(status, code, format, ap); verrc(status, code, format, ap);
va_end(ap); va_end(ap);
} }
void
verrc(int status, int code, const char *format, va_list ap)
{
errno = code;
verr(status, format, ap);
}