1 .\" $OpenBSD: strlcpy.3,v 1.5 1999/06/06 15:17:32 aaron Exp $
3 .\" Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission.
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.4.2.8 2002/01/19 12:29:40 yar Exp $
29 .\" $DragonFly: src/lib/libc/string/strlcpy.3,v 1.2 2003/06/17 04:26:46 dillon Exp $
37 .Nd size-bounded string copying and concatenation
43 .Fn strlcpy "char *dst" "const char *src" "size_t size"
45 .Fn strlcat "char *dst" "const char *src" "size_t size"
51 functions copy and concatenate strings respectively. They are designed
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 you should include a byte for the NUL 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
114 that means the length of
118 that means the initial length of
123 While this may seem somewhat confusing it was done to make
124 truncation detection simple.
126 Note however, that if
130 characters without finding a NUL, the length of the string is considered
133 and the destination string will not be NUL-terminated (since there was
134 no space for the NUL).
137 from running off the end of a string.
138 In practice this should not happen (as it means that either
145 The check exists to prevent potential security problems in incorrect code.
147 The following code fragment illustrates the simple case:
148 .Bd -literal -offset indent
149 char *s, *p, buf[BUFSIZ];
153 (void)strlcpy(buf, s, sizeof(buf));
154 (void)strlcat(buf, p, sizeof(buf));
157 To detect truncation, perhaps while building a pathname, something
158 like the following might be used:
159 .Bd -literal -offset indent
160 char *dir, *file, pname[MAXPATHLEN];
164 if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
166 if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
170 Since we know how many characters we copied the first time, we can
171 speed things up a bit by using a copy instead of an append:
172 .Bd -literal -offset indent
173 char *dir, *file, pname[MAXPATHLEN];
178 n = strlcpy(pname, dir, sizeof(pname));
179 if (n >= sizeof(pname))
181 if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
185 However, one may question the validity of such optimizations, as they
186 defeat the whole purpose of
190 As a matter of fact, the first version of this manual page got it wrong.
199 functions first appeared in
201 and made their appearance in