-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
52 lines (47 loc) · 1.58 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: ffrau <ffrau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 18:42:13 by ffrau #+# #+# */
/* Updated: 2022/05/23 01:10:52 by ffrau ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_read_to_backup(int fd, char *backup)
{
char *buff;
int bytes;
buff = malloc(BUFFER_SIZE + 1);
if (!buff)
return (NULL);
bytes = 1;
while (!ft_strchr(backup, '\n') && bytes != 0)
{
bytes = read(fd, buff, BUFFER_SIZE);
if (bytes == -1)
{
free(buff);
return (NULL);
}
buff[bytes] = '\0';
backup = ft_strjoin(backup, buff);
}
free(buff);
return (backup);
}
char *get_next_line(int fd)
{
char *line;
static char *backup[4096];
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
backup[fd] = ft_read_to_backup(fd, backup[fd]);
if (!backup[fd])
return (NULL);
line = ft_get_line(backup[fd]);
backup[fd] = ft_backup(backup[fd]);
return (line);
}