rt/libft/ft_strndup.c

38 lines
1.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: scebula <scebula@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 02:19:00 by scebula #+# #+# */
/* Updated: 2015/12/03 16:35:35 by scebula ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strndup(const char *s, size_t n)
{
int l;
int i;
char *str;
size_t length;
i = 0;
if (!s)
return (NULL);
length = ft_strlen(s);
l = (n < length) ? n : length;
str = (char *)malloc(sizeof(char) * (l + 1));
if (str == NULL)
return (NULL);
while (i < l)
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}