3 /* fwrite( void *, size_t, size_t, FILE * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
12 #include <_PDCLIB_io.h>
17 size_t _PDCLIB_fread_unlocked(
18 void * _PDCLIB_restrict ptr,
19 size_t size, size_t nmemb,
20 FILE * _PDCLIB_restrict stream
23 if ( _PDCLIB_prepread( stream ) == EOF )
27 char * dest = (char *)ptr;
29 for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
31 size_t numread = _PDCLIB_getchars( &dest[ nmemb_i * size ], size, EOF,
39 size_t fread( void * _PDCLIB_restrict ptr,
40 size_t size, size_t nmemb,
41 FILE * _PDCLIB_restrict stream )
43 _PDCLIB_flockfile( stream );
44 size_t r = _PDCLIB_fread_unlocked( ptr, size, nmemb, stream );
45 _PDCLIB_funlockfile( stream );
52 #include <_PDCLIB_test.h>
57 char const * message = "Testing fwrite()...\n";
60 TESTCASE( ( fh = tmpfile() ) != NULL );
61 /* fwrite() / readback */
62 TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
64 TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
65 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
66 TESTCASE( buffer[20] == 'x' );
67 /* same, different nmemb / size settings */
69 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
70 TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
72 TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
73 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
74 TESTCASE( buffer[20] == 'x' );
77 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
78 TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
80 TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
81 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
82 TESTCASE( buffer[20] == 'x' );
84 TESTCASE( fclose( fh ) == 0 );