-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
52 lines (48 loc) · 1.59 KB
/
get_next_line.c
File metadata and controls
52 lines (48 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line.c :+: :+: */
/* +:+ */
/* By: anonymous <anonymous@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/11/26 17:27:02 by anonymous #+# #+# */
/* Updated: 2020/12/12 21:52:30 by ivork ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include "get_next_line.h"
int ft_read(int fd, char *buff, int ret)
{
ret = read(fd, buff, BUFFER_SIZE);
if (ret < 0)
return (-1);
buff[ret] = '\0';
return (ret);
}
int get_next_line(int fd, char **line)
{
static char buff[BUFFER_SIZE + 1];
int ret;
if (BUFFER_SIZE <= 0 || fd < 0 || line == NULL)
return (-1);
ret = 1;
*line = malloc(sizeof(char));
if (*line == NULL)
return (-1);
*line[0] = '\0';
while (ret)
{
if (!*buff)
ret = ft_read(fd, buff, ret);
if (ret < 0)
return (-1);
*line = ft_strcjoin(*line, buff);
if (*line == NULL)
return (-1);
if (ft_memmove(buff, buff + (ft_strchr(buff, '\n'))))
return (1);
ft_bzero(buff);
}
return (0);
}