Add BSD compatible strlcpy function (uc_strlcpy)

This commit is contained in:
Nils O. Selåsdal
2014-01-08 00:37:33 +01:00
parent 0e2d4f07c3
commit 3e313932af
2 changed files with 29 additions and 0 deletions
+12
View File
@@ -101,6 +101,18 @@ char * uc_human_bytesz_dec(uint64_t bytes, char *result, size_t result_len);
//(base 2 ,1 KiB == 1024 bytes)
char * uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len);
/** Copy a C string ensuring dst is nul terminated.
* If @max is 0, no action is done
* src must be a nul terminated string
*
* @param dst destination buffer
* @param src source string
* @maram max max chars (including nul terminator to copy). This should
* is normally the size of the dst buffer)
* return strlen(src) which may be larger than the no. of bytes copied.
*/
size_t uc_strlcpy(char *dst, const char *src, size_t max);
#ifdef __cplusplus
}
#endif
+17
View File
@@ -0,0 +1,17 @@
#include <string.h>
size_t uc_strlcpy(char *dst, const char *src, size_t max)
{
size_t len = strlen(src);
if (max != 0) {
if (len >= max) {
memcpy(dst, src, max - 1);
dst[max - 1] = 0;
} else {
memcpy(dst, src, len + 1);
}
}
return len;
}