333 lines
7.3 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
strcat, strncat - concatenate two strings
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/string.h">string.h</A>></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"> </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> size_t dest_len = strlen(dest);
|
|
<BR> size_t i;
|
|
<P>
|
|
<BR> for (i = 0 ; i < n && src[i] != '\0' ; i++)
|
|
<BR> dest[dest_len + i] = src[i];
|
|
<BR> dest[dest_len + i] = '\0';
|
|
<P>
|
|
<BR> return dest;
|
|
}
|
|
|
|
|
|
<A NAME="lbAE"> </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"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
Some systems (the BSDs, Solaris, and others) provide the following function:
|
|
<P>
|
|
|
|
<BR> size_t strlcat(char *dest, const char *src, size_t 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"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
#define LIM 4000000
|
|
<BR> int j;
|
|
<BR> char p[LIM + 1]; /* +1 for terminating null byte */
|
|
<BR> time_t base;
|
|
<P>
|
|
<BR> base = time(NULL);
|
|
<BR> p[0] = '\0';
|
|
<P>
|
|
<BR> for (j = 0; j < LIM; j++) {
|
|
<BR> if ((j % 10000) == 0)
|
|
<BR> printf("%d %ld\n", j, (long) (time(NULL) - base));
|
|
<BR> strcat(p, "a");
|
|
<BR> }
|
|
}
|
|
|
|
|
|
<A NAME="lbAK"> </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"> </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"> </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>
|