From acb574418b603006718fc8e9584808b8757d7c0b Mon Sep 17 00:00:00 2001 From: solar Date: Fri, 3 Dec 2010 05:59:00 +0000 Subject: [PATCH] Test driver (and much-needed fixes) to fgets(). --- functions/stdio/fgets.c | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index a3c41e9..1afd79f 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -15,9 +15,12 @@ char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { - if ( size <= 1 ) + if ( size == 0 ) + { + return NULL; + } + if ( size == 1 ) { - /* TODO: This is the letter of the standard, but is it the right thing to do? */ *s = '\0'; return s; } @@ -26,33 +29,60 @@ char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCL return NULL; } char * dest = s; - while ( ( ( *dest = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 ) + while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 ) { if ( stream->bufidx == stream->bufend ) { if ( _PDCLIB_fillbuffer( stream ) == EOF ) { - /* EOF adds \0, error leaves target indeterminate, so we can - just add the \0 anyway. + /* In case of error / EOF before a character is read, this + will lead to a \0 be written anyway. Since the results + are "indeterminate" by definition, this does not hurt. */ - *dest = '\0'; - return NULL; + break; } } - ++dest; } *dest = '\0'; - return s; + return ( dest == s ) ? NULL : s; } #endif #ifdef TEST #include <_PDCLIB_test.h> +#include int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * fh; + char buffer[10]; + char const * fgets_test = "foo\nbar\0baz\nweenie"; + TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL ); + TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 ); + rewind( fh ); + TESTCASE( fgets( buffer, 10, fh ) == buffer ); + TESTCASE( strcmp( buffer, "foo\n" ) == 0 ); + TESTCASE( fgets( buffer, 10, fh ) == buffer ); + TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 ); + TESTCASE( fgets( buffer, 10, fh ) == buffer ); + TESTCASE( strcmp( buffer, "weenie" ) == 0 ); + TESTCASE( feof( fh ) ); + TESTCASE( fseek( fh, -1, SEEK_END ) == 0 ); + TESTCASE( fgets( buffer, 1, fh ) == buffer ); + TESTCASE( strcmp( buffer, "" ) == 0 ); + TESTCASE( fgets( buffer, 0, fh ) == NULL ); + TESTCASE( ! feof( fh ) ); + TESTCASE( fgets( buffer, 1, fh ) == buffer ); + TESTCASE( strcmp( buffer, "" ) == 0 ); + TESTCASE( ! feof( fh ) ); + TESTCASE( fgets( buffer, 2, fh ) == buffer ); + TESTCASE( strcmp( buffer, "e" ) == 0 ); + TESTCASE( fseek( fh, 0, SEEK_END ) == 0 ); + TESTCASE( fgets( buffer, 2, fh ) == NULL ); + TESTCASE( feof( fh ) ); + TESTCASE( fclose( fh ) == 0 ); + remove( testfile ); return TEST_RESULTS; } -- 2.40.0