-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line.c
72 lines (66 loc) · 1.79 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhiguera <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 12:39:18 by mhiguera #+# #+# */
/* Updated: 2023/04/26 19:33:42 by mhiguera ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_save(int fd, char *aux)
{
int save_read;
char *bufstr;
save_read = 1;
bufstr = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!bufstr)
return (NULL);
while (!ft_strchr(aux, '\n') && save_read != 0)
{
save_read = read(fd, bufstr, BUFFER_SIZE);
if (save_read == -1)
{
free(bufstr);
return (NULL);
}
bufstr[save_read] = '\0';
aux = ft_strjoin(aux, bufstr);
}
free(bufstr);
return (aux);
}
char *get_next_line(int fd)
{
static char *aux[4096];
char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
aux[fd] = ft_save(fd, aux[fd]);
if (!aux[fd])
{
free(aux[fd]);
return (NULL);
}
str = ft_ending(aux[fd]);
aux[fd] = ft_extra(aux[fd]);
return (str);
}
/*
int main()
{
int fd;
char *line;
fd = open("file.txt", O_RDONLY);
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
close(fd);
system("leaks a.out");
return (0);
}
*/