xref: /minix3/lib/libc/string/strcpy.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
12fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
22fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
32fe8fb19SBen Gras.\"
42fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
52fe8fb19SBen Gras.\" Chris Torek and the American National Standards Committee X3,
62fe8fb19SBen Gras.\" on Information Processing Systems.
72fe8fb19SBen Gras.\"
82fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
102fe8fb19SBen Gras.\" are met:
112fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
172fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
182fe8fb19SBen Gras.\"    without specific prior written permission.
192fe8fb19SBen Gras.\"
202fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
212fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
242fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302fe8fb19SBen Gras.\" SUCH DAMAGE.
312fe8fb19SBen Gras.\"
322fe8fb19SBen Gras.\"     from: @(#)strcpy.3	8.1 (Berkeley) 6/4/93
33*0a6a1f1dSLionel Sambuc.\"	$NetBSD: strcpy.3,v 1.23 2015/04/01 20:18:17 riastradh Exp $
342fe8fb19SBen Gras.\"
35*0a6a1f1dSLionel Sambuc.Dd April 1, 2015
362fe8fb19SBen Gras.Dt STRCPY 3
372fe8fb19SBen Gras.Os
382fe8fb19SBen Gras.Sh NAME
392fe8fb19SBen Gras.Nm stpcpy ,
402fe8fb19SBen Gras.Nm stpncpy ,
412fe8fb19SBen Gras.Nm strcpy ,
422fe8fb19SBen Gras.Nm strncpy
432fe8fb19SBen Gras.Nd copy strings
442fe8fb19SBen Gras.Sh LIBRARY
452fe8fb19SBen Gras.Lb libc
462fe8fb19SBen Gras.Sh SYNOPSIS
472fe8fb19SBen Gras.In string.h
482fe8fb19SBen Gras.Ft char *
492fe8fb19SBen Gras.Fn stpcpy "char * restrict dst" "const char * restrict src"
502fe8fb19SBen Gras.Ft char *
512fe8fb19SBen Gras.Fn stpncpy "char * restrict dst" "const char * restrict src" "size_t len"
522fe8fb19SBen Gras.Ft char *
532fe8fb19SBen Gras.Fn strcpy "char * restrict dst" "const char * restrict src"
542fe8fb19SBen Gras.Ft char *
552fe8fb19SBen Gras.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len"
562fe8fb19SBen Gras.Sh DESCRIPTION
572fe8fb19SBen GrasThe
582fe8fb19SBen Gras.Fn stpcpy
592fe8fb19SBen Grasand
602fe8fb19SBen Gras.Fn strcpy
612fe8fb19SBen Grasfunctions
622fe8fb19SBen Grascopy the string
632fe8fb19SBen Gras.Fa src
642fe8fb19SBen Grasto
652fe8fb19SBen Gras.Fa dst
662fe8fb19SBen Gras(including the terminating
672fe8fb19SBen Gras.Ql \e0
682fe8fb19SBen Grascharacter).
692fe8fb19SBen Gras.Pp
702fe8fb19SBen GrasThe
712fe8fb19SBen Gras.Fn stpncpy
722fe8fb19SBen Grasand
732fe8fb19SBen Gras.Fn strncpy
742fe8fb19SBen Grasfunctions copy at most
752fe8fb19SBen Gras.Fa len
762fe8fb19SBen Grascharacters from
772fe8fb19SBen Gras.Fa src
782fe8fb19SBen Grasinto
792fe8fb19SBen Gras.Fa dst .
802fe8fb19SBen GrasIf
812fe8fb19SBen Gras.Fa src
822fe8fb19SBen Grasis less than
832fe8fb19SBen Gras.Fa len
842fe8fb19SBen Grascharacters long,
852fe8fb19SBen Grasthe remainder of
862fe8fb19SBen Gras.Fa dst
872fe8fb19SBen Grasis filled with
882fe8fb19SBen Gras.Ql \e0
892fe8fb19SBen Grascharacters.
902fe8fb19SBen GrasOtherwise,
912fe8fb19SBen Gras.Fa dst
922fe8fb19SBen Grasis
932fe8fb19SBen Gras.Em not
942fe8fb19SBen Grasterminated.
95*0a6a1f1dSLionel Sambuc.Pp
96*0a6a1f1dSLionel SambucThe strings
97*0a6a1f1dSLionel Sambuc.Fa src
98*0a6a1f1dSLionel Sambucand
99*0a6a1f1dSLionel Sambuc.Fa dst
100*0a6a1f1dSLionel Sambucmay not overlap.
1012fe8fb19SBen Gras.Sh RETURN VALUES
1022fe8fb19SBen GrasThe
1032fe8fb19SBen Gras.Fn strcpy
1042fe8fb19SBen Grasand
1052fe8fb19SBen Gras.Fn strncpy
1062fe8fb19SBen Grasfunctions
1072fe8fb19SBen Grasreturn
1082fe8fb19SBen Gras.Fa dst .
1092fe8fb19SBen GrasThe
1102fe8fb19SBen Gras.Fn stpcpy
1112fe8fb19SBen Grasand
1122fe8fb19SBen Gras.Fn stpncpy
1132fe8fb19SBen Grasfunctions return a pointer to the terminating
1142fe8fb19SBen Gras.Ql \e0
1152fe8fb19SBen Grascharacter of
1162fe8fb19SBen Gras.Fa dst .
1172fe8fb19SBen GrasIf
1182fe8fb19SBen Gras.Fn stpncpy
1192fe8fb19SBen Grasdoes not terminate
1202fe8fb19SBen Gras.Fa dst
1212fe8fb19SBen Graswith a
1222fe8fb19SBen Gras.Dv NUL
1232fe8fb19SBen Grascharacter, it instead returns a pointer to
1242fe8fb19SBen Gras.Li dst[len]
1252fe8fb19SBen Gras(which does not necessarily refer to a valid memory location.)
1262fe8fb19SBen Gras.Sh EXAMPLES
1272fe8fb19SBen GrasThe following sets
1282fe8fb19SBen Gras.Va chararray
1292fe8fb19SBen Grasto
1302fe8fb19SBen Gras.Dq Li abc\e0\e0\e0 :
1312fe8fb19SBen Gras.Bd -literal -offset indent
1322fe8fb19SBen Graschar chararray[6];
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras(void)strncpy(chararray, "abc", sizeof(chararray));
1352fe8fb19SBen Gras.Ed
1362fe8fb19SBen Gras.Pp
1372fe8fb19SBen GrasThe following sets
1382fe8fb19SBen Gras.Va chararray
1392fe8fb19SBen Grasto
1402fe8fb19SBen Gras.Dq Li abcdef :
1412fe8fb19SBen Gras.Bd -literal -offset indent
1422fe8fb19SBen Graschar chararray[6];
1432fe8fb19SBen Gras
1442fe8fb19SBen Gras(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
1452fe8fb19SBen Gras.Ed
1462fe8fb19SBen Gras.Pp
1472fe8fb19SBen GrasNote that it does
1482fe8fb19SBen Gras.Em not
1492fe8fb19SBen Gras.Dv NUL Ns No -terminate
1502fe8fb19SBen Gras.Va chararray
1512fe8fb19SBen Grasbecause the length of the source string is greater than or equal
1522fe8fb19SBen Grasto the length parameter.
1532fe8fb19SBen Gras.Fn strncpy
1542fe8fb19SBen Gras.Em only
1552fe8fb19SBen Gras.Dv NUL Ns No -terminates
1562fe8fb19SBen Grasthe destination string when the length of the source
1572fe8fb19SBen Grasstring is less than the length parameter.
1582fe8fb19SBen Gras.Pp
1592fe8fb19SBen GrasThe following copies as many characters from
1602fe8fb19SBen Gras.Va input
1612fe8fb19SBen Grasto
1622fe8fb19SBen Gras.Va buf
1632fe8fb19SBen Grasas will fit and
1642fe8fb19SBen Gras.Dv NUL Ns No -terminates
1652fe8fb19SBen Grasthe result.
1662fe8fb19SBen GrasBecause
1672fe8fb19SBen Gras.Fn strncpy
1682fe8fb19SBen Grasdoes
1692fe8fb19SBen Gras.Em not
1702fe8fb19SBen Grasguarantee to
1712fe8fb19SBen Gras.Dv NUL Ns No -terminate
1722fe8fb19SBen Grasthe string itself, this must be done explicitly.
1732fe8fb19SBen Gras.Bd -literal -offset indent
1742fe8fb19SBen Graschar buf[1024];
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras(void)strncpy(buf, input, sizeof(buf) - 1);
1772fe8fb19SBen Grasbuf[sizeof(buf) - 1] = '\e0';
1782fe8fb19SBen Gras.Ed
1792fe8fb19SBen Gras.Pp
1802fe8fb19SBen GrasThis could be better and more simply achieved using
1812fe8fb19SBen Gras.Xr strlcpy 3 ,
1822fe8fb19SBen Grasas shown in the following example:
1832fe8fb19SBen Gras.Bd -literal -offset indent
1842fe8fb19SBen Gras(void)strlcpy(buf, input, sizeof(buf));
1852fe8fb19SBen Gras.Ed
1862fe8fb19SBen Gras.Pp
1872fe8fb19SBen GrasNote that because
1882fe8fb19SBen Gras.Xr strlcpy 3
1892fe8fb19SBen Grasis not defined in any standards, it should
1902fe8fb19SBen Grasonly be used when portability is not a concern.
1912fe8fb19SBen Gras.Sh SEE ALSO
1922fe8fb19SBen Gras.Xr bcopy 3 ,
1932fe8fb19SBen Gras.Xr memccpy 3 ,
1942fe8fb19SBen Gras.Xr memcpy 3 ,
1952fe8fb19SBen Gras.Xr memmove 3 ,
1962fe8fb19SBen Gras.Xr strlcpy 3 ,
1972fe8fb19SBen Gras.Xr wcscpy 3
1982fe8fb19SBen Gras.Sh STANDARDS
1992fe8fb19SBen GrasThe
2002fe8fb19SBen Gras.Fn strcpy
2012fe8fb19SBen Grasand
2022fe8fb19SBen Gras.Fn strncpy
2032fe8fb19SBen Grasfunctions
2042fe8fb19SBen Grasconform to
2052fe8fb19SBen Gras.St -isoC-99 .
2062fe8fb19SBen GrasThe
2072fe8fb19SBen Gras.Fn stpcpy
2082fe8fb19SBen Grasand
2092fe8fb19SBen Gras.Fn stpncpy
2102fe8fb19SBen Grasfunctions conform to
2112fe8fb19SBen Gras.St -p1003.1-2008 .
2122fe8fb19SBen Gras.Sh HISTORY
2132fe8fb19SBen GrasThe
2142fe8fb19SBen Gras.Fn stpcpy
2152fe8fb19SBen Grasand
2162fe8fb19SBen Gras.Fn stpncpy
2172fe8fb19SBen Grasfunctions first appeared in
2182fe8fb19SBen Gras.Nx 6.0 .
2192fe8fb19SBen Gras.Sh SECURITY CONSIDERATIONS
2202fe8fb19SBen GrasThe
2212fe8fb19SBen Gras.Fn strcpy
2222fe8fb19SBen Grasand
2232fe8fb19SBen Gras.Fn stpcpy
2242fe8fb19SBen Grasfunctions are easily misused in a manner which enables malicious users
2252fe8fb19SBen Grasto arbitrarily change a running program's functionality through a
2262fe8fb19SBen Grasbuffer overflow attack.
227