#include #include #define HIGHS(x) ((x) - ((x)*0-1) / 255 * 128) #define LOWS(x) (((x)*0-1) / 255) #define has_char(x, c) ((x) - LOWS(x) & ~(x) & HIGHS(x) ^ LOWS(x) * (c)) void *memccpy(void *restrict d, const void *restrict s, int c, size_t n) { unsigned char *cd = (unsigned char *)d; const unsigned char *cs = (const unsigned char *)s; size_t *wd; const size_t *ws; c = (unsigned char)c; if ((uintptr_t)s % sizeof(size_t) != (uintptr_t)d % sizeof(size_t) || n < sizeof(size_t)) goto bytewise; for (; (uintptr_t)s % sizeof(size_t); *cd = *cs, cd++, cs++) if (*cs == c) return cd + 1; for (wd = (size_t *)d, ws = (const size_t *)s ; !has_char(*ws, c) && n >= sizeof(size_t) ; ws++, wd++, *wd = *ws); cd = (unsigned char *)wd; cs = (const unsigned char *)ws; bytewise: for (; *cs != c; *cd = *cs, cs++, cd++, n--) if (!n) return NULL; return cd + 1; }