xref: /netbsd-src/lib/libc/string/strlcpy.3 (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1.\"	$NetBSD: strlcpy.3,v 1.2 1999/09/08 22:56:56 lukem Exp $
2.\" from OpenBSD: strlcpy.3,v 1.6 1999/09/04 02:22:46 pjanzen Exp
3.\"
4.\" Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5.\" All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. The name of the author may not be used to endorse or promote products
16.\"    derived from this software without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.Dd September 9, 1999
30.Dt STRLCPY 3
31.Os
32.Sh NAME
33.Nm strlcpy ,
34.Nm strlcat
35.Nd size-bounded string copying and concatenation
36.Sh SYNOPSIS
37.Fd #include <string.h>
38.Ft size_t
39.Fn strlcpy "char *dst" "const char *src" "size_t size"
40.Ft size_t
41.Fn strlcat "char *dst" "const char *src" "size_t size"
42.Sh DESCRIPTION
43The
44.Fn strlcpy
45and
46.Fn strlcat
47functions copy and concatenate strings respectively.  They are designed
48to be safer, more consistent, and less error prone replacements for
49.Xr strncpy 3
50and
51.Xr strncat 3 .
52Unlike those functions,
53.Fn strlcpy
54and
55.Fn strlcat
56take the full size of the buffer (not just the length) and guarantee to
57NUL-terminate the result (as long as
58.Fa size
59is larger than 0).  Note that you should include a byte for the NUL in
60.Fa size .
61.Pp
62The
63.Fn strlcpy
64function copies up to
65.Fa size
66- 1 characters from the NUL-terminated string
67.Fa src
68to
69.Fa dst ,
70NUL-terminating the result.
71.Pp
72The
73.Fn strlcat
74function appends the NUL-terminated string
75.Fa src
76to the end of
77.Fa dst .
78It will append at most
79.Fa size
80- strlen(dst) - 1 bytes, NUL-terminating the result.
81.Sh RETURN VALUES
82The
83.Fn strlcpy
84and
85.Fn strlcat
86functions return the total length of the string they tried to
87create.  For
88.Fn strlcpy
89that means the length of
90.Fa src .
91For
92.Fn strlcat
93that means the initial length of
94.Fa dst
95plus
96the length of
97.Fa src .
98While this may seem somewhat confusing it was done to make
99truncation detection simple.
100.Sh EXAMPLES
101The following code fragment illustrates the simple case:
102.Bd -literal -offset indent
103char *s, *p, buf[BUFSIZ];
104
105\&...
106
107(void)strlcpy(buf, s, sizeof(buf));
108(void)strlcat(buf, p, sizeof(buf));
109.Ed
110.Pp
111To detect truncation, perhaps while building a pathname, something
112like the following might be used:
113.Bd -literal -offset indent
114char *dir, *file, pname[MAXPATHNAMELEN];
115
116\&...
117
118if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
119	goto toolong;
120if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
121	goto toolong;
122.Ed
123.Pp
124Since we know how many characters we copied the first time, we can
125speed things up a bit by using a copy instead on an append:
126.Bd -literal -offset indent
127char *dir, *file, pname[MAXPATHNAMELEN];
128size_t n;
129
130\&...
131
132n = strlcpy(pname, dir, sizeof(pname));
133if (n >= sizeof(pname))
134	goto toolong;
135if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
136	goto toolong;
137.Ed
138.Pp
139However, one may question the validity of such optimizations, as they
140defeat the whole purpose of
141.Fn strlcpy
142and
143.Fn strlcat .
144As a matter of fact, the first version of this manual page got it wrong.
145.Sh SEE ALSO
146.Xr snprintf 3 ,
147.Xr strncat 3 ,
148.Xr strncpy 3
149