memory: add xstrlcpy

Less "blow a hole in your foot" than strncpy. As also indicated by coverity.
Implementation inspired by the linux kernel (very similar to OSX's Libc
implementation as well).
This commit is contained in:
Nicolas Hillegeer
2014-05-24 15:02:13 +02:00
committed by Thiago de Arruda
parent 3a68a4861a
commit a50a34f472
2 changed files with 26 additions and 0 deletions

View File

@@ -183,6 +183,19 @@ char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen)
}
}
size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dst, src, len);
dst[len] = '\0';
}
return ret;
}
char *xstrdup(const char *str)
{
char *ret = strdup(str);