-
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.54 KB
/
get_next_line.c
File metadata and controls
53 lines (48 loc) · 1.54 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: ctoptas <ctoptas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/17 16:37:44 by eozdur #+# #+# */
/* Updated: 2022/12/22 17:53:19 by ctoptas ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_buff(int fd, char *str)
{
int rd_bytes;
char *buff;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (NULL);
rd_bytes = 1;
while (!ft_strchr(str, '\n') && rd_bytes != 0)
{
rd_bytes = read(fd, buff, BUFFER_SIZE);
if (rd_bytes == -1)
{
free(str);
free(buff);
return (NULL);
}
buff[rd_bytes] = '\0';
str = ft_strjoin(str, buff);
}
free(buff);
return (str);
}
char *get_next_line(int fd)
{
char *line;
static char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
str = ft_buff(fd, str);
if (!str)
return (NULL);
line = ft_get_line(str);
str = ft_last_str(str);
return (line);
}