I have got the following code and it doesn't work fine all the time I run it:
->> ./a.out gnl1_2.txt
1234567
abcdefg
->> ./a.out gnl1_2.txt
1234567
abcdefg
->> ./a.out gnl1_2.txt
1234567
It returned only the first line and sometimes gives me nothing
(I might have some leaks). Basically it should give me the next line from a file in the line
parameter (godlike
= structure that has a string and a file descriptor)
int get_next_line(const int fd, char **line)
{
static t_gnl *godlike;
int i;
i = 0;
if (fd < 0 || !line || !fd)
return (-1);
if (!godlike)
godlike = malloc(sizeof *godlike);
//printf("%s\n", godlike[0].xx);
while ((godlike[i].xx) && (godlike[i].fd != fd))
i++;
if (!godlike[i].xx)
{
godlike[i].fd = fd;
godlike[i].xx = read_f(fd);
if (godlike[i].xx == NULL)
return (-1);
}
*line = ft_strcdup(godlike[i].xx, '\n');
while ((godlike[i].xx[0]) && (godlike[i].xx[0] != '\n'))
{
godlike[i].xx[0] = '\0';
godlike[i].xx++;
}
if (godlike[i].xx[0] == '\n')
{
godlike[i].xx[0] = '\0';
godlike[i].xx++;
return (1);
}
else
return (0);
}