-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
63 lines (60 loc) · 1.54 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: <> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/02 13:33:00 by #+# #+# */
/* Updated: 2016/08/25 14:01:08 by ### ########.fr */
/* */
/* ************************************************************************** */
#include "21sh.h"
int get_next_line(int fd, char **line)
{
int buff = BUFFSIZE;
static int i;
int c;
int j = 0;
i = 0;
line[j] = malloc(sizeof(char) * buff);
if (!line[j])
{
ft_putstr("Malloc error\n");
j--;
return (-1);
}
while (1)
{
c = ft_getchar_fd(fd);
if (c == '\0' || c == '\n')
{
line[j][i] = '\0';
if (c == '\n')
return (1);
else if (c == '\0')
return (0);
}
else if (c == -1)
{
return (-1);
}
else
{
line[j][i] = c;
}
i++;
if (i >= buff)
{
buff += BUFFSIZE;
line[j] = ft_realloc(line[j], buff);
if (!line[j])
{
ft_putstr("Malloc error\n");
j--;
return (-1);
}
}
}
j++;
}