From 4f6d63ce05974551285850b6a1dc0c72dcd83e83 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 25 Aug 2015 10:35:24 +1000 Subject: [PATCH] Add a strncpy_safe() utility function Does strncpy, but guarantees that the string is null-terminated. Signed-off-by: Peter Hutterer --- src/libratbag-util.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libratbag-util.h b/src/libratbag-util.h index 93897c60..ad9640b0 100644 --- a/src/libratbag-util.h +++ b/src/libratbag-util.h @@ -25,6 +25,7 @@ #define LIBRATBAG_UTIL_H #include +#include #include #include @@ -78,6 +79,14 @@ int list_empty(const struct list *list); #define streq(s1, s2) (strcmp((s1), (s2)) == 0) #define strneq(s1, s2, n) (strncmp((s1), (s2), (n)) == 0) +static inline char* +strncpy_safe(char *dest, const char *src, size_t n) +{ + strncpy(dest, src, n); + dest[n - 1] = '\0'; + return dest; +} + #define LIBRATBAG_EXPORT __attribute__ ((visibility("default"))) static inline void *