-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
executable file
·80 lines (73 loc) · 1.99 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* LE - / */
/* / */
/* get_next_line.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: brobicho <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/06 16:52:44 by brobicho #+# ## ## #+# */
/* Updated: 2018/02/03 16:50:26 by brobicho ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static int ft_read(char **str, int fd)
{
int ret;
char *tmp;
char buf[BUFF_SIZE + 1];
if ((ret = read(fd, buf, BUFF_SIZE)) == -1)
return (-1);
buf[ret] = '\0';
tmp = *str;
*str = ft_strjoin(*str, buf);
if (*tmp)
free(tmp);
return (ret);
}
static int ft_getline(char **str, char **line, char *s)
{
int ret;
char *tmp;
ret = 0;
if (*s == '\n')
ret = 1;
*s = 0;
*line = ft_strjoin("", *str);
if (ret == 0 && ft_strlen(*str))
{
*str = ft_strnew(1);
return (1);
}
else if (ret == 0 && !(ft_strlen(*str)))
return (0);
tmp = *str;
*str = ft_strjoin(s + 1, "");
free(tmp);
return (ret);
}
int get_next_line(const int fd, char **line)
{
int ret;
char *tmp;
static char *str;
if (!str)
str = "";
if (!line || BUFF_SIZE < 1)
return (-1);
ret = BUFF_SIZE;
while (line)
{
tmp = str;
while (*tmp || ret < BUFF_SIZE)
{
if (*tmp == '\n' || !*tmp || *tmp == -1)
return (ft_getline(&str, line, tmp));
tmp++;
}
ret = ft_read(&str, fd);
if (ret == -1)
return (-1);
}
return (0);
}