fork download
  1. # include <unistd.h>
  2. # include <fcntl.h>
  3. # include <stdlib.h>
  4.  
  5. size_t ft_strlen(const char *s)
  6. {
  7. int i;
  8.  
  9. i = 0;
  10. while (s[i])
  11. i++;
  12. return (i);
  13. }
  14.  
  15. int ft_strchr(const char *s)
  16. {
  17. int i;
  18.  
  19. i = 0;
  20. if (!s)
  21. return (1);
  22. while (s[i] != '\n' && s[i])
  23. i++;
  24. if (s[i] != '\n')
  25. return (1);
  26. return (0);
  27. }
  28.  
  29. char *ft_strjoin(char const *s1, char const *s2)
  30. {
  31. char *new;
  32. int i;
  33. int j;
  34.  
  35. if (!s1 || !s2)
  36. return (0);
  37. new = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2)) + 1);
  38. if (!new)
  39. return (0);
  40. i = 0;
  41. j = 0;
  42. while (s1[i])
  43. {
  44. new[j] = s1[i];
  45. i++;
  46. j++;
  47. }
  48. i = 0;
  49. while (s2[i])
  50. new[j++] = s2[i++];
  51. new[j] = '\0';
  52. return ((char *)new);
  53. }
  54.  
  55. void *ft_calloc(size_t count, size_t size)
  56. {
  57. char *d;
  58. size_t i;
  59. size_t j;
  60.  
  61. if (count == 0 || size == 0)
  62. j = 1;
  63. else if (count > 65535 || size > 65535 || count * size > 65535)
  64. return (NULL);
  65. else
  66. j = count * size;
  67. i = 0;
  68. d = malloc(j);
  69. if (d == 0)
  70. return (0);
  71. while (j > 0)
  72. {
  73. d[i] = '\0';
  74. j--;
  75. i++;
  76. }
  77. return ((void *)d);
  78. }
  79.  
  80. char *ft_substr(char const *s, unsigned int start, size_t len)
  81. {
  82. char *new;
  83. int i;
  84.  
  85. if (!s)
  86. return (0);
  87. i = 0;
  88. if (start > ft_strlen(s))
  89. len = 0;
  90. else if (ft_strlen(&s[start]) < len)
  91. len = ft_strlen(&s[start]);
  92. new = malloc(sizeof(char) * len + 1);
  93. if (!new)
  94. return (0);
  95. while (len > 0 && s[start])
  96. {
  97. new[i] = s[start];
  98. i++;
  99. start++;
  100. len--;
  101. }
  102. new[i] = '\0';
  103. return (new);
  104. }
  105.  
  106. char *ft_free(char *buffer, char *buf)
  107. {
  108. char *temp;
  109.  
  110. temp = ft_strjoin(buffer, buf);
  111. free(buffer);
  112. return (temp);
  113. }
  114.  
  115. void read_line(int fd, char **temp, int *lues, int BUFFER_SIZE)
  116. {
  117. char *buffer;
  118.  
  119. if (!*temp)
  120. *temp = ft_calloc(1, 1);
  121. while (*lues >= 1 && ft_strchr(*temp) == 1)
  122. {
  123. buffer = malloc(sizeof(char) * BUFFER_SIZE + 1);
  124. if (!buffer)
  125. return ;
  126. *lues = read(fd, buffer, BUFFER_SIZE);
  127. if (((*temp == NULL) && *lues == 0) || *lues == -1)
  128. {
  129. free(buffer);
  130. return ;
  131. }
  132. buffer[*lues] = '\0';
  133. *temp = ft_free(*temp, buffer);
  134. free(buffer);
  135. }
  136. }
  137.  
  138. void extract_line(char *temp, char **line)
  139. {
  140. int len;
  141.  
  142. len = 0;
  143. if (temp == NULL)
  144. return ;
  145. while (temp[len] && temp[len] != '\n')
  146. len++;
  147. *line = ft_substr(temp, 0, len + 1);
  148. if (*line == NULL)
  149. return ;
  150. }
  151.  
  152. char *clean_temp(char *temp)
  153. {
  154. int i;
  155. int j;
  156. char *c;
  157.  
  158. i = 0;
  159. while (temp[i] != '\n' && temp[i])
  160. i++;
  161. j = (int)ft_strlen(&temp[i]);
  162. c = ft_substr(temp, i +1, j);
  163. if (!c)
  164. return (NULL);
  165. free(temp);
  166. return (c);
  167. }
  168.  
  169. char *get_next_line(int fd)
  170. {
  171. static char *temp;
  172. char *line;
  173. int lues;
  174. int BUFFER_SIZE = 1;
  175.  
  176. if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
  177. return (NULL);
  178. lues = 1;
  179. read_line(fd, &temp, &lues, BUFFER_SIZE);
  180. if (temp == NULL)
  181. return (NULL);
  182. extract_line(temp, &line);
  183. if (line == NULL)
  184. return (NULL);
  185. temp = clean_temp(temp);
  186. if (line[0] == '\0')
  187. {
  188. free(line);
  189. line = NULL;
  190. return (NULL);
  191. }
  192. return (line);
  193. }
  194. #include <stdio.h>
  195. int main()
  196. {
  197. int fd;
  198. char *tmp;
  199. int i;
  200.  
  201. i = 0;
  202. fd = open("0123456789012345678901234567890123456789\n0", O_RDONLY);
  203. tmp = get_next_line(fd);
  204.  
  205. while (tmp)
  206. {
  207. printf("%s", tmp);
  208. free(tmp);
  209. tmp = get_next_line(fd);
  210. i++;
  211. }
  212. free(tmp);
  213. }
Success #stdin #stdout 0s 5304KB
stdin
0123456789012345678901234567890123456789
0
stdout
Standard output is empty