ft_strlcpy

Subject

STRLCPY(3) (simplified)

NAME
    strlcpy -- size-bounded string copying
SYNOPSIS
    size_t strlcpy(char *dst, const char *src, size_t dstsize);
DESCRIPTION
    The strlcpy() function copy strings with the same input parameters and output result as snprintf(3). It is designed to be safer, more consistent, and less error prone replacement for the easily misused function strncpy(3)
    strlcpy() take the full size of the destination buffer and guarantee NUL-termination if there is room. Note that room for the NUL should be included in dstsize. Also note that strlcpy() only operate on true ''C'' strings. This means that for strlcpy() src must be NUL-terminated.
    strlcpy() copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0.
    If the src and dst strings overlap, the behavior is undefined.
RETURN VALUES
    The strlcpy() function return the total length of the strings it tried to create. That means the length of src.
    If the return value is >= dstsize, the output string has been truncated.
    It is the caller's responsibility to handle this.

Understandable explanation

What this function does is pretty simple in that it's made to copy one string to another but with a small catch, it always NUL-terminate the string.

If you give a dstsize long enough to NUL-terminate the string without truncating it, strlcpy() will simply copy the string, as you'd do with strcpy(). If you don't give a dstsize long enough, it will copy dstsize - 1 characters from the source into the destination, adding the NUL-terminating character after that.

The strlcpy() function always returns the length of the string that it tried to create, this is the length of src, even if you have to truncate the string to NUL-terminate it.

Hints

I implemented this the same way it is implemented in the Apple's C version. Check the sources for the link to the page.

Commented solution

ft_strlcpy

Sources

Apple OpenSource strlcpy implementation

Last updated

Was this helpful?