Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* get_next_line.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: asaboure <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/12/10 16:26:02 by asaboure #+# #+# */
- /* Updated: 2019/12/12 16:50:00 by asaboure ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "get_next_line.h"
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- int is_nl(char *str)
- {
- size_t i;
- i = 0;
- while (str[i++])
- if (str[i] == '\n')
- return (1);
- return (0);
- }
- char *ft_next(char *result)
- {
- size_t i;
- i = 0;
- while (result[i] && result[i] != '\n')
- i++;
- return (result + i + 1);
- }
- int get_next_line(int fd, char **line)
- {
- static char *result = NULL;
- char buf[BUFFER_SIZE + 1];
- int i;
- i = 1;
- if (result == NULL)
- {
- if (!(result = (char *)malloc(1)))
- return (-1);
- result[0] = '\0';
- }
- while (i > 0 && is_nl(result) == 0)
- {
- i = read(fd, buf, BUFFER_SIZE);
- buf[i] = '\0';
- result = ft_strjoin(result, buf);
- }
- if ((result[0] == '\0' && i == 0) || i == -1)
- {
- free(result);
- return (i);
- }
- *line = ft_strdup_nl(result);
- result = ft_next(result);
- return (1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement