reduce spurious inclusion of libc.h
[musl] / src / string / stpncpy.c
1 #include <string.h>
2 #include <stdint.h>
3 #include <limits.h>
4
5 #define ALIGN (sizeof(size_t)-1)
6 #define ONES ((size_t)-1/UCHAR_MAX)
7 #define HIGHS (ONES * (UCHAR_MAX/2+1))
8 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9
10 char *__stpncpy(char *restrict d, const char *restrict s, size_t n)
11 {
12         size_t *wd;
13         const size_t *ws;
14
15         if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
16                 for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
17                 if (!n || !*s) goto tail;
18                 wd=(void *)d; ws=(const void *)s;
19                 for (; n>=sizeof(size_t) && !HASZERO(*ws);
20                        n-=sizeof(size_t), ws++, wd++) *wd = *ws;
21                 d=(void *)wd; s=(const void *)ws;
22         }
23         for (; n && (*d=*s); n--, s++, d++);
24 tail:
25         memset(d, 0, n);
26         return d;
27 }
28
29 weak_alias(__stpncpy, stpncpy);
30