diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-08-02 11:05:33 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-08-02 11:05:33 +0200 |
| commit | 5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch) | |
| tree | 80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/io/ft_getfile_fd.c | |
| parent | ee32953ea79616e72f5428cdf40c834714a891c9 (diff) | |
| parent | b96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff) | |
| download | libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2 libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip | |
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/io/ft_getfile_fd.c')
| -rw-r--r-- | src/io/ft_getfile_fd.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/io/ft_getfile_fd.c b/src/io/ft_getfile_fd.c new file mode 100644 index 0000000..91ebf7f --- /dev/null +++ b/src/io/ft_getfile_fd.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getfile_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 09:52:32 by charles #+# #+# */ +/* Updated: 2020/08/02 10:55:46 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_fd(int fd, t_ftmem *mem) +{ + char buf[FT_GETFILE_BUFFER_SIZE]; + int ret; + + if (fd < 0 || fd > OPEN_MAX || mem == NULL + || (mem->data = malloc(1)) == NULL) + return (-1); + mem->size = 0; + while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) + { + if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL) + return (-1); + mem->size += ret; + } + if (ret == -1) + free(mem->data); + return (ret); +} |
