36 lines
1.3 KiB
C
36 lines
1.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_substr.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/09/28 16:33:43 by erey-bet #+# #+# */
|
|
/* Updated: 2022/10/11 22:09:26 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
|
{
|
|
long size;
|
|
char *new_s;
|
|
|
|
if (s == NULL)
|
|
return (NULL);
|
|
size = ft_strlen(s) - start;
|
|
if (size < 0)
|
|
size = 0;
|
|
else if ((unsigned long)size > len)
|
|
size = len;
|
|
new_s = malloc(size + 1);
|
|
if (new_s == NULL)
|
|
return (NULL);
|
|
if (start <= ft_strlen(s))
|
|
ft_strlcpy(new_s, s + start, len + 1);
|
|
else
|
|
new_s[0] = '\0';
|
|
return (new_s);
|
|
}
|