X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Fclearerr.c;h=e569fd4085382801f8b34dedc20e8863e718461d;hp=4c51d714f54aefb4c019a6b8c216bd6820d19634;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=f603f88b4456cf9b7ab1328bf657ede22a0c9940 diff --git a/functions/stdio/clearerr.c b/functions/stdio/clearerr.c index 4c51d71..e569fd4 100644 --- a/functions/stdio/clearerr.c +++ b/functions/stdio/clearerr.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* clearerr( FILE * ) This file is part of the Public Domain C Library (PDCLib). @@ -9,50 +7,53 @@ #include #ifndef REGTEST +#include "_PDCLIB_io.h" -void clearerr( struct _PDCLIB_file_t * stream ) +void _PDCLIB_clearerr_unlocked( FILE * stream ) { stream->status &= ~( _PDCLIB_ERRORFLAG | _PDCLIB_EOFFLAG ); } +void clearerr( FILE * stream ) +{ + _PDCLIB_flockfile( stream ); + _PDCLIB_clearerr_unlocked( stream ); + _PDCLIB_funlockfile( stream ); +} + #endif #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" int main( void ) { -#ifndef REGTEST - FILE file = { 0, { 0 }, NULL, 0, 0, 0, NULL }; - FILE * fh = &file; + FILE * fh; + TESTCASE( ( fh = tmpfile() ) != NULL ); + /* Flags should be clear */ TESTCASE( ! ferror( fh ) ); TESTCASE( ! feof( fh ) ); - fh->status |= _PDCLIB_ERRORFLAG; + /* Reading from input stream - should provoke error */ + /* FIXME: Apparently glibc disagrees on this assumption. How to provoke error on glibc? */ + TESTCASE( fgetc( fh ) == EOF ); TESTCASE( ferror( fh ) ); TESTCASE( ! feof( fh ) ); + /* clearerr() should clear flags */ clearerr( fh ); TESTCASE( ! ferror( fh ) ); TESTCASE( ! feof( fh ) ); - fh->status |= _PDCLIB_EOFFLAG; - TESTCASE( ! ferror( fh ) ); - TESTCASE( feof( fh ) ); - clearerr( fh ); + /* Reading from empty stream - should provoke EOF */ + rewind( fh ); + TESTCASE( fgetc( fh ) == EOF ); TESTCASE( ! ferror( fh ) ); - TESTCASE( ! feof( fh ) ); - fh->status |= _PDCLIB_EOFFLAG | _PDCLIB_ERRORFLAG; - TESTCASE( ferror( fh ) ); TESTCASE( feof( fh ) ); + /* clearerr() should clear flags */ clearerr( fh ); TESTCASE( ! ferror( fh ) ); TESTCASE( ! feof( fh ) ); -#else - /* TODO: The above is ad-hoc and PDCLib specific. A better test driver - should be internals-agnostic (provoking the error / eof flag by - "regular" operations). - */ - TESTCASE( NO_TESTDRIVER ); -#endif + TESTCASE( fclose( fh ) == 0 ); return TEST_RESULTS; } #endif +