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