1*0a6a1f1dSLionel Sambuc /* $NetBSD: strlcpy.c,v 1.2 2015/01/22 03:48:07 christos Exp $ */
2*0a6a1f1dSLionel Sambuc /* $OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $ */
3*0a6a1f1dSLionel Sambuc
4*0a6a1f1dSLionel Sambuc /*
5*0a6a1f1dSLionel Sambuc * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Permission to use, copy, modify, and distribute this software for any
8*0a6a1f1dSLionel Sambuc * purpose with or without fee is hereby granted, provided that the above
9*0a6a1f1dSLionel Sambuc * copyright notice and this permission notice appear in all copies.
10*0a6a1f1dSLionel Sambuc *
11*0a6a1f1dSLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
12*0a6a1f1dSLionel Sambuc * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13*0a6a1f1dSLionel Sambuc * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
14*0a6a1f1dSLionel Sambuc * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*0a6a1f1dSLionel Sambuc * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16*0a6a1f1dSLionel Sambuc * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17*0a6a1f1dSLionel Sambuc * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0a6a1f1dSLionel Sambuc */
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
21*0a6a1f1dSLionel Sambuc #if HAVE_CONFIG_H
22*0a6a1f1dSLionel Sambuc #include "config.h"
23*0a6a1f1dSLionel Sambuc #endif
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
26*0a6a1f1dSLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
27*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: strlcpy.c,v 1.2 2015/01/22 03:48:07 christos Exp $");
28*0a6a1f1dSLionel Sambuc #endif /* LIBC_SCCS and not lint */
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc #ifdef _LIBC
31*0a6a1f1dSLionel Sambuc #include "namespace.h"
32*0a6a1f1dSLionel Sambuc #endif
33*0a6a1f1dSLionel Sambuc #include <sys/types.h>
34*0a6a1f1dSLionel Sambuc #include <assert.h>
35*0a6a1f1dSLionel Sambuc #include <string.h>
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc #ifdef _LIBC
38*0a6a1f1dSLionel Sambuc # ifdef __weak_alias
__weak_alias(strlcpy,_strlcpy)39*0a6a1f1dSLionel Sambuc __weak_alias(strlcpy, _strlcpy)
40*0a6a1f1dSLionel Sambuc # endif
41*0a6a1f1dSLionel Sambuc #endif
42*0a6a1f1dSLionel Sambuc #else
43*0a6a1f1dSLionel Sambuc #include <lib/libkern/libkern.h>
44*0a6a1f1dSLionel Sambuc #endif /* !_KERNEL && !_STANDALONE */
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc #if !HAVE_STRLCPY
48*0a6a1f1dSLionel Sambuc /*
49*0a6a1f1dSLionel Sambuc * Copy src to string dst of size siz. At most siz-1 characters
50*0a6a1f1dSLionel Sambuc * will be copied. Always NUL terminates (unless siz == 0).
51*0a6a1f1dSLionel Sambuc * Returns strlen(src); if retval >= siz, truncation occurred.
52*0a6a1f1dSLionel Sambuc */
53*0a6a1f1dSLionel Sambuc size_t
54*0a6a1f1dSLionel Sambuc strlcpy(char *dst, const char *src, size_t siz)
55*0a6a1f1dSLionel Sambuc {
56*0a6a1f1dSLionel Sambuc char *d = dst;
57*0a6a1f1dSLionel Sambuc const char *s = src;
58*0a6a1f1dSLionel Sambuc size_t n = siz;
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc /* Copy as many bytes as will fit */
61*0a6a1f1dSLionel Sambuc if (n != 0 && --n != 0) {
62*0a6a1f1dSLionel Sambuc do {
63*0a6a1f1dSLionel Sambuc if ((*d++ = *s++) == 0)
64*0a6a1f1dSLionel Sambuc break;
65*0a6a1f1dSLionel Sambuc } while (--n != 0);
66*0a6a1f1dSLionel Sambuc }
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc /* Not enough room in dst, add NUL and traverse rest of src */
69*0a6a1f1dSLionel Sambuc if (n == 0) {
70*0a6a1f1dSLionel Sambuc if (siz != 0)
71*0a6a1f1dSLionel Sambuc *d = '\0'; /* NUL-terminate dst */
72*0a6a1f1dSLionel Sambuc while (*s++)
73*0a6a1f1dSLionel Sambuc ;
74*0a6a1f1dSLionel Sambuc }
75*0a6a1f1dSLionel Sambuc
76*0a6a1f1dSLionel Sambuc return(s - src - 1); /* count does not include NUL */
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc #endif
79