It is designed so that usage requires the minimum number of lines of code for complete usage (including error handling checks):
[[
// When we already checked that 'size' is >= 1
// and truncation is not an issue:
strcpys_np(size, dest, src, NULL);
[[gnu::warn_unused_result]]
[[
#include <string.h>
#include <sys/types.h>
[[gnu::nonnull]]
ssize_t strscpy_np(ssize_t size,
char dest[static restrict size],
const char src[static restrict size])
{
ssize_t len;
if (size <= 0)
return -1;
len = strnlen(src, size - 1);
memcpy(dest, src, len);
dest[len] = '\0';
return len;
}
[[gnu::nonnull(2, 3)]]
int strcpys_np(ssize_t size,
char dest[static restrict size],
const char src[static restrict size],
ssize_t *restrict len)
{
ssize_t l;
l = strscpy_np(size, dest, src);
if (len)
*len = l;
if (l == -1)
return -1;
if (l >= size)
return 1;
return 0;
}
]]