-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
executable file
·82 lines (74 loc) · 2.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mberger <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/14 17:48:32 by mberger #+# #+# */
/* Updated: 2015/03/24 04:29:36 by mberger ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
#include <fcntl.h>
char *yo_realloc(char *s1, size_t size)
{
char *new;
size_t len;
len = ft_strlen(s1);
if (!(new = (char*)malloc(sizeof(char*) * (len + size))))
return (NULL);
if (new == NULL)
return (NULL);
new = ft_strncpy(new, s1, len);
return (new);
}
char *ft_traitement(char **hey, int *res)
{
int i;
char *lu;
i = 0;
if (!ft_strcmp(*hey, ""))
{
(*res) = 0;
return (ft_strdup(""));
}
while ((*hey)[i] != '\n' && (*hey)[i] != '\0')
i++;
lu = ft_strnew(i);
ft_strncpy(lu, (*hey), i);
(*res) = 1;
if (ft_strchr((*hey), '\n') == NULL)
(*hey) = ft_strdup("");
else
(*hey) = ft_strcpy((*hey), ((*hey) + i + 1));
return (lu);
}
int get_next_line(int fd, char **line)
{
int yo;
int len;
int res;
static char *save;
if (!save)
{
if (!(save = (char*)malloc(sizeof(char) * 1)))
return (-1);
}
len = ft_strlen(save);
if (!((*line) = (char*)malloc(sizeof(char) * BUFF_SIZE)))
return (-1);
while ((yo = read(fd, (*line), BUFF_SIZE)) > 0)
{
if (!(save = yo_realloc(save, len + yo)))
return (-1);
ft_strncat(save, (*line), yo);
if (ft_strchr(save, '\n'))
break ;
}
if (yo < 0)
return (-1);
(*line) = ft_strdup(ft_traitement(&save, &res));
return (res);
}