aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
blob: a5c2288ec178bd51df3048b31e903629f2b716ac (plain)
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
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
#include "get_next_line.h"

static unsigned int cursor;

static void *ft_memcat(void *ptr, void *tail,
                        unsigned int size, unsigned int tail_size);

int get_next_line(const int fd, char **line)
{
    unsigned int    newline_i;
    char            buf[BUFF_SIZE];

    if (read(fd, buf, BUFF_SIZE) < 0)
        return (-1);
    if ((*line = ft_strnew(0)) == NULL)
        return (-1);
    newline_i = 0;
    while (buf[newline_i])
    {
        while (buf[newline_i] != '\n')
            newline_i++;
        ft_memcat(*line, buf, 0, newline_i);
        if (read(fd, buf, BUFF_SIZE) < 0)
            return (-1);
    }
    return (0);
}

static void *ft_memcat(void *ptr, void *tail,
                        unsigned int size, unsigned int tail_size)
{
    void *copy;

    if ((copy = malloc(size)) == NULL)
        return (NULL);
    ft_memcpy(copy, ptr, size);
    free(ptr);
    if ((ptr = malloc(size + tail_size)) == NULL)
        return (NULL);
    ft_memcpy(ptr, copy, size);
    free(copy);
    ft_memcpy(ptr + size, tail, tail_size);
    return (ptr);
}