-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
83 lines (76 loc) · 2.08 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
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dcelsa <dcelsa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/16 21:27:51 by dcelsa #+# #+# */
/* Updated: 2021/10/16 21:36:51 by dcelsa ### ########.fr */
/* */
/* ************************************************************************** */
#include"get_next_line.h"
static char *line_builder(char *str, char *buf, char **shift, int sw)
{
size_t i;
i = 0;
while (buf[i])
{
if (buf[i++] == '\n')
{
*shift = buf + i;
break ;
}
}
if (sw)
return (NULL);
return (remal(str, buf, i));
}
static char *trunc(char *str, char *buf, char **shift)
{
char *sshift;
if (!buf)
{
*shift = NULL;
return (NULL);
}
sshift = NULL;
line_builder(NULL, buf, &sshift, 1);
if (sshift && !*sshift)
sshift = NULL;
*shift = sshift;
if (!sshift)
return (NULL);
buf = sshift;
*(buf - 1) += 1;
str = line_builder(str, buf, &sshift, 0);
*shift = sshift;
if (sshift == buf)
*shift = NULL;
return (str);
}
char *get_next_line(int fd)
{
ssize_t ret;
char *shift;
static char *buf = NULL;
char *str;
str = NULL;
str = trunc(str, buf, &shift);
if ((size_t) BUFFER_SIZE < 1)
return (NULL);
if (!buf)
buf = malloc(sizeof(*buf) * ((size_t) BUFFER_SIZE + 1));
ret = BUFFER_SIZE;
while (buf && !shift)
{
ret = read(fd, buf, BUFFER_SIZE);
if ((!ret || ret == -1) && !erase(NULL, (void **)&buf))
break ;
buf[ret] = '\0';
str = line_builder(str, buf, &shift, 0);
if (!str)
return (erase(str, (void **)&buf));
}
return (str);
}