]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/strncat.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / strncat.3
CommitLineData
2c5503c6 1'\" t
5c6f6e67
AC
2.\" Copyright 2022 Alejandro Colomar <alx@kernel.org>
3.\"
4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
5.\"
6.TH strncat 3 (date) "Linux man-pages (unreleased)"
7.SH NAME
9f3c55b0
AC
8strncat
9\-
10append non-null bytes from a source array to a string,
11and null-terminate the result
5c6f6e67
AC
12.SH LIBRARY
13Standard C library
14.RI ( libc ", " \-lc )
15.SH SYNOPSIS
16.nf
17.B #include <string.h>
c6d039a3 18.P
cfa4c15e
AC
19.BI "char *strncat(char *restrict " dst ", const char " src "[restrict ." ssize ],
20.BI " size_t " ssize );
5c6f6e67
AC
21.fi
22.SH DESCRIPTION
9f3c55b0
AC
23This function appends at most
24.I ssize
25non-null bytes from the array pointed to by
0f22dfb6
AC
26.IR src ,
27followed by a null character,
9f3c55b0 28to the end of the string pointed to by
5c6f6e67 29.IR dst .
3f8d7b04
AC
30.I dst
31must point to a string contained in a buffer that is large enough,
32that is, the buffer size must be at least
cfa4c15e 33.IR "strlen(dst) + strnlen(src, ssize) + 1" .
c6d039a3 34.P
2c5503c6 35An implementation of this function might be:
c6d039a3 36.P
5c6f6e67
AC
37.in +4n
38.EX
39char *
cfa4c15e 40strncat(char *restrict dst, const char *restrict src, size_t ssize)
5c6f6e67 41{
1f597745 42 #define strnul(s) (s + strlen(s))
fe5dba13 43\&
9aeebbde
AC
44 stpcpy(mempcpy(strnul(dst), src, strnlen(src, ssize)), "");
45 return dst;
5c6f6e67
AC
46}
47.EE
48.in
49.SH RETURN VALUE
50.BR strncat ()
2c5503c6
AC
51returns
52.IR dst .
5c6f6e67
AC
53.SH ATTRIBUTES
54For an explanation of the terms used in this section, see
55.BR attributes (7).
5c6f6e67
AC
56.TS
57allbox;
58lbx lb lb
59l l l.
60Interface Attribute Value
61T{
9e54434e
BR
62.na
63.nh
5c6f6e67
AC
64.BR strncat ()
65T} Thread safety MT-Safe
66.TE
5c6f6e67 67.SH STANDARDS
4131356c
AC
68C11, POSIX.1-2008.
69.SH HISTORY
70POSIX.1-2001, C89, SVr4, 4.3BSD.
a62b871e 71.SH CAVEATS
41125e32
AC
72The name of this function is confusing;
73it has no relation to
5c6f6e67 74.BR strncpy (3).
c6d039a3 75.P
3f8d7b04
AC
76If the destination buffer does not already contain a string,
77or is not large enough,
2c5503c6
AC
78the behavior is undefined.
79See
80.B _FORTIFY_SOURCE
81in
82.BR feature_test_macros (7).
83.SH BUGS
84This function can be very inefficient.
85Read about
86.UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
bd3c799e 87Shlemiel the painter
2c5503c6 88.UE .
fd2c7832 89.SH EXAMPLES
2c5503c6 90.\" SRC BEGIN (strncat.c)
fd2c7832 91.EX
66874ae1 92#include <err.h>
fd2c7832
AC
93#include <stdio.h>
94#include <stdlib.h>
95#include <string.h>
fe5dba13 96\&
fd2c7832 97#define nitems(arr) (sizeof((arr)) / sizeof((arr)[0]))
fe5dba13 98\&
fd2c7832
AC
99int
100main(void)
101{
ab40ee7c 102 size_t n;
fe5dba13 103\&
0866a774 104 // Null-padded fixed-size character sequences
2c5503c6
AC
105 char pre[4] = "pre.";
106 char new_post[50] = ".foo.bar";
fe5dba13 107\&
2c5503c6
AC
108 // Strings
109 char post[] = ".post";
110 char src[] = "some_long_body.post";
111 char *dest;
fe5dba13 112\&
ab40ee7c
AC
113 n = nitems(pre) + strlen(src) \- strlen(post) + nitems(new_post) + 1;
114 dest = malloc(sizeof(*dest) * n);
66874ae1
AC
115 if (dest == NULL)
116 err(EXIT_FAILURE, "malloc()");
fe5dba13 117\&
b957f81f 118 dest[0] = \[aq]\e0\[aq]; // There's no 'cpy' function to this 'cat'.
fd2c7832
AC
119 strncat(dest, pre, nitems(pre));
120 strncat(dest, src, strlen(src) \- strlen(post));
2c5503c6 121 strncat(dest, new_post, nitems(new_post));
fe5dba13 122\&
2c5503c6
AC
123 puts(dest); // "pre.some_long_body.foo.bar"
124 free(dest);
fd2c7832
AC
125 exit(EXIT_SUCCESS);
126}
127.EE
128.\" SRC END
129.in
5c6f6e67 130.SH SEE ALSO
2c5503c6 131.BR string (3),
16c9d44e 132.BR string_copying (7)