42_get_next_line/get_next_line_utils.c
Camille Chauvet 07565c2c6c d
2022-10-26 02:27:09 +02:00

94 lines
1.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/26 01:24:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *tab;
size_t i;
if (nmemb == 0 || size * nmemb / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
i = 0;
while (i < size * nmemb)
{
tab[i] = 0;
i++;
}
return ((void *) tab);
}
void *ft_realloc(void *ptr, size_t size)
{
char *tab;
char *in;
size_t i;
in = ptr;
tab = ft_calloc(sizeof(char), size);
if (tab == NULL)
return (NULL);
if (ptr == NULL)
return (tab);
i = 0;
while (in[i] != 0 && i < size)
{
tab[i] = in[i];
i++;
}
free(ptr);
return (tab);
}
char *ft_strfcat(char *dst, char *src)
{
size_t i;
size_t j;
j = 0;
while (dst[j] != '\0')
j++;
i = 0;
if (src != NULL)
{
while (src[i] != '\0')
{
dst[i + j] = src[i];
i++;
}
}
dst[i] = '\0';
free(src);
return (dst);
}
char *ft_strndup(char *src, size_t n)
{
char *out;
size_t i;
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (src[i] != '\0' && i < n)
{
out[i] = src[i];
i++;
}
return (out);
}