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