-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strndup.c
More file actions
37 lines (34 loc) · 1.26 KB
/
ft_strndup.c
File metadata and controls
37 lines (34 loc) · 1.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tyassine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 18:33:11 by tyassine #+# #+# */
/* Updated: 2016/11/07 18:33:14 by tyassine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
char *ft_strndup(const char *str, size_t n)
{
size_t len;
size_t i;
char *new;
len = ft_strlen(str);
if (!(*str))
return (NULL);
if (len <= n)
return (new = ft_strdup(str));
i = 0;
if (!(new = (char *)malloc(sizeof(char) * (n + 1))))
return (NULL);
while (str[i] && i < n)
{
new[i] = str[i];
i++;
}
new[i] = '\0';
return (new);
}