-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
69 lines (62 loc) · 1.77 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: lbartels <lbartels@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/13 16:55:05 by lbartels #+# #+# */
/* Updated: 2023/11/21 14:35:16 by lbartels ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *set_buffer(int fd, char *str)
{
char *buffer;
int len;
buffer = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buffer)
return (NULL);
len = 1;
while (!check_new_line(str) && len != 0)
{
len = read(fd, buffer, BUFFER_SIZE);
if (len == -1)
{
free(str);
free(buffer);
return (NULL);
}
buffer[len] = '\0';
str = ft_strjoin(str, buffer);
}
free(buffer);
return (str);
}
char *get_next_line(int fd)
{
char *next_line;
static char *str;
if (fd < 0 || !BUFFER_SIZE)
return (NULL);
str = set_buffer(fd, str);
if (!str)
return (NULL);
next_line = set_next_line(str);
str = new_str(str);
return (next_line);
}
#include <string.h>
#include <stdio.h>
int main()
{
int fd = open("test.txt", O_RDONLY);
for (int i = 0; i < 4; i++)
{
char *str = get_next_line(fd);
printf("%s", str);
free(str);
}
close(fd);
return (0);
}