strlcpy: simplify

This commit is contained in:
Rémi Denis-Courmont 2018-06-17 11:23:11 +03:00
parent a50366bc97
commit 893432c251
1 changed files with 8 additions and 11 deletions

View File

@ -22,7 +22,7 @@
# include <config.h>
#endif
#include <stddef.h>
#include <string.h>
/**
* Copy a string to a sized buffer. The result is always nul-terminated
@ -37,16 +37,13 @@
*/
size_t strlcpy (char *tgt, const char *src, size_t bufsize)
{
size_t length;
size_t length = strlen(src);
for (length = 1; (length < bufsize) && *src; length++)
*tgt++ = *src++;
if (bufsize > length)
memcpy(tgt, src, length + 1);
else
if (bufsize > 0)
memcpy(tgt, src, bufsize - 1), tgt[bufsize - 1] = '\0';
if (bufsize)
*tgt = '\0';
while (*src++)
length++;
return length - 1;
return length;
}