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.
13 #include <_PDCLIB_glue.h>
18 size_t fread_unlocked( void * _PDCLIB_restrict ptr,
19 size_t size, size_t nmemb,
20 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
22 if ( _PDCLIB_prepread( stream ) == EOF )
26 char * dest = (char *)ptr;
28 for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
30 size_t numread = _PDCLIB_getchars( &dest[ nmemb_i * size ], size, EOF,
38 size_t fread( void * _PDCLIB_restrict ptr,
39 size_t size, size_t nmemb,
40 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
43 size_t r = fread_unlocked( ptr, size, nmemb, stream );
44 funlockfile( stream );
51 #include <_PDCLIB_test.h>
56 char const * message = "Testing fwrite()...\n";
59 TESTCASE( ( fh = tmpfile() ) != NULL );
60 /* fwrite() / readback */
61 TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
63 TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
64 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
65 TESTCASE( buffer[20] == 'x' );
66 /* same, different nmemb / size settings */
68 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
69 TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
71 TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
72 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
73 TESTCASE( buffer[20] == 'x' );
76 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
77 TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
79 TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
80 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
81 TESTCASE( buffer[20] == 'x' );
83 TESTCASE( fclose( fh ) == 0 );