-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
63 lines (57 loc) · 1.77 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
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pgomez-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/02 09:52:58 by pgomez-a #+# #+# */
/* Updated: 2021/02/25 13:05:24 by pgomez-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int gnl_find_eof(char **res, char **line)
{
*line = ft_strdup(*res);
free(*res);
*res = NULL;
return (0);
}
static int gnl_find_nl(char **res, char **line)
{
char *temp_1;
char *temp_2;
temp_1 = *res;
temp_2 = ft_strchr(*res, '\n');
*temp_2 = '\0';
temp_2++;
*line = ft_strdup(*res);
*res = ft_strdup(temp_2);
free(temp_1);
return (1);
}
int get_next_line(int fd, char **line)
{
static char *res;
char *temp;
char buff[BUFFER_SIZE + 1];
int num;
if (fd < 0 || BUFFER_SIZE <= 0 || !line)
return (-1);
if (!res)
res = ft_strdup("");
while (!(ft_strchr(res, '\n')) && (num = read(fd, buff, BUFFER_SIZE)) > 0)
{
buff[num] = '\0';
temp = res;
res = ft_strjoin(res, buff);
free(temp);
}
if (ft_strchr(res, '\n'))
return (gnl_find_nl(&res, line));
if (num == 0)
return (gnl_find_eof(&res, line));
free(res);
res = NULL;
return (-1);
}