man-pages/man3/strcpy.3.html
2021-03-31 01:06:50 +01:00

344 lines
7.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of STRCPY</TITLE>
</HEAD><BODY>
<H1>STRCPY</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
strcpy, strncpy - copy a string
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;</B>
<B>char *strcpy(char *</B><I>dest</I><B>, const char *</B><I>src</I><B>);</B>
<B>char *strncpy(char *</B><I>dest</I><B>, const char *</B><I>src</I><B>, size_t </B><I>n</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>strcpy</B>()
function copies the string pointed to by
<I>src</I>,
including the terminating null byte ('\0'),
to the buffer pointed to by
<I>dest</I>.
The strings may not overlap, and the destination string
<I>dest</I>
must be large enough to receive the copy.
<I>Beware of buffer overruns!</I>
(See BUGS.)
<P>
The
<B>strncpy</B>()
function is similar, except that at most
<I>n</I>
bytes of
<I>src</I>
are copied.
<B>Warning</B>:
If there is no null byte
among the first
<I>n</I>
bytes of
<I>src</I>,
the string placed in
<I>dest</I>
will not be null-terminated.
<P>
If the length of
<I>src</I>
is less than
<I>n</I>,
<B>strncpy</B>()
writes additional null bytes to
<I>dest</I>
to ensure that a total of
<I>n</I>
bytes are written.
<P>
A simple implementation of
<B>strncpy</B>()
might be:
<P>
char *
strncpy(char *dest, const char *src, size_t n)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;i;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;n&nbsp;&amp;&amp;&nbsp;src[i]&nbsp;!=&nbsp;'\0';&nbsp;i++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest[i]&nbsp;=&nbsp;src[i];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(&nbsp;;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest[i]&nbsp;=&nbsp;'\0';
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;dest;
}
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>strcpy</B>()
and
<B>strncpy</B>()
functions return a pointer to
the destination string
<I>dest</I>.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>strcpy</B>(),
<B>strncpy</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
Some programmers consider
<B>strncpy</B>()
to be inefficient and error prone.
If the programmer knows (i.e., includes code to test!)
that the size of
<I>dest</I>
is greater than
the length of
<I>src</I>,
then
<B>strcpy</B>()
can be used.
<P>
One valid (and intended) use of
<B>strncpy</B>()
is to copy a C string to a fixed-length buffer
while ensuring both that the buffer is not overflowed
and that unused bytes in the destination buffer are zeroed out
(perhaps to prevent information leaks if the buffer is to be
written to media or transmitted to another process via an
interprocess communication technique).
<P>
If there is no terminating null byte in the first
<I>n</I>
bytes of
<I>src</I>,
<B>strncpy</B>()
produces an unterminated string in
<I>dest</I>.
If
<I>buf</I>
has length
<I>buflen</I>,
you can force termination using something like the following:
<P>
if (buflen &gt; 0) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strncpy(buf,&nbsp;str,&nbsp;buflen&nbsp;-&nbsp;1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;buf[buflen&nbsp;-&nbsp;1]=&nbsp;'\0';
}
<P>
(Of course, the above technique ignores the fact that, if
<I>src</I>
contains more than
<I>buflen&nbsp;-&nbsp;1</I>
bytes, information is lost in the copying to
<I>dest</I>.)
<A NAME="lbAI">&nbsp;</A>
<H3>strlcpy()</H3>
Some systems (the BSDs, Solaris, and others) provide the following function:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;strlcpy(char&nbsp;*dest,&nbsp;const&nbsp;char&nbsp;*src,&nbsp;size_t&nbsp;size);
<P>
This function is similar to
<B>strncpy</B>(),
but it copies at most
<I>size-1</I>
bytes to
<I>dest</I>,
always adds a terminating null byte,
and does not pad the destination with (further) null bytes.
This function fixes some of the problems of
<B>strcpy</B>()
and
<B>strncpy</B>(),
but the caller must still handle the possibility of data loss if
<I>size</I>
is too small.
The return value of the function is the length of
<I>src</I>,
which allows truncation to be easily detected:
if the return value is greater than or equal to
<I>size</I>,
truncation occurred.
If loss of data matters, the caller
<I>must</I>
either check the arguments before the call,
or test the function return value.
<B>strlcpy</B>()
is not present in glibc and is not standardized by POSIX,
but is available on Linux via the
<I>libbsd</I>
library.
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
If the destination string of a
<B>strcpy</B>()
is not large enough, then anything might happen.
Overflowing fixed-length string buffers is a favorite cracker technique
for taking complete control of the machine.
Any time a program reads or copies data into a buffer,
the program first needs to check that there's enough space.
This may be unnecessary if you can show that overflow is impossible,
but be careful: programs can get changed over time,
in ways that may make the impossible possible.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+bcopy">bcopy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+memccpy">memccpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+memcpy">memcpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+memmove">memmove</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+stpcpy">stpcpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+stpncpy">stpncpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strdup">strdup</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+string">string</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wcscpy">wcscpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wcsncpy">wcsncpy</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="8"><A HREF="#lbAI">strlcpy()</A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="10"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:58 GMT, March 31, 2021
</BODY>
</HTML>