From 7fcfc49a5aa4ace479906414d30b11560c96a7c5 Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Thu, 14 Dec 2023 13:31:08 +0100 Subject: [PATCH] add protect bonus --- get_next_line.c | 3 +-- get_next_line_bonus.c | 35 ++++++++++++----------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/get_next_line.c b/get_next_line.c index 0fe186e..851ad10 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,12 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2023/12/14 13:06:54 by adjoly ### ########.fr */ +/* Updated: 2023/12/14 13:27:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "get_next_line.h" -#include char check_line(char *res, char *buf) { diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c index 9d755f8..4c4000a 100644 --- a/get_next_line_bonus.c +++ b/get_next_line_bonus.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2023/12/14 11:57:16 by adjoly ### ########.fr */ +/* Updated: 2023/12/14 13:27:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,12 +37,22 @@ char check_line(char *res, char *buf) return (0); } +char *gnl_end_line(char *res) +{ + if (res[0] != 0) + return (res); + free(res); + return (NULL); +} + char *get_next_line(int fd) { static char *buf[1024]; char *res; ssize_t bytes_read; + if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023) + return (NULL); res = ft_calloc(1, 1); if (!buf[fd]) buf[fd] = ft_calloc(sizeof(char), BUFFER_SIZE + 1); @@ -56,30 +66,9 @@ char *get_next_line(int fd) { free(buf[fd]); buf[fd] = NULL; - if (res[0] != 0) - return (res); - free(res); - return (NULL); + return (gnl_end_line(res)); } buf[fd][bytes_read] = 0; } return (NULL); } - -/*#include -void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}} -#include -#include -int main(void) -{ - char *ln; - int fd; - fd = open("test.txt", O_RDONLY); - while (ln) - { - ln = get_next_line(fd); - ft_putstr(ln); - free(ln); - } - close(fd); -}*/