]>
Commit | Line | Data |
---|---|---|
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 |
8 | strncat |
9 | \- | |
10 | append non-null bytes from a source array to a string, | |
11 | and null-terminate the result | |
5c6f6e67 AC |
12 | .SH LIBRARY |
13 | Standard 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 |
23 | This function appends at most |
24 | .I ssize | |
25 | non-null bytes from the array pointed to by | |
0f22dfb6 AC |
26 | .IR src , |
27 | followed by a null character, | |
9f3c55b0 | 28 | to the end of the string pointed to by |
5c6f6e67 | 29 | .IR dst . |
3f8d7b04 AC |
30 | .I dst |
31 | must point to a string contained in a buffer that is large enough, | |
32 | that is, the buffer size must be at least | |
cfa4c15e | 33 | .IR "strlen(dst) + strnlen(src, ssize) + 1" . |
c6d039a3 | 34 | .P |
2c5503c6 | 35 | An implementation of this function might be: |
c6d039a3 | 36 | .P |
5c6f6e67 AC |
37 | .in +4n |
38 | .EX | |
39 | char * | |
cfa4c15e | 40 | strncat(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 |
51 | returns |
52 | .IR dst . | |
5c6f6e67 AC |
53 | .SH ATTRIBUTES |
54 | For an explanation of the terms used in this section, see | |
55 | .BR attributes (7). | |
5c6f6e67 AC |
56 | .TS |
57 | allbox; | |
58 | lbx lb lb | |
59 | l l l. | |
60 | Interface Attribute Value | |
61 | T{ | |
9e54434e BR |
62 | .na |
63 | .nh | |
5c6f6e67 AC |
64 | .BR strncat () |
65 | T} Thread safety MT-Safe | |
66 | .TE | |
5c6f6e67 | 67 | .SH STANDARDS |
4131356c AC |
68 | C11, POSIX.1-2008. |
69 | .SH HISTORY | |
70 | POSIX.1-2001, C89, SVr4, 4.3BSD. | |
a62b871e | 71 | .SH CAVEATS |
41125e32 AC |
72 | The name of this function is confusing; |
73 | it has no relation to | |
5c6f6e67 | 74 | .BR strncpy (3). |
c6d039a3 | 75 | .P |
3f8d7b04 AC |
76 | If the destination buffer does not already contain a string, |
77 | or is not large enough, | |
2c5503c6 AC |
78 | the behavior is undefined. |
79 | See | |
80 | .B _FORTIFY_SOURCE | |
81 | in | |
82 | .BR feature_test_macros (7). | |
83 | .SH BUGS | |
84 | This function can be very inefficient. | |
85 | Read about | |
86 | .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/ | |
bd3c799e | 87 | Shlemiel 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 |
99 | int |
100 | main(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) |