Created
September 29, 2022 03:01
-
-
Save viruskizz/771833ba009107430ed8200ce8a91f40 to your computer and use it in GitHub Desktop.
42Exam-Rank03-get_next_line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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