-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
55 lines (50 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:21:10 by dvan-der #+# #+# */
/* Updated: 2021/12/15 08:21:11 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "libft.h"
static char *letsgo(char *line, int fd, char *buffer)
{
int size_line;
int buffer_size;
size_line = 0;
buffer_size = check_buffer(buffer);
size_line += buffer_size;
line = add_buffer(line, buffer, buffer_size, size_line);
if (!check_line(line))
return (line);
while (read(fd, buffer, BUFFER_SIZE))
{
buffer_size = check_buffer(buffer);
size_line += buffer_size;
line = add_buffer(line, buffer, buffer_size, size_line);
if (!check_line(line))
break ;
}
return (line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE];
char *line;
if (fd < 0 || fd > 1024 || read(fd, buffer, 0) == -1)
return (NULL);
line = (char *)malloc(sizeof(char));
ft_check_malloc(line, "get_next_line");
line[0] = '\0';
line = letsgo(line, fd, buffer);
if (!*line)
{
free(line);
return (NULL);
}
return (line);
}