1
0

Refactor snprintf-usage in util.c

This commit is contained in:
Laslo Hunhold 2018-05-23 13:51:50 +02:00 committed by Aaron Marcher
parent d6ad87ce06
commit 13d77cd268

36
util.c
View File

@ -48,6 +48,24 @@ die(const char *fmt, ...)
exit(1); exit(1);
} }
static int
evsnprintf(char *str, size_t size, const char *fmt, va_list ap)
{
int ret;
ret = vsnprintf(str, size, fmt, ap);
if (ret < 0) {
warn("vsnprintf:");
return -1;
} else if ((size_t)ret >= size) {
warn("vsnprintf: Output truncated");
return -1;
}
return ret;
}
int int
esnprintf(char *str, size_t size, const char *fmt, ...) esnprintf(char *str, size_t size, const char *fmt, ...)
{ {
@ -55,17 +73,9 @@ esnprintf(char *str, size_t size, const char *fmt, ...)
int ret; int ret;
va_start(ap, fmt); va_start(ap, fmt);
ret = vsnprintf(str, size, fmt, ap); ret = evsnprintf(str, size, fmt, ap);
va_end(ap); va_end(ap);
if (ret < 0) {
warn("snprintf:");
return -1;
} else if ((size_t)ret >= size) {
warn("snprintf: Output truncated");
return -1;
}
return ret; return ret;
} }
@ -76,14 +86,10 @@ bprintf(const char *fmt, ...)
int ret; int ret;
va_start(ap, fmt); va_start(ap, fmt);
if ((ret = vsnprintf(buf, sizeof(buf), fmt, ap)) < 0) { ret = evsnprintf(buf, sizeof(buf), fmt, ap);
warn("vsnprintf:");
} else if ((size_t)ret >= sizeof(buf)) {
warn("vsnprintf: Output truncated");
}
va_end(ap); va_end(ap);
return buf; return (ret < 0) ? NULL : buf;
} }
const char * const char *