-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
92 lines (84 loc) · 2.13 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
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgabriel <hgabriel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 15:18:54 by hgabriel #+# #+# */
/* Updated: 2022/05/01 10:57:45 by hgabriel ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
static int read_bytes(int fd, char **buf, int *bytes_read)
{
int i;
i = read(fd, *buf, BUFFER_SIZE);
*bytes_read = i;
return (i);
}
static void freenull(char **str)
{
if (str)
{
free(*str);
*str = 0;
}
}
static char *gnl_output(char **str)
{
size_t i;
char *ret;
char *temp;
i = 0;
if (*str)
{
while ((*str)[i] && (*str)[i] != '\n')
i++;
if ((*str)[i])
{
ret = ft_substr(*str, 0, i + 1);
temp = ft_substr(*str, i + 1, ft_strlen(*str));
freenull(str);
if (temp[0] != '\0')
*str = temp;
else
freenull(&temp);
return (ret);
}
ret = ft_substr(*str, 0, ft_strlen(*str));
freenull(str);
return (ret);
}
return (NULL);
}
char *get_next_line(int fd)
{
static char *hold;
char *buff;
char *temp;
int bytes_read;
buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (fd >= 0 && BUFFER_SIZE > 0 && (buff))
{
while (read_bytes(fd, &buff, &bytes_read) > 0)
{
buff[bytes_read] = 0;
if (!hold)
hold = ft_bzero(0);
temp = ft_strjoin(hold, buff);
free(hold);
hold = temp;
if (find_nl(buff))
break ;
}
free(buff);
return (gnl_output(&hold));
}
free(buff);
return (NULL);
}