#include "get_next_line.h" char *ft_getstash(int fd) { char *str; int readed; str = ft_calloc(BUFFER_SIZE, sizeof(char)); if (str == NULL) return (NULL); readed = read(fd, str, BUFFER_SIZE); if (readed < 0) { free(str); return (NULL); } return (str); } ssize_t ft_strchr(char *str, char c) { size_t i; if (str == NULL) return (-1); i = 0; while (str[i] != c && str[i] != '\0') i++; if (str[i] == '\0' && c != '\0') return (-1); return (i); } char *ft_getline(int fd) { char *stash; char *buf; size_t i; stash = NULL; i = 0; while (ft_strchr(stash + i * BUFFER_SIZE, '\n')) { stash = ft_realloc(stash, (i + 1) * BUFFER_SIZE); if (stash == NULL) return (NULL); buf = ft_getstash(fd); if (buf == NULL) { free(stash); return (NULL); } ft_strcat(stash, buf); free(buf); if (stash[(i + 1) * BUFFER_SIZE -1] == '\0') break ; } return (stash); } char *get_next_line(int fd) { static char *stash = NULL; char *buf1; char *buf2; buf1 = NULL; if (ft_strchr(stash, '\n') == -1) { buf1 = ft_getline(fd); if (buf1 == NULL) return (NULL); stash = ft_realloc(stash, ft_strchr(buf1, '\0') + ft_strchr(stash, '\0') + 1); if (stash == NULL) return (NULL); ft_strcat(stash, buf1); free(buf1); } buf1 = ft_strndup(stash, ft_strchr(stash, '\n')); buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, '\0')); free(stash); stash = buf2; if (stash == NULL || buf1 == NULL) { free(stash); free(buf1); return (NULL); } return (buf1); }