-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
47 lines (44 loc) · 1.92 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 20:02:39 by nkawaguc #+# #+# */
/* Updated: 2024/06/28 00:35:41 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
// get_next_line: read a line from a file descriptor
// If fd is invalid or BUFFER_SIZE is invalid, return NULL.
// If buf is not initialized, initialize it with buf_init.
// Allocate a line buffer and initialize it with '\0'.
// Read until a newline character is found or EOF is reached with gnl_read.
// return: a line read from the file descriptor
char *get_next_line(int fd)
{
static char *buf = NULL;
char *line;
int line_size;
int flag;
if (fd < 0 || BUFFER_SIZE <= 0 || buf_init(&buf) == -1)
return (NULL);
line_size = BUFFER_SIZE + 1;
line = (char *)malloc(line_size * sizeof(char));
if (!line)
return (free(buf), gnl_bzero(&buf, sizeof(char *)), NULL);
gnl_bzero(line, line_size);
flag = gnl_read(fd, &line, &line_size, buf);
if (flag == -1 || flag == 0)
{
free(buf);
gnl_bzero(&buf, sizeof(char *));
if (flag == -1 || line[0] == '\0')
return (free(line), NULL);
}
line = gnl_realloc(line, 0, line_size);
if (line == NULL)
return (free(buf), gnl_bzero(&buf, sizeof(char *)), NULL);
return (line);
}