In the spirit of hacking together quick answers, here is "sgets" that I just wrote. It attempts to emulate fgets but with string input.
Edit Fixed a bug that Monte pointed out (thanks). Madly typing out a utility while believing that at least 15 other people with the exact same idea are frantically doing the same thing does not lead to well-tested code. Bad me. The original version was including the newline character on the succeeding call.
char *sgets( char * str, int num, char **input )
{
char *next = *input;
int numread = 0;
while ( numread + 1 < num && *next ) {
int isnewline = ( *next == '\n' );
*str++ = *next++;
numread++;
// newline terminates the line but is included
if ( isnewline )
break;
}
if ( numread == 0 )
return NULL; // "eof"
// must have hit the null terminator or end of line
*str = '\0'; // null terminate this tring
// set up input for next call
*input = next;
return str;
}
int main( int argc, char* argv[] )
{
// quick and dirty test
char *str = "abc\ndefghitjklksd\na\n12345\n12345\n123456\nabc\n\n";
char buf[5];
while ( sgets( buf, sizeof( buf ), &str ))
printf( "'%s'\n", buf );
}