diff --git a/include/libbb.h b/include/libbb.h index 9a1fdde..fdabc4b 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -24,4 +24,6 @@ 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 daa484c..dd32307 100644 --- a/lib/libbb.c +++ b/lib/libbb.c @@ -99,3 +99,11 @@ 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); +} +