Blame man3/strcat.3

Packit 7cfc04
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" References consulted:
Packit 7cfc04
.\"     Linux libc source code
Packit 7cfc04
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
Packit 7cfc04
.\"     386BSD man pages
Packit 7cfc04
.\" Modified Sat Jul 24 18:11:47 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
Packit 7cfc04
.\"     Improve discussion of strncat().
Packit 7cfc04
.TH STRCAT 3  2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
strcat, strncat \- concatenate two strings
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <string.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *strcat(char *" dest ", const char *" src );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *strncat(char *" dest ", const char *" src ", size_t " n );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR strcat ()
Packit 7cfc04
function appends the
Packit 7cfc04
.I src
Packit 7cfc04
string to the
Packit 7cfc04
.I dest
Packit 7cfc04
string,
Packit 7cfc04
overwriting the terminating null byte (\(aq\\0\(aq) at the end of
Packit 7cfc04
.IR dest ,
Packit 7cfc04
and then adds a terminating null byte.
Packit 7cfc04
The strings may not overlap, and the
Packit 7cfc04
.I dest
Packit 7cfc04
string must have
Packit 7cfc04
enough space for the result.
Packit 7cfc04
If
Packit 7cfc04
.I dest
Packit 7cfc04
is not large enough, program behavior is unpredictable;
Packit 7cfc04
.IR "buffer overruns are a favorite avenue for attacking secure programs" .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
function is similar, except that
Packit 7cfc04
.IP * 3
Packit 7cfc04
it will use at most
Packit 7cfc04
.I n
Packit 7cfc04
bytes from
Packit 7cfc04
.IR src ;
Packit 7cfc04
and
Packit 7cfc04
.IP *
Packit 7cfc04
.I src
Packit 7cfc04
does not need to be null-terminated if it contains
Packit 7cfc04
.I n
Packit 7cfc04
or more bytes.
Packit 7cfc04
.PP
Packit 7cfc04
As with
Packit 7cfc04
.BR strcat (),
Packit 7cfc04
the resulting string in
Packit 7cfc04
.I dest
Packit 7cfc04
is always null-terminated.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.IR src
Packit 7cfc04
contains
Packit 7cfc04
.I n
Packit 7cfc04
or more bytes,
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
writes
Packit 7cfc04
.I n+1
Packit 7cfc04
bytes to
Packit 7cfc04
.I dest
Packit 7cfc04
.RI ( n
Packit 7cfc04
from
Packit 7cfc04
.I src
Packit 7cfc04
plus the terminating null byte).
Packit 7cfc04
Therefore, the size of
Packit 7cfc04
.I dest
Packit 7cfc04
must be at least
Packit 7cfc04
.IR "strlen(dest)+n+1" .
Packit 7cfc04
.PP
Packit 7cfc04
A simple implementation of
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
might be:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char *
Packit 7cfc04
strncat(char *dest, const char *src, size_t n)
Packit 7cfc04
{
Packit 7cfc04
    size_t dest_len = strlen(dest);
Packit 7cfc04
    size_t i;
Packit 7cfc04
Packit 7cfc04
    for (i = 0 ; i < n && src[i] != \(aq\\0\(aq ; i++)
Packit 7cfc04
        dest[dest_len + i] = src[i];
Packit 7cfc04
    dest[dest_len + i] = \(aq\\0\(aq;
Packit 7cfc04
Packit 7cfc04
    return dest;
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
The
Packit 7cfc04
.BR strcat ()
Packit 7cfc04
and
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
functions return a pointer to the resulting string
Packit 7cfc04
.IR dest .
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw19 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR strcat (),
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Some systems (the BSDs, Solaris, and others) provide the following function:
Packit 7cfc04
.PP
Packit 7cfc04
    size_t strlcat(char *dest, const char *src, size_t size);
Packit 7cfc04
.PP
Packit 7cfc04
This function appends the null-terminated string
Packit 7cfc04
.I src
Packit 7cfc04
to the string
Packit 7cfc04
.IR dest ,
Packit 7cfc04
copying at most
Packit 7cfc04
.IR "size\-strlen(dest)\-1"
Packit 7cfc04
from
Packit 7cfc04
.IR src ,
Packit 7cfc04
and adds a terminating null byte to the result,
Packit 7cfc04
.I unless
Packit 7cfc04
.IR size
Packit 7cfc04
is less than
Packit 7cfc04
.IR strlen(dest) .
Packit 7cfc04
This function fixes the buffer overrun problem of
Packit 7cfc04
.BR strcat (),
Packit 7cfc04
but the caller must still handle the possibility of data loss if
Packit 7cfc04
.I size
Packit 7cfc04
is too small.
Packit 7cfc04
The function returns the length of the string
Packit 7cfc04
.BR strlcat ()
Packit 7cfc04
tried to create; if the return value is greater than or equal to
Packit 7cfc04
.IR size ,
Packit 7cfc04
data loss occurred.
Packit 7cfc04
If data loss matters, the caller
Packit 7cfc04
.I must
Packit 7cfc04
either check the arguments before the call, or test the function return value.
Packit 7cfc04
.BR strlcat ()
Packit 7cfc04
is not present in glibc and is not standardized by POSIX,
Packit 7cfc04
.\" https://lwn.net/Articles/506530/
Packit 7cfc04
but is available on Linux via the
Packit 7cfc04
.IR libbsd
Packit 7cfc04
library.
Packit 7cfc04
.\"
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
Because
Packit 7cfc04
.BR strcat ()
Packit 7cfc04
and
Packit 7cfc04
.BR strncat ()
Packit 7cfc04
must find the null byte that terminates the string
Packit 7cfc04
.I dest
Packit 7cfc04
using a search that starts at the beginning of the string,
Packit 7cfc04
the execution time of these functions
Packit 7cfc04
scales according to the length of the string
Packit 7cfc04
.IR dest .
Packit 7cfc04
This can be demonstrated by running the program below.
Packit 7cfc04
(If the goal is to concatenate many strings to one target,
Packit 7cfc04
then manually copying the bytes from each source string
Packit 7cfc04
while maintaining a pointer to the end of the target string
Packit 7cfc04
will provide better performance.)
Packit 7cfc04
.\"
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#include <string.h>
Packit 7cfc04
#include <time.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
#define LIM 4000000
Packit 7cfc04
    int j;
Packit 7cfc04
    char p[LIM];
Packit 7cfc04
    time_t base;
Packit 7cfc04
Packit 7cfc04
    base = time(NULL);
Packit 7cfc04
    p[0] = \(aq\\0\(aq;
Packit 7cfc04
Packit 7cfc04
    for (j = 0; j < LIM; j++) {
Packit 7cfc04
        if ((j % 10000) == 0)
Packit 7cfc04
            printf("%d %ld\\n", j, (long) (time(NULL) \- base));
Packit 7cfc04
        strcat(p, "a");
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.\"
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR bcopy (3),
Packit 7cfc04
.BR memccpy (3),
Packit 7cfc04
.BR memcpy (3),
Packit 7cfc04
.BR strcpy (3),
Packit 7cfc04
.BR string (3),
Packit 7cfc04
.BR strncpy (3),
Packit 7cfc04
.BR wcscat (3),
Packit 7cfc04
.BR wcsncat (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.