Add a strncpy_safe() utility function

Does strncpy, but guarantees that the string is null-terminated.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2015-08-25 10:35:24 +10:00
parent 268fbbbf40
commit 4f6d63ce05

View File

@@ -25,6 +25,7 @@
#define LIBRATBAG_UTIL_H
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libudev.h>
@@ -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 *