This repository has been archived by the owner on Jun 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
60 lines (55 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fbenneto <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/29 11:15:13 by fbenneto #+# #+# */
/* Updated: 2017/11/30 11:10:39 by fbenneto ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int ft_read_check(int fd, char *t[])
{
char buff[BUFF_SIZE + 1];
char *tmp;
int res;
res = 1;
ft_bzero(buff, BUFF_SIZE);
while (!(ft_strchr(t[fd], '\n')) && (res = read(fd, buff, BUFF_SIZE)) > 0)
{
buff[res] = '\0';
tmp = t[fd];
if (!(t[fd] = ft_strjoin(t[fd], buff)))
return (-1);
ft_strdel(&tmp);
}
return (res);
}
int get_next_line(const int fd, char **line)
{
static char *save[OPEN_MAX + 1];
char *tmp;
int res;
if (line == NULL || fd < 0 || fd > OPEN_MAX)
return (-1);
if (save[fd] == NULL && (save[fd] = ft_strnew(0)) == NULL)
return (-1);
if ((res = ft_read_check(fd, save)) == -1)
return (-1);
if (res == 0)
{
if (!(*line = ft_strdup(save[fd])))
return (-1);
ft_strdel(save + fd);
return (!!(*line));
}
if (!(*line = ft_strsub(save[fd], 0, ft_strchr(save[fd], '\n') - save[fd])))
return (-1);
tmp = save[fd];
if (!(save[fd] = ft_strdup(ft_strchr(save[fd], '\n') + 1)))
return (-1);
ft_strdel(&tmp);
return (1);
}