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