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