X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Fclearerr.c;h=a80e3e2987a1b727643717a5857c0cc8e48cd6d9;hb=b5b6c4a890795ea76f9b92b817b0a83c6bb4862c;hp=4c51d714f54aefb4c019a6b8c216bd6820d19634;hpb=52e702f02142dc34a476f01ca6bf8257cc9bc7e4;p=pdclib.old diff --git a/functions/stdio/clearerr.c b/functions/stdio/clearerr.c index 4c51d71..a80e3e2 100644 --- a/functions/stdio/clearerr.c +++ b/functions/stdio/clearerr.c @@ -9,12 +9,20 @@ #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 @@ -22,37 +30,32 @@ void clearerr( struct _PDCLIB_file_t * stream ) 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; + /* Reading from empty stream - should provoke EOF */ + rewind( fh ); + 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 | _PDCLIB_ERRORFLAG; - TESTCASE( ferror( fh ) ); - TESTCASE( feof( fh ) ); - 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 +