Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include "get_next_line.h"
  4. #include <stdlib.h>
  5. #include "libft/libft.h"
  6. #include <libc.h>
  7.  
  8. int ft_new_line(char **res, char **line, int fd)
  9. {
  10. char *tmp;
  11. size_t i;
  12.  
  13. i = 0;
  14. while (res[fd][i] != '\n' && res[fd][i] != '\0')
  15. i++;
  16. if (res[fd][i] == '\n')
  17. {
  18. *line = ft_strsub(res[fd], 0, i);
  19. tmp = ft_strdup(res[fd] + i + 1);
  20. free(res[fd]);
  21. res[fd] = tmp;
  22. if (res[fd][0] == '\0')
  23. ft_strdel(&res[fd]);
  24. }
  25. else if (res[fd][i] == '\0')
  26. {
  27. *line = ft_strdup(res[fd]);
  28. ft_strdel(&res[fd]);
  29. }
  30. return (1);
  31. }
  32.  
  33. int get_next_line(const int fd, char **line)
  34. {
  35. static char *res[15000];
  36. char buf[BUFF_SIZE + 1];
  37. char *tmp;
  38. int ret;
  39.  
  40. if (fd < 0 || line == NULL || read(fd, buf, 0) > 0 || fd > 15000)
  41. return (-1);
  42. while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
  43. {
  44. buf[ret] = '\0';
  45. if (res[fd] == NULL)
  46. res[fd] = ft_strnew(1);
  47. tmp = ft_strjoin(res[fd], buf);
  48. free(res[fd]);
  49. res[fd] = tmp;
  50. if (ft_strchr(buf, '\n'))
  51. break ;
  52. }
  53. if (ret < 0)
  54. return (-1);
  55. else if (ret == 0 && (res[fd] == NULL || res[fd] == '\0'))
  56. return (0);
  57. return (ft_new_line(res, line, fd));
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement