I do not understand why do we have to return an unsigned int
for strlcat
and strlcpy
, why do we need that ? it's not the aim of the function.
Thanks for your responses
I do not understand why do we have to return an unsigned int
for strlcat
and strlcpy
, why do we need that ? it's not the aim of the function.
Thanks for your responses
The aim of these functions is to move strings (in some way), and a very frequent question by the programmer when you do that is "how many chars were moved?".
Since it already knows this information, it is no more work for it to return this information. Otherwise the programmer would have to do some expensive strlen()'s before and/or after.
Also, if they fail due to a too small buffer, they give you the number of characters they would need, so you can detect the truncation, and potentially reallocate the buffer.
strlcpy
and strlcat
are non standard functions available on some BSD versions of Unix that perform a safe version of strcpy
and strcat
with truncation. They do not actually return an unsigned int
, but a size_t
, which is the type returned by sizeof
and may be different from unsigned int
.
The functions are declared in <string.h>
with these prototypes:
size_t strlcpy(char *dst, const char *src, size_t size);
size_t strlcat(char *dst, const char *src, size_t size);
dst
is a pointer to the destination array. This array must contain a valid C string for strlcat
, it can be NULL
if size
is 0
.src
must be a valid pointer to a C string.size
is the size in bytes of the destination array.The functions perform a string copy or concatenation, similar to strcpy
and strcat
, but do not write beyond the end of the destination array (size
). They null terminate the destination array unless size
is 0
or dst
points to an array without a null terminator in the first size
bytes for strlcat
.
Both functions return the length in bytes of the resulting string if the destination array was long enough. This permits easy detection of truncation.
Here is an example:
char dest[10];
size_t len = strlcpy(dest, "This is a random string", sizeof dest);
if (len >= sizeof dest) {
/* Truncation occurred. You could ignore it, issue a diagnostic,
or reallocate the destination array to at least `len+1` bytes */
printf("Truncation occurred\n");
}
So here is the answer to your question: the return value is useful if the programmer wants to detect truncation, otherwise it may be safely ignored.
Synopsis of strlcpy()
and strlcat()
#include <string.h>
size_t
strlcpy(char *dst, const char *src, size_t size);
size_t
strlcat(char *dst, const char *src, size_t size);
The strlcpy()
and strlcat()
functions return the total length of the
string they tried to create. For strlcpy()
that means the length of src
.
For strlcat()
that means the initial length of dst
plus the length of
src
. While this may seem somewhat confusing it was done to make truncation detection simple.
Note however, that if strlcat()
traverses size characters without finding
a NUL, the length of the string is considered to be size and the destination string will not be NUL terminated (since there was no space for the
NUL). This keeps strlcat()
from running off the end of a string. In
practice this should not happen (as it means that either size is incorrect or that dst is not a proper ``C'' string). The check exists to prevent potential security problems in incorrect code.