-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
52 lines (47 loc) · 1.6 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ygunay <ygunay@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/16 13:50:47 by ygunay #+# #+# */
/* Updated: 2022/08/16 14:18:18 by ygunay ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_read_file(int fd, char *buffer)
{
char *buff;
int rd_bytes;
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
return (NULL);
rd_bytes = 1;
while (!ft_strchr(buffer, '\n') && rd_bytes != 0)
{
rd_bytes = read(fd, buff, BUFFER_SIZE);
if (rd_bytes == -1)
{
free(buff);
return (NULL);
}
buff[rd_bytes] = '\0';
buffer = ft_strjoin(buffer, buff);
}
free(buff);
return (buffer);
}
char *get_next_line(int fd)
{
char *line;
static char *buffer[OPEN_MAX];
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
buffer[fd] = ft_read_file(fd, buffer[fd]);
if (!buffer[fd])
return (NULL);
line = ft_get_line(buffer[fd]);
buffer[fd] = ft_next(buffer[fd]);
return (line);
}