libc_string.c 831 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <stddef.h>
  6. void *memcpy(void * UA_RESTRICT dest, const void *UA_RESTRICT src, size_t n) {
  7. unsigned char *d = dest;
  8. const unsigned char *s = src;
  9. while(n--)
  10. *d++ = *s++;
  11. return dest;
  12. }
  13. void *memset(void *dest, int c, size_t n) {
  14. unsigned char c8 = n;
  15. unsigned char *target = dest;
  16. while(n--)
  17. *target++ = c8;
  18. return dest;
  19. }
  20. size_t strlen(const char *str) {
  21. size_t len = 0;
  22. for(const char *s = str; *s; s++)
  23. len += 1;
  24. return len;
  25. }
  26. int memcmp(const void *vl, const void *vr, size_t n) {
  27. const unsigned char *l = vl, *r = vr;
  28. for(; n && *l == *r; n--, l++, r++);
  29. return n ? *l-*r : 0;
  30. }