-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_multfd.c
103 lines (96 loc) · 3.13 KB
/
get_next_line_multfd.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
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_multfd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phemsi-a <phemsi-a@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/17 12:17:28 by phemsi-a #+# #+# */
/* Updated: 2021/04/19 23:59:34 by phemsi-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_errors(int fd, char **line, t_gnl *string)
{
*line = ft_strdup("");
if (*line == NULL)
return (1);
ft_memset(string, 0, sizeof(*string));
if ((fd < 0) || (fd > RLIMIT_NOFILE) || (BUFFER_SIZE < 1) || !(line))
return (1);
return (0);
}
static int add_to_line(t_gnl *string, char **line, char **excess, int new_line)
{
if (new_line)
*string->break_line_ptr = '\0';
string->read[string->read_return] = '\0';
string->temp = ft_strjoin(*line, string->read);
if (string->temp == NULL)
return (ERROR);
free(*line);
*line = string->temp;
if (new_line)
{
string->temp = ft_strdup(string->break_line_ptr + 1);
if (string->temp == NULL)
return (ERROR);
free(*excess);
*excess = string->temp;
}
return (READ_LINE);
}
static int add_excess(char **line, t_gnl *string, char **excess, int new_line)
{
if (new_line)
*string->break_line_ptr = '\0';
string->temp = ft_strjoin(*line, *excess);
if (string->temp == NULL)
return (ERROR);
free(*line);
*line = string->temp;
if (new_line)
{
string->temp = ft_strdup(string->break_line_ptr + 1);
if (string->temp == NULL)
return (ERROR);
}
free(*excess);
if (new_line)
*excess = string->temp;
else
*excess = NULL;
return (READ_LINE);
}
static int free_excess(t_gnl *string, char **excess)
{
free(*excess);
return (string->read_return);
}
int get_next_line_multfd(int fd, char **line)
{
static char *excess[RLIMIT_NOFILE];
t_gnl string;
if (check_errors(fd, line, &string))
return (-1);
if (excess[fd] != NULL)
{
string.break_line_ptr = ft_strchr(excess[fd], '\n');
if (string.break_line_ptr)
return (add_excess(line, &string, &excess[fd], NEW_LINE));
if ((add_excess(line, &string, &excess[fd], NO_NEW_LINE)) == ERROR)
return (ERROR);
}
string.read_return = read(fd, string.read, BUFFER_SIZE);
string.break_line_ptr = ft_strchr(string.read, '\n');
while (((string.read_return > 0) && !(string.break_line_ptr)))
{
if (add_to_line(&string, line, &excess[fd], NO_NEW_LINE) == ERROR)
return (ERROR);
string.read_return = read(fd, string.read, BUFFER_SIZE);
string.break_line_ptr = ft_strchr(string.read, '\n');
}
if (string.read_return < 1)
return (free_excess(&string, &excess[fd]));
return (add_to_line(&string, line, &excess[fd], NEW_LINE));
}