-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
58 lines (53 loc) · 1.65 KB
/
get_next_line.c
File metadata and controls
58 lines (53 loc) · 1.65 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
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ashulha <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 13:44:35 by ashulha #+# #+# */
/* Updated: 2017/03/20 13:44:37 by ashulha ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <limits.h>
static int ft_read(const int fd, char **s)
{
char buf[BUFF_SIZE + 1];
char *tmp;
int i;
if ((i = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[i] = '\0';
tmp = *s;
*s = ft_strjoin(tmp, buf);
free(tmp);
}
return (i);
}
int get_next_line(const int fd, char **line)
{
static char *s[4864];
char *end_s;
int k;
if (fd < 0 || fd > INT_MAX - 1 || line == NULL)
return (-1);
if (!s[fd])
s[fd] = ft_strnew(1);
while ((end_s = ft_strchr(s[fd], '\n')) == NULL)
{
if ((k = ft_read(fd, &s[fd])) < 0)
return (-1);
if (k == 0 && !end_s)
{
if (s[fd][0] == '\0')
return (0);
*line = s[fd];
s[fd] = NULL;
return (1);
}
}
*line = ft_strsub(s[fd], 0, end_s - s[fd]);
s[fd] = ft_strdup(end_s + 1);
return (1);
}