-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
74 lines (68 loc) · 1.8 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
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abertran <abertran@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 15:18:42 by abertran #+# #+# */
/* Updated: 2023/01/09 18:58:57 by abertran ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_read(int fd, char *str)
{
char *bff;
int bytes;
bff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!bff)
return (NULL);
bytes = 1;
while (!ft_strchr(str, '\n') && bytes != 0)
{
bytes = read(fd, bff, BUFFER_SIZE);
if (bytes == -1)
{
free(bff);
free(str);
return (NULL);
}
bff[bytes] = '\0';
str = ft_strjoin(str, bff);
}
free(bff);
return (str);
}
char *get_next_line(int fd)
{
char *ln;
static char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
str = ft_read(fd, str);
if (!str)
return (0);
ln = ft_line(str);
str = ft_nextstr(str);
return (ln);
}
/*
int main(void)
{
int fd;
char *tab;
fd = open("hola", O_RDONLY);
//fd = 0;
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);
}
*/