-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
40 lines (37 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsoares- <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 17:13:48 by vsoares- #+# #+# */
/* Updated: 2024/12/02 16:07:30 by vsoares- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_next_line(int fd)
{
static char buf[FOPEN_MAX][BUFFER_SIZE + 1];
char *line;
int c_read;
if (fd < 0 || fd >= FOPEN_MAX || BUFFER_SIZE < 1)
return (NULL);
line = NULL;
if (buf[fd][0])
c_read = 1;
else
c_read = read(fd, buf[fd], BUFFER_SIZE);
while (c_read > 0)
{
line = str_append(line, buf[fd]);
if (!line)
return (NULL);
if (line[line_len(line)] == '\n')
return (line);
c_read = read(fd, buf[fd], BUFFER_SIZE);
}
if (c_read < 0)
return (free(line), NULL);
return (line);
}