summaryrefslogtreecommitdiffstats
path: root/kernel/string.c
blob: 006b90746715ae4578178b4cccc9103cf148e9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "string.h"

size_t strlen(const char *str)
{
	size_t len = 0;
	while(str[len])
		len++;
	return len;
}

/*
	Copy bytes in memory. If copying takes place between objects
	that overlap, the behavior is undefined.
*/
void *memcpy(void *dest, const void *src, size_t n)
{
	size_t i;
	const char *s = src;
	char *d = dest;

	for(i = 0; i < n; i++)
		d[i] = s[i];
	return dest;
}

void *memset(void *dest, int c, size_t n)
{
	char *d = dest;
	while(n--)
		*d++ = c;
	return dest;
}

/*
	Copy bytes in memory with overlapping areas.
*/
void *memmove(void *dest, const void *src, size_t n)
{
	const char *s = src;
	char *d = dest;

	if(dest == src)
		return dest;

	if(dest < src)
		return memcpy(dest, src, n);

	while(n--)
		d[n] = s[n];
	return dest;
}