[liba] Improve strlcpy

Use memcpy (can be optimized) and return the correct size
This commit is contained in:
Romain Goyet
2020-02-13 16:03:32 -05:00
committed by Ecco
parent 7f43b73049
commit dc4f43eeb0

View File

@@ -1,17 +1,12 @@
#include <string.h>
size_t strlcpy(char * dst, const char * src, size_t dstSize) {
if (dstSize == 0) {
return strlen(src);
const size_t srcLen = strlen(src);
if (srcLen+1 < dstSize) {
memcpy(dst, src, srcLen+1);
} else if (dstSize != 0) {
memcpy(dst, src, dstSize-1);
dst[dstSize-1] = 0;
}
const char * cur = src;
const char * end = src+dstSize-1;
while (*cur != 0 && cur < end) {
*dst++ = *cur++;
}
*dst = 0;
while (*cur != 0) {
cur++;
}
return cur-src;
return srcLen;
}