Skip to content

Instantly share code, notes, and snippets.

@giuscri
Created October 6, 2013 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuscri/6854557 to your computer and use it in GitHub Desktop.
Save giuscri/6854557 to your computer and use it in GitHub Desktop.
get_line_from_instream()
char * get_line_from_instream(void) {
char * line = malloc(100), * linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if (line == NULL)
return NULL;
for (;;) {
c = fgetc(stdin);
if (c == EOF)
break;
if (--len == 0) {
len = lenmax;
char * linen = realloc(linep, lenmax *= 2);
if (linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if ((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
@giuscri
Copy link
Author

giuscri commented Oct 6, 2013

For a better understanding of line26 I've found this reference: http://denniskubes.com/2012/08/14/do-you-know-what-p-does-in-c/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment