-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
42 lines (39 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ratavare <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 14:32:00 by ratavare #+# #+# */
/* Updated: 2023/02/16 16:48:59 by ratavare ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1];
char *line;
int flag;
int i;
int y;
if (BUFFER_SIZE < 1 || read(fd, 0, 0) < 0)
return (gnl_ft_freebuffer(buffer));
line = NULL;
flag = 0;
while (!flag && (buffer[0] || (read(fd, buffer, BUFFER_SIZE) > 0)))
{
i = 0;
y = 0;
line = gnl_ft_strjoin(line, buffer);
while (buffer[i])
{
if (flag)
buffer[y++] = buffer[i];
if (buffer[i] == '\n')
flag = 1;
buffer[i++] = 0;
}
}
return (line);
}