-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
53 lines (48 loc) · 1.56 KB
/
get_next_line.c
File metadata and controls
53 lines (48 loc) · 1.56 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ogenc <ogenc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/31 12:15:59 by ogenc #+# #+# */
/* Updated: 2023/01/31 15:21:49 by ogenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *read_line(int fd, char *buffer)
{
char *buf;
int rdbyte;
rdbyte = 1;
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (NULL);
while (!ft_checknewline(buffer) && rdbyte != 0)
{
rdbyte = read(fd, buf, BUFFER_SIZE);
if (rdbyte == -1)
{
free(buffer);
free(buf);
return (NULL);
}
buf[rdbyte] = '\0';
buffer = ft_strjoin(buffer, buf);
}
free(buf);
return (buffer);
}
char *get_next_line(int fd)
{
static char *buffer;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
buffer = read_line(fd, buffer);
if (buffer == NULL)
return (NULL);
line = ft_newline(buffer);
buffer = new_buffer(buffer);
return (line);
}