Skip to content

Instantly share code, notes, and snippets.

@viruskizz
Created September 29, 2022 03:01
Show Gist options
  • Save viruskizz/771833ba009107430ed8200ce8a91f40 to your computer and use it in GitHub Desktop.
Save viruskizz/771833ba009107430ed8200ce8a91f40 to your computer and use it in GitHub Desktop.
42Exam-Rank03-get_next_line
#include <stdlib.h>
#include <unistd.h>
char *get_next_line(int fd)
{
if (BUFFER_SIZE < 1 || fd < 0 || read(fd, NULL, 0) < 0) { return (NULL); }
char *line = malloc(100000), *buf = line;
while (read(fd, buf, 1) > 0 && *buf++ != '\n');
return (buf > line) ? (*buf = 0, line) : (free(line), NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment