aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-08-06 14:53:06 +0800
committerverdant <im@verdant.ee>2026-08-06 14:53:06 +0800
commitf79065957e81d8d297cf5a4f1019d698de3b8137 (patch)
tree5b1107cde335f99b608107fdaf1dcabd155fc590
parentb87d0c4a36c2cfb3d79b09838b88fcab1a8d07f3 (diff)
downloadsf-f79065957e81d8d297cf5a4f1019d698de3b8137.tar.gz
sf-f79065957e81d8d297cf5a4f1019d698de3b8137.zip
utils: implement xsnprintf function
Check the return value of (v)snprintf
-rw-r--r--src/utils.c17
-rw-r--r--src/utils.h1
2 files changed, 18 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index c00c3f9..7a762f7 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,4 +1,5 @@
#include "utils.h"
+#include "err.h"
#include "fm.h"
#include "log.h"
#include <limits.h>
@@ -61,3 +62,19 @@ y_or_n(const char *prompt)
return ch == 'y';
}
+
+int
+xsnprintf(char *str, size_t size, const char *restrict format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+
+ size_t len = vsnprintf(str, size, format, ap);
+ if (len >= size) {
+ die("Length of items error");
+ }
+
+ va_end(ap);
+
+ return SF_OK;
+}
diff --git a/src/utils.h b/src/utils.h
index 7c73fc8..c539ad9 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -7,5 +7,6 @@
int get_input(const char *prompt, char *buf, size_t len);
bool y_or_n(const char *prompt);
+int xsnprintf(char *str, size_t size, const char *restrict format, ...);
#endif /* SF_UTILS_H */