1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-25 17:19:46 +05:30
gists/c-programming/pure_getline.c

49 lines
815 B
C

/*
* pure_getline.c
*
* Author: Intel A80486DX2-66
* License: Creative Commons Zero 1.0 Universal
*/
#include "pure_getline.h"
bool pure_getline(char** output) {
/*
* return value:
* true: no errors
* false: an error occurred, see errno
*/
char* line = NULL;
size_t len = 0;
int character;
bool past_first_time = false;
while ((character = fgetc(stdin)) != EOF) {
if (past_first_time && len == 0) { // check for integer overflow
errno = ERANGE;
*output = NULL;
return false;
}
char* new_line = realloc(line, (len + 1) * sizeof(char));
if (new_line == NULL) {
*output = line;
return false;
}
line = new_line;
if (character == '\n') {
line[len] = '\0';
break;
}
line[len++] = character;
past_first_time = true;
}
*output = line;
return true;
}