-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_next_line.c
67 lines (61 loc) · 1.21 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
#include "get_next_line.h"
static char *function_name(int fd, char *buf, char *backup)
{
int read_line;
char *char_temp;
read_line = 1;
while (read_line != '\0')
{
read_line = read(fd, buf, BUFFER_SIZE);
if (read_line == -1)
return (0);
else if (read_line == 0)
break ;
buf[read_line] = '\0';
if (!backup)
backup = ft_strdup("");
char_temp = backup;
backup = ft_strjoin(char_temp, buf);
free(char_temp);
char_temp = NULL;
if (ft_strchr (buf, '\n'))
break ;
}
return (backup);
}
static char *extract(char *line)
{
size_t count;
char *backup;
count = 0;
while (line[count] != '\n' && line[count] != '\0')
count++;
if (line[count] == '\0' || line[1] == '\0')
return (0);
backup = ft_substr(line, count + 1, ft_strlen(line) - count);
if (*backup == '\0')
{
free(backup);
backup = NULL;
}
line[count + 1] = '\0';
return (backup);
}
char *get_next_line(int fd)
{
char *line;
char *buf;
static char *backup;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
buf = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (0);
line = function_name(fd, buf, backup);
free(buf);
buf = NULL;
if (!line)
return (NULL);
backup = extract(line);
return (line);
}