-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnl.c
92 lines (85 loc) · 2 KB
/
gnl.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
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tespandj <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 23:23:49 by tespandj #+# #+# */
/* Updated: 2024/05/11 23:08:00 by tespandj ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int trouve(const char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}
char *init_save(char *save)
{
if (BUFFER_SIZE == 0)
return (NULL);
if (!save)
{
save = malloc(sizeof(char));
if (!save)
return (NULL);
save[0] = '\0';
}
return (save);
}
char *reset(char *buf, char **save, int i)
{
if (i == 1)
free(buf);
else if (i == 2)
free(*save);
else if (i == 3)
{
free(buf);
free(*save);
*save = 0;
}
return (NULL);
}
static char *fre(char *buf, char *str)
{
free(buf);
return (str);
}
char *gnl(int fd)
{
static char *save;
char *buf;
char *str;
ssize_t rv;
save = init_save(save);
if (fd < 0 || BUFFER_SIZE <= 0 || !(save))
return (0);
buf = malloc(sizeof(char) * BUFFER_SIZE + 1);
if (!buf)
return (reset(buf, &save, 2));
rv = 1;
while (rv > 0 && !trouve(save))
{
rv = read(fd, buf, BUFFER_SIZE);
if (rv < 0)
return (reset(buf, &save, 3));
buf[rv] = '\0';
save = add_str(save, buf);
if (!save)
return (NULL);
}
str = org(save);
save = save_static(save, rv);
return (fre(buf, str));
}