X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Ffgets.c;h=8752892bedc1a26103fe375089a5fff82b4b111b;hp=a3c41e94085eb3f8fa610d57c926dfa8115c36ef;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=e9a6658165caf38491a04d17a04accec11a3696c diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index a3c41e9..8752892 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* fgets( char *, int, FILE * ) This file is part of the Public Domain C Library (PDCLib). @@ -9,15 +7,16 @@ #include #ifndef REGTEST +#include "_PDCLIB_io.h" -#define _PDCLIB_GLUE_H _PDCLIB_GLUE_H -#include <_PDCLIB_glue.h> - -char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +char * _PDCLIB_fgets_unlocked( char * _PDCLIB_restrict s, int size, FILE * _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 +25,58 @@ 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 ) - { - 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. - */ - *dest = '\0'; - return NULL; - } - } - ++dest; - } + + dest += _PDCLIB_getchars( dest, size - 1, '\n', stream ); + *dest = '\0'; - return s; + return ( dest == s ) ? NULL : s; +} + +char * fgets( char * _PDCLIB_restrict s, int size, + FILE * _PDCLIB_restrict stream ) +{ + _PDCLIB_flockfile( stream ); + char* r = _PDCLIB_fgets_unlocked( s, size, stream ); + _PDCLIB_funlockfile( stream ); + return r; } #endif #ifdef TEST -#include <_PDCLIB_test.h> +#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 ); + TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; }