Debian Codegraph @

upstream/mageia-cauldron/man3/strncat.3

raw
1 '\" t
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH strncat 3 2026-02-10 "Linux man-pages 6.17"
7 .SH NAME
8 strncat
9 \-
10 append non-null bytes from a source array to a string,
11 and null-terminate the result
12 .SH LIBRARY
13 Standard C library
14 .RI ( libc ,\~ \-lc )
15 .SH SYNOPSIS
16 .nf
17 .B #include <string.h>
18 .P
19 .BR "char *strncat(" "size_t ssize;"
20 .BI " char *restrict " dst ", const char " src "[restrict " ssize ],
21 .BI " size_t " ssize );
22 .fi
23 .SH DESCRIPTION
24 This function appends at most
25 .I ssize
26 non-null bytes from the array pointed to by
27 .IR src ,
28 followed by a null character,
29 to the end of the string pointed to by
30 .IR dst .
31 .I dst
32 must point to a string contained in a buffer that is large enough,
33 that is, the buffer size must be at least
34 .IR "strlen(dst) + strnlen(src, ssize) + 1" .
35 .P
36 An implementation of this function might be:
37 .P
38 .in +4n
39 .EX
40 char *
41 strncat(char *restrict dst, const char *restrict src, size_t ssize)
42 {
43 #define strnul(s) (s + strlen(s))
44 \&
45 stpcpy(mempcpy(strnul(dst), src, strnlen(src, ssize)), "");
46 return dst;
47 }
48 .EE
49 .in
50 .SH RETURN VALUE
51 .BR strncat ()
52 returns
53 .IR dst .
54 .SH ATTRIBUTES
55 For an explanation of the terms used in this section, see
56 .BR attributes (7).
57 .TS
58 allbox;
59 lbx lb lb
60 l l l.
61 Interface Attribute Value
62 T{
63 .na
64 .nh
65 .BR strncat ()
66 T} Thread safety MT-Safe
67 .TE
68 .SH STANDARDS
69 C11, POSIX.1-2008.
70 .SH HISTORY
71 POSIX.1-2001, C89, SVr4, 4.3BSD.
72 .SH CAVEATS
73 The name of this function is confusing;
74 it has no relation to
75 .BR strncpy (3).
76 .P
77 If the destination buffer does not already contain a string,
78 or is not large enough,
79 the behavior is undefined.
80 See
81 .B _FORTIFY_SOURCE
82 in
83 .BR feature_test_macros (7).
84 .SH BUGS
85 This function can be very inefficient.
86 Read about
87 .UR https:\://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
88 Shlemiel the painter
89 .UE .
90 .SH EXAMPLES
91 .\" SRC BEGIN (strncat.c)
92 .EX
93 #include <stdcountof.h>
94 #include <stdio.h>
95 #include <string.h>
96 #include <utmp.h>
97 \&
98 void print_ut_user(struct utmp *ut);
99 \&
100 void
101 print_ut_user(struct utmp *ut)
102 {
103 char buf[countof(ut\->ut_user) + 1];
104 \&
105 strcpy(buf, "");
106 strncat(buf, ut\->ut_user, countof(ut\->ut_user));
107 puts(buf);
108 }
109 .EE
110 .\" SRC END
111 .in
112 .SH SEE ALSO
113 .BR string (3),
114 .BR string_copying (7)