-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
48 lines (44 loc) · 1.62 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/01 12:45:12 by bnidia #+# #+# */
/* Updated: 2022/01/15 22:49:59 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/*
* get_next_line(int fd)
* reads file and returns a string separated \n
* if nothing else to read or error returns NULL
* BUFFER_SIZE=xx will be used as the rb_ar size for the read calls
*/
char *get_next_line(int fd)
{
static t_file *file = NULL;
if (BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
return (NULL);
if (file == NULL || check_file(fd, &file) == NULL)
file = init_file(fd, &file);
if (file == NULL)
return (NULL);
file->s_size = 0;
file->str = NULL;
make_string(file);
if (file->rb_size == -1 && file->s_size > 0)
{
free(file->str);
file->str = NULL;
}
if (file->str == NULL)
{
delete_file(&file);
return (NULL);
}
if (file->s_size > 0)
file->str[file->s_size] = '\0';
return (file->str);
}