blob: 39ba9726ed6d0c6ac9166a63c151966de8fdc229 [file] [log] [blame]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01001#include "std.h"
2
3void *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 */
14size_t strlen(const char *str)
15{
16 const char *p = str;
17 while (*p)
18 p++;
19 return p - str;
20}
21
22void *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
36void *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
56int 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
71int 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}