]>
Commit | Line | Data |
---|---|---|
a1eaacb1 | 1 | '\" t |
c2e23f71 | 2 | .\" Copyright 2022 Alejandro Colomar <alx@kernel.org> |
fea681da | 3 | .\" |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da | 5 | .\" |
4c1c5274 | 6 | .TH strcpy 3 (date) "Linux man-pages (unreleased)" |
fea681da | 7 | .SH NAME |
c2e23f71 | 8 | stpcpy, strcpy, strcat \- copy or catenate a string |
3868603c AC |
9 | .SH LIBRARY |
10 | Standard C library | |
8fc3b2cf | 11 | .RI ( libc ", " \-lc ) |
fea681da MK |
12 | .SH SYNOPSIS |
13 | .nf | |
14 | .B #include <string.h> | |
c6d039a3 | 15 | .P |
c2e23f71 AC |
16 | .BI "char *stpcpy(char *restrict " dst ", const char *restrict " src ); |
17 | .BI "char *strcpy(char *restrict " dst ", const char *restrict " src ); | |
18 | .BI "char *strcat(char *restrict " dst ", const char *restrict " src ); | |
19 | .fi | |
c6d039a3 | 20 | .P |
c2e23f71 AC |
21 | .RS -4 |
22 | Feature Test Macro Requirements for glibc (see | |
23 | .BR feature_test_macros (7)): | |
24 | .RE | |
c6d039a3 | 25 | .P |
c2e23f71 AC |
26 | .BR stpcpy (): |
27 | .nf | |
28 | Since glibc 2.10: | |
29 | _POSIX_C_SOURCE >= 200809L | |
30 | Before glibc 2.10: | |
31 | _GNU_SOURCE | |
fea681da MK |
32 | .fi |
33 | .SH DESCRIPTION | |
c2e23f71 AC |
34 | .TP |
35 | .BR stpcpy () | |
36 | .TQ | |
60a90ecd | 37 | .BR strcpy () |
c2e23f71 AC |
38 | These functions copy the string pointed to by |
39 | .IR src , | |
40 | into a string | |
41 | at the buffer pointed to by | |
42 | .IR dst . | |
43 | The programmer is responsible for allocating a destination buffer large enough, | |
44 | that is, | |
45 | .IR "strlen(src) + 1" . | |
46 | For the difference between the two functions, see RETURN VALUE. | |
47 | .TP | |
48 | .BR strcat () | |
49 | This function catenates the string pointed to by | |
46d8df8e | 50 | .IR src , |
c2e23f71 AC |
51 | after the string pointed to by |
52 | .I dst | |
53 | (overwriting its terminating null byte). | |
54 | The programmer is responsible for allocating a destination buffer large enough, | |
55 | that is, | |
56 | .IR "strlen(dst) + strlen(src) + 1" . | |
c6d039a3 | 57 | .P |
c2e23f71 | 58 | An implementation of these functions might be: |
c6d039a3 | 59 | .P |
c2e23f71 AC |
60 | .in +4n |
61 | .EX | |
62 | char * | |
63 | stpcpy(char *restrict dst, const char *restrict src) | |
64 | { | |
65 | char *p; | |
fe5dba13 | 66 | \& |
c2e23f71 | 67 | p = mempcpy(dst, src, strlen(src)); |
b957f81f | 68 | *p = \[aq]\e0\[aq]; |
fe5dba13 | 69 | \& |
c2e23f71 AC |
70 | return p; |
71 | } | |
fe5dba13 | 72 | \& |
c2e23f71 AC |
73 | char * |
74 | strcpy(char *restrict dst, const char *restrict src) | |
75 | { | |
76 | stpcpy(dst, src); | |
77 | return dst; | |
78 | } | |
fe5dba13 | 79 | \& |
c2e23f71 AC |
80 | char * |
81 | strcat(char *restrict dst, const char *restrict src) | |
82 | { | |
83 | stpcpy(dst + strlen(dst), src); | |
84 | return dst; | |
85 | } | |
86 | .EE | |
87 | .in | |
47297adb | 88 | .SH RETURN VALUE |
c2e23f71 AC |
89 | .TP |
90 | .BR stpcpy () | |
91 | This function returns | |
92 | a pointer to the terminating null byte of the copied string. | |
93 | .TP | |
60a90ecd | 94 | .BR strcpy () |
c2e23f71 AC |
95 | .TQ |
96 | .BR strcat () | |
97 | These functions return | |
98 | .IR dst . | |
21b49240 | 99 | .SH ATTRIBUTES |
5d876bea PH |
100 | For an explanation of the terms used in this section, see |
101 | .BR attributes (7). | |
102 | .TS | |
103 | allbox; | |
c466875e | 104 | lbx lb lb |
5d876bea PH |
105 | l l l. |
106 | Interface Attribute Value | |
107 | T{ | |
9e54434e BR |
108 | .na |
109 | .nh | |
c2e23f71 AC |
110 | .BR stpcpy (), |
111 | .BR strcpy (), | |
112 | .BR strcat () | |
5d876bea PH |
113 | T} Thread safety MT-Safe |
114 | .TE | |
3113c7f3 | 115 | .SH STANDARDS |
c2e23f71 AC |
116 | .TP |
117 | .BR stpcpy () | |
118 | POSIX.1-2008. | |
119 | .TP | |
120 | .BR strcpy () | |
121 | .TQ | |
122 | .BR strcat () | |
4131356c AC |
123 | C11, POSIX.1-2008. |
124 | .SH STANDARDS | |
125 | .TP | |
126 | .BR stpcpy () | |
127 | POSIX.1-2008. | |
128 | .TP | |
129 | .BR strcpy () | |
130 | .TQ | |
131 | .BR strcat () | |
132 | POSIX.1-2001, C89, SVr4, 4.3BSD. | |
c2e23f71 AC |
133 | .SH CAVEATS |
134 | The strings | |
135 | .I src | |
136 | and | |
137 | .I dst | |
138 | may not overlap. | |
c6d039a3 | 139 | .P |
c2e23f71 AC |
140 | If the destination buffer is not large enough, |
141 | the behavior is undefined. | |
142 | See | |
143 | .B _FORTIFY_SOURCE | |
144 | in | |
145 | .BR feature_test_macros (7). | |
c6d039a3 | 146 | .P |
c2e23f71 AC |
147 | .BR strcat () |
148 | can be very inefficient. | |
149 | Read about | |
150 | .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/ | |
bd3c799e | 151 | Shlemiel the painter |
c2e23f71 AC |
152 | .UE . |
153 | .SH EXAMPLES | |
154 | .\" SRC BEGIN (strcpy.c) | |
1ae6b2c7 | 155 | .EX |
8a757718 | 156 | #include <err.h> |
c2e23f71 AC |
157 | #include <stdio.h> |
158 | #include <stdlib.h> | |
159 | #include <string.h> | |
fe5dba13 | 160 | \& |
c2e23f71 AC |
161 | int |
162 | main(void) | |
163 | { | |
164 | char *p; | |
165 | char *buf1; | |
166 | char *buf2; | |
167 | size_t len, maxsize; | |
fe5dba13 | 168 | \& |
c2e23f71 AC |
169 | maxsize = strlen("Hello ") + strlen("world") + strlen("!") + 1; |
170 | buf1 = malloc(sizeof(*buf1) * maxsize); | |
8a757718 AC |
171 | if (buf1 == NULL) |
172 | err(EXIT_FAILURE, "malloc()"); | |
c2e23f71 | 173 | buf2 = malloc(sizeof(*buf2) * maxsize); |
8a757718 AC |
174 | if (buf2 == NULL) |
175 | err(EXIT_FAILURE, "malloc()"); | |
fe5dba13 | 176 | \& |
c2e23f71 AC |
177 | p = buf1; |
178 | p = stpcpy(p, "Hello "); | |
179 | p = stpcpy(p, "world"); | |
180 | p = stpcpy(p, "!"); | |
181 | len = p \- buf1; | |
fe5dba13 | 182 | \& |
c2e23f71 AC |
183 | printf("[len = %zu]: ", len); |
184 | puts(buf1); // "Hello world!" | |
185 | free(buf1); | |
fe5dba13 | 186 | \& |
c2e23f71 AC |
187 | strcpy(buf2, "Hello "); |
188 | strcat(buf2, "world"); | |
189 | strcat(buf2, "!"); | |
190 | len = strlen(buf2); | |
fe5dba13 | 191 | \& |
c2e23f71 AC |
192 | printf("[len = %zu]: ", len); |
193 | puts(buf2); // "Hello world!" | |
194 | free(buf2); | |
fe5dba13 | 195 | \& |
c2e23f71 AC |
196 | exit(EXIT_SUCCESS); |
197 | } | |
1ae6b2c7 | 198 | .EE |
c2e23f71 | 199 | .\" SRC END |
47297adb | 200 | .SH SEE ALSO |
c7a20d46 | 201 | .BR strdup (3), |
3e5c319e | 202 | .BR string (3), |
c2e23f71 AC |
203 | .BR wcscpy (3), |
204 | .BR string_copying (7) |