Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* get_next_line.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: abaidali <abaidali@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/10/02 22:50:22 by abaidali #+# #+# */
- /* Updated: 2019/10/13 00:17:58 by abaidali ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "get_next_line.h"
- static int get_line(char **s, char **line)
- {
- int len;
- char *tmp;
- len = 0;
- while ((*s)[len] != '\n' && (*s)[len] != '\0')
- len++;
- if ((*s)[len] == '\n')
- {
- *line = ft_strsub(*s, 0, len);
- tmp = ft_strdup(&((*s)[len + 1]));
- free(*s);
- *s = tmp;
- if ((*s)[0] == '\0')
- ft_strdel(s);
- }
- else
- {
- *line = ft_strdup(*s);
- ft_strdel(s);
- }
- return (1);
- }
- static int ft_result(char **s, char **line, int res, int fd)
- {
- if (res < 0)
- return (-1);
- else if (res == 0 && s[fd] == NULL)
- return (0);
- else
- return (get_line(&s[fd], line));
- }
- int get_next_line(const int fd, char **line)
- {
- int status;
- static char *s[MAX_FD];
- char buf[BUFF_SIZE + 1];
- char *tmp;
- if (fd < 0 || line == NULL)
- return (-1);
- while ((status = read(fd, buf, BUFF_SIZE)) > 0)
- {
- buf[status] = '\0';
- if (s[fd] == NULL)
- s[fd] = ft_strdup(buf);
- else
- {
- tmp = ft_strjoin(s[fd], buf);
- free(s[fd]);
- s[fd] = tmp;
- }
- if (ft_strchr(s[fd], '\n'))
- break ;
- }
- return (ft_result(s, line, status, fd));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement