-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
59 lines (54 loc) · 909 Bytes
/
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
# include <unistd.h>
# include <stdlib.h>
# include <strings.h>
# include <fcntl.h>
# include <stdio.h>
# define BUFFER_SIZE 5
char *get_next_line(int fd)
{
char a[999999];
char buffer[1];
char *new_a;
int i;
if (fd < 0 && BUFFER_SIZE <= 0)
return (NULL);
i = 0;
a[i] = 0;
while (read(fd, buffer, 1) == 1)
{
a[i] = buffer[0];
a[i + 1] = '\0';
if (a[i] == '\n')
break;
i++;
}
if (!a[0])
return (NULL);
new_a = malloc(i + 1);
if (!new_a)
return (NULL);
i = 0;
while (a[i])
{
new_a[i] = a[i];
i++;
}
new_a[i] = '\0';
return (new_a);
}
//questo main serve per testare la funzione da soli ma non è necessario per l'esame vero e proprio
int main(int ac, char **av)
{
int fd;
char *line;
(void)ac;
fd = open(av[1], O_RDONLY);
while ((line = get_next_line(fd)) != 0)
{
printf("%s", line);
free(line);
}
close(fd);
system("leaks a.out");
return (0);
}