STRNCAT(3)

NAME
strncat - concatenate a string with part of another

SYNOPSIS
#include <string.h>
char *
strncat(char *dst, const char *append, size_t count);

DESCRIPTION
The strncat() 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 count.

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.

RETURN VALUES
The strncat() function returns the pointer dst.

EXAMPLES
The following example shows how to use strncat() in conjunction with strncpy(3):

    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. strncat() and strncpy() are dangerously easy to misuse. The strlcpy(3) and 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;

or for greatest portability,

    if (snprintf(buf, sizeof(buf), "%s%s",
        base, suffix) >= sizeof(buf))
            goto toolong;

SEE ALSO
strlcpy(3), wcscat(3), wcslcpy(3)

STANDARDS
The strncat() function conforms to ansiC.

HISTORY
The strncat() function first appeared in AT&T UNIX.