]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/strcpy.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / strcpy.3
CommitLineData
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 8stpcpy, strcpy, strcat \- copy or catenate a string
3868603c
AC
9.SH LIBRARY
10Standard 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
22Feature 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
38These functions copy the string pointed to by
39.IR src ,
40into a string
41at the buffer pointed to by
42.IR dst .
43The programmer is responsible for allocating a destination buffer large enough,
44that is,
45.IR "strlen(src) + 1" .
46For the difference between the two functions, see RETURN VALUE.
47.TP
48.BR strcat ()
49This function catenates the string pointed to by
46d8df8e 50.IR src ,
c2e23f71
AC
51after the string pointed to by
52.I dst
53(overwriting its terminating null byte).
54The programmer is responsible for allocating a destination buffer large enough,
55that is,
56.IR "strlen(dst) + strlen(src) + 1" .
c6d039a3 57.P
c2e23f71 58An implementation of these functions might be:
c6d039a3 59.P
c2e23f71
AC
60.in +4n
61.EX
62char *
63stpcpy(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
73char *
74strcpy(char *restrict dst, const char *restrict src)
75{
76 stpcpy(dst, src);
77 return dst;
78}
fe5dba13 79\&
c2e23f71
AC
80char *
81strcat(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 ()
91This function returns
92a pointer to the terminating null byte of the copied string.
93.TP
60a90ecd 94.BR strcpy ()
c2e23f71
AC
95.TQ
96.BR strcat ()
97These functions return
98.IR dst .
21b49240 99.SH ATTRIBUTES
5d876bea
PH
100For an explanation of the terms used in this section, see
101.BR attributes (7).
102.TS
103allbox;
c466875e 104lbx lb lb
5d876bea
PH
105l l l.
106Interface Attribute Value
107T{
9e54434e
BR
108.na
109.nh
c2e23f71
AC
110.BR stpcpy (),
111.BR strcpy (),
112.BR strcat ()
5d876bea
PH
113T} Thread safety MT-Safe
114.TE
3113c7f3 115.SH STANDARDS
c2e23f71
AC
116.TP
117.BR stpcpy ()
118POSIX.1-2008.
119.TP
120.BR strcpy ()
121.TQ
122.BR strcat ()
4131356c
AC
123C11, POSIX.1-2008.
124.SH STANDARDS
125.TP
126.BR stpcpy ()
127POSIX.1-2008.
128.TP
129.BR strcpy ()
130.TQ
131.BR strcat ()
132POSIX.1-2001, C89, SVr4, 4.3BSD.
c2e23f71
AC
133.SH CAVEATS
134The strings
135.I src
136and
137.I dst
138may not overlap.
c6d039a3 139.P
c2e23f71
AC
140If the destination buffer is not large enough,
141the behavior is undefined.
142See
143.B _FORTIFY_SOURCE
144in
145.BR feature_test_macros (7).
c6d039a3 146.P
c2e23f71
AC
147.BR strcat ()
148can be very inefficient.
149Read about
150.UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
bd3c799e 151Shlemiel 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
161int
162main(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)