-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
76 lines (67 loc) · 1.17 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
/*
** EPITECH PROJECT, 2017
** my lib
** File description:
** get_next_line
*/
#include "header_lib.h"
#include "get_next_line.h"
char *my_charcat(char *src, char next)
{
char *new;
int z;
z = 0;
new = malloc(sizeof(char) * my_strlen(src) + 2);
while (src[z] != '\0') {
new[z] = src[z];
z += 1;
}
new[z] = next;
new[z + 1] = '\0';
return (new);
}
char *first_one(char next)
{
char *new;
new = malloc(sizeof(char) * 2);
new[0] = next;
new[1] = '\0';
return (new);
}
char read_next_char(const int fd)
{
static char buf[READ_SIZE];
static int z = 0;
static int r = 0;
char next;
if (r == z) {
z = -1;
while (++z <= READ_SIZE)
buf[z] = 0;
if ((r = read(fd, buf, READ_SIZE)) == -1 || r == 0)
return (-1);
z = 0;
} next = buf[z];
z += 1;
if (next == '\n' || next == '\0')
return ((next == '\n') ? '\0' : -1);
return (next);
}
char *get_next_line(const int fd)
{
char next;
char *line;
line = "";
next = 'L';
if (fd < 0)
return (NULL);
while (next != '\0') {
if ((next = read_next_char(fd)) == -1)
return (NULL);
else if (line == NULL)
line = first_one(next);
else
line = my_charcat(line, next);
}
return (line);
}