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