stpcpy.c 652 B

1234567891011121314151617181920212223242526272829
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <limits.h>
  5. #include "libc.h"
  6. #define ALIGN (sizeof(size_t))
  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. char *__stpcpy(char *restrict d, const char *restrict s)
  11. {
  12. size_t *wd;
  13. const size_t *ws;
  14. if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) {
  15. for (; (uintptr_t)s % ALIGN; s++, d++)
  16. if (!(*d=*s)) return d;
  17. wd=(void *)d; ws=(const void *)s;
  18. for (; !HASZERO(*ws); *wd++ = *ws++);
  19. d=(void *)wd; s=(const void *)ws;
  20. }
  21. for (; (*d=*s); s++, d++);
  22. return d;
  23. }
  24. weak_alias(__stpcpy, stpcpy);