diff --git a/ft_close.c b/ft_close.c new file mode 100644 index 0000000..e6a9c7d --- /dev/null +++ b/ft_close.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_close.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:39:05 by tmaze #+# #+# */ +/* Updated: 2019/03/08 13:44:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_close(t_file *file) +{ + int filedes; + + filedes = file->fd; + ft_memdel(file); + return (close(filedes) == 0); +} diff --git a/ft_getline.c b/ft_getline.c index 05db5b8..3404a92 100644 --- a/ft_getline.c +++ b/ft_getline.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */ -/* Updated: 2019/03/07 22:06:57 by tmaze ### ########.fr */ +/* Updated: 2019/03/08 14:11:15 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,17 +19,18 @@ static char *supercat(char **s1, char *buff) if ((tmp = ft_strjoin(*s1, buff)) == NULL) return (NULL); ft_strdel(s1); - ft_strclr(buff); + ft_bzero(buff, BUFF_SIZE + 1); *s1 = tmp; return (*s1); } -static int update_line(char **line, char *buff) +static int flush_buff(char **line, char *buff) { if (*line == NULL) { if ((*line = ft_strdup(buff)) == NULL) return (-1); + ft_bzero(buff, BUFF_SIZE + 1); } else if (*line != NULL) if (supercat(line, buff) == NULL) @@ -42,29 +43,23 @@ static int update_line(char **line, char *buff) int ft_getline(char **line) { - static char buff[BUFF_SIZE + 1]; - char *ind; + char buff[BUFF_SIZE + 1]; int ret; + int i; if (line == NULL) return (-1); *line = NULL; - ind = NULL; ft_bzero(buff, BUFF_SIZE + 1); - while ((ret = read(0, buff, BUFF_SIZE)) == BUFF_SIZE - && (ind = ft_strchr(buff, '\n')) == NULL) + i = 0; + while ((ret = read(0, &(buff[i]), 1)) == 1 && buff[i] != '\n') { - if (update_line(line, buff) == -1) + if (++i > BUFF_SIZE && flush_buff(line, buff) != 0) return (-1); - ft_bzero(&buff, BUFF_SIZE + 1); + if (i > BUFF_SIZE) + i = 0; } - if (ind != NULL || (ind == NULL && (ind = ft_strchr(buff, '\n')) != NULL)) - *ind = '\0'; - if (update_line(line, buff) == -1) - return (-1); - if (ind == NULL) - ft_bzero(&buff, BUFF_SIZE + 1); - if (ind != NULL && ind < &(buff[BUFF_SIZE])) - ft_memmove(buff, ind + 1, ft_strlen(ind + 1)); - return (*line != NULL && ret != 0); + if (ret == 0 && i != 0 && flush_buff(line, buff) != 0) + return (-1); + return (line != NULL); } diff --git a/ft_gets.c b/ft_gets.c new file mode 100644 index 0000000..f522a57 --- /dev/null +++ b/ft_gets.c @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_gets.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:44:31 by tmaze #+# #+# */ +/* Updated: 2019/03/08 14:11:38 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + diff --git a/ft_open.c b/ft_open.c new file mode 100644 index 0000000..2706216 --- /dev/null +++ b/ft_open.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_open.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:27:26 by tmaze #+# #+# */ +/* Updated: 2019/03/08 13:38:29 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_file *ft_open(char *filename, oint oflag) +{ + t_file *ret; + + if ((ret = ft_memalloc(sizeof(t_file))) == NULL) + return (NULL); + if ((ret->fd = open(filename, oflag)) == -1) + { + ft_memdel(&ret); + return (NULL); + } + ft_bzero(ret->buff, BUFF_SIZE + 1); + return (ret); +} diff --git a/libft.h b/libft.h index bbb6e16..a3b40c0 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */ -/* Updated: 2019/03/07 15:38:26 by tmaze ### ########.fr */ +/* Updated: 2019/03/08 13:27:16 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,16 @@ typedef struct s_list # define BUFF_SIZE 30 +/* +** definition type t_file +*/ + +typedef struct s_file +{ + int fd; + char buff[BUFF_SIZE + 1]; +} t_file; + enum e_size { h,