1 .\" $OpenBSD: strlcpy.3,v 1.19 2007/05/31 19:19:32 jmc Exp $
3 .\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 .\" $FreeBSD: src/lib/libc/string/strlcpy.3,v 1.16 2009/04/07 13:42:53 trasz Exp $
36 .Nd size-bounded string copying and concatenation
42 .Fn strlcpy "char * restrict dst" "const char * restrict src" "size_t size"
44 .Fn strlcat "char * restrict dst" "const char * restrict src" "size_t size"
50 functions copy and concatenate strings respectively.
52 to be safer, more consistent, and less error prone replacements for
56 Unlike those functions,
60 take the full size of the buffer (not just the length) and guarantee to
61 NUL-terminate the result (as long as
63 is larger than 0 or, in the case of
65 as long as there is at least one byte free in
67 Note that a byte for the NUL should be included in
79 must be NUL-terminated and for
85 must be NUL-terminated.
91 - 1 characters from the NUL-terminated string
95 NUL-terminating the result.
99 function appends the NUL-terminated string
103 It will append at most
105 - strlen(dst) - 1 bytes, NUL-terminating the result.
111 functions return the total length of the string they tried to
115 that means the length of
119 that means the initial length of
124 While this may seem somewhat confusing, it was done to make
125 truncation detection simple.
127 Note however, that if
131 characters without finding a NUL, the length of the string is considered
134 and the destination string will not be NUL-terminated (since there was
135 no space for the NUL).
138 from running off the end of a string.
139 In practice this should not happen (as it means that either
146 The check exists to prevent potential security problems in incorrect code.
148 The following code fragment illustrates the simple case:
149 .Bd -literal -offset indent
150 char *s, *p, buf[BUFSIZ];
154 (void)strlcpy(buf, s, sizeof(buf));
155 (void)strlcat(buf, p, sizeof(buf));
158 To detect truncation, perhaps while building a pathname, something
159 like the following might be used:
160 .Bd -literal -offset indent
161 char *dir, *file, pname[MAXPATHLEN];
165 if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
167 if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
171 Since it is known how many characters were copied the first time, things
172 can be sped up a bit by using a copy instead of an append
173 .Bd -literal -offset indent
174 char *dir, *file, pname[MAXPATHLEN];
179 n = strlcpy(pname, dir, sizeof(pname));
180 if (n >= sizeof(pname))
182 if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
186 However, one may question the validity of such optimizations, as they
187 defeat the whole purpose of
191 As a matter of fact, the first version of this manual page got it wrong.
202 functions first appeared in
204 and made their appearance in