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