ft_get_file_content.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_get_file_content.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/23 19:01:27 by bchanot #+# #+# */
  9. /* Updated: 2018/10/29 12:19:25 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. #include <unistd.h>
  14. char *ft_get_file_content(int fd)
  15. {
  16. char *buff;
  17. char *tmp;
  18. char *s;
  19. buff = (char *)ft_memalloc(sizeof(char) * BUFF_SIZE + 1);
  20. ft_bzero(buff, BUFF_SIZE);
  21. s = NULL;
  22. while (read(fd, buff, BUFF_SIZE) > 0)
  23. {
  24. if (!s)
  25. s = ft_strdup(buff);
  26. else
  27. {
  28. tmp = ft_strjoin(s, buff);
  29. ft_memdel((void **)&s);
  30. s = tmp;
  31. tmp = NULL;
  32. }
  33. ft_bzero(buff, BUFF_SIZE);
  34. }
  35. ft_memdel((void **)&buff);
  36. return (s);
  37. }