safec 3.15
Safe C Library - ISO TR24731 Bounds Checking Interface
Loading...
Searching...
No Matches
strlcpy.c File Reference
#include "safe_str_lib.h"
Include dependency graph for strlcpy.c:

Functions

EXPORT size_t strlcpy (char *restrict dest, const char *restrict src, size_t dsize)
 strlcpy copies up to dsize - 1 characters from the NUL-terminated string src to dest, NUL-terminating the result (unless dsize is 0).

Function Documentation

◆ strlcpy()

EXPORT size_t strlcpy ( char *restrict dest,
const char *restrict src,
size_t dsize )

strlcpy copies up to dsize - 1 characters from the NUL-terminated string src to dest, NUL-terminating the result (unless dsize is 0).

Unlike strncpy, it never writes more than dsize bytes in total and always NUL-terminates a non-zero sized buffer. The caller must check whether truncation occurred by comparing the return value against dsize.

This is only compiled in when the platform libc does not already provide strlcpy (see configure's HAVE_STRLCPY / cmake's HAVE_STRLCPY probe).

Remarks
SPECIFIED IN
  • BSD, since 4.4BSD-Lite2, glibc 2.38+, musl, macOS
Parameters
[out]destpointer to the buffer that will receive src
[in]srcpointer to the NUL-terminated string to copy
[in]dsizesize of the dest buffer, in bytes
Precondition
Neither dest nor src shall be a null pointer, unless dsize is 0.
Returns
the total length of the string it tried to create, i.e. strlen(src). If the return value is >= dsize, the output was truncated.
See also
strlcat(), strcpy_s(), stpcpy_s()