-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathft_memccpy.c
50 lines (45 loc) · 1.62 KB
/
ft_memccpy.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
48
49
50
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 06:10:28 by mihykim #+# #+# */
/* Updated: 2020/04/03 21:59:52 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** - Copies bytes from string 'src' to string 'dst'.
** - Stops copying when 'c' converted to an unsigned char occurs in 'src'
** - Returns a pointer to the byte after the copy of c in 'dst',
** otherwise, returns a NULL pointer after n bytes are copied
*/
void *ft_memccpy(void *dst, const void *src, int c, size_t n)
{
size_t i;
int occured;
unsigned char *usrc;
usrc = (unsigned char *)src;
i = 0;
occured = 0;
while (i < n && usrc[i] && !occured)
{
occured = ((usrc)[i] == (unsigned char)c) ? 1 : 0;
i++;
}
if (occured)
{
ft_memcpy(dst, usrc, i);
return (dst + i);
}
else
{
ft_memcpy(dst, usrc, n);
return (0);
}
}
/*
** line 31 : add while condition 'i < n', preventioning from over searching.
*/