-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
48 lines (45 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wboutzou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/26 00:32:10 by wboutzou #+# #+# */
/* Updated: 2022/06/26 00:32:14 by wboutzou ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char *get_next_line(int fd)
{
char *buff;
char *saver;
int rd;
if (fd < 0)
return (0);
rd = 1;
saver = NULL;
buff = (char *) malloc(2 * sizeof(char));
if (!buff)
return (NULL);
buff[0] = 0;
while (rd > 0)
{
rd = read(fd, buff, 1);
if (rd < 0)
{
free(buff);
return (0);
}
buff[rd] = 0;
saver = ft_strjoin(saver, buff);
}
free(buff);
if (rd == 0 && saver[0] == '\0')
{
free(saver);
saver = NULL;
return (0);
}
return (saver);
}