-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_gnl.c
62 lines (57 loc) · 1.84 KB
/
ft_gnl.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: thifranc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/11 14:18:03 by thifranc #+# #+# */
/* Updated: 2016/04/23 16:33:39 by thifranc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int make_line(char **buf, char **line, int fd)
{
char *tmp;
int i;
i = ft_get_char(buf[fd], '\n');
if (i >= 0)
{
buf[fd][i] = '\0';
*line = ft_strdup(buf[fd]);
tmp = ft_strdup(buf[fd] + i + 1);
ft_memdel((void**)&buf[fd]);
buf[fd] = ft_strdup(tmp);
ft_memdel((void**)&tmp);
return (1);
}
if (buf[fd] && buf[fd][0] != '\0')
{
*line = ft_strdup(buf[fd]);
ft_memdel((void**)&buf[fd]);
return (1);
}
return (0);
}
int ft_gnl(int fd, char **line)
{
char tmp[BUFF_SIZE + 1];
static char *buf[NB_FD];
int red;
char *tmp2;
if (fd < 0 || BUFF_SIZE < 1 || (red = read(fd, tmp, BUFF_SIZE)) == -1)
return (-1);
tmp[red] = '\0';
if (buf[fd])
{
tmp2 = ft_strdup(buf[fd]);
ft_memdel((void**)&buf[fd]);
buf[fd] = ft_strjoin(tmp2, tmp);
ft_memdel((void**)&tmp2);
}
if (!buf[fd])
buf[fd] = ft_strdup(tmp);
if (ft_get_char(buf[fd], '\n') != -1 || red == 0)
return (make_line(buf, &(*line), fd));
return (ft_gnl(fd, &(*line)));
}