-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
57 lines (53 loc) · 1.13 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
#include "get_next_line.h"
char *ft_read(int fd, char *str)
{
char *buff;
int bytes;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (NULL);
bytes = 1;
while (!ft_strchr(str, '\n') && bytes != 0)
{
bytes = read(fd, buff, BUFFER_SIZE);
if (bytes == -1)
{
free(buff);
return (NULL);
}
buff[bytes] = '\0';
str = ft_strjoin(str, buff);
}
free(buff);
return (str);
}
char *get_next_line(int fd)
{
char *line;
static char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
str = ft_read(fd, str);
if (!str)
return (NULL);
line = ft_line(str);
str = ft_next_str(str);
return (line);
}
/*int main(void)
{
int fd;
char *tab;
fd = open("test.txt", O_RDONLY);
tab = get_next_line(fd);
printf("%s", tab);
free(tab);
tab = get_next_line(fd);
printf("%s", tab);
tab = get_next_line(fd);
printf("%s", tab);
tab = get_next_line(fd);
printf("%s", tab);
close(fd);
return (0);
}*/