-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_memmove.c
More file actions
55 lines (48 loc) · 1.7 KB
/
ft_memmove.c
File metadata and controls
55 lines (48 loc) · 1.7 KB
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
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/12 18:36:53 by dporhomo #+# #+# */
/* Updated: 2025/11/13 13:11:07 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void for_copy(unsigned char *dst, const unsigned char *src, size_t n);
static void back_copy(unsigned char *dst, const unsigned char *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *p_dst;
const unsigned char *p_src;
if (!dest && !src)
return (NULL);
if (n == 0 || dest == src)
return (dest);
p_dst = (unsigned char *)dest;
p_src = (const unsigned char *)src;
if (p_dst < p_src)
for_copy(p_dst, p_src, n);
else if (p_dst > p_src)
back_copy(p_dst, p_src, n);
return (dest);
}
static void for_copy(unsigned char *dst, const unsigned char *src, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
dst[i] = src[i];
i++;
}
}
static void back_copy(unsigned char *dst, const unsigned char *src, size_t n)
{
while (n > 0)
{
dst[n - 1] = src[n - 1];
n--;
}
}