1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-14 20:35:54 +05:30
gists/c-programming/io/pure_getline.c

48 lines
782 B
C
Raw Normal View History

2024-01-29 01:17:01 +05:30
/*
* pure_getline.c
*
* Author: Intel A80486DX2-66
2024-04-26 01:46:39 +05:30
* License: Unlicense
2024-02-20 04:06:11 +05:30
*/
2024-01-29 01:17:01 +05:30
#include "pure_getline.h"
bool pure_getline(char** output) {
/*
2024-02-20 04:06:11 +05:30
* arguments:
* char** output: must be a pointer to an allocated array
*
2024-01-29 01:17:01 +05:30
* return value:
* true: no errors
* false: an error occurred, see errno
*/
if (output == NULL) {
errno = EINVAL;
return false;
}
2024-01-29 01:17:01 +05:30
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;
}
if (character == '\n') {
line[len] = '\0';
break;
}
line[len++] = character;
past_first_time = true;
}
*output = line;
return true;
}