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

333 lines
7.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of STRCAT</TITLE>
</HEAD><BODY>
<H1>STRCAT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-08-02<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>
strcat, strncat - concatenate two strings
<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 *strcat(char *</B><I>dest</I><B>, const char *</B><I>src</I><B>);</B>
<B>char *strncat(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>strcat</B>()
function appends the
<I>src</I>
string to the
<I>dest</I>
string,
overwriting the terminating null byte ('\0') at the end of
<I>dest</I>,
and then adds a terminating null byte.
The strings may not overlap, and the
<I>dest</I>
string must have
enough space for the result.
If
<I>dest</I>
is not large enough, program behavior is unpredictable;
<I>buffer overruns are a favorite avenue for attacking secure programs</I>.
<P>
The
<B>strncat</B>()
function is similar, except that
<DL COMPACT>
<DT id="1">*<DD>
it will use at most
<I>n</I>
bytes from
<I>src</I>;
and
<DT id="2">*<DD>
<I>src</I>
does not need to be null-terminated if it contains
<I>n</I>
or more bytes.
</DL>
<P>
As with
<B>strcat</B>(),
the resulting string in
<I>dest</I>
is always null-terminated.
<P>
If
<I>src</I>
contains
<I>n</I>
or more bytes,
<B>strncat</B>()
writes
<I>n+1</I>
bytes to
<I>dest</I>
(<I>n</I>
from
<I>src</I>
plus the terminating null byte).
Therefore, the size of
<I>dest</I>
must be at least
<I>strlen(dest)+n+1</I>.
<P>
A simple implementation of
<B>strncat</B>()
might be:
<P>
char *
strncat(char *dest, const char *src, size_t n)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;dest_len&nbsp;=&nbsp;strlen(dest);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;i;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0&nbsp;;&nbsp;i&nbsp;&lt;&nbsp;n&nbsp;&amp;&amp;&nbsp;src[i]&nbsp;!=&nbsp;'\0'&nbsp;;&nbsp;i++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest[dest_len&nbsp;+&nbsp;i]&nbsp;=&nbsp;src[i];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;dest[dest_len&nbsp;+&nbsp;i]&nbsp;=&nbsp;'\0';
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;dest;
}
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>strcat</B>()
and
<B>strncat</B>()
functions return a pointer to the resulting 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>strcat</B>(),
<B>strncat</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 systems (the BSDs, Solaris, and others) provide the following function:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;strlcat(char&nbsp;*dest,&nbsp;const&nbsp;char&nbsp;*src,&nbsp;size_t&nbsp;size);
<P>
This function appends the null-terminated string
<I>src</I>
to the string
<I>dest</I>,
copying at most
<I>size-strlen(dest)-1</I>
from
<I>src</I>,
and adds a terminating null byte to the result,
<I>unless</I>
<I>size</I>
is less than
<I>strlen(dest)</I>.
This function fixes the buffer overrun problem of
<B>strcat</B>(),
but the caller must still handle the possibility of data loss if
<I>size</I>
is too small.
The function returns the length of the string
<B>strlcat</B>()
tried to create; if the return value is greater than or equal to
<I>size</I>,
data loss occurred.
If data loss matters, the caller
<I>must</I>
either check the arguments before the call, or test the function return value.
<B>strlcat</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="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
Because
<B>strcat</B>()
and
<B>strncat</B>()
must find the null byte that terminates the string
<I>dest</I>
using a search that starts at the beginning of the string,
the execution time of these functions
scales according to the length of the string
<I>dest</I>.
This can be demonstrated by running the program below.
(If the goal is to concatenate many strings to one target,
then manually copying the bytes from each source string
while maintaining a pointer to the end of the target string
will provide better performance.)
<A NAME="lbAJ">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
#define LIM 4000000
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;p[LIM&nbsp;+&nbsp;1];&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;+1&nbsp;for&nbsp;terminating&nbsp;null&nbsp;byte&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;base;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;base&nbsp;=&nbsp;time(NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;p[0]&nbsp;=&nbsp;'\0';
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;LIM;&nbsp;j++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((j&nbsp;%&nbsp;10000)&nbsp;==&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d&nbsp;%ld\n&quot;,&nbsp;j,&nbsp;(long)&nbsp;(time(NULL)&nbsp;-&nbsp;base));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcat(p,&nbsp;&quot;a&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<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+strcpy">strcpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+string">string</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strncpy">strncpy</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wcscat">wcscat</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+wcsncat">wcsncat</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="3"><A HREF="#lbAB">NAME</A><DD>
<DT id="4"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="5"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="6"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="7"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="8"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="9"><A HREF="#lbAH">NOTES</A><DD>
<DT id="10"><A HREF="#lbAI">EXAMPLE</A><DD>
<DL>
<DT id="11"><A HREF="#lbAJ">Program source</A><DD>
</DL>
<DT id="12"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="13"><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>