mirror of
https://gitlab.com/80486DX2-66/gists
synced 2025-05-31 08:31:41 +05:30
C: categorize files, update .gitignore
This commit is contained in:
42
c-programming/io/pure_getline.c
Normal file
42
c-programming/io/pure_getline.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* pure_getline.c
|
||||
*
|
||||
* Author: Intel A80486DX2-66
|
||||
* License: Creative Commons Zero 1.0 Universal
|
||||
*/
|
||||
|
||||
#include "pure_getline.h"
|
||||
|
||||
bool pure_getline(char** output) {
|
||||
/*
|
||||
* arguments:
|
||||
* char** output: must be a pointer to an allocated array
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
|
||||
if (character == '\n') {
|
||||
line[len] = '\0';
|
||||
break;
|
||||
}
|
||||
line[len++] = character;
|
||||
past_first_time = true;
|
||||
}
|
||||
|
||||
*output = line;
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user