-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
71 lines (65 loc) · 1.92 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line.c :+: :+: */
/* +:+ */
/* By: rcappend <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/30 10:26:46 by rcappend #+# #+# */
/* Updated: 2020/11/30 12:37:44 by rcappend ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ret_check(int ret, char **line)
{
if (ret == -1)
{
free(*line);
return (ret);
}
return (ret);
}
int ft_strjoin(char **line, char *buff)
{
char *new;
char *index;
int newl;
if (ft_strclen(buff, 0) == 0)
return (0);
new = (char *)malloc(ft_strclen(buff, '\n') + ft_strclen(*line, 0) + 1);
if (!new)
return (-1);
index = ft_memccpy(new, *line, 0);
index = ft_memccpy(index, buff, '\n');
free(*line);
*line = new;
newl = ft_strchr(buff, '\n') == NULL ? 0 : 1;
ft_memmove(buff, index);
return (newl);
}
int get_next_line(int fd, char **line)
{
static char buff[BUFFER_SIZE + 1];
int bytes;
int ret;
if (fd < 0 || BUFFER_SIZE <= 0 || !line)
return (-1);
*line = (char *)malloc(1);
if (!*line)
return (-1);
*line[0] = '\0';
ret = ft_strjoin(line, buff);
if (ret)
return (ret_check(ret, line));
bytes = 1;
while (bytes > 0)
{
bytes = read(fd, buff, BUFFER_SIZE);
if (bytes == -1)
return (-1);
ret = ft_strjoin(line, buff);
if (ret)
return (ret_check(ret, line));
}
return (0);
}