Refactor eprintf.c

When we move the exit() out of venprintf(), we can reuse it for
weprintf(), which basically had duplicate code.
I also renamed venprintf() to xvprintf (extended vprintf) so it's
more obvious what it actually does.
This commit is contained in:
FRIGN 2015-12-21 09:28:44 +01:00 committed by sin
parent 6135cad495
commit 94e92d9cc0
1 changed files with 13 additions and 19 deletions

View File

@ -8,7 +8,7 @@
char *argv0;
static void venprintf(int, const char *, va_list);
static void xvprintf(const char *, va_list);
void
eprintf(const char *fmt, ...)
@ -16,8 +16,10 @@ eprintf(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
venprintf(1, fmt, ap);
xvprintf(fmt, ap);
va_end(ap);
exit(1);
}
void
@ -26,22 +28,8 @@ enprintf(int status, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
venprintf(status, fmt, ap);
xvprintf(fmt, ap);
va_end(ap);
}
void
venprintf(int status, const char *fmt, va_list ap)
{
if (strncmp(fmt, "usage", strlen("usage")))
fprintf(stderr, "%s: ", argv0);
vfprintf(stderr, fmt, ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
}
exit(status);
}
@ -51,12 +39,18 @@ weprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
xvprintf(fmt, ap);
va_end(ap);
}
void
xvprintf(const char *fmt, va_list ap)
{
if (strncmp(fmt, "usage", strlen("usage")))
fprintf(stderr, "%s: ", argv0);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);