Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by nullPointer ( 17 years ago )
/*
* safe version of gets() function
* returns one line from FILE* or NULL on case of EOF or out of memory
*/
char* sgets(FILE* f) {
int bufsize = 10; // initial size of buffer
int gsize = 5; // grow step
char *buf;
if (!(buf = malloc(bufsize))) {
return NULL; // out of mem
}
char* ret = fgets(buf, bufsize, f);
if (!ret) {
return NULL; // EOF
}
while (strchr(buf, '\n') == 0) {
bufsize += gsize;
buf = realloc(buf, bufsize);
if (!buf) {
return NULL; // out of mem
}
char* p = buf + bufsize - gsize - 1; // pointer to prev str end ('\0')
fgets(p, gsize + 1, f); // EOF impossible; +1 because of additional \0 from prev fgets
}
return buf;
}
Revise this Paste