-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
62 lines (57 loc) · 1.81 KB
/
get_next_line.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: itoth <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 16:10:56 by itoth #+# #+# */
/* Updated: 2017/02/01 17:39:49 by itoth ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int read_it(int const fd, char **stock)
{
char *aux;
int ret;
char *buff;
if (!(buff = (char *)malloc(sizeof(*buff) * (BUFF_SIZE + 1))))
return (-1);
ret = read(fd, buff, BUFF_SIZE);
if (ret > 0)
{
buff[ret] = '\0';
aux = ft_strjoin(*stock, buff);
free(*stock);
*stock = aux;
}
free(buff);
return (ret);
}
int get_next_line(int const fd, char **line)
{
static char *stock = NULL;
char *bn;
int ret;
if ((!stock && (stock = (char *)malloc(sizeof(*stock))) == NULL) || !line
|| fd < 0 || BUFF_SIZE < 0)
return (-1);
bn = ft_strchr(stock, '\n');
while (bn == NULL)
{
ret = read_it(fd, &stock);
if (ret == 0)
{
if (ft_strlen(stock) == 0)
return (0);
stock = ft_strjoin(stock, "\n");
}
if (ret < 0)
return (-1);
else
bn = ft_strchr(stock, '\n');
}
*line = ft_strsub(stock, 0, ft_strlen(stock) - ft_strlen(bn));
stock = ft_strdup(bn + 1);
return (1);
}