From 5931ed5a14f56526f63c59d9ddbbae59b9a639cd Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Fri, 28 Sep 2007 10:45:59 +0200 Subject: [PATCH] add safe_strncpy() function --- include/libbb.h | 2 ++ lib/libbb.c | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/include/libbb.h b/include/libbb.h index 9a1fdde5b..fdabc4bb5 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -24,4 +24,6 @@ int recursive_action(const char *fileName, unsigned flags, int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), void* userData, const unsigned depth); +char * safe_strncpy(char *dst, const char *src, size_t size); + #endif /* __LIBBB_H */ diff --git a/lib/libbb.c b/lib/libbb.c index daa484c24..dd32307f1 100644 --- a/lib/libbb.c +++ b/lib/libbb.c @@ -99,3 +99,11 @@ char* last_char_is(const char *s, int c) return NULL; } +/* Like strncpy but make sure the resulting string is always 0 terminated. */ +char * safe_strncpy(char *dst, const char *src, size_t size) +{ + if (!size) return dst; + dst[--size] = '\0'; + return strncpy(dst, src, size); +} +