|STRNCAT(3)||Library Functions Manual||STRNCAT(3)|
concatenate a string with part of another
*dst, const char
function appends not more than count characters of the
string append to the end of the string found in the
buffer dst. Space for the terminating
\0’ should not be included in
Bounds checking must be performed manually with great care. If the buffer dst is not large enough to hold the result, subsequent memory will be damaged.
strncat() function returns the pointer
The following example shows how to use
strncat() in conjunction with
char buf[BUFSIZ]; char *base, *suffix; (void)strncpy(buf, base, sizeof(buf) - 1); buf[sizeof(buf) - 1] = '\0'; (void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
The above will copy as many characters from base to buf as will fit. It then appends as many characters from suffix as will fit. If either base or suffix are too large, truncation will occur without detection.
The above example shows dangerous coding patterns, including an
inability to detect truncation.
strncpy() are dangerously easy to misuse. The
strlcat(3) functions are
safer for this kind of operation:
if (strlcpy(buf, base, sizeof(buf)) >= sizeof(buf) || strlcat(buf, suffix, sizeof(buf)) >= sizeof(buf)) goto toolong;
if (snprintf(buf, sizeof(buf), "%s%s", base, suffix) >= sizeof(buf)) goto toolong;
strncat() function conforms to
strncat() function first appeared in
Version 7 AT&T UNIX.
|April 19, 2014||OpenBSD-6.0|