From 37358d8378f83aadd43f908e5a1b91c899ddae3a Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Sat, 21 Dec 2019 00:30:31 +0100 Subject: [PATCH] correction deux erreurs free avant return 0 et free list avant utilisation --- get_next_line.c | 116 +++++++++++++++++++++++------------------- get_next_line_bonus.c | 7 +-- 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/get_next_line.c b/get_next_line.c index 10ca5d6..d2b0edc 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -1,56 +1,69 @@ -//-g -fsanitize=address -//https://github.com/charMstr/GNL_lover -//https://github.com/mrjvs/42cursus_gnl_tests -//https://github.com/Sherchryst/gnlkiller -//https://github.com/Hellio404/Get_Next_Line_Tester -//https://github.com/TinfoilPancakes/get-next-line-testing-tools +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/21 00:28:33 by hulamy #+# #+# */ +/* Updated: 2019/12/21 00:28:39 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ #include "get_next_line.h" - - #include //for printf - #include //for open - - int main(int ac, char **av) - { - int *fd; - int i; - int j; - int ret; - char *line; - - if (ac < 2) - return (0); - line = NULL; - i = 0; - if (!(fd = (int *)malloc(sizeof(int) * ac))) - return (0); - while (++i <= ac - 1) - fd[i - 1] = open(av[i], O_RDONLY); - i = 0; - j = 0; - while (j < ac - 1) - { - if ((ret = get_next_line(fd[i], &line)) > 0) - { - printf("[fd%i-%i] %s\n", fd[i], ret, line); - free(line); - j = 0; - } - else - { - printf("[fd%i-%i]*FINI*\n", fd[i], ret); - j++; - } - i++; - if (i >= ac - 1) - i = 0; - } - free (fd); - //while (1); - return (0); - } - +/* +** //-g -fsanitize=address +** //:set noendofline binary +** //https://github.com/charMstr/GNL_lover +** //https://github.com/mrjvs/42cursus_gnl_tests +** //https://github.com/Sherchryst/gnlkiller +** //https://github.com/Hellio404/Get_Next_Line_Tester +** //https://github.com/TinfoilPancakes/get-next-line-testing-tools +** +** #include //for printf +** #include //for open +** +** int main(int ac, char **av) +** { +** int *fd; +** int i; +** int j; +** int ret; +** char *line; +** +** if (ac < 2) +** return (0); +** line = NULL; +** i = 0; +** if (!(fd = (int *)malloc(sizeof(int) * ac))) +** return (0); +** while (++i <= ac - 1) +** fd[i - 1] = open(av[i], O_RDONLY); +** i = 0; +** j = 0; +** while (j < ac - 1) +** { +** if ((ret = get_next_line(fd[i], &line)) > 0) +** { +** printf("[fd%i-%i] %s\n", fd[i], ret, line); +** free(line); +** j = 0; +** } +** else +** { +** printf("[fd%i-%i]*FINI*\n", fd[i], ret); +** j++; +** } +** i++; +** if (i >= ac - 1) +** i = 0; +** } +** free (fd); +** //while (1); +** return (0); +** } +*/ int multi_fd(int fd, t_gnlist **lst) { @@ -146,6 +159,7 @@ int get_next_line(const int fd, char **line) return (-1); if (*(lst->string) && ++ret == 1) (lst->string)[0] = '\0'; - free_lst(&lst, fd); + else + free_lst(&lst, fd); return (ret); } diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c index 4ee5a6e..5de312f 100644 --- a/get_next_line_bonus.c +++ b/get_next_line_bonus.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/12/16 14:08:36 by hulamy #+# #+# */ -/* Updated: 2019/12/16 14:25:08 by hulamy ### ########.fr */ +/* Updated: 2019/12/21 00:29:07 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -122,8 +122,8 @@ void free_lst(t_gnlist **lst, const int fd) while ((*lst)->next && (*lst)->next->lfd != fd) (*lst) = (*lst)->next; free(tmp->string); - free(tmp); (*lst)->next = (*lst)->next->next; + free(tmp); } int get_next_line(const int fd, char **line) @@ -149,6 +149,7 @@ int get_next_line(const int fd, char **line) return (-1); if (*(lst->string) && ++ret == 1) (lst->string)[0] = '\0'; - free_lst(&lst, fd); + else + free_lst(&lst, fd); return (ret); }