Created
October 6, 2013 14:08
-
-
Save giuscri/6854557 to your computer and use it in GitHub Desktop.
get_line_from_instream()
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/