xref: /aosp_15_r20/system/core/libcutils/strlcpy.c (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1*00c7fec1SAndroid Build Coastguard Worker /*
2*00c7fec1SAndroid Build Coastguard Worker  * Copyright (c) 1998 Todd C. Miller <[email protected]>
3*00c7fec1SAndroid Build Coastguard Worker  *
4*00c7fec1SAndroid Build Coastguard Worker  * Permission to use, copy, modify, and distribute this software for any
5*00c7fec1SAndroid Build Coastguard Worker  * purpose with or without fee is hereby granted, provided that the above
6*00c7fec1SAndroid Build Coastguard Worker  * copyright notice and this permission notice appear in all copies.
7*00c7fec1SAndroid Build Coastguard Worker  *
8*00c7fec1SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*00c7fec1SAndroid Build Coastguard Worker  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*00c7fec1SAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*00c7fec1SAndroid Build Coastguard Worker  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*00c7fec1SAndroid Build Coastguard Worker  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*00c7fec1SAndroid Build Coastguard Worker  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*00c7fec1SAndroid Build Coastguard Worker  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*00c7fec1SAndroid Build Coastguard Worker  */
16*00c7fec1SAndroid Build Coastguard Worker 
17*00c7fec1SAndroid Build Coastguard Worker #include <sys/types.h>
18*00c7fec1SAndroid Build Coastguard Worker 
19*00c7fec1SAndroid Build Coastguard Worker #if defined(__GLIBC__) || defined(_WIN32)
20*00c7fec1SAndroid Build Coastguard Worker 
21*00c7fec1SAndroid Build Coastguard Worker #include <string.h>
22*00c7fec1SAndroid Build Coastguard Worker 
23*00c7fec1SAndroid Build Coastguard Worker #include <cutils/memory.h>
24*00c7fec1SAndroid Build Coastguard Worker 
25*00c7fec1SAndroid Build Coastguard Worker /* Implementation of strlcpy() for platforms that don't already have it. */
26*00c7fec1SAndroid Build Coastguard Worker 
27*00c7fec1SAndroid Build Coastguard Worker /*
28*00c7fec1SAndroid Build Coastguard Worker  * Copy src to string dst of size siz.  At most siz-1 characters
29*00c7fec1SAndroid Build Coastguard Worker  * will be copied.  Always NUL terminates (unless siz == 0).
30*00c7fec1SAndroid Build Coastguard Worker  * Returns strlen(src); if retval >= siz, truncation occurred.
31*00c7fec1SAndroid Build Coastguard Worker  */
32*00c7fec1SAndroid Build Coastguard Worker size_t
strlcpy(char * dst,const char * src,size_t siz)33*00c7fec1SAndroid Build Coastguard Worker strlcpy(char *dst, const char *src, size_t siz)
34*00c7fec1SAndroid Build Coastguard Worker {
35*00c7fec1SAndroid Build Coastguard Worker 	char *d = dst;
36*00c7fec1SAndroid Build Coastguard Worker 	const char *s = src;
37*00c7fec1SAndroid Build Coastguard Worker 	size_t n = siz;
38*00c7fec1SAndroid Build Coastguard Worker 
39*00c7fec1SAndroid Build Coastguard Worker 	/* Copy as many bytes as will fit */
40*00c7fec1SAndroid Build Coastguard Worker 	if (n != 0) {
41*00c7fec1SAndroid Build Coastguard Worker 		while (--n != 0) {
42*00c7fec1SAndroid Build Coastguard Worker 			if ((*d++ = *s++) == '\0')
43*00c7fec1SAndroid Build Coastguard Worker 				break;
44*00c7fec1SAndroid Build Coastguard Worker 		}
45*00c7fec1SAndroid Build Coastguard Worker   }
46*00c7fec1SAndroid Build Coastguard Worker 
47*00c7fec1SAndroid Build Coastguard Worker 	/* Not enough room in dst, add NUL and traverse rest of src */
48*00c7fec1SAndroid Build Coastguard Worker 	if (n == 0) {
49*00c7fec1SAndroid Build Coastguard Worker 		if (siz != 0)
50*00c7fec1SAndroid Build Coastguard Worker 			*d = '\0';		/* NUL-terminate dst */
51*00c7fec1SAndroid Build Coastguard Worker 		while (*s++)
52*00c7fec1SAndroid Build Coastguard Worker 			;
53*00c7fec1SAndroid Build Coastguard Worker 	}
54*00c7fec1SAndroid Build Coastguard Worker 
55*00c7fec1SAndroid Build Coastguard Worker 	return(s - src - 1);	/* count does not include NUL */
56*00c7fec1SAndroid Build Coastguard Worker }
57*00c7fec1SAndroid Build Coastguard Worker 
58*00c7fec1SAndroid Build Coastguard Worker #endif
59