[hackers] [slstatus] Refactor snprintf-usage in util.c || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Wed, 23 May 2018 19:56:41 +0200 (CEST)

commit 13d77cd268bade5e9ee9c07d9b94ca7c733cbe3d
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Wed May 23 13:51:50 2018 +0200
Commit: Aaron Marcher <me_AT_drkhsh.at>
CommitDate: Wed May 23 19:56:04 2018 +0200

    Refactor snprintf-usage in util.c

diff --git a/util.c b/util.c
index b412211..5ce2396 100644
--- a/util.c
+++ b/util.c
_AT_@ -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, ...)
 {
_AT_@ -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 *
Received on Wed May 23 2018 - 19:56:41 CEST

This archive was generated by hypermail 2.3.0 : Wed May 23 2018 - 20:00:34 CEST