OpenBSD manual page server

Manual Page Search Parameters

STRNCAT(3) Library Functions Manual STRNCAT(3)

strncatconcatenate a string with part of another

#include <string.h>

char *
strncat(char *s, const char *append, size_t count);

The () function appends not more than count characters of the NUL-terminated string append to the end of the NUL-terminated string s. Space for the terminating ‘\0’ should not be included in count. The string s must have sufficient space to hold the result.

The strncat() function returns the pointer s.

The following appends “abc” to chararray:

char *letters = "abcdefghi";

(void)strncat(chararray, letters, 3);

The following example shows how to use strncat() safely in conjunction with strncpy(3).

char buf[BUFSIZ];
char *input, *suffix;

(void)strncpy(buf, input, 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 input to buf as will fit. It then appends as many characters from suffix as will fit (or none if there is no space). For operations like this, the strlcpy(3) and strlcat(3) functions are a better choice, as shown below.

(void)strlcpy(buf, input, sizeof(buf));
(void)strlcat(buf, suffix, sizeof(buf));

bcopy(3), memccpy(3), memcpy(3), memmove(3), strcat(3), strcpy(3), strlcpy(3), wcscat(3), wcslcpy(3)

The strcat() and strncat() functions conform to ANSI X3.159-1989 (“ANSI C89”).

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

December 19, 2013 OpenBSD-5.5