]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strcat.3
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man3 / strcat.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
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"
31 .SH NAME
32 strcat, strncat \- concatenate two strings
33 .SH SYNOPSIS
34 .nf
35 .B #include <string.h>
36 .sp
37 .BI "char *strcat(char *" dest ", const char *" src );
38 .sp
39 .BI "char *strncat(char *" dest ", const char *" src ", size_t " n );
40 .fi
41 .SH DESCRIPTION
42 The
43 .BR strcat ()
44 function appends the \fIsrc\fP string to the
45 \fIdest\fP string,
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.
50 If
51 .I dest
52 is not large enough, program behavior is unpredictable;
53 .IR "buffer overruns are a favorite avenue for attacking secure programs" .
54 .PP
55 The
56 .BR strncat ()
57 function is similar, except that
58 .IP * 3
59 it will use at most \fIn\fP bytes from \fIsrc\fP; and
60 .IP *
61 \fIsrc\fP does not need to be null-terminated if it contains
62 \fIn\fP or more bytes.
63 .PP
64 As with
65 .BR strcat (),
66 the resulting string in \fIdest\fP is always null-terminated.
67 .PP
68 If \fIsrc\fP contains \fIn\fP or more bytes,
69 .BR strncat ()
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.
74
75 A simple implementation of
76 .BR strncat ()
77 might be:
78 .in +4n
79 .nf
80
81 char*
82 strncat(char *dest, const char *src, size_t n)
83 {
84 size_t dest_len = strlen(dest);
85 size_t i;
86
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;
90
91 return dest;
92 }
93 .fi
94 .in
95 .SH RETURN VALUE
96 The
97 .BR strcat ()
98 and
99 .BR strncat ()
100 functions return a pointer to the resulting string \fIdest\fP.
101 .SH CONFORMING TO
102 SVr4, 4.3BSD, C89, C99.
103 .SH NOTES
104 Some systems (the BSDs, Solaris, and others) provide the following function:
105
106 size_t strlcat(char *dest, const char *src, size_t size);
107
108 This function appends the null-terminated string
109 .I src
110 to the string
111 .IR dest ,
112 copying at most
113 .IR "size\-strlen(dest)\-1"
114 from
115 .IR src ,
116 and adds a null terminator to the result,
117 .I unless
118 .IR size
119 is less than
120 .IR strlen(dest) .
121 This function fixes the buffer overrun problem of
122 .BR strcat (),
123 but the caller must still handle the possibility of data loss if
124 .I size
125 is too small.
126 The function returns the length of the string
127 .BR strlcat ()
128 tried to create; if the return value is greater than or equal to
129 .IR size ,
130 data loss occurred.
131 If data loss matters, the caller
132 .I must
133 either check the arguments before the call, or test the function return value.
134 .BR strlcat ()
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
138 .IR libbsd
139 library.
140 .SH SEE ALSO
141 .BR bcopy (3),
142 .BR memccpy (3),
143 .BR memcpy (3),
144 .BR strcpy (3),
145 .BR string (3),
146 .BR strncpy (3),
147 .BR wcscat (3),
148 .BR wcsncat (3)