Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* readfile.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: pbondoer <pbondoer@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2017/01/18 10:12:11 by pbondoer #+# #+# */
  9. /* Updated: 2017/01/19 21:34:17 by pbondoer ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include "libft.h"
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include "rtv1.h"
  17.  
  18. /*
  19. ** Reads a whole file into a string in memory
  20. */
  21.  
  22. char *readfile(int fd)
  23. {
  24. int ret;
  25. char *str;
  26. char *str2;
  27. char buf[4096 + 1];
  28.  
  29. if (fd == -1)
  30. return (NULL);
  31. str = ft_strnew(0);
  32. while ((ret = read(fd, buf, 4096)))
  33. {
  34. buf[ret] = '\0';
  35. str2 = ft_strnew(ret);
  36. ft_strcpy(str2, buf);
  37. str = strmerge(str, str2);
  38. }
  39. if (ret == -1)
  40. {
  41. ft_strdel(&str);
  42. return (NULL);
  43. }
  44. return (str);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement