[RFC/PATCH v2 2/5] libbb: add DECIMAL_STR_MAX macro

Bartosz Golaszewski bartekgola at gmail.com
Fri Aug 21 14:23:40 UTC 2015


Add a macro returning the maximum required buffer size for given
integer type.

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
 include/libbb.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/include/libbb.h b/include/libbb.h
index 478c030..a56b684 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -225,6 +225,13 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
 	: ((T)1 << (sizeof(T)*8-1)) \
 	)
 
+/* Found in systemd, src/basic/macro.h */
+#define DECIMAL_STR_MAX(type) \
+	(2+(sizeof(type) <= 1 ? 3 : \
+	sizeof(type) <= 2 ? 5 : \
+	sizeof(type) <= 4 ? 10 : \
+	sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
+
 /* Large file support */
 /* Note that CONFIG_LFS=y forces bbox to be built with all common ops
  * (stat, lseek etc) mapped to "largefile" variants by libc.
-- 
2.1.4



More information about the busybox mailing list