stpcpy.c 685 B

1234567891011121314151617181920212223242526272829
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <limits.h>
  4. #define ALIGN (sizeof(size_t))
  5. #define ONES ((size_t)-1/UCHAR_MAX)
  6. #define HIGHS (ONES * (UCHAR_MAX/2+1))
  7. #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
  8. char *__stpcpy(char *restrict d, const char *restrict s)
  9. {
  10. #ifdef __GNUC__
  11. typedef size_t __attribute__((__may_alias__)) word;
  12. word *wd;
  13. const word *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. #endif
  22. for (; (*d=*s); s++, d++);
  23. return d;
  24. }
  25. weak_alias(__stpcpy, stpcpy);