From ce374b5d6bc2a383c42d28d3523e5489b5fb090e Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 10 Oct 2019 11:34:40 +0200 Subject: Added null character in some place and it works until it doesnt --- get_next_line.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) (limited to 'get_next_line.c') diff --git a/get_next_line.c b/get_next_line.c index 082cd6f..9112ac7 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */ -/* Updated: 2019/10/10 10:18:51 by cacharle ### ########.fr */ +/* Updated: 2019/10/10 11:24:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,17 +23,16 @@ ** at each stack pop store local buf on the allocated line */ -#include int get_next_line(int fd, char **line) { int ret; int split_at; char local_buf[BUFFER_SIZE + 1]; static int line_len = 0; - static char rest_buf[BUFFER_SIZE + 1] = ""; + static char rest_buf[BUFFER_SIZE + 1] = {0}; - local_buf[0] = '\0'; - if ((ret = read_after(fd, local_buf, rest_buf)) <= 0) + local_buf[BUFFER_SIZE] = '\0'; + if ((ret = read_after(fd, local_buf, rest_buf)) <= 0 && local_buf[0] == 0) return (ret); if ((split_at = find_newline(local_buf)) != -1) { @@ -53,25 +52,17 @@ int get_next_line(int fd, char **line) return (ret); } -int read_after(int fd, char *buf, char *rest_buf) +int read_after(int fd, char *local_buf, char *rest_buf) { int offset; int ret; offset = ft_strlen(rest_buf); - ft_strncpy(buf, rest_buf, offset); - /* printf(">%s<\n", buf); */ + ft_strncpy(local_buf, rest_buf, offset + 1); rest_buf[0] = '\0'; - if ((ret = read(fd, buf + offset, BUFFER_SIZE - offset)) == -1) + if ((ret = read(fd, local_buf + offset, BUFFER_SIZE - offset)) == -1) return (ERROR); if (ret == 0) - return (0); - if (ret < BUFFER_SIZE - offset) - { - /* buf[offset + ret - 1] = '\0'; */ - /* printf(">eof? %s<\n", buf); */ - } - else - buf[BUFFER_SIZE] = '\0'; + local_buf[offset + ret] = '\0'; return (ret); } -- cgit