aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libft_io.h10
-rw-r--r--src/io/ft_getfile.c37
2 files changed, 45 insertions, 2 deletions
diff --git a/include/libft_io.h b/include/libft_io.h
index e59d70c..ce3c632 100644
--- a/include/libft_io.h
+++ b/include/libft_io.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:35:43 by cacharle #+# #+# */
-/* Updated: 2020/05/09 11:16:36 by charles ### ########.fr */
+/* Updated: 2020/05/11 09:57:42 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,8 +29,14 @@ void ft_putnbr_fd(int n, int fd);
char ft_getchar(void);
+# ifndef FT_GETFILE_BUFFER_SIZE
+# define FT_GETFILE_BUFFER_SIZE 64
+# endif
+
+char *ft_getfile(int fd);
+
# ifndef FT_GETLINE_BUFFER_SIZE
-# define FT_GETLINE_BUFFER_SIZE 32
+# define FT_GETLINE_BUFFER_SIZE 64
# endif
# define FT_LINE 1
diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c
new file mode 100644
index 0000000..891ea51
--- /dev/null
+++ b/src/io/ft_getfile.c
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getfile.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 09:52:32 by charles #+# #+# */
+/* Updated: 2020/05/11 10:15:12 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_io.h"
+
+char *ft_getfile(int fd)
+{
+ char *s;
+ char buf[FT_GETFILE_BUFFER_SIZE + 1];
+ int ret;
+
+ if (fd < 0 || fd > OPEN_MAX)
+ return (NULL);
+ if ((s = ft_strdup("")) == NULL)
+ return (NULL);
+ while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if ((s = ft_strjoinf(s, buf, FT_STRJOINF_FST)) == NULL)
+ return (NULL);
+ }
+ if (ret == -1)
+ {
+ free(s);
+ return (NULL);
+ }
+ return (s);
+}