add snd_strlcpy() and use it everywhere

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-03-25 16:45:43 +01:00
parent 639d404df6
commit 1755df1d9e
14 changed files with 74 additions and 61 deletions

View file

@ -177,3 +177,25 @@ static void snd_err_msg_default(const char *file, int line, const char *function
snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
#endif
/**
* \brief Copy a C-string into a sized buffer
* \param dst Where to copy the string to
* \param src Where to copy the string from
* \param size Size of destination buffer
* \retval The source string length
*
* The result is always a valid NUL-terminated string that fits
* in the buffer (unless, of course, the buffer size is zero).
* It does not pad out the result like strncpy() does.
*/
size_t snd_strlcpy(char *dst, const char *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;
}