1*2b15cb3dSCy Schubert /* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */
2*2b15cb3dSCy Schubert
3*2b15cb3dSCy Schubert /*
4*2b15cb3dSCy Schubert * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5*2b15cb3dSCy Schubert * All rights reserved.
6*2b15cb3dSCy Schubert *
7*2b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
8*2b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
9*2b15cb3dSCy Schubert * are met:
10*2b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
11*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
12*2b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
13*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
14*2b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
15*2b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
16*2b15cb3dSCy Schubert * derived from this software without specific prior written permission.
17*2b15cb3dSCy Schubert *
18*2b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19*2b15cb3dSCy Schubert * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20*2b15cb3dSCy Schubert * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21*2b15cb3dSCy Schubert * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22*2b15cb3dSCy Schubert * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23*2b15cb3dSCy Schubert * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24*2b15cb3dSCy Schubert * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25*2b15cb3dSCy Schubert * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*2b15cb3dSCy Schubert * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27*2b15cb3dSCy Schubert * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*2b15cb3dSCy Schubert */
29*2b15cb3dSCy Schubert
30*2b15cb3dSCy Schubert #if defined(LIBC_SCCS) && !defined(lint)
31*2b15cb3dSCy Schubert static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
32*2b15cb3dSCy Schubert #endif /* LIBC_SCCS and not lint */
33*2b15cb3dSCy Schubert
34*2b15cb3dSCy Schubert #include "event2/event-config.h"
35*2b15cb3dSCy Schubert #include "evconfig-private.h"
36*2b15cb3dSCy Schubert
37*2b15cb3dSCy Schubert #include <sys/types.h>
38*2b15cb3dSCy Schubert
39*2b15cb3dSCy Schubert #ifndef EVENT__HAVE_STRLCPY
40*2b15cb3dSCy Schubert #include "strlcpy-internal.h"
41*2b15cb3dSCy Schubert
42*2b15cb3dSCy Schubert /*
43*2b15cb3dSCy Schubert * Copy src to string dst of size siz. At most siz-1 characters
44*2b15cb3dSCy Schubert * will be copied. Always NUL terminates (unless siz == 0).
45*2b15cb3dSCy Schubert * Returns strlen(src); if retval >= siz, truncation occurred.
46*2b15cb3dSCy Schubert */
47*2b15cb3dSCy Schubert size_t
event_strlcpy_(dst,src,siz)48*2b15cb3dSCy Schubert event_strlcpy_(dst, src, siz)
49*2b15cb3dSCy Schubert char *dst;
50*2b15cb3dSCy Schubert const char *src;
51*2b15cb3dSCy Schubert size_t siz;
52*2b15cb3dSCy Schubert {
53*2b15cb3dSCy Schubert register char *d = dst;
54*2b15cb3dSCy Schubert register const char *s = src;
55*2b15cb3dSCy Schubert register size_t n = siz;
56*2b15cb3dSCy Schubert
57*2b15cb3dSCy Schubert /* Copy as many bytes as will fit */
58*2b15cb3dSCy Schubert if (n != 0 && --n != 0) {
59*2b15cb3dSCy Schubert do {
60*2b15cb3dSCy Schubert if ((*d++ = *s++) == 0)
61*2b15cb3dSCy Schubert break;
62*2b15cb3dSCy Schubert } while (--n != 0);
63*2b15cb3dSCy Schubert }
64*2b15cb3dSCy Schubert
65*2b15cb3dSCy Schubert /* Not enough room in dst, add NUL and traverse rest of src */
66*2b15cb3dSCy Schubert if (n == 0) {
67*2b15cb3dSCy Schubert if (siz != 0)
68*2b15cb3dSCy Schubert *d = '\0'; /* NUL-terminate dst */
69*2b15cb3dSCy Schubert while (*s++)
70*2b15cb3dSCy Schubert ;
71*2b15cb3dSCy Schubert }
72*2b15cb3dSCy Schubert
73*2b15cb3dSCy Schubert return (s - src - 1); /* count does not include NUL */
74*2b15cb3dSCy Schubert }
75*2b15cb3dSCy Schubert #endif
76