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