-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
99 lines (89 loc) · 2.32 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
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybachar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:52:56 by ybachar #+# #+# */
/* Updated: 2022/12/16 17:33:00 by ybachar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strchr(char *s, int c)
{
int i;
i = 0;
if (!s)
return (NULL);
while (s[i])
{
if (s[i] == c)
return (&s[i]);
i++;
}
return (0);
}
char *reader(int fd, char *str)
{
char *buffer;
int len;
buffer = malloc(sizeof(char) * BUFFER_SIZE + 1);
if (!buffer)
return (NULL);
len = 1;
while (!(ft_strchr(str, '\n')) && len != 0)
{
len = read(fd, buffer, BUFFER_SIZE);
if (len == 0)
break ;
if (len == -1)
{
free(buffer);
free(str);
return (NULL);
}
buffer[len] = '\0';
str = ft_strjoin(str, buffer);
}
free(buffer);
return (str);
}
char *get_next_line(int fd)
{
static char *str;
char *res;
if(fd < 0 || fd == 1 || fd == 2)
return (NULL);
str = reader(fd, str);
if (!str)
return (NULL);
res = get_ligne(str);
str = get_extra(str);
return (res);
}
int main()
{
char *str;
// int fd = open("foo.txt", O_RDONLY);
// str = get_next_line(fd);
// printf("%s", str);
str = get_next_line(1);
printf("%s", str);
// while (str)
// {
// str = get_next_line(fd);
// printf("%s", str);
// }
// str = get_next_line(fd);
// printf("%s", str);
// str = get_next_line(fd);
// printf("%s", str);
// str = get_next_line(fd);
// printf("%s", str);
// // str = get_next_line(fd);
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// //printf("%s\n", get_next_line(fd));
// printf("%s", get_next_line(fd));
}