2 .\" Copyright 2022 Alejandro Colomar <alx@kernel.org>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH strncat 3 (date) "Linux man-pages (unreleased)"
8 strncat \- concatenate a null-padded character sequence into a string
11 .RI ( libc ", " \-lc )
14 .B #include <string.h>
16 .BI "char *strncat(char *restrict " dst ", const char " src "[restrict ." sz ],
20 This function catenates the input character sequence
21 contained in a null-padded fixed-width buffer,
22 into a string at the buffer pointed to by
24 The programmer is responsible for allocating a destination buffer large enough,
26 .IR "strlen(dst) + strnlen(src, sz) + 1" .
28 An implementation of this function might be:
33 strncat(char *restrict dst, const char *restrict src, size_t sz)
38 len = strnlen(src, sz);
39 p = dst + strlen(dst);
40 p = mempcpy(p, src, len);
52 For an explanation of the terms used in this section, see
58 Interface Attribute Value
63 T} Thread safety MT-Safe
68 POSIX.1-2001, C89, SVr4, 4.3BSD.
70 The name of this function is confusing.
71 This function has no relation to
74 If the destination buffer is not large enough,
75 the behavior is undefined.
79 .BR feature_test_macros (7).
81 This function can be very inefficient.
83 .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
87 .\" SRC BEGIN (strncat.c)
94 #define nitems(arr) (sizeof((arr)) / sizeof((arr)[0]))
101 // Null-padded fixed-width character sequences
102 char pre[4] = "pre.";
103 char new_post[50] = ".foo.bar";
106 char post[] = ".post";
107 char src[] = "some_long_body.post";
110 maxsize = nitems(pre) + strlen(src) \- strlen(post) +
111 nitems(new_post) + 1;
112 dest = malloc(sizeof(*dest) * maxsize);
114 err(EXIT_FAILURE, "malloc()");
116 dest[0] = \[aq]\e0\[aq]; // There's no 'cpy' function to this 'cat'.
117 strncat(dest, pre, nitems(pre));
118 strncat(dest, src, strlen(src) \- strlen(post));
119 strncat(dest, new_post, nitems(new_post));
121 puts(dest); // "pre.some_long_body.foo.bar"
130 .BR string_copying (3)