-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_memmove.c
47 lines (43 loc) · 1.7 KB
/
ft_memmove.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 10:49:44 by lclerc #+# #+# */
/* Updated: 2022/11/23 16:17:45 by lclerc ### ########.fr */
/* */
/* ************************************************************************** */
/*
* The memmove() function copies len bytes from string src to string dst. the 2
* strings may overlap. If the beginning of destination overlaps with end of the
* source, copying should be done backward.
*
* The difference here with memcpy() is that if the source pointer is located
* before the destination pointer, it is needed to start copying from 'len'
* to avoid overlapping.
*
*/
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
size_t i;
char *source;
char *destination;
i = 0;
source = (char *)src;
destination = (char *)dst;
if (!source && !destination)
return (NULL);
if (source < destination)
{
source = source + len - 1;
destination = destination + len - 1;
while (len--)
*destination-- = *source--;
}
else
ft_memcpy(destination, source, len);
return (dst);
}