1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified Sat Jul 24 18:11:47 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
29 .\" Improve discussion of strncat().
30 .TH STRCAT 3 2012-07-19 "GNU" "Linux Programmer's Manual"
32 strcat, strncat \- concatenate two strings
35 .B #include <string.h>
37 .BI "char *strcat(char *" dest ", const char *" src );
39 .BI "char *strncat(char *" dest ", const char *" src ", size_t " n );
44 function appends the \fIsrc\fP string to the
46 overwriting the terminating null byte (\(aq\\0\(aq) at the end of
47 \fIdest\fP, and then adds a terminating null byte.
48 The strings may not overlap, and the \fIdest\fP string must have
49 enough space for the result.
52 is not large enough, program behavior is unpredictable;
53 .IR "buffer overruns are a favorite avenue for attacking secure programs" .
57 function is similar, except that
59 it will use at most \fIn\fP bytes from \fIsrc\fP; and
61 \fIsrc\fP does not need to be null-terminated if it contains
62 \fIn\fP or more bytes.
66 the resulting string in \fIdest\fP is always null-terminated.
68 If \fIsrc\fP contains \fIn\fP or more bytes,
70 writes \fIn+1\fP bytes to \fIdest\fP (\fIn\fP
71 from \fIsrc\fP plus the terminating null byte).
72 Therefore, the size of \fIdest\fP must be at least
73 \fIstrlen(dest)+n+1\fP.
75 A simple implementation of
82 strncat(char *dest, const char *src, size_t n)
84 size_t dest_len = strlen(dest);
87 for (i = 0 ; i < n && src[i] != \(aq\\0\(aq ; i++)
88 dest[dest_len + i] = src[i];
89 dest[dest_len + i] = \(aq\\0\(aq;
100 functions return a pointer to the resulting string \fIdest\fP.
102 SVr4, 4.3BSD, C89, C99.
104 Some systems (the BSDs, Solaris, and others) provide the following function:
106 size_t strlcat(char *dest, const char *src, size_t size);
108 This function appends the null-terminated string
113 .IR "size\-strlen(dest)\-1"
116 and adds a null terminator to the result,
121 This function fixes the buffer overrun problem of
123 but the caller must still handle the possibility of data loss if
126 The function returns the length of the string
128 tried to create; if the return value is greater than or equal to
131 If data loss matters, the caller
133 either check the arguments before the call, or test the function return value.
135 is not present in glibc and is not standardized by POSIX,
136 .\" https://lwn.net/Articles/506530/
137 but is available on Linux via the