2013-11-04 19:05:31 +00:00
|
|
|
.\" $OpenBSD: strlcpy.3,v 1.26 2013/09/30 12:02:35 millert Exp $
|
1999-08-10 05:21:31 +00:00
|
|
|
.\"
|
2009-01-12 06:10:48 +00:00
|
|
|
.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
|
1999-08-10 05:21:31 +00:00
|
|
|
.\"
|
2009-01-12 06:10:48 +00:00
|
|
|
.\" Permission to use, copy, modify, and distribute this software for any
|
|
|
|
.\" purpose with or without fee is hereby granted, provided that the above
|
|
|
|
.\" copyright notice and this permission notice appear in all copies.
|
|
|
|
.\"
|
|
|
|
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-08-10 05:21:31 +00:00
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
1999-08-28 23:04:49 +00:00
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2016-02-26 22:14:19 +00:00
|
|
|
.Dd February 26, 2016
|
1999-08-10 05:21:31 +00:00
|
|
|
.Dt STRLCPY 3
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
1999-08-10 05:58:58 +00:00
|
|
|
.Nm strlcpy ,
|
1999-08-10 05:21:31 +00:00
|
|
|
.Nm strlcat
|
|
|
|
.Nd size-bounded string copying and concatenation
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1999-08-10 05:21:31 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In string.h
|
1999-08-10 05:21:31 +00:00
|
|
|
.Ft size_t
|
2016-02-26 22:14:19 +00:00
|
|
|
.Fn strlcpy "char * restrict dst" "const char * restrict src" "size_t dstsize"
|
1999-08-10 05:21:31 +00:00
|
|
|
.Ft size_t
|
2016-02-26 22:14:19 +00:00
|
|
|
.Fn strlcat "char * restrict dst" "const char * restrict src" "size_t dstsize"
|
1999-08-10 05:21:31 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn strlcpy
|
|
|
|
and
|
|
|
|
.Fn strlcat
|
2013-11-04 19:05:31 +00:00
|
|
|
functions copy and concatenate strings with the
|
|
|
|
same input parameters and output result as
|
|
|
|
.Xr snprintf 3 .
|
|
|
|
They are designed to be safer, more consistent, and less error
|
|
|
|
prone replacements for the easily misused functions
|
1999-08-10 05:21:31 +00:00
|
|
|
.Xr strncpy 3
|
|
|
|
and
|
|
|
|
.Xr strncat 3 .
|
2013-11-04 19:05:31 +00:00
|
|
|
.Pp
|
2001-07-15 07:53:42 +00:00
|
|
|
.Fn strlcpy
|
2001-01-17 20:51:20 +00:00
|
|
|
and
|
|
|
|
.Fn strlcat
|
2013-11-04 19:05:31 +00:00
|
|
|
take the full size of the destination buffer and guarantee
|
|
|
|
NUL-termination if there is room.
|
|
|
|
Note that room for the NUL should be included in
|
|
|
|
.Fa dstsize .
|
1999-08-10 05:21:31 +00:00
|
|
|
.Pp
|
|
|
|
.Fn strlcpy
|
2013-11-04 19:05:31 +00:00
|
|
|
copies up to
|
|
|
|
.Fa dstsize
|
|
|
|
\- 1 characters from the string
|
1999-08-10 05:21:31 +00:00
|
|
|
.Fa src
|
|
|
|
to
|
|
|
|
.Fa dst ,
|
2013-11-04 19:05:31 +00:00
|
|
|
NUL-terminating the result if
|
|
|
|
.Fa dstsize
|
|
|
|
is not 0.
|
1999-08-10 05:21:31 +00:00
|
|
|
.Pp
|
|
|
|
.Fn strlcat
|
2013-11-04 19:05:31 +00:00
|
|
|
appends string
|
1999-08-10 05:21:31 +00:00
|
|
|
.Fa src
|
|
|
|
to the end of
|
|
|
|
.Fa dst .
|
|
|
|
It will append at most
|
2013-11-04 19:05:31 +00:00
|
|
|
.Fa dstsize
|
|
|
|
\- strlen(dst) \- 1 characters.
|
|
|
|
It will then NUL-terminate, unless
|
|
|
|
.Fa dstsize
|
|
|
|
is 0 or the original
|
|
|
|
.Fa dst
|
|
|
|
string was longer than
|
|
|
|
.Fa dstsize
|
|
|
|
(in practice this should not happen
|
|
|
|
as it means that either
|
|
|
|
.Fa dstsize
|
|
|
|
is incorrect or that
|
|
|
|
.Fa dst
|
|
|
|
is not a proper string).
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Fa src
|
|
|
|
and
|
|
|
|
.Fa dst
|
|
|
|
strings overlap, the behavior is undefined.
|
1999-08-10 05:21:31 +00:00
|
|
|
.Sh RETURN VALUES
|
2013-11-04 19:05:31 +00:00
|
|
|
Besides quibbles over the return type
|
|
|
|
.Pf ( Va size_t
|
|
|
|
versus
|
|
|
|
.Va int )
|
|
|
|
and signal handler safety
|
|
|
|
.Pf ( Xr snprintf 3
|
|
|
|
is not entirely safe on some systems), the
|
|
|
|
following two are equivalent:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
n = strlcpy(dst, src, len);
|
|
|
|
n = snprintf(dst, len, "%s", src);
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
Like
|
|
|
|
.Xr snprintf 3 ,
|
|
|
|
the
|
1999-08-10 05:21:31 +00:00
|
|
|
.Fn strlcpy
|
|
|
|
and
|
|
|
|
.Fn strlcat
|
2013-11-04 19:05:31 +00:00
|
|
|
functions return the total length of the string they tried to create.
|
2004-07-02 23:52:20 +00:00
|
|
|
For
|
1999-08-10 05:21:31 +00:00
|
|
|
.Fn strlcpy
|
|
|
|
that means the length of
|
|
|
|
.Fa src .
|
|
|
|
For
|
|
|
|
.Fn strlcat
|
|
|
|
that means the initial length of
|
|
|
|
.Fa dst
|
|
|
|
plus
|
|
|
|
the length of
|
|
|
|
.Fa src .
|
2001-07-24 11:32:29 +00:00
|
|
|
.Pp
|
2013-11-04 19:05:31 +00:00
|
|
|
If the return value is
|
|
|
|
.Cm >=
|
|
|
|
.Va dstsize ,
|
|
|
|
the output string has been truncated.
|
|
|
|
It is the caller's responsibility to handle this.
|
1999-08-10 05:21:31 +00:00
|
|
|
.Sh EXAMPLES
|
|
|
|
The following code fragment illustrates the simple case:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
char *s, *p, buf[BUFSIZ];
|
|
|
|
|
2000-12-29 14:08:20 +00:00
|
|
|
\&...
|
1999-08-10 05:21:31 +00:00
|
|
|
|
|
|
|
(void)strlcpy(buf, s, sizeof(buf));
|
|
|
|
(void)strlcat(buf, p, sizeof(buf));
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
To detect truncation, perhaps while building a pathname, something
|
|
|
|
like the following might be used:
|
|
|
|
.Bd -literal -offset indent
|
2000-11-19 11:29:58 +00:00
|
|
|
char *dir, *file, pname[MAXPATHLEN];
|
1999-08-10 05:21:31 +00:00
|
|
|
|
2000-12-29 14:08:20 +00:00
|
|
|
\&...
|
1999-08-10 05:21:31 +00:00
|
|
|
|
|
|
|
if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
|
|
|
|
goto toolong;
|
|
|
|
if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
|
|
|
|
goto toolong;
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2009-01-12 06:10:48 +00:00
|
|
|
Since it is known how many characters were copied the first time, things
|
2013-11-04 19:05:31 +00:00
|
|
|
can be sped up a bit by using a copy instead of an append:
|
1999-08-10 05:21:31 +00:00
|
|
|
.Bd -literal -offset indent
|
2001-07-24 11:32:29 +00:00
|
|
|
char *dir, *file, pname[MAXPATHLEN];
|
1999-08-10 05:21:31 +00:00
|
|
|
size_t n;
|
|
|
|
|
2000-12-29 14:08:20 +00:00
|
|
|
\&...
|
1999-08-10 05:21:31 +00:00
|
|
|
|
|
|
|
n = strlcpy(pname, dir, sizeof(pname));
|
|
|
|
if (n >= sizeof(pname))
|
|
|
|
goto toolong;
|
|
|
|
if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
|
|
|
|
goto toolong;
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
However, one may question the validity of such optimizations, as they
|
|
|
|
defeat the whole purpose of
|
|
|
|
.Fn strlcpy
|
|
|
|
and
|
|
|
|
.Fn strlcat .
|
|
|
|
As a matter of fact, the first version of this manual page got it wrong.
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr snprintf 3 ,
|
1999-08-10 05:58:58 +00:00
|
|
|
.Xr strncat 3 ,
|
2009-04-07 13:42:53 +00:00
|
|
|
.Xr strncpy 3 ,
|
|
|
|
.Xr wcslcpy 3
|
1999-10-29 16:50:22 +00:00
|
|
|
.Sh HISTORY
|
2002-12-18 12:45:11 +00:00
|
|
|
The
|
1999-10-29 16:50:22 +00:00
|
|
|
.Fn strlcpy
|
|
|
|
and
|
|
|
|
.Fn strlcat
|
|
|
|
functions first appeared in
|
|
|
|
.Ox 2.4 ,
|
2013-11-04 19:05:31 +00:00
|
|
|
and
|
1999-10-29 16:50:22 +00:00
|
|
|
.Fx 3.3 .
|