diff options
Diffstat (limited to 'src/io/ft_getfile.c')
| -rw-r--r-- | src/io/ft_getfile.c | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c index d3f697c..6ae4833 100644 --- a/src/io/ft_getfile.c +++ b/src/io/ft_getfile.c @@ -3,38 +3,25 @@ /* ::: :::::::: */ /* ft_getfile.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/05/11 09:52:32 by charles #+# #+# */ -/* Updated: 2020/05/11 16:09:31 by charles ### ########.fr */ +/* Created: 2020/02/22 10:32:47 by cacharle #+# #+# */ +/* Updated: 2020/08/02 11:01:06 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_io.h" -/* -** \brief Read a file in a memory buffer -** \param fd File descriptor to read from -** \param mem Pointer to mem struct (buffer and buffer size) -** \return -1 on error, 0 otherwise -*/ - -int ft_getfile(int fd, t_ftmem *mem) +int ft_getfile(char *filename, t_ftmem *mem) { - char buf[FT_GETFILE_BUFFER_SIZE]; - int ret; + int fd; - if (fd < 0 || fd > OPEN_MAX || mem == NULL - || (mem->data = malloc(1)) == NULL) + if ((fd = open(filename, O_RDONLY)) < 0) return (-1); - mem->size = 0; - while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) + if (ft_getfile_fd(fd, mem) < 0) { - if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL) - return (-1); - mem->size += ret; + close(fd); + return (-1); } - if (ret == -1) - free(mem->data); - return (ret); + return (close(fd)); } |
