-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
54 lines (49 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abouabra < [email protected] > +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/13 15:37:28 by abouabra #+# #+# */
/* Updated: 2022/11/13 15:50:18 by abouabra ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *read_fd(int fd, char *line)
{
char *str;
int counter;
str = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!str)
return (0);
counter = 1;
while (!ft_strchr(line, '\n') && counter != 0)
{
counter = read(fd, str, BUFFER_SIZE);
if (counter == -1)
{
free(str);
free(line);
return (0);
}
str[counter] = '\0';
line = ft_strjoin(line, str);
}
free(str);
return (line);
}
char *get_next_line(int fd)
{
static char *line;
char *final;
if (fd < 0 || fd == 1 || fd == 2 || BUFFER_SIZE <= 0)
return (0);
final = 0;
line = read_fd(fd, line);
if (!line)
return (0);
final = set_the_line(line);
line = set_next_line(line);
return (final);
}