xref: /freebsd/lib/libc/string/strcpy.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
558f0484fSRodney W. Grimes.\" Chris Torek and the American National Standards Committee X3,
658f0484fSRodney W. Grimes.\" on Information Processing Systems.
758f0484fSRodney W. Grimes.\"
858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes.\" are met:
1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
163fb3b97cSEd Maste.\" 3. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
32*b7e471c2SConrad Meyer.Dd June 6, 2018
3358f0484fSRodney W. Grimes.Dt STRCPY 3
34a307d598SRuslan Ermilov.Os
3558f0484fSRodney W. Grimes.Sh NAME
36604f1c41SEdward Tomasz Napierala.Nm stpcpy ,
37604f1c41SEdward Tomasz Napierala.Nm stpncpy ,
38604f1c41SEdward Tomasz Napierala.Nm strcpy ,
39604f1c41SEdward Tomasz Napierala.Nm strncpy
4058f0484fSRodney W. Grimes.Nd copy strings
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4432eef9aeSRuslan Ermilov.In string.h
4558f0484fSRodney W. Grimes.Ft char *
4669099ba2SDavid Schultz.Fn stpcpy "char * restrict dst" "const char * restrict src"
4769099ba2SDavid Schultz.Ft char *
4869099ba2SDavid Schultz.Fn stpncpy "char * restrict dst" "const char * restrict src" "size_t len"
498269e8c8SDavid E. O'Brien.Ft char *
50ad906968SRobert Drehmel.Fn strcpy "char * restrict dst" "const char * restrict src"
5158f0484fSRodney W. Grimes.Ft char *
52ad906968SRobert Drehmel.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len"
5358f0484fSRodney W. Grimes.Sh DESCRIPTION
5458f0484fSRodney W. GrimesThe
5558f0484fSRodney W. Grimes.Fn strcpy
56*b7e471c2SConrad Meyerand
57*b7e471c2SConrad Meyer.Fn stpcpy
5802edf220SDag-Erling Smørgravfunctions
5902edf220SDag-Erling Smørgravcopy the string
6058f0484fSRodney W. Grimes.Fa src
6158f0484fSRodney W. Grimesto
6258f0484fSRodney W. Grimes.Fa dst
6358f0484fSRodney W. Grimes(including the terminating
6458f0484fSRodney W. Grimes.Ql \e0
6502edf220SDag-Erling Smørgravcharacter.)
6658f0484fSRodney W. Grimes.Pp
6758f0484fSRodney W. GrimesThe
6858f0484fSRodney W. Grimes.Fn strncpy
69*b7e471c2SConrad Meyerand
70*b7e471c2SConrad Meyer.Fn stpncpy
7169099ba2SDavid Schultzfunctions copy at most
7258f0484fSRodney W. Grimes.Fa len
73df523897SDima Dorfmancharacters from
74df523897SDima Dorfman.Fa src
75df523897SDima Dorfmaninto
7602edf220SDag-Erling Smørgrav.Fa dst .
77*b7e471c2SConrad Meyer.Bf Sy
7802edf220SDag-Erling SmørgravIf
7958f0484fSRodney W. Grimes.Fa src
8058f0484fSRodney W. Grimesis less than
8158f0484fSRodney W. Grimes.Fa len
8202edf220SDag-Erling Smørgravcharacters long,
8302edf220SDag-Erling Smørgravthe remainder of
8458f0484fSRodney W. Grimes.Fa dst
8502edf220SDag-Erling Smørgravis filled with
8602edf220SDag-Erling Smørgrav.Ql \e0
8702edf220SDag-Erling Smørgravcharacters.
88*b7e471c2SConrad Meyer.Ef
8902edf220SDag-Erling SmørgravOtherwise,
9002edf220SDag-Erling Smørgrav.Fa dst
9102edf220SDag-Erling Smørgravis
9202edf220SDag-Erling Smørgrav.Em not
9302edf220SDag-Erling Smørgravterminated.
94*b7e471c2SConrad Meyer.Pp
95*b7e471c2SConrad MeyerFor all of
96*b7e471c2SConrad Meyer.Fn strcpy ,
97*b7e471c2SConrad Meyer.Fn strncpy ,
98*b7e471c2SConrad Meyer.Fn stpcpy ,
99*b7e471c2SConrad Meyerand
100*b7e471c2SConrad Meyer.Fn stpncpy ,
101*b7e471c2SConrad Meyerthe result is undefined
102*b7e471c2SConrad Meyerif
10350a717a6SWarner Losh.Fa src
10450a717a6SWarner Loshand
10550a717a6SWarner Losh.Fa dst
106*b7e471c2SConrad Meyeroverlap.
10758f0484fSRodney W. Grimes.Sh RETURN VALUES
10858f0484fSRodney W. GrimesThe
10958f0484fSRodney W. Grimes.Fn strcpy
11058f0484fSRodney W. Grimesand
11158f0484fSRodney W. Grimes.Fn strncpy
11258f0484fSRodney W. Grimesfunctions
11358f0484fSRodney W. Grimesreturn
11458f0484fSRodney W. Grimes.Fa dst .
1158269e8c8SDavid E. O'BrienThe
1168269e8c8SDavid E. O'Brien.Fn stpcpy
11769099ba2SDavid Schultzand
11869099ba2SDavid Schultz.Fn stpncpy
11969099ba2SDavid Schultzfunctions return a pointer to the terminating
1208269e8c8SDavid E. O'Brien.Ql \e0
1218269e8c8SDavid E. O'Briencharacter of
1228269e8c8SDavid E. O'Brien.Fa dst .
12369099ba2SDavid SchultzIf
12469099ba2SDavid Schultz.Fn stpncpy
12569099ba2SDavid Schultzdoes not terminate
12669099ba2SDavid Schultz.Fa dst
12769099ba2SDavid Schultzwith a
12869099ba2SDavid Schultz.Dv NUL
12969099ba2SDavid Schultzcharacter, it instead returns a pointer to
13069099ba2SDavid Schultz.Li dst[n]
13169099ba2SDavid Schultz(which does not necessarily refer to a valid memory location.)
13258f0484fSRodney W. Grimes.Sh EXAMPLES
13358f0484fSRodney W. GrimesThe following sets
134107d3f78SMike Barcroft.Va chararray
13558f0484fSRodney W. Grimesto
13658f0484fSRodney W. Grimes.Dq Li abc\e0\e0\e0 :
13758f0484fSRodney W. Grimes.Bd -literal -offset indent
138107d3f78SMike Barcroftchar chararray[6];
139107d3f78SMike Barcroft
140107d3f78SMike Barcroft(void)strncpy(chararray, "abc", sizeof(chararray));
14158f0484fSRodney W. Grimes.Ed
14258f0484fSRodney W. Grimes.Pp
14358f0484fSRodney W. GrimesThe following sets
144107d3f78SMike Barcroft.Va chararray
14558f0484fSRodney W. Grimesto
14658f0484fSRodney W. Grimes.Dq Li abcdef :
14758f0484fSRodney W. Grimes.Bd -literal -offset indent
148107d3f78SMike Barcroftchar chararray[6];
149107d3f78SMike Barcroft
150107d3f78SMike Barcroft(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
15158f0484fSRodney W. Grimes.Ed
152107d3f78SMike Barcroft.Pp
153107d3f78SMike BarcroftNote that it does
154107d3f78SMike Barcroft.Em not
155107d3f78SMike Barcroft.Tn NUL
156107d3f78SMike Barcroftterminate
157107d3f78SMike Barcroft.Va chararray
158107d3f78SMike Barcroftbecause the length of the source string is greater than or equal
1592efeeba5SRuslan Ermilovto the length argument.
160107d3f78SMike Barcroft.Pp
161107d3f78SMike BarcroftThe following copies as many characters from
162107d3f78SMike Barcroft.Va input
163107d3f78SMike Barcroftto
164107d3f78SMike Barcroft.Va buf
165107d3f78SMike Barcroftas will fit and
166107d3f78SMike Barcroft.Tn NUL
167107d3f78SMike Barcroftterminates the result.
168107d3f78SMike BarcroftBecause
169107d3f78SMike Barcroft.Fn strncpy
170107d3f78SMike Barcroftdoes
171107d3f78SMike Barcroft.Em not
172107d3f78SMike Barcroftguarantee to
173107d3f78SMike Barcroft.Tn NUL
174107d3f78SMike Barcroftterminate the string itself, this must be done explicitly.
175107d3f78SMike Barcroft.Bd -literal -offset indent
176107d3f78SMike Barcroftchar buf[1024];
177107d3f78SMike Barcroft
178107d3f78SMike Barcroft(void)strncpy(buf, input, sizeof(buf) - 1);
179107d3f78SMike Barcroftbuf[sizeof(buf) - 1] = '\e0';
180107d3f78SMike Barcroft.Ed
181107d3f78SMike Barcroft.Pp
182107d3f78SMike BarcroftThis could be better achieved using
183107d3f78SMike Barcroft.Xr strlcpy 3 ,
184107d3f78SMike Barcroftas shown in the following example:
185107d3f78SMike Barcroft.Pp
186107d3f78SMike Barcroft.Dl "(void)strlcpy(buf, input, sizeof(buf));"
18758f0484fSRodney W. Grimes.Sh SEE ALSO
18858f0484fSRodney W. Grimes.Xr bcopy 3 ,
18958f0484fSRodney W. Grimes.Xr memccpy 3 ,
19058f0484fSRodney W. Grimes.Xr memcpy 3 ,
1914e2abf95SWarner Losh.Xr memmove 3 ,
1925174a6a2SEdward Tomasz Napierala.Xr strlcpy 3 ,
1935174a6a2SEdward Tomasz Napierala.Xr wcscpy 3
19458f0484fSRodney W. Grimes.Sh STANDARDS
19558f0484fSRodney W. GrimesThe
19658f0484fSRodney W. Grimes.Fn strcpy
19758f0484fSRodney W. Grimesand
19858f0484fSRodney W. Grimes.Fn strncpy
19958f0484fSRodney W. Grimesfunctions
20058f0484fSRodney W. Grimesconform to
201588a200cSRuslan Ermilov.St -isoC .
2028269e8c8SDavid E. O'BrienThe
2038269e8c8SDavid E. O'Brien.Fn stpcpy
20469099ba2SDavid Schultzand
20569099ba2SDavid Schultz.Fn stpncpy
20669099ba2SDavid Schultzfunctions conform to
20769099ba2SDavid Schultz.St -p1003.1-2008 .
2088269e8c8SDavid E. O'Brien.Sh HISTORY
2098269e8c8SDavid E. O'BrienThe
2108269e8c8SDavid E. O'Brien.Fn stpcpy
2118269e8c8SDavid E. O'Brienfunction first appeared in
2128269e8c8SDavid E. O'Brien.Fx 4.4 ,
21369099ba2SDavid Schultzand
21469099ba2SDavid Schultz.Fn stpncpy
21569099ba2SDavid Schultzwas added in
21669099ba2SDavid Schultz.Fx 8.0 .
2170afc94c1SUlrich Spörlein.Sh SECURITY CONSIDERATIONS
218*b7e471c2SConrad MeyerAll of the functions documented in this manual page are easily misused in a
219*b7e471c2SConrad Meyermanner which enables malicious users to arbitrarily change a running program's
220*b7e471c2SConrad Meyerfunctionality through a buffer overflow attack.
221*b7e471c2SConrad Meyer.Pp
222*b7e471c2SConrad MeyerIt is strongly suggested that the
223*b7e471c2SConrad Meyer.Fn strlcpy
224*b7e471c2SConrad Meyerfunction be used in almost all cases.
225*b7e471c2SConrad Meyer.Pp
226*b7e471c2SConrad MeyerFor some, but not all, fixed-length records, non-terminated strings may be both
227*b7e471c2SConrad Meyervalid and desirable.
228*b7e471c2SConrad MeyerIn that specific case, the
229*b7e471c2SConrad Meyer.Fn strncpy
230*b7e471c2SConrad Meyerfunction may be most sensible.
231