1570af302Sopenharmony_ci#define _BSD_SOURCE 2570af302Sopenharmony_ci#include <string.h> 3570af302Sopenharmony_ci#include <stdint.h> 4570af302Sopenharmony_ci#include <limits.h> 5570af302Sopenharmony_ci 6570af302Sopenharmony_ci#define ALIGN (sizeof(size_t)-1) 7570af302Sopenharmony_ci#define ONES ((size_t)-1/UCHAR_MAX) 8570af302Sopenharmony_ci#define HIGHS (ONES * (UCHAR_MAX/2+1)) 9570af302Sopenharmony_ci#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) 10570af302Sopenharmony_ci 11570af302Sopenharmony_cisize_t strlcpy(char *d, const char *s, size_t n) 12570af302Sopenharmony_ci{ 13570af302Sopenharmony_ci char *d0 = d; 14570af302Sopenharmony_ci size_t *wd; 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci if (!n--) goto finish; 17570af302Sopenharmony_ci#ifdef __GNUC__ 18570af302Sopenharmony_ci typedef size_t __attribute__((__may_alias__)) word; 19570af302Sopenharmony_ci const word *ws; 20570af302Sopenharmony_ci if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { 21570af302Sopenharmony_ci for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); 22570af302Sopenharmony_ci if (n && *s) { 23570af302Sopenharmony_ci wd=(void *)d; ws=(const void *)s; 24570af302Sopenharmony_ci for (; n>=sizeof(size_t) && !HASZERO(*ws); 25570af302Sopenharmony_ci n-=sizeof(size_t), ws++, wd++) *wd = *ws; 26570af302Sopenharmony_ci d=(void *)wd; s=(const void *)ws; 27570af302Sopenharmony_ci } 28570af302Sopenharmony_ci } 29570af302Sopenharmony_ci#endif 30570af302Sopenharmony_ci for (; n && (*d=*s); n--, s++, d++); 31570af302Sopenharmony_ci *d = 0; 32570af302Sopenharmony_cifinish: 33570af302Sopenharmony_ci return d-d0 + strlen(s); 34570af302Sopenharmony_ci} 35