-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
69 lines (62 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: babreton <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/26 11:46:34 by babreton #+# #+# */
/* Updated: 2023/02/28 11:54:45 by babreton ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_strlen(char *s)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i])
i++;
return (i);
}
char *get_next_line(int fd)
{
static char *temp;
char *buffer;
int bytes_read;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
return (free(temp), temp = NULL, NULL);
if (have_n(temp))
return (print_line(&temp));
buffer = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buffer)
return (NULL);
bytes_read = 1;
while (bytes_read > 0)
{
bytes_read = read(fd, buffer, BUFFER_SIZE);
buffer[bytes_read] = 0;
temp = ft_strjoin(temp, buffer);
if (have_n(temp))
break ;
}
if (buffer)
free(buffer);
buffer = NULL;
return (print_line(&temp));
}
/*int main(void)
{
int fd = open("fd", O_RDONLY);
char *str;
int i = 0;
while (i < 10)
{
str = get_next_line(fd);
printf("%s", str);
free(str);
i++;
}
return (0);
}*/