-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strndup.c
executable file
·48 lines (43 loc) · 1.39 KB
/
ft_strndup.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/18 15:14:55 by akharrou #+# #+# */
/* Updated: 2019/05/19 20:05:04 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Includes/stdlib_42.h"
#include "../Includes/string_42.h"
char *ft_strndup(const char *s1, size_t n)
{
char *dest;
size_t len;
size_t i;
dest = NULL;
if (s1)
{
len = ft_strnlen(s1, n);
dest = malloc(len + 1);
if (!dest)
return (NULL);
i = 0;
while (i < len)
{
dest[i] = s1[i];
i++;
}
dest[i] = '\0';
}
return (dest);
}
char *ft_strndupfre(const char *src, size_t n, int free_src)
{
char *res;
res = ft_strndup(src, n);
if (free_src && src)
free((void *)src);
return (res);
}