Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "get_next_line.h"
- int next_line(char **str, char **line, int fd)
- {
- char *tmp;
- if (ft_strchr(str[fd], '\n'))
- {
- tmp = ft_strdup(ft_strchr(str[fd], '\n') + 1);
- *line = ft_strsub(str[fd], 0, ft_strchr(str[fd], '\n') - str[fd]);
- ft_strdel(&str[fd]);
- str[fd] = tmp;
- return (1);
- }
- else if (str[fd][0])
- {
- *line = ft_strdup(str[fd]);
- ft_strdel(&str[fd]);
- return (1);
- }
- else
- return (0);
- }
- int get_next_line(const int fd, char **line)
- {
- static char *str[10000];
- char buff[BUFF_SIZE + 1];
- char *tmp;
- int ret;
- if (fd < 0 && !line)
- return (-1);
- while ((ret = read(fd, buff, BUFF_SIZE)) > 0)
- {
- buff[ret] = '\0';
- if (!str[fd])
- str[fd] = ft_strnew(1);
- tmp = ft_strjoin(str[fd], buff);
- free(str[fd]);
- str[fd] = tmp;
- if (ft_strchr(str[fd], '\n'))
- break ;
- }
- if (ret < 0)
- return (-1);
- if (!ret && !str[fd])
- return (0);
- return (next_line(str, line, fd));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement