util.c 827 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "util.h"
  2. void memory_copy(char *source, char *dest, int nbytes) {
  3. int i;
  4. for (i = 0; i < nbytes; i++) {
  5. *(dest + i) = *(source + i);
  6. }
  7. }
  8. void memory_set(u8 *dest, u8 val, u32 len) {
  9. u8 *temp = (u8 *)dest;
  10. for ( ; len != 0; len--) *temp++ = val;
  11. }
  12. /**
  13. * K&R implementation
  14. */
  15. void int_to_ascii(int n, char str[]) {
  16. int i, sign;
  17. if ((sign = n) < 0) n = -n;
  18. i = 0;
  19. do {
  20. str[i++] = n % 10 + '0';
  21. } while ((n /= 10) > 0);
  22. if (sign < 0) str[i++] = '-';
  23. str[i] = '\0';
  24. reverse(str);
  25. }
  26. /* K&R */
  27. void reverse(char s[]) {
  28. int c, i, j;
  29. for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
  30. c = s[i];
  31. s[i] = s[j];
  32. s[j] = c;
  33. }
  34. }
  35. /* K&R */
  36. int strlen(char s[]) {
  37. int i = 0;
  38. while (s[i] != '\0') ++i;
  39. return i;
  40. }