OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / strcat.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sat Jul 24 18:11:47 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
31 .\"     Improve discussion of strncat().
32 .TH STRCAT 3  2014-01-20 "GNU" "Linux Programmer's Manual"
33 .SH NAME
34 strcat, strncat \- concatenate two strings
35 .SH SYNOPSIS
36 .nf
37 .B #include <string.h>
38 .sp
39 .BI "char *strcat(char *" dest ", const char *" src );
40 .sp
41 .BI "char *strncat(char *" dest ", const char *" src ", size_t " n );
42 .fi
43 .SH DESCRIPTION
44 The
45 .BR strcat ()
46 function appends the
47 .I src
48 string to the
49 .I dest
50 string,
51 overwriting the terminating null byte (\(aq\\0\(aq) at the end of
52 .IR dest ,
53 and then adds a terminating null byte.
54 The strings may not overlap, and the
55 .I dest
56 string must have
57 enough space for the result.
58 If
59 .I dest
60 is not large enough, program behavior is unpredictable;
61 .IR "buffer overruns are a favorite avenue for attacking secure programs" .
62 .PP
63 The
64 .BR strncat ()
65 function is similar, except that
66 .IP * 3
67 it will use at most
68 .I n
69 bytes from
70 .IR src ;
71 and
72 .IP *
73 .I src
74 does not need to be null-terminated if it contains
75 .I n
76 or more bytes.
77 .PP
78 As with
79 .BR strcat (),
80 the resulting string in
81 .I dest
82 is always null-terminated.
83 .PP
84 If
85 .IR src
86 contains
87 .I n
88 or more bytes,
89 .BR strncat ()
90 writes
91 .I n+1
92 bytes to
93 .I dest
94 .RI ( n
95 from
96 .I src
97 plus the terminating null byte).
98 Therefore, the size of
99 .I dest
100 must be at least
101 .IR "strlen(dest)+n+1" .
102
103 A simple implementation of
104 .BR strncat ()
105 might be:
106 .in +4n
107 .nf
108
109 char*
110 strncat(char *dest, const char *src, size_t n)
111 {
112     size_t dest_len = strlen(dest);
113     size_t i;
114
115     for (i = 0 ; i < n && src[i] != \(aq\\0\(aq ; i++)
116         dest[dest_len + i] = src[i];
117     dest[dest_len + i] = \(aq\\0\(aq;
118
119     return dest;
120 }
121 .fi
122 .in
123 .SH RETURN VALUE
124 The
125 .BR strcat ()
126 and
127 .BR strncat ()
128 functions return a pointer to the resulting string
129 .IR dest .
130 .SH ATTRIBUTES
131 .SS Multithreading (see pthreads(7))
132 The
133 .BR strcat ()
134 and
135 .BR strncat ()
136 functions are thread-safe.
137 .SH CONFORMING TO
138 SVr4, 4.3BSD, C89, C99.
139 .SH NOTES
140 Some systems (the BSDs, Solaris, and others) provide the following function:
141
142     size_t strlcat(char *dest, const char *src, size_t size);
143
144 This function appends the null-terminated string
145 .I src
146 to the string
147 .IR dest ,
148 copying at most
149 .IR "size\-strlen(dest)\-1"
150 from
151 .IR src ,
152 and adds a terminating null byte to the result,
153 .I unless
154 .IR size
155 is less than
156 .IR strlen(dest) .
157 This function fixes the buffer overrun problem of
158 .BR strcat (),
159 but the caller must still handle the possibility of data loss if
160 .I size
161 is too small.
162 The function returns the length of the string
163 .BR strlcat ()
164 tried to create; if the return value is greater than or equal to
165 .IR size ,
166 data loss occurred.
167 If data loss matters, the caller
168 .I must
169 either check the arguments before the call, or test the function return value.
170 .BR strlcat ()
171 is not present in glibc and is not standardized by POSIX,
172 .\" https://lwn.net/Articles/506530/
173 but is available on Linux via the
174 .IR libbsd
175 library.
176 .SH SEE ALSO
177 .BR bcopy (3),
178 .BR memccpy (3),
179 .BR memcpy (3),
180 .BR strcpy (3),
181 .BR string (3),
182 .BR strncpy (3),
183 .BR wcscat (3),
184 .BR wcsncat (3)
185 .SH COLOPHON
186 This page is part of release 3.79 of the Linux
187 .I man-pages
188 project.
189 A description of the project,
190 information about reporting bugs,
191 and the latest version of this page,
192 can be found at
193 \%http://www.kernel.org/doc/man\-pages/.