1
/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
1
/* $NetBSD: strlcpy.c,v 1.3 2007/06/04 18:19:27 christos Exp $ */
2
/* $OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $ */
4
5
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
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.
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.
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
11
* THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
12
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
14
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
#if !defined(_KERNEL) && !defined(_STANDALONE)
21
#if HAVE_NBTOOL_CONFIG_H
22
#include "nbtool_config.h"
30
24
#include "config.h"
31
26
#if defined(LIBC_SCCS) && !defined(lint)
33
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
35
27
#endif /* LIBC_SCCS and not lint */
37
static const char rcsid[] =
38
"$FreeBSD: src/lib/libc/string/strlcpy.c,v 1.2.4.1 2001/07/09 23:30:06 obrien Exp $";
30
#include "namespace.h"
41
32
#include <sys/types.h>
42
34
#include <string.h>
38
__weak_alias(strlcpy, _strlcpy)
42
#include <lib/libkern/libkern.h>
43
#endif /* !_KERNEL && !_STANDALONE */
45
48
* Copy src to string dst of size siz. At most siz-1 characters
46
49
* will be copied. Always NUL terminates (unless siz == 0).
47
50
* Returns strlen(src); if retval >= siz, truncation occurred.
49
size_t strlcpy(dst, src, siz)
53
strlcpy(char *dst, const char *src, size_t siz)
54
register char *d = dst;
55
register const char *s = src;
56
register size_t n = siz;
59
_DIAGASSERT(dst != NULL);
60
_DIAGASSERT(src != NULL);
58
62
/* Copy as many bytes as will fit */
59
63
if (n != 0 && --n != 0) {