diff options
author | Laslo Hunhold <dev@frign.de> | 2018-05-23 13:51:50 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-23 19:56:04 +0200 |
commit | 13d77cd268bade5e9ee9c07d9b94ca7c733cbe3d (patch) | |
tree | 83e658e56ca4baea0858fb0f558b49915a424f19 /util.c | |
parent | d6ad87ce06593979b397fe1a01fc5fe9cb86e6a9 (diff) |
Refactor snprintf-usage in util.c
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -48,27 +48,37 @@ die(const char *fmt, ...) exit(1); } -int -esnprintf(char *str, size_t size, const char *fmt, ...) +static int +evsnprintf(char *str, size_t size, const char *fmt, va_list ap) { - va_list ap; int ret; - va_start(ap, fmt); ret = vsnprintf(str, size, fmt, ap); - va_end(ap); if (ret < 0) { - warn("snprintf:"); + warn("vsnprintf:"); return -1; } else if ((size_t)ret >= size) { - warn("snprintf: Output truncated"); + warn("vsnprintf: Output truncated"); return -1; } return ret; } +int +esnprintf(char *str, size_t size, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = evsnprintf(str, size, fmt, ap); + va_end(ap); + + return ret; +} + const char * bprintf(const char *fmt, ...) { @@ -76,14 +86,10 @@ bprintf(const char *fmt, ...) int ret; va_start(ap, fmt); - if ((ret = vsnprintf(buf, sizeof(buf), fmt, ap)) < 0) { - warn("vsnprintf:"); - } else if ((size_t)ret >= sizeof(buf)) { - warn("vsnprintf: Output truncated"); - } + ret = evsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - return buf; + return (ret < 0) ? NULL : buf; } const char * |