xref: /aosp_15_r20/external/musl/src/string/stpncpy.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1*c9945492SAndroid Build Coastguard Worker #include <string.h>
2*c9945492SAndroid Build Coastguard Worker #include <stdint.h>
3*c9945492SAndroid Build Coastguard Worker #include <limits.h>
4*c9945492SAndroid Build Coastguard Worker 
5*c9945492SAndroid Build Coastguard Worker #define ALIGN (sizeof(size_t)-1)
6*c9945492SAndroid Build Coastguard Worker #define ONES ((size_t)-1/UCHAR_MAX)
7*c9945492SAndroid Build Coastguard Worker #define HIGHS (ONES * (UCHAR_MAX/2+1))
8*c9945492SAndroid Build Coastguard Worker #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9*c9945492SAndroid Build Coastguard Worker 
__stpncpy(char * restrict d,const char * restrict s,size_t n)10*c9945492SAndroid Build Coastguard Worker char *__stpncpy(char *restrict d, const char *restrict s, size_t n)
11*c9945492SAndroid Build Coastguard Worker {
12*c9945492SAndroid Build Coastguard Worker #ifdef __GNUC__
13*c9945492SAndroid Build Coastguard Worker 	typedef size_t __attribute__((__may_alias__)) word;
14*c9945492SAndroid Build Coastguard Worker 	word *wd;
15*c9945492SAndroid Build Coastguard Worker 	const word *ws;
16*c9945492SAndroid Build Coastguard Worker 	if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
17*c9945492SAndroid Build Coastguard Worker 		for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
18*c9945492SAndroid Build Coastguard Worker 		if (!n || !*s) goto tail;
19*c9945492SAndroid Build Coastguard Worker 		wd=(void *)d; ws=(const void *)s;
20*c9945492SAndroid Build Coastguard Worker 		for (; n>=sizeof(size_t) && !HASZERO(*ws);
21*c9945492SAndroid Build Coastguard Worker 		       n-=sizeof(size_t), ws++, wd++) *wd = *ws;
22*c9945492SAndroid Build Coastguard Worker 		d=(void *)wd; s=(const void *)ws;
23*c9945492SAndroid Build Coastguard Worker 	}
24*c9945492SAndroid Build Coastguard Worker #endif
25*c9945492SAndroid Build Coastguard Worker 	for (; n && (*d=*s); n--, s++, d++);
26*c9945492SAndroid Build Coastguard Worker tail:
27*c9945492SAndroid Build Coastguard Worker 	memset(d, 0, n);
28*c9945492SAndroid Build Coastguard Worker 	return d;
29*c9945492SAndroid Build Coastguard Worker }
30*c9945492SAndroid Build Coastguard Worker 
31*c9945492SAndroid Build Coastguard Worker weak_alias(__stpncpy, stpncpy);
32*c9945492SAndroid Build Coastguard Worker 
33