Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame^] | 1 | #include "std.h" |
| 2 | |
| 3 | void *memset(void *s, int c, size_t n) |
| 4 | { |
| 5 | char *p = (char *)s; |
| 6 | while (n--) |
| 7 | *p++ = c; |
| 8 | return s; |
| 9 | } |
| 10 | |
| 11 | /* |
| 12 | * Calculates the length of the provided null-terminated string. |
| 13 | */ |
| 14 | size_t strlen(const char *str) |
| 15 | { |
| 16 | const char *p = str; |
| 17 | while (*p) |
| 18 | p++; |
| 19 | return p - str; |
| 20 | } |
| 21 | |
| 22 | void *memcpy(void *dst, const void *src, size_t n) |
| 23 | { |
| 24 | char *x = dst; |
| 25 | const char *y = src; |
| 26 | |
| 27 | while (n--) { |
| 28 | *x = *y; |
| 29 | x++; |
| 30 | y++; |
| 31 | } |
| 32 | |
| 33 | return dst; |
| 34 | } |
| 35 | |
| 36 | void *memmove(void *dst, const void *src, size_t n) |
| 37 | { |
| 38 | char *x; |
| 39 | const char *y; |
| 40 | |
| 41 | if (dst < src) |
| 42 | return memcpy(dst, src, n); |
| 43 | |
| 44 | x = (char *)dst + n - 1; |
| 45 | y = (const char *)src + n - 1; |
| 46 | |
| 47 | while (n--) { |
| 48 | *x = *y; |
| 49 | x--; |
| 50 | y--; |
| 51 | } |
| 52 | |
| 53 | return dst; |
| 54 | } |
| 55 | |
| 56 | int memcmp(const void *a, const void *b, size_t n) |
| 57 | { |
| 58 | const char *x = a; |
| 59 | const char *y = b; |
| 60 | |
| 61 | while (n--) { |
| 62 | if (*x != *y) |
| 63 | return *x - *y; |
| 64 | x++; |
| 65 | y++; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int strcmp(const char *a, const char *b) |
| 72 | { |
| 73 | const char *x = a; |
| 74 | const char *y = b; |
| 75 | |
| 76 | while (*x != 0 && *y != 0) { |
| 77 | if (*x != *y) |
| 78 | return *x - *y; |
| 79 | x++; |
| 80 | y++; |
| 81 | } |
| 82 | |
| 83 | return *x - *y; |
| 84 | } |