aboutsummaryrefslogtreecommitdiffstats
path: root/libft/src/ft_memmove.c
blob: 1aa35883ac8763255f10c5cb58ad76c0bc6fbddb (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_memmove.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:06:42 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:06:42 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stddef.h>

void
	*ft_memmove(void *dst, const void *src, size_t len)
{
	const char	*from = (const char*)src;
	char		*to;
	int			i;
	size_t		j;

	to = (char*)dst;
	if (to > from && to - from < (int)len)
	{
		i = len;
		while (--i >= 0)
			to[i] = from[i];
		return (dst);
	}
	if (from > to && from - to < (int)len)
	{
		j = -1;
		while (++j < len)
			to[j] = from[j];
		return (dst);
	}
	ft_memcpy(dst, src, len);
	return (dst);
}