-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
42 lines (39 loc) · 1.65 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asansyzb <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 21:38:11 by asansyzb #+# #+# */
/* Updated: 2019/04/08 21:38:12 by asansyzb ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int get_next_line(const int fd, char **line)
{
static char *c[127];
char buff[BUFF_SIZE + 1];
int i;
char *endl;
char *tmp;
if (fd < 0 || fd > OPEN_MAX || !line || BUFF_SIZE < 1
|| (!c[fd] && !(c[fd] = ft_strnew(1))))
return (-1);
while ((i = read(fd, buff, BUFF_SIZE)) > 0)
{
buff[i] = '\0';
c[fd] = ft_strupd(c[fd], buff);
if (ft_strchr(buff, '\n'))
break ;
}
if (i == -1 || !*(tmp = c[fd]))
return (i == -1 ? -1 : 0);
endl = ft_strchr(c[fd], '\n');
*line = (endl) ?
ft_strndup(c[fd], endl - c[fd]) : ft_strdup(c[fd]);
c[fd] = ft_strsub(c[fd], (unsigned int)(ft_strlen(*line) + (endl ? 1 : 0)),
(size_t)(ft_strlen(c[fd]) - (ft_strlen(*line) + (endl ? 1 : 0))));
ft_strdel(&tmp);
return (!(!c[fd] && !ft_strlen(*line)));
}